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