socks.c 24 KB

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