blob: 9c3f116d2ec1c89e18a0967c96c4060b17e98122 [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);
Denis Vlasenko3fa36e22008-11-09 00:15:11 +0000132 /* Non-ignored siganls revert to SIG_DFL on exec anyway */
133 /*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) {
Denis Vlasenko5d687242007-01-12 20:59:31 +0000138 signal(SIGCHLD, SIG_IGN);
Rob Landley1cca9482006-07-10 19:45:20 +0000139 // With more than one -l, repeatedly act as server.
Denis Vlasenko13858992006-10-08 12:49:22 +0000140 if (do_listen > 1 && vfork()) {
Denis Vlasenko5d687242007-01-12 20:59:31 +0000141 /* parent */
Rob Landley1cca9482006-07-10 19:45:20 +0000142 // This is a bit weird as cleanup goes, since we wind up with no
143 // stdin/stdout/stderr. But it's small and shouldn't hurt anything.
144 // We check for cfd == 0 above.
Denis Vlasenko13858992006-10-08 12:49:22 +0000145 logmode = LOGMODE_NONE;
Rob Landley1cca9482006-07-10 19:45:20 +0000146 close(0);
147 close(1);
148 close(2);
Denis Vlasenko5d687242007-01-12 20:59:31 +0000149 goto accept_again;
Rob Landley1cca9482006-07-10 19:45:20 +0000150 }
Denis Vlasenko5d687242007-01-12 20:59:31 +0000151 /* child (or main thread if no multiple -l) */
Denis Vlasenkofa0b56d2008-07-01 16:09:07 +0000152 xmove_fd(cfd, 0);
153 xdup2(0, 1);
154 xdup2(0, 2);
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000155 IF_NC_EXTRA(BB_EXECVP(execparam[0], execparam);)
Rob Landley1cca9482006-07-10 19:45:20 +0000156 /* Don't print stuff or it will go over the wire.... */
157 _exit(127);
158 }
159
160 // Select loop copying stdin to cfd, and cfd to stdout.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000161
Erik Andersene49d5ec2000-02-08 19:58:47 +0000162 FD_ZERO(&readfds);
Rob Landley1cca9482006-07-10 19:45:20 +0000163 FD_SET(cfd, &readfds);
Matt Kraaibfa79672000-12-15 22:34:34 +0000164 FD_SET(STDIN_FILENO, &readfds);
Eric Andersencc8ed391999-10-05 16:24:54 +0000165
Rob Landley1cca9482006-07-10 19:45:20 +0000166 for (;;) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000167 int fd;
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000168 int ofd;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 int nread;
Eric Andersencc8ed391999-10-05 16:24:54 +0000170
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 testfds = readfds;
Eric Andersencc8ed391999-10-05 16:24:54 +0000172
Matt Kraaibfa79672000-12-15 22:34:34 +0000173 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 bb_perror_msg_and_die("select");
Eric Andersencc8ed391999-10-05 16:24:54 +0000175
Denis Vlasenko74324c82007-06-04 10:16:52 +0000176#define iobuf bb_common_bufsiz1
Erik Andersene49d5ec2000-02-08 19:58:47 +0000177 for (fd = 0; fd < FD_SETSIZE; fd++) {
178 if (FD_ISSET(fd, &testfds)) {
Denis Vlasenko74324c82007-06-04 10:16:52 +0000179 nread = safe_read(fd, iobuf, sizeof(iobuf));
Rob Landley1cca9482006-07-10 19:45:20 +0000180 if (fd == cfd) {
Denis Vlasenko5d687242007-01-12 20:59:31 +0000181 if (nread < 1)
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000182 exit(EXIT_SUCCESS);
Matt Kraaibfa79672000-12-15 22:34:34 +0000183 ofd = STDOUT_FILENO;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 } else {
Rob Landley53437472006-07-16 08:14:35 +0000185 if (nread<1) {
Rob Landley1cca9482006-07-10 19:45:20 +0000186 // Close outgoing half-connection so they get EOF, but
187 // leave incoming alone so we can see response.
188 shutdown(cfd, 1);
Paul Fox7b71d742005-07-18 22:23:16 +0000189 FD_CLR(STDIN_FILENO, &readfds);
190 }
Rob Landley1cca9482006-07-10 19:45:20 +0000191 ofd = cfd;
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000192 }
Denis Vlasenko74324c82007-06-04 10:16:52 +0000193 xwrite(ofd, iobuf, nread);
Rob Landley1cca9482006-07-10 19:45:20 +0000194 if (delay > 0) sleep(delay);
Eric Andersen2ce1edc1999-10-12 15:42:48 +0000195 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 }
197 }
Eric Andersencc8ed391999-10-05 16:24:54 +0000198}
Denis Vlasenko29fe7262007-04-05 20:26:28 +0000199#endif