[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.117

1.117   ! sthen       1: /* $OpenBSD: netcat.c,v 1.116 2013/10/21 09:12:55 phessler 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.113     djm        37: #include <sys/uio.h>
1.42      ericj      38: #include <sys/un.h>
1.21      ericj      39:
1.7       deraadt    40: #include <netinet/in.h>
1.83      dtucker    41: #include <netinet/in_systm.h>
1.65      markus     42: #include <netinet/tcp.h>
1.83      dtucker    43: #include <netinet/ip.h>
1.21      ericj      44: #include <arpa/telnet.h>
1.29      smart      45:
1.11      ericj      46: #include <err.h>
1.7       deraadt    47: #include <errno.h>
1.21      ericj      48: #include <netdb.h>
                     49: #include <poll.h>
1.13      ericj      50: #include <stdarg.h>
1.21      ericj      51: #include <stdio.h>
1.1       deraadt    52: #include <stdlib.h>
1.21      ericj      53: #include <string.h>
1.5       art        54: #include <unistd.h>
1.42      ericj      55: #include <fcntl.h>
1.85      millert    56: #include <limits.h>
1.79      avsm       57: #include "atomicio.h"
1.51      vincent    58:
                     59: #ifndef SUN_LEN
                     60: #define SUN_LEN(su) \
                     61:        (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
                     62: #endif
1.1       deraadt    63:
1.55      fgsch      64: #define PORT_MAX       65535
                     65: #define PORT_MAX_LEN   6
1.99      jeremy     66: #define UNIX_DG_TMP_SOCKET_SIZE        19
1.31      ericj      67:
1.21      ericj      68: /* Command Line Options */
1.68      tedu       69: int    dflag;                                  /* detached, no stdin */
1.113     djm        70: int    Fflag;                                  /* fdpass sock to stdout */
1.88      ray        71: unsigned int iflag;                            /* Interval Flag */
1.21      ericj      72: int    kflag;                                  /* More than one connect */
                     73: int    lflag;                                  /* Bind to local port */
1.111     sthen      74: int    Nflag;                                  /* shutdown() network socket */
1.67      jmc        75: int    nflag;                                  /* Don't do name look up */
1.86      djm        76: char   *Pflag;                                 /* Proxy username */
1.21      ericj      77: char   *pflag;                                 /* Localport flag */
                     78: int    rflag;                                  /* Random ports flag */
                     79: char   *sflag;                                 /* Source Address */
                     80: int    tflag;                                  /* Telnet Emulation */
                     81: int    uflag;                                  /* UDP - Default to TCP */
                     82: int    vflag;                                  /* Verbosity */
1.34      jakob      83: int    xflag;                                  /* Socks proxy */
1.21      ericj      84: int    zflag;                                  /* Port Scan Flag */
1.73      markus     85: int    Dflag;                                  /* sodebug */
1.90      djm        86: int    Iflag;                                  /* TCP receive buffer size */
                     87: int    Oflag;                                  /* TCP send buffer size */
1.65      markus     88: int    Sflag;                                  /* TCP MD5 signature option */
1.83      dtucker    89: int    Tflag = -1;                             /* IP Type of Service */
1.117   ! sthen      90: int    rtableid = -1;
1.21      ericj      91:
1.49      hugh       92: int timeout = -1;
1.21      ericj      93: int family = AF_UNSPEC;
1.63      miod       94: char *portlist[PORT_MAX+1];
1.99      jeremy     95: char *unix_dg_tmp_socket;
1.21      ericj      96:
1.40      millert    97: void   atelnet(int, unsigned char *, unsigned int);
                     98: void   build_ports(char *);
                     99: void   help(void);
                    100: int    local_listen(char *, char *, struct addrinfo);
                    101: void   readwrite(int);
1.113     djm       102: void   fdpass(int nfd) __attribute__((noreturn));
1.77      otto      103: int    remote_connect(const char *, const char *, struct addrinfo);
1.103     fgsch     104: int    timeout_connect(int, const struct sockaddr *, socklen_t);
1.86      djm       105: int    socks_connect(const char *, const char *, struct addrinfo,
                    106:            const char *, const char *, struct addrinfo, int, const char *);
1.40      millert   107: int    udptest(int);
1.99      jeremy    108: int    unix_bind(char *);
1.42      ericj     109: int    unix_connect(char *);
                    110: int    unix_listen(char *);
1.84      dtucker   111: void   set_common_sockopts(int);
1.102     haesbaer  112: int    map_tos(char *, int *);
1.108     haesbaer  113: void   report_connect(const struct sockaddr *, socklen_t);
1.40      millert   114: void   usage(int);
1.1       deraadt   115:
1.21      ericj     116: int
1.37      jakob     117: main(int argc, char *argv[])
1.21      ericj     118: {
1.46      markus    119:        int ch, s, ret, socksv;
1.88      ray       120:        char *host, *uport;
1.21      ericj     121:        struct addrinfo hints;
1.29      smart     122:        struct servent *sv;
1.21      ericj     123:        socklen_t len;
1.76      hshoexer  124:        struct sockaddr_storage cliaddr;
1.34      jakob     125:        char *proxy;
1.88      ray       126:        const char *errstr, *proxyhost = "", *proxyport = NULL;
1.34      jakob     127:        struct addrinfo proxyhints;
1.99      jeremy    128:        char unix_dg_tmp_socket_buf[UNIX_DG_TMP_SOCKET_SIZE];
1.11      ericj     129:
1.29      smart     130:        ret = 1;
                    131:        s = 0;
1.46      markus    132:        socksv = 5;
1.29      smart     133:        host = NULL;
                    134:        uport = NULL;
                    135:        sv = NULL;
1.116     phessler  136:        rtableid = getrtable();
1.29      smart     137:
1.80      mcbride   138:        while ((ch = getopt(argc, argv,
1.113     djm       139:            "46DdFhI:i:klNnO:P:p:rSs:tT:UuV:vw:X:x:z")) != -1) {
1.21      ericj     140:                switch (ch) {
                    141:                case '4':
                    142:                        family = AF_INET;
                    143:                        break;
                    144:                case '6':
                    145:                        family = AF_INET6;
                    146:                        break;
1.42      ericj     147:                case 'U':
                    148:                        family = AF_UNIX;
                    149:                        break;
1.46      markus    150:                case 'X':
1.75      djm       151:                        if (strcasecmp(optarg, "connect") == 0)
                    152:                                socksv = -1; /* HTTP proxy CONNECT */
                    153:                        else if (strcmp(optarg, "4") == 0)
                    154:                                socksv = 4; /* SOCKS v.4 */
                    155:                        else if (strcmp(optarg, "5") == 0)
                    156:                                socksv = 5; /* SOCKS v.5 */
                    157:                        else
                    158:                                errx(1, "unsupported proxy protocol");
1.46      markus    159:                        break;
1.68      tedu      160:                case 'd':
                    161:                        dflag = 1;
                    162:                        break;
1.113     djm       163:                case 'F':
                    164:                        Fflag = 1;
                    165:                        break;
1.21      ericj     166:                case 'h':
                    167:                        help();
                    168:                        break;
                    169:                case 'i':
1.88      ray       170:                        iflag = strtonum(optarg, 0, UINT_MAX, &errstr);
                    171:                        if (errstr)
                    172:                                errx(1, "interval %s: %s", errstr, optarg);
1.21      ericj     173:                        break;
                    174:                case 'k':
                    175:                        kflag = 1;
                    176:                        break;
                    177:                case 'l':
                    178:                        lflag = 1;
                    179:                        break;
1.111     sthen     180:                case 'N':
                    181:                        Nflag = 1;
                    182:                        break;
1.21      ericj     183:                case 'n':
                    184:                        nflag = 1;
                    185:                        break;
1.86      djm       186:                case 'P':
                    187:                        Pflag = optarg;
                    188:                        break;
1.21      ericj     189:                case 'p':
                    190:                        pflag = optarg;
                    191:                        break;
                    192:                case 'r':
                    193:                        rflag = 1;
                    194:                        break;
                    195:                case 's':
                    196:                        sflag = optarg;
                    197:                        break;
                    198:                case 't':
                    199:                        tflag = 1;
                    200:                        break;
                    201:                case 'u':
                    202:                        uflag = 1;
                    203:                        break;
1.93      claudio   204:                case 'V':
1.117   ! sthen     205:                        rtableid = (int)strtonum(optarg, 0,
1.93      claudio   206:                            RT_TABLEID_MAX, &errstr);
                    207:                        if (errstr)
1.98      guenther  208:                                errx(1, "rtable %s: %s", errstr, optarg);
1.93      claudio   209:                        break;
1.21      ericj     210:                case 'v':
                    211:                        vflag = 1;
                    212:                        break;
1.70      deraadt   213:                case 'w':
1.88      ray       214:                        timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
                    215:                        if (errstr)
                    216:                                errx(1, "timeout %s: %s", errstr, optarg);
1.49      hugh      217:                        timeout *= 1000;
1.21      ericj     218:                        break;
1.34      jakob     219:                case 'x':
                    220:                        xflag = 1;
1.64      deraadt   221:                        if ((proxy = strdup(optarg)) == NULL)
                    222:                                err(1, NULL);
1.34      jakob     223:                        break;
1.21      ericj     224:                case 'z':
                    225:                        zflag = 1;
                    226:                        break;
1.73      markus    227:                case 'D':
                    228:                        Dflag = 1;
                    229:                        break;
1.90      djm       230:                case 'I':
                    231:                        Iflag = strtonum(optarg, 1, 65536 << 14, &errstr);
                    232:                        if (errstr != NULL)
                    233:                                errx(1, "TCP receive window %s: %s",
                    234:                                    errstr, optarg);
                    235:                        break;
                    236:                case 'O':
                    237:                        Oflag = strtonum(optarg, 1, 65536 << 14, &errstr);
                    238:                        if (errstr != NULL)
                    239:                                errx(1, "TCP send window %s: %s",
                    240:                                    errstr, optarg);
                    241:                        break;
1.65      markus    242:                case 'S':
                    243:                        Sflag = 1;
                    244:                        break;
1.83      dtucker   245:                case 'T':
1.102     haesbaer  246:                        errstr = NULL;
                    247:                        errno = 0;
                    248:                        if (map_tos(optarg, &Tflag))
                    249:                                break;
                    250:                        if (strlen(optarg) > 1 && optarg[0] == '0' &&
                    251:                            optarg[1] == 'x')
                    252:                                Tflag = (int)strtol(optarg, NULL, 16);
                    253:                        else
                    254:                                Tflag = (int)strtonum(optarg, 0, 255,
                    255:                                    &errstr);
                    256:                        if (Tflag < 0 || Tflag > 255 || errstr || errno)
                    257:                                errx(1, "illegal tos value %s", optarg);
1.83      dtucker   258:                        break;
1.21      ericj     259:                default:
                    260:                        usage(1);
                    261:                }
                    262:        }
                    263:        argc -= optind;
                    264:        argv += optind;
1.11      ericj     265:
1.21      ericj     266:        /* Cruft to make sure options are clean, and used properly. */
1.42      ericj     267:        if (argv[0] && !argv[1] && family == AF_UNIX) {
                    268:                host = argv[0];
                    269:                uport = NULL;
                    270:        } else if (argv[0] && !argv[1]) {
1.21      ericj     271:                if  (!lflag)
                    272:                        usage(1);
                    273:                uport = argv[0];
                    274:                host = NULL;
                    275:        } else if (argv[0] && argv[1]) {
                    276:                host = argv[0];
                    277:                uport = argv[1];
                    278:        } else
                    279:                usage(1);
1.1       deraadt   280:
1.21      ericj     281:        if (lflag && sflag)
                    282:                errx(1, "cannot use -s and -l");
                    283:        if (lflag && pflag)
                    284:                errx(1, "cannot use -p and -l");
                    285:        if (lflag && zflag)
1.32      ericj     286:                errx(1, "cannot use -z and -l");
1.21      ericj     287:        if (!lflag && kflag)
1.32      ericj     288:                errx(1, "must use -l with -k");
1.21      ericj     289:
1.99      jeremy    290:        /* Get name of temporary socket for unix datagram client */
                    291:        if ((family == AF_UNIX) && uflag && !lflag) {
                    292:                if (sflag) {
                    293:                        unix_dg_tmp_socket = sflag;
                    294:                } else {
                    295:                        strlcpy(unix_dg_tmp_socket_buf, "/tmp/nc.XXXXXXXXXX",
                    296:                                UNIX_DG_TMP_SOCKET_SIZE);
                    297:                        if (mktemp(unix_dg_tmp_socket_buf) == NULL)
                    298:                                err(1, "mktemp");
                    299:                        unix_dg_tmp_socket = unix_dg_tmp_socket_buf;
                    300:                }
                    301:        }
                    302:
1.67      jmc       303:        /* Initialize addrinfo structure. */
1.42      ericj     304:        if (family != AF_UNIX) {
                    305:                memset(&hints, 0, sizeof(struct addrinfo));
                    306:                hints.ai_family = family;
                    307:                hints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
                    308:                hints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
                    309:                if (nflag)
                    310:                        hints.ai_flags |= AI_NUMERICHOST;
                    311:        }
1.1       deraadt   312:
1.34      jakob     313:        if (xflag) {
                    314:                if (uflag)
                    315:                        errx(1, "no proxy support for UDP mode");
                    316:
                    317:                if (lflag)
                    318:                        errx(1, "no proxy support for listen");
                    319:
1.42      ericj     320:                if (family == AF_UNIX)
                    321:                        errx(1, "no proxy support for unix sockets");
                    322:
1.34      jakob     323:                /* XXX IPv6 transport to proxy would probably work */
                    324:                if (family == AF_INET6)
                    325:                        errx(1, "no proxy support for IPv6");
                    326:
                    327:                if (sflag)
                    328:                        errx(1, "no proxy support for local source address");
                    329:
                    330:                proxyhost = strsep(&proxy, ":");
                    331:                proxyport = proxy;
                    332:
                    333:                memset(&proxyhints, 0, sizeof(struct addrinfo));
                    334:                proxyhints.ai_family = family;
                    335:                proxyhints.ai_socktype = SOCK_STREAM;
                    336:                proxyhints.ai_protocol = IPPROTO_TCP;
                    337:                if (nflag)
                    338:                        proxyhints.ai_flags |= AI_NUMERICHOST;
                    339:        }
                    340:
1.21      ericj     341:        if (lflag) {
                    342:                int connfd;
1.27      ericj     343:                ret = 0;
1.1       deraadt   344:
1.99      jeremy    345:                if (family == AF_UNIX) {
                    346:                        if (uflag)
                    347:                                s = unix_bind(host);
                    348:                        else
                    349:                                s = unix_listen(host);
                    350:                }
1.42      ericj     351:
1.67      jmc       352:                /* Allow only one connection at a time, but stay alive. */
1.21      ericj     353:                for (;;) {
1.42      ericj     354:                        if (family != AF_UNIX)
                    355:                                s = local_listen(host, uport, hints);
                    356:                        if (s < 0)
1.30      smart     357:                                err(1, NULL);
1.21      ericj     358:                        /*
1.109     haesbaer  359:                         * For UDP and -k, don't connect the socket, let it
                    360:                         * receive datagrams from multiple socket pairs.
1.21      ericj     361:                         */
1.109     haesbaer  362:                        if (uflag && kflag)
                    363:                                readwrite(s);
                    364:                        /*
                    365:                         * For UDP and not -k, we will use recvfrom() initially
                    366:                         * to wait for a caller, then use the regular functions
                    367:                         * to talk to the caller.
                    368:                         */
                    369:                        else if (uflag && !kflag) {
1.80      mcbride   370:                                int rv, plen;
1.97      nicm      371:                                char buf[16384];
1.21      ericj     372:                                struct sockaddr_storage z;
                    373:
                    374:                                len = sizeof(z);
1.106     dlg       375:                                plen = 2048;
1.80      mcbride   376:                                rv = recvfrom(s, buf, plen, MSG_PEEK,
1.37      jakob     377:                                    (struct sockaddr *)&z, &len);
1.23      ericj     378:                                if (rv < 0)
1.57      stevesk   379:                                        err(1, "recvfrom");
1.21      ericj     380:
1.37      jakob     381:                                rv = connect(s, (struct sockaddr *)&z, len);
1.23      ericj     382:                                if (rv < 0)
1.57      stevesk   383:                                        err(1, "connect");
1.1       deraadt   384:
1.108     haesbaer  385:                                if (vflag)
                    386:                                        report_connect((struct sockaddr *)&z, len);
                    387:
1.99      jeremy    388:                                readwrite(s);
1.21      ericj     389:                        } else {
1.78      otto      390:                                len = sizeof(cliaddr);
1.21      ericj     391:                                connfd = accept(s, (struct sockaddr *)&cliaddr,
1.37      jakob     392:                                    &len);
1.110     deraadt   393:                                if (connfd == -1) {
                    394:                                        /* For now, all errnos are fatal */
                    395:                                        err(1, "accept");
                    396:                                }
1.108     haesbaer  397:                                if (vflag)
                    398:                                        report_connect((struct sockaddr *)&cliaddr, len);
                    399:
1.99      jeremy    400:                                readwrite(connfd);
                    401:                                close(connfd);
1.21      ericj     402:                        }
1.1       deraadt   403:
1.42      ericj     404:                        if (family != AF_UNIX)
                    405:                                close(s);
1.99      jeremy    406:                        else if (uflag) {
                    407:                                if (connect(s, NULL, 0) < 0)
                    408:                                        err(1, "connect");
                    409:                        }
1.27      ericj     410:
1.21      ericj     411:                        if (!kflag)
                    412:                                break;
1.11      ericj     413:                }
1.42      ericj     414:        } else if (family == AF_UNIX) {
                    415:                ret = 0;
                    416:
                    417:                if ((s = unix_connect(host)) > 0 && !zflag) {
                    418:                        readwrite(s);
                    419:                        close(s);
                    420:                } else
                    421:                        ret = 1;
                    422:
1.99      jeremy    423:                if (uflag)
                    424:                        unlink(unix_dg_tmp_socket);
1.42      ericj     425:                exit(ret);
                    426:
1.21      ericj     427:        } else {
                    428:                int i = 0;
1.6       deraadt   429:
1.67      jmc       430:                /* Construct the portlist[] array. */
1.21      ericj     431:                build_ports(uport);
1.1       deraadt   432:
1.67      jmc       433:                /* Cycle through portlist, connecting to each port. */
1.21      ericj     434:                for (i = 0; portlist[i] != NULL; i++) {
                    435:                        if (s)
                    436:                                close(s);
1.34      jakob     437:
                    438:                        if (xflag)
                    439:                                s = socks_connect(host, portlist[i], hints,
1.86      djm       440:                                    proxyhost, proxyport, proxyhints, socksv,
                    441:                                    Pflag);
1.34      jakob     442:                        else
                    443:                                s = remote_connect(host, portlist[i], hints);
                    444:
                    445:                        if (s < 0)
1.21      ericj     446:                                continue;
1.1       deraadt   447:
1.21      ericj     448:                        ret = 0;
                    449:                        if (vflag || zflag) {
1.67      jmc       450:                                /* For UDP, make sure we are connected. */
1.21      ericj     451:                                if (uflag) {
1.50      vincent   452:                                        if (udptest(s) == -1) {
1.21      ericj     453:                                                ret = 1;
                    454:                                                continue;
                    455:                                        }
                    456:                                }
1.1       deraadt   457:
1.67      jmc       458:                                /* Don't look up port if -n. */
1.21      ericj     459:                                if (nflag)
                    460:                                        sv = NULL;
                    461:                                else {
                    462:                                        sv = getservbyport(
1.37      jakob     463:                                            ntohs(atoi(portlist[i])),
                    464:                                            uflag ? "udp" : "tcp");
1.21      ericj     465:                                }
1.50      vincent   466:
1.94      mpf       467:                                fprintf(stderr,
                    468:                                    "Connection to %s %s port [%s/%s] "
                    469:                                    "succeeded!\n", host, portlist[i],
                    470:                                    uflag ? "udp" : "tcp",
1.37      jakob     471:                                    sv ? sv->s_name : "*");
1.21      ericj     472:                        }
1.113     djm       473:                        if (Fflag)
                    474:                                fdpass(s);
                    475:                        else if (!zflag)
1.21      ericj     476:                                readwrite(s);
1.7       deraadt   477:                }
1.11      ericj     478:        }
1.1       deraadt   479:
1.21      ericj     480:        if (s)
                    481:                close(s);
                    482:
                    483:        exit(ret);
1.7       deraadt   484: }
1.1       deraadt   485:
1.11      ericj     486: /*
1.99      jeremy    487:  * unix_bind()
                    488:  * Returns a unix socket bound to the given path
1.42      ericj     489:  */
                    490: int
1.99      jeremy    491: unix_bind(char *path)
1.42      ericj     492: {
                    493:        struct sockaddr_un sun;
                    494:        int s;
                    495:
1.99      jeremy    496:        /* Create unix domain socket. */
                    497:        if ((s = socket(AF_UNIX, uflag ? SOCK_DGRAM : SOCK_STREAM,
                    498:             0)) < 0)
1.50      vincent   499:                return (-1);
1.42      ericj     500:
                    501:        memset(&sun, 0, sizeof(struct sockaddr_un));
                    502:        sun.sun_family = AF_UNIX;
1.60      avsm      503:
                    504:        if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
                    505:            sizeof(sun.sun_path)) {
                    506:                close(s);
                    507:                errno = ENAMETOOLONG;
                    508:                return (-1);
                    509:        }
1.99      jeremy    510:
                    511:        if (bind(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
1.50      vincent   512:                close(s);
                    513:                return (-1);
1.42      ericj     514:        }
                    515:        return (s);
                    516: }
                    517:
                    518: /*
1.99      jeremy    519:  * unix_connect()
                    520:  * Returns a socket connected to a local unix socket. Returns -1 on failure.
1.42      ericj     521:  */
                    522: int
1.99      jeremy    523: unix_connect(char *path)
1.42      ericj     524: {
                    525:        struct sockaddr_un sun;
                    526:        int s;
                    527:
1.99      jeremy    528:        if (uflag) {
                    529:                if ((s = unix_bind(unix_dg_tmp_socket)) < 0)
                    530:                        return (-1);
                    531:        } else {
                    532:                if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
                    533:                        return (-1);
                    534:        }
1.112     okan      535:        (void)fcntl(s, F_SETFD, FD_CLOEXEC);
1.42      ericj     536:
1.60      avsm      537:        memset(&sun, 0, sizeof(struct sockaddr_un));
1.42      ericj     538:        sun.sun_family = AF_UNIX;
1.60      avsm      539:
                    540:        if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
                    541:            sizeof(sun.sun_path)) {
                    542:                close(s);
                    543:                errno = ENAMETOOLONG;
                    544:                return (-1);
                    545:        }
1.99      jeremy    546:        if (connect(s, (struct sockaddr *)&sun, SUN_LEN(&sun)) < 0) {
1.42      ericj     547:                close(s);
                    548:                return (-1);
                    549:        }
1.99      jeremy    550:        return (s);
                    551:
                    552: }
                    553:
                    554: /*
                    555:  * unix_listen()
                    556:  * Create a unix domain socket, and listen on it.
                    557:  */
                    558: int
                    559: unix_listen(char *path)
                    560: {
                    561:        int s;
                    562:        if ((s = unix_bind(path)) < 0)
                    563:                return (-1);
1.42      ericj     564:
                    565:        if (listen(s, 5) < 0) {
                    566:                close(s);
                    567:                return (-1);
                    568:        }
                    569:        return (s);
                    570: }
                    571:
                    572: /*
1.21      ericj     573:  * remote_connect()
1.67      jmc       574:  * Returns a socket connected to a remote host. Properly binds to a local
                    575:  * port or source address if needed. Returns -1 on failure.
1.11      ericj     576:  */
1.21      ericj     577: int
1.77      otto      578: remote_connect(const char *host, const char *port, struct addrinfo hints)
1.21      ericj     579: {
                    580:        struct addrinfo *res, *res0;
1.91      markus    581:        int s, error, on = 1;
1.21      ericj     582:
                    583:        if ((error = getaddrinfo(host, port, &hints, &res)))
1.56      stevesk   584:                errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21      ericj     585:
                    586:        res0 = res;
                    587:        do {
                    588:                if ((s = socket(res0->ai_family, res0->ai_socktype,
1.37      jakob     589:                    res0->ai_protocol)) < 0)
1.21      ericj     590:                        continue;
                    591:
1.117   ! sthen     592:                if (rtableid >= 0 && (setsockopt(s, SOL_SOCKET, SO_RTABLE,
        !           593:                    &rtableid, sizeof(rtableid)) == -1))
1.115     phessler  594:                        err(1, "setsockopt SO_RTABLE");
1.93      claudio   595:
1.67      jmc       596:                /* Bind to a local port or source address if specified. */
1.21      ericj     597:                if (sflag || pflag) {
                    598:                        struct addrinfo ahints, *ares;
1.6       deraadt   599:
1.91      markus    600:                        /* try SO_BINDANY, but don't insist */
                    601:                        setsockopt(s, SOL_SOCKET, SO_BINDANY, &on, sizeof(on));
1.21      ericj     602:                        memset(&ahints, 0, sizeof(struct addrinfo));
                    603:                        ahints.ai_family = res0->ai_family;
                    604:                        ahints.ai_socktype = uflag ? SOCK_DGRAM : SOCK_STREAM;
                    605:                        ahints.ai_protocol = uflag ? IPPROTO_UDP : IPPROTO_TCP;
1.25      ericj     606:                        ahints.ai_flags = AI_PASSIVE;
1.38      jakob     607:                        if ((error = getaddrinfo(sflag, pflag, &ahints, &ares)))
1.56      stevesk   608:                                errx(1, "getaddrinfo: %s", gai_strerror(error));
1.21      ericj     609:
                    610:                        if (bind(s, (struct sockaddr *)ares->ai_addr,
1.62      millert   611:                            ares->ai_addrlen) < 0)
1.21      ericj     612:                                errx(1, "bind failed: %s", strerror(errno));
                    613:                        freeaddrinfo(ares);
1.6       deraadt   614:                }
1.81      marius    615:
                    616:                set_common_sockopts(s);
1.6       deraadt   617:
1.103     fgsch     618:                if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
1.6       deraadt   619:                        break;
1.71      mcbride   620:                else if (vflag)
                    621:                        warn("connect to %s port %s (%s) failed", host, port,
                    622:                            uflag ? "udp" : "tcp");
1.34      jakob     623:
1.21      ericj     624:                close(s);
                    625:                s = -1;
                    626:        } while ((res0 = res0->ai_next) != NULL);
                    627:
                    628:        freeaddrinfo(res);
1.1       deraadt   629:
1.21      ericj     630:        return (s);
1.103     fgsch     631: }
                    632:
                    633: int
                    634: timeout_connect(int s, const struct sockaddr *name, socklen_t namelen)
                    635: {
                    636:        struct pollfd pfd;
                    637:        socklen_t optlen;
                    638:        int flags, optval;
                    639:        int ret;
                    640:
                    641:        if (timeout != -1) {
                    642:                flags = fcntl(s, F_GETFL, 0);
                    643:                if (fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
                    644:                        err(1, "set non-blocking mode");
                    645:        }
                    646:
                    647:        if ((ret = connect(s, name, namelen)) != 0 && errno == EINPROGRESS) {
                    648:                pfd.fd = s;
                    649:                pfd.events = POLLOUT;
                    650:                if ((ret = poll(&pfd, 1, timeout)) == 1) {
                    651:                        optlen = sizeof(optval);
                    652:                        if ((ret = getsockopt(s, SOL_SOCKET, SO_ERROR,
                    653:                            &optval, &optlen)) == 0) {
                    654:                                errno = optval;
                    655:                                ret = optval == 0 ? 0 : -1;
                    656:                        }
                    657:                } else if (ret == 0) {
                    658:                        errno = ETIMEDOUT;
                    659:                        ret = -1;
                    660:                } else
                    661:                        err(1, "poll failed");
                    662:        }
                    663:
                    664:        if (timeout != -1 && fcntl(s, F_SETFL, flags) == -1)
                    665:                err(1, "restoring flags");
                    666:
                    667:        return (ret);
1.7       deraadt   668: }
1.1       deraadt   669:
1.11      ericj     670: /*
1.21      ericj     671:  * local_listen()
1.67      jmc       672:  * Returns a socket listening on a local port, binds to specified source
                    673:  * address. Returns -1 on failure.
1.11      ericj     674:  */
1.21      ericj     675: int
1.37      jakob     676: local_listen(char *host, char *port, struct addrinfo hints)
1.21      ericj     677: {
                    678:        struct addrinfo *res, *res0;
                    679:        int s, ret, x = 1;
                    680:        int error;
1.6       deraadt   681:
1.67      jmc       682:        /* Allow nodename to be null. */
1.21      ericj     683:        hints.ai_flags |= AI_PASSIVE;
1.7       deraadt   684:
1.21      ericj     685:        /*
                    686:         * In the case of binding to a wildcard address
                    687:         * default to binding to an ipv4 address.
                    688:         */
                    689:        if (host == NULL && hints.ai_family == AF_UNSPEC)
                    690:                hints.ai_family = AF_INET;
1.1       deraadt   691:
1.21      ericj     692:        if ((error = getaddrinfo(host, port, &hints, &res)))
1.70      deraadt   693:                errx(1, "getaddrinfo: %s", gai_strerror(error));
1.14      ericj     694:
1.21      ericj     695:        res0 = res;
                    696:        do {
                    697:                if ((s = socket(res0->ai_family, res0->ai_socktype,
1.82      marius    698:                    res0->ai_protocol)) < 0)
1.21      ericj     699:                        continue;
1.1       deraadt   700:
1.117   ! sthen     701:                if (rtableid >= 0 && (setsockopt(s, IPPROTO_IP, SO_RTABLE,
        !           702:                    &rtableid, sizeof(rtableid)) == -1))
1.115     phessler  703:                        err(1, "setsockopt SO_RTABLE");
1.93      claudio   704:
1.21      ericj     705:                ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof(x));
                    706:                if (ret == -1)
1.30      smart     707:                        err(1, NULL);
1.81      marius    708:
                    709:                set_common_sockopts(s);
1.1       deraadt   710:
1.21      ericj     711:                if (bind(s, (struct sockaddr *)res0->ai_addr,
1.37      jakob     712:                    res0->ai_addrlen) == 0)
1.21      ericj     713:                        break;
1.1       deraadt   714:
1.21      ericj     715:                close(s);
                    716:                s = -1;
                    717:        } while ((res0 = res0->ai_next) != NULL);
1.1       deraadt   718:
1.47      ericj     719:        if (!uflag && s != -1) {
1.21      ericj     720:                if (listen(s, 1) < 0)
1.57      stevesk   721:                        err(1, "listen");
1.12      ericj     722:        }
1.1       deraadt   723:
1.21      ericj     724:        freeaddrinfo(res);
1.1       deraadt   725:
1.21      ericj     726:        return (s);
1.7       deraadt   727: }
                    728:
