Annotation of src/usr.bin/nc/netcat.c, Revision 1.97
1.97 ! nicm 1: /* $OpenBSD: netcat.c,v 1.96 2010/04/20 07:26:34 nicm 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.88 ray 68: unsigned 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.86 djm 73: char *Pflag; /* Proxy username */
1.21 ericj 74: char *pflag; /* Localport flag */
75: int rflag; /* Random ports flag */
76: char *sflag; /* Source Address */
77: int tflag; /* Telnet Emulation */
78: int uflag; /* UDP - Default to TCP */
79: int vflag; /* Verbosity */
1.34 jakob 80: int xflag; /* Socks proxy */
1.21 ericj 81: int zflag; /* Port Scan Flag */
1.73 markus 82: int Dflag; /* sodebug */
1.90 djm 83: int Iflag; /* TCP receive buffer size */
84: int Oflag; /* TCP send buffer size */
1.65 markus 85: int Sflag; /* TCP MD5 signature option */
1.83 dtucker 86: int Tflag = -1; /* IP Type of Service */
1.93 claudio 87: u_int rdomain;
1.21 ericj 88:
1.49 hugh 89: int timeout = -1;
1.21 ericj 90: int family = AF_UNSPEC;
1.63 miod 91: char *portlist[PORT_MAX+1];
1.21 ericj 92:
1.40 millert 93: void atelnet(int, unsigned char *, unsigned int);
94: void build_ports(char *);
95: void help(void);
96: int local_listen(char *, char *, struct addrinfo);
97: void readwrite(int);
1.77 otto 98: int remote_connect(const char *, const char *, struct addrinfo);
1.86 djm 99: int socks_connect(const char *, const char *, struct addrinfo,
100: const char *, const char *, struct addrinfo, int, const char *);
1.40 millert 101: int udptest(int);
1.42 ericj 102: int unix_connect(char *);
103: int unix_listen(char *);
1.84 dtucker 104: void set_common_sockopts(int);
1.83 dtucker 105: int parse_iptos(char *);
1.40 millert 106: void usage(int);
1.1 deraadt 107:
1.21 ericj 108: int
1.37 jakob 109: main(int argc, char *argv[])
1.21 ericj 110: {
1.46 markus 111: int ch, s, ret, socksv;
1.88 ray 112: char *host, *uport;
1.21 ericj 113: struct addrinfo hints;
1.29 smart 114: struct servent *sv;
1.21 ericj 115: socklen_t len;
1.76 hshoexer 116: struct sockaddr_storage cliaddr;
1.34 jakob 117: char *proxy;
1.88 ray 118: const char *errstr, *proxyhost = "", *proxyport = NULL;
1.34 jakob 119: struct addrinfo proxyhints;
1.11 ericj 120:
1.29 smart 121: ret = 1;
122: s = 0;
1.46 markus 123: socksv = 5;
1.29 smart 124: host = NULL;
125: uport = NULL;
126: sv = NULL;
127:
1.80 mcbride 128: while ((ch = getopt(argc, argv,
1.93 claudio 129: "46DdhI:i:jklnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) {
1.21 ericj 130: switch (ch) {
131: case '4':
132: family = AF_INET;
133: break;
134: case '6':
135: family = AF_INET6;
136: break;
1.42 ericj 137: case 'U':
138: family = AF_UNIX;
139: break;
1.46 markus 140: case 'X':
1.75 djm 141: if (strcasecmp(optarg, "connect") == 0)
142: socksv = -1; /* HTTP proxy CONNECT */
143: else if (strcmp(optarg, "4") == 0)
144: socksv = 4; /* SOCKS v.4 */
145: else if (strcmp(optarg, "5") == 0)
146: socksv = 5; /* SOCKS v.5 */
147: else
148: errx(1, "unsupported proxy protocol");
1.46 markus 149: break;
1.68 tedu 150: case 'd':
151: dflag = 1;
152: break;
1.21 ericj 153: case 'h':
154: help();
155: break;
156: case 'i':
1.88 ray 157: iflag = strtonum(optarg, 0, UINT_MAX, &errstr);
158: if (errstr)
159: errx(1, "interval %s: %s", errstr, optarg);
1.21 ericj 160: break;
1.80 mcbride 161: case 'j':
162: jflag = 1;
163: break;
1.21 ericj 164: case 'k':
165: kflag = 1;
166: break;
167: case 'l':
168: lflag = 1;
169: break;
170: case 'n':
171: nflag = 1;
172: break;
1.86 djm 173: case 'P':
174: Pflag = optarg;
175: break;
1.21 ericj 176: case 'p':
177: pflag = optarg;
178: break;
179: case 'r':
180: rflag = 1;
181: break;
182: case 's':
183: sflag = optarg;
184: break;
185: case 't':
186: tflag = 1;
187: break;
188: case 'u':
189: uflag = 1;
190: break;
1.93 claudio 191: case 'V':
192: rdomain = (unsigned int)strtonum(optarg, 0,
193: RT_TABLEID_MAX, &errstr);
194: if (errstr)
195: errx(1, "rdomain %s: %s", errstr, optarg);
196: break;
1.21 ericj 197: case 'v':
198: vflag = 1;
199: break;
1.70 deraadt 200: case 'w':
1.88 ray 201: timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
202: if (errstr)
203: errx(1, "timeout %s: %s", errstr, optarg);
1.49 hugh 204: timeout *= 1000;
1.21 ericj 205: break;
1.34 jakob 206: case 'x':
207: xflag = 1;
1.64 deraadt 208: if ((proxy = strdup(optarg)) == NULL)
209: err(1, NULL);
1.34 jakob 210: break;
1.21 ericj 211: case 'z':
212: zflag = 1;
213: break;
1.73 markus 214: case 'D':
215: Dflag = 1;
216: break;
1.90 djm 217: case 'I':
218: Iflag = strtonum(optarg, 1, 65536 << 14, &errstr);
219: if (errstr != NULL)
220: errx(1, "TCP receive window %s: %s",
221: errstr, optarg);
222: break;
223: case 'O':
224: Oflag = strtonum(optarg, 1, 65536 << 14, &errstr);
225: if (errstr != NULL)
226: errx(1, "TCP send window %s: %s",
227: errstr, optarg);
228: break;
1.65 markus 229: case 'S':
230: Sflag = 1;
231: break;
1.83 dtucker 232: case 'T':
233: Tflag = parse_iptos(optarg);
234: break;
1.21 ericj 235: default:
236: usage(1);
237: }
238: }
239: argc -= optind;
240: argv += optind;
1.11 ericj 241:
1.21 ericj 242: /* Cruft to make sure options are clean, and used properly. */
1.42 ericj 243: if (argv[0] && !argv[1] && family == AF_UNIX) {
244: if (uflag)
245: errx(1, "cannot use -u and -U");
246: host = argv[0];
247: uport = NULL;
248: } else if (argv[0] && !argv[1]) {
1.21 ericj 249: if (!lflag)
250: usage(1);
251: uport = argv[0];
252: host = NULL;
253: } else if (argv[0] && argv[1]) {
254: host = argv[0];
255: uport = argv[1];
256: } else
257: usage(1);
1.1 deraadt 258:
1.21 ericj 259: if (lflag && sflag)
260: errx(1, "cannot use -s and -l");
261: if (lflag && pflag)
262: errx(1, "cannot use -p and -l");
263: if (lflag && zflag)
1.32 ericj 264: errx(1, "cannot use -z and -l");
1.21 ericj 265: if (!lflag && kflag)
1.32 ericj 266: errx(1, "must use -l with -k");
1.21 ericj 267:
1.67 jmc 268: /* Initialize addrinfo structure. */
1.42 ericj 269: if (family != AF_UNIX) {
270: memset(&hints, 0, sizeof(struct addrinfo));
271: hints.ai_family = family;
272: hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
273: hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
274: if (nflag)
275: hints.ai_flags |= AI_NUMERICHOST;
276: }
1.1 deraadt 277:
1.34 jakob 278: if (xflag) {
279: if (uflag)
280: errx(1, "no proxy support for UDP mode");
281:
282: if (lflag)
283: errx(1, "no proxy support for listen");
284:
1.42 ericj 285: if (family == AF_UNIX)
286: errx(1, "no proxy support for unix sockets");
287:
1.34 jakob 288: /* XXX IPv6 transport to proxy would probably work */
289: if (family == AF_INET6)
290: errx(1, "no proxy support for IPv6");
291:
292: if (sflag)
293: errx(1, "no proxy support for local source address");
294:
295: proxyhost = strsep(&proxy, ":");
296: proxyport = proxy;
297:
298: memset(&proxyhints, 0, sizeof(struct addrinfo));
299: proxyhints.ai_family = family;
300: proxyhints.ai_socktype = SOCK_STREAM;
301: proxyhints.ai_protocol = IPPROTO_TCP;
302: if (nflag)
303: proxyhints.ai_flags |= AI_NUMERICHOST;
304: }
305:
1.21 ericj 306: if (lflag) {
307: int connfd;
1.27 ericj 308: ret = 0;
1.1 deraadt 309:
1.42 ericj 310: if (family == AF_UNIX)
311: s = unix_listen(host);
312:
1.67 jmc 313: /* Allow only one connection at a time, but stay alive. */
1.21 ericj 314: for (;;) {
1.42 ericj 315: if (family != AF_UNIX)
316: s = local_listen(host, uport, hints);
317: if (s < 0)
1.30 smart 318: err(1, NULL);
1.21 ericj 319: /*
320: * For UDP, we will use recvfrom() initially
321: * to wait for a caller, then use the regular
322: * functions to talk to the caller.
323: */
324: if (uflag) {
1.80 mcbride 325: int rv, plen;
1.97 ! nicm 326: char buf[16384];
1.21 ericj 327: struct sockaddr_storage z;
328:
329: len = sizeof(z);
1.97 ! nicm 330: plen = jflag ? 16384 : 2048;
1.80 mcbride 331: rv = recvfrom(s, buf, plen, MSG_PEEK,
1.37 jakob 332: (struct sockaddr *)&z, &len);
1.23 ericj 333: if (rv < 0)
1.57 stevesk 334: err(1, "recvfrom");
1.21 ericj 335:
1.37 jakob 336: rv = connect(s, (struct sockaddr *)&z, len);
1.23 ericj 337: if (rv < 0)
1.57 stevesk 338: err(1, "connect");
1.1 deraadt 339:
1.21 ericj 340: connfd = s;
341: } else {
1.78 otto 342: len = sizeof(cliaddr);
1.21 ericj 343: connfd = accept(s, (struct sockaddr *)&cliaddr,
1.37 jakob 344: &len);
1.21 ericj 345: }
1.1 deraadt 346:
1.21 ericj 347: readwrite(connfd);
348: close(connfd);
1.42 ericj 349: if (family != AF_UNIX)
350: close(s);
1.27 ericj 351:
1.21 ericj 352: if (!kflag)
353: break;
1.11 ericj 354: }
1.42 ericj 355: } else if (family == AF_UNIX) {
356: ret = 0;
357:
358: if ((s = unix_connect(host)) > 0 && !zflag) {
359: readwrite(s);
360: close(s);
361: } else
362: ret = 1;
363:
364: exit(ret);
365:
1.21 ericj 366: } else {
367: int i = 0;
1.6 deraadt 368:
1.67 jmc 369: /* Construct the portlist[] array. */
1.21 ericj 370: build_ports(uport);
1.1 deraadt 371:
1.67 jmc 372: /* Cycle through portlist, connecting to each port. */
1.21 ericj 373: for (i = 0; portlist[i] != NULL; i++) {
374: if (s)
375: close(s);
1.34 jakob 376:
377: if (xflag)
378: s = socks_connect(host, portlist[i], hints,
1.86 djm 379: proxyhost, proxyport, proxyhints, socksv,
380: Pflag);
1.34 jakob 381: else
382: s = remote_connect(host, portlist[i], hints);
383:
384: if (s < 0)
1.21 ericj 385: continue;
1.1 deraadt 386:
1.21 ericj 387: ret = 0;
388: if (vflag || zflag) {
1.67 jmc 389: /* For UDP, make sure we are connected. */
1.21 ericj 390: if (uflag) {
1.50 vincent 391: if (udptest(s) == -1) {
1.21 ericj 392: ret = 1;
393: continue;
394: }
395: }
1.1 deraadt 396:
1.67 jmc 397: /* Don't look up port if -n. */
1.21 ericj 398: if (nflag)
399: sv = NULL;
400: else {
401: sv = getservbyport(
1.37 jakob 402: ntohs(atoi(portlist[i])),
403: uflag ? "udp" : "tcp");
1.21 ericj 404: }
1.50 vincent 405:
1.94 mpf 406: fprintf(stderr,
407: "Connection to %s %s port [%s/%s] "
408: "succeeded!\n", host, portlist[i],
409: uflag ? "udp" : "tcp",
1.37 jakob 410: sv ? sv->s_name : "*");
1.21 ericj 411: }
412: if (!zflag)
413: readwrite(s);
1.7 deraadt 414: }
1.11 ericj 415: }
1.1 deraadt 416:
1.21 ericj 417: if (s)
418: close(s);
419:
420: exit(ret);
1.7 deraadt 421: }
1.1 deraadt 422:
1.11 ericj 423: /*
1.42 ericj 424: * unix_connect()
1.67 jmc 425: * Returns a socket connected to a local unix socket. Returns -1 on failure.
1.42 ericj 426: */
427: int
428: unix_connect(char *path)
429: {
430: struct sockaddr_un sun;
431: int s;
432:
433: if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
1.50 vincent 434: return (-1);
1.42 ericj 435: (void)fcntl(s, F_SETFD, 1);
436:
437: memset(&sun, 0, sizeof(struct sockaddr_un));
438: sun.sun_family = AF_UNIX;
1.60 avsm 439:
440: if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
441: sizeof(sun.sun_path)) {
442: close(s);
443: errno = ENAMETOOLONG;
444: return (-1);
445: }
1.50 vincent 446: if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
447: close(s);
448: return (-1);
1.42 ericj 449: }
450: return (s);
1.50 vincent 451:
1.42 ericj 452: }
453:
454: /*
455: * unix_listen()
1.67 jmc 456: * Create a unix domain socket, and listen on it.
1.42 ericj 457: */
458: int
459: unix_listen(char *path)
460: {
461: struct sockaddr_un sun;
462: int s;
463:
1.67 jmc 464: /* Create unix domain socket. */
1.42 ericj 465: if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
466: return (-1);
467:
1.60 avsm 468: memset(&sun, 0, sizeof(struct sockaddr_un));
1.42 ericj 469: sun.sun_family = AF_UNIX;
1.60 avsm 470:
471: if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
472: sizeof(sun.sun_path)) {
473: close(s);
474: errno = ENAMETOOLONG;
475: return (-1);
476: }
477:
1.50 vincent 478: if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
1.42 ericj 479: close(s);
480: return (-1);
481: }
482:
483: if (listen(s, 5) < 0) {
484: close(s);
485: return (-1);
486: }
487: return (s);
488: }
489:
490: /*
1.21 ericj 491: * remote_connect()
1.67 jmc 492: * Returns a socket connected to a remote host. Properly binds to a local
493: * port or source address if needed. Returns -1 on failure.
1.11 ericj 494: */
1.21 ericj 495: int
1.77 otto 496: remote_connect(const char *host, const char *port, struct addrinfo hints)
1.21 ericj 497: {
498: struct addrinfo *res, *res0;
1.91 markus 499: int s, error, on = 1;
1.21 ericj 500:
501: if ((error = getaddrinfo(host, port, &hints, &res)))
1.56 stevesk 502: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 503:
504: res0 = res;
505: do {
506: if ((s = socket(res0->ai_family, res0->ai_socktype,
1.37 jakob 507: res0->ai_protocol)) < 0)
1.21 ericj 508: continue;
509:
1.93 claudio 510: if (rdomain) {
511: if (setsockopt(s, IPPROTO_IP, SO_RDOMAIN, &rdomain,
512: sizeof(rdomain)) == -1)
513: err(1, "setsockopt SO_RDOMAIN");
514: }
515:
1.67 jmc 516: /* Bind to a local port or source address if specified. */
1.21 ericj 517: if (sflag || pflag) {
518: struct addrinfo ahints, *ares;
1.6 deraadt 519:
1.91 markus 520: /* try SO_BINDANY, but don't insist */
521: setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
1.21 ericj 522: memset(&ahints, 0, sizeof(struct addrinfo));
523: ahints.ai_family = res0->ai_family;
524: ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
525: ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
1.25 ericj 526: ahints.ai_flags = AI_PASSIVE;
1.38 jakob 527: if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
1.56 stevesk 528: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21 ericj 529:
530: if (bind(s, (struct sockaddr *)ares->ai_addr,
1.62 millert 531: ares->ai_addrlen) < 0)
1.21 ericj 532: errx(1, "bind failed: %s", strerror(errno));
533: freeaddrinfo(ares);
1.6 deraadt 534: }
1.81 marius 535:
536: set_common_sockopts(s);
1.6 deraadt 537:
1.21 ericj 538: if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
1.6 deraadt 539: break;
1.71 mcbride 540: else if (vflag)
541: warn("connect to %s port %s (%s) failed", host, port,
542: uflag ? "udp" : "tcp");
1.34 jakob 543:
1.21 ericj 544: close(s);
545: s = -1;
546: } while ((res0 = res0->ai_next) != NULL);
547:
548: freeaddrinfo(res);
1.1 deraadt 549:
1.21 ericj 550: return (s);
1.7 deraadt 551: }
1.1 deraadt 552:
1.11 ericj 553: /*
1.21 ericj 554: * local_listen()
1.67 jmc 555: * Returns a socket listening on a local port, binds to specified source
556: * address. Returns -1 on failure.
1.11 ericj 557: */
1.21 ericj 558: int
1.37 jakob 559: local_listen(char *host, char *port, struct addrinfo hints)
1.21 ericj 560: {
561: struct addrinfo *res, *res0;
562: int s, ret, x = 1;
563: int error;
1.6 deraadt 564:
1.67 jmc 565: /* Allow nodename to be null. */
1.21 ericj 566: hints.ai_flags |= AI_PASSIVE;
1.7 deraadt 567:
1.21 ericj 568: /*
569: * In the case of binding to a wildcard address
570: * default to binding to an ipv4 address.
571: */
572: if (host == NULL && hints.ai_family == AF_UNSPEC)
573: hints.ai_family = AF_INET;
1.1 deraadt 574:
1.21 ericj 575: if ((error = getaddrinfo(host, port, &hints, &res)))
1.70 deraadt 576: errx(1, "getaddrinfo: %s", gai_strerror(error));
1.14 ericj 577:
1.21 ericj 578: res0 = res;
579: do {
580: if ((s = socket(res0->ai_family, res0->ai_socktype,
1.82 marius 581: res0->ai_protocol)) < 0)
1.21 ericj 582: continue;
1.1 deraadt 583:
1.93 claudio 584: if (rdomain) {
585: if (setsockopt(s, IPPROTO_IP, SO_RDOMAIN, &rdomain,
586: sizeof(rdomain)) == -1)
587: err(1, "setsockopt SO_RDOMAIN");
588: }
589:
1.21 ericj 590: ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
591: if (ret == -1)
1.30 smart 592: err(1, NULL);
1.81 marius 593:
594: set_common_sockopts(s);
1.1 deraadt 595:
1.21 ericj 596: if (bind(s, (struct sockaddr *)res0->ai_addr,
1.37 jakob 597: res0->ai_addrlen) == 0)
1.21 ericj 598: break;
1.1 deraadt 599:
1.21 ericj 600: close(s);
601: s = -1;
602: } while ((res0 = res0->ai_next) != NULL);
1.1 deraadt 603:
1.47 ericj 604: if (!uflag && s != -1) {
1.21 ericj 605: if (listen(s, 1) < 0)
1.57 stevesk 606: err(1, "listen");
1.12 ericj 607: }
1.1 deraadt 608:
1.21 ericj 609: freeaddrinfo(res);
1.1 deraadt 610:
1.21 ericj 611: return (s);
1.7 deraadt 612: }
613:
1.11 ericj 614: /*
1.21 ericj 615: * readwrite()
616: * Loop that polls on the network file descriptor and stdin.
1.11 ericj 617: */
1.21 ericj 618: void
1.37 jakob 619: readwrite(int nfd)
1.6 deraadt 620: {
1.52 vincent 621: struct pollfd pfd[2];
1.97 ! nicm 622: unsigned char buf[16384];
1.79 avsm 623: int n, wfd = fileno(stdin);
1.21 ericj 624: int lfd = fileno(stdout);
1.80 mcbride 625: int plen;
626:
1.97 ! nicm 627: plen = jflag ? 16384 : 2048;
1.21 ericj 628:
629: /* Setup Network FD */
630: pfd[0].fd = nfd;
631: pfd[0].events = POLLIN;
632:
1.67 jmc 633: /* Set up STDIN FD. */
1.21 ericj 634: pfd[1].fd = wfd;
635: pfd[1].events = POLLIN;
636:
1.54 aaron 637: while (pfd[0].fd != -1) {
1.21 ericj 638: if (iflag)
639: sleep(iflag);
640:
1.68 tedu 641: if ((n = poll(pfd, 2 - dflag, timeout)) < 0) {
1.21 ericj 642: close(nfd);
1.52 vincent 643: err(1, "Polling Error");
1.21 ericj 644: }
1.49 hugh 645:
646: if (n == 0)
647: return;
1.21 ericj 648:
649: if (pfd[0].revents & POLLIN) {
1.80 mcbride 650: if ((n = read(nfd, buf, plen)) < 0)
1.21 ericj 651: return;
1.52 vincent 652: else if (n == 0) {
653: shutdown(nfd, SHUT_RD);
654: pfd[0].fd = -1;
655: pfd[0].events = 0;
1.21 ericj 656: } else {
657: if (tflag)
658: atelnet(nfd, buf, n);
1.79 avsm 659: if (atomicio(vwrite, lfd, buf, n) != n)
1.21 ericj 660: return;
1.6 deraadt 661: }
1.21 ericj 662: }
663:
1.68 tedu 664: if (!dflag && pfd[1].revents & POLLIN) {
1.80 mcbride 665: if ((n = read(wfd, buf, plen)) < 0)
1.21 ericj 666: return;
1.52 vincent 667: else if (n == 0) {
668: shutdown(nfd, SHUT_WR);
669: pfd[1].fd = -1;
670: pfd[1].events = 0;
671: } else {
1.79 avsm 672: if (atomicio(vwrite, nfd, buf, n) != n)
1.21 ericj 673: return;
1.50 vincent 674: }
1.21 ericj 675: }
1.11 ericj 676: }
1.7 deraadt 677: }
1.50 vincent 678:
1.67 jmc 679: /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1.21 ericj 680: void
1.37 jakob 681: atelnet(int nfd, unsigned char *buf, unsigned int size)
1.6 deraadt 682: {
1.24 ericj 683: unsigned char *p, *end;
684: unsigned char obuf[4];
685:
1.95 nicm 686: if (size < 3)
687: return;
688: end = buf + size - 2;
1.24 ericj 689:
690: for (p = buf; p < end; p++) {
1.21 ericj 691: if (*p != IAC)
1.95 nicm 692: continue;
1.24 ericj 693:
1.25 ericj 694: obuf[0] = IAC;
1.24 ericj 695: p++;
1.50 vincent 696: if ((*p == WILL) || (*p == WONT))
1.24 ericj 697: obuf[1] = DONT;
1.95 nicm 698: else if ((*p == DO) || (*p == DONT))
1.24 ericj 699: obuf[1] = WONT;
1.95 nicm 700: else
701: continue;
702:
703: p++;
704: obuf[2] = *p;
705: if (atomicio(vwrite, nfd, obuf, 3) != 3)
706: warn("Write Error!");
1.11 ericj 707: }
1.7 deraadt 708: }
709:
1.11 ericj 710: /*
1.21 ericj 711: * build_ports()
712: * Build an array or ports in portlist[], listing each port
1.67 jmc 713: * that we should try to connect to.
1.11 ericj 714: */
1.21 ericj 715: void
1.37 jakob 716: build_ports(char *p)
1.6 deraadt 717: {
1.88 ray 718: const char *errstr;
719: char *n;
1.21 ericj 720: int hi, lo, cp;
721: int x = 0;
722:
723: if ((n = strchr(p, '-')) != NULL) {
724: if (lflag)
725: errx(1, "Cannot use -l with multiple ports!");
726:
727: *n = '\0';
728: n++;
729:
1.67 jmc 730: /* Make sure the ports are in order: lowest->highest. */
1.88 ray 731: hi = strtonum(n, 1, PORT_MAX, &errstr);
732: if (errstr)
733: errx(1, "port number %s: %s", errstr, n);
734: lo = strtonum(p, 1, PORT_MAX, &errstr);
735: if (errstr)
736: errx(1, "port number %s: %s", errstr, p);
1.21 ericj 737:
738: if (lo > hi) {
739: cp = hi;
740: hi = lo;
741: lo = cp;
742: }
743:
1.67 jmc 744: /* Load ports sequentially. */
1.21 ericj 745: for (cp = lo; cp <= hi; cp++) {
1.55 fgsch 746: portlist[x] = calloc(1, PORT_MAX_LEN);
747: if (portlist[x] == NULL)
748: err(1, NULL);
749: snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
1.21 ericj 750: x++;
751: }
752:
1.67 jmc 753: /* Randomly swap ports. */
1.21 ericj 754: if (rflag) {
755: int y;
756: char *c;
757:
758: for (x = 0; x <= (hi - lo); x++) {
759: y = (arc4random() & 0xFFFF) % (hi - lo);
760: c = portlist[x];
761: portlist[x] = portlist[y];
762: portlist[y] = c;
1.6 deraadt 763: }
1.11 ericj 764: }
1.21 ericj 765: } else {
1.88 ray 766: hi = strtonum(p, 1, PORT_MAX, &errstr);
767: if (errstr)
768: errx(1, "port number %s: %s", errstr, p);
1.96 nicm 769: portlist[0] = strdup(p);
1.55 fgsch 770: if (portlist[0] == NULL)
771: err(1, NULL);
1.11 ericj 772: }
1.13 ericj 773: }
774:
775: /*
1.21 ericj 776: * udptest()
777: * Do a few writes to see if the UDP port is there.
1.67 jmc 778: * XXX - Better way of doing this? Doesn't work for IPv6.
1.21 ericj 779: * Also fails after around 100 ports checked.
1.13 ericj 780: */
1.21 ericj 781: int
1.37 jakob 782: udptest(int s)
1.13 ericj 783: {
1.74 deraadt 784: int i, ret;
1.13 ericj 785:
1.52 vincent 786: for (i = 0; i <= 3; i++) {
1.74 deraadt 787: if (write(s, "X", 1) == 1)
1.21 ericj 788: ret = 1;
1.14 ericj 789: else
1.21 ericj 790: ret = -1;
1.14 ericj 791: }
1.21 ericj 792: return (ret);
1.81 marius 793: }
794:
1.84 dtucker 795: void
1.81 marius 796: set_common_sockopts(int s)
797: {
798: int x = 1;
799:
800: if (Sflag) {
801: if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
802: &x, sizeof(x)) == -1)
803: err(1, NULL);
804: }
805: if (Dflag) {
806: if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
807: &x, sizeof(x)) == -1)
808: err(1, NULL);
809: }
810: if (jflag) {
811: if (setsockopt(s, SOL_SOCKET, SO_JUMBO,
812: &x, sizeof(x)) == -1)
813: err(1, NULL);
814: }
1.83 dtucker 815: if (Tflag != -1) {
816: if (setsockopt(s, IPPROTO_IP, IP_TOS,
817: &Tflag, sizeof(Tflag)) == -1)
818: err(1, "set IP ToS");
819: }
1.90 djm 820: if (Iflag) {
821: if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
822: &Iflag, sizeof(Iflag)) == -1)
823: err(1, "set TCP receive buffer size");
824: }
825: if (Oflag) {
826: if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
827: &Oflag, sizeof(Oflag)) == -1)
828: err(1, "set TCP send buffer size");
829: }
1.83 dtucker 830: }
831:
832: int
833: parse_iptos(char *s)
834: {
835: int tos = -1;
836:
837: if (strcmp(s, "lowdelay") == 0)
838: return (IPTOS_LOWDELAY);
839: if (strcmp(s, "throughput") == 0)
840: return (IPTOS_THROUGHPUT);
841: if (strcmp(s, "reliability") == 0)
842: return (IPTOS_RELIABILITY);
843:
844: if (sscanf(s, "0x%x", &tos) != 1 || tos < 0 || tos > 0xff)
845: errx(1, "invalid IP Type of Service");
846: return (tos);
1.7 deraadt 847: }
1.1 deraadt 848:
1.11 ericj 849: void
1.58 deraadt 850: help(void)
1.1 deraadt 851: {
1.21 ericj 852: usage(0);
853: fprintf(stderr, "\tCommand Summary:\n\
854: \t-4 Use IPv4\n\
855: \t-6 Use IPv6\n\
1.73 markus 856: \t-D Enable the debug socket option\n\
1.69 tedu 857: \t-d Detach from stdin\n\
1.21 ericj 858: \t-h This help text\n\
1.90 djm 859: \t-I length TCP receive buffer length\n\
1.21 ericj 860: \t-i secs\t Delay interval for lines sent, ports scanned\n\
861: \t-k Keep inbound sockets open for multiple connects\n\
862: \t-l Listen mode, for inbound connects\n\
1.22 jasoni 863: \t-n Suppress name/port resolutions\n\
1.90 djm 864: \t-O length TCP send buffer length\n\
1.86 djm 865: \t-P proxyuser\tUsername for proxy authentication\n\
1.36 jakob 866: \t-p port\t Specify local port for remote connects\n\
1.21 ericj 867: \t-r Randomize remote ports\n\
1.67 jmc 868: \t-S Enable the TCP MD5 signature option\n\
1.21 ericj 869: \t-s addr\t Local source address\n\
1.83 dtucker 870: \t-T ToS\t Set IP Type of Service\n\
1.21 ericj 871: \t-t Answer TELNET negotiation\n\
1.67 jmc 872: \t-U Use UNIX domain socket\n\
1.21 ericj 873: \t-u UDP mode\n\
1.93 claudio 874: \t-V rdomain Specify alternate routing domain\n\
1.21 ericj 875: \t-v Verbose\n\
876: \t-w secs\t Timeout for connects and final net reads\n\
1.75 djm 877: \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
878: \t-x addr[:port]\tSpecify proxy address and port\n\
1.21 ericj 879: \t-z Zero-I/O mode [used for scanning]\n\
880: Port numbers can be individual or ranges: lo-hi [inclusive]\n");
881: exit(1);
1.11 ericj 882: }
883:
884: void
1.37 jakob 885: usage(int ret)
1.11 ericj 886: {
1.92 sobrado 887: fprintf(stderr,
888: "usage: nc [-46DdhklnrStUuvz] [-I length] [-i interval] [-O length]\n"
889: "\t [-P proxy_username] [-p source_port] [-s source_ip_address] [-T ToS]\n"
1.93 claudio 890: "\t [-V rdomain] [-w timeout] [-X proxy_protocol]\n"
891: "\t [-x proxy_address[:port]] [hostname] [port]\n");
1.21 ericj 892: if (ret)
893: exit(1);
1.7 deraadt 894: }