Ticket #3626: netcat-with-option-q.patch
File netcat-with-option-q.patch, 2.9 KB (added by , 15 years ago) |
---|
-
netcat.c
154 154 USHORT o_listen = 0; 155 155 USHORT o_nflag = 0; 156 156 USHORT o_wfile = 0; 157 int o_quittimeout = 0; 157 158 USHORT o_random = 0; 158 159 USHORT o_udpmode = 0; 159 160 USHORT o_verbose = 0; … … 224 225 bail (" punt!"); 225 226 } 226 227 228 /* quitaftertimeout: 229 singal handler which quits after user given seconds after close of stdin */ 230 void quitaftertimeout () 231 { 232 close(netfd); 233 exit(0); 234 } 235 227 236 /* timeout and other signal handling cruft */ 228 237 void tmtravel () 229 238 { … … 1172 1181 } /* select fuckup */ 1173 1182 /* if we have a timeout AND stdin is closed AND we haven't heard anything 1174 1183 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 */ 1187 if (o_quittimeout == 0) { 1175 1188 if (rr == 0) { 1176 1189 if (! FD_ISSET (0, ding1)) 1177 1190 netretry--; /* we actually try a coupla times. */ … … 1182 1195 return (0); /* not an error! */ 1183 1196 } 1184 1197 } /* select timeout */ 1198 } 1185 1199 /* xxx: should we check the exception fds too? The read fds seem to give 1186 1200 us the right info, and none of the examples I found bothered. */ 1187 1201 … … 1215 1229 if (rr <= 0) { /* at end, or fukt, or ... */ 1216 1230 FD_CLR (0, ding1); /* disable and close stdin */ 1217 1231 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 } 1218 1241 } else { 1219 1242 rzleft = rr; 1220 1243 zp = bigbuf_in; … … 1388 1411 1389 1412 /* If your shitbox doesn't have getopt, step into the nineties already. */ 1390 1413 /* 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) { 1392 1415 /* Debug (("in go: x now %c, optarg %x optind %d", x, optarg, optind)) */ 1393 1416 switch (x) { 1394 1417 case 'a': … … 1440 1463 if (o_lport == 0) 1441 1464 bail ("invalid local port %s", optarg); 1442 1465 break; 1466 case 'q': 1467 o_quittimeout = atoi(optarg); break; 1443 1468 case 'r': /* randomize various things */ 1444 1469 o_random++; break; 1445 1470 case 's': /* local source address */ … … 1649 1674 -n numeric-only IP addresses, no DNS\n\ 1650 1675 -o file hex dump of traffic\n\ 1651 1676 -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\ 1652 1680 -r randomize local and remote ports\n\ 1653 1681 -s addr local source address"); 1654 1682 #ifdef TELNET