nc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * network client
  3. *
  4. * @build: make examples
  5. * @server bin/httpd -s restart -d
  6. * @usage: bin/nc 127.0.0.1 8080
  7. * > GET / HTTP/1.1
  8. * > Connection: close
  9. * > [Enter]
  10. * > GET / HTTP/1.1
  11. * > Connection: keep-alive
  12. * > [Enter]
  13. */
  14. #include "hloop.h"
  15. #include "hbase.h"
  16. #include "hsocket.h"
  17. #include "hssl.h"
  18. /*
  19. * @test ssl_client
  20. * #define TEST_SSL 1
  21. *
  22. * @build ./configure --with-openssl && make clean && make
  23. *
  24. */
  25. #define TEST_SSL 0
  26. /*
  27. * @test kcp_client
  28. * #define TEST_KCP 1
  29. *
  30. * @build ./configure --with-kcp && make clean && make
  31. * @server bin/udp_echo_server 1234
  32. * @client bin/nc -u 127.0.0.1 1234
  33. *
  34. */
  35. #define TEST_KCP 0
  36. #define RECV_BUFSIZE 8192
  37. static char recvbuf[RECV_BUFSIZE];
  38. // 1:tcp 2:udp
  39. int protocol = 1;
  40. // for stdin
  41. hio_t* stdinio = NULL;
  42. // for socket
  43. hio_t* sockio = NULL;
  44. int verbose = 0;
  45. static void send_heartbeat(hio_t* io) {
  46. static char buf[] = "PING\r\n";
  47. // printf("send_heartbeat %s", buf);
  48. hio_write(io, buf, 6);
  49. }
  50. static void on_recv(hio_t* io, void* buf, int readbytes) {
  51. // printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  52. if (verbose) {
  53. char localaddrstr[SOCKADDR_STRLEN] = {0};
  54. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  55. printf("[%s] <=> [%s]\n",
  56. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  57. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  58. }
  59. printf("%.*s", readbytes, (char*)buf);
  60. // test hio_set_readbuf in hread_cb
  61. #if 0
  62. static int total_readbytes = 0;
  63. total_readbytes += readbytes;
  64. if (total_readbytes >= RECV_BUFSIZE) {
  65. total_readbytes = 0;
  66. }
  67. hio_set_readbuf(io, recvbuf + total_readbytes, RECV_BUFSIZE - total_readbytes);
  68. printf("%.*s", total_readbytes, recvbuf);
  69. #endif
  70. fflush(stdout);
  71. }
  72. static void on_stdin(hio_t* io, void* buf, int readbytes) {
  73. // printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  74. // printf("> %s\n", buf);
  75. char* str = (char*)buf;
  76. // test hio_read_start/hio_read_stop/hio_close/hloop_stop
  77. #if 1
  78. if (strncmp(str, "start", 5) == 0) {
  79. printf("call hio_read_start\n");
  80. hio_read_start(sockio);
  81. return;
  82. }
  83. else if (strncmp(str, "stop", 4) == 0) {
  84. printf("call hio_read_stop\n");
  85. hio_read_stop(sockio);
  86. return;
  87. }
  88. else if (strncmp(str, "close", 5) == 0) {
  89. printf("call hio_close\n");
  90. hio_close(sockio);
  91. return;
  92. }
  93. else if (strncmp(str, "quit", 4) == 0) {
  94. printf("call hloop_stop\n");
  95. hloop_stop(hevent_loop(io));
  96. return;
  97. }
  98. #endif
  99. // CR|LF => CRLF for test most protocols
  100. char eol = str[readbytes-1];
  101. if (eol == '\n' || eol == '\r') {
  102. if (readbytes > 1 && str[readbytes-2] == '\r' && eol == '\n') {
  103. // have been CRLF
  104. }
  105. else {
  106. ++readbytes;
  107. str[readbytes - 2] = '\r';
  108. str[readbytes - 1] = '\n';
  109. }
  110. }
  111. hio_write(sockio, buf, readbytes);
  112. #if TEST_KCP
  113. if (strncmp(str, "CLOSE", 5) == 0) {
  114. printf("call hio_close\n");
  115. hio_close(sockio);
  116. }
  117. #endif
  118. }
  119. static void on_close(hio_t* io) {
  120. // printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  121. hio_del(stdinio, HV_READ);
  122. }
  123. static void on_connect(hio_t* io) {
  124. // printf("on_connect fd=%d\n", hio_fd(io));
  125. if (verbose) {
  126. char localaddrstr[SOCKADDR_STRLEN] = {0};
  127. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  128. printf("connect connfd=%d [%s] => [%s]\n", hio_fd(io),
  129. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  130. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  131. }
  132. hio_read_start(io);
  133. // uncomment to test heartbeat
  134. // hio_set_heartbeat(sockio, 3000, send_heartbeat);
  135. }
  136. int main(int argc, char** argv) {
  137. if (argc < 3) {
  138. printf("\
  139. Usage: nc [-ut] host port\n\
  140. Options:\n\
  141. -t Use tcp protocol (default)\n\
  142. -u Use udp protocol\n\
  143. Examples: nc 127.0.0.1 80\n\
  144. nc -u 127.0.0.1 80\n");
  145. return -10;
  146. }
  147. int index = 1;
  148. const char* protocolname;
  149. if (argv[1][0] == '-') {
  150. ++index;
  151. if (argv[1][1] == 't') {
  152. protocol = 1;
  153. protocolname = "tcp";
  154. }
  155. else if (argv[1][1] == 'u') {
  156. protocol = 2;
  157. protocolname = "udp";
  158. }
  159. }
  160. const char* host = argv[index++];
  161. int port = atoi(argv[index++]);
  162. if (verbose) {
  163. printf("%s %s %d\n", protocolname, host, port);
  164. }
  165. HV_MEMCHECK;
  166. hloop_t* loop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS);
  167. // stdin use default readbuf
  168. stdinio = hread(loop, 0, NULL, 0, on_stdin);
  169. if (stdinio == NULL) {
  170. return -20;
  171. }
  172. // socket
  173. if (protocol == 1) {
  174. #if TEST_SSL
  175. // ssl
  176. sockio = hloop_create_ssl_client(loop, host, port, on_connect);
  177. #else
  178. // tcp
  179. sockio = hloop_create_tcp_client(loop, host, port, on_connect);
  180. #endif
  181. }
  182. else if (protocol == 2) {
  183. // udp
  184. sockio = hloop_create_udp_client(loop, host, port);
  185. #if TEST_KCP
  186. hio_set_kcp(sockio, NULL);
  187. #endif
  188. hio_read(sockio);
  189. }
  190. if (sockio == NULL) {
  191. return -20;
  192. }
  193. // printf("sockfd=%d\n", hio_fd(sockio));
  194. hio_setcb_close(sockio, on_close);
  195. hio_setcb_read(sockio, on_recv);
  196. hio_set_readbuf(sockio, recvbuf, RECV_BUFSIZE);
  197. hloop_run(loop);
  198. hloop_free(&loop);
  199. return 0;
  200. }