• 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 #ifdef TOYBOX_OH_ADAPT
82 /* Remove unsupported -f and -t option */
83  if (toys.optflags & FLAG_t) error_exit("Unknown option 't'");
84  if (toys.optflags & FLAG_f) error_exit("Unknown option 'f'");
85 #endif
86 
87   int sockfd = -1, in1 = 0, in2 = 0, out1 = 1, out2 = 1, family = AF_UNSPEC,
88     type = FLAG(u) ? SOCK_DGRAM : SOCK_STREAM;
89   socklen_t len;
90   pid_t child;
91 
92   // Addjust idle and quit_delay to ms or -1 for no timeout
93   TT.W = TT.W ? TT.W*1000 : -1;
94   TT.q = TT.q ? TT.q*1000 : -1;
95 
96   xsignal(SIGCHLD, SIG_IGN);
97   if (TT.w) {
98     xsignal(SIGALRM, timeout);
99     alarm(TT.w);
100   }
101 
102   // The argument parsing logic can't make "<2" conditional on other
103   // arguments like -f and -l, so do it by hand here.
104   if (FLAG(f) ? toys.optc : (!FLAG(l) && !FLAG(L) && toys.optc!=2-FLAG(U)))
105     help_exit("bad argument count");
106 
107   if (FLAG(4)) family = AF_INET;
108   else if (FLAG(6)) family = AF_INET6;
109   else if (FLAG(U)) family = AF_UNIX;
110 
111   if (TT.f) in1 = out2 = xopen(TT.f, O_RDWR);
112   else {
113     // Setup socket
114     if (!FLAG(l) && !FLAG(L)) {
115       if (FLAG(U)) sockfd = usock(toys.optargs[0], type, 1);
116       else sockfd = xconnectany(xgetaddrinfo(toys.optargs[0],
117         toys.optargs[1], family, type, 0, AI_NUMERICHOST*FLAG(n)));
118 
119       // We have a connection. Disarm timeout and start poll/send loop.
120       alarm(0);
121       in1 = out2 = sockfd;
122       pollinate(in1, in2, out1, out2, TT.W, TT.q);
123     } else {
124       // Listen for incoming connections
125       if (FLAG(U)) {
126         if (!FLAG(s)) error_exit("-s must be provided if using -U with -L/-l");
127         sockfd = usock(TT.s, type, 0);
128       } else {
129         sprintf(toybuf, "%ld", TT.p);
130         sockfd = xbindany(xgetaddrinfo(TT.s, toybuf, family, type, 0, 0));
131       }
132 
133       if (!FLAG(u) && listen(sockfd, 5)) perror_exit("listen");
134       if (!TT.p && !FLAG(U)) {
135         struct sockaddr* address = (void*)toybuf;
136         short port_be;
137 
138         len = sizeof(struct sockaddr_storage);
139         getsockname(sockfd, address, &len);
140         if (address->sa_family == AF_INET)
141           port_be = ((struct sockaddr_in*)address)->sin_port;
142         else if (address->sa_family == AF_INET6)
143           port_be = ((struct sockaddr_in6*)address)->sin6_port;
144         else perror_exit("getsockname: bad family");
145 
146         dprintf(1, "%d\n", SWAP_BE16(port_be));
147         // Return immediately if no -p and -Ll has arguments, so wrapper
148         // script can use port number.
149         if (CFG_TOYBOX_FORK && toys.optc && xfork()) goto cleanup;
150       }
151 
152       do {
153        len = sizeof(struct sockaddr_storage);
154         if (FLAG(u)) {
155           if (-1 == recvfrom(in1 = dup(sockfd), &child, 1, MSG_PEEK,
156             (void *)toybuf, &len)) perror_exit("recvfrom");
157         } else if ((in1 = accept(sockfd, 0, 0))<0) perror_exit("accept");
158         out2 = in1;
159         child = 0;
160 
161         // We have a connection. Disarm timeout.
162         alarm(0);
163 
164         // Fork a child as necessary. Parent cleans up and continues here.
165         if (toys.optc && FLAG(L)) NOEXIT(child = XVFORK());
166         if (child) {
167           close(in1);
168           continue;
169         }
170 
171         if (FLAG(u))
172           xconnect(in1, (void *)toybuf, sizeof(struct sockaddr_storage));
173 
174         // Cleanup and redirect for exec
175         if (toys.optc) {
176           // Do we need a tty?
177 // TODO nommu and -t only affects server mode...
178 //        if (FLAG(t)) child = forkpty(&fdout, NULL, NULL, NULL);
179 
180           close(sockfd);
181           dup2(in1, 0);
182           dup2(in1, 1);
183           if (FLAG(E)) dup2(in1, 2);
184           if (in1>2) close(in1);
185           xexec(toys.optargs);
186 
187         // Copy stdin/out
188         } else {
189           pollinate(in1, in2, out1, out2, TT.W, TT.q);
190           close(in1);
191         }
192       } while (FLAG(L));
193     }
194   }
195 
196 cleanup:
197   if (CFG_TOYBOX_FREE) {
198     close(in1);
199     close(sockfd);
200   }
201 }