[BACK]Return to netcat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / nc

Annotation of src/usr.bin/nc/netcat.c, Revision 1.56

1.56    ! stevesk     1: /* $OpenBSD: netcat.c,v 1.55 2002/12/28 10:24:09 fgsch 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.21      ericj      40: #include <arpa/telnet.h>
1.29      smart      41:
1.11      ericj      42: #include <err.h>
1.7       deraadt    43: #include <errno.h>
1.21      ericj      44: #include <netdb.h>
                     45: #include <poll.h>
1.13      ericj      46: #include <stdarg.h>
1.21      ericj      47: #include <stdio.h>
1.1       deraadt    48: #include <stdlib.h>
1.21      ericj      49: #include <string.h>
1.5       art        50: #include <unistd.h>
1.42      ericj      51: #include <fcntl.h>
1.51      vincent    52:
                     53: #ifndef SUN_LEN
                     54: #define SUN_LEN(su) \
                     55:        (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
                     56: #endif
1.1       deraadt    57:
1.55      fgsch      58: #define PORT_MAX       65535
                     59: #define PORT_MAX_LEN   6
1.31      ericj      60:
1.21      ericj      61: /* Command Line Options */
                     62: int    iflag;                                  /* Interval Flag */
                     63: int    kflag;                                  /* More than one connect */
                     64: int    lflag;                                  /* Bind to local port */
                     65: int    nflag;                                  /* Dont do name lookup */
                     66: char   *pflag;                                 /* Localport flag */
                     67: int    rflag;                                  /* Random ports flag */
                     68: char   *sflag;                                 /* Source Address */
                     69: int    tflag;                                  /* Telnet Emulation */
                     70: int    uflag;                                  /* UDP - Default to TCP */
                     71: int    vflag;                                  /* Verbosity */
1.34      jakob      72: int    xflag;                                  /* Socks proxy */
1.21      ericj      73: int    zflag;                                  /* Port Scan Flag */
                     74:
1.49      hugh       75: int timeout = -1;
1.21      ericj      76: int family = AF_UNSPEC;
1.31      ericj      77: char *portlist[PORT_MAX];
1.21      ericj      78:
1.44      millert    79: ssize_t        atomicio(ssize_t (*)(), int, void *, size_t);
1.40      millert    80: void   atelnet(int, unsigned char *, unsigned int);
                     81: void   build_ports(char *);
                     82: void   help(void);
                     83: int    local_listen(char *, char *, struct addrinfo);
                     84: void   readwrite(int);
                     85: int    remote_connect(char *, char *, struct addrinfo);
1.45      ericj      86: int    socks_connect(char *, char *, struct addrinfo, char *, char *,
1.46      markus     87:        struct addrinfo, int);
1.40      millert    88: int    udptest(int);
1.42      ericj      89: int    unix_connect(char *);
                     90: int    unix_listen(char *);
