socks.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_PROXY)
  26. #ifdef HAVE_NETINET_IN_H
  27. #include <netinet/in.h>
  28. #endif
  29. #ifdef HAVE_ARPA_INET_H
  30. #include <arpa/inet.h>
  31. #endif
  32. #include "urldata.h"
  33. #include "sendf.h"
  34. #include "select.h"
  35. #include "cfilters.h"
  36. #include "connect.h"
  37. #include "timeval.h"
  38. #include "socks.h"
  39. #include "multiif.h" /* for getsock macros */
  40. #include "inet_pton.h"
  41. #include "url.h"
  42. /* The last 3 #include files should be in this order */
  43. #include "curl_printf.h"
  44. #include "curl_memory.h"
  45. #include "memdebug.h"
  46. /* for the (SOCKS) connect state machine */
  47. enum connect_t {
  48. CONNECT_INIT,
  49. CONNECT_SOCKS_INIT, /* 1 */
  50. CONNECT_SOCKS_SEND, /* 2 waiting to send more first data */
  51. CONNECT_SOCKS_READ_INIT, /* 3 set up read */
  52. CONNECT_SOCKS_READ, /* 4 read server response */
  53. CONNECT_GSSAPI_INIT, /* 5 */
  54. CONNECT_AUTH_INIT, /* 6 setup outgoing auth buffer */
  55. CONNECT_AUTH_SEND, /* 7 send auth */
  56. CONNECT_AUTH_READ, /* 8 read auth response */
  57. CONNECT_REQ_INIT, /* 9 init SOCKS "request" */
  58. CONNECT_RESOLVING, /* 10 */
  59. CONNECT_RESOLVED, /* 11 */
  60. CONNECT_RESOLVE_REMOTE, /* 12 */
  61. CONNECT_REQ_SEND, /* 13 */
  62. CONNECT_REQ_SENDING, /* 14 */
  63. CONNECT_REQ_READ, /* 15 */
  64. CONNECT_REQ_READ_MORE, /* 16 */
  65. CONNECT_DONE /* 17 connected fine to the remote or the SOCKS proxy */
  66. };
  67. #define CURL_SOCKS_BUF_SIZE 600
  68. /* make sure we configure it not too low */
  69. #if CURL_SOCKS_BUF_SIZE < 600
  70. #error CURL_SOCKS_BUF_SIZE must be at least 600
  71. #endif
  72. struct socks_state {
  73. enum connect_t state;
  74. ssize_t outstanding; /* send this many bytes more */
  75. unsigned char buffer[CURL_SOCKS_BUF_SIZE];
  76. unsigned char *outp; /* send from this pointer */
  77. const char *hostname;
  78. int remote_port;
  79. const char *proxy_user;
  80. const char *proxy_password;
  81. };
  82. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  83. /*
  84. * Helper read-from-socket functions. Does the same as Curl_read() but it
  85. * blocks until all bytes amount of buffersize will be read. No more, no less.
  86. *
  87. * This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions.
  88. */
  89. int Curl_blockread_all(struct Curl_cfilter *cf,
  90. struct Curl_easy *data, /* transfer */
  91. char *buf, /* store read data here */
  92. ssize_t buffersize, /* max amount to read */
  93. ssize_t *n) /* amount bytes read */
  94. {
  95. ssize_t nread = 0;
  96. ssize_t allread = 0;
  97. int result;
  98. CURLcode err = CURLE_OK;
  99. *n = 0;
  100. for(;;) {
  101. timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  102. if(timeout_ms < 0) {
  103. /* we already got the timeout */
  104. result = CURLE_OPERATION_TIMEDOUT;
  105. break;
  106. }
  107. if(!timeout_ms)
  108. timeout_ms = TIMEDIFF_T_MAX;
  109. if(SOCKET_READABLE(cf->conn->sock[cf->sockindex], timeout_ms) <= 0) {
  110. result = ~CURLE_OK;
  111. break;
  112. }
  113. nread = Curl_conn_cf_recv(cf->next, data, buf, buffersize, &err);
  114. if(nread <= 0) {
  115. result = err;
  116. if(CURLE_AGAIN == err)
  117. continue;
  118. if(err) {
  119. break;
  120. }
  121. }
  122. if(buffersize == nread) {
  123. allread += nread;
  124. *n = allread;
  125. result = CURLE_OK;
  126. break;
  127. }
  128. if(!nread) {
  129. result = ~CURLE_OK;
  130. break;
  131. }
  132. buffersize -= nread;
  133. buf += nread;
  134. allread += nread;
  135. }
  136. return result;
  137. }
  138. #endif
  139. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  140. #define DEBUG_AND_VERBOSE
  141. #define sxstate(x,d,y) socksstate(x,d,y, __LINE__)
  142. #else
  143. #define sxstate(x,d,y) socksstate(x,d,y)
  144. #endif
  145. /* always use this function to change state, to make debugging easier */
  146. static void socksstate(struct socks_state *sx, struct Curl_easy *data,
  147. enum connect_t state
  148. #ifdef DEBUG_AND_VERBOSE
  149. , int lineno
  150. #endif
  151. )
  152. {
  153. enum connect_t oldstate = sx->state;
  154. #ifdef DEBUG_AND_VERBOSE
  155. /* synced with the state list in urldata.h */
  156. static const char * const socks_statename[] = {
  157. "INIT",
  158. "SOCKS_INIT",
  159. "SOCKS_SEND",
  160. "SOCKS_READ_INIT",
  161. "SOCKS_READ",
  162. "GSSAPI_INIT",
  163. "AUTH_INIT",
  164. "AUTH_SEND",
  165. "AUTH_READ",
  166. "REQ_INIT",
  167. "RESOLVING",
  168. "RESOLVED",
  169. "RESOLVE_REMOTE",
  170. "REQ_SEND",
  171. "REQ_SENDING",
  172. "REQ_READ",
  173. "REQ_READ_MORE",
  174. "DONE"
  175. };
  176. #endif
  177. (void)data;
  178. if(oldstate == state)
  179. /* don't bother when the new state is the same as the old state */
  180. return;
  181. sx->state = state;
  182. #ifdef DEBUG_AND_VERBOSE
  183. infof(data,
  184. "SXSTATE: %s => %s; line %d",
  185. socks_statename[oldstate], socks_statename[sx->state],
  186. lineno);
  187. #endif
  188. }
  189. static CURLproxycode socks_state_send(struct Curl_cfilter *cf,
  190. struct socks_state *sx,
  191. struct Curl_easy *data,
  192. CURLproxycode failcode,
  193. const char *description)
  194. {
  195. ssize_t nwritten;
  196. CURLcode result;
  197. nwritten = Curl_conn_cf_send(cf->next, data, (char *)sx->outp,
  198. sx->outstanding, &result);
  199. if(nwritten <= 0) {
  200. if(CURLE_AGAIN == result) {
  201. return CURLPX_OK;
  202. }
  203. else if(CURLE_OK == result) {
  204. /* connection closed */
  205. failf(data, "connection to proxy closed");
  206. return CURLPX_CLOSED;
  207. }
  208. failf(data, "Failed to send %s: %s", description,
  209. curl_easy_strerror(result));
  210. return failcode;
  211. }
  212. DEBUGASSERT(sx->outstanding >= nwritten);
  213. /* not done, remain in state */
  214. sx->outstanding -= nwritten;
  215. sx->outp += nwritten;
  216. return CURLPX_OK;
  217. }
  218. static CURLproxycode socks_state_recv(struct Curl_cfilter *cf,
  219. struct socks_state *sx,
  220. struct Curl_easy *data,
  221. CURLproxycode failcode,
  222. const char *description)
  223. {
  224. ssize_t nread;
  225. CURLcode result;
  226. nread = Curl_conn_cf_recv(cf->next, data, (char *)sx->outp,
  227. sx->outstanding, &result);
  228. if(nread <= 0) {
  229. if(CURLE_AGAIN == result) {
  230. return CURLPX_OK;
  231. }
  232. else if(CURLE_OK == result) {
  233. /* connection closed */
  234. failf(data, "connection to proxy closed");
  235. return CURLPX_CLOSED;
  236. }
  237. failf(data, "SOCKS: Failed receiving %s: %s", description,
  238. curl_easy_strerror(result));
  239. return failcode;
  240. }
  241. /* remain in reading state */
  242. DEBUGASSERT(sx->outstanding >= nread);
  243. sx->outstanding -= nread;
  244. sx->outp += nread;
  245. return CURLPX_OK;
  246. }
  247. /*
  248. * This function logs in to a SOCKS4 proxy and sends the specifics to the final
  249. * destination server.
  250. *
  251. * Reference :
  252. * https://www.openssh.com/txt/socks4.protocol
  253. *
  254. * Note :
  255. * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
  256. * Nonsupport "Identification Protocol (RFC1413)"
  257. */
  258. static CURLproxycode do_SOCKS4(struct Curl_cfilter *cf,
  259. struct socks_state *sx,
  260. struct Curl_easy *data)
  261. {
  262. struct connectdata *conn = cf->conn;
  263. const bool protocol4a =
  264. (conn->socks_proxy.proxytype == CURLPROXY_SOCKS4A) ? TRUE : FALSE;
  265. unsigned char *socksreq = sx->buffer;
  266. CURLcode result;
  267. CURLproxycode presult;
  268. struct Curl_dns_entry *dns = NULL;
  269. switch(sx->state) {
  270. case CONNECT_SOCKS_INIT:
  271. /* SOCKS4 can only do IPv4, insist! */
  272. conn->ip_version = CURL_IPRESOLVE_V4;
  273. if(conn->bits.httpproxy)
  274. infof(data, "SOCKS4%s: connecting to HTTP proxy %s port %d",
  275. protocol4a ? "a" : "", sx->hostname, sx->remote_port);
  276. infof(data, "SOCKS4 communication to %s:%d",
  277. sx->hostname, sx->remote_port);
  278. /*
  279. * Compose socks4 request
  280. *
  281. * Request format
  282. *
  283. * +----+----+----+----+----+----+----+----+----+----+....+----+
  284. * | VN | CD | DSTPORT | DSTIP | USERID |NULL|
  285. * +----+----+----+----+----+----+----+----+----+----+....+----+
  286. * # of bytes: 1 1 2 4 variable 1
  287. */
  288. socksreq[0] = 4; /* version (SOCKS4) */
  289. socksreq[1] = 1; /* connect */
  290. socksreq[2] = (unsigned char)((sx->remote_port >> 8) & 0xff); /* MSB */
  291. socksreq[3] = (unsigned char)(sx->remote_port & 0xff); /* LSB */
  292. /* DNS resolve only for SOCKS4, not SOCKS4a */
  293. if(!protocol4a) {
  294. enum resolve_t rc =
  295. Curl_resolv(data, sx->hostname, sx->remote_port, TRUE, &dns);
  296. if(rc == CURLRESOLV_ERROR)
  297. return CURLPX_RESOLVE_HOST;
  298. else if(rc == CURLRESOLV_PENDING) {
  299. sxstate(sx, data, CONNECT_RESOLVING);
  300. infof(data, "SOCKS4 non-blocking resolve of %s", sx->hostname);
  301. return CURLPX_OK;
  302. }
  303. sxstate(sx, data, CONNECT_RESOLVED);
  304. goto CONNECT_RESOLVED;
  305. }
  306. /* socks4a doesn't resolve anything locally */
  307. sxstate(sx, data, CONNECT_REQ_INIT);
  308. goto CONNECT_REQ_INIT;
  309. case CONNECT_RESOLVING:
  310. /* check if we have the name resolved by now */
  311. dns = Curl_fetch_addr(data, sx->hostname, conn->primary.remote_port);
  312. if(dns) {
  313. #ifdef CURLRES_ASYNCH
  314. data->state.async.dns = dns;
  315. data->state.async.done = TRUE;
  316. #endif
  317. infof(data, "Hostname '%s' was found", sx->hostname);
  318. sxstate(sx, data, CONNECT_RESOLVED);
  319. }
  320. else {
  321. result = Curl_resolv_check(data, &dns);
  322. if(!dns) {
  323. if(result)
  324. return CURLPX_RESOLVE_HOST;
  325. return CURLPX_OK;
  326. }
  327. }
  328. FALLTHROUGH();
  329. case CONNECT_RESOLVED:
  330. CONNECT_RESOLVED:
  331. {
  332. struct Curl_addrinfo *hp = NULL;
  333. /*
  334. * We cannot use 'hostent' as a struct that Curl_resolv() returns. It
  335. * returns a Curl_addrinfo pointer that may not always look the same.
  336. */
  337. if(dns) {
  338. hp = dns->addr;
  339. /* scan for the first IPv4 address */
  340. while(hp && (hp->ai_family != AF_INET))
  341. hp = hp->ai_next;
  342. if(hp) {
  343. struct sockaddr_in *saddr_in;
  344. char buf[64];
  345. Curl_printable_address(hp, buf, sizeof(buf));
  346. saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
  347. socksreq[4] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[0];
  348. socksreq[5] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[1];
  349. socksreq[6] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[2];
  350. socksreq[7] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[3];
  351. infof(data, "SOCKS4 connect to IPv4 %s (locally resolved)", buf);
  352. Curl_resolv_unlock(data, dns); /* not used anymore from now on */
  353. }
  354. else
  355. failf(data, "SOCKS4 connection to %s not supported", sx->hostname);
  356. }
  357. else
  358. failf(data, "Failed to resolve \"%s\" for SOCKS4 connect.",
  359. sx->hostname);
  360. if(!hp)
  361. return CURLPX_RESOLVE_HOST;
  362. }
  363. FALLTHROUGH();
  364. case CONNECT_REQ_INIT:
  365. CONNECT_REQ_INIT:
  366. /*
  367. * This is currently not supporting "Identification Protocol (RFC1413)".
  368. */
  369. socksreq[8] = 0; /* ensure empty userid is NUL-terminated */
  370. if(sx->proxy_user) {
  371. size_t plen = strlen(sx->proxy_user);
  372. if(plen > 255) {
  373. /* there is no real size limit to this field in the protocol, but
  374. SOCKS5 limits the proxy user field to 255 bytes and it seems likely
  375. that a longer field is either a mistake or malicious input */
  376. failf(data, "Too long SOCKS proxy user name");
  377. return CURLPX_LONG_USER;
  378. }
  379. /* copy the proxy name WITH trailing zero */
  380. memcpy(socksreq + 8, sx->proxy_user, plen + 1);
  381. }
  382. /*
  383. * Make connection
  384. */
  385. {
  386. size_t packetsize = 9 +
  387. strlen((char *)socksreq + 8); /* size including NUL */
  388. /* If SOCKS4a, set special invalid IP address 0.0.0.x */
  389. if(protocol4a) {
  390. size_t hostnamelen = 0;
  391. socksreq[4] = 0;
  392. socksreq[5] = 0;
  393. socksreq[6] = 0;
  394. socksreq[7] = 1;
  395. /* append hostname */
  396. hostnamelen = strlen(sx->hostname) + 1; /* length including NUL */
  397. if((hostnamelen <= 255) &&
  398. (packetsize + hostnamelen < sizeof(sx->buffer)))
  399. strcpy((char *)socksreq + packetsize, sx->hostname);
  400. else {
  401. failf(data, "SOCKS4: too long host name");
  402. return CURLPX_LONG_HOSTNAME;
  403. }
  404. packetsize += hostnamelen;
  405. }
  406. sx->outp = socksreq;
  407. DEBUGASSERT(packetsize <= sizeof(sx->buffer));
  408. sx->outstanding = packetsize;
  409. sxstate(sx, data, CONNECT_REQ_SENDING);
  410. }
  411. FALLTHROUGH();
  412. case CONNECT_REQ_SENDING:
  413. /* Send request */
  414. presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
  415. "SOCKS4 connect request");
  416. if(CURLPX_OK != presult)
  417. return presult;
  418. else if(sx->outstanding) {
  419. /* remain in sending state */
  420. return CURLPX_OK;
  421. }
  422. /* done sending! */
  423. sx->outstanding = 8; /* receive data size */
  424. sx->outp = socksreq;
  425. sxstate(sx, data, CONNECT_SOCKS_READ);
  426. FALLTHROUGH();
  427. case CONNECT_SOCKS_READ:
  428. /* Receive response */
  429. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
  430. "connect request ack");
  431. if(CURLPX_OK != presult)
  432. return presult;
  433. else if(sx->outstanding) {
  434. /* remain in reading state */
  435. return CURLPX_OK;
  436. }
  437. sxstate(sx, data, CONNECT_DONE);
  438. break;
  439. default: /* lots of unused states in SOCKS4 */
  440. break;
  441. }
  442. /*
  443. * Response format
  444. *
  445. * +----+----+----+----+----+----+----+----+
  446. * | VN | CD | DSTPORT | DSTIP |
  447. * +----+----+----+----+----+----+----+----+
  448. * # of bytes: 1 1 2 4
  449. *
  450. * VN is the version of the reply code and should be 0. CD is the result
  451. * code with one of the following values:
  452. *
  453. * 90: request granted
  454. * 91: request rejected or failed
  455. * 92: request rejected because SOCKS server cannot connect to
  456. * identd on the client
  457. * 93: request rejected because the client program and identd
  458. * report different user-ids
  459. */
  460. /* wrong version ? */
  461. if(socksreq[0]) {
  462. failf(data,
  463. "SOCKS4 reply has wrong version, version should be 0.");
  464. return CURLPX_BAD_VERSION;
  465. }
  466. /* Result */
  467. switch(socksreq[1]) {
  468. case 90:
  469. infof(data, "SOCKS4%s request granted.", protocol4a?"a":"");
  470. break;
  471. case 91:
  472. failf(data,
  473. "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
  474. ", request rejected or failed.",
  475. socksreq[4], socksreq[5], socksreq[6], socksreq[7],
  476. (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
  477. (unsigned char)socksreq[1]);
  478. return CURLPX_REQUEST_FAILED;
  479. case 92:
  480. failf(data,
  481. "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
  482. ", request rejected because SOCKS server cannot connect to "
  483. "identd on the client.",
  484. socksreq[4], socksreq[5], socksreq[6], socksreq[7],
  485. (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
  486. (unsigned char)socksreq[1]);
  487. return CURLPX_IDENTD;
  488. case 93:
  489. failf(data,
  490. "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
  491. ", request rejected because the client program and identd "
  492. "report different user-ids.",
  493. socksreq[4], socksreq[5], socksreq[6], socksreq[7],
  494. (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
  495. (unsigned char)socksreq[1]);
  496. return CURLPX_IDENTD_DIFFER;
  497. default:
  498. failf(data,
  499. "Can't complete SOCKS4 connection to %d.%d.%d.%d:%d. (%d)"
  500. ", Unknown.",
  501. socksreq[4], socksreq[5], socksreq[6], socksreq[7],
  502. (((unsigned char)socksreq[2] << 8) | (unsigned char)socksreq[3]),
  503. (unsigned char)socksreq[1]);
  504. return CURLPX_UNKNOWN_FAIL;
  505. }
  506. return CURLPX_OK; /* Proxy was successful! */
  507. }
  508. /*
  509. * This function logs in to a SOCKS5 proxy and sends the specifics to the final
  510. * destination server.
  511. */
  512. static CURLproxycode do_SOCKS5(struct Curl_cfilter *cf,
  513. struct socks_state *sx,
  514. struct Curl_easy *data)
  515. {
  516. /*
  517. According to the RFC1928, section "6. Replies". This is what a SOCK5
  518. replies:
  519. +----+-----+-------+------+----------+----------+
  520. |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
  521. +----+-----+-------+------+----------+----------+
  522. | 1 | 1 | X'00' | 1 | Variable | 2 |
  523. +----+-----+-------+------+----------+----------+
  524. Where:
  525. o VER protocol version: X'05'
  526. o REP Reply field:
  527. o X'00' succeeded
  528. */
  529. struct connectdata *conn = cf->conn;
  530. unsigned char *socksreq = sx->buffer;
  531. size_t idx;
  532. CURLcode result;
  533. CURLproxycode presult;
  534. bool socks5_resolve_local =
  535. (conn->socks_proxy.proxytype == CURLPROXY_SOCKS5) ? TRUE : FALSE;
  536. const size_t hostname_len = strlen(sx->hostname);
  537. size_t len = 0;
  538. const unsigned char auth = data->set.socks5auth;
  539. bool allow_gssapi = FALSE;
  540. struct Curl_dns_entry *dns = NULL;
  541. DEBUGASSERT(auth & (CURLAUTH_BASIC | CURLAUTH_GSSAPI));
  542. switch(sx->state) {
  543. case CONNECT_SOCKS_INIT:
  544. if(conn->bits.httpproxy)
  545. infof(data, "SOCKS5: connecting to HTTP proxy %s port %d",
  546. sx->hostname, sx->remote_port);
  547. /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
  548. if(!socks5_resolve_local && hostname_len > 255) {
  549. failf(data, "SOCKS5: the destination hostname is too long to be "
  550. "resolved remotely by the proxy.");
  551. return CURLPX_LONG_HOSTNAME;
  552. }
  553. if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
  554. infof(data,
  555. "warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %u",
  556. auth);
  557. if(!(auth & CURLAUTH_BASIC))
  558. /* disable username/password auth */
  559. sx->proxy_user = NULL;
  560. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  561. if(auth & CURLAUTH_GSSAPI)
  562. allow_gssapi = TRUE;
  563. #endif
  564. idx = 0;
  565. socksreq[idx++] = 5; /* version */
  566. idx++; /* number of authentication methods */
  567. socksreq[idx++] = 0; /* no authentication */
  568. if(allow_gssapi)
  569. socksreq[idx++] = 1; /* GSS-API */
  570. if(sx->proxy_user)
  571. socksreq[idx++] = 2; /* username/password */
  572. /* write the number of authentication methods */
  573. socksreq[1] = (unsigned char) (idx - 2);
  574. sx->outp = socksreq;
  575. DEBUGASSERT(idx <= sizeof(sx->buffer));
  576. sx->outstanding = idx;
  577. presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
  578. "initial SOCKS5 request");
  579. if(CURLPX_OK != presult)
  580. return presult;
  581. else if(sx->outstanding) {
  582. /* remain in sending state */
  583. return CURLPX_OK;
  584. }
  585. sxstate(sx, data, CONNECT_SOCKS_READ);
  586. goto CONNECT_SOCKS_READ_INIT;
  587. case CONNECT_SOCKS_SEND:
  588. presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
  589. "initial SOCKS5 request");
  590. if(CURLPX_OK != presult)
  591. return presult;
  592. else if(sx->outstanding) {
  593. /* remain in sending state */
  594. return CURLPX_OK;
  595. }
  596. FALLTHROUGH();
  597. case CONNECT_SOCKS_READ_INIT:
  598. CONNECT_SOCKS_READ_INIT:
  599. sx->outstanding = 2; /* expect two bytes */
  600. sx->outp = socksreq; /* store it here */
  601. FALLTHROUGH();
  602. case CONNECT_SOCKS_READ:
  603. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
  604. "initial SOCKS5 response");
  605. if(CURLPX_OK != presult)
  606. return presult;
  607. else if(sx->outstanding) {
  608. /* remain in reading state */
  609. return CURLPX_OK;
  610. }
  611. else if(socksreq[0] != 5) {
  612. failf(data, "Received invalid version in initial SOCKS5 response.");
  613. return CURLPX_BAD_VERSION;
  614. }
  615. else if(socksreq[1] == 0) {
  616. /* DONE! No authentication needed. Send request. */
  617. sxstate(sx, data, CONNECT_REQ_INIT);
  618. goto CONNECT_REQ_INIT;
  619. }
  620. else if(socksreq[1] == 2) {
  621. /* regular name + password authentication */
  622. sxstate(sx, data, CONNECT_AUTH_INIT);
  623. goto CONNECT_AUTH_INIT;
  624. }
  625. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  626. else if(allow_gssapi && (socksreq[1] == 1)) {
  627. sxstate(sx, data, CONNECT_GSSAPI_INIT);
  628. result = Curl_SOCKS5_gssapi_negotiate(cf, data);
  629. if(result) {
  630. failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
  631. return CURLPX_GSSAPI;
  632. }
  633. }
  634. #endif
  635. else {
  636. /* error */
  637. if(!allow_gssapi && (socksreq[1] == 1)) {
  638. failf(data,
  639. "SOCKS5 GSSAPI per-message authentication is not supported.");
  640. return CURLPX_GSSAPI_PERMSG;
  641. }
  642. else if(socksreq[1] == 255) {
  643. failf(data, "No authentication method was acceptable.");
  644. return CURLPX_NO_AUTH;
  645. }
  646. }
  647. failf(data,
  648. "Undocumented SOCKS5 mode attempted to be used by server.");
  649. return CURLPX_UNKNOWN_MODE;
  650. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  651. case CONNECT_GSSAPI_INIT:
  652. /* GSSAPI stuff done non-blocking */
  653. break;
  654. #endif
  655. default: /* do nothing! */
  656. break;
  657. CONNECT_AUTH_INIT:
  658. case CONNECT_AUTH_INIT: {
  659. /* Needs user name and password */
  660. size_t proxy_user_len, proxy_password_len;
  661. if(sx->proxy_user && sx->proxy_password) {
  662. proxy_user_len = strlen(sx->proxy_user);
  663. proxy_password_len = strlen(sx->proxy_password);
  664. }
  665. else {
  666. proxy_user_len = 0;
  667. proxy_password_len = 0;
  668. }
  669. /* username/password request looks like
  670. * +----+------+----------+------+----------+
  671. * |VER | ULEN | UNAME | PLEN | PASSWD |
  672. * +----+------+----------+------+----------+
  673. * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
  674. * +----+------+----------+------+----------+
  675. */
  676. len = 0;
  677. socksreq[len++] = 1; /* username/pw subnegotiation version */
  678. socksreq[len++] = (unsigned char) proxy_user_len;
  679. if(sx->proxy_user && proxy_user_len) {
  680. /* the length must fit in a single byte */
  681. if(proxy_user_len > 255) {
  682. failf(data, "Excessive user name length for proxy auth");
  683. return CURLPX_LONG_USER;
  684. }
  685. memcpy(socksreq + len, sx->proxy_user, proxy_user_len);
  686. }
  687. len += proxy_user_len;
  688. socksreq[len++] = (unsigned char) proxy_password_len;
  689. if(sx->proxy_password && proxy_password_len) {
  690. /* the length must fit in a single byte */
  691. if(proxy_password_len > 255) {
  692. failf(data, "Excessive password length for proxy auth");
  693. return CURLPX_LONG_PASSWD;
  694. }
  695. memcpy(socksreq + len, sx->proxy_password, proxy_password_len);
  696. }
  697. len += proxy_password_len;
  698. sxstate(sx, data, CONNECT_AUTH_SEND);
  699. DEBUGASSERT(len <= sizeof(sx->buffer));
  700. sx->outstanding = len;
  701. sx->outp = socksreq;
  702. }
  703. FALLTHROUGH();
  704. case CONNECT_AUTH_SEND:
  705. presult = socks_state_send(cf, sx, data, CURLPX_SEND_AUTH,
  706. "SOCKS5 sub-negotiation request");
  707. if(CURLPX_OK != presult)
  708. return presult;
  709. else if(sx->outstanding) {
  710. /* remain in sending state */
  711. return CURLPX_OK;
  712. }
  713. sx->outp = socksreq;
  714. sx->outstanding = 2;
  715. sxstate(sx, data, CONNECT_AUTH_READ);
  716. FALLTHROUGH();
  717. case CONNECT_AUTH_READ:
  718. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_AUTH,
  719. "SOCKS5 sub-negotiation response");
  720. if(CURLPX_OK != presult)
  721. return presult;
  722. else if(sx->outstanding) {
  723. /* remain in reading state */
  724. return CURLPX_OK;
  725. }
  726. /* ignore the first (VER) byte */
  727. else if(socksreq[1]) { /* status */
  728. failf(data, "User was rejected by the SOCKS5 server (%d %d).",
  729. socksreq[0], socksreq[1]);
  730. return CURLPX_USER_REJECTED;
  731. }
  732. /* Everything is good so far, user was authenticated! */
  733. sxstate(sx, data, CONNECT_REQ_INIT);
  734. FALLTHROUGH();
  735. case CONNECT_REQ_INIT:
  736. CONNECT_REQ_INIT:
  737. if(socks5_resolve_local) {
  738. enum resolve_t rc = Curl_resolv(data, sx->hostname, sx->remote_port,
  739. TRUE, &dns);
  740. if(rc == CURLRESOLV_ERROR)
  741. return CURLPX_RESOLVE_HOST;
  742. if(rc == CURLRESOLV_PENDING) {
  743. sxstate(sx, data, CONNECT_RESOLVING);
  744. return CURLPX_OK;
  745. }
  746. sxstate(sx, data, CONNECT_RESOLVED);
  747. goto CONNECT_RESOLVED;
  748. }
  749. goto CONNECT_RESOLVE_REMOTE;
  750. case CONNECT_RESOLVING:
  751. /* check if we have the name resolved by now */
  752. dns = Curl_fetch_addr(data, sx->hostname, sx->remote_port);
  753. if(dns) {
  754. #ifdef CURLRES_ASYNCH
  755. data->state.async.dns = dns;
  756. data->state.async.done = TRUE;
  757. #endif
  758. infof(data, "SOCKS5: hostname '%s' found", sx->hostname);
  759. }
  760. if(!dns) {
  761. result = Curl_resolv_check(data, &dns);
  762. if(!dns) {
  763. if(result)
  764. return CURLPX_RESOLVE_HOST;
  765. return CURLPX_OK;
  766. }
  767. }
  768. FALLTHROUGH();
  769. case CONNECT_RESOLVED:
  770. CONNECT_RESOLVED:
  771. {
  772. char dest[MAX_IPADR_LEN]; /* printable address */
  773. struct Curl_addrinfo *hp = NULL;
  774. if(dns)
  775. hp = dns->addr;
  776. #ifdef USE_IPV6
  777. if(data->set.ipver != CURL_IPRESOLVE_WHATEVER) {
  778. int wanted_family = data->set.ipver == CURL_IPRESOLVE_V4 ?
  779. AF_INET : AF_INET6;
  780. /* scan for the first proper address */
  781. while(hp && (hp->ai_family != wanted_family))
  782. hp = hp->ai_next;
  783. }
  784. #endif
  785. if(!hp) {
  786. failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
  787. sx->hostname);
  788. return CURLPX_RESOLVE_HOST;
  789. }
  790. Curl_printable_address(hp, dest, sizeof(dest));
  791. len = 0;
  792. socksreq[len++] = 5; /* version (SOCKS5) */
  793. socksreq[len++] = 1; /* connect */
  794. socksreq[len++] = 0; /* must be zero */
  795. if(hp->ai_family == AF_INET) {
  796. int i;
  797. struct sockaddr_in *saddr_in;
  798. socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
  799. saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
  800. for(i = 0; i < 4; i++) {
  801. socksreq[len++] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[i];
  802. }
  803. infof(data, "SOCKS5 connect to %s:%d (locally resolved)", dest,
  804. sx->remote_port);
  805. }
  806. #ifdef USE_IPV6
  807. else if(hp->ai_family == AF_INET6) {
  808. int i;
  809. struct sockaddr_in6 *saddr_in6;
  810. socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
  811. saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr;
  812. for(i = 0; i < 16; i++) {
  813. socksreq[len++] =
  814. ((unsigned char *)&saddr_in6->sin6_addr.s6_addr)[i];
  815. }
  816. infof(data, "SOCKS5 connect to [%s]:%d (locally resolved)", dest,
  817. sx->remote_port);
  818. }
  819. #endif
  820. else {
  821. hp = NULL; /* fail! */
  822. failf(data, "SOCKS5 connection to %s not supported", dest);
  823. }
  824. Curl_resolv_unlock(data, dns); /* not used anymore from now on */
  825. goto CONNECT_REQ_SEND;
  826. }
  827. CONNECT_RESOLVE_REMOTE:
  828. case CONNECT_RESOLVE_REMOTE:
  829. /* Authentication is complete, now specify destination to the proxy */
  830. len = 0;
  831. socksreq[len++] = 5; /* version (SOCKS5) */
  832. socksreq[len++] = 1; /* connect */
  833. socksreq[len++] = 0; /* must be zero */
  834. if(!socks5_resolve_local) {
  835. /* ATYP: domain name = 3,
  836. IPv6 == 4,
  837. IPv4 == 1 */
  838. unsigned char ip4[4];
  839. #ifdef USE_IPV6
  840. if(conn->bits.ipv6_ip) {
  841. char ip6[16];
  842. if(1 != Curl_inet_pton(AF_INET6, sx->hostname, ip6))
  843. return CURLPX_BAD_ADDRESS_TYPE;
  844. socksreq[len++] = 4;
  845. memcpy(&socksreq[len], ip6, sizeof(ip6));
  846. len += sizeof(ip6);
  847. }
  848. else
  849. #endif
  850. if(1 == Curl_inet_pton(AF_INET, sx->hostname, ip4)) {
  851. socksreq[len++] = 1;
  852. memcpy(&socksreq[len], ip4, sizeof(ip4));
  853. len += sizeof(ip4);
  854. }
  855. else {
  856. socksreq[len++] = 3;
  857. socksreq[len++] = (unsigned char) hostname_len; /* one byte length */
  858. memcpy(&socksreq[len], sx->hostname, hostname_len); /* w/o NULL */
  859. len += hostname_len;
  860. }
  861. infof(data, "SOCKS5 connect to %s:%d (remotely resolved)",
  862. sx->hostname, sx->remote_port);
  863. }
  864. FALLTHROUGH();
  865. case CONNECT_REQ_SEND:
  866. CONNECT_REQ_SEND:
  867. /* PORT MSB */
  868. socksreq[len++] = (unsigned char)((sx->remote_port >> 8) & 0xff);
  869. /* PORT LSB */
  870. socksreq[len++] = (unsigned char)(sx->remote_port & 0xff);
  871. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  872. if(conn->socks5_gssapi_enctype) {
  873. failf(data, "SOCKS5 GSS-API protection not yet implemented.");
  874. return CURLPX_GSSAPI_PROTECTION;
  875. }
  876. #endif
  877. sx->outp = socksreq;
  878. DEBUGASSERT(len <= sizeof(sx->buffer));
  879. sx->outstanding = len;
  880. sxstate(sx, data, CONNECT_REQ_SENDING);
  881. FALLTHROUGH();
  882. case CONNECT_REQ_SENDING:
  883. presult = socks_state_send(cf, sx, data, CURLPX_SEND_REQUEST,
  884. "SOCKS5 connect request");
  885. if(CURLPX_OK != presult)
  886. return presult;
  887. else if(sx->outstanding) {
  888. /* remain in send state */
  889. return CURLPX_OK;
  890. }
  891. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  892. if(conn->socks5_gssapi_enctype) {
  893. failf(data, "SOCKS5 GSS-API protection not yet implemented.");
  894. return CURLPX_GSSAPI_PROTECTION;
  895. }
  896. #endif
  897. sx->outstanding = 10; /* minimum packet size is 10 */
  898. sx->outp = socksreq;
  899. sxstate(sx, data, CONNECT_REQ_READ);
  900. FALLTHROUGH();
  901. case CONNECT_REQ_READ:
  902. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_REQACK,
  903. "SOCKS5 connect request ack");
  904. if(CURLPX_OK != presult)
  905. return presult;
  906. else if(sx->outstanding) {
  907. /* remain in reading state */
  908. return CURLPX_OK;
  909. }
  910. else if(socksreq[0] != 5) { /* version */
  911. failf(data,
  912. "SOCKS5 reply has wrong version, version should be 5.");
  913. return CURLPX_BAD_VERSION;
  914. }
  915. else if(socksreq[1]) { /* Anything besides 0 is an error */
  916. CURLproxycode rc = CURLPX_REPLY_UNASSIGNED;
  917. int code = socksreq[1];
  918. failf(data, "Can't complete SOCKS5 connection to %s. (%d)",
  919. sx->hostname, (unsigned char)socksreq[1]);
  920. if(code < 9) {
  921. /* RFC 1928 section 6 lists: */
  922. static const CURLproxycode lookup[] = {
  923. CURLPX_OK,
  924. CURLPX_REPLY_GENERAL_SERVER_FAILURE,
  925. CURLPX_REPLY_NOT_ALLOWED,
  926. CURLPX_REPLY_NETWORK_UNREACHABLE,
  927. CURLPX_REPLY_HOST_UNREACHABLE,
  928. CURLPX_REPLY_CONNECTION_REFUSED,
  929. CURLPX_REPLY_TTL_EXPIRED,
  930. CURLPX_REPLY_COMMAND_NOT_SUPPORTED,
  931. CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED,
  932. };
  933. rc = lookup[code];
  934. }
  935. return rc;
  936. }
  937. /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
  938. 1928, so the reply packet should be read until the end to avoid errors
  939. at subsequent protocol level.
  940. +----+-----+-------+------+----------+----------+
  941. |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
  942. +----+-----+-------+------+----------+----------+
  943. | 1 | 1 | X'00' | 1 | Variable | 2 |
  944. +----+-----+-------+------+----------+----------+
  945. ATYP:
  946. o IP v4 address: X'01', BND.ADDR = 4 byte
  947. o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
  948. o IP v6 address: X'04', BND.ADDR = 16 byte
  949. */
  950. /* Calculate real packet size */
  951. if(socksreq[3] == 3) {
  952. /* domain name */
  953. int addrlen = (int) socksreq[4];
  954. len = 5 + addrlen + 2;
  955. }
  956. else if(socksreq[3] == 4) {
  957. /* IPv6 */
  958. len = 4 + 16 + 2;
  959. }
  960. else if(socksreq[3] == 1) {
  961. len = 4 + 4 + 2;
  962. }
  963. else {
  964. failf(data, "SOCKS5 reply has wrong address type.");
  965. return CURLPX_BAD_ADDRESS_TYPE;
  966. }
  967. /* At this point we already read first 10 bytes */
  968. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  969. if(!conn->socks5_gssapi_enctype) {
  970. /* decrypt_gssapi_blockread already read the whole packet */
  971. #endif
  972. if(len > 10) {
  973. DEBUGASSERT(len <= sizeof(sx->buffer));
  974. sx->outstanding = len - 10; /* get the rest */
  975. sx->outp = &socksreq[10];
  976. sxstate(sx, data, CONNECT_REQ_READ_MORE);
  977. }
  978. else {
  979. sxstate(sx, data, CONNECT_DONE);
  980. break;
  981. }
  982. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  983. }
  984. #endif
  985. FALLTHROUGH();
  986. case CONNECT_REQ_READ_MORE:
  987. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_ADDRESS,
  988. "SOCKS5 connect request address");
  989. if(CURLPX_OK != presult)
  990. return presult;
  991. else if(sx->outstanding) {
  992. /* remain in reading state */
  993. return CURLPX_OK;
  994. }
  995. sxstate(sx, data, CONNECT_DONE);
  996. }
  997. infof(data, "SOCKS5 request granted.");
  998. return CURLPX_OK; /* Proxy was successful! */
  999. }
  1000. static CURLcode connect_SOCKS(struct Curl_cfilter *cf,
  1001. struct socks_state *sxstate,
  1002. struct Curl_easy *data)
  1003. {
  1004. CURLcode result = CURLE_OK;
  1005. CURLproxycode pxresult = CURLPX_OK;
  1006. struct connectdata *conn = cf->conn;
  1007. switch(conn->socks_proxy.proxytype) {
  1008. case CURLPROXY_SOCKS5:
  1009. case CURLPROXY_SOCKS5_HOSTNAME:
  1010. pxresult = do_SOCKS5(cf, sxstate, data);
  1011. break;
  1012. case CURLPROXY_SOCKS4:
  1013. case CURLPROXY_SOCKS4A:
  1014. pxresult = do_SOCKS4(cf, sxstate, data);
  1015. break;
  1016. default:
  1017. failf(data, "unknown proxytype option given");
  1018. result = CURLE_COULDNT_CONNECT;
  1019. } /* switch proxytype */
  1020. if(pxresult) {
  1021. result = CURLE_PROXY;
  1022. data->info.pxcode = pxresult;
  1023. }
  1024. return result;
  1025. }
  1026. static void socks_proxy_cf_free(struct Curl_cfilter *cf)
  1027. {
  1028. struct socks_state *sxstate = cf->ctx;
  1029. if(sxstate) {
  1030. free(sxstate);
  1031. cf->ctx = NULL;
  1032. }
  1033. }
  1034. /* After a TCP connection to the proxy has been verified, this function does
  1035. the next magic steps. If 'done' isn't set TRUE, it is not done yet and
  1036. must be called again.
  1037. Note: this function's sub-functions call failf()
  1038. */
  1039. static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf,
  1040. struct Curl_easy *data,
  1041. bool blocking, bool *done)
  1042. {
  1043. CURLcode result;
  1044. struct connectdata *conn = cf->conn;
  1045. int sockindex = cf->sockindex;
  1046. struct socks_state *sx = cf->ctx;
  1047. if(cf->connected) {
  1048. *done = TRUE;
  1049. return CURLE_OK;
  1050. }
  1051. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  1052. if(result || !*done)
  1053. return result;
  1054. if(!sx) {
  1055. sx = calloc(1, sizeof(*sx));
  1056. if(!sx)
  1057. return CURLE_OUT_OF_MEMORY;
  1058. cf->ctx = sx;
  1059. }
  1060. if(sx->state == CONNECT_INIT) {
  1061. /* for the secondary socket (FTP), use the "connect to host"
  1062. * but ignore the "connect to port" (use the secondary port)
  1063. */
  1064. sxstate(sx, data, CONNECT_SOCKS_INIT);
  1065. sx->hostname =
  1066. conn->bits.httpproxy ?
  1067. conn->http_proxy.host.name :
  1068. conn->bits.conn_to_host ?
  1069. conn->conn_to_host.name :
  1070. sockindex == SECONDARYSOCKET ?
  1071. conn->secondaryhostname : conn->host.name;
  1072. sx->remote_port =
  1073. conn->bits.httpproxy ? (int)conn->http_proxy.port :
  1074. sockindex == SECONDARYSOCKET ? conn->secondary_port :
  1075. conn->bits.conn_to_port ? conn->conn_to_port :
  1076. conn->remote_port;
  1077. sx->proxy_user = conn->socks_proxy.user;
  1078. sx->proxy_password = conn->socks_proxy.passwd;
  1079. }
  1080. result = connect_SOCKS(cf, sx, data);
  1081. if(!result && sx->state == CONNECT_DONE) {
  1082. cf->connected = TRUE;
  1083. Curl_verboseconnect(data, conn, cf->sockindex);
  1084. socks_proxy_cf_free(cf);
  1085. }
  1086. *done = cf->connected;
  1087. return result;
  1088. }
  1089. static void socks_cf_adjust_pollset(struct Curl_cfilter *cf,
  1090. struct Curl_easy *data,
  1091. struct easy_pollset *ps)
  1092. {
  1093. struct socks_state *sx = cf->ctx;
  1094. if(!cf->connected && sx) {
  1095. /* If we are not connected, the filter below is and has nothing
  1096. * to wait on, we determine what to wait for. */
  1097. curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
  1098. switch(sx->state) {
  1099. case CONNECT_RESOLVING:
  1100. case CONNECT_SOCKS_READ:
  1101. case CONNECT_AUTH_READ:
  1102. case CONNECT_REQ_READ:
  1103. case CONNECT_REQ_READ_MORE:
  1104. Curl_pollset_set_in_only(data, ps, sock);
  1105. break;
  1106. default:
  1107. Curl_pollset_set_out_only(data, ps, sock);
  1108. break;
  1109. }
  1110. }
  1111. }
  1112. static void socks_proxy_cf_close(struct Curl_cfilter *cf,
  1113. struct Curl_easy *data)
  1114. {
  1115. DEBUGASSERT(cf->next);
  1116. cf->connected = FALSE;
  1117. socks_proxy_cf_free(cf);
  1118. cf->next->cft->do_close(cf->next, data);
  1119. }
  1120. static void socks_proxy_cf_destroy(struct Curl_cfilter *cf,
  1121. struct Curl_easy *data)
  1122. {
  1123. (void)data;
  1124. socks_proxy_cf_free(cf);
  1125. }
  1126. static void socks_cf_get_host(struct Curl_cfilter *cf,
  1127. struct Curl_easy *data,
  1128. const char **phost,
  1129. const char **pdisplay_host,
  1130. int *pport)
  1131. {
  1132. (void)data;
  1133. if(!cf->connected) {
  1134. *phost = cf->conn->socks_proxy.host.name;
  1135. *pdisplay_host = cf->conn->http_proxy.host.dispname;
  1136. *pport = (int)cf->conn->socks_proxy.port;
  1137. }
  1138. else {
  1139. cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
  1140. }
  1141. }
  1142. struct Curl_cftype Curl_cft_socks_proxy = {
  1143. "SOCKS-PROXYY",
  1144. CF_TYPE_IP_CONNECT|CF_TYPE_PROXY,
  1145. 0,
  1146. socks_proxy_cf_destroy,
  1147. socks_proxy_cf_connect,
  1148. socks_proxy_cf_close,
  1149. socks_cf_get_host,
  1150. socks_cf_adjust_pollset,
  1151. Curl_cf_def_data_pending,
  1152. Curl_cf_def_send,
  1153. Curl_cf_def_recv,
  1154. Curl_cf_def_cntrl,
  1155. Curl_cf_def_conn_is_alive,
  1156. Curl_cf_def_conn_keep_alive,
  1157. Curl_cf_def_query,
  1158. };
  1159. CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at,
  1160. struct Curl_easy *data)
  1161. {
  1162. struct Curl_cfilter *cf;
  1163. CURLcode result;
  1164. (void)data;
  1165. result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, NULL);
  1166. if(!result)
  1167. Curl_conn_cf_insert_after(cf_at, cf);
  1168. return result;
  1169. }
  1170. #endif /* CURL_DISABLE_PROXY */