socks.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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 https://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 "select.h"
  33. #include "connect.h"
  34. #include "timeval.h"
  35. #include "socks.h"
  36. /* The last #include file should be: */
  37. #include "memdebug.h"
  38. /*
  39. * Helper read-from-socket functions. Does the same as Curl_read() but it
  40. * blocks until all bytes amount of buffersize will be read. No more, no less.
  41. *
  42. * This is STUPID BLOCKING behaviour which we frown upon, but right now this
  43. * is what we have...
  44. */
  45. int Curl_blockread_all(struct connectdata *conn, /* connection data */
  46. curl_socket_t sockfd, /* read from this socket */
  47. char *buf, /* store read data here */
  48. ssize_t buffersize, /* max amount to read */
  49. ssize_t *n) /* amount bytes read */
  50. {
  51. ssize_t nread;
  52. ssize_t allread = 0;
  53. int result;
  54. long timeleft;
  55. *n = 0;
  56. for(;;) {
  57. timeleft = Curl_timeleft(conn->data, NULL, TRUE);
  58. if(timeleft < 0) {
  59. /* we already got the timeout */
  60. result = CURLE_OPERATION_TIMEDOUT;
  61. break;
  62. }
  63. if(SOCKET_READABLE(sockfd, timeleft) <= 0) {
  64. result = ~CURLE_OK;
  65. break;
  66. }
  67. result = Curl_read_plain(sockfd, buf, buffersize, &nread);
  68. if(CURLE_AGAIN == result)
  69. continue;
  70. else if(result)
  71. break;
  72. if(buffersize == nread) {
  73. allread += nread;
  74. *n = allread;
  75. result = CURLE_OK;
  76. break;
  77. }
  78. if(!nread) {
  79. result = ~CURLE_OK;
  80. break;
  81. }
  82. buffersize -= nread;
  83. buf += nread;
  84. allread += nread;
  85. }
  86. return result;
  87. }
  88. /*
  89. * This function logs in to a SOCKS4 proxy and sends the specifics to the final
  90. * destination server.
  91. *
  92. * Reference :
  93. * http://socks.permeo.com/protocol/socks4.protocol
  94. *
  95. * Note :
  96. * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
  97. * Nonsupport "Identification Protocol (RFC1413)"
  98. */
  99. CURLcode Curl_SOCKS4(const char *proxy_name,
  100. const char *hostname,
  101. int remote_port,
  102. int sockindex,
  103. struct connectdata *conn,
  104. bool protocol4a)
  105. {
  106. #define SOCKS4REQLEN 262
  107. unsigned char socksreq[SOCKS4REQLEN]; /* room for SOCKS4 request incl. user
  108. id */
  109. int result;
  110. CURLcode code;
  111. curl_socket_t sock = conn->sock[sockindex];
  112. struct Curl_easy *data = conn->data;
  113. if(Curl_timeleft(data, NULL, TRUE) < 0) {
  114. /* time-out, bail out, go home */
  115. failf(data, "Connection time-out");
  116. return CURLE_OPERATION_TIMEDOUT;
  117. }
  118. (void)curlx_nonblock(sock, FALSE);
  119. infof(data, "SOCKS4 communication to %s:%d\n", hostname, remote_port);
  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. Curl_printable_address(hp, buf, sizeof(buf));
  154. if(hp->ai_family == AF_INET) {
  155. struct sockaddr_in *saddr_in;
  156. saddr_in = (struct sockaddr_in*)(void*)hp->ai_addr;
  157. socksreq[4] = ((unsigned char*)&saddr_in->sin_addr.s_addr)[0];
  158. socksreq[5] = ((unsigned char*)&saddr_in->sin_addr.s_addr)[1];
  159. socksreq[6] = ((unsigned char*)&saddr_in->sin_addr.s_addr)[2];
  160. socksreq[7] = ((unsigned char*)&saddr_in->sin_addr.s_addr)[3];
  161. infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)\n", buf);
  162. }
  163. else {
  164. hp = NULL; /* fail! */
  165. failf(data, "SOCKS4 connection to %s not supported\n", buf);
  166. }
  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. (((unsigned char)socksreq[8] << 8) | (unsigned char)socksreq[9]),
  272. (unsigned char)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. (((unsigned char)socksreq[8] << 8) | (unsigned char)socksreq[9]),
  282. (unsigned char)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. (((unsigned char)socksreq[8] << 8) | (unsigned char)socksreq[9]),
  292. (unsigned char)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. (((unsigned char)socksreq[8] << 8) | (unsigned char)socksreq[9]),
  301. (unsigned char)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 Curl_easy *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 = SOCKET_WRITABLE(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. infof(data, "SOCKS5 communication to %s:%d\n", hostname, remote_port);
  384. code = Curl_write_plain(conn, sock, (char *)socksreq, (2 + (int)socksreq[1]),
  385. &written);
  386. if(code || (written != (2 + (int)socksreq[1]))) {
  387. failf(data, "Unable to send initial SOCKS5 request.");
  388. return CURLE_COULDNT_CONNECT;
  389. }
  390. (void)curlx_nonblock(sock, TRUE);
  391. result = SOCKET_READABLE(sock, timeout);
  392. if(-1 == result) {
  393. failf(conn->data, "SOCKS5 nothing to read");
  394. return CURLE_COULDNT_CONNECT;
  395. }
  396. else if(0 == result) {
  397. failf(conn->data, "SOCKS5 read timeout");
  398. return CURLE_OPERATION_TIMEDOUT;
  399. }
  400. if(result & CURL_CSELECT_ERR) {
  401. failf(conn->data, "SOCKS5 read error occurred");
  402. return CURLE_RECV_ERROR;
  403. }
  404. (void)curlx_nonblock(sock, FALSE);
  405. result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
  406. if(result || (actualread != 2)) {
  407. failf(data, "Unable to receive initial SOCKS5 response.");
  408. return CURLE_COULDNT_CONNECT;
  409. }
  410. if(socksreq[0] != 5) {
  411. failf(data, "Received invalid version in initial SOCKS5 response.");
  412. return CURLE_COULDNT_CONNECT;
  413. }
  414. if(socksreq[1] == 0) {
  415. /* Nothing to do, no authentication needed */
  416. ;
  417. }
  418. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  419. else if(socksreq[1] == 1) {
  420. code = Curl_SOCKS5_gssapi_negotiate(sockindex, conn);
  421. if(code) {
  422. failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
  423. return CURLE_COULDNT_CONNECT;
  424. }
  425. }
  426. #endif
  427. else if(socksreq[1] == 2) {
  428. /* Needs user name and password */
  429. size_t proxy_name_len, proxy_password_len;
  430. if(proxy_name && proxy_password) {
  431. proxy_name_len = strlen(proxy_name);
  432. proxy_password_len = strlen(proxy_password);
  433. }
  434. else {
  435. proxy_name_len = 0;
  436. proxy_password_len = 0;
  437. }
  438. /* username/password request looks like
  439. * +----+------+----------+------+----------+
  440. * |VER | ULEN | UNAME | PLEN | PASSWD |
  441. * +----+------+----------+------+----------+
  442. * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
  443. * +----+------+----------+------+----------+
  444. */
  445. len = 0;
  446. socksreq[len++] = 1; /* username/pw subnegotiation version */
  447. socksreq[len++] = (unsigned char) proxy_name_len;
  448. if(proxy_name && proxy_name_len)
  449. memcpy(socksreq + len, proxy_name, proxy_name_len);
  450. len += proxy_name_len;
  451. socksreq[len++] = (unsigned char) proxy_password_len;
  452. if(proxy_password && proxy_password_len)
  453. memcpy(socksreq + len, proxy_password, proxy_password_len);
  454. len += proxy_password_len;
  455. code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
  456. if(code || (len != written)) {
  457. failf(data, "Failed to send SOCKS5 sub-negotiation request.");
  458. return CURLE_COULDNT_CONNECT;
  459. }
  460. result=Curl_blockread_all(conn, sock, (char *)socksreq, 2, &actualread);
  461. if(result || (actualread != 2)) {
  462. failf(data, "Unable to receive SOCKS5 sub-negotiation response.");
  463. return CURLE_COULDNT_CONNECT;
  464. }
  465. /* ignore the first (VER) byte */
  466. if(socksreq[1] != 0) { /* status */
  467. failf(data, "User was rejected by the SOCKS5 server (%d %d).",
  468. socksreq[0], socksreq[1]);
  469. return CURLE_COULDNT_CONNECT;
  470. }
  471. /* Everything is good so far, user was authenticated! */
  472. }
  473. else {
  474. /* error */
  475. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  476. if(socksreq[1] == 255) {
  477. #else
  478. if(socksreq[1] == 1) {
  479. failf(data,
  480. "SOCKS5 GSSAPI per-message authentication is not supported.");
  481. return CURLE_COULDNT_CONNECT;
  482. }
  483. else if(socksreq[1] == 255) {
  484. #endif
  485. if(!proxy_name || !*proxy_name) {
  486. failf(data,
  487. "No authentication method was acceptable. (It is quite likely"
  488. " that the SOCKS5 server wanted a username/password, since none"
  489. " was supplied to the server on this connection.)");
  490. }
  491. else {
  492. failf(data, "No authentication method was acceptable.");
  493. }
  494. return CURLE_COULDNT_CONNECT;
  495. }
  496. else {
  497. failf(data,
  498. "Undocumented SOCKS5 mode attempted to be used by server.");
  499. return CURLE_COULDNT_CONNECT;
  500. }
  501. }
  502. /* Authentication is complete, now specify destination to the proxy */
  503. len = 0;
  504. socksreq[len++] = 5; /* version (SOCKS5) */
  505. socksreq[len++] = 1; /* connect */
  506. socksreq[len++] = 0; /* must be zero */
  507. if(!socks5_resolve_local) {
  508. socksreq[len++] = 3; /* ATYP: domain name = 3 */
  509. socksreq[len++] = (char) hostname_len; /* address length */
  510. memcpy(&socksreq[len], hostname, hostname_len); /* address str w/o NULL */
  511. len += hostname_len;
  512. }
  513. else {
  514. struct Curl_dns_entry *dns;
  515. Curl_addrinfo *hp = NULL;
  516. int rc = Curl_resolv(conn, hostname, remote_port, &dns);
  517. if(rc == CURLRESOLV_ERROR)
  518. return CURLE_COULDNT_RESOLVE_HOST;
  519. if(rc == CURLRESOLV_PENDING) {
  520. /* this requires that we're in "wait for resolve" state */
  521. code = Curl_resolver_wait_resolv(conn, &dns);
  522. if(code)
  523. return code;
  524. }
  525. /*
  526. * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
  527. * returns a Curl_addrinfo pointer that may not always look the same.
  528. */
  529. if(dns)
  530. hp=dns->addr;
  531. if(hp) {
  532. int i;
  533. char buf[64];
  534. Curl_printable_address(hp, buf, sizeof(buf));
  535. if(hp->ai_family == AF_INET) {
  536. struct sockaddr_in *saddr_in;
  537. socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
  538. saddr_in = (struct sockaddr_in*)(void*)hp->ai_addr;
  539. for(i = 0; i < 4; i++) {
  540. socksreq[len++] = ((unsigned char*)&saddr_in->sin_addr.s_addr)[i];
  541. }
  542. infof(data, "SOCKS5 connect to IPv4 %s (locally resolved)\n", buf);
  543. }
  544. #ifdef ENABLE_IPV6
  545. else if(hp->ai_family == AF_INET6) {
  546. struct sockaddr_in6 *saddr_in6;
  547. socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
  548. saddr_in6 = (struct sockaddr_in6*)(void*)hp->ai_addr;
  549. for(i = 0; i < 16; i++) {
  550. socksreq[len++] = ((unsigned char*)&saddr_in6->sin6_addr.s6_addr)[i];
  551. }
  552. infof(data, "SOCKS5 connect to IPv6 %s (locally resolved)\n", buf);
  553. }
  554. #endif
  555. else {
  556. hp = NULL; /* fail! */
  557. failf(data, "SOCKS5 connection to %s not supported\n", buf);
  558. }
  559. Curl_resolv_unlock(data, dns); /* not used anymore from now on */
  560. }
  561. if(!hp) {
  562. failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
  563. hostname);
  564. return CURLE_COULDNT_RESOLVE_HOST;
  565. }
  566. }
  567. socksreq[len++] = (unsigned char)((remote_port >> 8) & 0xff); /* PORT MSB */
  568. socksreq[len++] = (unsigned char)(remote_port & 0xff); /* PORT LSB */
  569. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  570. if(conn->socks5_gssapi_enctype) {
  571. failf(data, "SOCKS5 GSS-API protection not yet implemented.");
  572. }
  573. else
  574. #endif
  575. code = Curl_write_plain(conn, sock, (char *)socksreq, len, &written);
  576. if(code || (len != written)) {
  577. failf(data, "Failed to send SOCKS5 connect request.");
  578. return CURLE_COULDNT_CONNECT;
  579. }
  580. len = 10; /* minimum packet size is 10 */
  581. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  582. if(conn->socks5_gssapi_enctype) {
  583. failf(data, "SOCKS5 GSS-API protection not yet implemented.");
  584. }
  585. else
  586. #endif
  587. result = Curl_blockread_all(conn, sock, (char *)socksreq,
  588. len, &actualread);
  589. if(result || (len != actualread)) {
  590. failf(data, "Failed to receive SOCKS5 connect request ack.");
  591. return CURLE_COULDNT_CONNECT;
  592. }
  593. if(socksreq[0] != 5) { /* version */
  594. failf(data,
  595. "SOCKS5 reply has wrong version, version should be 5.");
  596. return CURLE_COULDNT_CONNECT;
  597. }
  598. /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
  599. 1928, so the reply packet should be read until the end to avoid errors at
  600. subsequent protocol level.
  601. +----+-----+-------+------+----------+----------+
  602. |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
  603. +----+-----+-------+------+----------+----------+
  604. | 1 | 1 | X'00' | 1 | Variable | 2 |
  605. +----+-----+-------+------+----------+----------+
  606. ATYP:
  607. o IP v4 address: X'01', BND.ADDR = 4 byte
  608. o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
  609. o IP v6 address: X'04', BND.ADDR = 16 byte
  610. */
  611. /* Calculate real packet size */
  612. if(socksreq[3] == 3) {
  613. /* domain name */
  614. int addrlen = (int) socksreq[4];
  615. len = 5 + addrlen + 2;
  616. }
  617. else if(socksreq[3] == 4) {
  618. /* IPv6 */
  619. len = 4 + 16 + 2;
  620. }
  621. /* At this point we already read first 10 bytes */
  622. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  623. if(!conn->socks5_gssapi_enctype) {
  624. /* decrypt_gssapi_blockread already read the whole packet */
  625. #endif
  626. if(len > 10) {
  627. result = Curl_blockread_all(conn, sock, (char *)&socksreq[10],
  628. len - 10, &actualread);
  629. if(result || ((len - 10) != actualread)) {
  630. failf(data, "Failed to receive SOCKS5 connect request ack.");
  631. return CURLE_COULDNT_CONNECT;
  632. }
  633. }
  634. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  635. }
  636. #endif
  637. if(socksreq[1] != 0) { /* Anything besides 0 is an error */
  638. if(socksreq[3] == 1) {
  639. failf(data,
  640. "Can't complete SOCKS5 connection to %d.%d.%d.%d:%d. (%d)",
  641. (unsigned char)socksreq[4], (unsigned char)socksreq[5],
  642. (unsigned char)socksreq[6], (unsigned char)socksreq[7],
  643. (((unsigned char)socksreq[8] << 8) |
  644. (unsigned char)socksreq[9]),
  645. (unsigned char)socksreq[1]);
  646. }
  647. else if(socksreq[3] == 3) {
  648. unsigned char port_upper = (unsigned char)socksreq[len - 2];
  649. socksreq[len - 2] = 0;
  650. failf(data,
  651. "Can't complete SOCKS5 connection to %s:%d. (%d)",
  652. (char *)&socksreq[5],
  653. ((port_upper << 8) |
  654. (unsigned char)socksreq[len - 1]),
  655. (unsigned char)socksreq[1]);
  656. socksreq[len - 2] = port_upper;
  657. }
  658. else if(socksreq[3] == 4) {
  659. failf(data,
  660. "Can't complete SOCKS5 connection to %02x%02x:%02x%02x:"
  661. "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%d. (%d)",
  662. (unsigned char)socksreq[4], (unsigned char)socksreq[5],
  663. (unsigned char)socksreq[6], (unsigned char)socksreq[7],
  664. (unsigned char)socksreq[8], (unsigned char)socksreq[9],
  665. (unsigned char)socksreq[10], (unsigned char)socksreq[11],
  666. (unsigned char)socksreq[12], (unsigned char)socksreq[13],
  667. (unsigned char)socksreq[14], (unsigned char)socksreq[15],
  668. (unsigned char)socksreq[16], (unsigned char)socksreq[17],
  669. (unsigned char)socksreq[18], (unsigned char)socksreq[19],
  670. (((unsigned char)socksreq[20] << 8) |
  671. (unsigned char)socksreq[21]),
  672. (unsigned char)socksreq[1]);
  673. }
  674. return CURLE_COULDNT_CONNECT;
  675. }
  676. else {
  677. infof(data, "SOCKS5 request granted.\n");
  678. }
  679. (void)curlx_nonblock(sock, TRUE);
  680. return CURLE_OK; /* Proxy was successful! */
  681. }
  682. #endif /* CURL_DISABLE_PROXY */