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