netcat.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* netcat.c - Forward stdin/stdout to a file or network connection.
  2. *
  3. * Copyright 2007 Rob Landley <rob@landley.net>
  4. *
  5. * TODO: udp, ipv6, genericize for telnet/microcom/tail-f
  6. * fix -t, xconnect
  7. * netcat -L zombies
  8. USE_NETCAT(OLDTOY(nc, netcat, TOYFLAG_USR|TOYFLAG_BIN))
  9. USE_NETCAT(NEWTOY(netcat, USE_NETCAT_LISTEN("^tElL")"w#<1W#<1p#<1>65535q#<1s:f:46uU"USE_NETCAT_LISTEN("[!tlL][!Lw]")"[!46U]", TOYFLAG_BIN))
  10. config NETCAT
  11. bool "netcat"
  12. default y
  13. help
  14. usage: netcat [-46U] [-u] [-wpq #] [-s addr] {IPADDR PORTNUM|-f FILENAME|COMMAND...}
  15. Forward stdin/stdout to a file or network connection.
  16. -4 Force IPv4
  17. -6 Force IPv6
  18. -f Use FILENAME (ala /dev/ttyS0) instead of network
  19. -p Local port number
  20. -q Quit SECONDS after EOF on stdin, even if stdout hasn't closed yet
  21. -s Local source address
  22. -u Use UDP
  23. -U Use a UNIX domain socket
  24. -w SECONDS timeout to establish connection
  25. -W SECONDS timeout for more data on an idle connection
  26. Use "stty 115200 -F /dev/ttyS0 && stty raw -echo -ctlecho" with
  27. netcat -f to connect to a serial port.
  28. config NETCAT_LISTEN
  29. bool "netcat server options (-let)"
  30. default y
  31. depends on NETCAT
  32. help
  33. usage: netcat [-tElL]
  34. -l Listen for one incoming connection, then exit
  35. -L Listen and background each incoming connection (server mode)
  36. -t Allocate tty
  37. -E Forward stderr
  38. When listening the COMMAND line is executed as a child process to handle
  39. an incoming connection. With no COMMAND -l forwards the connection
  40. to stdin/stdout. If no -p specified, -l prints the port it bound to and
  41. backgrounds itself (returning immediately).
  42. For a quick-and-dirty server, try something like:
  43. netcat -s 127.0.0.1 -p 1234 -tL sh -l
  44. */
  45. #define FOR_netcat
  46. #include "toys.h"
  47. GLOBALS(
  48. char *f, *s;
  49. long q, p, W, w;
  50. )
  51. static void timeout(int signum)
  52. {
  53. if (TT.w) error_exit("Timeout");
  54. xexit();
  55. }
  56. static void set_alarm(int seconds)
  57. {
  58. xsignal(SIGALRM, seconds ? timeout : SIG_DFL);
  59. alarm(seconds);
  60. }
  61. // open AF_UNIX socket
  62. static int usock(char *name, int type, int out)
  63. {
  64. int sockfd;
  65. struct sockaddr_un sockaddr;
  66. memset(&sockaddr, 0, sizeof(struct sockaddr_un));
  67. if (strlen(name) + 1 > sizeof(sockaddr.sun_path))
  68. error_exit("socket path too long %s", name);
  69. strcpy(sockaddr.sun_path, name);
  70. sockaddr.sun_family = AF_UNIX;
  71. sockfd = xsocket(AF_UNIX, type, 0);
  72. (out?xconnect:xbind)(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr));
  73. return sockfd;
  74. }
  75. void netcat_main(void)
  76. {
  77. int sockfd = -1, in1 = 0, in2 = 0, out1 = 1, out2 = 1, family = AF_UNSPEC,
  78. ll = FLAG(L)|FLAG(l), type = FLAG(u) ? SOCK_DGRAM : SOCK_STREAM;
  79. pid_t child;
  80. // Addjust idle and quit_delay to ms or -1 for no timeout
  81. TT.W = TT.W ? TT.W*1000 : -1;
  82. TT.q = TT.q ? TT.q*1000 : -1;
  83. xsignal(SIGCHLD, SIG_IGN);
  84. set_alarm(TT.w);
  85. // The argument parsing logic can't make "<2" conditional on other
  86. // arguments like -f and -l, so do it by hand here.
  87. if (FLAG(f) ? toys.optc : (!ll && toys.optc!=(FLAG(U)?1:2)))
  88. help_exit("bad argument count");
  89. if (FLAG(4)) family = AF_INET;
  90. else if (FLAG(6)) family = AF_INET6;
  91. else if (FLAG(U)) family = AF_UNIX;
  92. if (TT.f) in1 = out2 = xopen(TT.f, O_RDWR);
  93. else {
  94. // Setup socket
  95. if (!ll) {
  96. if (FLAG(U)) sockfd = usock(toys.optargs[0], type, 1);
  97. else sockfd = xconnectany(xgetaddrinfo(toys.optargs[0], toys.optargs[1],
  98. family, type, 0, 0));
  99. // We have a connection. Disarm timeout and start poll/send loop.
  100. set_alarm(0);
  101. in1 = out2 = sockfd;
  102. pollinate(in1, in2, out1, out2, TT.W, TT.q);
  103. } else {
  104. // Listen for incoming connections
  105. if (FLAG(U)) {
  106. if (!FLAG(s)) error_exit("-s must be provided if using -U with -L/-l");
  107. sockfd = usock(TT.s, type, 0);
  108. } else {
  109. sprintf(toybuf, "%ld", TT.p);
  110. sockfd = xbindany(xgetaddrinfo(TT.s, toybuf, family, type, 0, 0));
  111. }
  112. if (listen(sockfd, 5)) error_exit("listen");
  113. if (!TT.p && !FLAG(U)) {
  114. struct sockaddr* address = (void*)toybuf;
  115. socklen_t len = sizeof(struct sockaddr_storage);
  116. short port_be;
  117. getsockname(sockfd, address, &len);
  118. if (address->sa_family == AF_INET)
  119. port_be = ((struct sockaddr_in*)address)->sin_port;
  120. else if (address->sa_family == AF_INET6)
  121. port_be = ((struct sockaddr_in6*)address)->sin6_port;
  122. else perror_exit("getsockname: bad family");
  123. dprintf(1, "%d\n", SWAP_BE16(port_be));
  124. // Return immediately if no -p and -Ll has arguments, so wrapper
  125. // script can use port number.
  126. if (CFG_TOYBOX_FORK && toys.optc && xfork()) goto cleanup;
  127. }
  128. do {
  129. child = 0;
  130. in1 = out2 = accept(sockfd, 0, 0);
  131. if (in1<0) perror_exit("accept");
  132. // We have a connection. Disarm timeout.
  133. set_alarm(0);
  134. if (toys.optc) {
  135. // Do we need a tty?
  136. // TODO nommu, and -t only affects server mode...? Only do -t with optc
  137. // if (CFG_TOYBOX_FORK && (toys.optflags&FLAG_t))
  138. // child = forkpty(&fdout, NULL, NULL, NULL);
  139. // else
  140. // Do we need to fork and/or redirect for exec?
  141. // TODO xpopen_both() here?
  142. if (FLAG(L)) NOEXIT(child = XVFORK());
  143. if (child) {
  144. close(in1);
  145. continue;
  146. }
  147. close(sockfd);
  148. dup2(in1, 0);
  149. dup2(in1, 1);
  150. if (FLAG(E)) dup2(in1, 2);
  151. if (in1>2) close(in1);
  152. xexec(toys.optargs);
  153. }
  154. pollinate(in1, in2, out1, out2, TT.W, TT.q);
  155. close(in1);
  156. } while (!FLAG(l));
  157. }
  158. }
  159. cleanup:
  160. if (CFG_TOYBOX_FREE) {
  161. close(in1);
  162. close(sockfd);
  163. }
  164. }