2
0

socks.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  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 socks_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. socks_statename[oldstate], socks_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. int idx;
  522. CURLcode result;
  523. CURLproxycode presult;
  524. bool socks5_resolve_local =
  525. (conn->socks_proxy.proxytype == CURLPROXY_SOCKS5) ? TRUE : FALSE;
  526. const size_t hostname_len = strlen(sx->hostname);
  527. ssize_t len = 0;
  528. const unsigned char auth = data->set.socks5auth;
  529. bool allow_gssapi = FALSE;
  530. struct Curl_dns_entry *dns = NULL;
  531. DEBUGASSERT(auth & (CURLAUTH_BASIC | CURLAUTH_GSSAPI));
  532. switch(sx->state) {
  533. case CONNECT_SOCKS_INIT:
  534. if(conn->bits.httpproxy)
  535. infof(data, "SOCKS5: connecting to HTTP proxy %s port %d",
  536. sx->hostname, sx->remote_port);
  537. /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
  538. if(!socks5_resolve_local && hostname_len > 255) {
  539. infof(data, "SOCKS5: server resolving disabled for hostnames of "
  540. "length > 255 [actual len=%zu]", hostname_len);
  541. socks5_resolve_local = TRUE;
  542. }
  543. if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
  544. infof(data,
  545. "warning: unsupported value passed to CURLOPT_SOCKS5_AUTH: %u",
  546. auth);
  547. if(!(auth & CURLAUTH_BASIC))
  548. /* disable username/password auth */
  549. sx->proxy_user = NULL;
  550. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  551. if(auth & CURLAUTH_GSSAPI)
  552. allow_gssapi = TRUE;
  553. #endif
  554. idx = 0;
  555. socksreq[idx++] = 5; /* version */
  556. idx++; /* number of authentication methods */
  557. socksreq[idx++] = 0; /* no authentication */
  558. if(allow_gssapi)
  559. socksreq[idx++] = 1; /* GSS-API */
  560. if(sx->proxy_user)
  561. socksreq[idx++] = 2; /* username/password */
  562. /* write the number of authentication methods */
  563. socksreq[1] = (unsigned char) (idx - 2);
  564. sx->outp = socksreq;
  565. sx->outstanding = idx;
  566. presult = socks_state_send(cf, sx, data, CURLPX_SEND_CONNECT,
  567. "initial SOCKS5 request");
  568. if(CURLPX_OK != presult)
  569. return presult;
  570. else if(sx->outstanding) {
  571. /* remain in sending state */
  572. return CURLPX_OK;
  573. }
  574. sxstate(sx, data, CONNECT_SOCKS_READ);
  575. goto CONNECT_SOCKS_READ_INIT;
  576. case CONNECT_SOCKS_SEND:
  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. /* FALLTHROUGH */
  586. CONNECT_SOCKS_READ_INIT:
  587. case CONNECT_SOCKS_READ_INIT:
  588. sx->outstanding = 2; /* expect two bytes */
  589. sx->outp = socksreq; /* store it here */
  590. /* FALLTHROUGH */
  591. case CONNECT_SOCKS_READ:
  592. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_CONNECT,
  593. "initial SOCKS5 response");
  594. if(CURLPX_OK != presult)
  595. return presult;
  596. else if(sx->outstanding) {
  597. /* remain in reading state */
  598. return CURLPX_OK;
  599. }
  600. else if(socksreq[0] != 5) {
  601. failf(data, "Received invalid version in initial SOCKS5 response.");
  602. return CURLPX_BAD_VERSION;
  603. }
  604. else if(socksreq[1] == 0) {
  605. /* DONE! No authentication needed. Send request. */
  606. sxstate(sx, data, CONNECT_REQ_INIT);
  607. goto CONNECT_REQ_INIT;
  608. }
  609. else if(socksreq[1] == 2) {
  610. /* regular name + password authentication */
  611. sxstate(sx, data, CONNECT_AUTH_INIT);
  612. goto CONNECT_AUTH_INIT;
  613. }
  614. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  615. else if(allow_gssapi && (socksreq[1] == 1)) {
  616. sxstate(sx, data, CONNECT_GSSAPI_INIT);
  617. result = Curl_SOCKS5_gssapi_negotiate(cf, data);
  618. if(result) {
  619. failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
  620. return CURLPX_GSSAPI;
  621. }
  622. }
  623. #endif
  624. else {
  625. /* error */
  626. if(!allow_gssapi && (socksreq[1] == 1)) {
  627. failf(data,
  628. "SOCKS5 GSSAPI per-message authentication is not supported.");
  629. return CURLPX_GSSAPI_PERMSG;
  630. }
  631. else if(socksreq[1] == 255) {
  632. failf(data, "No authentication method was acceptable.");
  633. return CURLPX_NO_AUTH;
  634. }
  635. }
  636. failf(data,
  637. "Undocumented SOCKS5 mode attempted to be used by server.");
  638. return CURLPX_UNKNOWN_MODE;
  639. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  640. case CONNECT_GSSAPI_INIT:
  641. /* GSSAPI stuff done non-blocking */
  642. break;
  643. #endif
  644. default: /* do nothing! */
  645. break;
  646. CONNECT_AUTH_INIT:
  647. case CONNECT_AUTH_INIT: {
  648. /* Needs user name and password */
  649. size_t proxy_user_len, proxy_password_len;
  650. if(sx->proxy_user && sx->proxy_password) {
  651. proxy_user_len = strlen(sx->proxy_user);
  652. proxy_password_len = strlen(sx->proxy_password);
  653. }
  654. else {
  655. proxy_user_len = 0;
  656. proxy_password_len = 0;
  657. }
  658. /* username/password request looks like
  659. * +----+------+----------+------+----------+
  660. * |VER | ULEN | UNAME | PLEN | PASSWD |
  661. * +----+------+----------+------+----------+
  662. * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
  663. * +----+------+----------+------+----------+
  664. */
  665. len = 0;
  666. socksreq[len++] = 1; /* username/pw subnegotiation version */
  667. socksreq[len++] = (unsigned char) proxy_user_len;
  668. if(sx->proxy_user && proxy_user_len) {
  669. /* the length must fit in a single byte */
  670. if(proxy_user_len > 255) {
  671. failf(data, "Excessive user name length for proxy auth");
  672. return CURLPX_LONG_USER;
  673. }
  674. memcpy(socksreq + len, sx->proxy_user, proxy_user_len);
  675. }
  676. len += proxy_user_len;
  677. socksreq[len++] = (unsigned char) proxy_password_len;
  678. if(sx->proxy_password && proxy_password_len) {
  679. /* the length must fit in a single byte */
  680. if(proxy_password_len > 255) {
  681. failf(data, "Excessive password length for proxy auth");
  682. return CURLPX_LONG_PASSWD;
  683. }
  684. memcpy(socksreq + len, sx->proxy_password, proxy_password_len);
  685. }
  686. len += proxy_password_len;
  687. sxstate(sx, data, CONNECT_AUTH_SEND);
  688. sx->outstanding = len;
  689. sx->outp = socksreq;
  690. }
  691. /* FALLTHROUGH */
  692. case CONNECT_AUTH_SEND:
  693. presult = socks_state_send(cf, sx, data, CURLPX_SEND_AUTH,
  694. "SOCKS5 sub-negotiation request");
  695. if(CURLPX_OK != presult)
  696. return presult;
  697. else if(sx->outstanding) {
  698. /* remain in sending state */
  699. return CURLPX_OK;
  700. }
  701. sx->outp = socksreq;
  702. sx->outstanding = 2;
  703. sxstate(sx, data, CONNECT_AUTH_READ);
  704. /* FALLTHROUGH */
  705. case CONNECT_AUTH_READ:
  706. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_AUTH,
  707. "SOCKS5 sub-negotiation response");
  708. if(CURLPX_OK != presult)
  709. return presult;
  710. else if(sx->outstanding) {
  711. /* remain in reading state */
  712. return CURLPX_OK;
  713. }
  714. /* ignore the first (VER) byte */
  715. else if(socksreq[1]) { /* status */
  716. failf(data, "User was rejected by the SOCKS5 server (%d %d).",
  717. socksreq[0], socksreq[1]);
  718. return CURLPX_USER_REJECTED;
  719. }
  720. /* Everything is good so far, user was authenticated! */
  721. sxstate(sx, data, CONNECT_REQ_INIT);
  722. /* FALLTHROUGH */
  723. CONNECT_REQ_INIT:
  724. case CONNECT_REQ_INIT:
  725. if(socks5_resolve_local) {
  726. enum resolve_t rc = Curl_resolv(data, sx->hostname, sx->remote_port,
  727. TRUE, &dns);
  728. if(rc == CURLRESOLV_ERROR)
  729. return CURLPX_RESOLVE_HOST;
  730. if(rc == CURLRESOLV_PENDING) {
  731. sxstate(sx, data, CONNECT_RESOLVING);
  732. return CURLPX_OK;
  733. }
  734. sxstate(sx, data, CONNECT_RESOLVED);
  735. goto CONNECT_RESOLVED;
  736. }
  737. goto CONNECT_RESOLVE_REMOTE;
  738. case CONNECT_RESOLVING:
  739. /* check if we have the name resolved by now */
  740. dns = Curl_fetch_addr(data, sx->hostname, sx->remote_port);
  741. if(dns) {
  742. #ifdef CURLRES_ASYNCH
  743. data->state.async.dns = dns;
  744. data->state.async.done = TRUE;
  745. #endif
  746. infof(data, "SOCKS5: hostname '%s' found", sx->hostname);
  747. }
  748. if(!dns) {
  749. result = Curl_resolv_check(data, &dns);
  750. if(!dns) {
  751. if(result)
  752. return CURLPX_RESOLVE_HOST;
  753. return CURLPX_OK;
  754. }
  755. }
  756. /* FALLTHROUGH */
  757. CONNECT_RESOLVED:
  758. case CONNECT_RESOLVED: {
  759. char dest[MAX_IPADR_LEN] = "unknown"; /* printable address */
  760. struct Curl_addrinfo *hp = NULL;
  761. if(dns)
  762. hp = dns->addr;
  763. if(!hp) {
  764. failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
  765. sx->hostname);
  766. return CURLPX_RESOLVE_HOST;
  767. }
  768. Curl_printable_address(hp, dest, sizeof(dest));
  769. len = 0;
  770. socksreq[len++] = 5; /* version (SOCKS5) */
  771. socksreq[len++] = 1; /* connect */
  772. socksreq[len++] = 0; /* must be zero */
  773. if(hp->ai_family == AF_INET) {
  774. int i;
  775. struct sockaddr_in *saddr_in;
  776. socksreq[len++] = 1; /* ATYP: IPv4 = 1 */
  777. saddr_in = (struct sockaddr_in *)(void *)hp->ai_addr;
  778. for(i = 0; i < 4; i++) {
  779. socksreq[len++] = ((unsigned char *)&saddr_in->sin_addr.s_addr)[i];
  780. }
  781. infof(data, "SOCKS5 connect to %s:%d (locally resolved)", dest,
  782. sx->remote_port);
  783. }
  784. #ifdef ENABLE_IPV6
  785. else if(hp->ai_family == AF_INET6) {
  786. int i;
  787. struct sockaddr_in6 *saddr_in6;
  788. socksreq[len++] = 4; /* ATYP: IPv6 = 4 */
  789. saddr_in6 = (struct sockaddr_in6 *)(void *)hp->ai_addr;
  790. for(i = 0; i < 16; i++) {
  791. socksreq[len++] =
  792. ((unsigned char *)&saddr_in6->sin6_addr.s6_addr)[i];
  793. }
  794. infof(data, "SOCKS5 connect to [%s]:%d (locally resolved)", dest,
  795. sx->remote_port);
  796. }
  797. #endif
  798. else {
  799. hp = NULL; /* fail! */
  800. failf(data, "SOCKS5 connection to %s not supported", dest);
  801. }
  802. Curl_resolv_unlock(data, dns); /* not used anymore from now on */
  803. goto CONNECT_REQ_SEND;
  804. }
  805. CONNECT_RESOLVE_REMOTE:
  806. case CONNECT_RESOLVE_REMOTE:
  807. /* Authentication is complete, now specify destination to the proxy */
  808. len = 0;
  809. socksreq[len++] = 5; /* version (SOCKS5) */
  810. socksreq[len++] = 1; /* connect */
  811. socksreq[len++] = 0; /* must be zero */
  812. if(!socks5_resolve_local) {
  813. /* ATYP: domain name = 3,
  814. IPv6 == 4,
  815. IPv4 == 1 */
  816. unsigned char ip4[4];
  817. #ifdef ENABLE_IPV6
  818. if(conn->bits.ipv6_ip) {
  819. char ip6[16];
  820. if(1 != Curl_inet_pton(AF_INET6, sx->hostname, ip6))
  821. return CURLPX_BAD_ADDRESS_TYPE;
  822. socksreq[len++] = 4;
  823. memcpy(&socksreq[len], ip6, sizeof(ip6));
  824. len += sizeof(ip6);
  825. }
  826. else
  827. #endif
  828. if(1 == Curl_inet_pton(AF_INET, sx->hostname, ip4)) {
  829. socksreq[len++] = 1;
  830. memcpy(&socksreq[len], ip4, sizeof(ip4));
  831. len += sizeof(ip4);
  832. }
  833. else {
  834. socksreq[len++] = 3;
  835. socksreq[len++] = (char) hostname_len; /* one byte address length */
  836. memcpy(&socksreq[len], sx->hostname, hostname_len); /* w/o NULL */
  837. len += hostname_len;
  838. }
  839. infof(data, "SOCKS5 connect to %s:%d (remotely resolved)",
  840. sx->hostname, sx->remote_port);
  841. }
  842. /* FALLTHROUGH */
  843. CONNECT_REQ_SEND:
  844. case CONNECT_REQ_SEND:
  845. /* PORT MSB */
  846. socksreq[len++] = (unsigned char)((sx->remote_port >> 8) & 0xff);
  847. /* PORT LSB */
  848. socksreq[len++] = (unsigned char)(sx->remote_port & 0xff);
  849. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  850. if(conn->socks5_gssapi_enctype) {
  851. failf(data, "SOCKS5 GSS-API protection not yet implemented.");
  852. return CURLPX_GSSAPI_PROTECTION;
  853. }
  854. #endif
  855. sx->outp = socksreq;
  856. sx->outstanding = len;
  857. sxstate(sx, data, CONNECT_REQ_SENDING);
  858. /* FALLTHROUGH */
  859. case CONNECT_REQ_SENDING:
  860. presult = socks_state_send(cf, sx, data, CURLPX_SEND_REQUEST,
  861. "SOCKS5 connect request");
  862. if(CURLPX_OK != presult)
  863. return presult;
  864. else if(sx->outstanding) {
  865. /* remain in send state */
  866. return CURLPX_OK;
  867. }
  868. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  869. if(conn->socks5_gssapi_enctype) {
  870. failf(data, "SOCKS5 GSS-API protection not yet implemented.");
  871. return CURLPX_GSSAPI_PROTECTION;
  872. }
  873. #endif
  874. sx->outstanding = 10; /* minimum packet size is 10 */
  875. sx->outp = socksreq;
  876. sxstate(sx, data, CONNECT_REQ_READ);
  877. /* FALLTHROUGH */
  878. case CONNECT_REQ_READ:
  879. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_REQACK,
  880. "SOCKS5 connect request ack");
  881. if(CURLPX_OK != presult)
  882. return presult;
  883. else if(sx->outstanding) {
  884. /* remain in reading state */
  885. return CURLPX_OK;
  886. }
  887. else if(socksreq[0] != 5) { /* version */
  888. failf(data,
  889. "SOCKS5 reply has wrong version, version should be 5.");
  890. return CURLPX_BAD_VERSION;
  891. }
  892. else if(socksreq[1]) { /* Anything besides 0 is an error */
  893. CURLproxycode rc = CURLPX_REPLY_UNASSIGNED;
  894. int code = socksreq[1];
  895. failf(data, "Can't complete SOCKS5 connection to %s. (%d)",
  896. sx->hostname, (unsigned char)socksreq[1]);
  897. if(code < 9) {
  898. /* RFC 1928 section 6 lists: */
  899. static const CURLproxycode lookup[] = {
  900. CURLPX_OK,
  901. CURLPX_REPLY_GENERAL_SERVER_FAILURE,
  902. CURLPX_REPLY_NOT_ALLOWED,
  903. CURLPX_REPLY_NETWORK_UNREACHABLE,
  904. CURLPX_REPLY_HOST_UNREACHABLE,
  905. CURLPX_REPLY_CONNECTION_REFUSED,
  906. CURLPX_REPLY_TTL_EXPIRED,
  907. CURLPX_REPLY_COMMAND_NOT_SUPPORTED,
  908. CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED,
  909. };
  910. rc = lookup[code];
  911. }
  912. return rc;
  913. }
  914. /* Fix: in general, returned BND.ADDR is variable length parameter by RFC
  915. 1928, so the reply packet should be read until the end to avoid errors
  916. at subsequent protocol level.
  917. +----+-----+-------+------+----------+----------+
  918. |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
  919. +----+-----+-------+------+----------+----------+
  920. | 1 | 1 | X'00' | 1 | Variable | 2 |
  921. +----+-----+-------+------+----------+----------+
  922. ATYP:
  923. o IP v4 address: X'01', BND.ADDR = 4 byte
  924. o domain name: X'03', BND.ADDR = [ 1 byte length, string ]
  925. o IP v6 address: X'04', BND.ADDR = 16 byte
  926. */
  927. /* Calculate real packet size */
  928. if(socksreq[3] == 3) {
  929. /* domain name */
  930. int addrlen = (int) socksreq[4];
  931. len = 5 + addrlen + 2;
  932. }
  933. else if(socksreq[3] == 4) {
  934. /* IPv6 */
  935. len = 4 + 16 + 2;
  936. }
  937. else if(socksreq[3] == 1) {
  938. len = 4 + 4 + 2;
  939. }
  940. else {
  941. failf(data, "SOCKS5 reply has wrong address type.");
  942. return CURLPX_BAD_ADDRESS_TYPE;
  943. }
  944. /* At this point we already read first 10 bytes */
  945. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  946. if(!conn->socks5_gssapi_enctype) {
  947. /* decrypt_gssapi_blockread already read the whole packet */
  948. #endif
  949. if(len > 10) {
  950. sx->outstanding = len - 10; /* get the rest */
  951. sx->outp = &socksreq[10];
  952. sxstate(sx, data, CONNECT_REQ_READ_MORE);
  953. }
  954. else {
  955. sxstate(sx, data, CONNECT_DONE);
  956. break;
  957. }
  958. #if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
  959. }
  960. #endif
  961. /* FALLTHROUGH */
  962. case CONNECT_REQ_READ_MORE:
  963. presult = socks_state_recv(cf, sx, data, CURLPX_RECV_ADDRESS,
  964. "SOCKS5 connect request address");
  965. if(CURLPX_OK != presult)
  966. return presult;
  967. else if(sx->outstanding) {
  968. /* remain in reading state */
  969. return CURLPX_OK;
  970. }
  971. sxstate(sx, data, CONNECT_DONE);
  972. }
  973. infof(data, "SOCKS5 request granted.");
  974. return CURLPX_OK; /* Proxy was successful! */
  975. }
  976. static CURLcode connect_SOCKS(struct Curl_cfilter *cf,
  977. struct socks_state *sxstate,
  978. struct Curl_easy *data)
  979. {
  980. CURLcode result = CURLE_OK;
  981. CURLproxycode pxresult = CURLPX_OK;
  982. struct connectdata *conn = cf->conn;
  983. switch(conn->socks_proxy.proxytype) {
  984. case CURLPROXY_SOCKS5:
  985. case CURLPROXY_SOCKS5_HOSTNAME:
  986. pxresult = do_SOCKS5(cf, sxstate, data);
  987. break;
  988. case CURLPROXY_SOCKS4:
  989. case CURLPROXY_SOCKS4A:
  990. pxresult = do_SOCKS4(cf, sxstate, data);
  991. break;
  992. default:
  993. failf(data, "unknown proxytype option given");
  994. result = CURLE_COULDNT_CONNECT;
  995. } /* switch proxytype */
  996. if(pxresult) {
  997. result = CURLE_PROXY;
  998. data->info.pxcode = pxresult;
  999. }
  1000. return result;
  1001. }
  1002. static void socks_proxy_cf_free(struct Curl_cfilter *cf)
  1003. {
  1004. struct socks_state *sxstate = cf->ctx;
  1005. if(sxstate) {
  1006. free(sxstate);
  1007. cf->ctx = NULL;
  1008. }
  1009. }
  1010. /* After a TCP connection to the proxy has been verified, this function does
  1011. the next magic steps. If 'done' isn't set TRUE, it is not done yet and
  1012. must be called again.
  1013. Note: this function's sub-functions call failf()
  1014. */
  1015. static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf,
  1016. struct Curl_easy *data,
  1017. bool blocking, bool *done)
  1018. {
  1019. CURLcode result;
  1020. struct connectdata *conn = cf->conn;
  1021. int sockindex = cf->sockindex;
  1022. struct socks_state *sx = cf->ctx;
  1023. if(cf->connected) {
  1024. *done = TRUE;
  1025. return CURLE_OK;
  1026. }
  1027. result = cf->next->cft->do_connect(cf->next, data, blocking, done);
  1028. if(result || !*done)
  1029. return result;
  1030. if(!sx) {
  1031. sx = calloc(sizeof(*sx), 1);
  1032. if(!sx)
  1033. return CURLE_OUT_OF_MEMORY;
  1034. cf->ctx = sx;
  1035. }
  1036. if(sx->state == CONNECT_INIT) {
  1037. /* for the secondary socket (FTP), use the "connect to host"
  1038. * but ignore the "connect to port" (use the secondary port)
  1039. */
  1040. sxstate(sx, data, CONNECT_SOCKS_INIT);
  1041. sx->hostname =
  1042. conn->bits.httpproxy ?
  1043. conn->http_proxy.host.name :
  1044. conn->bits.conn_to_host ?
  1045. conn->conn_to_host.name :
  1046. sockindex == SECONDARYSOCKET ?
  1047. conn->secondaryhostname : conn->host.name;
  1048. sx->remote_port =
  1049. conn->bits.httpproxy ? (int)conn->http_proxy.port :
  1050. sockindex == SECONDARYSOCKET ? conn->secondary_port :
  1051. conn->bits.conn_to_port ? conn->conn_to_port :
  1052. conn->remote_port;
  1053. sx->proxy_user = conn->socks_proxy.user;
  1054. sx->proxy_password = conn->socks_proxy.passwd;
  1055. }
  1056. result = connect_SOCKS(cf, sx, data);
  1057. if(!result && sx->state == CONNECT_DONE) {
  1058. cf->connected = TRUE;
  1059. Curl_verboseconnect(data, conn);
  1060. socks_proxy_cf_free(cf);
  1061. }
  1062. *done = cf->connected;
  1063. return result;
  1064. }
  1065. static int socks_cf_get_select_socks(struct Curl_cfilter *cf,
  1066. struct Curl_easy *data,
  1067. curl_socket_t *socks)
  1068. {
  1069. struct socks_state *sx = cf->ctx;
  1070. int fds;
  1071. fds = cf->next->cft->get_select_socks(cf->next, data, socks);
  1072. if(!fds && cf->next->connected && !cf->connected && sx) {
  1073. /* If we are not connected, the filter below is and has nothing
  1074. * to wait on, we determine what to wait for. */
  1075. socks[0] = Curl_conn_cf_get_socket(cf, data);
  1076. switch(sx->state) {
  1077. case CONNECT_RESOLVING:
  1078. case CONNECT_SOCKS_READ:
  1079. case CONNECT_AUTH_READ:
  1080. case CONNECT_REQ_READ:
  1081. case CONNECT_REQ_READ_MORE:
  1082. fds = GETSOCK_READSOCK(0);
  1083. break;
  1084. default:
  1085. fds = GETSOCK_WRITESOCK(0);
  1086. break;
  1087. }
  1088. }
  1089. return fds;
  1090. }
  1091. static void socks_proxy_cf_close(struct Curl_cfilter *cf,
  1092. struct Curl_easy *data)
  1093. {
  1094. DEBUGASSERT(cf->next);
  1095. cf->connected = FALSE;
  1096. socks_proxy_cf_free(cf);
  1097. cf->next->cft->do_close(cf->next, data);
  1098. }
  1099. static void socks_proxy_cf_destroy(struct Curl_cfilter *cf,
  1100. struct Curl_easy *data)
  1101. {
  1102. (void)data;
  1103. socks_proxy_cf_free(cf);
  1104. }
  1105. static void socks_cf_get_host(struct Curl_cfilter *cf,
  1106. struct Curl_easy *data,
  1107. const char **phost,
  1108. const char **pdisplay_host,
  1109. int *pport)
  1110. {
  1111. (void)data;
  1112. if(!cf->connected) {
  1113. *phost = cf->conn->socks_proxy.host.name;
  1114. *pdisplay_host = cf->conn->http_proxy.host.dispname;
  1115. *pport = (int)cf->conn->socks_proxy.port;
  1116. }
  1117. else {
  1118. cf->next->cft->get_host(cf->next, data, phost, pdisplay_host, pport);
  1119. }
  1120. }
  1121. struct Curl_cftype Curl_cft_socks_proxy = {
  1122. "SOCKS-PROXYY",
  1123. CF_TYPE_IP_CONNECT,
  1124. 0,
  1125. socks_proxy_cf_destroy,
  1126. socks_proxy_cf_connect,
  1127. socks_proxy_cf_close,
  1128. socks_cf_get_host,
  1129. socks_cf_get_select_socks,
  1130. Curl_cf_def_data_pending,
  1131. Curl_cf_def_send,
  1132. Curl_cf_def_recv,
  1133. Curl_cf_def_cntrl,
  1134. Curl_cf_def_conn_is_alive,
  1135. Curl_cf_def_conn_keep_alive,
  1136. Curl_cf_def_query,
  1137. };
  1138. CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at,
  1139. struct Curl_easy *data)
  1140. {
  1141. struct Curl_cfilter *cf;
  1142. CURLcode result;
  1143. (void)data;
  1144. result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, NULL);
  1145. if(!result)
  1146. Curl_conn_cf_insert_after(cf_at, cf);
  1147. return result;
  1148. }
  1149. #endif /* CURL_DISABLE_PROXY */