1.40      millert    91: void   usage(int);
1.1       deraadt    92:
1.21      ericj      93: int
1.37      jakob      94: main(int argc, char *argv[])
1.21      ericj      95: {
1.46      markus     96:        int ch, s, ret, socksv;
1.26      ericj      97:        char *host, *uport, *endp;
1.21      ericj      98:        struct addrinfo hints;
1.29      smart      99:        struct servent *sv;
1.21      ericj     100:        socklen_t len;
                    101:        struct sockaddr *cliaddr;
1.34      jakob     102:        char *proxy;
1.45      ericj     103:        char *proxyhost = "", *proxyport = NULL;
1.34      jakob     104:        struct addrinfo proxyhints;
1.11      ericj     105:
1.29      smart     106:        ret = 1;
                    107:        s = 0;
1.46      markus    108:        socksv = 5;
1.29      smart     109:        host = NULL;
                    110:        uport = NULL;
                    111:        endp = NULL;
                    112:        sv = NULL;
                    113:
1.46      markus    114:        while ((ch = getopt(argc, argv, "46UX:hi:klnp:rs:tuvw:x:z")) != -1) {
1.21      ericj     115:                switch (ch) {
                    116:                case '4':
                    117:                        family = AF_INET;
                    118:                        break;
                    119:                case '6':
                    120:                        family = AF_INET6;
                    121:                        break;
1.42      ericj     122:                case 'U':
                    123:                        family = AF_UNIX;
                    124:                        break;
1.46      markus    125:                case 'X':
                    126:                        socksv = (int)strtoul(optarg, &endp, 10);
                    127:                        if ((socksv != 4 && socksv != 5) || *endp != '\0')
                    128:                                errx(1, "only SOCKS version 4 and 5 supported");
                    129:                        break;
1.21      ericj     130:                case 'h':
                    131:                        help();
                    132:                        break;
                    133:                case 'i':
1.26      ericj     134:                        iflag = (int)strtoul(optarg, &endp, 10);
                    135:                        if (iflag < 0 || *endp != '\0')
                    136:                                errx(1, "interval cannot be negative");
1.21      ericj     137:                        break;
                    138:                case 'k':
                    139:                        kflag = 1;
                    140:                        break;
                    141:                case 'l':
                    142:                        lflag = 1;
                    143:                        break;
                    144:                case 'n':
                    145:                        nflag = 1;
                    146:                        break;
                    147:                case 'p':
                    148:                        pflag = optarg;
                    149:                        break;
                    150:                case 'r':
                    151:                        rflag = 1;
                    152:                        break;
                    153:                case 's':
                    154:                        sflag = optarg;
                    155:                        break;
                    156:                case 't':
                    157:                        tflag = 1;
                    158:                        break;
                    159:                case 'u':
                    160:                        uflag = 1;
                    161:                        break;
                    162:                case 'v':
                    163:                        vflag = 1;
                    164:                        break;
1.26      ericj     165:                case 'w':
                    166:                        timeout = (int)strtoul(optarg, &endp, 10);
                    167:                        if (timeout < 0 || *endp != '\0')
                    168:                                errx(1, "timeout cannot be negative");
1.49      hugh      169:                        if (timeout >= (INT_MAX / 1000))
                    170:                                errx(1, "timeout too large");
                    171:                        timeout *= 1000;
1.21      ericj     172:                        break;
1.34      jakob     173:                case 'x':
                    174:                        xflag = 1;
                    175:                        proxy = strdup(optarg);
                    176:                        break;
1.21      ericj     177:                case 'z':
                    178:                        zflag = 1;
                    179:                        break;
                    180:                default:
                    181:                        usage(1);
                    182:                }
                    183:        }
                    184:        argc -= optind;
                    185:        argv += optind;
1.11      ericj     186:
1.21      ericj     187:        /* Cruft to make sure options are clean, and used properly. */
1.42      ericj     188:        if (argv[0] && !argv[1] && family == AF_UNIX) {
                    189:                if (uflag)
                    190:                        errx(1, "cannot use -u and -U");
                    191:                host = argv[0];
                    192:                uport = NULL;
                    193:        } else if (argv[0] && !argv[1]) {
1.21      ericj     194:                if  (!lflag)
                    195:                        usage(1);
                    196:                uport = argv[0];
                    197:                host = NULL;
                    198:        } else if (argv[0] && argv[1]) {
                    199:                host = argv[0];
                    200:                uport = argv[1];
                    201:        } else
                    202:                usage(1);
1.1       deraadt   203:
1.21      ericj     204:        if (lflag && sflag)
                    205:                errx(1, "cannot use -s and -l");
                    206:        if (lflag && pflag)
                    207:                errx(1, "cannot use -p and -l");
                    208:        if (lflag && zflag)
1.32      ericj     209:                errx(1, "cannot use -z and -l");
1.21      ericj     210:        if (!lflag && kflag)
1.32      ericj     211:                errx(1, "must use -l with -k");
1.21      ericj     212:
                    213:        /* Initialize addrinfo structure */
1.42      ericj     214:        if (family != AF_UNIX) {
                    215:                memset(&hints, 0, sizeof(struct addrinfo));
                    216:                hints.ai_family = family;
                    217:                hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
                    218:                hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
                    219:                if (nflag)
                    220:                        hints.ai_flags |= AI_NUMERICHOST;
                    221:        }
1.50      vincent   222:
1.1       deraadt   223:
1.34      jakob     224:        if (xflag) {
                    225:                if (uflag)
                    226:                        errx(1, "no proxy support for UDP mode");
                    227:
                    228:                if (lflag)
                    229:                        errx(1, "no proxy support for listen");
                    230:
1.42      ericj     231:                if (family == AF_UNIX)
                    232:                        errx(1, "no proxy support for unix sockets");
                    233:
1.34      jakob     234:                /* XXX IPv6 transport to proxy would probably work */
                    235:                if (family == AF_INET6)
                    236:                        errx(1, "no proxy support for IPv6");
                    237:
                    238:                if (sflag)
                    239:                        errx(1, "no proxy support for local source address");
                    240:
                    241:                proxyhost = strsep(&proxy, ":");
                    242:                proxyport = proxy;
                    243:
                    244:                memset(&proxyhints, 0, sizeof(struct addrinfo));
                    245:                proxyhints.ai_family = family;
                    246:                proxyhints.ai_socktype = SOCK_STREAM;
                    247:                proxyhints.ai_protocol = IPPROTO_TCP;
                    248:                if (nflag)
                    249:                        proxyhints.ai_flags |= AI_NUMERICHOST;
                    250:        }
                    251:
1.21      ericj     252:        if (lflag) {
                    253:                int connfd;
1.27      ericj     254:                ret = 0;
1.1       deraadt   255:
1.42      ericj     256:                if (family == AF_UNIX)
                    257:                        s = unix_listen(host);
                    258:
1.21      ericj     259:                /* Allow only one connection at a time, but stay alive */
                    260:                for (;;) {
1.42      ericj     261:                        if (family != AF_UNIX)
                    262:                                s = local_listen(host, uport, hints);
                    263:                        if (s < 0)
1.30      smart     264:                                err(1, NULL);
1.21      ericj     265:                        /*
                    266:                         * For UDP, we will use recvfrom() initially
                    267:                         * to wait for a caller, then use the regular
                    268:                         * functions to talk to the caller.
                    269:                         */
                    270:                        if (uflag) {
1.23      ericj     271:                                int rv;
1.21      ericj     272:                                char buf[1024];
                    273:                                struct sockaddr_storage z;
                    274:
                    275:                                len = sizeof(z);
1.23      ericj     276:                                rv = recvfrom(s, buf, sizeof(buf), MSG_PEEK,
1.37      jakob     277:                                    (struct sockaddr *)&z, &len);
1.23      ericj     278:                                if (rv < 0)
1.21      ericj     279:                                        errx(1, "%s", strerror(errno));
                    280:
1.37      jakob     281:                                rv = connect(s, (struct sockaddr *)&z, len);
1.23      ericj     282:                                if (rv < 0)
1.21      ericj     283:                                        errx(1, "%s", strerror(errno));
1.1       deraadt   284:
1.21      ericj     285:                                connfd = s;
                    286:                        } else {
                    287:                                connfd = accept(s, (struct sockaddr *)&cliaddr,
1.37      jakob     288:                                    &len);
1.21      ericj     289:                        }
1.1       deraadt   290:
1.21      ericj     291:                        readwrite(connfd);
                    292:                        close(connfd);
1.42      ericj     293:                        if (family != AF_UNIX)
                    294:                                close(s);
1.27      ericj     295:
1.21      ericj     296:                        if (!kflag)
                    297:                                break;
1.11      ericj     298:                }
1.42      ericj     299:        } else if (family == AF_UNIX) {
                    300:                ret = 0;
                    301:
                    302:                if ((s = unix_connect(host)) > 0 && !zflag) {
                    303:                        readwrite(s);
                    304:                        close(s);
                    305:                } else
                    306:                        ret = 1;
                    307:
                    308:                exit(ret);
                    309:
1.21      ericj     310:        } else {
                    311:                int i = 0;
1.6       deraadt   312:
1.21      ericj     313:                /* construct the portlist[] array */
                    314:                build_ports(uport);
1.1       deraadt   315:
1.21      ericj     316:                /* Cycle through portlist, connecting to each port */
                    317:                for (i = 0; portlist[i] != NULL; i++) {
                    318:                        if (s)
                    319:                                close(s);
1.34      jakob     320:
                    321:                        if (xflag)
                    322:                                s = socks_connect(host, portlist[i], hints,
1.46      markus    323:                                    proxyhost, proxyport, proxyhints, socksv);
1.34      jakob     324:                        else
                    325:                                s = remote_connect(host, portlist[i], hints);
                    326:
                    327:                        if (s < 0)
1.21      ericj     328:                                continue;
1.1       deraadt   329:
1.21      ericj     330:                        ret = 0;
                    331:                        if (vflag || zflag) {
                    332:                                /* For UDP, make sure we are connected */
                    333:                                if (uflag) {
1.50      vincent   334:                                        if (udptest(s) == -1) {
1.21      ericj     335:                                                ret = 1;
                    336:                                                continue;
                    337:                                        }
                    338:                                }
1.1       deraadt   339:
1.21      ericj     340:                                /* Don't lookup port if -n */
                    341:                                if (nflag)
                    342:                                        sv = NULL;
                    343:                                else {
                    344:                                        sv = getservbyport(
1.37      jakob     345:                                            ntohs(atoi(portlist[i])),
                    346:                                            uflag ? "udp" : "tcp");
1.21      ericj     347:                                }
1.50      vincent   348:
1.21      ericj     349:                                printf("Connection to %s %s port [%s/%s] succeeded!\n",
1.37      jakob     350:                                    host, portlist[i], uflag ? "udp" : "tcp",
                    351:                                    sv ? sv->s_name : "*");
1.21      ericj     352:                        }
                    353:                        if (!zflag)
                    354:                                readwrite(s);
1.7       deraadt   355:                }
1.11      ericj     356:        }
1.1       deraadt   357:
1.21      ericj     358:        if (s)
                    359:                close(s);
                    360:
                    361:        exit(ret);
1.7       deraadt   362: }
1.1       deraadt   363:
1.11      ericj     364: /*
1.42      ericj     365:  * unix_connect()
                    366:  * Return's a socket connected to a local unix socket. Return's -1 on failure.
                    367:  */
                    368: int
                    369: unix_connect(char *path)
                    370: {
                    371:        struct sockaddr_un sun;
                    372:        int s;
                    373:
                    374:        if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
1.50      vincent   375:                return (-1);
1.42      ericj     376:        (void)fcntl(s, F_SETFD, 1);
                    377:
                    378:        memset(&sun, 0, sizeof(struct sockaddr_un));
                    379:        sun.sun_family = AF_UNIX;
                    380:        strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
1.50      vincent   381:        if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
                    382:                close(s);
                    383:                return (-1);
1.42      ericj     384:        }
                    385:        return (s);
