1
0

nc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #define RECV_BUFSIZE 8192
  27. static char recvbuf[RECV_BUFSIZE];
  28. // 1:tcp 2:udp
  29. int protocol = 1;
  30. // for stdin
  31. hio_t* stdinio = NULL;
  32. // for socket
  33. hio_t* sockio = NULL;
  34. int verbose = 0;
  35. static void send_heartbeat(hio_t* io) {
  36. static char buf[] = "PING\r\n";
  37. // printf("send_heartbeat %s", buf);
  38. hio_write(io, buf, 6);
  39. }
  40. static void on_recv(hio_t* io, void* buf, int readbytes) {
  41. // printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  42. if (verbose) {
  43. char localaddrstr[SOCKADDR_STRLEN] = {0};
  44. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  45. printf("[%s] <=> [%s]\n",
  46. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  47. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  48. }
  49. printf("%.*s", readbytes, (char*)buf);
  50. // test hio_set_readbuf in hread_cb
  51. #if 0
  52. static int total_readbytes = 0;
  53. total_readbytes += readbytes;
  54. if (total_readbytes >= RECV_BUFSIZE) {
  55. total_readbytes = 0;
  56. }
  57. hio_set_readbuf(io, recvbuf + total_readbytes, RECV_BUFSIZE - total_readbytes);
  58. printf("%.*s", total_readbytes, recvbuf);
  59. #endif
  60. fflush(stdout);
  61. }
  62. static void on_stdin(hio_t* io, void* buf, int readbytes) {
  63. // printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  64. // printf("> %s\n", buf);
  65. char* str = (char*)buf;
  66. // test hio_read_start/hio_read_stop/hio_close/hloop_stop
  67. #if 1
  68. if (strncmp(str, "start", 5) == 0) {
  69. printf("call hio_read_start\n");
  70. hio_read_start(sockio);
  71. return;
  72. }
  73. else if (strncmp(str, "stop", 4) == 0) {
  74. printf("call hio_read_stop\n");
  75. hio_read_stop(sockio);
  76. return;
  77. }
  78. else if (strncmp(str, "close", 5) == 0) {
  79. printf("call hio_close\n");
  80. hio_close(sockio);
  81. return;
  82. }
  83. else if (strncmp(str, "quit", 4) == 0) {
  84. printf("call hloop_stop\n");
  85. hloop_stop(hevent_loop(io));
  86. return;
  87. }
  88. #endif
  89. // CR|LF => CRLF for test most protocols
  90. char eol = str[readbytes-1];
  91. if (eol == '\n' || eol == '\r') {
  92. if (readbytes > 1 && str[readbytes-2] == '\r' && eol == '\n') {
  93. // have been CRLF
  94. }
  95. else {
  96. ++readbytes;
  97. str[readbytes - 2] = '\r';
  98. str[readbytes - 1] = '\n';
  99. }
  100. }
  101. hio_write(sockio, buf, readbytes);
  102. }
  103. static void on_close(hio_t* io) {
  104. // printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  105. hio_del(stdinio, HV_READ);
  106. }
  107. static void on_connect(hio_t* io) {
  108. // printf("on_connect fd=%d\n", hio_fd(io));
  109. if (verbose) {
  110. char localaddrstr[SOCKADDR_STRLEN] = {0};
  111. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  112. printf("connect connfd=%d [%s] => [%s]\n", hio_fd(io),
  113. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  114. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  115. }
  116. hio_read_start(io);
  117. // uncomment to test heartbeat
  118. // hio_set_heartbeat(sockio, 3000, send_heartbeat);
  119. }
  120. int main(int argc, char** argv) {
  121. if (argc < 3) {
  122. printf("\
  123. Usage: nc [-ut] host port\n\
  124. Options:\n\
  125. -t Use tcp protocol (default)\n\
  126. -u Use udp protocol\n\
  127. Examples: nc 127.0.0.1 80\n\
  128. nc -u 127.0.0.1 80\n");
  129. return -10;
  130. }
  131. int index = 1;
  132. const char* protocolname;
  133. if (argv[1][0] == '-') {
  134. ++index;
  135. if (argv[1][1] == 't') {
  136. protocol = 1;
  137. protocolname = "tcp";
  138. }
  139. else if (argv[1][1] == 'u') {
  140. protocol = 2;
  141. protocolname = "udp";
  142. }
  143. }
  144. const char* host = argv[index++];
  145. int port = atoi(argv[index++]);
  146. if (verbose) {
  147. printf("%s %s %d\n", protocolname, host, port);
  148. }
  149. HV_MEMCHECK;
  150. hloop_t* loop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS);
  151. // stdin use default readbuf
  152. stdinio = hread(loop, 0, NULL, 0, on_stdin);
  153. if (stdinio == NULL) {
  154. return -20;
  155. }
  156. // socket
  157. if (protocol == 1) {
  158. #if TEST_SSL
  159. // ssl
  160. sockio = hloop_create_ssl_client(loop, host, port, on_connect);
  161. #else
  162. // tcp
  163. sockio = hloop_create_tcp_client(loop, host, port, on_connect);
  164. #endif
  165. }
  166. else if (protocol == 2) {
  167. // udp
  168. sockio = hloop_create_udp_client(loop, host, port);
  169. hio_read(sockio);
  170. }
  171. if (sockio == NULL) {
  172. return -20;
  173. }
  174. // printf("sockfd=%d\n", hio_fd(sockio));
  175. hio_setcb_close(sockio, on_close);
  176. hio_setcb_read(sockio, on_recv);
  177. hio_set_readbuf(sockio, recvbuf, RECV_BUFSIZE);
  178. hloop_run(loop);
  179. hloop_free(&loop);
  180. return 0;
  181. }