1.11      ericj     729: /*
1.21      ericj     730:  * readwrite()
                    731:  * Loop that polls on the network file descriptor and stdin.
1.11      ericj     732:  */
1.21      ericj     733: void
1.37      jakob     734: readwrite(int nfd)
1.6       deraadt   735: {
1.52      vincent   736:        struct pollfd pfd[2];
1.97      nicm      737:        unsigned char buf[16384];
1.79      avsm      738:        int n, wfd = fileno(stdin);
1.21      ericj     739:        int lfd = fileno(stdout);
1.80      mcbride   740:        int plen;
                    741:
1.106     dlg       742:        plen = 2048;
1.21      ericj     743:
                    744:        /* Setup Network FD */
                    745:        pfd[0].fd = nfd;
                    746:        pfd[0].events = POLLIN;
                    747:
1.67      jmc       748:        /* Set up STDIN FD. */
1.21      ericj     749:        pfd[1].fd = wfd;
                    750:        pfd[1].events = POLLIN;
                    751:
1.54      aaron     752:        while (pfd[0].fd != -1) {
1.21      ericj     753:                if (iflag)
                    754:                        sleep(iflag);
                    755:
1.68      tedu      756:                if ((n = poll(pfd, 2 - dflag, timeout)) < 0) {
1.21      ericj     757:                        close(nfd);
1.52      vincent   758:                        err(1, "Polling Error");
1.21      ericj     759:                }
1.49      hugh      760:
                    761:                if (n == 0)
                    762:                        return;
1.21      ericj     763:
                    764:                if (pfd[0].revents & POLLIN) {
1.80      mcbride   765:                        if ((n = read(nfd, buf, plen)) < 0)
1.21      ericj     766:                                return;
1.52      vincent   767:                        else if (n == 0) {
                    768:                                shutdown(nfd, SHUT_RD);
                    769:                                pfd[0].fd = -1;
                    770:                                pfd[0].events = 0;
1.21      ericj     771:                        } else {
                    772:                                if (tflag)
                    773:                                        atelnet(nfd, buf, n);
1.79      avsm      774:                                if (atomicio(vwrite, lfd, buf, n) != n)
1.21      ericj     775:                                        return;
1.6       deraadt   776:                        }
1.21      ericj     777:                }
                    778:
1.68      tedu      779:                if (!dflag && pfd[1].revents & POLLIN) {
1.80      mcbride   780:                        if ((n = read(wfd, buf, plen)) < 0)
1.21      ericj     781:                                return;
1.52      vincent   782:                        else if (n == 0) {
1.111     sthen     783:                                if (Nflag)
                    784:                                        shutdown(nfd, SHUT_WR);
1.52      vincent   785:                                pfd[1].fd = -1;
                    786:                                pfd[1].events = 0;
                    787:                        } else {
1.79      avsm      788:                                if (atomicio(vwrite, nfd, buf, n) != n)
1.21      ericj     789:                                        return;
1.50      vincent   790:                        }
1.21      ericj     791:                }
1.11      ericj     792:        }
1.113     djm       793: }
                    794:
                    795: /*
                    796:  * fdpass()
                    797:  * Pass the connected file descriptor to stdout and exit.
                    798:  */
                    799: void
                    800: fdpass(int nfd)
                    801: {
                    802:        struct msghdr mh;
                    803:        union {
                    804:                struct cmsghdr hdr;
                    805:                char buf[CMSG_SPACE(sizeof(int))];
                    806:        } cmsgbuf;
                    807:        struct cmsghdr *cmsg;
                    808:        struct iovec iov;
                    809:        char c = '\0';
                    810:        ssize_t r;
                    811:        struct pollfd pfd;
                    812:
                    813:        /* Avoid obvious stupidity */
                    814:        if (isatty(STDOUT_FILENO))
                    815:                errx(1, "Cannot pass file descriptor to tty");
                    816:
                    817:        bzero(&mh, sizeof(mh));
                    818:        bzero(&cmsgbuf, sizeof(cmsgbuf));
                    819:        bzero(&iov, sizeof(iov));
                    820:        bzero(&pfd, sizeof(pfd));
                    821:
                    822:        mh.msg_control = (caddr_t)&cmsgbuf.buf;
                    823:        mh.msg_controllen = sizeof(cmsgbuf.buf);
                    824:        cmsg = CMSG_FIRSTHDR(&mh);
                    825:        cmsg->cmsg_len = CMSG_LEN(sizeof(int));
                    826:        cmsg->cmsg_level = SOL_SOCKET;
                    827:        cmsg->cmsg_type = SCM_RIGHTS;
                    828:        *(int *)CMSG_DATA(cmsg) = nfd;
                    829:
                    830:        iov.iov_base = &c;
                    831:        iov.iov_len = 1;
                    832:        mh.msg_iov = &iov;
                    833:        mh.msg_iovlen = 1;
                    834:
                    835:        bzero(&pfd, sizeof(pfd));
                    836:        pfd.fd = STDOUT_FILENO;
                    837:        for (;;) {
                    838:                r = sendmsg(STDOUT_FILENO, &mh, 0);
                    839:                if (r == -1) {
                    840:                        if (errno == EAGAIN || errno == EINTR) {
                    841:                                pfd.events = POLLOUT;
                    842:                                if (poll(&pfd, 1, -1) == -1)
                    843:                                        err(1, "poll");
                    844:                                continue;
                    845:                        }
                    846:                        err(1, "sendmsg");
                    847:                } else if (r == -1)
                    848:                        errx(1, "sendmsg: unexpected return value %zd", r);
                    849:                else
                    850:                        break;
                    851:        }
                    852:        exit(0);
1.7       deraadt   853: }
1.50      vincent   854:
1.67      jmc       855: /* Deal with RFC 854 WILL/WONT DO/DONT negotiation. */
1.21      ericj     856: void
1.37      jakob     857: atelnet(int nfd, unsigned char *buf, unsigned int size)
1.6       deraadt   858: {
1.24      ericj     859:        unsigned char *p, *end;
                    860:        unsigned char obuf[4];
                    861:
1.95      nicm      862:        if (size < 3)
                    863:                return;
                    864:        end = buf + size - 2;
1.24      ericj     865:
                    866:        for (p = buf; p < end; p++) {
1.21      ericj     867:                if (*p != IAC)
1.95      nicm      868:                        continue;
1.24      ericj     869:
1.25      ericj     870:                obuf[0] = IAC;
1.24      ericj     871:                p++;
1.50      vincent   872:                if ((*p == WILL) || (*p == WONT))
1.24      ericj     873:                        obuf[1] = DONT;
1.95      nicm      874:                else if ((*p == DO) || (*p == DONT))
1.24      ericj     875:                        obuf[1] = WONT;
1.95      nicm      876:                else
                    877:                        continue;
                    878:
                    879:                p++;
                    880:                obuf[2] = *p;
                    881:                if (atomicio(vwrite, nfd, obuf, 3) != 3)
                    882:                        warn("Write Error!");
1.11      ericj     883:        }
1.7       deraadt   884: }
                    885:
1.11      ericj     886: /*
1.21      ericj     887:  * build_ports()
1.105     lum       888:  * Build an array of ports in portlist[], listing each port
1.67      jmc       889:  * that we should try to connect to.
1.11      ericj     890:  */
1.21      ericj     891: void
1.37      jakob     892: build_ports(char *p)
1.6       deraadt   893: {
1.88      ray       894:        const char *errstr;
                    895:        char *n;
1.21      ericj     896:        int hi, lo, cp;
                    897:        int x = 0;
                    898:
                    899:        if ((n = strchr(p, '-')) != NULL) {
                    900:                *n = '\0';
                    901:                n++;
                    902:
1.67      jmc       903:                /* Make sure the ports are in order: lowest->highest. */
1.88      ray       904:                hi = strtonum(n, 1, PORT_MAX, &errstr);
                    905:                if (errstr)
                    906:                        errx(1, "port number %s: %s", errstr, n);
                    907:                lo = strtonum(p, 1, PORT_MAX, &errstr);
                    908:                if (errstr)
                    909:                        errx(1, "port number %s: %s", errstr, p);
1.21      ericj     910:
                    911:                if (lo > hi) {
                    912:                        cp = hi;
                    913:                        hi = lo;
                    914:                        lo = cp;
                    915:                }
                    916:
1.67      jmc       917:                /* Load ports sequentially. */
1.21      ericj     918:                for (cp = lo; cp <= hi; cp++) {
1.55      fgsch     919:                        portlist[x] = calloc(1, PORT_MAX_LEN);
                    920:                        if (portlist[x] == NULL)
                    921:                                err(1, NULL);
                    922:                        snprintf(portlist[x], PORT_MAX_LEN, "%d", cp);
1.21      ericj     923:                        x++;
                    924:                }
                    925:
1.67      jmc       926:                /* Randomly swap ports. */
1.21      ericj     927:                if (rflag) {
                    928:                        int y;
                    929:                        char *c;
                    930:
                    931:                        for (x = 0; x <= (hi - lo); x++) {
                    932:                                y = (arc4random() & 0xFFFF) % (hi - lo);
                    933:                                c = portlist[x];
                    934:                                portlist[x] = portlist[y];
                    935:                                portlist[y] = c;
1.6       deraadt   936:                        }
1.11      ericj     937:                }
1.21      ericj     938:        } else {
1.88      ray       939:                hi = strtonum(p, 1, PORT_MAX, &errstr);
                    940:                if (errstr)
                    941:                        errx(1, "port number %s: %s", errstr, p);
1.96      nicm      942:                portlist[0] = strdup(p);
1.55      fgsch     943:                if (portlist[0] == NULL)
                    944:                        err(1, NULL);
1.11      ericj     945:        }
1.13      ericj     946: }
                    947:
                    948: /*
1.21      ericj     949:  * udptest()
                    950:  * Do a few writes to see if the UDP port is there.
1.105     lum       951:  * Fails once PF state table is full.
1.13      ericj     952:  */
1.21      ericj     953: int
1.37      jakob     954: udptest(int s)
1.13      ericj     955: {
1.74      deraadt   956:        int i, ret;
1.13      ericj     957:
1.52      vincent   958:        for (i = 0; i <= 3; i++) {
1.74      deraadt   959:                if (write(s, "X", 1) == 1)
1.21      ericj     960:                        ret = 1;
1.14      ericj     961:                else
1.21      ericj     962:                        ret = -1;
1.14      ericj     963:        }
1.21      ericj     964:        return (ret);
1.81      marius    965: }
                    966:
