socks.c 25 KB

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