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