1.50      vincent   386:
1.42      ericj     387: }
                    388:
                    389: /*
                    390:  * unix_listen()
                    391:  * create a unix domain socket, and listen on it.
                    392:  */
                    393: int
                    394: unix_listen(char *path)
                    395: {
                    396:        struct sockaddr_un sun;
                    397:        int s;
                    398:
                    399:        /* create unix domain socket */
                    400:        if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
                    401:                return (-1);
                    402:
1.50      vincent   403:        strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
1.42      ericj     404:        sun.sun_family = AF_UNIX;
1.50      vincent   405:        if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
1.42      ericj     406:                close(s);
                    407:                return (-1);
                    408:        }
                    409:
                    410:        if (listen(s, 5) < 0) {
                    411:                close(s);
                    412:                return (-1);
                    413:        }
                    414:        return (s);
                    415: }
                    416:
                    417: /*
1.21      ericj     418:  * remote_connect()
                    419:  * Return's a socket connected to a remote host. Properly bind's to a local
                    420:  * port or source address if needed. Return's -1 on failure.
1.11      ericj     421:  */
1.21      ericj     422: int
1.37      jakob     423: remote_connect(char *host, char *port, struct addrinfo hints)
1.21      ericj     424: {
                    425:        struct addrinfo *res, *res0;
                    426:        int s, error;
                    427:
                    428:        if ((error = getaddrinfo(host, port, &hints, &res)))
1.56    ! stevesk   429:                errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21      ericj     430:
                    431:        res0 = res;
                    432:        do {
                    433:                if ((s = socket(res0->ai_family, res0->ai_socktype,
1.37      jakob     434:                    res0->ai_protocol)) < 0)
1.21      ericj     435:                        continue;
                    436:
                    437:                /* Bind to a local port or source address if specified */
                    438:                if (sflag || pflag) {
                    439:                        struct addrinfo ahints, *ares;
                    440:
                    441:                        if (!(sflag && pflag)) {
                    442:                                if (!sflag)
                    443:                                        sflag = NULL;
                    444:                                else
                    445:                                        pflag = NULL;
                    446:                        }
1.6       deraadt   447:
1.21      ericj     448:                        memset(&ahints, 0, sizeof(struct addrinfo));
                    449:                        ahints.ai_family = res0->ai_family;
                    450:                        ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
                    451:                        ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
1.25      ericj     452:                        ahints.ai_flags = AI_PASSIVE;
1.38      jakob     453:                        if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
1.56    ! stevesk   454:                                errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21      ericj     455:
                    456:                        if (bind(s, (struct sockaddr *)ares->ai_addr,
1.50      vincent   457:                            ares->ai_addrlen) < 0) {
1.21      ericj     458:                                errx(1, "bind failed: %s", strerror(errno));
                    459:                                freeaddrinfo(ares);
                    460:                                continue;
                    461:                        }
                    462:                        freeaddrinfo(ares);
1.6       deraadt   463:                }
                    464:
1.21      ericj     465:                if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
1.6       deraadt   466:                        break;
1.34      jakob     467:
1.21      ericj     468:                close(s);
                    469:                s = -1;
                    470:        } while ((res0 = res0->ai_next) != NULL);
                    471:
                    472:        freeaddrinfo(res);
1.1       deraadt   473:
1.21      ericj     474:        return (s);
1.7       deraadt   475: }
1.1       deraadt   476:
1.11      ericj     477: /*
1.21      ericj     478:  * local_listen()
                    479:  * Return's a socket listening on a local port, binds to specified source
                    480:  * address. Return's -1 on failure.
1.11      ericj     481:  */
1.21      ericj     482: int
1.37      jakob     483: local_listen(char *host, char *port, struct addrinfo hints)
1.21      ericj     484: {
                    485:        struct addrinfo *res, *res0;
                    486:        int s, ret, x = 1;
                    487:        int error;
1.6       deraadt   488:
1.21      ericj     489:        /* Allow nodename to be null */
                    490:        hints.ai_flags |= AI_PASSIVE;
1.7       deraadt   491:
1.21      ericj     492:        /*
                    493:         * In the case of binding to a wildcard address
                    494:         * default to binding to an ipv4 address.
                    495:         */
                    496:        if (host == NULL && hints.ai_family == AF_UNSPEC)
                    497:                hints.ai_family = AF_INET;
1.1       deraadt   498:
1.21      ericj     499:        if ((error = getaddrinfo(host, port, &hints, &res)))
1.56    ! stevesk   500:                 errx(1, "getaddrinfo: %s", gai_strerror(error));
1.14      ericj     501:
1.21      ericj     502:        res0 = res;
                    503:        do {
                    504:                if ((s = socket(res0->ai_family, res0->ai_socktype,
1.37      jakob     505:                    res0->ai_protocol)) == 0)
1.21      ericj     506:                        continue;
1.1       deraadt   507:
1.21      ericj     508:                ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
                    509:                if (ret == -1)
1.30      smart     510:                        err(1, NULL);
1.1       deraadt   511:
1.21      ericj     512:                if (bind(s, (struct sockaddr *)res0->ai_addr,
1.37      jakob     513:                    res0->ai_addrlen) == 0)
1.21      ericj     514:                        break;
1.1       deraadt   515:
1.21      ericj     516:                close(s);
                    517:                s = -1;
                    518:        } while ((res0 = res0->ai_next) != NULL);
1.1       deraadt   519:
1.47      ericj     520:        if (!uflag && s != -1) {
1.21      ericj     521:                if (listen(s, 1) < 0)
                    522:                        errx(1, "%s", strerror(errno));
1.12      ericj     523:        }
1.1       deraadt   524:
1.21      ericj     525:        freeaddrinfo(res);
1.1       deraadt   526:
1.21      ericj     527:        return (s);
1.7       deraadt   528: }
                    529:
