attempt to regularize atoi mess.
[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, cfd;
20         unsigned opt;
21         unsigned lport = 0, wsecs = 0, delay = 0;
22         unsigned do_listen = 0, execflag = 0;
23         struct sockaddr_in address;
24         struct hostent *hostinfo;
25         fd_set readfds, testfds;
26         char *infile = NULL;
27
28         memset(&address, 0, sizeof(address));
29
30         if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
31                 while ((opt = getopt(argc, argv, "lp:" USE_NC_EXTRA("i:ew:f:"))) > 0) {
32                         if (ENABLE_NC_SERVER && opt=='l') do_listen++;
33                         else if (ENABLE_NC_SERVER && opt=='p')
34                                 lport = bb_lookup_port(optarg, "tcp", 0);
35                         else if (ENABLE_NC_EXTRA && opt=='w') wsecs = xatou(optarg);
36                         else if (ENABLE_NC_EXTRA && opt=='i') delay = xatou(optarg);
37                         else if (ENABLE_NC_EXTRA && opt=='f') infile = optarg;
38                         else if (ENABLE_NC_EXTRA && opt=='e' && optind!=argc) {
39                                 execflag++;
40                                 break;
41                         } else bb_show_usage();
42                 }
43         }
44
45         // For listen or file we need zero arguments, dialout is 2.
46         // For exec we need at least one more argument at the end, more ok
47
48         opt = (do_listen || infile) ? 0 : 2 + execflag;
49         if (execflag ? argc-optind < opt : argc-optind!=opt ||
50                 (infile && do_listen))
51                         bb_show_usage();
52
53         if (wsecs) {
54                 signal(SIGALRM, timeout);
55                 alarm(wsecs);
56         }
57
58         if (infile) cfd = xopen(infile, O_RDWR);
59         else {
60                 opt = 1;
61                 sfd = xsocket(AF_INET, SOCK_STREAM, 0);
62                 fcntl(sfd, F_SETFD, FD_CLOEXEC);
63                 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
64                 address.sin_family = AF_INET;
65
66                 // Set local port.
67
68                 if (lport != 0) {
69                         address.sin_port = lport;
70                         xbind(sfd, (struct sockaddr *) &address, sizeof(address));
71                 }
72
73                 if (do_listen) {
74                         socklen_t addrlen = sizeof(address);
75
76                         xlisten(sfd, do_listen);
77
78                         // If we didn't specify a port number, query and print it to stderr.
79
80                         if (!lport) {
81                                 socklen_t len = sizeof(address);
82                                 getsockname(sfd, &address, &len);
83                                 fdprintf(2, "%d\n", SWAP_BE16(address.sin_port));
84                         }
85 repeatyness:
86                         cfd = accept(sfd, (struct sockaddr *) &address, &addrlen);
87                         if (cfd < 0)
88                                 bb_perror_msg_and_die("accept");
89
90                         if (!execflag) close(sfd);
91                 } else {
92                         hostinfo = xgethostbyname(argv[optind]);
93
94                         address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
95                         address.sin_port = bb_lookup_port(argv[optind+1], "tcp", 0);
96
97                         if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
98                                 bb_perror_msg_and_die("connect");
99                         cfd = sfd;
100                 }
101         }
102
103         if (wsecs) {
104                 alarm(0);
105                 signal(SIGALRM, SIG_DFL);
106         }
107
108         /* -e given? */
109         if (execflag) {
110                 if(cfd) {
111                         signal(SIGCHLD, SIG_IGN);
112                         dup2(cfd, 0);
113                         close(cfd);
114                 }
115                 dup2(0, 1);
116                 dup2(0, 2);
117
118                 // With more than one -l, repeatedly act as server.
119
120                 if (do_listen > 1 && vfork()) {
121                         // This is a bit weird as cleanup goes, since we wind up with no
122                         // stdin/stdout/stderr.  But it's small and shouldn't hurt anything.
123                         // We check for cfd == 0 above.
124                         logmode = LOGMODE_NONE;
125                         close(0);
126                         close(1);
127                         close(2);
128
129                         goto repeatyness;
130                 }
131                 execvp(argv[optind], argv+optind);
132                 /* Don't print stuff or it will go over the wire.... */
133                 _exit(127);
134         }
135
136         // Select loop copying stdin to cfd, and cfd to stdout.
137
138         FD_ZERO(&readfds);
139         FD_SET(cfd, &readfds);
140         FD_SET(STDIN_FILENO, &readfds);
141
142         for (;;) {
143                 int fd;
144                 int ofd;
145                 int nread;
146
147                 testfds = readfds;
148
149                 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
150                         bb_perror_msg_and_die("select");
151
152                 for (fd = 0; fd < FD_SETSIZE; fd++) {
153                         if (FD_ISSET(fd, &testfds)) {
154                                 nread = safe_read(fd, bb_common_bufsiz1,
155                                                         sizeof(bb_common_bufsiz1));
156
157                                 if (fd == cfd) {
158                                         if (nread<1) exit(0);
159                                         ofd = STDOUT_FILENO;
160                                 } else {
161                                         if (nread<1) {
162                                                 // Close outgoing half-connection so they get EOF, but
163                                                 // leave incoming alone so we can see response.
164                                                 shutdown(cfd, 1);
165                                                 FD_CLR(STDIN_FILENO, &readfds);
166                                         }
167                                         ofd = cfd;
168                                 }
169
170                                 xwrite(ofd, bb_common_bufsiz1, nread);
171                                 if (delay > 0) sleep(delay);
172                         }
173                 }
174         }
175 }