Upgrade netcat a lot. Make -e able to take the rest of the command line as
[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 #define xread bb_xread
13
14 static void timeout(int signum)
15 {
16         bb_error_msg_and_die("Timed out");
17 }
18
19 int nc_main(int argc, char **argv)
20 {
21         int do_listen = 0, lport = 0, delay = 0, wsecs = 0, execflag = 0, opt,
22                 sfd = 0, cfd;
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 = atoi(optarg);
36                         else if (ENABLE_NC_EXTRA && opt=='i') delay = atoi(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         
46         // For listen or file we need zero arguments, dialout is 2.
47         // For exec we need at least one more argument at the end, more ok
48
49         opt = (do_listen  || infile) ? 0 : 2 + execflag;
50         if (execflag ? argc-optind < opt : argc-optind!=opt ||
51                 (infile && do_listen))
52                         bb_show_usage();
53
54         if (wsecs) {
55                 signal(SIGALRM, timeout);
56                 alarm(wsecs);
57         }
58         
59         if (infile) cfd = bb_xopen(infile, O_RDWR);
60         else {
61                 opt = 1;
62                 sfd = bb_xsocket(AF_INET, SOCK_STREAM, 0);
63                 fcntl(sfd, F_SETFD, FD_CLOEXEC);
64                 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
65                 address.sin_family = AF_INET;
66
67                 // Set local port.
68
69                 if (lport != 0) {
70                         address.sin_port = lport;
71
72                         bb_xbind(sfd, (struct sockaddr *) &address, sizeof(address));
73                 }
74
75                 if (do_listen) {
76                         socklen_t addrlen = sizeof(address);
77
78                         bb_xlisten(sfd, do_listen);
79
80                         // If we didn't specify a port number, query and print it to stderr.
81
82                         if (!lport) {
83                                 socklen_t len = sizeof(address);
84                                 getsockname(sfd, &address, &len);
85                                 fdprintf(2, "%d\n", SWAP_BE16(address.sin_port));
86                         }
87 repeatyness:
88                         if ((cfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0)
89                                 bb_perror_msg_and_die("accept");
90
91                         if (!execflag) close(sfd);
92                 } else {
93                         hostinfo = xgethostbyname(argv[optind]);
94
95                         address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
96                         address.sin_port = bb_lookup_port(argv[optind+1], "tcp", 0);
97
98                         if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
99                                 bb_perror_msg_and_die("connect");
100                         cfd = sfd;
101                 }
102         }
103
104         if (wsecs) {
105                 alarm(0);
106                 signal(SIGALRM, SIG_DFL);
107         }
108
109         /* -e given? */
110         if (execflag) {
111                 if(cfd) {
112                         signal(SIGCHLD, SIG_IGN);
113                         dup2(cfd, 0);
114                         close(cfd);
115                 }
116                 dup2(0, 1);
117                 dup2(0, 2);
118
119                 // With more than one -l, repeatedly act as server.
120
121                 if (do_listen>1 && vfork()) {
122                         // This is a bit weird as cleanup goes, since we wind up with no
123                         // stdin/stdout/stderr.  But it's small and shouldn't hurt anything.
124                         // We check for cfd == 0 above.
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 = xread(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1));
155
156                                 if (fd == cfd) {
157                                         if (!nread) exit(0);
158                                         ofd = STDOUT_FILENO;
159                                 } else {
160                                         if (!nread) {
161                                                 // Close outgoing half-connection so they get EOF, but
162                                                 // leave incoming alone so we can see response.
163                                                 shutdown(cfd, 1);
164                                                 FD_CLR(STDIN_FILENO, &readfds);
165                                         }
166                                         ofd = cfd;
167                                 }
168
169                                 if (bb_full_write(ofd, bb_common_bufsiz1, nread) < 0)
170                                         bb_perror_msg_and_die(bb_msg_write_error);
171                                 if (delay > 0) sleep(delay);
172                         }
173                 }
174         }
175 }