1 /* vi: set sw=4 ts=4: */
3 * nc: mini-netcat - built from the ground up for LRP
5 * Copyright (C) 1998, 1999 Charles P. Wright
6 * Copyright (C) 1998 Dave Cinege
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 //config: bool "nc (11 kb)"
14 //config: A simple Unix utility which reads and writes data across network
15 //config: connections.
17 //config:config NETCAT
18 //config: bool "netcat (11 kb)"
21 //config: Alias to nc.
23 //config:config NC_SERVER
24 //config: bool "Netcat server options (-l)"
26 //config: depends on NC || NETCAT
28 //config: Allow netcat to act as a server.
30 //config:config NC_EXTRA
31 //config: bool "Netcat extensions (-eiw and -f FILE)"
33 //config: depends on NC || NETCAT
35 //config: Add -e (support for executing the rest of the command line after
36 //config: making or receiving a successful connection), -i (delay interval for
37 //config: lines sent), -w (timeout for initial connection).
39 //config:config NC_110_COMPAT
40 //config: bool "Netcat 1.10 compatibility (+2.5k)"
42 //config: depends on NC || NETCAT
44 //config: This option makes nc closely follow original nc-1.10.
45 //config: The code is about 2.5k bigger. It enables
46 //config: -s ADDR, -n, -u, -v, -o FILE, -z options, but loses
47 //config: busybox-specific extensions: -f FILE.
49 //applet:IF_NC(APPLET(nc, BB_DIR_USR_BIN, BB_SUID_DROP))
50 // APPLET_ODDNAME:name main location suid_type help
51 //applet:IF_NETCAT(APPLET_ODDNAME(netcat, nc, BB_DIR_USR_BIN, BB_SUID_DROP, nc))
53 //kbuild:lib-$(CONFIG_NC) += nc.o
54 //kbuild:lib-$(CONFIG_NETCAT) += nc.o
57 #include "common_bufsiz.h"
58 #if ENABLE_NC_110_COMPAT
59 # include "nc_bloaty.c"
62 //usage:#if !ENABLE_NC_110_COMPAT
64 //usage:#if ENABLE_NC_SERVER || ENABLE_NC_EXTRA
65 //usage:#define NC_OPTIONS_STR "\n"
67 //usage:#define NC_OPTIONS_STR
70 //usage:#define nc_trivial_usage
71 //usage: IF_NC_EXTRA("[-iN] [-wN] ")IF_NC_SERVER("[-l] [-p PORT] ")
72 //usage: "["IF_NC_EXTRA("-f FILE|")"IPADDR PORT]"IF_NC_EXTRA(" [-e PROG]")
73 //usage:#define nc_full_usage "\n\n"
74 //usage: "Open a pipe to IP:PORT" IF_NC_EXTRA(" or FILE")
75 //usage: NC_OPTIONS_STR
76 //usage: IF_NC_SERVER(
77 //usage: "\n -l Listen mode, for inbound connects"
79 //usage: "\n (use -ll with -e for persistent server)"
81 //usage: "\n -p PORT Local port"
84 //usage: "\n -w SEC Connect timeout"
85 //usage: "\n -i SEC Delay interval for lines sent"
86 //usage: "\n -f FILE Use file (ala /dev/ttyS0) instead of network"
87 //usage: "\n -e PROG Run PROG after connect"
90 //usage:#define nc_notes_usage ""
92 //usage: "To use netcat as a terminal emulator on a serial port:\n\n"
93 //usage: "$ stty 115200 -F /dev/ttyS0\n"
94 //usage: "$ stty raw -echo -ctlecho && nc -f /dev/ttyS0\n"
97 //usage:#define nc_example_usage
98 //usage: "$ nc foobar.somedomain.com 25\n"
99 //usage: "220 foobar ESMTP Exim 3.12 #1 Sat, 15 Apr 2000 00:03:02 -0600\n"
101 //usage: "214-Commands supported:\n"
102 //usage: "214- HELO EHLO MAIL RCPT DATA AUTH\n"
103 //usage: "214 NOOP QUIT RSET HELP\n"
105 //usage: "221 foobar closing connection\n"
109 /* Lots of small differences in features
110 * when compared to "standard" nc
113 static void timeout(int signum UNUSED_PARAM)
115 bb_error_msg_and_die("timed out");
118 int nc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
119 int nc_main(int argc, char **argv)
121 /* sfd sits _here_ only because of "repeat" option (-l -l). */
122 int sfd = sfd; /* for gcc */
125 IF_NOT_NC_SERVER(const) unsigned do_listen = 0;
126 IF_NOT_NC_EXTRA (const) unsigned wsecs = 0;
127 IF_NOT_NC_EXTRA (const) unsigned delay = 0;
128 IF_NOT_NC_EXTRA (const int execparam = 0;)
129 IF_NC_EXTRA (char **execparam = NULL;)
130 struct pollfd pfds[2];
131 int opt; /* must be signed (getopt returns -1) */
133 if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
134 /* getopt32 is _almost_ usable:
135 ** it cannot handle "... -e PROG -prog-opt" */
136 while ((opt = getopt(argc, argv,
137 "" IF_NC_SERVER("lp:") IF_NC_EXTRA("w:i:f:e:") )) > 0
139 if (ENABLE_NC_SERVER && opt == 'l')
140 IF_NC_SERVER(do_listen++);
141 else if (ENABLE_NC_SERVER && opt == 'p')
142 IF_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0));
143 else if (ENABLE_NC_EXTRA && opt == 'w')
144 IF_NC_EXTRA( wsecs = xatou(optarg));
145 else if (ENABLE_NC_EXTRA && opt == 'i')
146 IF_NC_EXTRA( delay = xatou(optarg));
147 else if (ENABLE_NC_EXTRA && opt == 'f')
148 IF_NC_EXTRA( cfd = xopen(optarg, O_RDWR));
149 else if (ENABLE_NC_EXTRA && opt == 'e' && optind <= argc) {
150 /* We cannot just 'break'. We should let getopt finish.
151 ** Or else we won't be able to find where
152 ** 'host' and 'port' params are
153 ** (think "nc -w 60 host port -e PROG"). */
156 // +2: one for progname (optarg) and one for NULL
157 execparam = xzalloc(sizeof(char*) * (argc - optind + 2));
160 while (optind < argc) {
161 *p++ = argv[optind++];
164 /* optind points to argv[argc] (NULL) now.
165 ** FIXME: we assume that getopt will not count options
166 ** possibly present on "-e PROG ARGS" and will not
167 ** include them into final value of optind
168 ** which is to be used ... */
169 } else bb_show_usage();
171 argv += optind; /* ... here! */
173 // -l and -f don't mix
174 if (do_listen && cfd) bb_show_usage();
175 // File mode needs need zero arguments, listen mode needs zero or one,
176 // client mode needs one or two
178 if (argc) bb_show_usage();
179 } else if (do_listen) {
180 if (argc > 1) bb_show_usage();
182 if (!argc || argc > 2) bb_show_usage();
185 if (argc != 3) bb_show_usage();
191 signal(SIGALRM, timeout);
197 sfd = create_and_bind_stream_or_die(argv[0], lport);
198 xlisten(sfd, do_listen); /* can be > 1 */
199 #if 0 /* nc-1.10 does not do this (without -v) */
200 /* If we didn't specify a port number,
201 * query and print it after listen() */
203 len_and_sockaddr lsa;
204 lsa.len = LSA_SIZEOF_SA;
205 getsockname(sfd, &lsa.u.sa, &lsa.len);
206 lport = get_nport(&lsa.u.sa);
207 fdprintf(2, "%d\n", ntohs(lport));
210 close_on_exec_on(sfd);
212 cfd = accept(sfd, NULL, 0);
214 bb_perror_msg_and_die("accept");
218 cfd = create_and_connect_stream_or_die(argv[0],
219 argv[1] ? bb_lookup_port(argv[1], "tcp", 0) : 0);
225 /* Non-ignored signals revert to SIG_DFL on exec anyway */
226 /*signal(SIGALRM, SIG_DFL);*/
232 /* With more than one -l, repeatedly act as server */
233 if (do_listen > 1 && (pid = xvfork()) != 0) {
235 /* prevent zombies */
236 signal(SIGCHLD, SIG_IGN);
240 /* child, or main thread if only one -l */
243 /*xdup2(0, 2); - original nc 1.10 does this, we don't */
244 IF_NC_EXTRA(BB_EXECVP(execparam[0], execparam);)
245 IF_NC_EXTRA(bb_perror_msg_and_die("can't execute '%s'", execparam[0]);)
248 /* loop copying stdin to cfd, and cfd to stdout */
250 pfds[0].fd = STDIN_FILENO;
251 pfds[0].events = POLLIN;
253 pfds[1].events = POLLIN;
255 #define iobuf bb_common_bufsiz1
256 setup_common_bufsiz();
262 if (safe_poll(pfds, 2, -1) < 0)
263 bb_perror_msg_and_die("poll");
267 if (pfds[fdidx].revents) {
268 nread = safe_read(pfds[fdidx].fd, iobuf, COMMON_BUFSIZE);
275 /* Close outgoing half-connection so they get EOF,
276 * but leave incoming alone so we can see response */
277 shutdown(cfd, SHUT_WR);
282 xwrite(ofd, iobuf, nread);