]>
Commit | Line | Data |
---|---|---|
fdcd7086 JR |
1 | From: Aron Xu <aron@debian.org> |
2 | Date: Mon, 13 Feb 2012 15:16:04 +0800 | |
3 | Subject: [PATCH] quit timer | |
4 | ||
5 | --- | |
6 | nc.1 | 5 +++++ | |
7 | netcat.c | 39 ++++++++++++++++++++++++++++++++++----- | |
8 | 2 files changed, 39 insertions(+), 5 deletions(-) | |
9 | ||
10 | --- a/nc.1 | |
11 | +++ b/nc.1 | |
12 | @@ -40,6 +40,7 @@ | |
13 | .Op Fl O Ar length | |
14 | .Op Fl P Ar proxy_username | |
15 | .Op Fl p Ar source_port | |
16 | +.Op Fl q Ar seconds | |
17 | .Op Fl s Ar source | |
18 | .Op Fl T Ar toskeyword | |
19 | .Op Fl V Ar rtable | |
20 | @@ -148,6 +149,10 @@ | |
21 | Specifies the source port | |
22 | .Nm | |
23 | should use, subject to privilege restrictions and availability. | |
24 | +.It Fl q Ar seconds | |
25 | +after EOF on stdin, wait the specified number of seconds and then quit. If | |
26 | +.Ar seconds | |
27 | +is negative, wait forever. | |
28 | .It Fl r | |
29 | Specifies that source and/or destination ports should be chosen randomly | |
30 | instead of sequentially within a range or in the order that the system | |
31 | --- a/netcat.c | |
32 | +++ b/netcat.c | |
33 | @@ -86,6 +86,7 @@ | |
34 | #include <errno.h> | |
35 | #include <netdb.h> | |
36 | #include <poll.h> | |
37 | +#include <signal.h> | |
38 | #include <stdarg.h> | |
39 | #include <stdio.h> | |
40 | #include <stdlib.h> | |
41 | @@ -120,6 +121,7 @@ | |
42 | int nflag; /* Don't do name look up */ | |
43 | char *Pflag; /* Proxy username */ | |
44 | char *pflag; /* Localport flag */ | |
45 | +int qflag = 0; /* Quit after some secs */ | |
46 | int rflag; /* Random ports flag */ | |
47 | char *sflag; /* Source Address */ | |
48 | int tflag; /* Telnet Emulation */ | |
49 | @@ -158,6 +160,7 @@ | |
50 | ||
51 | static int connect_with_timeout(int fd, const struct sockaddr *sa, | |
52 | socklen_t salen, int ctimeout); | |
53 | +static void quit(); | |
54 | ||
55 | int | |
56 | main(int argc, char *argv[]) | |
57 | @@ -181,7 +184,7 @@ | |
58 | sv = NULL; | |
59 | ||
60 | while ((ch = getopt(argc, argv, | |
61 | - "46CDdhI:i:jklnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) { | |
62 | + "46CDdhI:i:jklnO:P:p:q:rSs:tT:UuV:vw:X:x:z")) != -1) { | |
63 | switch (ch) { | |
64 | case '4': | |
65 | family = AF_INET; | |
66 | @@ -235,6 +238,11 @@ | |
67 | case 'p': | |
68 | pflag = optarg; | |
69 | break; | |
70 | + case 'q': | |
7c95fee9 | 71 | + qflag = strtonum(optarg, INT_MIN, INT_MAX, &errstr); |
fdcd7086 JR |
72 | + if (errstr) |
73 | + errx(1, "quit timer %s: %s", errstr, optarg); | |
74 | + break; | |
75 | case 'r': | |
76 | rflag = 1; | |
77 | break; | |
78 | @@ -924,9 +932,18 @@ | |
79 | } | |
80 | else if (pfd[1].revents & POLLHUP) { | |
81 | shutdown_wr: | |
82 | + /* if the user asked to exit on EOF, do it */ | |
83 | + if (qflag == 0) { | |
84 | shutdown(nfd, SHUT_WR); | |
85 | - pfd[1].fd = -1; | |
86 | - pfd[1].events = 0; | |
87 | + close(wfd); | |
88 | + } | |
89 | + /* if user asked to die after a while, arrange for it */ | |
90 | + if (qflag > 0) { | |
91 | + signal(SIGALRM, quit); | |
92 | + alarm(qflag); | |
93 | + } | |
94 | + pfd[1].fd = -1; | |
95 | + pfd[1].events = 0; | |
96 | } | |
97 | } | |
98 | } | |
99 | @@ -1164,6 +1181,7 @@ | |
100 | \t-O length TCP send buffer length\n\ | |
101 | \t-P proxyuser\tUsername for proxy authentication\n\ | |
102 | \t-p port\t Specify local port for remote connects\n\ | |
103 | + \t-q secs\t quit after EOF on stdin and delay of secs\n\ | |
104 | \t-r Randomize remote ports\n\ | |
105 | \t-S Enable the TCP MD5 signature option\n\ | |
106 | \t-s addr\t Local source address\n\ | |
107 | @@ -1186,9 +1204,19 @@ | |
108 | { | |
109 | fprintf(stderr, | |
110 | "usage: nc [-46CDdhjklnrStUuvz] [-I length] [-i interval] [-O length]\n" | |
111 | - "\t [-P proxy_username] [-p source_port] [-s source] [-T toskeyword]\n" | |
112 | - "\t [-V rtable] [-w timeout] [-X proxy_protocol]\n" | |
113 | + "\t [-P proxy_username] [-p source_port] [-q seconds] [-s source]\n" | |
114 | + "\t [-T toskeyword] [-V rtable] [-w timeout] [-X proxy_protocol]\n" | |
115 | "\t [-x proxy_address[:port]] [destination] [port]\n"); | |
116 | if (ret) | |
117 | exit(1); | |
118 | } | |
119 | + | |
120 | +/* | |
121 | + * quit() | |
122 | + * handler for a "-q" timeout (exit 0 instead of 1) | |
123 | + */ | |
124 | +static void quit() | |
125 | +{ | |
126 | + /* XXX: should explicitly close fds here */ | |
127 | + exit(0); | |
128 | +} |