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)"
30 usage: netcat [-t] [-lL COMMAND...]
32 -t allocate tty (must come before -l or -L)
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
47 char *filename; // -f read from filename instead of network
48 long quit_delay; // -q Exit after EOF from stdin after # seconds.
49 char *source_address; // -s Bind to a specific source address.
50 long port; // -p Bind to a specific source port.
51 long wait; // -w Wait # seconds for a connection.
54 static void timeout(int signum)
56 if (TT.wait) error_exit("Timeout");
57 // This should be xexit() but would need siglongjmp()...
61 static void set_alarm(int seconds)
63 xsignal(SIGALRM, seconds ? timeout : SIG_DFL);
67 // Translate x.x.x.x numeric IPv4 address, or else DNS lookup an IPv4 name.
68 static void lookup_name(char *name, uint32_t *result)
70 struct hostent *hostbyname;
72 hostbyname = gethostbyname(name); // getaddrinfo
73 if (!hostbyname) error_exit("no host '%s'", name);
74 *result = *(uint32_t *)*hostbyname->h_addr_list;
77 // Worry about a fancy lookup later.
78 static void lookup_port(char *str, uint16_t *port)
80 *port = SWAP_BE16(atoi(str));
83 void netcat_main(void)
85 int sockfd=-1, pollcount=2;
86 struct pollfd pollfds[2];
88 memset(pollfds, 0, 2*sizeof(struct pollfd));
89 pollfds[0].events = pollfds[1].events = POLLIN;
92 // The argument parsing logic can't make "<2" conditional on other
93 // arguments like -f and -l, so we do it by hand here.
94 if (toys.optflags&FLAG_f) {
95 if (toys.optc) toys.exithelp++;
96 } else if (!(toys.optflags&(FLAG_l|FLAG_L)) && toys.optc!=2) toys.exithelp++;
98 if (toys.exithelp) error_exit("Argument count wrong");
100 if (TT.filename) pollfds[0].fd = xopen(TT.filename, O_RDWR);
103 struct sockaddr_in address;
106 sockfd = xsocket(AF_INET, SOCK_STREAM, 0);
107 fcntl(sockfd, F_SETFD, FD_CLOEXEC);
109 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &temp, sizeof(temp));
110 memset(&address, 0, sizeof(address));
111 address.sin_family = AF_INET;
112 if (TT.source_address || TT.port) {
113 address.sin_port = SWAP_BE16(TT.port);
114 if (TT.source_address)
115 lookup_name(TT.source_address, (uint32_t *)&address.sin_addr);
116 if (bind(sockfd, (struct sockaddr *)&address, sizeof(address)))
122 if (!CFG_NETCAT_LISTEN || !(toys.optflags&(FLAG_L|FLAG_l))) {
123 // Figure out where to dial out to.
124 lookup_name(*toys.optargs, (uint32_t *)&address.sin_addr);
125 lookup_port(toys.optargs[1], &address.sin_port);
126 temp = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
127 if (temp<0) perror_exit("connect");
128 pollfds[0].fd = sockfd;
130 // Listen for incoming connections
133 socklen_t len = sizeof(address);
135 if (listen(sockfd, 5)) error_exit("listen");
137 getsockname(sockfd, (struct sockaddr *)&address, &len);
138 printf("%d\n", SWAP_BE16(address.sin_port));
141 // Do we need to return immediately because -l has arguments?
143 if ((toys.optflags & FLAG_l) && toys.optc) {
144 if (xfork()) goto cleanup;
153 // For -l, call accept from the _new_ process.
155 pollfds[0].fd = accept(sockfd, (struct sockaddr *)&address, &len);
156 if (pollfds[0].fd<0) perror_exit("accept");
160 if (toys.optflags&FLAG_t)
161 child = forkpty(&(pollfds[1].fd), NULL, NULL, NULL);
163 // Do we need to fork and/or redirect for exec?
166 if (toys.optflags&FLAG_L) child = fork();
167 if (!child && toys.optc) {
168 int fd = pollfds[0].fd;
172 if (toys.optflags&FLAG_L) dup2(fd, 2);
177 if (child<0) error_msg("Fork failed\n");
179 close(pollfds[0].fd);
184 // We have a connection. Disarm timeout.
185 // (Does not play well with -L, but what _should_ that do?)
188 if (CFG_NETCAT_LISTEN && (toys.optflags&(FLAG_L|FLAG_l) && toys.optc))
191 // Poll loop copying stdin->socket and socket->stdout.
195 if (0>poll(pollfds, pollcount, -1)) perror_exit("poll");
197 for (i=0; i<pollcount; i++) {
198 if (pollfds[i].revents & POLLIN) {
199 int len = read(pollfds[i].fd, toybuf, sizeof(toybuf));
200 if (len<1) goto dohupnow;
201 xwrite(i ? pollfds[0].fd : 1, toybuf, len);
202 } else if (pollfds[i].revents & POLLHUP) {
204 // Close half-connection. This is needed for things like
205 // "echo GET / | netcat landley.net 80"
207 shutdown(pollfds[0].fd, SHUT_WR);
209 set_alarm(TT.quit_delay);
215 if (CFG_TOYBOX_FREE) {
216 close(pollfds[0].fd);