1.11      ericj     530: /*
1.21      ericj     531:  * readwrite()
                    532:  * Loop that polls on the network file descriptor and stdin.
1.11      ericj     533:  */
1.21      ericj     534: void
1.37      jakob     535: readwrite(int nfd)
1.6       deraadt   536: {
1.52      vincent   537:        struct pollfd pfd[2];
1.21      ericj     538:        char buf[BUFSIZ];
                    539:        int wfd = fileno(stdin), n, ret;
                    540:        int lfd = fileno(stdout);
                    541:
                    542:        /* Setup Network FD */
                    543:        pfd[0].fd = nfd;
                    544:        pfd[0].events = POLLIN;
                    545:
                    546:        /* Setup STDIN FD */
                    547:        pfd[1].fd = wfd;
                    548:        pfd[1].events = POLLIN;
                    549:
1.54      aaron     550:        while (pfd[0].fd != -1) {
1.21      ericj     551:                if (iflag)
                    552:                        sleep(iflag);
                    553:
1.49      hugh      554:                if ((n = poll(pfd, 2, timeout)) < 0) {
1.21      ericj     555:                        close(nfd);
1.52      vincent   556:                        err(1, "Polling Error");
1.21      ericj     557:                }
1.49      hugh      558:
                    559:                if (n == 0)
                    560:                        return;
1.21      ericj     561:
                    562:                if (pfd[0].revents & POLLIN) {
1.52      vincent   563:                        if ((n = read(nfd, buf, sizeof(buf))) < 0)
1.21      ericj     564:                                return;
1.52      vincent   565:                        else if (n == 0) {
                    566:                                shutdown(nfd, SHUT_RD);
                    567:                                pfd[0].fd = -1;
                    568:                                pfd[0].events = 0;
1.21      ericj     569:                        } else {
                    570:                                if (tflag)
                    571:                                        atelnet(nfd, buf, n);
                    572:                                if ((ret = atomicio(write, lfd, buf, n)) != n)
                    573:                                        return;
1.6       deraadt   574:                        }
1.21      ericj     575:                }
                    576:
                    577:                if (pfd[1].revents & POLLIN) {
1.50      vincent   578:                        if ((n = read(wfd, buf, sizeof(buf))) < 0)
1.21      ericj     579:                                return;
1.52      vincent   580:                        else if (n == 0) {
                    581:                                shutdown(nfd, SHUT_WR);
                    582:                                pfd[1].fd = -1;
                    583:                                pfd[1].events = 0;
                    584:                        } else {
1.21      ericj     585:                                if((ret = atomicio(write, nfd, buf, n)) != n)
                    586:                                        return;
1.50      vincent   587:                        }
1.21      ericj     588:                }
1.11      ericj     589:        }
1.7       deraadt   590: }
1.50      vincent   591:
1.24      ericj     592: /* Deal with RFC854 WILL/WONT DO/DONT negotiation */
1.21      ericj     593: void
1.37      jakob     594: atelnet(int nfd, unsigned char *buf, unsigned int size)
1.6       deraadt   595: {
1.28      ericj     596:        int ret;
1.24      ericj     597:        unsigned char *p, *end;
                    598:        unsigned char obuf[4];
                    599:
                    600:        end = buf + size;
                    601:        obuf[0] = '\0';
                    602:
                    603:        for (p = buf; p < end; p++) {
1.21      ericj     604:                if (*p != IAC)
1.24      ericj     605:                        break;
                    606:
1.25      ericj     607:                obuf[0] = IAC;
1.24      ericj     608:                p++;
1.50      vincent   609:                if ((*p == WILL) || (*p == WONT))
1.24      ericj     610:                        obuf[1] = DONT;
1.50      vincent   611:                if ((*p == DO) || (*p == DONT))
1.24      ericj     612:                        obuf[1] = WONT;
                    613:                if (obuf) {
                    614:                        p++;
1.11      ericj     615:                        obuf[2] = *p;
1.24      ericj     616:                        obuf[3] = '\0';
1.21      ericj     617:                        if ((ret = atomicio(write , nfd, obuf, 3)) != 3)
                    618:                                warnx("Write Error!");
1.24      ericj     619:                        obuf[0] = '\0';
1.11      ericj     620:                }
                    621:        }
1.7       deraadt   622: }
                    623:
