socks.c 21 KB

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