1.84      dtucker   967: void
1.81      marius    968: set_common_sockopts(int s)
                    969: {
                    970:        int x = 1;
                    971:
                    972:        if (Sflag) {
                    973:                if (setsockopt(s, IPPROTO_TCP, TCP_MD5SIG,
                    974:                        &x, sizeof(x)) == -1)
                    975:                        err(1, NULL);
                    976:        }
                    977:        if (Dflag) {
                    978:                if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
                    979:                        &x, sizeof(x)) == -1)
                    980:                        err(1, NULL);
                    981:        }
1.83      dtucker   982:        if (Tflag != -1) {
                    983:                if (setsockopt(s, IPPROTO_IP, IP_TOS,
                    984:                    &Tflag, sizeof(Tflag)) == -1)
                    985:                        err(1, "set IP ToS");
                    986:        }
1.90      djm       987:        if (Iflag) {
                    988:                if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,
                    989:                    &Iflag, sizeof(Iflag)) == -1)
                    990:                        err(1, "set TCP receive buffer size");
                    991:        }
                    992:        if (Oflag) {
                    993:                if (setsockopt(s, SOL_SOCKET, SO_SNDBUF,
                    994:                    &Oflag, sizeof(Oflag)) == -1)
                    995:                        err(1, "set TCP send buffer size");
                    996:        }
