Ticket #3626: netcat-with-option-q.patch

File netcat-with-option-q.patch, 2.9 KB (added by raghuram87, 15 years ago)
  • netcat.c

     
    154154USHORT o_listen = 0;
    155155USHORT o_nflag = 0;
    156156USHORT o_wfile = 0;
     157int o_quittimeout = 0;
    157158USHORT o_random = 0;
    158159USHORT o_udpmode = 0;
    159160USHORT o_verbose = 0;
     
    224225  bail (" punt!");
    225226}
    226227
     228/* quitaftertimeout:
     229   singal handler which quits after user given seconds after close of stdin */
     230void quitaftertimeout ()
     231{
     232  close(netfd);
     233  exit(0);
     234}
     235
    227236/* timeout and other signal handling cruft */
    228237void tmtravel ()
    229238{
     
    11721181    } /* select fuckup */
    11731182/* if we have a timeout AND stdin is closed AND we haven't heard anything
    11741183   from the net during that time, assume it's dead and close it too. */
     1184
     1185/* we need this section of if -q is not given, otherwise we dont quit if we
     1186   dont receive from net, we quit after timeout given with -q */
     1187if (o_quittimeout == 0) {
    11751188    if (rr == 0) {
    11761189    if (! FD_ISSET (0, ding1))
    11771190      netretry--;           /* we actually try a coupla times. */
     
    11821195      return (0);           /* not an error! */
    11831196    }
    11841197    } /* select timeout */
     1198}
    11851199/* xxx: should we check the exception fds too?  The read fds seem to give
    11861200   us the right info, and none of the examples I found bothered. */
    11871201
     
    12151229    if (rr <= 0) {          /* at end, or fukt, or ... */
    12161230      FD_CLR (0, ding1);        /* disable and close stdin */
    12171231      close (0);
     1232      if (o_quittimeout > 0) {
     1233        if (o_verbose > 1)
     1234          fprintf (stderr, "STDIN closed, quit after %d seconds\n", o_quittimeout);
     1235        signal (SIGALRM, quitaftertimeout);
     1236        alarm (o_quittimeout);
     1237        // if -q argument is < 0 we dont trigger any signal and we also don't
     1238        // check for rr == 0 above, hence this is an infinite loop, essentially
     1239        // waiting forever on fd of net.
     1240      }
    12181241    } else {
    12191242      rzleft = rr;
    12201243      zp = bigbuf_in;
     
    13881411
    13891412/* If your shitbox doesn't have getopt, step into the nineties already. */
    13901413/* optarg, optind = next-argv-component [i.e. flag arg]; optopt = last-char */
    1391   while ((x = getopt (argc, argv, "ae:g:G:hi:lno:p:rs:tuvw:z")) != EOF) {
     1414  while ((x = getopt (argc, argv, "ae:g:G:hi:lno:p:q:rs:tuvw:z")) != EOF) {
    13921415/* Debug (("in go: x now %c, optarg %x optind %d", x, optarg, optind)) */
    13931416    switch (x) {
    13941417      case 'a':
     
    14401463    if (o_lport == 0)
    14411464      bail ("invalid local port %s", optarg);
    14421465    break;
     1466        case 'q':
     1467    o_quittimeout = atoi(optarg); break;
    14431468      case 'r':             /* randomize various things */
    14441469    o_random++; break;
    14451470      case 's':             /* local source address */
     
    16491674    -n          numeric-only IP addresses, no DNS\n\
    16501675    -o file         hex dump of traffic\n\
    16511676    -p port         local port number\n\
     1677    -q secs         after  EOF on stdin, wait the specified number\n\
     1678                    of seconds and then quit. If seconds is\n\
     1679                    negative, wait forever.\n\
    16521680    -r          randomize local and remote ports\n\
    16531681    -s addr         local source address");
    16541682#ifdef TELNET