e1c22839c6d474631bba9cc4e46139bd120e5678
[oweals/busybox.git] / networking / nc.c
1 /* vi: set sw=4 ts=4: */
2 /*  nc: mini-netcat - built from the ground up for LRP
3  *
4  *  Copyright (C) 1998, 1999  Charles P. Wright
5  *  Copyright (C) 1998  Dave Cinege
6  *
7  *  Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "busybox.h"
11
12 static void timeout(int signum)
13 {
14         bb_error_msg_and_die("timed out");
15 }
16
17 int nc_main(int argc, char **argv)
18 {
19         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;)
27         struct sockaddr_in address;
28         fd_set readfds, testfds;
29         int opt; /* must be signed (getopt returns -1) */
30
31         memset(&address, 0, sizeof(address));
32
33         if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
34                 /* 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++);
40                         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                         }
44                         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 ...  */
67                         } else bb_show_usage();
68                 }
69                 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)
76                         bb_show_usage();
77         } else {
78                 if (argc != 3) bb_show_usage();
79                 argc--;
80                 argv++;
81         }
82
83         if (wsecs) {
84                 signal(SIGALRM, timeout);
85                 alarm(wsecs);
86         }
87
88         if (!cfd) {
89                 sfd = xsocket(AF_INET, SOCK_STREAM, 0);
90                 fcntl(sfd, F_SETFD, FD_CLOEXEC);
91                 setsockopt_reuseaddr(sfd);
92                 address.sin_family = AF_INET;
93
94                 // Set local port.
95
96                 if (lport != 0) {
97                         address.sin_port = lport;
98                         xbind(sfd, (struct sockaddr *) &address, sizeof(address));
99                 }
100
101                 if (do_listen) {
102                         socklen_t addrlen = sizeof(address);
103
104                         xlisten(sfd, do_listen);
105
106                         // If we didn't specify a port number, query and print it to stderr.
107
108                         if (!lport) {
109                                 socklen_t len = sizeof(address);
110                                 getsockname(sfd, (struct sockaddr *) &address, &len);
111                                 fdprintf(2, "%d\n", SWAP_BE16(address.sin_port));
112                         }
113  repeatyness:
114                         cfd = accept(sfd, (struct sockaddr *) &address, &addrlen);
115                         if (cfd < 0)
116                                 bb_perror_msg_and_die("accept");
117
118                         if (!execparam) close(sfd);
119                 } else {
120                         struct hostent *hostinfo;
121                         hostinfo = xgethostbyname(argv[0]);
122
123                         address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
124                         address.sin_port = bb_lookup_port(argv[1], "tcp", 0);
125                         address.sin_port = htons(address.sin_port);
126
127                         xconnect(sfd, (struct sockaddr *) &address, sizeof(address));
128                         cfd = sfd;
129                 }
130         }
131
132         if (wsecs) {
133                 alarm(0);
134                 signal(SIGALRM, SIG_DFL);
135         }
136
137         /* -e given? */
138         if (execparam) {
139                 if (cfd) {
140                         signal(SIGCHLD, SIG_IGN);
141                         dup2(cfd, 0);
142                         close(cfd);
143                 }
144                 dup2(0, 1);
145                 dup2(0, 2);
146
147                 // With more than one -l, repeatedly act as server.
148
149                 if (do_listen > 1 && vfork()) {
150                         // 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.
153                         logmode = LOGMODE_NONE;
154                         close(0);
155                         close(1);
156                         close(2);
157
158                         goto repeatyness;
159                 }
160                 USE_NC_EXTRA(execvp(execparam[0], execparam);)
161                 /* 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.
166
167         FD_ZERO(&readfds);
168         FD_SET(cfd, &readfds);
169         FD_SET(STDIN_FILENO, &readfds);
170
171         for (;;) {
172                 int fd;
173                 int ofd;
174                 int nread;
175
176                 testfds = readfds;
177
178                 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
179                         bb_perror_msg_and_die("select");
180
181                 for (fd = 0; fd < FD_SETSIZE; fd++) {
182                         if (FD_ISSET(fd, &testfds)) {
183                                 nread = safe_read(fd, bb_common_bufsiz1,
184                                                         sizeof(bb_common_bufsiz1));
185
186                                 if (fd == cfd) {
187                                         if (nread<1) exit(0);
188                                         ofd = STDOUT_FILENO;
189                                 } else {
190                                         if (nread<1) {
191                                                 // Close outgoing half-connection so they get EOF, but
192                                                 // leave incoming alone so we can see response.
193                                                 shutdown(cfd, 1);
194                                                 FD_CLR(STDIN_FILENO, &readfds);
195                                         }
196                                         ofd = cfd;
197                                 }
198
199                                 xwrite(ofd, bb_common_bufsiz1, nread);
200                                 if (delay > 0) sleep(delay);
201                         }
202                 }
203         }
204 }