1.11      ericj     624: /*
1.21      ericj     625:  * build_ports()
                    626:  * Build an array or ports in portlist[], listing each port
                    627:  * that we should try to connect too.
1.11      ericj     628:  */
1.21      ericj     629: void
1.37      jakob     630: build_ports(char *p)
1.6       deraadt   631: {
1.26      ericj     632:        char *n, *endp;
1.21      ericj     633:        int hi, lo, cp;
                    634:        int x = 0;
                    635:
                    636:        if ((n = strchr(p, '-')) != NULL) {
                    637:                if (lflag)
                    638:                        errx(1, "Cannot use -l with multiple ports!");
                    639:
                    640:                *n = '\0';
                    641:                n++;
                    642:
                    643:                /* Make sure the ports are in order: lowest->highest */
1.26      ericj     644:                hi = (int)strtoul(n, &endp, 10);
1.31      ericj     645:                if (hi <= 0 || hi > PORT_MAX || *endp != '\0')
1.26      ericj     646:                        errx(1, "port range not valid");
                    647:                lo = (int)strtoul(p, &endp, 10);
1.31      ericj     648:                if (lo <= 0 || lo > PORT_MAX || *endp != '\0')
1.26      ericj     649:                        errx(1, "port range not valid");
1.21      ericj     650:
                    651:                if (lo > hi) {
                    652:                        cp = hi;
                    653:                        hi = lo;
                    654:                        lo = cp;
                    655:                }
                    656:
                    657:                /* Load ports sequentially */
                    658:                for (cp = lo; cp <= hi; cp++) {
1.55      fgsch     659:                        portlist[x] = calloc(1, PORT_MAX_LEN);
                    660:                        if (portlist[x] == NULL)
                    661:                                err(1, NULL);
                    662:                        snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
1.21      ericj     663:                        x++;
                    664:                }
                    665:
                    666:                /* Randomly swap ports */
                    667:                if (rflag) {
                    668:                        int y;
                    669:                        char *c;
                    670:
                    671:                        for (x = 0; x <= (hi - lo); x++) {
                    672:                                y = (arc4random() & 0xFFFF) % (hi - lo);
                    673:                                c = portlist[x];
                    674:                                portlist[x] = portlist[y];
                    675:                                portlist[y] = c;
1.6       deraadt   676:                        }
1.11      ericj     677:                }
1.21      ericj     678:        } else {
1.26      ericj     679:                hi = (int)strtoul(p, &endp, 10);
1.31      ericj     680:                if (hi <= 0 || hi > PORT_MAX || *endp != '\0')
1.26      ericj     681:                        errx(1, "port range not valid");
1.55      fgsch     682:                portlist[0] = calloc(1, PORT_MAX_LEN);
                    683:                if (portlist[0] == NULL)
                    684:                        err(1, NULL);
1.21      ericj     685:                portlist[0] = p;
1.11      ericj     686:        }
1.13      ericj     687: }
                    688:
                    689: /*
1.21      ericj     690:  * udptest()
                    691:  * Do a few writes to see if the UDP port is there.
                    692:  * XXX - Better way of doing this? Doesn't work for IPv6
                    693:  * Also fails after around 100 ports checked.
1.13      ericj     694:  */
1.21      ericj     695: int
1.37      jakob     696: udptest(int s)
1.13      ericj     697: {
1.21      ericj     698:        int i, rv, ret;
1.13      ericj     699:
1.52      vincent   700:        for (i = 0; i <= 3; i++) {
1.21      ericj     701:                if ((rv = write(s, "X", 1)) == 1)
                    702:                        ret = 1;
1.14      ericj     703:                else
1.21      ericj     704:                        ret = -1;
1.14      ericj     705:        }
1.21      ericj     706:        return (ret);
1.7       deraadt   707: }
1.1       deraadt   708:
1.11      ericj     709: void
1.21      ericj     710: help()
1.1       deraadt   711: {
1.21      ericj     712:        usage(0);
                    713:        fprintf(stderr, "\tCommand Summary:\n\
                    714:        \t-4            Use IPv4\n\
                    715:        \t-6            Use IPv6\n\
1.42      ericj     716:        \t-U            Use UNIX domain socket\n\
1.53      markus    717:        \t-X vers\t     SOCKS version (4 or 5)\n\
1.21      ericj     718:        \t-h            This help text\n\
                    719:        \t-i secs\t     Delay interval for lines sent, ports scanned\n\
                    720:        \t-k            Keep inbound sockets open for multiple connects\n\
                    721:        \t-l            Listen mode, for inbound connects\n\
1.22      jasoni    722:        \t-n            Suppress name/port resolutions\n\
1.36      jakob     723:        \t-p port\t     Specify local port for remote connects\n\
1.21      ericj     724:        \t-r            Randomize remote ports\n\
                    725:        \t-s addr\t     Local source address\n\
                    726:        \t-t            Answer TELNET negotiation\n\
                    727:        \t-u            UDP mode\n\
                    728:        \t-v            Verbose\n\
                    729:        \t-w secs\t     Timeout for connects and final net reads\n\
1.53      markus    730:        \t-x addr[:port]\tSpecify socks proxy address and port\n\
1.21      ericj     731:        \t-z            Zero-I/O mode [used for scanning]\n\
                    732:        Port numbers can be individual or ranges: lo-hi [inclusive]\n");
                    733:        exit(1);
1.11      ericj     734: }
                    735:
                    736: void
1.37      jakob     737: usage(int ret)
1.11      ericj     738: {
1.43      ericj     739:        fprintf(stderr, "usage: nc [-46Uhklnrtuvz] [-i interval] [-p source port]\n");
1.53      markus    740:        fprintf(stderr, "\t  [-s ip address] [-w timeout] [-X vers] [-x proxy address [:port]]\n");
1.36      jakob     741:        fprintf(stderr, "\t  [hostname] [port[s...]]\n");
1.21      ericj     742:        if (ret)
                    743:                exit(1);
1.7       deraadt   744: }