summaryrefslogtreecommitdiff
path: root/src/usr.bin/nc/socks.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/usr.bin/nc/socks.c')
-rw-r--r--src/usr.bin/nc/socks.c169
1 files changed, 169 insertions, 0 deletions
diff --git a/src/usr.bin/nc/socks.c b/src/usr.bin/nc/socks.c
new file mode 100644
index 0000000000..a6b4fdd7e3
--- /dev/null
+++ b/src/usr.bin/nc/socks.c
@@ -0,0 +1,169 @@
1/* $OpenBSD: socks.c,v 1.8 2003/07/07 21:36:23 deraadt 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 *
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"
40#define SOCKS_V5 5
41#define SOCKS_V4 4
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
47
48int remote_connect(char *, char *, struct addrinfo);
49int socks_connect(char *host, char *port, struct addrinfo hints,
50 char *proxyhost, char *proxyport, struct addrinfo proxyhints,
51 int socksv);
52
53static in_addr_t
54decode_addr(const char *s)
55{
56 struct hostent *hp = gethostbyname (s);
57 struct in_addr retval;
58
59 if (hp)
60 return *(in_addr_t *)hp->h_addr_list[0];
61 if (inet_aton (s, &retval))
62 return retval.s_addr;
63 errx (1, "cannot decode address \"%s\"", s);
64}
65
66static in_port_t
67decode_port(const char *s)
68{
69 struct servent *sp;
70 in_port_t port;
71 char *p;
72
73 port = strtol (s, &p, 10);
74 if (s == p) {
75 sp = getservbyname (s, "tcp");
76 if (sp)
77 return sp->s_port;
78 }
79 if (*s != '\0' && *p == '\0')
80 return htons (port);
81 errx (1, "cannot decode port \"%s\"", s);
82}
83
84int
85socks_connect(char *host, char *port, struct addrinfo hints,
86 char *proxyhost, char *proxyport, struct addrinfo proxyhints,
87 int socksv)
88{
89 int proxyfd;
90 unsigned char buf[SOCKS_MAXCMDSZ];
91 ssize_t cnt;
92 in_addr_t serveraddr;
93 in_port_t serverport;
94
95 if (proxyport)
96 proxyfd = remote_connect(proxyhost, proxyport, proxyhints);
97 else
98 proxyfd = remote_connect(proxyhost, SOCKS_PORT, proxyhints);
99
100 if (proxyfd < 0)
101 return -1;
102
103 serveraddr = decode_addr (host);
104 serverport = decode_port (port);
105
106 if (socksv == 5) {
107 /* Version 5, one method: no authentication */
108 buf[0] = SOCKS_V5;
109 buf[1] = 1;
110 buf[2] = SOCKS_NOAUTH;
111 cnt = write (proxyfd, buf, 3);
112 if (cnt == -1)
113 err (1, "write failed");
114 if (cnt != 3)
115 errx (1, "short write, %d (expected 3)", cnt);
116
117 read (proxyfd, buf, 2);
118 if (buf[1] == SOCKS_NOMETHOD)
119 errx (1, "authentication method negotiation failed");
120
121 /* Version 5, connect: IPv4 address */
122 buf[0] = SOCKS_V5;
123 buf[1] = SOCKS_CONNECT;
124 buf[2] = 0;
125 buf[3] = SOCKS_IPV4;
126 memcpy (buf + 4, &serveraddr, sizeof serveraddr);
127 memcpy (buf + 8, &serverport, sizeof serverport);
128
129 /* XXX Handle short writes better */
130 cnt = write (proxyfd, buf, 10);
131 if (cnt == -1)
132 err (1, "write failed");
133 if (cnt != 10)
134 errx (1, "short write, %d (expected 10)", cnt);
135
136 /* XXX Handle short reads better */
137 cnt = read (proxyfd, buf, sizeof buf);
138 if (cnt == -1)
139 err (1, "read failed");
140 if (cnt != 10)
141 errx (1, "unexpected reply size %d (expected 10)", cnt);
142 if (buf[1] != 0)
143 errx (1, "connection failed, SOCKS error %d", buf[1]);
144 } else {
145 /* Version 4 */
146 buf[0] = SOCKS_V4;
147 buf[1] = SOCKS_CONNECT; /* connect */
148 memcpy (buf + 2, &serverport, sizeof serverport);
149 memcpy (buf + 4, &serveraddr, sizeof serveraddr);
150 buf[8] = 0; /* empty username */
151
152 cnt = write (proxyfd, buf, 9);
153 if (cnt == -1)
154 err (1, "write failed");
155 if (cnt != 9)
156 errx (1, "short write, %d (expected 9)", cnt);
157
158 /* XXX Handle short reads better */
159 cnt = read (proxyfd, buf, 8);
160 if (cnt == -1)
161 err (1, "read failed");
162 if (cnt != 8)
163 errx (1, "unexpected reply size %d (expected 8)", cnt);
164 if (buf[1] != 90)
165 errx (1, "connection failed, SOCKS error %d", buf[1]);
166 }
167
168 return proxyfd;
169}