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