dc9102b2a25c3caf8019826f38aaa729711ee355
[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 /* Lots of small differences in features
13  * when compared to "standard" nc
14  */
15
16 static void timeout(int signum)
17 {
18         bb_error_msg_and_die("timed out");
19 }
20
21 int nc_main(int argc, char **argv)
22 {
23         /* sfd sits _here_ only because of "repeat" option (-l -l). */
24         int sfd = sfd; /* for gcc */
25         int cfd = 0;
26         unsigned lport = 0;
27         SKIP_NC_SERVER(const) unsigned do_listen = 0;
28         SKIP_NC_EXTRA (const) unsigned wsecs = 0;
29         SKIP_NC_EXTRA (const) unsigned delay = 0;
30         SKIP_NC_EXTRA (const int execparam = 0;)
31         USE_NC_EXTRA  (char **execparam = NULL;)
32         len_and_sockaddr *lsa;
33         fd_set readfds, testfds;
34         int opt; /* must be signed (getopt returns -1) */
35
36         if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
37                 /* getopt32 is _almost_ usable:
38                 ** it cannot handle "... -e prog -prog-opt" */
39                 while ((opt = getopt(argc, argv,
40                         "" USE_NC_SERVER("lp:") USE_NC_EXTRA("w:i:f:e:") )) > 0
41                 ) {
42                         if (ENABLE_NC_SERVER && opt=='l')      USE_NC_SERVER(do_listen++);
43                         else if (ENABLE_NC_SERVER && opt=='p') {
44                                 USE_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0));
45                         }
46                         else if (ENABLE_NC_EXTRA && opt=='w') USE_NC_EXTRA( wsecs = xatou(optarg));
47                         else if (ENABLE_NC_EXTRA && opt=='i') USE_NC_EXTRA( delay = xatou(optarg));
48                         else if (ENABLE_NC_EXTRA && opt=='f') USE_NC_EXTRA( cfd = xopen(optarg, O_RDWR));
49                         else if (ENABLE_NC_EXTRA && opt=='e' && optind<=argc) {
50                                 /* We cannot just 'break'. We should let getopt finish.
51                                 ** Or else we won't be able to find where
52                                 ** 'host' and 'port' params are
53                                 ** (think "nc -w 60 host port -e prog"). */
54                                 USE_NC_EXTRA(
55                                         char **p;
56                                         // +2: one for progname (optarg) and one for NULL
57                                         execparam = xzalloc(sizeof(char*) * (argc - optind + 2));
58                                         p = execparam;
59                                         *p++ = optarg;
60                                         while (optind < argc) {
61                                                 *p++ = argv[optind++];
62                                         }
63                                 )
64                                 /* optind points to argv[arvc] (NULL) now.
65                                 ** FIXME: we assume that getopt will not count options
66                                 ** possibly present on "-e prog args" and will not
67                                 ** include them into final value of optind
68                                 ** which is to be used ...  */
69                         } else bb_show_usage();
70                 }
71                 argv += optind; /* ... here! */
72                 argc -= optind;
73                 // -l and -f don't mix
74                 if (do_listen && cfd) bb_show_usage();
75                 // Listen or file modes need zero arguments, client mode needs 2
76                 if (do_listen || cfd) {
77                         if (argc) bb_show_usage();
78                 } else {
79                         if (!argc || argc > 2) bb_show_usage();
80                 }
81         } else {
82                 if (argc != 3) bb_show_usage();
83                 argc--;
84                 argv++;
85         }
86
87         if (wsecs) {
88                 signal(SIGALRM, timeout);
89                 alarm(wsecs);
90         }
91
92         if (!cfd) {
93                 if (do_listen) {
94                         /* create_and_bind_stream_or_die(NULL, lport)
95                          * would've work wonderfully, but we need
96                          * to know lsa */
97                         sfd = xsocket_stream(&lsa);
98                         if (lport)
99                                 set_nport(lsa, htons(lport));
100                         setsockopt_reuseaddr(sfd);
101                         xbind(sfd, &lsa->sa, lsa->len);
102                         xlisten(sfd, do_listen); /* can be > 1 */
103                         /* If we didn't specify a port number,
104                          * query and print it after listen() */
105                         if (!lport) {
106                                 socklen_t addrlen = lsa->len;
107                                 getsockname(sfd, &lsa->sa, &addrlen);
108                                 lport = get_nport(lsa);
109                                 fdprintf(2, "%d\n", ntohs(lport));
110                         }
111                         fcntl(sfd, F_SETFD, FD_CLOEXEC);
112  accept_again:
113                         cfd = accept(sfd, NULL, 0);
114                         if (cfd < 0)
115                                 bb_perror_msg_and_die("accept");
116                         if (!execparam)
117                                 close(sfd);
118                 } else {
119                         cfd = create_and_connect_stream_or_die(argv[0],
120                                 argv[1] ? bb_lookup_port(argv[1], "tcp", 0) : 0);
121                 }
122         }
123
124         if (wsecs) {
125                 alarm(0);
126                 signal(SIGALRM, SIG_DFL);
127         }
128
129         /* -e given? */
130         if (execparam) {
131                 signal(SIGCHLD, SIG_IGN);
132                 // With more than one -l, repeatedly act as server.
133                 if (do_listen > 1 && vfork()) {
134                         /* parent */
135                         // This is a bit weird as cleanup goes, since we wind up with no
136                         // stdin/stdout/stderr.  But it's small and shouldn't hurt anything.
137                         // We check for cfd == 0 above.
138                         logmode = LOGMODE_NONE;
139                         close(0);
140                         close(1);
141                         close(2);
142                         goto accept_again;
143                 }
144                 /* child (or main thread if no multiple -l) */
145                 if (cfd) {
146                         dup2(cfd, 0);
147                         close(cfd);
148                 }
149                 dup2(0, 1);
150                 dup2(0, 2);
151                 USE_NC_EXTRA(execvp(execparam[0], execparam);)
152                 /* Don't print stuff or it will go over the wire.... */
153                 _exit(127);
154         }
155
156         // Select loop copying stdin to cfd, and cfd to stdout.
157
158         FD_ZERO(&readfds);
159         FD_SET(cfd, &readfds);
160         FD_SET(STDIN_FILENO, &readfds);
161
162         for (;;) {
163                 int fd;
164                 int ofd;
165                 int nread;
166
167                 testfds = readfds;
168
169                 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
170                         bb_perror_msg_and_die("select");
171
172                 for (fd = 0; fd < FD_SETSIZE; fd++) {
173                         if (FD_ISSET(fd, &testfds)) {
174                                 nread = safe_read(fd, bb_common_bufsiz1,
175                                                         sizeof(bb_common_bufsiz1));
176
177                                 if (fd == cfd) {
178                                         if (nread < 1)
179                                                 exit(0);
180                                         ofd = STDOUT_FILENO;
181                                 } else {
182                                         if (nread<1) {
183                                                 // Close outgoing half-connection so they get EOF, but
184                                                 // leave incoming alone so we can see response.
185                                                 shutdown(cfd, 1);
186                                                 FD_CLR(STDIN_FILENO, &readfds);
187                                         }
188                                         ofd = cfd;
189                                 }
190
191                                 xwrite(ofd, bb_common_bufsiz1, nread);
192                                 if (delay > 0) sleep(delay);
193                         }
194                 }
195         }
196 }