1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2009, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * $Id: socks.c,v 1.28 2009-01-28 21:34:16 bagder Exp $
22 ***************************************************************************/
23
24 #include "setup.h"
25
26 #ifndef CURL_DISABLE_PROXY
27 #include <string.h>
28
29 #ifdef NEED_MALLOC_H
30 #include <malloc.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
37 #endif
38 #ifdef HAVE_NETINET_IN_H
39 #include <netinet/in.h>
40 #endif
41 #ifdef HAVE_ARPA_INET_H
42 #include <arpa/inet.h>
43 #endif
44
45 #include "urldata.h"
46 #include "sendf.h"
47 #include "strequal.h"
48 #include "select.h"
49 #include "connect.h"
50 #include "timeval.h"
51 #include "socks.h"
52
53 /* The last #include file should be: */
54 #include "memdebug.h"
55
56 /*
57 * Helper read-from-socket functions. Does the same as Curl_read() but it
58 * blocks until all bytes amount of buffersize will be read. No more, no less.
59 *
60 * This is STUPID BLOCKING behaviour which we frown upon, but right now this
61 * is what we have...
62 */
Curl_blockread_all(struct connectdata * conn,curl_socket_t sockfd,char * buf,ssize_t buffersize,ssize_t * n,long conn_timeout)63 int Curl_blockread_all(struct connectdata *conn, /* connection data */
64 curl_socket_t sockfd, /* read from this socket */
65 char *buf, /* store read data here */
66 ssize_t buffersize, /* max amount to read */
67 ssize_t *n, /* amount bytes read */
68 long conn_timeout) /* timeout for data wait
69 relative to
70 conn->created */
71 {
72 ssize_t nread;
73 ssize_t allread = 0;
74 int result;
75 struct timeval tvnow;
76 long conntime;
77 *n = 0;
78 do {
79 tvnow = Curl_tvnow();
80 /* calculating how long connection is establishing */
81 conntime = Curl_tvdiff(tvnow, conn->created);
82 if(conntime > conn_timeout) {
83 /* we already got the timeout */
84 result = ~CURLE_OK;
85 break;
86 }
87 if(Curl_socket_ready(sockfd, CURL_SOCKET_BAD,
88 (int)(conn_timeout - conntime)) <= 0) {
89 result = ~CURLE_OK;
90 break;
91 }
92 result = Curl_read_plain(sockfd, buf, buffersize, &nread);
93 if(result)
94 break;
95
96 if(buffersize == nread) {
97 allread += nread;
98 *n = allread;
99 result = CURLE_OK;
100 break;
101 }
102 if(!nread) {
103 result = ~CURLE_OK;
104 break;
105 }
106
107 buffersize -= nread;
108 buf += nread;
109 allread += nread;
110 } while(1);
111 return result;
112 }
113
114 /*
115 * This function logs in to a SOCKS4 proxy and sends the specifics to the final
116 * destination server.
117 *
118 * Reference :
119 * http://socks.permeo.com/protocol/socks4.protocol
120 *
121 * Note :
122 * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
123 * Nonsupport "Identification Protocol (RFC1413)"
124 */
Curl_SOCKS4(const char * proxy_name,const char * hostname,int remote_port,int sockindex,struct connectdata * conn,bool protocol4a)125 CURLcode Curl_SOCKS4(const char *proxy_name,
126 const char *hostname,
127 int remote_port,
128 int sockindex,
129 struct connectdata *conn,
130 bool protocol4a)
131 {
132 #define SOCKS4REQLEN 262
133 unsigned char socksreq[SOCKS4REQLEN]; /* room for SOCKS4 request incl. user
134 id */
135 int result;
136 CURLcode code;
137 curl_socket_t sock = conn->sock[sockindex];
138 long timeout;
139 struct SessionHandle *data = conn->data;
140
141 /* get timeout */
142 timeout = Curl_timeleft(conn, NULL, TRUE);
143
144 if(timeout < 0) {
145 /* time-out, bail out, go home */
146 failf(data, "Connection time-out");
147 return CURLE_OPERATION_TIMEDOUT;
148 }
149
150 Curl_nonblock(sock, FALSE);
151
152 /*
153 * Compose socks4 request
154 *
155 * Request format
156 *
157 * +----+----+----+----+----+----+----+----+----+----+....+----+
158 * | VN | CD | DSTPORT | DSTIP | USERID |NULL|
159 * +----+----+----+----+----+----+----+----+----+----+....+----+
160 * # of bytes: 1 1 2 4 variable 1
161 */
162
163 socksreq[0] = 4; /* version (SOCKS4) */
164 socksreq[1] = 1; /* connect */
165 *((unsigned short*)&socksreq[2]) = htons((unsigned short)remote_port);
166
167 /* DNS resolve only for SOCKS4, not SOCKS4a */
168 if (!protocol4a) {
169 struct Curl_dns_entry *dns;
170 Curl_addrinfo *hp=NULL;
171 int rc;
172
173 rc = Curl_resolv(conn, hostname, remote_port, &dns);
174
175 if(rc == CURLRESOLV_ERROR)
176 return CURLE_COULDNT_RESOLVE_PROXY;
177
178 if(rc == CURLRESOLV_PENDING)
179 /* this requires that we're in "wait for resolve" state */
180 rc = Curl_wait_for_resolv(conn, &dns);
181
182 /*
183 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
184 * returns a Curl_addrinfo pointer that may not always look the same.
185 */
186 if(dns)
187 hp=dns->addr;
188 if(hp) {
189 char buf[64];
190 unsigned short ip[4];
191 Curl_printable_address(hp, buf, sizeof(buf));
192
193 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
194 &ip[0], &ip[1], &ip[2], &ip[3])) {
195 /* Set DSTIP */
196 socksreq[4] = (unsigned char)ip[0];
197 socksreq[5] = (unsigned char)ip[1];
198 socksreq[6] = (unsigned char)ip[2];
199 socksreq[7] = (unsigned char)ip[3];
200 }
201 else
202 hp = NULL; /* fail! */
203
204 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
205
206 }
207 if(!hp) {
208 failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
209 hostname);
210 return CURLE_COULDNT_RESOLVE_HOST;
211 }
212 }
213
214 /*
215 * This is currently not supporting "Identification Protocol (RFC1413)".
216 */
217 socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
218 if(proxy_name)
219 strlcat((char*)socksreq + 8, proxy_name, sizeof(socksreq) - 8);
220
221 /*
222 * Make connection
223 */
224 {
225 ssize_t actualread;
226 ssize_t written;
227 ssize_t hostnamelen = 0;
228 int packetsize = 9 +
229 (int)strlen((char*)socksreq + 8); /* size including NUL */
230
231 /* If SOCKS4a, set special invalid IP address 0.0.0.x */
232 if (protocol4a) {
233 socksreq[4] = 0;
234 socksreq[5] = 0;
235 socksreq[6] = 0;
236 socksreq[7] = 1;
237 /* If still enough room in buffer, also append hostname */
238 hostnamelen = (ssize_t)strlen(hostname) + 1; /* length including NUL */
239 if (packetsize + hostnamelen <= SOCKS4REQLEN)
240 strcpy((char*)socksreq + packetsize, hostname);
241 else
242 hostnamelen = 0; /* Flag: hostname did not fit in buffer */
243 }
244
245 /* Send request */
246 code = Curl_write_plain(conn, sock, (char *)socksreq,
247 packetsize + hostnamelen,
248 &written);
249 if((code != CURLE_OK) || (written != packetsize + hostnamelen)) {
250 failf(data, "Failed to send SOCKS4 connect request.");
251 return CURLE_COULDNT_CONNECT;
252 }
253 if (protocol4a && hostnamelen == 0) {
254 /* SOCKS4a with very long hostname - send that name separately */
255 hostnamelen = (ssize_t)strlen(hostname) + 1;
256 code = Curl_write_plain(conn, sock, (char *)hostname, hostnamelen,
257 &written);
258 if((code != CURLE_OK) || (written != hostnamelen)) {
259 failf(data, "Failed to send SOCKS4 connect request.");
260 return CURLE_COULDNT_CONNECT;
261 }
262 }
263
264 packetsize = 8; /* receive data size */
265
266 /* Receive response */
267 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
268 &actualread, timeout);
269 if((result != CURLE_OK) || (actualread != packetsize)) {
270 failf(data, "Failed to receive SOCKS4 connect request ack.");
271 return CURLE_COULDNT_CONNECT;
272 }
273
274 /*
275 * Response format
276 *
277 * +----+----+----+----+----+----+----+----+
278 * | VN | CD | DSTPORT | DSTIP |
279 * +----+----+----+----+----+----+----+----+
280 * # of bytes: 1 1 2 4
281 *
282 * VN is the version of the reply code and should be 0. CD is the result
283 * code with one of the following values:
284 *
285 * 90: request granted
286 * 91: request rejected or failed
287 * 92: request rejected because SOCKS server cannot connect to
288 * identd on the client
289 * 93: request rejected because the client program and identd
290 * report different user-ids
291 */
292
293 /* wrong version ? */
294 if(socksreq[0] != 0) {
295 failf(data,
296 "SOCKS4 reply has wrong version, version should be 4.");
297 return CURLE_COULDNT_CONNECT;
298 }
299
300 /* Result */
301 switch(socksreq[1])
302 {
303 case 90:
304 if (protocol4a)
305 infof(data, "SOCKS4a request granted.\n");
306 else
307 infof(data, "SOCKS4 request granted.\n");
308 break;
309 case 91:
310 failf(data,
311 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
312 ", request rejected or failed.",
313 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
314 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
315 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
316 socksreq[1]);
317 return CURLE_COULDNT_CONNECT;
318 case 92:
319 failf(data,
320 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
321 ", request rejected because SOCKS server cannot connect to "
322 "identd on the client.",
323 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
324 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
325 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
326 socksreq[1]);
327 return CURLE_COULDNT_CONNECT;
328 case 93:
329 failf(data,
330 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
331 ", request rejected because the client program and identd "
332 "report different user-ids.",
333 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
334 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
335 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
336 socksreq[1]);
337 return CURLE_COULDNT_CONNECT;
338 default:
339 failf(data,
340 "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
341 ", Unknown.",
342 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
343 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
344 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
345 socksreq[1]);
346 return CURLE_COULDNT_CONNECT;
347 }
348 }
349
350 Curl_nonblock(sock, TRUE);
351
352 return CURLE_OK; /* Proxy was successful! */
353 }
354
355 /*
356 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
357 * destination server.
358 */
Curl_SOCKS5(const char * proxy_name,const char * proxy_password,const char * hostname,int remote_port,int sockindex,struct connectdata * conn)359 CURLcode Curl_SOCKS5(const char *proxy_name,
360 const char *proxy_password,
361 const char *hostname,
362 int remote_port,
363 int sockindex,
364 struct connectdata *conn)
365 {
366 /*
367 According to the RFC1928, section "6. Replies". This is what a SOCK5
368 replies:
369
370 +----+-----+-------+------+----------+----------+
371 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
372 +----+-----+-------+------+----------+----------+
373 | 1 | 1 | X'00' | 1 | Variable | 2 |
374 +----+-----+-------+------+----------+----------+
375
376 Where:
377
378 o VER protocol version: X'05'
379 o REP Reply field:
380 o X'00' succeeded
381 */
382
383 unsigned char socksreq[600]; /* room for large user/pw (255 max each) */
384 ssize_t actualread;
385 ssize_t written;
386 int result;
387 CURLcode code;
388 curl_socket_t sock = conn->sock[sockindex];
389 struct SessionHandle *data = conn->data;
390 long timeout;
391 bool socks5_resolve_local = (bool)(data->set.proxytype == CURLPROXY_SOCKS5);
392 const size_t hostname_len = strlen(hostname);
393 ssize_t packetsize = 0;
394
395 /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
396 if(!socks5_resolve_local && hostname_len > 255)
397 {
398 infof(conn->data,"SOCKS5: server resolving disabled for hostnames of "
399 "length > 255 [actual len=%d]\n", hostname_len);
400 socks5_resolve_local = TRUE;
401 }
402
403 /* get timeout */
404 timeout = Curl_timeleft(conn, NULL, TRUE);
405
406 if(timeout < 0) {
407 /* time-out, bail out, go home */
408 failf(data, "Connection time-out");
409 return CURLE_OPERATION_TIMEDOUT;
410 }
411
412 Curl_nonblock(sock, TRUE);
413
414 /* wait until socket gets connected */
415 result = Curl_socket_ready(CURL_SOCKET_BAD, sock, (int)timeout);
416
417 if(-1 == result) {
418 failf(conn->data, "SOCKS5: no connection here");
419 return CURLE_COULDNT_CONNECT;
420 }
421 else if(0 == result) {
422 failf(conn->data, "SOCKS5: connection timeout");
423 return CURLE_OPERATION_TIMEDOUT;
424 }
425
426 if(result & CURL_CSELECT_ERR) {
427 failf(conn->data, "SOCKS5: error occured during connection");
428 return CURLE_COULDNT_CONNECT;
429 }
430
431 socksreq[0] = 5; /* version */
432 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
433 socksreq[1] = (char)(proxy_name ? 3 : 2); /* number of methods (below) */
434 socksreq[2] = 0; /* no authentication */
435 socksreq[3] = 1; /* gssapi */
436 socksreq[4] = 2; /* username/password */
437 #else
438 socksreq[1] = (char)(proxy_name ? 2 : 1); /* number of methods (below) */
439 socksreq[2] = 0; /* no authentication */
440 socksreq[3] = 2; /* username/password */
441 #endif
442
443 Curl_nonblock(sock, FALSE);
444
445 code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
446 &written);
447 if((code != CURLE_OK) || (written != (2 + (int)socksreq[1]))) {
448 failf(data, "Unable to send initial SOCKS5 request.");
449 return CURLE_COULDNT_CONNECT;
450 }
451
452 Curl_nonblock(sock, TRUE);
453
454 result = Curl_socket_ready(sock, CURL_SOCKET_BAD, (int)timeout);
455
456 if(-1 == result) {
457 failf(conn->data, "SOCKS5 nothing to read");
458 return CURLE_COULDNT_CONNECT;
459 }
460 else if(0 == result) {
461 failf(conn->data, "SOCKS5 read timeout");
462 return CURLE_OPERATION_TIMEDOUT;
463 }
464
465 if(result & CURL_CSELECT_ERR) {
466 failf(conn->data, "SOCKS5 read error occured");
467 return CURLE_RECV_ERROR;
468 }
469
470 Curl_nonblock(sock, FALSE);
471
472 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
473 timeout);
474 if((result != CURLE_OK) || (actualread != 2)) {
475 failf(data, "Unable to receive initial SOCKS5 response.");
476 return CURLE_COULDNT_CONNECT;
477 }
478
479 if(socksreq[0] != 5) {
480 failf(data, "Received invalid version in initial SOCKS5 response.");
481 return CURLE_COULDNT_CONNECT;
482 }
483 if(socksreq[1] == 0) {
484 /* Nothing to do, no authentication needed */
485 ;
486 }
487 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
488 else if(socksreq[1] == 1) {
489 code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
490 if(code != CURLE_OK) {
491 failf(data, "Unable to negotiate SOCKS5 gssapi context.");
492 return CURLE_COULDNT_CONNECT;
493 }
494 }
495 #endif
496 else if(socksreq[1] == 2) {
497 /* Needs user name and password */
498 size_t userlen, pwlen;
499 int len;
500 if(proxy_name && proxy_password) {
501 userlen = strlen(proxy_name);
502 pwlen = strlen(proxy_password);
503 }
504 else {
505 userlen = 0;
506 pwlen = 0;
507 }
508
509 /* username/password request looks like
510 * +----+------+----------+------+----------+
511 * |VER | ULEN | UNAME | PLEN | PASSWD |
512 * +----+------+----------+------+----------+
513 * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
514 * +----+------+----------+------+----------+
515 */
516 len = 0;
517 socksreq[len++] = 1; /* username/pw subnegotiation version */
518 socksreq[len++] = (char) userlen;
519 memcpy(socksreq + len, proxy_name, (int) userlen);
520 len += userlen;
521 socksreq[len++] = (char) pwlen;
522 memcpy(socksreq + len, proxy_password, (int) pwlen);
523 len += pwlen;
524
525 code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
526 if((code != CURLE_OK) || (len != written)) {
527 failf(data, "Failed to send SOCKS5 sub-negotiation request.");
528 return CURLE_COULDNT_CONNECT;
529 }
530
531 result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread,
532 timeout);
533 if((result != CURLE_OK) || (actualread != 2)) {
534 failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
535 return CURLE_COULDNT_CONNECT;
536 }
537
538 /* ignore the first (VER) byte */
539 if(socksreq[1] != 0) { /* status */
540 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
541 socksreq[0], socksreq[1]);
542 return CURLE_COULDNT_CONNECT;
543 }
544
545 /* Everything is good so far, user was authenticated! */
546 }
547 else {
548 /* error */
549 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
550 if(socksreq[1] == 255) {
551 #else
552 if(socksreq[1] == 1) {
553 failf(data,
554 "SOCKS5 GSSAPI per-message authentication is not supported.");
555 return CURLE_COULDNT_CONNECT;
556 }
557 else if(socksreq[1] == 255) {
558 #endif
559 if(!proxy_name || !*proxy_name) {
560 failf(data,
561 "No authentication method was acceptable. (It is quite likely"
562 " that the SOCKS5 server wanted a username/password, since none"
563 " was supplied to the server on this connection.)");
564 }
565 else {
566 failf(data, "No authentication method was acceptable.");
567 }
568 return CURLE_COULDNT_CONNECT;
569 }
570 else {
571 failf(data,
572 "Undocumented SOCKS5 mode attempted to be used by server.");
573 return CURLE_COULDNT_CONNECT;
574 }
575 }
576
577 /* Authentication is complete, now specify destination to the proxy */
578 socksreq[0] = 5; /* version (SOCKS5) */
579 socksreq[1] = 1; /* connect */
580 socksreq[2] = 0; /* must be zero */
581
582 if(!socks5_resolve_local) {
583 packetsize = (ssize_t)(5 + hostname_len + 2);
584
585 socksreq[3] = 3; /* ATYP: domain name = 3 */
586 socksreq[4] = (char) hostname_len; /* address length */
587 memcpy(&socksreq[5], hostname, hostname_len); /* address bytes w/o NULL */
588
589 *((unsigned short*)&socksreq[hostname_len+5]) =
590 htons((unsigned short)remote_port);
591 }
592 else {
593 struct Curl_dns_entry *dns;
594 Curl_addrinfo *hp=NULL;
595 int rc = Curl_resolv(conn, hostname, remote_port, &dns);
596
597 packetsize = 10;
598
599 socksreq[3] = 1; /* IPv4 = 1 */
600
601 if(rc == CURLRESOLV_ERROR)
602 return CURLE_COULDNT_RESOLVE_HOST;
603
604 if(rc == CURLRESOLV_PENDING)
605 /* this requires that we're in "wait for resolve" state */
606 rc = Curl_wait_for_resolv(conn, &dns);
607
608 /*
609 * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
610 * returns a Curl_addrinfo pointer that may not always look the same.
611 */
612 if(dns)
613 hp=dns->addr;
614 if(hp) {
615 char buf[64];
616 unsigned short ip[4];
617 Curl_printable_address(hp, buf, sizeof(buf));
618
619 if(4 == sscanf( buf, "%hu.%hu.%hu.%hu",
620 &ip[0], &ip[1], &ip[2], &ip[3])) {
621 socksreq[4] = (unsigned char)ip[0];
622 socksreq[5] = (unsigned char)ip[1];
623 socksreq[6] = (unsigned char)ip[2];
624 socksreq[7] = (unsigned char)ip[3];
625 }
626 else
627 hp = NULL; /* fail! */
628
629 Curl_resolv_unlock(data, dns); /* not used anymore from now on */
630 }
631 if(!hp) {
632 failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
633 hostname);
634 return CURLE_COULDNT_RESOLVE_HOST;
635 }
636
637 *((unsigned short*)&socksreq[8]) = htons((unsigned short)remote_port);
638 }
639
640 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
641 if(conn->socks5_gssapi_enctype) {
642 failf(data, "SOCKS5 gssapi protection not yet implemented.");
643 } else
644 #endif
645 code = Curl_write_plain(conn, sock, (char *)socksreq, packetsize, &written);
646 if((code != CURLE_OK) || (written != packetsize)) {
647 failf(data, "Failed to send SOCKS5 connect request.");
648 return CURLE_COULDNT_CONNECT;
649 }
650
651 packetsize = 10; /* minimum packet size is 10 */
652
653 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
654 if(conn->socks5_gssapi_enctype) {
655 failf(data, "SOCKS5 gssapi protection not yet implemented.");
656 } else
657 #endif
658 result = Curl_blockread_all(conn, sock, (char *)socksreq, packetsize,
659 &actualread, timeout);
660 if((result != CURLE_OK) || (actualread != packetsize)) {
661 failf(data, "Failed to receive SOCKS5 connect request ack.");
662 return CURLE_COULDNT_CONNECT;
663 }
664
665 if(socksreq[0] != 5) { /* version */
666 failf(data,
667 "SOCKS5 reply has wrong version, version should be 5.");
668 return CURLE_COULDNT_CONNECT;
669 }
670 if(socksreq[1] != 0) { /* Anything besides 0 is an error */
671 failf(data,
672 "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
673 (unsigned char)socksreq[4], (unsigned char)socksreq[5],
674 (unsigned char)socksreq[6], (unsigned char)socksreq[7],
675 (unsigned int)ntohs(*(unsigned short*)(&socksreq[8])),
676 socksreq[1]);
677 return CURLE_COULDNT_CONNECT;
678 }
679
680 /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
681 1928, so the reply packet should be read until the end to avoid errors at
682 subsequent protocol level.
683
684 +----+-----+-------+------+----------+----------+
685 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
686 +----+-----+-------+------+----------+----------+
687 | 1 | 1 | X'00' | 1 | Variable | 2 |
688 +----+-----+-------+------+----------+----------+
689
690 ATYP:
691 o IP v4 address: X'01', BND.ADDR = 4 byte
692 o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
693 o IP v6 address: X'04', BND.ADDR = 16 byte
694 */
695
696 /* Calculate real packet size */
697 if(socksreq[3] == 3) {
698 /* domain name */
699 int addrlen = (int) socksreq[4];
700 packetsize = 5 + addrlen + 2;
701 }
702 else if(socksreq[3] == 4) {
703 /* IPv6 */
704 packetsize = 4 + 16 + 2;
705 }
706
707 /* At this point we already read first 10 bytes */
708 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
709 if(!conn->socks5_gssapi_enctype) {
710 /* decrypt_gssapi_blockread already read the whole packet */
711 #endif
712 if(packetsize > 10) {
713 packetsize -= 10;
714 result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
715 packetsize, &actualread, timeout);
716 if((result != CURLE_OK) || (actualread != packetsize)) {
717 failf(data, "Failed to receive SOCKS5 connect request ack.");
718 return CURLE_COULDNT_CONNECT;
719 }
720 }
721 #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
722 }
723 #endif
724
725 Curl_nonblock(sock, TRUE);
726 return CURLE_OK; /* Proxy was successful! */
727 }
728
729 #endif /* CURL_DISABLE_PROXY */
730
731