Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 2 | /* nc: mini-netcat - built from the ground up for LRP |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 3 | * |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 4 | * Copyright (C) 1998, 1999 Charles P. Wright |
| 5 | * Copyright (C) 1998 Dave Cinege |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 6 | * |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 8 | */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 11 | |
Mike Frysinger | 60a5c38 | 2005-05-06 04:45:38 +0000 | [diff] [blame] | 12 | static void timeout(int signum) |
| 13 | { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 14 | bb_error_msg_and_die("timed out"); |
Mike Frysinger | 60a5c38 | 2005-05-06 04:45:38 +0000 | [diff] [blame] | 15 | } |
| 16 | |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 17 | int nc_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 18 | { |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 19 | int sfd = 0; |
| 20 | int cfd = 0; |
| 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;) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 27 | struct sockaddr_in address; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 28 | fd_set readfds, testfds; |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 29 | int opt; /* must be signed (getopt returns -1) */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 30 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 31 | memset(&address, 0, sizeof(address)); |
| 32 | |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 33 | if (ENABLE_NC_SERVER || ENABLE_NC_EXTRA) { |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 34 | /* getopt32 is _almost_ usable: |
| 35 | ** it cannot handle "... -e prog -prog-opt" */ |
| 36 | while ((opt = getopt(argc, argv, |
| 37 | "" USE_NC_SERVER("lp:") USE_NC_EXTRA("w:i:f:e:") )) > 0 |
| 38 | ) { |
| 39 | if (ENABLE_NC_SERVER && opt=='l') USE_NC_SERVER(do_listen++); |
| 40 | else if (ENABLE_NC_SERVER && opt=='p') USE_NC_SERVER(lport = bb_lookup_port(optarg, "tcp", 0)); |
| 41 | else if (ENABLE_NC_EXTRA && opt=='w') USE_NC_EXTRA( wsecs = xatou(optarg)); |
| 42 | else if (ENABLE_NC_EXTRA && opt=='i') USE_NC_EXTRA( delay = xatou(optarg)); |
| 43 | else if (ENABLE_NC_EXTRA && opt=='f') USE_NC_EXTRA( cfd = xopen(optarg, O_RDWR)); |
| 44 | else if (ENABLE_NC_EXTRA && opt=='e' && optind<=argc) { |
| 45 | /* We cannot just 'break'. We should let getopt finish. |
| 46 | ** Or else we won't be able to find where |
| 47 | ** 'host' and 'port' params are |
| 48 | ** (think "nc -w 60 host port -e prog"). */ |
| 49 | USE_NC_EXTRA( |
| 50 | char **p; |
| 51 | // +2: one for progname (optarg) and one for NULL |
| 52 | execparam = xzalloc(sizeof(char*) * (argc - optind + 2)); |
| 53 | p = execparam; |
| 54 | *p++ = optarg; |
| 55 | while (optind < argc) { |
| 56 | *p++ = argv[optind++]; |
| 57 | } |
| 58 | ) |
| 59 | /* optind points to argv[arvc] (NULL) now. |
| 60 | ** FIXME: we assume that getopt will not count options |
| 61 | ** possibly present on "-e prog args" and will not |
| 62 | ** include them into final value of optind |
| 63 | ** which is to be used ... */ |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 64 | } else bb_show_usage(); |
Matt Kraai | 1d70267 | 2001-02-07 04:09:23 +0000 | [diff] [blame] | 65 | } |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 66 | argv += optind; /* ... here! */ |
| 67 | argc -= optind; |
| 68 | // -l and -f don't mix |
| 69 | if (do_listen && cfd) bb_show_usage(); |
| 70 | // Listen or file modes need zero arguments, client mode needs 2 |
| 71 | opt = ((do_listen || cfd) ? 0 : 2); |
| 72 | if (argc != opt) |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 73 | bb_show_usage(); |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 74 | } else { |
| 75 | if (argc != 3) bb_show_usage(); |
| 76 | argc--; |
| 77 | argv++; |
| 78 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 79 | |
Mike Frysinger | 60a5c38 | 2005-05-06 04:45:38 +0000 | [diff] [blame] | 80 | if (wsecs) { |
| 81 | signal(SIGALRM, timeout); |
| 82 | alarm(wsecs); |
| 83 | } |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 84 | |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 85 | if (!cfd) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 86 | sfd = xsocket(AF_INET, SOCK_STREAM, 0); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 87 | fcntl(sfd, F_SETFD, FD_CLOEXEC); |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 88 | setsockopt_reuseaddr(sfd); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 89 | address.sin_family = AF_INET; |
Mike Frysinger | 60a5c38 | 2005-05-06 04:45:38 +0000 | [diff] [blame] | 90 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 91 | // Set local port. |
Matt Kraai | 1d70267 | 2001-02-07 04:09:23 +0000 | [diff] [blame] | 92 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 93 | if (lport != 0) { |
| 94 | address.sin_port = lport; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 95 | xbind(sfd, (struct sockaddr *) &address, sizeof(address)); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 96 | } |
Matt Kraai | be9f44a | 2001-05-15 03:05:39 +0000 | [diff] [blame] | 97 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 98 | if (do_listen) { |
| 99 | socklen_t addrlen = sizeof(address); |
Matt Kraai | 1d70267 | 2001-02-07 04:09:23 +0000 | [diff] [blame] | 100 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 101 | xlisten(sfd, do_listen); |
Matt Kraai | 1d70267 | 2001-02-07 04:09:23 +0000 | [diff] [blame] | 102 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 103 | // If we didn't specify a port number, query and print it to stderr. |
Matt Kraai | 1d70267 | 2001-02-07 04:09:23 +0000 | [diff] [blame] | 104 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 105 | if (!lport) { |
| 106 | socklen_t len = sizeof(address); |
Denis Vlasenko | c8e6e35 | 2006-12-18 22:10:24 +0000 | [diff] [blame^] | 107 | getsockname(sfd, (struct sockaddr *) &address, &len); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 108 | fdprintf(2, "%d\n", SWAP_BE16(address.sin_port)); |
| 109 | } |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 110 | repeatyness: |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 111 | cfd = accept(sfd, (struct sockaddr *) &address, &addrlen); |
| 112 | if (cfd < 0) |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 113 | bb_perror_msg_and_die("accept"); |
| 114 | |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 115 | if (!execparam) close(sfd); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 116 | } else { |
Denis Vlasenko | 8528151 | 2006-11-07 19:05:43 +0000 | [diff] [blame] | 117 | struct hostent *hostinfo; |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 118 | hostinfo = xgethostbyname(argv[0]); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 119 | |
| 120 | address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 121 | address.sin_port = bb_lookup_port(argv[1], "tcp", 0); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 122 | |
| 123 | if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) |
| 124 | bb_perror_msg_and_die("connect"); |
| 125 | cfd = sfd; |
| 126 | } |
Matt Kraai | 1d70267 | 2001-02-07 04:09:23 +0000 | [diff] [blame] | 127 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 128 | |
Mike Frysinger | 60a5c38 | 2005-05-06 04:45:38 +0000 | [diff] [blame] | 129 | if (wsecs) { |
| 130 | alarm(0); |
| 131 | signal(SIGALRM, SIG_DFL); |
| 132 | } |
| 133 | |
Eric Andersen | 1323c94 | 2002-04-26 23:59:12 +0000 | [diff] [blame] | 134 | /* -e given? */ |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 135 | if (execparam) { |
| 136 | if (cfd) { |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 137 | signal(SIGCHLD, SIG_IGN); |
| 138 | dup2(cfd, 0); |
| 139 | close(cfd); |
| 140 | } |
Mike Frysinger | 7dc7f40 | 2005-05-06 05:00:34 +0000 | [diff] [blame] | 141 | dup2(0, 1); |
| 142 | dup2(0, 2); |
Eric Andersen | 1323c94 | 2002-04-26 23:59:12 +0000 | [diff] [blame] | 143 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 144 | // With more than one -l, repeatedly act as server. |
| 145 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 146 | if (do_listen > 1 && vfork()) { |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 147 | // This is a bit weird as cleanup goes, since we wind up with no |
| 148 | // stdin/stdout/stderr. But it's small and shouldn't hurt anything. |
| 149 | // We check for cfd == 0 above. |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 150 | logmode = LOGMODE_NONE; |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 151 | close(0); |
| 152 | close(1); |
| 153 | close(2); |
| 154 | |
| 155 | goto repeatyness; |
| 156 | } |
Denis Vlasenko | d0e70af | 2006-10-16 01:10:28 +0000 | [diff] [blame] | 157 | USE_NC_EXTRA(execvp(execparam[0], execparam);) |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 158 | /* Don't print stuff or it will go over the wire.... */ |
| 159 | _exit(127); |
| 160 | } |
| 161 | |
| 162 | // Select loop copying stdin to cfd, and cfd to stdout. |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 163 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 164 | FD_ZERO(&readfds); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 165 | FD_SET(cfd, &readfds); |
Matt Kraai | bfa7967 | 2000-12-15 22:34:34 +0000 | [diff] [blame] | 166 | FD_SET(STDIN_FILENO, &readfds); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 167 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 168 | for (;;) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | int fd; |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 170 | int ofd; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | int nread; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 172 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 173 | testfds = readfds; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 174 | |
Matt Kraai | bfa7967 | 2000-12-15 22:34:34 +0000 | [diff] [blame] | 175 | if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 176 | bb_perror_msg_and_die("select"); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 177 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 178 | for (fd = 0; fd < FD_SETSIZE; fd++) { |
| 179 | if (FD_ISSET(fd, &testfds)) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 180 | nread = safe_read(fd, bb_common_bufsiz1, |
| 181 | sizeof(bb_common_bufsiz1)); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 182 | |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 183 | if (fd == cfd) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 184 | if (nread<1) exit(0); |
Matt Kraai | bfa7967 | 2000-12-15 22:34:34 +0000 | [diff] [blame] | 185 | ofd = STDOUT_FILENO; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 186 | } else { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 187 | if (nread<1) { |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 188 | // Close outgoing half-connection so they get EOF, but |
| 189 | // leave incoming alone so we can see response. |
| 190 | shutdown(cfd, 1); |
Paul Fox | 7b71d74 | 2005-07-18 22:23:16 +0000 | [diff] [blame] | 191 | FD_CLR(STDIN_FILENO, &readfds); |
| 192 | } |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 193 | ofd = cfd; |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 194 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 195 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 196 | xwrite(ofd, bb_common_bufsiz1, nread); |
Rob Landley | 1cca948 | 2006-07-10 19:45:20 +0000 | [diff] [blame] | 197 | if (delay > 0) sleep(delay); |
Eric Andersen | 2ce1edc | 1999-10-12 15:42:48 +0000 | [diff] [blame] | 198 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 201 | } |