1
0

nc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /*
  15. * @test udp client
  16. * @build ./configure && make examples
  17. * @client bin/nc -u 127.0.0.1 1234
  18. *
  19. */
  20. /*
  21. * @test ssl client
  22. * @build ./configure --with-openssl && make clean && make
  23. * @client bin/nc -s 127.0.0.1 1234
  24. *
  25. */
  26. /*
  27. * @test kcp client
  28. * @build ./configure --with-kcp && make clean && make
  29. * @client bin/nc -k 127.0.0.1 1234
  30. *
  31. */
  32. #include "hloop.h"
  33. #include "hbase.h"
  34. #include "hsocket.h"
  35. #include "hssl.h"
  36. #define RECV_BUFSIZE 8192
  37. static char recvbuf[RECV_BUFSIZE];
  38. static char protocol = 't';
  39. static const char* protocolname = "tcp";
  40. // for stdin
  41. static hio_t* stdinio = NULL;
  42. // for socket
  43. static hio_t* sockio = NULL;
  44. static 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 (strncmp(str, "CLOSE", 5) == 0) {
  113. printf("call hio_close\n");
  114. hio_close(sockio);
  115. }
  116. }
  117. static void on_close(hio_t* io) {
  118. // printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  119. hio_del(stdinio, HV_READ);
  120. }
  121. static void on_connect(hio_t* io) {
  122. // printf("on_connect fd=%d\n", hio_fd(io));
  123. if (verbose) {
  124. char localaddrstr[SOCKADDR_STRLEN] = {0};
  125. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  126. printf("connect connfd=%d [%s] => [%s]\n", hio_fd(io),
  127. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  128. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  129. }
  130. hio_read_start(io);
  131. // uncomment to test heartbeat
  132. // hio_set_heartbeat(sockio, 3000, send_heartbeat);
  133. }
  134. int main(int argc, char** argv) {
  135. if (argc < 3) {
  136. printf("\
  137. Usage: nc [-tusk] host port\n\
  138. Options:\n\
  139. -t Use tcp protocol (default)\n\
  140. -u Use udp protocol\n\
  141. -s Use ssl protocol\n\
  142. -k Use kcp 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. if (argv[1][0] == '-') {
  149. protocol = argv[1][1];
  150. switch(protocol) {
  151. case 't': protocolname = "tcp"; break;
  152. case 'u': protocolname = "udp"; break;
  153. case 's': protocolname = "ssl"; break;
  154. case 'k': protocolname = "kcp"; break;
  155. default: fprintf(stderr, "Unsupported protocol '%c'\n", protocol); exit(1);
  156. }
  157. ++index;
  158. }
  159. const char* host = argv[index++];
  160. int port = atoi(argv[index++]);
  161. if (verbose) {
  162. printf("%s %s %d\n", protocolname, host, port);
  163. }
  164. HV_MEMCHECK;
  165. hloop_t* loop = hloop_new(HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS);
  166. // stdin use default readbuf
  167. stdinio = hread(loop, 0, NULL, 0, on_stdin);
  168. if (stdinio == NULL) {
  169. return -20;
  170. }
  171. // socket
  172. if (protocol == 't' || protocol == 's') {
  173. // tcp
  174. sockio = hloop_create_tcp_client(loop, host, port, on_connect, on_close);
  175. if (sockio == NULL) {
  176. return -20;
  177. }
  178. if (protocol == 's') {
  179. if (strcmp(hssl_backend(), "nossl") == 0) {
  180. fprintf(stderr, "Please recompile WITH_SSL!\n");
  181. exit(1);
  182. }
  183. hio_enable_ssl(sockio);
  184. }
  185. }
  186. else if (protocol == 'u' || protocol == 'k') {
  187. // udp
  188. sockio = hloop_create_udp_client(loop, host, port);
  189. if (sockio == NULL) {
  190. return -20;
  191. }
  192. if (protocol == 'k') {
  193. #if WITH_KCP
  194. static kcp_setting_t s_kcp_setting;
  195. kcp_setting_init_with_normal_mode(&s_kcp_setting);
  196. s_kcp_setting.conv = hv_rand(1, 999999);
  197. hio_set_kcp(sockio, &s_kcp_setting);
  198. #else
  199. fprintf(stderr, "Please recompile WITH_KCP!\n");
  200. exit(1);
  201. #endif
  202. }
  203. hio_read(sockio);
  204. }
  205. // printf("sockfd=%d\n", hio_fd(sockio));
  206. hio_setcb_close(sockio, on_close);
  207. hio_setcb_read(sockio, on_recv);
  208. hio_set_readbuf(sockio, recvbuf, RECV_BUFSIZE);
  209. hloop_run(loop);
  210. hloop_free(&loop);
  211. return 0;
  212. }