blob: 243c47976b9e6a0f5e1ad183840c5cb39f353314 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen7ab9c7e2000-05-12 19:41:47 +00002/* nc: mini-netcat - built from the ground up for LRP
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00003 *
Rob Landley1cca9482006-07-10 19:45:20 +00004 * Copyright (C) 1998, 1999 Charles P. Wright
5 * Copyright (C) 1998 Dave Cinege
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00006 *
Rob Landley1cca9482006-07-10 19:45:20 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
Eric Andersencc8ed391999-10-05 16:24:54 +00009
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000011
Denis Vlasenko29fe7262007-04-05 20:26:28 +000012#if ENABLE_DESKTOP
13#include "nc_bloaty.c"
14#else
15
Denis Vlasenko5d687242007-01-12 20:59:31 +000016/* Lots of small differences in features
17 * when compared to "standard" nc
18 */
19
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000020static void timeout(int signum UNUSED_PARAM)
Mike Frysinger60a5c382005-05-06 04:45:38 +000021{
Denis Vlasenko13858992006-10-08 12:49:22 +000022 bb_error_msg_and_die("timed out");
Mike Frysinger60a5c382005-05-06 04:45:38 +000023}
24
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000025int nc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000026int nc_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000027{
Denis Vlasenko5d687242007-01-12 20:59:31 +000028 /* sfd sits _here_ only because of "repeat" option (-l -l). */
29 int sfd = sfd; /* for gcc */
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000030 int cfd = 0;
Denis Vlasenko31635552007-01-20 16:54:19 +000031 unsigned lport = 0;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000032 IF_NOT_NC_SERVER(const) unsigned do_listen = 0;
33 IF_NOT_NC_EXTRA (const) unsigned wsecs = 0;
34 IF_NOT_NC_EXTRA (const) unsigned delay = 0;
35 IF_NOT_NC_EXTRA (const int execparam = 0;)
36 IF_NC_EXTRA (char **execparam = NULL;)
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 fd_set readfds, testfds;
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000038 int opt; /* must be signed (getopt returns -1) */
Erik Andersene49d5ec2000-02-08 19:58:47 +000039
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000040 if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) {
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000041 /* getopt32 is _almost_ usable:
42 ** it cannot handle "... -e prog -prog-opt" */
43 while ((opt = getopt(argc, argv,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000044 "" IF_NC_SERVER("lp:") IF_NC_EXTRA("w:i:f:e:") )) > 0
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000045 ) {
Denis Vlasenkof6b46852009-04-25 13:16:53 +000046 if (ENABLE_NC_SERVER && opt == 'l')
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000047 IF_NC_SERVER(do_listen++);
Denis Vlasenkof6b46852009-04-25 13:16:53 +000048 else if (ENABLE_NC_SERVER && opt == 'p')
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000049 IF_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0));
Denis Vlasenkof6b46852009-04-25 13:16:53 +000050 else if (ENABLE_NC_EXTRA && opt == 'w')
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000051 IF_NC_EXTRA( wsecs = xatou(optarg));
Denis Vlasenkof6b46852009-04-25 13:16:53 +000052 else if (ENABLE_NC_EXTRA && opt == 'i')
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000053 IF_NC_EXTRA( delay = xatou(optarg));
Denis Vlasenkof6b46852009-04-25 13:16:53 +000054 else if (ENABLE_NC_EXTRA && opt == 'f')
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000055 IF_NC_EXTRA( cfd = xopen(optarg, O_RDWR));
Denis Vlasenkof6b46852009-04-25 13:16:53 +000056 else if (ENABLE_NC_EXTRA && opt == 'e' && optind <= argc) {
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000057 /* We cannot just 'break'. We should let getopt finish.
58 ** Or else we won't be able to find where
59 ** 'host' and 'port' params are
60 ** (think "nc -w 60 host port -e prog"). */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000061 IF_NC_EXTRA(
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000062 char **p;
63 // +2: one for progname (optarg) and one for NULL
64 execparam = xzalloc(sizeof(char*) * (argc - optind + 2));
65 p = execparam;
66 *p++ = optarg;
67 while (optind < argc) {
68 *p++ = argv[optind++];
69 }
70 )
71 /* optind points to argv[arvc] (NULL) now.
72 ** FIXME: we assume that getopt will not count options
73 ** possibly present on "-e prog args" and will not
74 ** include them into final value of optind
75 ** which is to be used ... */
Rob Landley1cca9482006-07-10 19:45:20 +000076 } else bb_show_usage();
Matt Kraai1d702672001-02-07 04:09:23 +000077 }
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000078 argv += optind; /* ... here! */
79 argc -= optind;
80 // -l and -f don't mix
81 if (do_listen && cfd) bb_show_usage();
Denis Vlasenkof6b46852009-04-25 13:16:53 +000082 // File mode needs need zero arguments, listen mode needs zero or one,
83 // client mode needs one or two
84 if (cfd) {
Denis Vlasenko5d687242007-01-12 20:59:31 +000085 if (argc) bb_show_usage();
Denis Vlasenkof6b46852009-04-25 13:16:53 +000086 } else if (do_listen) {
87 if (argc > 1) bb_show_usage();
Denis Vlasenko5d687242007-01-12 20:59:31 +000088 } else {
89 if (!argc || argc > 2) bb_show_usage();
90 }
Denis Vlasenkod0e70af2006-10-16 01:10:28 +000091 } else {
92 if (argc != 3) bb_show_usage();
93 argc--;
94 argv++;
95 }
Eric Andersencc8ed391999-10-05 16:24:54 +000096
Mike Frysinger60a5c382005-05-06 04:45:38 +000097 if (wsecs) {
98 signal(SIGALRM, timeout);
99 alarm(wsecs);
100 }
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000101
Denis Vlasenkod0e70af2006-10-16 01:10:28 +0000102 if (!cfd) {
Rob Landley1cca9482006-07-10 19:45:20 +0000103 if (do_listen) {
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000104 sfd = create_and_bind_stream_or_die(argv[0], lport);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000105 xlisten(sfd, do_listen); /* can be > 1 */
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000106#if 0 /* nc-1.10 does not do this (without -v) */
Denis Vlasenko5d687242007-01-12 20:59:31 +0000107 /* If we didn't specify a port number,
108 * query and print it after listen() */
Rob Landley1cca9482006-07-10 19:45:20 +0000109 if (!lport) {
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000110 len_and_sockaddr lsa;
111 lsa.len = LSA_SIZEOF_SA;
112 getsockname(sfd, &lsa.u.sa, &lsa.len);
113 lport = get_nport(&lsa.u.sa);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000114 fdprintf(2, "%d\n", ntohs(lport));
Rob Landley1cca9482006-07-10 19:45:20 +0000115 }
Denis Vlasenkof6b46852009-04-25 13:16:53 +0000116#endif
Denis Vlasenko96e1b382007-09-30 23:50:48 +0000117 close_on_exec_on(sfd);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000118 accept_again:
Denis Vlasenko703e2022007-01-22 14:12:08 +0000119 cfd = accept(sfd, NULL, 0);
Denis Vlasenko13858992006-10-08 12:49:22 +0000120 if (cfd < 0)
Rob Landley1cca9482006-07-10 19:45:20 +0000121 bb_perror_msg_and_die("accept");
Denis Vlasenko5d687242007-01-12 20:59:31 +0000122 if (!execparam)
123 close(sfd);
Rob Landley1cca9482006-07-10 19:45:20 +0000124 } else {
Denis Vlasenko5d687242007-01-12 20:59:31 +0000125 cfd = create_and_connect_stream_or_die(argv[0],
126 argv[1] ? bb_lookup_port(argv[1], "tcp", 0) : 0);
Rob Landley1cca9482006-07-10 19:45:20 +0000127 }
Matt Kraai1d702672001-02-07 04:09:23 +0000128 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000129
Mike Frysinger60a5c382005-05-06 04:45:38 +0000130 if (wsecs) {
131 alarm(0);
Denys Vlasenko28a105d2009-06-01 11:26:30 +0200132 /* Non-ignored signals revert to SIG_DFL on exec anyway */
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000133 /*signal(SIGALRM, SIG_DFL);*/
Mike Frysinger60a5c382005-05-06 04:45:38 +0000134 }
135
Eric Andersen1323c942002-04-26 23:59:12 +0000136 /* -e given? */
Denis Vlasenkod0e70af2006-10-16 01:10:28 +0000137 if (execparam) {
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200138 pid_t pid;
139 /* With more than one -l, repeatedly act as server */
140 if (do_listen > 1 && (pid = vfork()) != 0) {
141 /* parent or error */
142 if (pid < 0)
143 bb_perror_msg_and_die("vfork");
144 /* prevent zombies */
145 signal(SIGCHLD, SIG_IGN);
146 close(cfd);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000147 goto accept_again;
Rob Landley1cca9482006-07-10 19:45:20 +0000148 }
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200149 /* child, or main thread if only one -l */
Denis Vlasenkofa0b56d2008-07-01 16:09:07 +0000150 xmove_fd(cfd, 0);
151 xdup2(0, 1);
152 xdup2(0, 2);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000153 IF_NC_EXTRA(BB_EXECVP(execparam[0], execparam);)
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200154 /* Don't print stuff or it will go over the wire... */
Rob Landley1cca9482006-07-10 19:45:20 +0000155 _exit(127);
156 }
157
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200158 /* Select loop copying stdin to cfd, and cfd to stdout */
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000159
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 FD_ZERO(&readfds);
Rob Landley1cca9482006-07-10 19:45:20 +0000161 FD_SET(cfd, &readfds);
Matt Kraaibfa79672000-12-15 22:34:34 +0000162 FD_SET(STDIN_FILENO, &readfds);
Eric Andersencc8ed391999-10-05 16:24:54 +0000163
Rob Landley1cca9482006-07-10 19:45:20 +0000164 for (;;) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 int fd;
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000166 int ofd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 int nread;
Eric Andersencc8ed391999-10-05 16:24:54 +0000168
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 testfds = readfds;
Eric Andersencc8ed391999-10-05 16:24:54 +0000170
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200171 if (select(cfd + 1, &testfds, NULL, NULL, NULL) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 bb_perror_msg_and_die("select");
Eric Andersencc8ed391999-10-05 16:24:54 +0000173
Denis Vlasenko74324c82007-06-04 10:16:52 +0000174#define iobuf bb_common_bufsiz1
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200175 fd = STDIN_FILENO;
176 while (1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 if (FD_ISSET(fd, &testfds)) {
Denis Vlasenko74324c82007-06-04 10:16:52 +0000178 nread = safe_read(fd, iobuf, sizeof(iobuf));
Rob Landley1cca9482006-07-10 19:45:20 +0000179 if (fd == cfd) {
Denis Vlasenko5d687242007-01-12 20:59:31 +0000180 if (nread < 1)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000181 exit(EXIT_SUCCESS);
Matt Kraaibfa79672000-12-15 22:34:34 +0000182 ofd = STDOUT_FILENO;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 } else {
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200184 if (nread < 1) {
185 /* Close outgoing half-connection so they get EOF,
186 * but leave incoming alone so we can see response */
Rob Landley1cca9482006-07-10 19:45:20 +0000187 shutdown(cfd, 1);
Paul Fox7b71d742005-07-18 22:23:16 +0000188 FD_CLR(STDIN_FILENO, &readfds);
189 }
Rob Landley1cca9482006-07-10 19:45:20 +0000190 ofd = cfd;
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000191 }
Denis Vlasenko74324c82007-06-04 10:16:52 +0000192 xwrite(ofd, iobuf, nread);
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200193 if (delay > 0)
194 sleep(delay);
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000195 }
Tomoya Adachi63416cc2009-08-03 02:59:22 +0200196 if (fd == cfd)
197 break;
198 fd = cfd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 }
200 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000201}
Denis Vlasenko29fe7262007-04-05 20:26:28 +0000202#endif