nc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "hloop.h"
  2. #include "hbase.h"
  3. #include "hsocket.h"
  4. #define RECV_BUFSIZE 8192
  5. static char recvbuf[RECV_BUFSIZE];
  6. // 1:tcp 2:udp
  7. int protocol = 1;
  8. // for stdin
  9. hio_t* stdinio = NULL;
  10. // for socket
  11. hio_t* sockio = NULL;
  12. int verbose = 0;
  13. void on_recv(hio_t* io, void* buf, int readbytes) {
  14. //printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  15. if (verbose) {
  16. char localaddrstr[SOCKADDR_STRLEN] = {0};
  17. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  18. printf("[%s] <=> [%s]\n",
  19. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  20. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  21. }
  22. printf("%s", (char*)buf);
  23. fflush(stdout);
  24. }
  25. void on_stdin(hio_t* io, void* buf, int readbytes) {
  26. //printf("on_stdin fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  27. //printf("> %s\n", buf);
  28. // CR|LF => CRLF for test most protocols
  29. char* str = (char*)buf;
  30. char eol = str[readbytes-1];
  31. if (eol == '\n' || eol == '\r') {
  32. if (readbytes > 1 && str[readbytes-2] == '\r' && eol == '\n') {
  33. // have been CRLF
  34. }
  35. else {
  36. ++readbytes;
  37. str[readbytes - 2] = '\r';
  38. str[readbytes - 1] = '\n';
  39. }
  40. }
  41. hio_write(sockio, buf, readbytes);
  42. }
  43. void on_close(hio_t* io) {
  44. //printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  45. hio_del(stdinio, READ_EVENT);
  46. }
  47. void on_connect(hio_t* io) {
  48. //printf("on_connect fd=%d\n", hio_fd(io));
  49. if (verbose) {
  50. char localaddrstr[SOCKADDR_STRLEN] = {0};
  51. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  52. printf("connect connfd=%d [%s] => [%s]\n", hio_fd(io),
  53. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  54. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  55. }
  56. hio_read(io);
  57. }
  58. int main(int argc, char** argv) {
  59. if (argc < 3) {
  60. printf("\
  61. Usage: cmd [-ut] host port\n\
  62. Options:\n\
  63. -t Use tcp protocol (default)\n\
  64. -u Use udp protocol\n\
  65. Examples: nc 127.0.0.1 80\n\
  66. nc -u 127.0.0.1 80\n");
  67. return -10;
  68. }
  69. int index = 1;
  70. const char* protocolname;
  71. if (argv[1][0] == '-') {
  72. ++index;
  73. if (argv[1][1] == 't') {
  74. protocol = 1;
  75. protocolname = "tcp";
  76. }
  77. else if (argv[1][1] == 'u') {
  78. protocol = 2;
  79. protocolname = "udp";
  80. }
  81. }
  82. const char* host = argv[index++];
  83. int port = atoi(argv[index++]);
  84. if (verbose) {
  85. printf("%s %s %d\n", protocolname, host, port);
  86. }
  87. MEMCHECK;
  88. hloop_t* loop = hloop_new(0);
  89. // stdin
  90. stdinio = hread(loop, 0, recvbuf, RECV_BUFSIZE, on_stdin);
  91. if (stdinio == NULL) {
  92. return -20;
  93. }
  94. // socket
  95. if (protocol == 1) {
  96. // tcp
  97. sockio = create_tcp_client(loop, host, port, on_connect);
  98. }
  99. else if (protocol == 2) {
  100. // udp
  101. sockio = create_udp_client(loop, host, port);
  102. hio_read(sockio);
  103. }
  104. if (sockio == NULL) {
  105. return -20;
  106. }
  107. //printf("sockfd=%d\n", hio_fd(sockio));
  108. hio_setcb_close(sockio, on_close);
  109. hio_setcb_read(sockio, on_recv);
  110. hio_set_readbuf(sockio, recvbuf, RECV_BUFSIZE);
  111. hloop_run(loop);
  112. hloop_free(&loop);
  113. return 0;
  114. }