• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* netcat.c - Forward stdin/stdout to a file or network connection.
2  *
3  * Copyright 2007 Rob Landley <rob@landley.net>
4  *
5  * TODO: genericize for telnet/microcom/tail-f, fix -t
6 
7 USE_NETCAT(NEWTOY(netcat, "^tElLw#<1W#<1p#<1>65535q#<1s:f:46uUn[!tlL][!Lw][!Lu][!46U]", TOYFLAG_BIN))
8 USE_NETCAT(OLDTOY(nc, netcat, TOYFLAG_USR|TOYFLAG_BIN))
9 
10 config NETCAT
11   bool "netcat"
12   default y
13   help
14     usage: netcat [-46ElLtUu] [-wpq #] [-s addr] {IPADDR PORTNUM|-f FILENAME|COMMAND...}
15 
16     Forward stdin/stdout to a file or network connection.
17 
18     -4	Force IPv4
19     -6	Force IPv6
20     -E	Forward stderr
21     -f	Use FILENAME (ala /dev/ttyS0) instead of network
22     -l	Listen for one incoming connection, then exit
23     -L	Listen and background each incoming connection (server mode)
24     -n	No DNS lookup
25     -p	Local port number
26     -q	Quit SECONDS after EOF on stdin, even if stdout hasn't closed yet
27     -s	Local source address
28     -t	Allocate tty
29     -u	Use UDP
30     -U	Use a UNIX domain socket
31     -w	SECONDS timeout to establish connection
32     -W	SECONDS timeout for more data on an idle connection
33 
34     When listening the COMMAND line is executed as a child process to handle
35     an incoming connection. With no COMMAND -l forwards the connection
36     to stdin/stdout. If no -p specified, -l prints the port it bound to and
37     backgrounds itself (returning immediately).
38 
39     For a quick-and-dirty server, try something like:
40     netcat -s 127.0.0.1 -p 1234 -tL sh -l
41 
42     Or use "stty 115200 -F /dev/ttyS0 && stty raw -echo -ctlecho" with
43     netcat -f to connect to a serial port.
44 */
45 
46 #define FOR_netcat
47 #include "toys.h"
48 
49 GLOBALS(
50   char *f, *s;
51   long q, p, W, w;
52 )
53 
timeout(int signum)54 static void timeout(int signum)
55 {
56   if (TT.w) error_exit("Timeout");
57   xexit();
58 }
59 
60 // open AF_UNIX socket
usock(char * name,int type,int out)61 static int usock(char *name, int type, int out)
62 {
63   int sockfd;
64   struct sockaddr_un sockaddr;
65 
66   memset(&sockaddr, 0, sizeof(struct sockaddr_un));
67 
68   if (strlen(name) + 1 > sizeof(sockaddr.sun_path))
69     error_exit("socket path too long %s", name);
70   strcpy(sockaddr.sun_path, name);
71   sockaddr.sun_family = AF_UNIX;
72 
73   sockfd = xsocket(AF_UNIX, type, 0);
74   (out?xconnect:xbind)(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr));
75 
76   return sockfd;
77 }
78 
netcat_main(void)79 void netcat_main(void)
80 {
81   int sockfd = -1, in1 = 0, in2 = 0, out1 = 1, out2 = 1, family = AF_UNSPEC,
82     type = FLAG(u) ? SOCK_DGRAM : SOCK_STREAM;
83   socklen_t len;
84   pid_t child;
85 
86   // Addjust idle and quit_delay to ms or -1 for no timeout
87   TT.W = TT.W ? TT.W*1000 : -1;
88   TT.q = TT.q ? TT.q*1000 : -1;
89 
90   xsignal(SIGCHLD, SIG_IGN);
91   if (TT.w) {
92     xsignal(SIGALRM, timeout);
93     alarm(TT.w);
94   }
95 
96   // The argument parsing logic can't make "<2" conditional on other
97   // arguments like -f and -l, so do it by hand here.
98   if (FLAG(f) ? toys.optc : (!FLAG(l) && !FLAG(L) && toys.optc!=2-FLAG(U)))
99     help_exit("bad argument count");
100 
101   if (FLAG(4)) family = AF_INET;
102   else if (FLAG(6)) family = AF_INET6;
103   else if (FLAG(U)) family = AF_UNIX;
104 
105   if (TT.f) in1 = out2 = xopen(TT.f, O_RDWR);
106   else {
107     // Setup socket
108     if (!FLAG(l) && !FLAG(L)) {
109       if (FLAG(U)) sockfd = usock(toys.optargs[0], type, 1);
110       else sockfd = xconnectany(xgetaddrinfo(toys.optargs[0],
111         toys.optargs[1], family, type, 0, AI_NUMERICHOST*FLAG(n)));
112 
113       // We have a connection. Disarm timeout and start poll/send loop.
114       alarm(0);
115       in1 = out2 = sockfd;
116       pollinate(in1, in2, out1, out2, TT.W, TT.q);
117     } else {
118       // Listen for incoming connections
119       if (FLAG(U)) {
120         if (!FLAG(s)) error_exit("-s must be provided if using -U with -L/-l");
121         sockfd = usock(TT.s, type, 0);
122       } else {
123         sprintf(toybuf, "%ld", TT.p);
124         sockfd = xbindany(xgetaddrinfo(TT.s, toybuf, family, type, 0, 0));
125       }
126 
127       if (!FLAG(u) && listen(sockfd, 5)) perror_exit("listen");
128       if (!TT.p && !FLAG(U)) {
129         struct sockaddr* address = (void*)toybuf;
130         short port_be;
131 
132         len = sizeof(struct sockaddr_storage);
133         getsockname(sockfd, address, &len);
134         if (address->sa_family == AF_INET)
135           port_be = ((struct sockaddr_in*)address)->sin_port;
136         else if (address->sa_family == AF_INET6)
137           port_be = ((struct sockaddr_in6*)address)->sin6_port;
138         else perror_exit("getsockname: bad family");
139 
140         dprintf(1, "%d\n", SWAP_BE16(port_be));
141         // Return immediately if no -p and -Ll has arguments, so wrapper
142         // script can use port number.
143         if (CFG_TOYBOX_FORK && toys.optc && xfork()) goto cleanup;
144       }
145 
146       do {
147        len = sizeof(struct sockaddr_storage);
148         if (FLAG(u)) {
149           if (-1 == recvfrom(in1 = dup(sockfd), &child, 1, MSG_PEEK,
150             (void *)toybuf, &len)) perror_exit("recvfrom");
151         } else if ((in1 = accept(sockfd, 0, 0))<0) perror_exit("accept");
152         out2 = in1;
153         child = 0;
154 
155         // We have a connection. Disarm timeout.
156         alarm(0);
157 
158         // Fork a child as necessary. Parent cleans up and continues here.
159         if (toys.optc && FLAG(L)) NOEXIT(child = XVFORK());
160         if (child) {
161           close(in1);
162           continue;
163         }
164 
165         if (FLAG(u))
166           xconnect(in1, (void *)toybuf, sizeof(struct sockaddr_storage));
167 
168         // Cleanup and redirect for exec
169         if (toys.optc) {
170           // Do we need a tty?
171 // TODO nommu and -t only affects server mode...
172 //        if (FLAG(t)) child = forkpty(&fdout, NULL, NULL, NULL);
173 
174           close(sockfd);
175           dup2(in1, 0);
176           dup2(in1, 1);
177           if (FLAG(E)) dup2(in1, 2);
178           if (in1>2) close(in1);
179           xexec(toys.optargs);
180 
181         // Copy stdin/out
182         } else {
183           pollinate(in1, in2, out1, out2, TT.W, TT.q);
184           close(in1);
185         }
186       } while (FLAG(L));
187     }
188   }
189 
190 cleanup:
191   if (CFG_TOYBOX_FREE) {
192     close(in1);
193     close(sockfd);
194   }
195 }
196