small ipv6 doc changes; nslookup a tiny bit smaller
[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') USE_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0));
41                         else if (ENABLE_NC_EXTRA  && opt=='w') USE_NC_EXTRA( wsecs = xatou(optarg));
42                         else if (ENABLE_NC_EXTRA  && opt=='i') USE_NC_EXTRA( delay = xatou(optarg));
43                         else if (ENABLE_NC_EXTRA  && opt=='f') USE_NC_EXTRA( cfd = xopen(optarg, O_RDWR));
44                         else if (ENABLE_NC_EXTRA  && opt=='e' && optind<=argc) {
45                                 /* We cannot just 'break'. We should let getopt finish.
46                                 ** Or else we won't be able to find where
47                                 ** 'host' and 'port' params are
48                                 ** (think "nc -w 60 host port -e prog"). */
49                                 USE_NC_EXTRA(
50                                         char **p;
51                                         // +2: one for progname (optarg) and one for NULL
52                                         execparam = xzalloc(sizeof(char*) * (argc - optind + 2));
53                                         p = execparam;
54                                         *p++ = optarg;
55                                         while (optind < argc) {
56                                                 *p++ = argv[optind++];
57                                         }
58                                 )
59                                 /* optind points to argv[arvc] (NULL) now.
60                                 ** FIXME: we assume that getopt will not count options
61                                 ** possibly present on "-e prog args" and will not
62                                 ** include them into final value of optind
63                                 ** which is to be used ...  */
64                         } else bb_show_usage();
65                 }
66                 argv += optind; /* ... here! */
67                 argc -= optind;
68                 // -l and -f don't mix
69                 if (do_listen && cfd) bb_show_usage();
70                 // Listen or file modes need zero arguments, client mode needs 2
71                 opt = ((do_listen || cfd) ? 0 : 2);
72                 if (argc != opt)
73                         bb_show_usage();
74         } else {
75                 if (argc != 3) bb_show_usage();
76                 argc--;
77                 argv++;
78         }
79
80         if (wsecs) {
81                 signal(SIGALRM, timeout);
82                 alarm(wsecs);
83         }
84
85         if (!cfd) {
86                 sfd = xsocket(AF_INET, SOCK_STREAM, 0);
87                 fcntl(sfd, F_SETFD, FD_CLOEXEC);
88                 opt = 1;
89                 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
90                 address.sin_family = AF_INET;
91
92                 // Set local port.
93
94                 if (lport != 0) {
95                         address.sin_port = lport;
96                         xbind(sfd, (struct sockaddr *) &address, sizeof(address));
97                 }
98
99                 if (do_listen) {
100                         socklen_t addrlen = sizeof(address);
101
102                         xlisten(sfd, do_listen);
103
104                         // If we didn't specify a port number, query and print it to stderr.
105
106                         if (!lport) {
107                                 socklen_t len = sizeof(address);
108                                 getsockname(sfd, &address, &len);
109                                 fdprintf(2, "%d\n", SWAP_BE16(address.sin_port));
110                         }
111  repeatyness:
112                         cfd = accept(sfd, (struct sockaddr *) &address, &addrlen);
113                         if (cfd < 0)
114                                 bb_perror_msg_and_die("accept");
115
116                         if (!execparam) close(sfd);
117                 } else {
118                         struct hostent *hostinfo;
119                         hostinfo = xgethostbyname(argv[0]);
120
121                         address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
122                         address.sin_port = bb_lookup_port(argv[1], "tcp", 0);
123
124                         if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
125                                 bb_perror_msg_and_die("connect");
126                         cfd = sfd;
127                 }
128         }
129
130         if (wsecs) {
131                 alarm(0);
132                 signal(SIGALRM, SIG_DFL);
133         }
134
135         /* -e given? */
136         if (execparam) {
137                 if (cfd) {
138                         signal(SIGCHLD, SIG_IGN);
139                         dup2(cfd, 0);
140                         close(cfd);
141                 }
142                 dup2(0, 1);
143                 dup2(0, 2);
144
145                 // With more than one -l, repeatedly act as server.
146
147                 if (do_listen > 1 && vfork()) {
148                         // This is a bit weird as cleanup goes, since we wind up with no
149                         // stdin/stdout/stderr.  But it's small and shouldn't hurt anything.
150                         // We check for cfd == 0 above.
151                         logmode = LOGMODE_NONE;
152                         close(0);
153                         close(1);
154                         close(2);
155
156                         goto repeatyness;
157                 }
158                 USE_NC_EXTRA(execvp(execparam[0], execparam);)
159                 /* Don't print stuff or it will go over the wire.... */
160                 _exit(127);
161         }
162
163         // Select loop copying stdin to cfd, and cfd to stdout.
164
165         FD_ZERO(&readfds);
166         FD_SET(cfd, &readfds);
167         FD_SET(STDIN_FILENO, &readfds);
168
169         for (;;) {
170                 int fd;
171                 int ofd;
172                 int nread;
173
174                 testfds = readfds;
175
176                 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
177                         bb_perror_msg_and_die("select");
178
179                 for (fd = 0; fd < FD_SETSIZE; fd++) {
180                         if (FD_ISSET(fd, &testfds)) {
181                                 nread = safe_read(fd, bb_common_bufsiz1,
182                                                         sizeof(bb_common_bufsiz1));
183
184                                 if (fd == cfd) {
185                                         if (nread<1) exit(0);
186                                         ofd = STDOUT_FILENO;
187                                 } else {
188                                         if (nread<1) {
189                                                 // Close outgoing half-connection so they get EOF, but
190                                                 // leave incoming alone so we can see response.
191                                                 shutdown(cfd, 1);
192                                                 FD_CLR(STDIN_FILENO, &readfds);
193                                         }
194                                         ofd = cfd;
195                                 }
196
197                                 xwrite(ofd, bb_common_bufsiz1, nread);
198                                 if (delay > 0) sleep(delay);
199                         }
200                 }
201         }
202 }