Package: netcat / 1.10-41

quit.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Summary: Add the -q option.
Contributor: Alessandro Rubini <rubini@prosa.it>

Index: netcat-1.10/netcat.c
===================================================================
--- netcat-1.10.orig/netcat.c
+++ netcat-1.10/netcat.c
@@ -176,6 +176,7 @@ USHORT o_udpmode = 0;
 USHORT o_verbose = 0;
 unsigned int o_wait = 0;
 USHORT o_zero = 0;
+int o_quit = -1; /* 0 == quit-now; >0 == quit after o_quit seconds */
 /* o_tn in optional section */
 
 /* Debug macro: squirt whatever message and sleep a bit so we can see it go
@@ -240,6 +241,14 @@ void catch ()
   bail ("");
 }
 
+/* quit :
+   handler for a "-q" timeout (exit 0 instead of 1) */
+void quit()
+{
+  close(netfd);
+  exit(0);
+}
+
 /* timeout and other signal handling cruft */
 void tmtravel ()
 {
@@ -1265,6 +1274,18 @@ Debug (("got %d from the net, errno %d",
 	if (rr <= 0) {			/* at end, or fukt, or ... */
 	  FD_CLR (0, ding1);		/* disable and close stdin */
 	  close (0);
+	  /* if the user asked to exit on EOF, do it */
+	  if (o_quit == 0) {
+	    shutdown(netfd, 1);
+	    close (fd);
+	    exit (0);
+	  }
+	  /* if user asked to die after a while, arrange for it */
+	  if (o_quit > 0) {
+	    shutdown(netfd, 1);
+	    signal (SIGALRM, quit);
+	    alarm(o_quit);
+	  }
 	} else {
 	  rzleft = rr;
 	  zp = bigbuf_in;
@@ -1439,7 +1460,7 @@ main (argc, argv)
 
 /* If your shitbox doesn't have getopt, step into the nineties already. */
 /* optarg, optind = next-argv-component [i.e. flag arg]; optopt = last-char */
-  while ((x = getopt (argc, argv, "abe:g:G:hi:lno:p:rs:tuvw:z")) != EOF) {
+  while ((x = getopt (argc, argv, "abe:g:G:hi:lno:p:q:rs:tuvw:z")) != EOF) {
 /* Debug (("in go: x now %c, optarg %x optind %d", x, optarg, optind)) */
     switch (x) {
       case 'a':
@@ -1495,6 +1516,8 @@ main (argc, argv)
 	break;
       case 'r':				/* randomize various things */
 	o_random++; break;
+      case 'q':				/* quit after stdin does EOF */
+	o_quit = atoi(optarg); break;
       case 's':				/* local source address */
 /* do a full lookup [since everything else goes through the same mill],
    unless -n was previously specified.  In fact, careful placement of -n can
@@ -1712,6 +1735,7 @@ options:");
 	-o file			hex dump of traffic\n\
 	-p port			local port number\n\
 	-r			randomize local and remote ports\n\
+	-q secs			quit after EOF on stdin and delay of secs\n\
 	-s addr			local source address");
 #ifdef TELNET
   holler ("\