xref: /openbsd/usr.bin/nc/socks.c (revision 78b63d65)
1 /*	$OpenBSD: socks.c,v 1.3 2001/10/28 19:46:12 jakob Exp $	*/
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"
45 #define SOCKS_VERSION	5
46 #define SOCKS_NOAUTH	0
47 #define SOCKS_NOMETHOD	0xff
48 #define SOCKS_CONNECT	1
49 #define SOCKS_IPV4	1
50 #define SOCKS_MAXCMDSZ	10
51 
52 static in_addr_t
53 decode_addr (const char *s)
54 {
55 	struct hostent *hp = gethostbyname (s);
56 	struct in_addr retval;
57 
58 	if (hp)
59 		return *(in_addr_t *)hp->h_addr_list[0];
60 	if (inet_aton (s, &retval))
61 		return retval.s_addr;
62 	errx (1, "cannot decode address \"%s\"", s);
63 }
64 
65 static in_port_t
66 decode_port (const char *s)
67 {
68 	struct servent *sp;
69 	in_port_t port;
70 	char *p;
71 
72 	port = strtol (s, &p, 10);
73 	if (s == p) {
74 		sp = getservbyname (s, "tcp");
75 		if (sp)
76 			return sp->s_port;
77 	}
78 	if (*s != '\0' && *p == '\0')
79 		return htons (port);
80 	errx (1, "cannot decode port \"%s\"", s);
81 }
82 
83 int
84 socks_connect (char *host, char *port, struct addrinfo hints,
85     char *proxyhost, char *proxyport, struct addrinfo proxyhints)
86 {
87 	int proxyfd;
88 	unsigned char buf[SOCKS_MAXCMDSZ];
89 	ssize_t cnt;
90 	in_addr_t serveraddr;
91 	in_port_t serverport;
92 
93 	if (proxyport)
94 		proxyfd = remote_connect(proxyhost, proxyport, proxyhints);
95 	else
96 		proxyfd = remote_connect(proxyhost, SOCKS_PORT, proxyhints);
97 
98 	if (!proxyfd)
99 		return -1;
100 
101 	serveraddr = decode_addr (host);
102 	serverport = decode_port (port);
103 
104 	/* Version 5, one method: no authentication */
105 	buf[0] = SOCKS_VERSION;
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_VERSION;
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 
142 	return proxyfd;
143 }
144