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