Annotation of src/usr.bin/nc/netcat.c, Revision 1.85
1.85 ! millert 1: /* $OpenBSD: netcat.c,v 1.84 2005/10/25 06:51:37 dtucker Exp $ */
1.21 ericj 2: /*
3: * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
1.7 deraadt 4: *
1.21 ericj 5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
1.7 deraadt 8: *
1.21 ericj 9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: * 3. The name of the author may not be used to endorse or promote products
15: * derived from this software without specific prior written permission.
1.7 deraadt 16: *
1.21 ericj 17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: */
1.1 deraadt 28:
1.24 ericj 29: /*
30: * Re-written nc(1) for OpenBSD. Original implementation by
1.21 ericj 31: * *Hobbit* <hobbit@avian.org>.
32: */
1.1 deraadt 33:
1.7 deraadt 34: #include <sys/types.h>
1.21 ericj 35: #include <sys/socket.h>
1.7 deraadt 36: #include <sys/time.h>
1.42 ericj 37: #include <sys/un.h>
1.21 ericj 38:
1.7 deraadt 39: #include <netinet/in.h>
1.83 dtucker 40: #include <netinet/in_systm.h>
1.65 markus 41: #include <netinet/tcp.h>
1.83 dtucker 42: #include <netinet/ip.h>
1.21 ericj 43: #include <arpa/telnet.h>
1.29 smart 44:
1.11 ericj 45: #include <err.h>
1.7 deraadt 46: #include <errno.h>
1.21 ericj 47: #include <netdb.h>
48: #include <poll.h>
1.13 ericj 49: #include <stdarg.h>
1.21 ericj 50: #include <stdio.h>
1.1 deraadt 51: #include <stdlib.h>
1.21 ericj 52: #include <string.h>
1.5 art 53: #include <unistd.h>
1.42 ericj 54: #include <fcntl.h>
1.85 ! millert 55: #include <limits.h>
1.79 avsm 56: #include "atomicio.h"
1.51 vincent 57:
58: #ifndef SUN_LEN
59: #define SUN_LEN(su) \
60: (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
61: #endif
1.1 deraadt 62:
1.55 fgsch 63: #define PORT_MAX 65535
64: #define PORT_MAX_LEN 6
1.31 ericj 65:
1.21 ericj 66: /* Command Line Options */
1.68 tedu 67: int dflag; /* detached, no stdin */
1.21 ericj 68: int iflag; /* Interval Flag */
1.80 mcbride 69: int jflag; /* use jumbo frames if we can */
1.21 ericj 70: int kflag; /* More than one connect */
71: int lflag; /* Bind to local port */
1.67 jmc 72: int nflag; /* Don't do name look up */
1.21 ericj 73: char *pflag; /* Localport flag */
74: int rflag; /* Random ports flag */
75: char *sflag; /* Source Address */
76: int tflag; /* Telnet Emulation */
77: int uflag; /* UDP - Default to TCP */
78: int vflag; /* Verbosity */
1.34 jakob 79: int xflag; /* Socks proxy */
1.21 ericj 80: int zflag; /* Port Scan Flag */
1.73 markus 81: int Dflag; /* sodebug */
1.65 markus 82: int Sflag; /* TCP MD5 signature option */
1.83 dtucker 83: int Tflag = -1; /* IP Type of Service */
1.21 ericj 84:
1.49 hugh 85: int timeout = -1;
1.21 ericj 86: int family = AF_UNSPEC;
1.63 miod 87: char *portlist[PORT_MAX+1];
1.21 ericj 88:
1.40 millert 89: void atelnet(int, unsigned char *, unsigned int);
90: void build_ports(char *);
91: void help(void);
92: int local_listen(char *, char *, struct addrinfo);
93: void readwrite(int);
1.77 otto 94: int remote_connect(const char *, const char *, struct addrinfo);
95: int socks_connect(const char *, const char *, struct addrinfo, const char *, const char *,
1.46 markus 96: struct addrinfo, int);
1.40 millert 97: int udptest(int);
1.42 ericj 98: int unix_connect(char *);
99: int unix_listen(char *);
1.84 dtucker 100: void set_common_sockopts(int);
1.83 dtucker 101: int parse_iptos(char *);
1.40 millert 102: void usage(int);
1.1 deraadt 103:
1.21 ericj 104: int
1.37 jakob 105: main(int argc, char *argv[])
1.21 ericj 106: {
1.46 markus 107: int ch, s, ret, socksv;
1.26 ericj 108: char *host, *uport, *endp;
1.21 ericj 109: struct addrinfo hints;
1.29 smart 110: struct servent *sv;
1.21 ericj 111: socklen_t len;
1.76 hshoexer 112: struct sockaddr_storage cliaddr;
1.34 jakob 113: char *proxy;
1.77 otto 114: const char *proxyhost = "", *proxyport = NULL;
1.34 jakob 115: struct addrinfo proxyhints;
1.11 ericj 116:
1.29 smart 117: ret = 1;
118: s = 0;
1.46 markus 119: socksv = 5;
1.29 smart 120: host = NULL;
121: uport = NULL;
122: endp = NULL;
123: sv = NULL;
124:
1.80 mcbride 125: while ((ch = getopt(argc, argv,
1.83 dtucker 126: "46Ddhi:jklnp:rSs:tT:Uuvw:X:x:z")) != -1) {
1.21 ericj 127: switch (ch) {
128: case '4':
129: family = AF_INET;
130: break;
131: case '6':
132: family = AF_INET6;
133: break;
1.42 ericj 134: case 'U':
135: family = AF_UNIX;
136: break;
1.46 markus 137: case 'X':
1.75 djm 138: if (strcasecmp(optarg, "connect") == 0)
139: socksv = -1; /* HTTP proxy CONNECT */
140: else if (strcmp(optarg, "4") == 0)
141: socksv = 4; /* SOCKS v.4 */
142: else if (strcmp(optarg, "5") == 0)
143: socksv = 5; /* SOCKS v.5 */
144: else
145: errx(1, "unsupported proxy protocol");
1.46 markus 146: break;
1.68 tedu 147: case 'd':
148: dflag = 1;
149: break;
1.21 ericj 150: case 'h':
151: help();
152: break;
153: case 'i':
1.26 ericj 154: iflag = (int)strtoul(optarg, &endp, 10);
155: if (iflag < 0 || *endp != '\0')
156: errx(1, "interval cannot be negative");
1.21 ericj 157: break;
1.80 mcbride 158: case 'j':
159: jflag = 1;
160: break;
1.21 ericj 161: case 'k':
162: kflag = 1;
163: break;
164: case 'l':
165: lflag = 1;
166: break;
167: case 'n':
168: nflag = 1;
169: break;
170: case 'p':
171: pflag = optarg;
172: break;
173: case 'r':
174: rflag = 1;
175: break;
176: case 's':
177: sflag = optarg;
178: break;
179: case 't':
180: tflag = 1;
181: break;
182: case 'u':
183: uflag = 1;
184: break;
185: case 'v':
186: vflag = 1;
187: break;
1.70 deraadt 188: case 'w':
1.26 ericj 189: timeout = (int)strtoul(optarg, &endp, 10);
190: if (timeout < 0 || *endp != '\0')
191: errx(1, "timeout cannot be negative");
1.49 hugh 192: if (timeout >= (INT_MAX / 1000))
193: errx(1, "timeout too large");
194: timeout *= 1000;
1.21 ericj 195: break;
1.34 jakob 196: case 'x':
197: xflag = 1;
1.64 deraadt 198: if ((proxy = strdup(optarg)) == NULL)
199: err(1, NULL);
1.34 jakob 200: break;
1.21 ericj 201: case 'z':
202: zflag = 1;
203: break;
1.73 markus 204: case 'D':
205: Dflag = 1;
206: break;
1.65 markus 207: case 'S':
208: Sflag = 1;
209: break;
1.83 dtucker 210: case 'T':
211: Tflag = parse_iptos(optarg);
212: break;
1.21 ericj 213: default:
214: usage(1);
215: }
216: }
217: argc -= optind;
218: argv += optind;
1.11 ericj 219:
1.21 ericj 220: /* Cruft to make sure options are clean, and used properly. */
1.42 ericj 221: if (argv[0] && !argv[1] && family == AF_UNIX) {
222: if (uflag)
223: errx(1, "cannot use -u and -U");
224: host = argv[0];
225: uport = NULL;
226: } else if (argv[0] && !argv[1]) {
1.21 ericj 227: if (!lflag)
228: usage(1);
229: uport = argv[0];
230: host = NULL;
231: } else if (argv[0] && argv[1]) {
232: host = argv[0];
233: uport = argv[1];
234: } else
235: usage(1);
1.1 deraadt 236:
1.21 ericj 237: if (lflag && sflag)
238: errx(1, "cannot use -s and -l");
239: if (lflag && pflag)
240: errx(1, "cannot use -p and -l");
241: if (lflag && zflag)
1.32 ericj 242: errx(1, "cannot use -z and -l");
1.21 ericj 243: if (!lflag && kflag)
1.32 ericj 244: errx(1, "must use -l with -k");
1.21 ericj 245:
1.67 jmc 246: /* Initialize addrinfo structure. */
1.42 ericj 247: if (family != AF_UNIX) {
248: memset(&hints, 0, sizeof(struct addrinfo));
249: hints.ai_family = family;
250: hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
251: hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
252: if (nflag)
253: hints.ai_flags |= AI_NUMERICHOST;
254: }
1.1 deraadt 255:
1.34 jakob 256: if (xflag) {
257: if (uflag)
258: errx(1, "no proxy support for UDP mode");
259:
260: if (lflag)
261: errx(1, "no proxy support for listen");
262:
1.42 ericj 263: if (family == AF_UNIX)
264: errx(1, "no proxy support for unix sockets");
265:
1.34 jakob 266: /* XXX IPv6 transport to proxy would probably work */
267: if (family == AF_INET6)
268: errx(1, "no proxy support for IPv6");
269:
270: if (sflag)
271: errx(1, "no proxy support for local source address");
272:
273: proxyhost = strsep(&proxy, ":");
274: proxyport = proxy;
275:
276: memset(&proxyhints, 0, sizeof(struct addrinfo));
277: proxyhints.ai_family = family;
278: proxyhints.ai_socktype = SOCK_STREAM;
279: proxyhints.ai_protocol = IPPROTO_TCP;
280: if (nflag)
281: proxyhints.ai_flags |= AI_NUMERICHOST;
282: }
283:
1.21 ericj 284: if (lflag) {
285: int connfd;
1.27 ericj 286: ret = 0;
1.1 deraadt 287:
1.42 ericj 288: if (family == AF_UNIX)
289: s = unix_listen(host);
290:
1.67 jmc 291: /* Allow only one connection at a time, but stay alive. */
1.21 ericj 292: for (;;) {
1.42 ericj 293: if (family != AF_UNIX)
294: s = local_listen(host, uport, hints);
295: if (s < 0)
1.30 smart 296: err(1, NULL);
1.21 ericj 297: /*
298: * For UDP, we will use recvfrom() initially
299: * to wait for a caller, then use the regular
300: * functions to talk to the caller.
301: */
302: if (uflag) {
1.80 mcbride 303: int rv, plen;
304: char buf[8192];
1.21 ericj 305: struct sockaddr_storage z;
306:
307: len = sizeof(z);
1.80 mcbride 308: plen = jflag ? 8192 : 1024;
309: rv = recvfrom(s, buf, plen, MSG_PEEK,
1.37 jakob 310: (struct sockaddr *)&z, &len);
1.23 ericj 311: if (rv < 0)
1.57 stevesk 312: err(1, "recvfrom");
1.21 ericj 313:
1.37 jakob 314: rv = connect(s, (struct sockaddr *)&z, len);
1.23 ericj 315: if (rv < 0)
1.57 stevesk 316: err(1, "connect");
1.1 deraadt 317:
1.21 ericj 318: connfd = s;
319: } else {
1.78 otto 320: len = sizeof(cliaddr);
1.21 ericj 321: connfd = accept(s, (struct sockaddr *)&cliaddr,
1.37 jakob 322: &len);
1.21 ericj 323: }
1.1 deraadt 324:
1.21 ericj 325: readwrite(connfd);
326: close(connfd);
1.42 ericj 327: if (family != AF_UNIX)
328: close(s);
1.27 ericj 329:
1.21 ericj 330: if (!kflag)
331: break;
1.11 ericj 332: }
1.42 ericj 333: } else if (family == AF_UNIX) {
334: ret = 0;
335:
336: if ((s = unix_connect(host)) > 0 && !zflag) {
337: readwrite(s);
338: close(s);
339: } else
340: ret = 1;
341:
342: exit(ret);
343:
1.21 ericj 344: } else {
345: int i = 0;
1.6 deraadt 346:
1.67 jmc 347: /* Construct the portlist[] array. */
1.21 ericj 348: build_ports(uport);
1.1 deraadt 349:
1.67 jmc 350: /* Cycle through portlist, connecting to each port. */
1.21 ericj 351: for (i = 0; portlist[i] != NULL; i++) {
352: if (s)
353: close(s);
1.34 jakob 354:
355: if (xflag)
356: s = socks_connect(host, portlist[i], hints,
1.46 markus 357: proxyhost, proxyport, proxyhints, socksv);
1.34 jakob 358: else
359: s = remote_connect(host, portlist[i], hints);
360:
361: if (s < 0)
1.21 ericj 362: continue;
1.1 deraadt 363:
1.21 ericj 364: ret = 0;
365: if (vflag || zflag) {
1.67 jmc 366: /* For UDP, make sure we are connected. */
1.21 ericj 367: if (uflag) {
1.50 vincent 368: if (udptest(s) == -1) {
1.21 ericj 369: ret = 1;
370: continue;
371: }
372: }
1.1 deraadt 373:
1.67 jmc 374: /* Don't look up port if -n. */
1.21 ericj 375: if (nflag)
376: sv = NULL;
377: else {
378: sv = getservbyport(
1.37 jakob 379: ntohs(atoi(portlist[i])),
380: uflag ? "udp" : "tcp");
1.21 ericj 381: }
1.50 vincent 382:
1.21 ericj 383: printf("Connection to %s %s port [%s/%s] succeeded!\n",
1.37 jakob 384: host, portlist[i], uflag ? "udp" : "tcp",
385: sv ? sv->s_name : "*");
1.21 ericj 386: }
387: if (!zflag)
388: readwrite(s);
1.7 deraadt 389: }
1.11 ericj 390: }
1.1 deraadt 391:
1.21 ericj 392: if (s)
393: close(s);
394:
395: exit(ret);
1.7 deraadt 396: }
1.1 deraadt 397:
1.11 ericj 398: /*
1.42 ericj 399: * unix_connect()
1.67 jmc 400: * Returns a socket connected to a local unix socket. Returns -1 on failure.
1.42 ericj 401: */
402: int
403: unix_connect(char *path)
404: {
405: struct sockaddr_un sun;
406: int s;
407:
408: if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
1.50 vincent 409: return (-1);
1.42 ericj 410: (void)fcntl(s, F_SETFD, 1);
411:
412: memset(&sun, 0, sizeof(struct sockaddr_un));
413: sun.sun_family = AF_UNIX;
1.60 avsm 414:
415: if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
416: sizeof(sun.sun_path)) {
417: close(s);
418: errno = ENAMETOOLONG;
419: return (-1);
420: }
1.50 vincent 421: if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
422: close(s);
423: return (-1);
1.42 ericj 424: }
425: return (s);
1.50 vincent 426:
1.42 ericj 427: }
428:
429: /*
430: * unix_listen()
1.67 jmc 431: * Create a unix domain socket, and listen on it.
1.42 ericj 432: */
433: int
434: unix_listen(char *path)
435: {
436: struct sockaddr_un sun;
437: int s;
438:
1.67 jmc 439: /* Create unix domain socket. */
1.42 ericj 440: if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
441: return (-1);
442:
1.60 avsm 443: memset(&sun, 0, sizeof(struct sockaddr_un));
1.42 ericj 444: sun.sun_family = AF_UNIX;
1.60 avsm 445:
446: if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
447: sizeof(sun.sun_path)) {
448: close(s);
449: errno = ENAMETOOLONG;
450: return (-1);
451: }
452:
1.50 vincent 453: if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
1.42 ericj 454: close(s);
455: return (-1);
456: }
457:
458: if (listen(s, 5) < 0) {
459: close(s);
460: return (-1);
461: }
462: return (s);
463: }
464:
465: /*
1.21 ericj 466: * remote_connect()
1.67 jmc 467: * Returns a socket connected to a remote host. Properly binds to a local
468: * port or source address if needed. Returns -1 on failure.
1.11 ericj 469: */
1.21 ericj 470: int
1.77 otto 471: remote_connect(const char *host, const char *port, struct addrinfo hints)
1.21 ericj 472: {
473: struct addrinfo *res, *res0;
1.81 marius 474: int s, error;
1.21 ericj 475:
476: if ((error = getaddrinfo(host, port, &hints, &res)))
1.56 stevesk 477: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 478:
479: res0 = res;
480: do {
481: if ((s = socket(res0->ai_family, res0->ai_socktype,
1.37 jakob 482: res0->ai_protocol)) < 0)
1.21 ericj 483: continue;
484:
1.67 jmc 485: /* Bind to a local port or source address if specified. */
1.21 ericj 486: if (sflag || pflag) {
487: struct addrinfo ahints, *ares;
488:
489: if (!(sflag && pflag)) {
490: if (!sflag)
491: sflag = NULL;
492: else
493: pflag = NULL;
494: }
1.6 deraadt 495:
1.21 ericj 496: memset(&ahints, 0, sizeof(struct addrinfo));
497: ahints.ai_family = res0->ai_family;
498: ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
499: ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
1.25 ericj 500: ahints.ai_flags = AI_PASSIVE;
1.38 jakob 501: if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
1.56 stevesk 502: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 503:
504: if (bind(s, (struct sockaddr *)ares->ai_addr,
1.62 millert 505: ares->ai_addrlen) < 0)
1.21 ericj 506: errx(1, "bind failed: %s", strerror(errno));
507: freeaddrinfo(ares);
1.6 deraadt 508: }
1.81 marius 509:
510: set_common_sockopts(s);
1.6 deraadt 511:
1.21 ericj 512: if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
1.6 deraadt 513: break;
1.71 mcbride 514: else if (vflag)
515: warn("connect to %s port %s (%s) failed", host, port,
516: uflag ? "udp" : "tcp");
1.34 jakob 517:
1.21 ericj 518: close(s);
519: s = -1;
520: } while ((res0 = res0->ai_next) != NULL);
521:
522: freeaddrinfo(res);
1.1 deraadt 523:
1.21 ericj 524: return (s);
1.7 deraadt 525: }
1.1 deraadt 526:
1.11 ericj 527: /*
1.21 ericj 528: * local_listen()
1.67 jmc 529: * Returns a socket listening on a local port, binds to specified source
530: * address. Returns -1 on failure.
1.11 ericj 531: */
1.21 ericj 532: int
1.37 jakob 533: local_listen(char *host, char *port, struct addrinfo hints)
1.21 ericj 534: {
535: struct addrinfo *res, *res0;
536: int s, ret, x = 1;
537: int error;
1.6 deraadt 538:
1.67 jmc 539: /* Allow nodename to be null. */
1.21 ericj 540: hints.ai_flags |= AI_PASSIVE;
1.7 deraadt 541:
1.21 ericj 542: /*
543: * In the case of binding to a wildcard address
544: * default to binding to an ipv4 address.
545: */
546: if (host == NULL && hints.ai_family == AF_UNSPEC)
547: hints.ai_family = AF_INET;
1.1 deraadt 548:
1.21 ericj 549: if ((error = getaddrinfo(host, port, &hints, &res)))
1.70 deraadt 550: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.14 ericj 551:
1.21 ericj 552: res0 = res;
553: do {
554: if ((s = socket(res0->ai_family, res0->ai_socktype,
1.82 marius 555: res0->ai_protocol)) < 0)
1.21 ericj 556: continue;
1.1 deraadt 557:
1.21 ericj 558: ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
559: if (ret == -1)
1.30 smart 560: err(1, NULL);
1.81 marius 561:
562: set_common_sockopts(s);
1.1 deraadt 563:
1.21 ericj 564: if (bind(s, (struct sockaddr *)res0->ai_addr,
1.37 jakob 565: res0->ai_addrlen) == 0)
1.21 ericj 566: break;
1.1 deraadt 567:
1.21 ericj 568: close(s);
569: s = -1;
570: } while ((res0 = res0->ai_next) != NULL);
1.1 deraadt 571:
1.47 ericj 572: if (!uflag && s != -1) {
1.21 ericj 573: if (listen(s, 1) < 0)
1.57 stevesk 574: err(1, "listen");
1.12 ericj 575: }
1.1 deraadt 576:
1.21 ericj 577: freeaddrinfo(res);
1.1 deraadt 578:
1.21 ericj 579: return (s);
1.7 deraadt 580: }
581:
1.11 ericj 582: /*
1.21 ericj 583: * readwrite()
584: * Loop that polls on the network file descriptor and stdin.
1.11 ericj 585: */
1.21 ericj 586: void
1.37 jakob 587: readwrite(int nfd)
1.6 deraadt 588: {
1.52 vincent 589: struct pollfd pfd[2];
1.80 mcbride 590: unsigned char buf[8192];
1.79 avsm 591: int n, wfd = fileno(stdin);
1.21 ericj 592: int lfd = fileno(stdout);
1.80 mcbride 593: int plen;
594:
595: plen = jflag ? 8192 : 1024;
1.21 ericj 596:
597: /* Setup Network FD */
598: pfd[0].fd = nfd;
599: pfd[0].events = POLLIN;
600:
1.67 jmc 601: /* Set up STDIN FD. */
1.21 ericj 602: pfd[1].fd = wfd;
603: pfd[1].events = POLLIN;
604:
1.54 aaron 605: while (pfd[0].fd != -1) {
1.21 ericj 606: if (iflag)
607: sleep(iflag);
608:
1.68 tedu 609: if ((n = poll(pfd, 2 - dflag, timeout)) < 0) {
1.21 ericj 610: close(nfd);
1.52 vincent 611: err(1, "Polling Error");
1.21 ericj 612: }
1.49 hugh 613:
614: if (n == 0)
615: return;
1.21 ericj 616:
617: if (pfd[0].revents & POLLIN) {
1.80 mcbride 618: if ((n = read(nfd, buf, plen)) < 0)
1.21 ericj 619: return;
1.52 vincent 620: else if (n == 0) {
621: shutdown(nfd, SHUT_RD);
622: pfd[0].fd = -1;
623: pfd[0].events = 0;
1.21 ericj 624: } else {
625: if (tflag)
626: atelnet(nfd, buf, n);
1.79 avsm 627: if (atomicio(vwrite, lfd, buf, n) != n)
1.21 ericj 628: return;
1.6 deraadt 629: }
1.21 ericj 630: }
631:
1.68 tedu 632: if (!dflag && pfd[1].revents & POLLIN) {
1.80 mcbride 633: if ((n = read(wfd, buf, plen)) < 0)
1.21 ericj 634: return;
1.52 vincent 635: else if (n == 0) {
636: shutdown(nfd, SHUT_WR);
637: pfd[1].fd = -1;
638: pfd[1].events = 0;
639: } else {
1.79 avsm 640: if (atomicio(vwrite, nfd, buf, n) != n)
1.21 ericj 641: return;
1.50 vincent 642: }
1.21 ericj 643: }
1.11 ericj 644: }
1.7 deraadt 645: }
1.50 vincent 646:
1.67 jmc 647: /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1.21 ericj 648: void
1.37 jakob 649: atelnet(int nfd, unsigned char *buf, unsigned int size)
1.6 deraadt 650: {
1.24 ericj 651: unsigned char *p, *end;
652: unsigned char obuf[4];
653:
654: end = buf + size;
655: obuf[0] = '\0';
656:
657: for (p = buf; p < end; p++) {
1.21 ericj 658: if (*p != IAC)
1.24 ericj 659: break;
660:
1.25 ericj 661: obuf[0] = IAC;
1.24 ericj 662: p++;
1.50 vincent 663: if ((*p == WILL) || (*p == WONT))
1.24 ericj 664: obuf[1] = DONT;
1.50 vincent 665: if ((*p == DO) || (*p == DONT))
1.24 ericj 666: obuf[1] = WONT;
667: if (obuf) {
668: p++;
1.11 ericj 669: obuf[2] = *p;
1.24 ericj 670: obuf[3] = '\0';
1.79 avsm 671: if (atomicio(vwrite, nfd, obuf, 3) != 3)
672: warn("Write Error!");
1.24 ericj 673: obuf[0] = '\0';
1.11 ericj 674: }
675: }
1.7 deraadt 676: }
677:
1.11 ericj 678: /*
1.21 ericj 679: * build_ports()
680: * Build an array or ports in portlist[], listing each port
1.67 jmc 681: * that we should try to connect to.
1.11 ericj 682: */
1.21 ericj 683: void
1.37 jakob 684: build_ports(char *p)
1.6 deraadt 685: {
1.26 ericj 686: char *n, *endp;
1.21 ericj 687: int hi, lo, cp;
688: int x = 0;
689:
690: if ((n = strchr(p, '-')) != NULL) {
691: if (lflag)
692: errx(1, "Cannot use -l with multiple ports!");
693:
694: *n = '\0';
695: n++;
696:
1.67 jmc 697: /* Make sure the ports are in order: lowest->highest. */
1.26 ericj 698: hi = (int)strtoul(n, &endp, 10);
1.31 ericj 699: if (hi <= 0 || hi > PORT_MAX || *endp != '\0')
1.26 ericj 700: errx(1, "port range not valid");
701: lo = (int)strtoul(p, &endp, 10);
1.31 ericj 702: if (lo <= 0 || lo > PORT_MAX || *endp != '\0')
1.26 ericj 703: errx(1, "port range not valid");
1.21 ericj 704:
705: if (lo > hi) {
706: cp = hi;
707: hi = lo;
708: lo = cp;
709: }
710:
1.67 jmc 711: /* Load ports sequentially. */
1.21 ericj 712: for (cp = lo; cp <= hi; cp++) {
1.55 fgsch 713: portlist[x] = calloc(1, PORT_MAX_LEN);
714: if (portlist[x] == NULL)
715: err(1, NULL);
716: snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
1.21 ericj 717: x++;
718: }
719:
1.67 jmc 720: /* Randomly swap ports. */
1.21 ericj 721: if (rflag) {
722: int y;
723: char *c;
724:
725: for (x = 0; x <= (hi - lo); x++) {
726: y = (arc4random() & 0xFFFF) % (hi - lo);
727: c = portlist[x];
728: portlist[x] = portlist[y];
729: portlist[y] = c;
1.6 deraadt 730: }
1.11 ericj 731: }
1.21 ericj 732: } else {
1.26 ericj 733: hi = (int)strtoul(p, &endp, 10);
1.31 ericj 734: if (hi <= 0 || hi > PORT_MAX || *endp != '\0')
1.26 ericj 735: errx(1, "port range not valid");
1.55 fgsch 736: portlist[0] = calloc(1, PORT_MAX_LEN);
737: if (portlist[0] == NULL)
738: err(1, NULL);
1.21 ericj 739: portlist[0] = p;
1.11 ericj 740: }
1.13 ericj 741: }
742:
743: /*
1.21 ericj 744: * udptest()
745: * Do a few writes to see if the UDP port is there.
1.67 jmc 746: * XXX - Better way of doing this? Doesn't work for IPv6.
1.21 ericj 747: * Also fails after around 100 ports checked.
1.13 ericj 748: */
1.21 ericj 749: int
1.37 jakob 750: udptest(int s)
1.13 ericj 751: {
1.74 deraadt 752: int i, ret;
1.13 ericj 753:
1.52 vincent 754: for (i = 0; i <= 3; i++) {
1.74 deraadt 755: if (write(s, "X", 1) == 1)
1.21 ericj 756: ret = 1;
1.14 ericj 757: else
1.21 ericj 758: ret = -1;
1.14 ericj 759: }
1.21 ericj 760: return (ret);
1.81 marius 761: }
762:
1.84 dtucker 763: void
1.81 marius 764: set_common_sockopts(int s)
765: {
766: int x = 1;
767:
768: if (Sflag) {
769: if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
770: &x, sizeof(x)) == -1)
771: err(1, NULL);
772: }
773: if (Dflag) {
774: if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
775: &x, sizeof(x)) == -1)
776: err(1, NULL);
777: }
778: if (jflag) {
779: if (setsockopt(s, SOL_SOCKET, SO_JUMBO,
780: &x, sizeof(x)) == -1)
781: err(1, NULL);
782: }
1.83 dtucker 783: if (Tflag != -1) {
784: if (setsockopt(s, IPPROTO_IP, IP_TOS,
785: &Tflag, sizeof(Tflag)) == -1)
786: err(1, "set IP ToS");
787: }
788: }
789:
790: int
791: parse_iptos(char *s)
792: {
793: int tos = -1;
794:
795: if (strcmp(s, "lowdelay") == 0)
796: return (IPTOS_LOWDELAY);
797: if (strcmp(s, "throughput") == 0)
798: return (IPTOS_THROUGHPUT);
799: if (strcmp(s, "reliability") == 0)
800: return (IPTOS_RELIABILITY);
801:
802: if (sscanf(s, "0x%x", &tos) != 1 || tos < 0 || tos > 0xff)
803: errx(1, "invalid IP Type of Service");
804: return (tos);
1.7 deraadt 805: }
1.1 deraadt 806:
1.11 ericj 807: void
1.58 deraadt 808: help(void)
1.1 deraadt 809: {
1.21 ericj 810: usage(0);
811: fprintf(stderr, "\tCommand Summary:\n\
812: \t-4 Use IPv4\n\
813: \t-6 Use IPv6\n\
1.73 markus 814: \t-D Enable the debug socket option\n\
1.69 tedu 815: \t-d Detach from stdin\n\
1.21 ericj 816: \t-h This help text\n\
817: \t-i secs\t Delay interval for lines sent, ports scanned\n\
818: \t-k Keep inbound sockets open for multiple connects\n\
819: \t-l Listen mode, for inbound connects\n\
1.22 jasoni 820: \t-n Suppress name/port resolutions\n\
1.36 jakob 821: \t-p port\t Specify local port for remote connects\n\
1.21 ericj 822: \t-r Randomize remote ports\n\
1.67 jmc 823: \t-S Enable the TCP MD5 signature option\n\
1.21 ericj 824: \t-s addr\t Local source address\n\
1.83 dtucker 825: \t-T ToS\t Set IP Type of Service\n\
1.21 ericj 826: \t-t Answer TELNET negotiation\n\
1.67 jmc 827: \t-U Use UNIX domain socket\n\
1.21 ericj 828: \t-u UDP mode\n\
829: \t-v Verbose\n\
830: \t-w secs\t Timeout for connects and final net reads\n\
1.75 djm 831: \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
832: \t-x addr[:port]\tSpecify proxy address and port\n\
1.21 ericj 833: \t-z Zero-I/O mode [used for scanning]\n\
834: Port numbers can be individual or ranges: lo-hi [inclusive]\n");
835: exit(1);
1.11 ericj 836: }
837:
838: void
1.37 jakob 839: usage(int ret)
1.11 ericj 840: {
1.73 markus 841: fprintf(stderr, "usage: nc [-46DdhklnrStUuvz] [-i interval] [-p source_port]\n");
1.83 dtucker 842: fprintf(stderr, "\t [-s source_ip_address] [-T ToS] [-w timeout] [-X proxy_version]\n");
1.72 jmc 843: fprintf(stderr, "\t [-x proxy_address[:port]] [hostname] [port[s]]\n");
1.21 ericj 844: if (ret)
845: exit(1);
1.7 deraadt 846: }