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

Annotation of src/usr.bin/nc/socks.c, Revision 1.5

1.5     ! markus      1: /*     $OpenBSD: socks.c,v 1.4 2002/02/19 22:42:04 ericj Exp $ */
1.1       jakob       2:
                      3: /*
                      4:  * Copyright (c) 1999 Niklas Hallqvist.  All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      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. All advertising materials mentioning features or use of this software
                     15:  *    must display the following acknowledgement:
                     16:  *     This product includes software developed by Niklas Hallqvist.
                     17:  * 4. The name of the author may not be used to endorse or promote products
                     18:  *    derived from this software without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     21:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     22:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     23:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     24:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     25:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     26:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     27:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     28:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     29:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     30:  */
                     31:
                     32: #include <sys/types.h>
                     33: #include <sys/socket.h>
                     34: #include <netinet/in.h>
                     35: #include <arpa/inet.h>
                     36:
                     37: #include <err.h>
                     38: #include <netdb.h>
                     39: #include <stdio.h>
                     40: #include <stdlib.h>
                     41: #include <string.h>
                     42: #include <unistd.h>
                     43:
                     44: #define SOCKS_PORT     "1080"
1.5     ! markus     45: #define SOCKS_V5       5
        !            46: #define SOCKS_V4       4
1.1       jakob      47: #define SOCKS_NOAUTH   0
                     48: #define SOCKS_NOMETHOD 0xff
                     49: #define SOCKS_CONNECT  1
                     50: #define SOCKS_IPV4     1
                     51: #define SOCKS_MAXCMDSZ 10
1.4       ericj      52:
                     53: int    remote_connect(char *, char *, struct addrinfo);
1.1       jakob      54:
                     55: static in_addr_t
                     56: decode_addr (const char *s)
                     57: {
                     58:        struct hostent *hp = gethostbyname (s);
                     59:        struct in_addr retval;
                     60:
                     61:        if (hp)
                     62:                return *(in_addr_t *)hp->h_addr_list[0];
                     63:        if (inet_aton (s, &retval))
                     64:                return retval.s_addr;
                     65:        errx (1, "cannot decode address \"%s\"", s);
                     66: }
                     67:
                     68: static in_port_t
                     69: decode_port (const char *s)
                     70: {
                     71:        struct servent *sp;
                     72:        in_port_t port;
                     73:        char *p;
                     74:
                     75:        port = strtol (s, &p, 10);
                     76:        if (s == p) {
                     77:                sp = getservbyname (s, "tcp");
                     78:                if (sp)
                     79:                        return sp->s_port;
                     80:        }
                     81:        if (*s != '\0' && *p == '\0')
                     82:                return htons (port);
                     83:        errx (1, "cannot decode port \"%s\"", s);
                     84: }
                     85:
                     86: int
                     87: socks_connect (char *host, char *port, struct addrinfo hints,
1.5     ! markus     88:     char *proxyhost, char *proxyport, struct addrinfo proxyhints,
        !            89:     int socksv)
1.1       jakob      90: {
                     91:        int proxyfd;
                     92:        unsigned char buf[SOCKS_MAXCMDSZ];
                     93:        ssize_t cnt;
                     94:        in_addr_t serveraddr;
                     95:        in_port_t serverport;
                     96:
                     97:        if (proxyport)
                     98:                proxyfd = remote_connect(proxyhost, proxyport, proxyhints);
                     99:        else
                    100:                proxyfd = remote_connect(proxyhost, SOCKS_PORT, proxyhints);
                    101:
                    102:        if (!proxyfd)
                    103:                return -1;
                    104:
                    105:        serveraddr = decode_addr (host);
                    106:        serverport = decode_port (port);
                    107:
1.5     ! markus    108:        if (socksv == 5) {
        !           109:                /* Version 5, one method: no authentication */
        !           110:                buf[0] = SOCKS_V5;
        !           111:                buf[1] = 1;
        !           112:                buf[2] = SOCKS_NOAUTH;
        !           113:                cnt = write (proxyfd, buf, 3);
        !           114:                if (cnt == -1)
        !           115:                        err (1, "write failed");
        !           116:                if (cnt != 3)
        !           117:                        errx (1, "short write, %d (expected 3)", cnt);
        !           118:
        !           119:                read (proxyfd, buf, 2);
        !           120:                if (buf[1] == SOCKS_NOMETHOD)
        !           121:                        errx (1, "authentication method negotiation failed");
        !           122:
        !           123:                /* Version 5, connect: IPv4 address */
        !           124:                buf[0] = SOCKS_V5;
        !           125:                buf[1] = SOCKS_CONNECT;
        !           126:                buf[2] = 0;
        !           127:                buf[3] = SOCKS_IPV4;
        !           128:                memcpy (buf + 4, &serveraddr, sizeof serveraddr);
        !           129:                memcpy (buf + 8, &serverport, sizeof serverport);
        !           130:
        !           131:                /* XXX Handle short writes better */
        !           132:                cnt = write (proxyfd, buf, 10);
        !           133:                if (cnt == -1)
        !           134:                        err (1, "write failed");
        !           135:                if (cnt != 10)
        !           136:                        errx (1, "short write, %d (expected 10)", cnt);
        !           137:
        !           138:                /* XXX Handle short reads better */
        !           139:                cnt = read (proxyfd, buf, sizeof buf);
        !           140:                if (cnt == -1)
        !           141:                        err (1, "read failed");
        !           142:                if (cnt != 10)
        !           143:                        errx (1, "unexpected reply size %d (expected 10)", cnt);
        !           144:                if (buf[1] != 0)
        !           145:                        errx (1, "connection failed, SOCKS error %d", buf[1]);
        !           146:        } else {
        !           147:                /* Version 4 */
        !           148:                buf[0] = SOCKS_V4;
        !           149:                buf[1] = SOCKS_CONNECT; /* connect */
        !           150:                memcpy (buf + 2, &serverport, sizeof serverport);
        !           151:                memcpy (buf + 4, &serveraddr, sizeof serveraddr);
        !           152:                buf[8] = 0;     /* empty username */
        !           153:
        !           154:                cnt = write (proxyfd, buf, 9);
        !           155:                if (cnt == -1)
        !           156:                        err (1, "write failed");
        !           157:                if (cnt != 9)
        !           158:                        errx (1, "short write, %d (expected 9)", cnt);
        !           159:
        !           160:                /* XXX Handle short reads better */
        !           161:                cnt = read (proxyfd, buf, 8);
        !           162:                if (cnt == -1)
        !           163:                        err (1, "read failed");
        !           164:                if (cnt != 8)
        !           165:                        errx (1, "unexpected reply size %d (expected 8)", cnt);
        !           166:                if (buf[1] != 90)
        !           167:                        errx (1, "connection failed, SOCKS error %d", buf[1]);
        !           168:        }
1.1       jakob     169:
                    170:        return proxyfd;
                    171: }