1.83      dtucker   997: }
                    998:
                    999: int
1.102     haesbaer 1000: map_tos(char *s, int *val)
1.83      dtucker  1001: {
1.102     haesbaer 1002:        /* DiffServ Codepoints and other TOS mappings */
                   1003:        const struct toskeywords {
                   1004:                const char      *keyword;
                   1005:                int              val;
                   1006:        } *t, toskeywords[] = {
                   1007:                { "af11",               IPTOS_DSCP_AF11 },
                   1008:                { "af12",               IPTOS_DSCP_AF12 },
                   1009:                { "af13",               IPTOS_DSCP_AF13 },
                   1010:                { "af21",               IPTOS_DSCP_AF21 },
                   1011:                { "af22",               IPTOS_DSCP_AF22 },
                   1012:                { "af23",               IPTOS_DSCP_AF23 },
                   1013:                { "af31",               IPTOS_DSCP_AF31 },
                   1014:                { "af32",               IPTOS_DSCP_AF32 },
                   1015:                { "af33",               IPTOS_DSCP_AF33 },
                   1016:                { "af41",               IPTOS_DSCP_AF41 },
                   1017:                { "af42",               IPTOS_DSCP_AF42 },
                   1018:                { "af43",               IPTOS_DSCP_AF43 },
                   1019:                { "critical",           IPTOS_PREC_CRITIC_ECP },
                   1020:                { "cs0",                IPTOS_DSCP_CS0 },
                   1021:                { "cs1",                IPTOS_DSCP_CS1 },
                   1022:                { "cs2",                IPTOS_DSCP_CS2 },
                   1023:                { "cs3",                IPTOS_DSCP_CS3 },
                   1024:                { "cs4",                IPTOS_DSCP_CS4 },
                   1025:                { "cs5",                IPTOS_DSCP_CS5 },
                   1026:                { "cs6",                IPTOS_DSCP_CS6 },
                   1027:                { "cs7",                IPTOS_DSCP_CS7 },
                   1028:                { "ef",                 IPTOS_DSCP_EF },
                   1029:                { "inetcontrol",        IPTOS_PREC_INTERNETCONTROL },
                   1030:                { "lowdelay",           IPTOS_LOWDELAY },
                   1031:                { "netcontrol",         IPTOS_PREC_NETCONTROL },
                   1032:                { "reliability",        IPTOS_RELIABILITY },
                   1033:                { "throughput",         IPTOS_THROUGHPUT },
                   1034:                { NULL,                 -1 },
                   1035:        };
                   1036:
                   1037:        for (t = toskeywords; t->keyword != NULL; t++) {
                   1038:                if (strcmp(s, t->keyword) == 0) {
                   1039:                        *val = t->val;
                   1040:                        return (1);
                   1041:                }
                   1042:        }
1.83      dtucker  1043:
1.102     haesbaer 1044:        return (0);
1.108     haesbaer 1045: }
                   1046:
                   1047: void
                   1048: report_connect(const struct sockaddr *sa, socklen_t salen)
                   1049: {
                   1050:        char remote_host[NI_MAXHOST];
                   1051:        char remote_port[NI_MAXSERV];
                   1052:        int herr;
                   1053:        int flags = NI_NUMERICSERV;
                   1054:
                   1055:        if (nflag)
                   1056:                flags |= NI_NUMERICHOST;
                   1057:
                   1058:        if ((herr = getnameinfo(sa, salen,
                   1059:            remote_host, sizeof(remote_host),
                   1060:            remote_port, sizeof(remote_port),
                   1061:            flags)) != 0) {
                   1062:                if (herr == EAI_SYSTEM)
                   1063:                        err(1, "getnameinfo");
                   1064:                else
                   1065:                        errx(1, "getnameinfo: %s", gai_strerror(herr));
                   1066:        }
                   1067:
                   1068:        fprintf(stderr,
                   1069:            "Connection from %s %s "
                   1070:            "received!\n", remote_host, remote_port);
1.7       deraadt  1071: }
1.1       deraadt  1072:
1.11      ericj    1073: void
1.58      deraadt  1074: help(void)
1.1       deraadt  1075: {
1.21      ericj    1076:        usage(0);
                   1077:        fprintf(stderr, "\tCommand Summary:\n\
                   1078:        \t-4            Use IPv4\n\
                   1079:        \t-6            Use IPv6\n\
1.73      markus   1080:        \t-D            Enable the debug socket option\n\
1.69      tedu     1081:        \t-d            Detach from stdin\n\
1.114     jmc      1082:        \t-F            Pass socket fd\n\
1.21      ericj    1083:        \t-h            This help text\n\
1.90      djm      1084:        \t-I length     TCP receive buffer length\n\
1.21      ericj    1085:        \t-i secs\t     Delay interval for lines sent, ports scanned\n\
                   1086:        \t-k            Keep inbound sockets open for multiple connects\n\
                   1087:        \t-l            Listen mode, for inbound connects\n\
1.111     sthen    1088:        \t-N            Shutdown the network socket after EOF on stdin\n\
1.22      jasoni   1089:        \t-n            Suppress name/port resolutions\n\
1.90      djm      1090:        \t-O length     TCP send buffer length\n\
1.86      djm      1091:        \t-P proxyuser\tUsername for proxy authentication\n\
1.36      jakob    1092:        \t-p port\t     Specify local port for remote connects\n\
1.21      ericj    1093:        \t-r            Randomize remote ports\n\
1.67      jmc      1094:        \t-S            Enable the TCP MD5 signature option\n\
1.21      ericj    1095:        \t-s addr\t     Local source address\n\
1.102     haesbaer 1096:        \t-T toskeyword\tSet IP Type of Service\n\
1.21      ericj    1097:        \t-t            Answer TELNET negotiation\n\
1.67      jmc      1098:        \t-U            Use UNIX domain socket\n\
1.21      ericj    1099:        \t-u            UDP mode\n\
1.98      guenther 1100:        \t-V rtable     Specify alternate routing table\n\
1.21      ericj    1101:        \t-v            Verbose\n\
                   1102:        \t-w secs\t     Timeout for connects and final net reads\n\
1.75      djm      1103:        \t-X proto      Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
                   1104:        \t-x addr[:port]\tSpecify proxy address and port\n\
1.21      ericj    1105:        \t-z            Zero-I/O mode [used for scanning]\n\
                   1106:        Port numbers can be individual or ranges: lo-hi [inclusive]\n");
                   1107:        exit(1);
1.11      ericj    1108: }
                   1109:
                   1110: void
1.37      jakob    1111: usage(int ret)
1.11      ericj    1112: {
1.92      sobrado  1113:        fprintf(stderr,
1.114     jmc      1114:            "usage: nc [-46DdFhklNnrStUuvz] [-I length] [-i interval] [-O length]\n"
1.100     jeremy   1115:            "\t  [-P proxy_username] [-p source_port] [-s source] [-T ToS]\n"
1.98      guenther 1116:            "\t  [-V rtable] [-w timeout] [-X proxy_protocol]\n"
1.100     jeremy   1117:            "\t  [-x proxy_address[:port]] [destination] [port]\n");
1.21      ericj    1118:        if (ret)
                   1119:                exit(1);
1.7       deraadt  1120: }