socks.c 24 KB

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