socks.c 38 KB

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