1
0

nc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 udp
  11. struct sockaddr* peeraddr = NULL;
  12. socklen_t peeraddrlen = sizeof(struct sockaddr_in6);
  13. // for tcp
  14. hio_t* sockio = NULL;
  15. int verbose = 0;
  16. void on_recv(hio_t* io, void* buf, int readbytes) {
  17. //printf("on_recv fd=%d readbytes=%d\n", io->fd, readbytes);
  18. if (verbose) {
  19. char localaddrstr[INET6_ADDRSTRLEN+16] = {0};
  20. char peeraddrstr[INET6_ADDRSTRLEN+16] = {0};
  21. printf("[%s] <=> [%s]\n",
  22. sockaddr_snprintf(io->localaddr, localaddrstr, sizeof(localaddrstr)),
  23. sockaddr_snprintf(io->peeraddr, peeraddrstr, sizeof(peeraddrstr)));
  24. }
  25. printf("%s", (char*)buf);
  26. fflush(stdout);
  27. }
  28. void on_stdin(hio_t* io, void* buf, int readbytes) {
  29. //printf("on_stdin fd=%d readbytes=%d\n", io->fd, readbytes);
  30. //printf("> %s\n", buf);
  31. if (protocol == 1) {
  32. hsend(sockio->loop, sockio->fd, buf, readbytes, NULL);
  33. }
  34. else if (protocol == 2) {
  35. hsendto(sockio->loop, sockio->fd, buf, readbytes, NULL);
  36. hrecvfrom(sockio->loop, sockio->fd, recvbuf, RECV_BUFSIZE, on_recv);
  37. }
  38. }
  39. void on_close(hio_t* io) {
  40. //printf("on_close fd=%d error=%d\n", io->fd, io->error);
  41. hio_del(stdinio, READ_EVENT);
  42. }
  43. void on_connect(hio_t* io, int state) {
  44. //printf("on_connect fd=%d state=%d\n", io->fd, state);
  45. if (state == 0) {
  46. printf("connect failed: %d: %s\n", io->error, strerror(io->error));
  47. return;
  48. }
  49. if (verbose) {
  50. char localaddrstr[INET6_ADDRSTRLEN+16] = {0};
  51. char peeraddrstr[INET6_ADDRSTRLEN+16] = {0};
  52. printf("connect connfd=%d [%s] => [%s]\n", io->fd,
  53. sockaddr_snprintf(io->localaddr, localaddrstr, sizeof(localaddrstr)),
  54. sockaddr_snprintf(io->peeraddr, peeraddrstr, sizeof(peeraddrstr)));
  55. }
  56. hrecv(io->loop, io->fd, recvbuf, RECV_BUFSIZE, on_recv);
  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;
  89. hloop_init(&loop);
  90. // stdin
  91. stdinio = hread(&loop, 0, recvbuf, RECV_BUFSIZE, on_stdin);
  92. if (stdinio == NULL) {
  93. return -20;
  94. }
  95. // socket
  96. if (protocol == 1) {
  97. // tcp
  98. sockio = create_tcp_client(&loop, host, port, on_connect);
  99. }
  100. else if (protocol == 2) {
  101. // udp
  102. sockio = create_udp_client(&loop, host, port);
  103. }
  104. if (sockio == NULL) {
  105. return -20;
  106. }
  107. //printf("sockfd=%d\n", sockio->fd);
  108. sockio->close_cb = on_close;
  109. hloop_run(&loop);
  110. return 0;
  111. }