1 /* netcat.c - Forward stdin/stdout to a file or network connection.
3 * Copyright 2007 Rob Landley <rob@landley.net>
5 * TODO: udp, ipv6, genericize for telnet/microcom/tail-f
7 USE_NETCAT(OLDTOY(nc, netcat, TOYFLAG_USR|TOYFLAG_BIN))
8 USE_NETCAT(NEWTOY(netcat, USE_NETCAT_LISTEN("^tlL")"w#p#s:q#f:", TOYFLAG_BIN))
14 usage: netcat [-u] [-wpq #] [-s addr] {IPADDR PORTNUM|-f FILENAME}
16 -f use FILENAME (ala /dev/ttyS0) instead of network
18 -q SECONDS quit this many seconds after EOF on stdin.
20 -w SECONDS timeout for connection
22 Use "stty 115200 -F /dev/ttyS0 && stty raw -echo -ctlecho" with
23 netcat -f to connect to a serial port.
26 bool "netcat server options (-let)"
29 depends on TOYBOX_FORK
31 usage: netcat [-lL COMMAND...]
33 -l listen for one incoming connection.
34 -L listen for multiple incoming connections (server mode).
36 The command line after -l or -L is executed to handle each incoming
37 connection. If none, the connection is forwarded to stdin/stdout.
39 For a quick-and-dirty server, try something like:
40 netcat -s 127.0.0.1 -p 1234 -tL /bin/bash -l
42 config NETCAT_LISTEN_TTY
45 depends on NETCAT_LISTEN
46 depends on TOYBOX_FORK
50 -t allocate tty (must come before -l or -L)
57 char *filename; // -f read from filename instead of network
58 long quit_delay; // -q Exit after EOF from stdin after # seconds.
59 char *source_address; // -s Bind to a specific source address.
60 long port; // -p Bind to a specific source port.
61 long wait; // -w Wait # seconds for a connection.
64 static void timeout(int signum)
66 if (TT.wait) error_exit("Timeout");
67 // This should be xexit() but would need siglongjmp()...
71 static void set_alarm(int seconds)
73 xsignal(SIGALRM, seconds ? timeout : SIG_DFL);
77 // Translate x.x.x.x numeric IPv4 address, or else DNS lookup an IPv4 name.
78 static void lookup_name(char *name, uint32_t *result)
80 struct hostent *hostbyname;
82 hostbyname = gethostbyname(name); // getaddrinfo
83 if (!hostbyname) error_exit("no host '%s'", name);
84 *result = *(uint32_t *)*hostbyname->h_addr_list;
87 // Worry about a fancy lookup later.
88 static void lookup_port(char *str, uint16_t *port)
90 *port = SWAP_BE16(atoi(str));
93 void netcat_main(void)
95 int sockfd=-1, pollcount=2;
96 struct pollfd pollfds[2];
98 memset(pollfds, 0, 2*sizeof(struct pollfd));
99 pollfds[0].events = pollfds[1].events = POLLIN;
102 // The argument parsing logic can't make "<2" conditional on other
103 // arguments like -f and -l, so we do it by hand here.
104 if ((toys.optflags&FLAG_f) ? toys.optc :
105 (!(toys.optflags&(FLAG_l|FLAG_L)) && toys.optc!=2))
106 help_exit("Argument count wrong");
108 if (TT.filename) pollfds[0].fd = xopen(TT.filename, O_RDWR);
111 struct sockaddr_in address;
114 sockfd = xsocket(AF_INET, SOCK_STREAM, 0);
115 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
117 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(temp));
118 memset(&address, 0, sizeof(address));
119 address.sin_family = AF_INET;
120 if (TT.source_address || TT.port) {
121 address.sin_port = SWAP_BE16(TT.port);
122 if (TT.source_address)
123 lookup_name(TT.source_address, (uint32_t *)&address.sin_addr);
124 if (bind(sockfd, (struct sockaddr *)&address, sizeof(address)))
130 if (!CFG_NETCAT_LISTEN || !(toys.optflags&(FLAG_L|FLAG_l))) {
131 // Figure out where to dial out to.
132 lookup_name(*toys.optargs, (uint32_t *)&address.sin_addr);
133 lookup_port(toys.optargs[1], &address.sin_port);
134 temp = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
135 if (temp<0) perror_exit("connect");
136 pollfds[0].fd = sockfd;
138 // Listen for incoming connections
141 socklen_t len = sizeof(address);
143 if (listen(sockfd, 5)) error_exit("listen");
145 getsockname(sockfd, (struct sockaddr *)&address, &len);
146 printf("%d\n", SWAP_BE16(address.sin_port));
149 // Do we need to return immediately because -l has arguments?
151 if ((toys.optflags & FLAG_l) && toys.optc) {
152 if (CFG_TOYBOX_FORK && xfork()) goto cleanup;
161 // For -l, call accept from the _new_ process.
163 pollfds[0].fd = accept(sockfd, (struct sockaddr *)&address, &len);
164 if (pollfds[0].fd<0) perror_exit("accept");
168 if (toys.optflags&FLAG_t)
169 child = forkpty(&(pollfds[1].fd), NULL, NULL, NULL);
171 // Do we need to fork and/or redirect for exec?
174 if (toys.optflags&FLAG_L) {
178 if (!child && toys.optc) {
179 int fd = pollfds[0].fd;
183 if (toys.optflags&FLAG_L) dup2(fd, 2);
188 if (child<0) error_msg("Fork failed\n");
190 close(pollfds[0].fd);
195 // We have a connection. Disarm timeout.
196 // (Does not play well with -L, but what _should_ that do?)
199 if (CFG_NETCAT_LISTEN && ((toys.optflags&(FLAG_L|FLAG_l)) && toys.optc))
202 // Poll loop copying stdin->socket and socket->stdout.
206 if (0>poll(pollfds, pollcount, -1)) perror_exit("poll");
208 for (i=0; i<pollcount; i++) {
209 if (pollfds[i].revents & POLLIN) {
210 int len = read(pollfds[i].fd, toybuf, sizeof(toybuf));
211 if (len<1) goto dohupnow;
212 xwrite(i ? pollfds[0].fd : 1, toybuf, len);
213 } else if (pollfds[i].revents & POLLHUP) {
215 // Close half-connection. This is needed for things like
216 // "echo GET / | netcat landley.net 80"
218 shutdown(pollfds[0].fd, SHUT_WR);
220 set_alarm(TT.quit_delay);
226 if (CFG_TOYBOX_FREE) {
227 close(pollfds[0].fd);