blob: e1c22839c6d474631bba9cc4e46139bd120e5678 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00002/* nc: mini-netcat - built from the ground up for LRP
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00003 *
Rob Landley1cca9482006-07-10 19:45:20 +00004 * Copyright (C) 1998, 1999 Charles P. Wright
5 * Copyright (C) 1998 Dave Cinege
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00006 *
Rob Landley1cca9482006-07-10 19:45:20 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
Eric Andersencc8ed391999-10-05 16:24:54 +00009
Eric Andersencbe31da2001-02-20 06:14:08 +000010#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000011
Mike Frysinger60a5c382005-05-06 04:45:38 +000012static void timeout(int signum)
13{
Denis Vlasenko13858992006-10-08 12:49:22 +000014 bb_error_msg_and_die("timed out");
Mike Frysinger60a5c382005-05-06 04:45:38 +000015}
16
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000017int nc_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000018{
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000019 int sfd = 0;
20 int cfd = 0;
21 SKIP_NC_SERVER(const) unsigned do_listen = 0;
22 SKIP_NC_SERVER(const) unsigned lport = 0;
23 SKIP_NC_EXTRA (const) unsigned wsecs = 0;
24 SKIP_NC_EXTRA (const) unsigned delay = 0;
25 SKIP_NC_EXTRA (const int execparam = 0;)
26 USE_NC_EXTRA (char **execparam = NULL;)
Erik Andersene49d5ec2000-02-08 19:58:47 +000027 struct sockaddr_in address;
Erik Andersene49d5ec2000-02-08 19:58:47 +000028 fd_set readfds, testfds;
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000029 int opt; /* must be signed (getopt returns -1) */
Erik Andersene49d5ec2000-02-08 19:58:47 +000030
Rob Landley1cca9482006-07-10 19:45:20 +000031 memset(&address, 0, sizeof(address));
32
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000033 if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000034 /* getopt32 is _almost_ usable:
35 ** it cannot handle "... -e prog -prog-opt" */
36 while ((opt = getopt(argc, argv,
37 "" USE_NC_SERVER("lp:") USE_NC_EXTRA("w:i:f:e:") )) > 0
38 ) {
39 if (ENABLE_NC_SERVER && opt=='l') USE_NC_SERVER(do_listen++);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +000040 else if (ENABLE_NC_SERVER && opt=='p') {
41 USE_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0));
42 USE_NC_SERVER(lport = htons(lport));
43 }
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000044 else if (ENABLE_NC_EXTRA && opt=='w') USE_NC_EXTRA( wsecs = xatou(optarg));
45 else if (ENABLE_NC_EXTRA && opt=='i') USE_NC_EXTRA( delay = xatou(optarg));
46 else if (ENABLE_NC_EXTRA && opt=='f') USE_NC_EXTRA( cfd = xopen(optarg, O_RDWR));
47 else if (ENABLE_NC_EXTRA && opt=='e' && optind<=argc) {
48 /* We cannot just 'break'. We should let getopt finish.
49 ** Or else we won't be able to find where
50 ** 'host' and 'port' params are
51 ** (think "nc -w 60 host port -e prog"). */
52 USE_NC_EXTRA(
53 char **p;
54 // +2: one for progname (optarg) and one for NULL
55 execparam = xzalloc(sizeof(char*) * (argc - optind + 2));
56 p = execparam;
57 *p++ = optarg;
58 while (optind < argc) {
59 *p++ = argv[optind++];
60 }
61 )
62 /* optind points to argv[arvc] (NULL) now.
63 ** FIXME: we assume that getopt will not count options
64 ** possibly present on "-e prog args" and will not
65 ** include them into final value of optind
66 ** which is to be used ... */
Rob Landley1cca9482006-07-10 19:45:20 +000067 } else bb_show_usage();
Matt Kraai1d702672001-02-07 04:09:23 +000068 }
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000069 argv += optind; /* ... here! */
70 argc -= optind;
71 // -l and -f don't mix
72 if (do_listen && cfd) bb_show_usage();
73 // Listen or file modes need zero arguments, client mode needs 2
74 opt = ((do_listen || cfd) ? 0 : 2);
75 if (argc != opt)
Rob Landley1cca9482006-07-10 19:45:20 +000076 bb_show_usage();
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000077 } else {
78 if (argc != 3) bb_show_usage();
79 argc--;
80 argv++;
81 }
Eric Andersencc8ed391999-10-05 16:24:54 +000082
Mike Frysinger60a5c382005-05-06 04:45:38 +000083 if (wsecs) {
84 signal(SIGALRM, timeout);
85 alarm(wsecs);
86 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000087
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000088 if (!cfd) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000089 sfd = xsocket(AF_INET, SOCK_STREAM, 0);
Rob Landley1cca9482006-07-10 19:45:20 +000090 fcntl(sfd, F_SETFD, FD_CLOEXEC);
Denis Vlasenko48237b02006-11-22 23:22:06 +000091 setsockopt_reuseaddr(sfd);
Rob Landley1cca9482006-07-10 19:45:20 +000092 address.sin_family = AF_INET;
Mike Frysinger60a5c382005-05-06 04:45:38 +000093
Rob Landley1cca9482006-07-10 19:45:20 +000094 // Set local port.
Matt Kraai1d702672001-02-07 04:09:23 +000095
Rob Landley1cca9482006-07-10 19:45:20 +000096 if (lport != 0) {
97 address.sin_port = lport;
Rob Landleyd921b2e2006-08-03 15:41:12 +000098 xbind(sfd, (struct sockaddr *) &address, sizeof(address));
Rob Landley1cca9482006-07-10 19:45:20 +000099 }
Matt Kraaibe9f44a2001-05-15 03:05:39 +0000100
Rob Landley1cca9482006-07-10 19:45:20 +0000101 if (do_listen) {
102 socklen_t addrlen = sizeof(address);
Matt Kraai1d702672001-02-07 04:09:23 +0000103
Rob Landleyd921b2e2006-08-03 15:41:12 +0000104 xlisten(sfd, do_listen);
Matt Kraai1d702672001-02-07 04:09:23 +0000105
Rob Landley1cca9482006-07-10 19:45:20 +0000106 // If we didn't specify a port number, query and print it to stderr.
Matt Kraai1d702672001-02-07 04:09:23 +0000107
Rob Landley1cca9482006-07-10 19:45:20 +0000108 if (!lport) {
109 socklen_t len = sizeof(address);
Denis Vlasenkoc8e6e352006-12-18 22:10:24 +0000110 getsockname(sfd, (struct sockaddr *) &address, &len);
Rob Landley1cca9482006-07-10 19:45:20 +0000111 fdprintf(2, "%d\n", SWAP_BE16(address.sin_port));
112 }
Denis Vlasenkod0e70af2006-10-16 01:10:28 +0000113 repeatyness:
Denis Vlasenko13858992006-10-08 12:49:22 +0000114 cfd = accept(sfd, (struct sockaddr *) &address, &addrlen);
115 if (cfd < 0)
Rob Landley1cca9482006-07-10 19:45:20 +0000116 bb_perror_msg_and_die("accept");
117
Denis Vlasenkod0e70af2006-10-16 01:10:28 +0000118 if (!execparam) close(sfd);
Rob Landley1cca9482006-07-10 19:45:20 +0000119 } else {
Denis Vlasenko85281512006-11-07 19:05:43 +0000120 struct hostent *hostinfo;
Denis Vlasenkod0e70af2006-10-16 01:10:28 +0000121 hostinfo = xgethostbyname(argv[0]);
Rob Landley1cca9482006-07-10 19:45:20 +0000122
123 address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
Denis Vlasenkod0e70af2006-10-16 01:10:28 +0000124 address.sin_port = bb_lookup_port(argv[1], "tcp", 0);
Denis Vlasenko6536a9b2007-01-12 10:35:23 +0000125 address.sin_port = htons(address.sin_port);
Rob Landley1cca9482006-07-10 19:45:20 +0000126
Denis Vlasenko1dc1b372006-12-23 02:48:44 +0000127 xconnect(sfd, (struct sockaddr *) &address, sizeof(address));
Rob Landley1cca9482006-07-10 19:45:20 +0000128 cfd = sfd;
129 }
Matt Kraai1d702672001-02-07 04:09:23 +0000130 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000131
Mike Frysinger60a5c382005-05-06 04:45:38 +0000132 if (wsecs) {
133 alarm(0);
134 signal(SIGALRM, SIG_DFL);
135 }
136
Eric Andersen1323c942002-04-26 23:59:12 +0000137 /* -e given? */
Denis Vlasenkod0e70af2006-10-16 01:10:28 +0000138 if (execparam) {
139 if (cfd) {
Rob Landley1cca9482006-07-10 19:45:20 +0000140 signal(SIGCHLD, SIG_IGN);
141 dup2(cfd, 0);
142 close(cfd);
143 }
Mike Frysinger7dc7f402005-05-06 05:00:34 +0000144 dup2(0, 1);
145 dup2(0, 2);
Eric Andersen1323c942002-04-26 23:59:12 +0000146
Rob Landley1cca9482006-07-10 19:45:20 +0000147 // With more than one -l, repeatedly act as server.
148
Denis Vlasenko13858992006-10-08 12:49:22 +0000149 if (do_listen > 1 && vfork()) {
Rob Landley1cca9482006-07-10 19:45:20 +0000150 // This is a bit weird as cleanup goes, since we wind up with no
151 // stdin/stdout/stderr. But it's small and shouldn't hurt anything.
152 // We check for cfd == 0 above.
Denis Vlasenko13858992006-10-08 12:49:22 +0000153 logmode = LOGMODE_NONE;
Rob Landley1cca9482006-07-10 19:45:20 +0000154 close(0);
155 close(1);
156 close(2);
157
158 goto repeatyness;
159 }
Denis Vlasenkod0e70af2006-10-16 01:10:28 +0000160 USE_NC_EXTRA(execvp(execparam[0], execparam);)
Rob Landley1cca9482006-07-10 19:45:20 +0000161 /* Don't print stuff or it will go over the wire.... */
162 _exit(127);
163 }
164
165 // Select loop copying stdin to cfd, and cfd to stdout.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000166
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 FD_ZERO(&readfds);
Rob Landley1cca9482006-07-10 19:45:20 +0000168 FD_SET(cfd, &readfds);
Matt Kraaibfa79672000-12-15 22:34:34 +0000169 FD_SET(STDIN_FILENO, &readfds);
Eric Andersencc8ed391999-10-05 16:24:54 +0000170
Rob Landley1cca9482006-07-10 19:45:20 +0000171 for (;;) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 int fd;
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000173 int ofd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174 int nread;
Eric Andersencc8ed391999-10-05 16:24:54 +0000175
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176 testfds = readfds;
Eric Andersencc8ed391999-10-05 16:24:54 +0000177
Matt Kraaibfa79672000-12-15 22:34:34 +0000178 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000179 bb_perror_msg_and_die("select");
Eric Andersencc8ed391999-10-05 16:24:54 +0000180
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 for (fd = 0; fd < FD_SETSIZE; fd++) {
182 if (FD_ISSET(fd, &testfds)) {
Rob Landley53437472006-07-16 08:14:35 +0000183 nread = safe_read(fd, bb_common_bufsiz1,
184 sizeof(bb_common_bufsiz1));
Eric Andersencc8ed391999-10-05 16:24:54 +0000185
Rob Landley1cca9482006-07-10 19:45:20 +0000186 if (fd == cfd) {
Rob Landley53437472006-07-16 08:14:35 +0000187 if (nread<1) exit(0);
Matt Kraaibfa79672000-12-15 22:34:34 +0000188 ofd = STDOUT_FILENO;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 } else {
Rob Landley53437472006-07-16 08:14:35 +0000190 if (nread<1) {
Rob Landley1cca9482006-07-10 19:45:20 +0000191 // Close outgoing half-connection so they get EOF, but
192 // leave incoming alone so we can see response.
193 shutdown(cfd, 1);
Paul Fox7b71d742005-07-18 22:23:16 +0000194 FD_CLR(STDIN_FILENO, &readfds);
195 }
Rob Landley1cca9482006-07-10 19:45:20 +0000196 ofd = cfd;
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000197 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000198
Rob Landley53437472006-07-16 08:14:35 +0000199 xwrite(ofd, bb_common_bufsiz1, nread);
Rob Landley1cca9482006-07-10 19:45:20 +0000200 if (delay > 0) sleep(delay);
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000201 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 }
203 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000204}