1 /* vi: set sw=4 ts=4: */
2 /* nc: mini-netcat - built from the ground up for LRP
4 * Copyright (C) 1998, 1999 Charles P. Wright
5 * Copyright (C) 1998 Dave Cinege
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 static void timeout(int signum)
14 bb_error_msg_and_die("timed out");
17 int nc_main(int argc, char **argv)
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 struct hostent *hostinfo;
29 fd_set readfds, testfds;
30 int opt; /* must be signed (getopt returns -1) */
32 memset(&address, 0, sizeof(address));
34 if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
35 /* getopt32 is _almost_ usable:
36 ** it cannot handle "... -e prog -prog-opt" */
37 while ((opt = getopt(argc, argv,
38 "" USE_NC_SERVER("lp:") USE_NC_EXTRA("w:i:f:e:") )) > 0
40 if (ENABLE_NC_SERVER && opt=='l') USE_NC_SERVER(do_listen++);
41 else if (ENABLE_NC_SERVER && opt=='p') USE_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0));
42 else if (ENABLE_NC_EXTRA && opt=='w') USE_NC_EXTRA( wsecs = xatou(optarg));
43 else if (ENABLE_NC_EXTRA && opt=='i') USE_NC_EXTRA( delay = xatou(optarg));
44 else if (ENABLE_NC_EXTRA && opt=='f') USE_NC_EXTRA( cfd = xopen(optarg, O_RDWR));
45 else if (ENABLE_NC_EXTRA && opt=='e' && optind<=argc) {
46 /* We cannot just 'break'. We should let getopt finish.
47 ** Or else we won't be able to find where
48 ** 'host' and 'port' params are
49 ** (think "nc -w 60 host port -e prog"). */
52 // +2: one for progname (optarg) and one for NULL
53 execparam = xzalloc(sizeof(char*) * (argc - optind + 2));
56 while (optind < argc) {
57 *p++ = argv[optind++];
60 /* optind points to argv[arvc] (NULL) now.
61 ** FIXME: we assume that getopt will not count options
62 ** possibly present on "-e prog args" and will not
63 ** include them into final value of optind
64 ** which is to be used ... */
65 } else bb_show_usage();
67 argv += optind; /* ... here! */
69 // -l and -f don't mix
70 if (do_listen && cfd) bb_show_usage();
71 // Listen or file modes need zero arguments, client mode needs 2
72 opt = ((do_listen || cfd) ? 0 : 2);
76 if (argc != 3) bb_show_usage();
82 signal(SIGALRM, timeout);
87 sfd = xsocket(AF_INET, SOCK_STREAM, 0);
88 fcntl(sfd, F_SETFD, FD_CLOEXEC);
90 setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
91 address.sin_family = AF_INET;
96 address.sin_port = lport;
97 xbind(sfd, (struct sockaddr *) &address, sizeof(address));
101 socklen_t addrlen = sizeof(address);
103 xlisten(sfd, do_listen);
105 // If we didn't specify a port number, query and print it to stderr.
108 socklen_t len = sizeof(address);
109 getsockname(sfd, &address, &len);
110 fdprintf(2, "%d\n", SWAP_BE16(address.sin_port));
113 cfd = accept(sfd, (struct sockaddr *) &address, &addrlen);
115 bb_perror_msg_and_die("accept");
117 if (!execparam) close(sfd);
119 hostinfo = xgethostbyname(argv[0]);
121 address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
122 address.sin_port = bb_lookup_port(argv[1], "tcp", 0);
124 if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
125 bb_perror_msg_and_die("connect");
132 signal(SIGALRM, SIG_DFL);
138 signal(SIGCHLD, SIG_IGN);
145 // With more than one -l, repeatedly act as server.
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;
158 USE_NC_EXTRA(execvp(execparam[0], execparam);)
159 /* Don't print stuff or it will go over the wire.... */
163 // Select loop copying stdin to cfd, and cfd to stdout.
166 FD_SET(cfd, &readfds);
167 FD_SET(STDIN_FILENO, &readfds);
176 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
177 bb_perror_msg_and_die("select");
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));
185 if (nread<1) exit(0);
189 // Close outgoing half-connection so they get EOF, but
190 // leave incoming alone so we can see response.
192 FD_CLR(STDIN_FILENO, &readfds);
197 xwrite(ofd, bb_common_bufsiz1, nread);
198 if (delay > 0) sleep(delay);