diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/usr.bin/nc/socks.c | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/src/usr.bin/nc/socks.c b/src/usr.bin/nc/socks.c index 035898e7e6..31721ccd24 100644 --- a/src/usr.bin/nc/socks.c +++ b/src/usr.bin/nc/socks.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: socks.c,v 1.10 2005/02/08 15:26:23 otto Exp $ */ | 1 | /* $OpenBSD: socks.c,v 1.11 2005/05/19 04:29:46 djm Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 1999 Niklas Hallqvist. All rights reserved. | 4 | * Copyright (c) 1999 Niklas Hallqvist. All rights reserved. |
@@ -56,14 +56,14 @@ int socks_connect(const char *host, const char *port, struct addrinfo hints, | |||
56 | static in_addr_t | 56 | static in_addr_t |
57 | decode_addr(const char *s) | 57 | decode_addr(const char *s) |
58 | { | 58 | { |
59 | struct hostent *hp = gethostbyname (s); | 59 | struct hostent *hp = gethostbyname(s); |
60 | struct in_addr retval; | 60 | struct in_addr retval; |
61 | 61 | ||
62 | if (hp) | 62 | if (hp) |
63 | return *(in_addr_t *)hp->h_addr_list[0]; | 63 | return (*(in_addr_t *)hp->h_addr_list[0]); |
64 | if (inet_aton (s, &retval)) | 64 | if (inet_aton(s, &retval)) |
65 | return retval.s_addr; | 65 | return (retval.s_addr); |
66 | errx (1, "cannot decode address \"%s\"", s); | 66 | errx(1, "cannot decode address \"%s\"", s); |
67 | } | 67 | } |
68 | 68 | ||
69 | static in_port_t | 69 | static in_port_t |
@@ -73,14 +73,14 @@ decode_port(const char *s) | |||
73 | in_port_t port; | 73 | in_port_t port; |
74 | char *p; | 74 | char *p; |
75 | 75 | ||
76 | port = strtol (s, &p, 10); | 76 | port = strtol(s, &p, 10); |
77 | if (s == p) { | 77 | if (s == p) { |
78 | sp = getservbyname (s, "tcp"); | 78 | sp = getservbyname(s, "tcp"); |
79 | if (sp) | 79 | if (sp) |
80 | return sp->s_port; | 80 | return (sp->s_port); |
81 | } | 81 | } |
82 | if (*s != '\0' && *p == '\0') | 82 | if (*s != '\0' && *p == '\0') |
83 | return htons (port); | 83 | return (htons(port)); |
84 | errx (1, "cannot decode port \"%s\"", s); | 84 | errx (1, "cannot decode port \"%s\"", s); |
85 | } | 85 | } |
86 | 86 | ||
@@ -127,77 +127,77 @@ socks_connect(const char *host, const char *port, | |||
127 | proxyfd = remote_connect(proxyhost, proxyport, proxyhints); | 127 | proxyfd = remote_connect(proxyhost, proxyport, proxyhints); |
128 | 128 | ||
129 | if (proxyfd < 0) | 129 | if (proxyfd < 0) |
130 | return -1; | 130 | return (-1); |
131 | 131 | ||
132 | serveraddr = decode_addr (host); | 132 | serveraddr = decode_addr(host); |
133 | serverport = decode_port (port); | 133 | serverport = decode_port(port); |
134 | 134 | ||
135 | if (socksv == 5) { | 135 | if (socksv == 5) { |
136 | /* Version 5, one method: no authentication */ | 136 | /* Version 5, one method: no authentication */ |
137 | buf[0] = SOCKS_V5; | 137 | buf[0] = SOCKS_V5; |
138 | buf[1] = 1; | 138 | buf[1] = 1; |
139 | buf[2] = SOCKS_NOAUTH; | 139 | buf[2] = SOCKS_NOAUTH; |
140 | cnt = write (proxyfd, buf, 3); | 140 | cnt = write(proxyfd, buf, 3); |
141 | if (cnt == -1) | 141 | if (cnt == -1) |
142 | err (1, "write failed"); | 142 | err(1, "write failed"); |
143 | if (cnt != 3) | 143 | if (cnt != 3) |
144 | errx (1, "short write, %d (expected 3)", cnt); | 144 | errx(1, "short write, %d (expected 3)", cnt); |
145 | 145 | ||
146 | read (proxyfd, buf, 2); | 146 | read(proxyfd, buf, 2); |
147 | if (buf[1] == SOCKS_NOMETHOD) | 147 | if (buf[1] == SOCKS_NOMETHOD) |
148 | errx (1, "authentication method negotiation failed"); | 148 | errx(1, "authentication method negotiation failed"); |
149 | 149 | ||
150 | /* Version 5, connect: IPv4 address */ | 150 | /* Version 5, connect: IPv4 address */ |
151 | buf[0] = SOCKS_V5; | 151 | buf[0] = SOCKS_V5; |
152 | buf[1] = SOCKS_CONNECT; | 152 | buf[1] = SOCKS_CONNECT; |
153 | buf[2] = 0; | 153 | buf[2] = 0; |
154 | buf[3] = SOCKS_IPV4; | 154 | buf[3] = SOCKS_IPV4; |
155 | memcpy (buf + 4, &serveraddr, sizeof serveraddr); | 155 | memcpy(buf + 4, &serveraddr, sizeof serveraddr); |
156 | memcpy (buf + 8, &serverport, sizeof serverport); | 156 | memcpy(buf + 8, &serverport, sizeof serverport); |
157 | 157 | ||
158 | /* XXX Handle short writes better */ | 158 | /* XXX Handle short writes better */ |
159 | cnt = write (proxyfd, buf, 10); | 159 | cnt = write(proxyfd, buf, 10); |
160 | if (cnt == -1) | 160 | if (cnt == -1) |
161 | err (1, "write failed"); | 161 | err(1, "write failed"); |
162 | if (cnt != 10) | 162 | if (cnt != 10) |
163 | errx (1, "short write, %d (expected 10)", cnt); | 163 | errx(1, "short write, %d (expected 10)", cnt); |
164 | 164 | ||
165 | /* XXX Handle short reads better */ | 165 | /* XXX Handle short reads better */ |
166 | cnt = read (proxyfd, buf, sizeof buf); | 166 | cnt = read(proxyfd, buf, sizeof buf); |
167 | if (cnt == -1) | 167 | if (cnt == -1) |
168 | err (1, "read failed"); | 168 | err(1, "read failed"); |
169 | if (cnt != 10) | 169 | if (cnt != 10) |
170 | errx (1, "unexpected reply size %d (expected 10)", cnt); | 170 | errx(1, "unexpected reply size %d (expected 10)", cnt); |
171 | if (buf[1] != 0) | 171 | if (buf[1] != 0) |
172 | errx (1, "connection failed, SOCKS error %d", buf[1]); | 172 | errx(1, "connection failed, SOCKS error %d", buf[1]); |
173 | } else if (socksv == 4) { | 173 | } else if (socksv == 4) { |
174 | /* Version 4 */ | 174 | /* Version 4 */ |
175 | buf[0] = SOCKS_V4; | 175 | buf[0] = SOCKS_V4; |
176 | buf[1] = SOCKS_CONNECT; /* connect */ | 176 | buf[1] = SOCKS_CONNECT; /* connect */ |
177 | memcpy (buf + 2, &serverport, sizeof serverport); | 177 | memcpy(buf + 2, &serverport, sizeof serverport); |
178 | memcpy (buf + 4, &serveraddr, sizeof serveraddr); | 178 | memcpy(buf + 4, &serveraddr, sizeof serveraddr); |
179 | buf[8] = 0; /* empty username */ | 179 | buf[8] = 0; /* empty username */ |
180 | 180 | ||
181 | cnt = write (proxyfd, buf, 9); | 181 | cnt = write(proxyfd, buf, 9); |
182 | if (cnt == -1) | 182 | if (cnt == -1) |
183 | err (1, "write failed"); | 183 | err(1, "write failed"); |
184 | if (cnt != 9) | 184 | if (cnt != 9) |
185 | errx (1, "short write, %d (expected 9)", cnt); | 185 | errx(1, "short write, %d (expected 9)", cnt); |
186 | 186 | ||
187 | /* XXX Handle short reads better */ | 187 | /* XXX Handle short reads better */ |
188 | cnt = read (proxyfd, buf, 8); | 188 | cnt = read(proxyfd, buf, 8); |
189 | if (cnt == -1) | 189 | if (cnt == -1) |
190 | err (1, "read failed"); | 190 | err(1, "read failed"); |
191 | if (cnt != 8) | 191 | if (cnt != 8) |
192 | errx (1, "unexpected reply size %d (expected 8)", cnt); | 192 | errx(1, "unexpected reply size %d (expected 8)", cnt); |
193 | if (buf[1] != 90) | 193 | if (buf[1] != 90) |
194 | errx (1, "connection failed, SOCKS error %d", buf[1]); | 194 | errx(1, "connection failed, SOCKS error %d", buf[1]); |
195 | } else if (socksv == -1) { | 195 | } else if (socksv == -1) { |
196 | /* HTTP proxy CONNECT */ | 196 | /* HTTP proxy CONNECT */ |
197 | 197 | ||
198 | /* Disallow bad chars in hostname */ | 198 | /* Disallow bad chars in hostname */ |
199 | if (strcspn(host, "\r\n\t []:") != strlen(host)) | 199 | if (strcspn(host, "\r\n\t []:") != strlen(host)) |
200 | errx (1, "Invalid hostname"); | 200 | errx(1, "Invalid hostname"); |
201 | 201 | ||
202 | /* Try to be sane about numeric IPv6 addresses */ | 202 | /* Try to be sane about numeric IPv6 addresses */ |
203 | if (strchr(host, ':') != NULL) { | 203 | if (strchr(host, ':') != NULL) { |
@@ -210,27 +210,27 @@ socks_connect(const char *host, const char *port, | |||
210 | host, ntohs(serverport)); | 210 | host, ntohs(serverport)); |
211 | } | 211 | } |
212 | if (r == -1 || (size_t)r >= sizeof(buf)) | 212 | if (r == -1 || (size_t)r >= sizeof(buf)) |
213 | errx (1, "hostname too long"); | 213 | errx(1, "hostname too long"); |
214 | r = strlen(buf); | 214 | r = strlen(buf); |
215 | 215 | ||
216 | /* XXX atomicio */ | 216 | /* XXX atomicio */ |
217 | cnt = write (proxyfd, buf, r); | 217 | cnt = write(proxyfd, buf, r); |
218 | if (cnt == -1) | 218 | if (cnt == -1) |
219 | err (1, "write failed"); | 219 | err(1, "write failed"); |
220 | if (cnt != r) | 220 | if (cnt != r) |
221 | errx (1, "short write, %d (expected %d)", cnt, r); | 221 | errx(1, "short write, %d (expected %d)", cnt, r); |
222 | 222 | ||
223 | /* Read reply */ | 223 | /* Read reply */ |
224 | for (r = 0; r < HTTP_MAXHDRS; r++) { | 224 | for (r = 0; r < HTTP_MAXHDRS; r++) { |
225 | proxy_read_line(proxyfd, buf, sizeof(buf)); | 225 | proxy_read_line(proxyfd, buf, sizeof(buf)); |
226 | if (r == 0 && strncmp(buf, "HTTP/1.0 200 ", 12) != 0) | 226 | if (r == 0 && strncmp(buf, "HTTP/1.0 200 ", 12) != 0) |
227 | errx (1, "Proxy error: \"%s\"", buf); | 227 | errx(1, "Proxy error: \"%s\"", buf); |
228 | /* Discard headers until we hit an empty line */ | 228 | /* Discard headers until we hit an empty line */ |
229 | if (*buf == '\0') | 229 | if (*buf == '\0') |
230 | break; | 230 | break; |
231 | } | 231 | } |
232 | } else | 232 | } else |
233 | errx (1, "Unknown proxy protocol %d", socksv); | 233 | errx(1, "Unknown proxy protocol %d", socksv); |
234 | 234 | ||
235 | return proxyfd; | 235 | return (proxyfd); |
236 | } | 236 | } |