cf-socket.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  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. #ifdef HAVE_NETINET_IN_H
  26. #include <netinet/in.h> /* <netinet/tcp.h> may need it */
  27. #endif
  28. #ifdef HAVE_SYS_UN_H
  29. #include <sys/un.h> /* for sockaddr_un */
  30. #endif
  31. #ifdef HAVE_LINUX_TCP_H
  32. #include <linux/tcp.h>
  33. #elif defined(HAVE_NETINET_TCP_H)
  34. #include <netinet/tcp.h>
  35. #endif
  36. #ifdef HAVE_NETINET_UDP_H
  37. #include <netinet/udp.h>
  38. #endif
  39. #ifdef HAVE_SYS_IOCTL_H
  40. #include <sys/ioctl.h>
  41. #endif
  42. #ifdef HAVE_NETDB_H
  43. #include <netdb.h>
  44. #endif
  45. #ifdef HAVE_FCNTL_H
  46. #include <fcntl.h>
  47. #endif
  48. #ifdef HAVE_ARPA_INET_H
  49. #include <arpa/inet.h>
  50. #endif
  51. #ifdef __VMS
  52. #include <in.h>
  53. #include <inet.h>
  54. #endif
  55. #ifdef __DragonFly__
  56. /* Required for __DragonFly_version */
  57. #include <sys/param.h>
  58. #endif
  59. #include "urldata.h"
  60. #include "bufq.h"
  61. #include "sendf.h"
  62. #include "if2ip.h"
  63. #include "strerror.h"
  64. #include "cfilters.h"
  65. #include "cf-socket.h"
  66. #include "connect.h"
  67. #include "select.h"
  68. #include "url.h" /* for Curl_safefree() */
  69. #include "multiif.h"
  70. #include "sockaddr.h" /* required for Curl_sockaddr_storage */
  71. #include "inet_ntop.h"
  72. #include "inet_pton.h"
  73. #include "progress.h"
  74. #include "warnless.h"
  75. #include "conncache.h"
  76. #include "multihandle.h"
  77. #include "rand.h"
  78. #include "share.h"
  79. #include "strdup.h"
  80. #include "version_win32.h"
  81. /* The last 3 #include files should be in this order */
  82. #include "curl_printf.h"
  83. #include "curl_memory.h"
  84. #include "memdebug.h"
  85. #if defined(USE_IPV6) && defined(IPV6_V6ONLY) && defined(_WIN32)
  86. /* It makes support for IPv4-mapped IPv6 addresses.
  87. * Linux kernel, NetBSD, FreeBSD and Darwin: default is off;
  88. * Windows Vista and later: default is on;
  89. * DragonFly BSD: acts like off, and dummy setting;
  90. * OpenBSD and earlier Windows: unsupported.
  91. * Linux: controlled by /proc/sys/net/ipv6/bindv6only.
  92. */
  93. static void set_ipv6_v6only(curl_socket_t sockfd, int on)
  94. {
  95. (void)setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on, sizeof(on));
  96. }
  97. #else
  98. #define set_ipv6_v6only(x,y)
  99. #endif
  100. static void tcpnodelay(struct Curl_easy *data, curl_socket_t sockfd)
  101. {
  102. #if defined(TCP_NODELAY)
  103. curl_socklen_t onoff = (curl_socklen_t) 1;
  104. int level = IPPROTO_TCP;
  105. char buffer[STRERROR_LEN];
  106. if(setsockopt(sockfd, level, TCP_NODELAY, (void *)&onoff,
  107. sizeof(onoff)) < 0)
  108. infof(data, "Could not set TCP_NODELAY: %s",
  109. Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  110. #else
  111. (void)data;
  112. (void)sockfd;
  113. #endif
  114. }
  115. #ifdef SO_NOSIGPIPE
  116. /* The preferred method on Mac OS X (10.2 and later) to prevent SIGPIPEs when
  117. sending data to a dead peer (instead of relying on the 4th argument to send
  118. being MSG_NOSIGNAL). Possibly also existing and in use on other BSD
  119. systems? */
  120. static void nosigpipe(struct Curl_easy *data,
  121. curl_socket_t sockfd)
  122. {
  123. int onoff = 1;
  124. (void)data;
  125. if(setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&onoff,
  126. sizeof(onoff)) < 0) {
  127. #if !defined(CURL_DISABLE_VERBOSE_STRINGS)
  128. char buffer[STRERROR_LEN];
  129. infof(data, "Could not set SO_NOSIGPIPE: %s",
  130. Curl_strerror(SOCKERRNO, buffer, sizeof(buffer)));
  131. #endif
  132. }
  133. }
  134. #else
  135. #define nosigpipe(x,y) Curl_nop_stmt
  136. #endif
  137. #if defined(USE_WINSOCK) || \
  138. (defined(__sun) && !defined(TCP_KEEPIDLE)) || \
  139. (defined(__DragonFly__) && __DragonFly_version < 500702) || \
  140. (defined(_WIN32) && !defined(TCP_KEEPIDLE))
  141. /* Solaris < 11.4, DragonFlyBSD < 500702 and Windows < 10.0.16299
  142. * use millisecond units. */
  143. #define KEEPALIVE_FACTOR(x) (x *= 1000)
  144. #else
  145. #define KEEPALIVE_FACTOR(x)
  146. #endif
  147. #if defined(USE_WINSOCK) && !defined(SIO_KEEPALIVE_VALS)
  148. #define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR,4)
  149. struct tcp_keepalive {
  150. u_long onoff;
  151. u_long keepalivetime;
  152. u_long keepaliveinterval;
  153. };
  154. #endif
  155. static void
  156. tcpkeepalive(struct Curl_easy *data,
  157. curl_socket_t sockfd)
  158. {
  159. int optval = data->set.tcp_keepalive?1:0;
  160. /* only set IDLE and INTVL if setting KEEPALIVE is successful */
  161. if(setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
  162. (void *)&optval, sizeof(optval)) < 0) {
  163. infof(data, "Failed to set SO_KEEPALIVE on fd "
  164. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  165. sockfd, SOCKERRNO);
  166. }
  167. else {
  168. #if defined(SIO_KEEPALIVE_VALS) /* Windows */
  169. /* Windows 10, version 1709 (10.0.16299) and later versions */
  170. #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
  171. optval = curlx_sltosi(data->set.tcp_keepidle);
  172. KEEPALIVE_FACTOR(optval);
  173. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE,
  174. (const char *)&optval, sizeof(optval)) < 0) {
  175. infof(data, "Failed to set TCP_KEEPIDLE on fd "
  176. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  177. sockfd, SOCKERRNO);
  178. }
  179. optval = curlx_sltosi(data->set.tcp_keepintvl);
  180. KEEPALIVE_FACTOR(optval);
  181. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL,
  182. (const char *)&optval, sizeof(optval)) < 0) {
  183. infof(data, "Failed to set TCP_KEEPINTVL on fd "
  184. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  185. sockfd, SOCKERRNO);
  186. }
  187. optval = curlx_sltosi(data->set.tcp_keepcnt);
  188. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT,
  189. (const char *)&optval, sizeof(optval)) < 0) {
  190. infof(data, "Failed to set TCP_KEEPCNT on fd "
  191. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  192. sockfd, SOCKERRNO);
  193. }
  194. #else /* Windows < 10.0.16299 */
  195. struct tcp_keepalive vals;
  196. DWORD dummy;
  197. vals.onoff = 1;
  198. optval = curlx_sltosi(data->set.tcp_keepidle);
  199. KEEPALIVE_FACTOR(optval);
  200. vals.keepalivetime = (u_long)optval;
  201. optval = curlx_sltosi(data->set.tcp_keepintvl);
  202. KEEPALIVE_FACTOR(optval);
  203. vals.keepaliveinterval = (u_long)optval;
  204. if(WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, (LPVOID) &vals, sizeof(vals),
  205. NULL, 0, &dummy, NULL, NULL) != 0) {
  206. infof(data, "Failed to set SIO_KEEPALIVE_VALS on fd "
  207. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  208. sockfd, SOCKERRNO);
  209. }
  210. #endif
  211. #else /* !Windows */
  212. #ifdef TCP_KEEPIDLE
  213. optval = curlx_sltosi(data->set.tcp_keepidle);
  214. KEEPALIVE_FACTOR(optval);
  215. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE,
  216. (void *)&optval, sizeof(optval)) < 0) {
  217. infof(data, "Failed to set TCP_KEEPIDLE on fd "
  218. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  219. sockfd, SOCKERRNO);
  220. }
  221. #elif defined(TCP_KEEPALIVE)
  222. /* Mac OS X style */
  223. optval = curlx_sltosi(data->set.tcp_keepidle);
  224. KEEPALIVE_FACTOR(optval);
  225. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE,
  226. (void *)&optval, sizeof(optval)) < 0) {
  227. infof(data, "Failed to set TCP_KEEPALIVE on fd "
  228. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  229. sockfd, SOCKERRNO);
  230. }
  231. #elif defined(TCP_KEEPALIVE_THRESHOLD)
  232. /* Solaris <11.4 style */
  233. optval = curlx_sltosi(data->set.tcp_keepidle);
  234. KEEPALIVE_FACTOR(optval);
  235. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD,
  236. (void *)&optval, sizeof(optval)) < 0) {
  237. infof(data, "Failed to set TCP_KEEPALIVE_THRESHOLD on fd "
  238. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  239. sockfd, SOCKERRNO);
  240. }
  241. #endif
  242. #ifdef TCP_KEEPINTVL
  243. optval = curlx_sltosi(data->set.tcp_keepintvl);
  244. KEEPALIVE_FACTOR(optval);
  245. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL,
  246. (void *)&optval, sizeof(optval)) < 0) {
  247. infof(data, "Failed to set TCP_KEEPINTVL on fd "
  248. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  249. sockfd, SOCKERRNO);
  250. }
  251. #elif defined(TCP_KEEPALIVE_ABORT_THRESHOLD)
  252. /* Solaris <11.4 style */
  253. /* TCP_KEEPALIVE_ABORT_THRESHOLD should equal to
  254. * TCP_KEEPCNT * TCP_KEEPINTVL on other platforms.
  255. * The default value of TCP_KEEPCNT is 9 on Linux,
  256. * 8 on *BSD/macOS, 5 or 10 on Windows. We use the
  257. * default config for Solaris <11.4 because there is
  258. * no default value for TCP_KEEPCNT on Solaris 11.4.
  259. *
  260. * Note that the consequent probes will not be sent
  261. * at equal intervals on Solaris, but will be sent
  262. * using the exponential backoff algorithm. */
  263. optval = curlx_sltosi(data->set.tcp_keepcnt) *
  264. curlx_sltosi(data->set.tcp_keepintvl);
  265. KEEPALIVE_FACTOR(optval);
  266. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD,
  267. (void *)&optval, sizeof(optval)) < 0) {
  268. infof(data, "Failed to set TCP_KEEPALIVE_ABORT_THRESHOLD on fd "
  269. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  270. sockfd, SOCKERRNO);
  271. }
  272. #endif
  273. #ifdef TCP_KEEPCNT
  274. optval = curlx_sltosi(data->set.tcp_keepcnt);
  275. if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT,
  276. (void *)&optval, sizeof(optval)) < 0) {
  277. infof(data, "Failed to set TCP_KEEPCNT on fd "
  278. "%" CURL_FORMAT_SOCKET_T ": errno %d",
  279. sockfd, SOCKERRNO);
  280. }
  281. #endif
  282. #endif
  283. }
  284. }
  285. /**
  286. * Assign the address `ai` to the Curl_sockaddr_ex `dest` and
  287. * set the transport used.
  288. */
  289. void Curl_sock_assign_addr(struct Curl_sockaddr_ex *dest,
  290. const struct Curl_addrinfo *ai,
  291. int transport)
  292. {
  293. /*
  294. * The Curl_sockaddr_ex structure is basically libcurl's external API
  295. * curl_sockaddr structure with enough space available to directly hold
  296. * any protocol-specific address structures. The variable declared here
  297. * will be used to pass / receive data to/from the fopensocket callback
  298. * if this has been set, before that, it is initialized from parameters.
  299. */
  300. dest->family = ai->ai_family;
  301. switch(transport) {
  302. case TRNSPRT_TCP:
  303. dest->socktype = SOCK_STREAM;
  304. dest->protocol = IPPROTO_TCP;
  305. break;
  306. case TRNSPRT_UNIX:
  307. dest->socktype = SOCK_STREAM;
  308. dest->protocol = IPPROTO_IP;
  309. break;
  310. default: /* UDP and QUIC */
  311. dest->socktype = SOCK_DGRAM;
  312. dest->protocol = IPPROTO_UDP;
  313. break;
  314. }
  315. dest->addrlen = (unsigned int)ai->ai_addrlen;
  316. if(dest->addrlen > sizeof(struct Curl_sockaddr_storage))
  317. dest->addrlen = sizeof(struct Curl_sockaddr_storage);
  318. memcpy(&dest->sa_addr, ai->ai_addr, dest->addrlen);
  319. }
  320. static CURLcode socket_open(struct Curl_easy *data,
  321. struct Curl_sockaddr_ex *addr,
  322. curl_socket_t *sockfd)
  323. {
  324. DEBUGASSERT(data);
  325. DEBUGASSERT(data->conn);
  326. if(data->set.fopensocket) {
  327. /*
  328. * If the opensocket callback is set, all the destination address
  329. * information is passed to the callback. Depending on this information the
  330. * callback may opt to abort the connection, this is indicated returning
  331. * CURL_SOCKET_BAD; otherwise it will return a not-connected socket. When
  332. * the callback returns a valid socket the destination address information
  333. * might have been changed and this 'new' address will actually be used
  334. * here to connect.
  335. */
  336. Curl_set_in_callback(data, true);
  337. *sockfd = data->set.fopensocket(data->set.opensocket_client,
  338. CURLSOCKTYPE_IPCXN,
  339. (struct curl_sockaddr *)addr);
  340. Curl_set_in_callback(data, false);
  341. }
  342. else {
  343. /* opensocket callback not set, so simply create the socket now */
  344. *sockfd = socket(addr->family, addr->socktype, addr->protocol);
  345. }
  346. if(*sockfd == CURL_SOCKET_BAD)
  347. /* no socket, no connection */
  348. return CURLE_COULDNT_CONNECT;
  349. #if defined(USE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
  350. if(data->conn->scope_id && (addr->family == AF_INET6)) {
  351. struct sockaddr_in6 * const sa6 = (void *)&addr->sa_addr;
  352. sa6->sin6_scope_id = data->conn->scope_id;
  353. }
  354. #endif
  355. return CURLE_OK;
  356. }
  357. /*
  358. * Create a socket based on info from 'conn' and 'ai'.
  359. *
  360. * 'addr' should be a pointer to the correct struct to get data back, or NULL.
  361. * 'sockfd' must be a pointer to a socket descriptor.
  362. *
  363. * If the open socket callback is set, used that!
  364. *
  365. */
  366. CURLcode Curl_socket_open(struct Curl_easy *data,
  367. const struct Curl_addrinfo *ai,
  368. struct Curl_sockaddr_ex *addr,
  369. int transport,
  370. curl_socket_t *sockfd)
  371. {
  372. struct Curl_sockaddr_ex dummy;
  373. if(!addr)
  374. /* if the caller doesn't want info back, use a local temp copy */
  375. addr = &dummy;
  376. Curl_sock_assign_addr(addr, ai, transport);
  377. return socket_open(data, addr, sockfd);
  378. }
  379. static int socket_close(struct Curl_easy *data, struct connectdata *conn,
  380. int use_callback, curl_socket_t sock)
  381. {
  382. if(use_callback && conn && conn->fclosesocket) {
  383. int rc;
  384. Curl_multi_closed(data, sock);
  385. Curl_set_in_callback(data, true);
  386. rc = conn->fclosesocket(conn->closesocket_client, sock);
  387. Curl_set_in_callback(data, false);
  388. return rc;
  389. }
  390. if(conn)
  391. /* tell the multi-socket code about this */
  392. Curl_multi_closed(data, sock);
  393. sclose(sock);
  394. return 0;
  395. }
  396. /*
  397. * Close a socket.
  398. *
  399. * 'conn' can be NULL, beware!
  400. */
  401. int Curl_socket_close(struct Curl_easy *data, struct connectdata *conn,
  402. curl_socket_t sock)
  403. {
  404. return socket_close(data, conn, FALSE, sock);
  405. }
  406. #ifdef USE_WINSOCK
  407. /* When you run a program that uses the Windows Sockets API, you may
  408. experience slow performance when you copy data to a TCP server.
  409. https://support.microsoft.com/kb/823764
  410. Work-around: Make the Socket Send Buffer Size Larger Than the Program Send
  411. Buffer Size
  412. The problem described in this knowledge-base is applied only to pre-Vista
  413. Windows. Following function trying to detect OS version and skips
  414. SO_SNDBUF adjustment for Windows Vista and above.
  415. */
  416. #define DETECT_OS_NONE 0
  417. #define DETECT_OS_PREVISTA 1
  418. #define DETECT_OS_VISTA_OR_LATER 2
  419. void Curl_sndbuf_init(curl_socket_t sockfd)
  420. {
  421. int val = CURL_MAX_WRITE_SIZE + 32;
  422. int curval = 0;
  423. int curlen = sizeof(curval);
  424. static int detectOsState = DETECT_OS_NONE;
  425. if(detectOsState == DETECT_OS_NONE) {
  426. if(curlx_verify_windows_version(6, 0, 0, PLATFORM_WINNT,
  427. VERSION_GREATER_THAN_EQUAL))
  428. detectOsState = DETECT_OS_VISTA_OR_LATER;
  429. else
  430. detectOsState = DETECT_OS_PREVISTA;
  431. }
  432. if(detectOsState == DETECT_OS_VISTA_OR_LATER)
  433. return;
  434. if(getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&curval, &curlen) == 0)
  435. if(curval > val)
  436. return;
  437. setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (const char *)&val, sizeof(val));
  438. }
  439. #endif /* USE_WINSOCK */
  440. /*
  441. * Curl_parse_interface()
  442. *
  443. * This is used to parse interface argument in the following formats.
  444. * In all the examples, `host` can be an IP address or a hostname.
  445. *
  446. * <iface_or_host> - can be either an interface name or a host.
  447. * if!<iface> - interface name.
  448. * host!<host> - host name.
  449. * ifhost!<iface>!<host> - interface name and host name.
  450. *
  451. * Parameters:
  452. *
  453. * input [in] - input string.
  454. * len [in] - length of the input string.
  455. * dev [in/out] - address where a pointer to newly allocated memory
  456. * holding the interface-or-host will be stored upon
  457. * completion.
  458. * iface [in/out] - address where a pointer to newly allocated memory
  459. * holding the interface will be stored upon completion.
  460. * host [in/out] - address where a pointer to newly allocated memory
  461. * holding the host will be stored upon completion.
  462. *
  463. * Returns CURLE_OK on success.
  464. */
  465. CURLcode Curl_parse_interface(const char *input, size_t len,
  466. char **dev, char **iface, char **host)
  467. {
  468. static const char if_prefix[] = "if!";
  469. static const char host_prefix[] = "host!";
  470. static const char if_host_prefix[] = "ifhost!";
  471. DEBUGASSERT(dev);
  472. DEBUGASSERT(iface);
  473. DEBUGASSERT(host);
  474. if(strncmp(if_prefix, input, strlen(if_prefix)) == 0) {
  475. input += strlen(if_prefix);
  476. if(!*input)
  477. return CURLE_BAD_FUNCTION_ARGUMENT;
  478. *iface = Curl_memdup0(input, len - strlen(if_prefix));
  479. return *iface ? CURLE_OK : CURLE_OUT_OF_MEMORY;
  480. }
  481. if(strncmp(host_prefix, input, strlen(host_prefix)) == 0) {
  482. input += strlen(host_prefix);
  483. if(!*input)
  484. return CURLE_BAD_FUNCTION_ARGUMENT;
  485. *host = Curl_memdup0(input, len - strlen(host_prefix));
  486. return *host ? CURLE_OK : CURLE_OUT_OF_MEMORY;
  487. }
  488. if(strncmp(if_host_prefix, input, strlen(if_host_prefix)) == 0) {
  489. const char *host_part;
  490. input += strlen(if_host_prefix);
  491. len -= strlen(if_host_prefix);
  492. host_part = memchr(input, '!', len);
  493. if(!host_part || !*(host_part + 1))
  494. return CURLE_BAD_FUNCTION_ARGUMENT;
  495. *iface = Curl_memdup0(input, host_part - input);
  496. if(!*iface)
  497. return CURLE_OUT_OF_MEMORY;
  498. ++host_part;
  499. *host = Curl_memdup0(host_part, len - (host_part - input));
  500. if(!*host) {
  501. free(*iface);
  502. *iface = NULL;
  503. return CURLE_OUT_OF_MEMORY;
  504. }
  505. return CURLE_OK;
  506. }
  507. if(!*input)
  508. return CURLE_BAD_FUNCTION_ARGUMENT;
  509. *dev = Curl_memdup0(input, len);
  510. return *dev ? CURLE_OK : CURLE_OUT_OF_MEMORY;
  511. }
  512. #ifndef CURL_DISABLE_BINDLOCAL
  513. static CURLcode bindlocal(struct Curl_easy *data, struct connectdata *conn,
  514. curl_socket_t sockfd, int af, unsigned int scope)
  515. {
  516. struct Curl_sockaddr_storage sa;
  517. struct sockaddr *sock = (struct sockaddr *)&sa; /* bind to this address */
  518. curl_socklen_t sizeof_sa = 0; /* size of the data sock points to */
  519. struct sockaddr_in *si4 = (struct sockaddr_in *)&sa;
  520. #ifdef USE_IPV6
  521. struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)&sa;
  522. #endif
  523. struct Curl_dns_entry *h = NULL;
  524. unsigned short port = data->set.localport; /* use this port number, 0 for
  525. "random" */
  526. /* how many port numbers to try to bind to, increasing one at a time */
  527. int portnum = data->set.localportrange;
  528. const char *dev = data->set.str[STRING_DEVICE];
  529. const char *iface_input = data->set.str[STRING_INTERFACE];
  530. const char *host_input = data->set.str[STRING_BINDHOST];
  531. const char *iface = iface_input ? iface_input : dev;
  532. const char *host = host_input ? host_input : dev;
  533. int error;
  534. #ifdef IP_BIND_ADDRESS_NO_PORT
  535. int on = 1;
  536. #endif
  537. #ifndef USE_IPV6
  538. (void)scope;
  539. #endif
  540. /*************************************************************
  541. * Select device to bind socket to
  542. *************************************************************/
  543. if(!iface && !host && !port)
  544. /* no local kind of binding was requested */
  545. return CURLE_OK;
  546. memset(&sa, 0, sizeof(struct Curl_sockaddr_storage));
  547. if(iface && (strlen(iface)<255) ) {
  548. char myhost[256] = "";
  549. int done = 0; /* -1 for error, 1 for address found */
  550. if2ip_result_t if2ip_result = IF2IP_NOT_FOUND;
  551. /* interface */
  552. #ifdef SO_BINDTODEVICE
  553. /*
  554. * This binds the local socket to a particular interface. This will
  555. * force even requests to other local interfaces to go out the external
  556. * interface. Only bind to the interface when specified as interface,
  557. * not just as a hostname or ip address.
  558. *
  559. * The interface might be a VRF, eg: vrf-blue, which means it cannot be
  560. * converted to an IP address and would fail Curl_if2ip. Simply try to
  561. * use it straight away.
  562. */
  563. if(setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
  564. iface, (curl_socklen_t)strlen(iface) + 1) == 0) {
  565. /* This is often "errno 1, error: Operation not permitted" if you're
  566. * not running as root or another suitable privileged user. If it
  567. * succeeds it means the parameter was a valid interface and not an IP
  568. * address. Return immediately.
  569. */
  570. if(!host_input) {
  571. infof(data, "socket successfully bound to interface '%s'", iface);
  572. return CURLE_OK;
  573. }
  574. }
  575. #endif
  576. if(!host_input) {
  577. /* Discover IP from input device, then bind to it */
  578. if2ip_result = Curl_if2ip(af,
  579. #ifdef USE_IPV6
  580. scope, conn->scope_id,
  581. #endif
  582. iface, myhost, sizeof(myhost));
  583. }
  584. switch(if2ip_result) {
  585. case IF2IP_NOT_FOUND:
  586. if(iface_input && !host_input) {
  587. /* Do not fall back to treating it as a host name */
  588. failf(data, "Couldn't bind to interface '%s'", iface);
  589. return CURLE_INTERFACE_FAILED;
  590. }
  591. break;
  592. case IF2IP_AF_NOT_SUPPORTED:
  593. /* Signal the caller to try another address family if available */
  594. return CURLE_UNSUPPORTED_PROTOCOL;
  595. case IF2IP_FOUND:
  596. /*
  597. * We now have the numerical IP address in the 'myhost' buffer
  598. */
  599. host = myhost;
  600. infof(data, "Local Interface %s is ip %s using address family %i",
  601. iface, host, af);
  602. done = 1;
  603. break;
  604. }
  605. if(!iface_input || host_input) {
  606. /*
  607. * This was not an interface, resolve the name as a host name
  608. * or IP number
  609. *
  610. * Temporarily force name resolution to use only the address type
  611. * of the connection. The resolve functions should really be changed
  612. * to take a type parameter instead.
  613. */
  614. unsigned char ipver = conn->ip_version;
  615. int rc;
  616. if(af == AF_INET)
  617. conn->ip_version = CURL_IPRESOLVE_V4;
  618. #ifdef USE_IPV6
  619. else if(af == AF_INET6)
  620. conn->ip_version = CURL_IPRESOLVE_V6;
  621. #endif
  622. rc = Curl_resolv(data, host, 80, FALSE, &h);
  623. if(rc == CURLRESOLV_PENDING)
  624. (void)Curl_resolver_wait_resolv(data, &h);
  625. conn->ip_version = ipver;
  626. if(h) {
  627. /* convert the resolved address, sizeof myhost >= INET_ADDRSTRLEN */
  628. Curl_printable_address(h->addr, myhost, sizeof(myhost));
  629. infof(data, "Name '%s' family %i resolved to '%s' family %i",
  630. host, af, myhost, h->addr->ai_family);
  631. Curl_resolv_unlock(data, h);
  632. if(af != h->addr->ai_family) {
  633. /* bad IP version combo, signal the caller to try another address
  634. family if available */
  635. return CURLE_UNSUPPORTED_PROTOCOL;
  636. }
  637. done = 1;
  638. }
  639. else {
  640. /*
  641. * provided dev was no interface (or interfaces are not supported
  642. * e.g. solaris) no ip address and no domain we fail here
  643. */
  644. done = -1;
  645. }
  646. }
  647. if(done > 0) {
  648. #ifdef USE_IPV6
  649. /* IPv6 address */
  650. if(af == AF_INET6) {
  651. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  652. char *scope_ptr = strchr(myhost, '%');
  653. if(scope_ptr)
  654. *(scope_ptr++) = '\0';
  655. #endif
  656. if(Curl_inet_pton(AF_INET6, myhost, &si6->sin6_addr) > 0) {
  657. si6->sin6_family = AF_INET6;
  658. si6->sin6_port = htons(port);
  659. #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
  660. if(scope_ptr) {
  661. /* The "myhost" string either comes from Curl_if2ip or from
  662. Curl_printable_address. The latter returns only numeric scope
  663. IDs and the former returns none at all. So the scope ID, if
  664. present, is known to be numeric */
  665. unsigned long scope_id = strtoul(scope_ptr, NULL, 10);
  666. if(scope_id > UINT_MAX)
  667. return CURLE_UNSUPPORTED_PROTOCOL;
  668. si6->sin6_scope_id = (unsigned int)scope_id;
  669. }
  670. #endif
  671. }
  672. sizeof_sa = sizeof(struct sockaddr_in6);
  673. }
  674. else
  675. #endif
  676. /* IPv4 address */
  677. if((af == AF_INET) &&
  678. (Curl_inet_pton(AF_INET, myhost, &si4->sin_addr) > 0)) {
  679. si4->sin_family = AF_INET;
  680. si4->sin_port = htons(port);
  681. sizeof_sa = sizeof(struct sockaddr_in);
  682. }
  683. }
  684. if(done < 1) {
  685. /* errorbuf is set false so failf will overwrite any message already in
  686. the error buffer, so the user receives this error message instead of a
  687. generic resolve error. */
  688. data->state.errorbuf = FALSE;
  689. failf(data, "Couldn't bind to '%s'", host);
  690. return CURLE_INTERFACE_FAILED;
  691. }
  692. }
  693. else {
  694. /* no device was given, prepare sa to match af's needs */
  695. #ifdef USE_IPV6
  696. if(af == AF_INET6) {
  697. si6->sin6_family = AF_INET6;
  698. si6->sin6_port = htons(port);
  699. sizeof_sa = sizeof(struct sockaddr_in6);
  700. }
  701. else
  702. #endif
  703. if(af == AF_INET) {
  704. si4->sin_family = AF_INET;
  705. si4->sin_port = htons(port);
  706. sizeof_sa = sizeof(struct sockaddr_in);
  707. }
  708. }
  709. #ifdef IP_BIND_ADDRESS_NO_PORT
  710. (void)setsockopt(sockfd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &on, sizeof(on));
  711. #endif
  712. for(;;) {
  713. if(bind(sockfd, sock, sizeof_sa) >= 0) {
  714. /* we succeeded to bind */
  715. infof(data, "Local port: %hu", port);
  716. conn->bits.bound = TRUE;
  717. return CURLE_OK;
  718. }
  719. if(--portnum > 0) {
  720. port++; /* try next port */
  721. if(port == 0)
  722. break;
  723. infof(data, "Bind to local port %d failed, trying next", port - 1);
  724. /* We reuse/clobber the port variable here below */
  725. if(sock->sa_family == AF_INET)
  726. si4->sin_port = ntohs(port);
  727. #ifdef USE_IPV6
  728. else
  729. si6->sin6_port = ntohs(port);
  730. #endif
  731. }
  732. else
  733. break;
  734. }
  735. {
  736. char buffer[STRERROR_LEN];
  737. data->state.os_errno = error = SOCKERRNO;
  738. failf(data, "bind failed with errno %d: %s",
  739. error, Curl_strerror(error, buffer, sizeof(buffer)));
  740. }
  741. return CURLE_INTERFACE_FAILED;
  742. }
  743. #endif
  744. /*
  745. * verifyconnect() returns TRUE if the connect really has happened.
  746. */
  747. static bool verifyconnect(curl_socket_t sockfd, int *error)
  748. {
  749. bool rc = TRUE;
  750. #ifdef SO_ERROR
  751. int err = 0;
  752. curl_socklen_t errSize = sizeof(err);
  753. #ifdef _WIN32
  754. /*
  755. * In October 2003 we effectively nullified this function on Windows due to
  756. * problems with it using all CPU in multi-threaded cases.
  757. *
  758. * In May 2004, we bring it back to offer more info back on connect failures.
  759. * Gisle Vanem could reproduce the former problems with this function, but
  760. * could avoid them by adding this SleepEx() call below:
  761. *
  762. * "I don't have Rational Quantify, but the hint from his post was
  763. * ntdll::NtRemoveIoCompletion(). So I'd assume the SleepEx (or maybe
  764. * just Sleep(0) would be enough?) would release whatever
  765. * mutex/critical-section the ntdll call is waiting on.
  766. *
  767. * Someone got to verify this on Win-NT 4.0, 2000."
  768. */
  769. #ifdef _WIN32_WCE
  770. Sleep(0);
  771. #else
  772. SleepEx(0, FALSE);
  773. #endif
  774. #endif
  775. if(0 != getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&err, &errSize))
  776. err = SOCKERRNO;
  777. #ifdef _WIN32_WCE
  778. /* Old WinCE versions don't support SO_ERROR */
  779. if(WSAENOPROTOOPT == err) {
  780. SET_SOCKERRNO(0);
  781. err = 0;
  782. }
  783. #endif
  784. #if defined(EBADIOCTL) && defined(__minix)
  785. /* Minix 3.1.x doesn't support getsockopt on UDP sockets */
  786. if(EBADIOCTL == err) {
  787. SET_SOCKERRNO(0);
  788. err = 0;
  789. }
  790. #endif
  791. if((0 == err) || (EISCONN == err))
  792. /* we are connected, awesome! */
  793. rc = TRUE;
  794. else
  795. /* This wasn't a successful connect */
  796. rc = FALSE;
  797. if(error)
  798. *error = err;
  799. #else
  800. (void)sockfd;
  801. if(error)
  802. *error = SOCKERRNO;
  803. #endif
  804. return rc;
  805. }
  806. /**
  807. * Determine the curl code for a socket connect() == -1 with errno.
  808. */
  809. static CURLcode socket_connect_result(struct Curl_easy *data,
  810. const char *ipaddress, int error)
  811. {
  812. switch(error) {
  813. case EINPROGRESS:
  814. case EWOULDBLOCK:
  815. #if defined(EAGAIN)
  816. #if (EAGAIN) != (EWOULDBLOCK)
  817. /* On some platforms EAGAIN and EWOULDBLOCK are the
  818. * same value, and on others they are different, hence
  819. * the odd #if
  820. */
  821. case EAGAIN:
  822. #endif
  823. #endif
  824. return CURLE_OK;
  825. default:
  826. /* unknown error, fallthrough and try another address! */
  827. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  828. (void)ipaddress;
  829. #else
  830. {
  831. char buffer[STRERROR_LEN];
  832. infof(data, "Immediate connect fail for %s: %s",
  833. ipaddress, Curl_strerror(error, buffer, sizeof(buffer)));
  834. }
  835. #endif
  836. data->state.os_errno = error;
  837. /* connect failed */
  838. return CURLE_COULDNT_CONNECT;
  839. }
  840. }
  841. /* We have a recv buffer to enhance reads with len < NW_SMALL_READS.
  842. * This happens often on TLS connections where the TLS implementation
  843. * tries to read the head of a TLS record, determine the length of the
  844. * full record and then make a subsequent read for that.
  845. * On large reads, we will not fill the buffer to avoid the double copy. */
  846. #define NW_RECV_CHUNK_SIZE (64 * 1024)
  847. #define NW_RECV_CHUNKS 1
  848. #define NW_SMALL_READS (1024)
  849. struct cf_socket_ctx {
  850. int transport;
  851. struct Curl_sockaddr_ex addr; /* address to connect to */
  852. curl_socket_t sock; /* current attempt socket */
  853. struct bufq recvbuf; /* used when `buffer_recv` is set */
  854. struct ip_quadruple ip; /* The IP quadruple 2x(addr+port) */
  855. struct curltime started_at; /* when socket was created */
  856. struct curltime connected_at; /* when socket connected/got first byte */
  857. struct curltime first_byte_at; /* when first byte was recvd */
  858. #ifdef USE_WINSOCK
  859. struct curltime last_sndbuf_query_at; /* when SO_SNDBUF last queried */
  860. ULONG sndbuf_size; /* the last set SO_SNDBUF size */
  861. #endif
  862. int error; /* errno of last failure or 0 */
  863. #ifdef DEBUGBUILD
  864. int wblock_percent; /* percent of writes doing EAGAIN */
  865. int wpartial_percent; /* percent of bytes written in send */
  866. int rblock_percent; /* percent of reads doing EAGAIN */
  867. size_t recv_max; /* max enforced read size */
  868. #endif
  869. BIT(got_first_byte); /* if first byte was received */
  870. BIT(accepted); /* socket was accepted, not connected */
  871. BIT(sock_connected); /* socket is "connected", e.g. in UDP */
  872. BIT(active);
  873. BIT(buffer_recv);
  874. };
  875. static void cf_socket_ctx_init(struct cf_socket_ctx *ctx,
  876. const struct Curl_addrinfo *ai,
  877. int transport)
  878. {
  879. memset(ctx, 0, sizeof(*ctx));
  880. ctx->sock = CURL_SOCKET_BAD;
  881. ctx->transport = transport;
  882. Curl_sock_assign_addr(&ctx->addr, ai, transport);
  883. Curl_bufq_init(&ctx->recvbuf, NW_RECV_CHUNK_SIZE, NW_RECV_CHUNKS);
  884. #ifdef DEBUGBUILD
  885. {
  886. char *p = getenv("CURL_DBG_SOCK_WBLOCK");
  887. if(p) {
  888. long l = strtol(p, NULL, 10);
  889. if(l >= 0 && l <= 100)
  890. ctx->wblock_percent = (int)l;
  891. }
  892. p = getenv("CURL_DBG_SOCK_WPARTIAL");
  893. if(p) {
  894. long l = strtol(p, NULL, 10);
  895. if(l >= 0 && l <= 100)
  896. ctx->wpartial_percent = (int)l;
  897. }
  898. p = getenv("CURL_DBG_SOCK_RBLOCK");
  899. if(p) {
  900. long l = strtol(p, NULL, 10);
  901. if(l >= 0 && l <= 100)
  902. ctx->rblock_percent = (int)l;
  903. }
  904. p = getenv("CURL_DBG_SOCK_RMAX");
  905. if(p) {
  906. long l = strtol(p, NULL, 10);
  907. if(l >= 0)
  908. ctx->recv_max = (size_t)l;
  909. }
  910. }
  911. #endif
  912. }
  913. struct reader_ctx {
  914. struct Curl_cfilter *cf;
  915. struct Curl_easy *data;
  916. };
  917. static ssize_t nw_in_read(void *reader_ctx,
  918. unsigned char *buf, size_t len,
  919. CURLcode *err)
  920. {
  921. struct reader_ctx *rctx = reader_ctx;
  922. struct cf_socket_ctx *ctx = rctx->cf->ctx;
  923. ssize_t nread;
  924. *err = CURLE_OK;
  925. nread = sread(ctx->sock, buf, len);
  926. if(-1 == nread) {
  927. int sockerr = SOCKERRNO;
  928. if(
  929. #ifdef WSAEWOULDBLOCK
  930. /* This is how Windows does it */
  931. (WSAEWOULDBLOCK == sockerr)
  932. #else
  933. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
  934. due to its inability to send off data without blocking. We therefore
  935. treat both error codes the same here */
  936. (EWOULDBLOCK == sockerr) || (EAGAIN == sockerr) || (EINTR == sockerr)
  937. #endif
  938. ) {
  939. /* this is just a case of EWOULDBLOCK */
  940. *err = CURLE_AGAIN;
  941. nread = -1;
  942. }
  943. else {
  944. char buffer[STRERROR_LEN];
  945. failf(rctx->data, "Recv failure: %s",
  946. Curl_strerror(sockerr, buffer, sizeof(buffer)));
  947. rctx->data->state.os_errno = sockerr;
  948. *err = CURLE_RECV_ERROR;
  949. nread = -1;
  950. }
  951. }
  952. CURL_TRC_CF(rctx->data, rctx->cf, "nw_in_read(len=%zu, fd=%"
  953. CURL_FORMAT_SOCKET_T ") -> %d, err=%d",
  954. len, ctx->sock, (int)nread, *err);
  955. return nread;
  956. }
  957. static void cf_socket_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  958. {
  959. struct cf_socket_ctx *ctx = cf->ctx;
  960. if(ctx && CURL_SOCKET_BAD != ctx->sock) {
  961. CURL_TRC_CF(data, cf, "cf_socket_close(%" CURL_FORMAT_SOCKET_T
  962. ")", ctx->sock);
  963. if(ctx->sock == cf->conn->sock[cf->sockindex])
  964. cf->conn->sock[cf->sockindex] = CURL_SOCKET_BAD;
  965. socket_close(data, cf->conn, !ctx->accepted, ctx->sock);
  966. ctx->sock = CURL_SOCKET_BAD;
  967. if(ctx->active && cf->sockindex == FIRSTSOCKET)
  968. cf->conn->remote_addr = NULL;
  969. Curl_bufq_reset(&ctx->recvbuf);
  970. ctx->active = FALSE;
  971. ctx->buffer_recv = FALSE;
  972. memset(&ctx->started_at, 0, sizeof(ctx->started_at));
  973. memset(&ctx->connected_at, 0, sizeof(ctx->connected_at));
  974. }
  975. cf->connected = FALSE;
  976. }
  977. static CURLcode cf_socket_shutdown(struct Curl_cfilter *cf,
  978. struct Curl_easy *data,
  979. bool *done)
  980. {
  981. if(cf->connected) {
  982. struct cf_socket_ctx *ctx = cf->ctx;
  983. CURL_TRC_CF(data, cf, "cf_socket_shutdown(%" CURL_FORMAT_SOCKET_T
  984. ")", ctx->sock);
  985. /* On TCP, and when the socket looks well and non-blocking mode
  986. * can be enabled, receive dangling bytes before close to avoid
  987. * entering RST states unnecessarily. */
  988. if(ctx->sock != CURL_SOCKET_BAD &&
  989. ctx->transport == TRNSPRT_TCP &&
  990. (curlx_nonblock(ctx->sock, TRUE) >= 0)) {
  991. unsigned char buf[1024];
  992. (void)sread(ctx->sock, buf, sizeof(buf));
  993. }
  994. }
  995. *done = TRUE;
  996. return CURLE_OK;
  997. }
  998. static void cf_socket_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
  999. {
  1000. struct cf_socket_ctx *ctx = cf->ctx;
  1001. cf_socket_close(cf, data);
  1002. CURL_TRC_CF(data, cf, "destroy");
  1003. Curl_bufq_free(&ctx->recvbuf);
  1004. free(ctx);
  1005. cf->ctx = NULL;
  1006. }
  1007. static CURLcode set_local_ip(struct Curl_cfilter *cf,
  1008. struct Curl_easy *data)
  1009. {
  1010. struct cf_socket_ctx *ctx = cf->ctx;
  1011. #ifdef HAVE_GETSOCKNAME
  1012. if((ctx->sock != CURL_SOCKET_BAD) &&
  1013. !(data->conn->handler->protocol & CURLPROTO_TFTP)) {
  1014. /* TFTP does not connect, so it cannot get the IP like this */
  1015. char buffer[STRERROR_LEN];
  1016. struct Curl_sockaddr_storage ssloc;
  1017. curl_socklen_t slen = sizeof(struct Curl_sockaddr_storage);
  1018. memset(&ssloc, 0, sizeof(ssloc));
  1019. if(getsockname(ctx->sock, (struct sockaddr*) &ssloc, &slen)) {
  1020. int error = SOCKERRNO;
  1021. failf(data, "getsockname() failed with errno %d: %s",
  1022. error, Curl_strerror(error, buffer, sizeof(buffer)));
  1023. return CURLE_FAILED_INIT;
  1024. }
  1025. if(!Curl_addr2string((struct sockaddr*)&ssloc, slen,
  1026. ctx->ip.local_ip, &ctx->ip.local_port)) {
  1027. failf(data, "ssloc inet_ntop() failed with errno %d: %s",
  1028. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  1029. return CURLE_FAILED_INIT;
  1030. }
  1031. }
  1032. #else
  1033. (void)data;
  1034. ctx->ip.local_ip[0] = 0;
  1035. ctx->ip.local_port = -1;
  1036. #endif
  1037. return CURLE_OK;
  1038. }
  1039. static CURLcode set_remote_ip(struct Curl_cfilter *cf,
  1040. struct Curl_easy *data)
  1041. {
  1042. struct cf_socket_ctx *ctx = cf->ctx;
  1043. /* store remote address and port used in this connection attempt */
  1044. if(!Curl_addr2string(&ctx->addr.sa_addr, (curl_socklen_t)ctx->addr.addrlen,
  1045. ctx->ip.remote_ip, &ctx->ip.remote_port)) {
  1046. char buffer[STRERROR_LEN];
  1047. ctx->error = errno;
  1048. /* malformed address or bug in inet_ntop, try next address */
  1049. failf(data, "sa_addr inet_ntop() failed with errno %d: %s",
  1050. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  1051. return CURLE_FAILED_INIT;
  1052. }
  1053. return CURLE_OK;
  1054. }
  1055. static CURLcode cf_socket_open(struct Curl_cfilter *cf,
  1056. struct Curl_easy *data)
  1057. {
  1058. struct cf_socket_ctx *ctx = cf->ctx;
  1059. int error = 0;
  1060. bool isconnected = FALSE;
  1061. CURLcode result = CURLE_COULDNT_CONNECT;
  1062. bool is_tcp;
  1063. (void)data;
  1064. DEBUGASSERT(ctx->sock == CURL_SOCKET_BAD);
  1065. ctx->started_at = Curl_now();
  1066. #ifdef SOCK_NONBLOCK
  1067. /* Don't tuck SOCK_NONBLOCK into socktype when opensocket callback is set
  1068. * because we wouldn't know how socketype is about to be used in the
  1069. * callback, SOCK_NONBLOCK might get factored out before calling socket().
  1070. */
  1071. if(!data->set.fopensocket)
  1072. ctx->addr.socktype |= SOCK_NONBLOCK;
  1073. #endif
  1074. result = socket_open(data, &ctx->addr, &ctx->sock);
  1075. #ifdef SOCK_NONBLOCK
  1076. /* Restore the socktype after the socket is created. */
  1077. if(!data->set.fopensocket)
  1078. ctx->addr.socktype &= ~SOCK_NONBLOCK;
  1079. #endif
  1080. if(result)
  1081. goto out;
  1082. result = set_remote_ip(cf, data);
  1083. if(result)
  1084. goto out;
  1085. #ifdef USE_IPV6
  1086. if(ctx->addr.family == AF_INET6) {
  1087. set_ipv6_v6only(ctx->sock, 0);
  1088. infof(data, " Trying [%s]:%d...", ctx->ip.remote_ip, ctx->ip.remote_port);
  1089. }
  1090. else
  1091. #endif
  1092. infof(data, " Trying %s:%d...", ctx->ip.remote_ip, ctx->ip.remote_port);
  1093. #ifdef USE_IPV6
  1094. is_tcp = (ctx->addr.family == AF_INET
  1095. || ctx->addr.family == AF_INET6) &&
  1096. ctx->addr.socktype == SOCK_STREAM;
  1097. #else
  1098. is_tcp = (ctx->addr.family == AF_INET) &&
  1099. ctx->addr.socktype == SOCK_STREAM;
  1100. #endif
  1101. if(is_tcp && data->set.tcp_nodelay)
  1102. tcpnodelay(data, ctx->sock);
  1103. nosigpipe(data, ctx->sock);
  1104. Curl_sndbuf_init(ctx->sock);
  1105. if(is_tcp && data->set.tcp_keepalive)
  1106. tcpkeepalive(data, ctx->sock);
  1107. if(data->set.fsockopt) {
  1108. /* activate callback for setting socket options */
  1109. Curl_set_in_callback(data, true);
  1110. error = data->set.fsockopt(data->set.sockopt_client,
  1111. ctx->sock,
  1112. CURLSOCKTYPE_IPCXN);
  1113. Curl_set_in_callback(data, false);
  1114. if(error == CURL_SOCKOPT_ALREADY_CONNECTED)
  1115. isconnected = TRUE;
  1116. else if(error) {
  1117. result = CURLE_ABORTED_BY_CALLBACK;
  1118. goto out;
  1119. }
  1120. }
  1121. #ifndef CURL_DISABLE_BINDLOCAL
  1122. /* possibly bind the local end to an IP, interface or port */
  1123. if(ctx->addr.family == AF_INET
  1124. #ifdef USE_IPV6
  1125. || ctx->addr.family == AF_INET6
  1126. #endif
  1127. ) {
  1128. result = bindlocal(data, cf->conn, ctx->sock, ctx->addr.family,
  1129. Curl_ipv6_scope(&ctx->addr.sa_addr));
  1130. if(result) {
  1131. if(result == CURLE_UNSUPPORTED_PROTOCOL) {
  1132. /* The address family is not supported on this interface.
  1133. We can continue trying addresses */
  1134. result = CURLE_COULDNT_CONNECT;
  1135. }
  1136. goto out;
  1137. }
  1138. }
  1139. #endif
  1140. #ifndef SOCK_NONBLOCK
  1141. /* Set socket non-blocking, must be a non-blocking socket for
  1142. * a non-blocking connect. */
  1143. error = curlx_nonblock(ctx->sock, TRUE);
  1144. if(error < 0) {
  1145. result = CURLE_UNSUPPORTED_PROTOCOL;
  1146. ctx->error = SOCKERRNO;
  1147. goto out;
  1148. }
  1149. #else
  1150. if(data->set.fopensocket) {
  1151. /* Set socket non-blocking, must be a non-blocking socket for
  1152. * a non-blocking connect. */
  1153. error = curlx_nonblock(ctx->sock, TRUE);
  1154. if(error < 0) {
  1155. result = CURLE_UNSUPPORTED_PROTOCOL;
  1156. ctx->error = SOCKERRNO;
  1157. goto out;
  1158. }
  1159. }
  1160. #endif
  1161. ctx->sock_connected = (ctx->addr.socktype != SOCK_DGRAM);
  1162. out:
  1163. if(result) {
  1164. if(ctx->sock != CURL_SOCKET_BAD) {
  1165. socket_close(data, cf->conn, TRUE, ctx->sock);
  1166. ctx->sock = CURL_SOCKET_BAD;
  1167. }
  1168. }
  1169. else if(isconnected) {
  1170. set_local_ip(cf, data);
  1171. ctx->connected_at = Curl_now();
  1172. cf->connected = TRUE;
  1173. }
  1174. CURL_TRC_CF(data, cf, "cf_socket_open() -> %d, fd=%" CURL_FORMAT_SOCKET_T,
  1175. result, ctx->sock);
  1176. return result;
  1177. }
  1178. static int do_connect(struct Curl_cfilter *cf, struct Curl_easy *data,
  1179. bool is_tcp_fastopen)
  1180. {
  1181. struct cf_socket_ctx *ctx = cf->ctx;
  1182. #ifdef TCP_FASTOPEN_CONNECT
  1183. int optval = 1;
  1184. #endif
  1185. int rc = -1;
  1186. (void)data;
  1187. if(is_tcp_fastopen) {
  1188. #if defined(CONNECT_DATA_IDEMPOTENT) /* Darwin */
  1189. # if defined(HAVE_BUILTIN_AVAILABLE)
  1190. /* while connectx function is available since macOS 10.11 / iOS 9,
  1191. it did not have the interface declared correctly until
  1192. Xcode 9 / macOS SDK 10.13 */
  1193. if(__builtin_available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *)) {
  1194. sa_endpoints_t endpoints;
  1195. endpoints.sae_srcif = 0;
  1196. endpoints.sae_srcaddr = NULL;
  1197. endpoints.sae_srcaddrlen = 0;
  1198. endpoints.sae_dstaddr = &ctx->addr.sa_addr;
  1199. endpoints.sae_dstaddrlen = ctx->addr.addrlen;
  1200. rc = connectx(ctx->sock, &endpoints, SAE_ASSOCID_ANY,
  1201. CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
  1202. NULL, 0, NULL, NULL);
  1203. }
  1204. else {
  1205. rc = connect(ctx->sock, &ctx->addr.sa_addr, ctx->addr.addrlen);
  1206. }
  1207. # else
  1208. rc = connect(ctx->sock, &ctx->addr.sa_addr, ctx->addr.addrlen);
  1209. # endif /* HAVE_BUILTIN_AVAILABLE */
  1210. #elif defined(TCP_FASTOPEN_CONNECT) /* Linux >= 4.11 */
  1211. if(setsockopt(ctx->sock, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
  1212. (void *)&optval, sizeof(optval)) < 0)
  1213. infof(data, "Failed to enable TCP Fast Open on fd %"
  1214. CURL_FORMAT_SOCKET_T, ctx->sock);
  1215. rc = connect(ctx->sock, &ctx->addr.sa_addr, ctx->addr.addrlen);
  1216. #elif defined(MSG_FASTOPEN) /* old Linux */
  1217. if(cf->conn->given->flags & PROTOPT_SSL)
  1218. rc = connect(ctx->sock, &ctx->addr.sa_addr, ctx->addr.addrlen);
  1219. else
  1220. rc = 0; /* Do nothing */
  1221. #endif
  1222. }
  1223. else {
  1224. rc = connect(ctx->sock, &ctx->addr.sa_addr,
  1225. (curl_socklen_t)ctx->addr.addrlen);
  1226. }
  1227. return rc;
  1228. }
  1229. static CURLcode cf_tcp_connect(struct Curl_cfilter *cf,
  1230. struct Curl_easy *data,
  1231. bool blocking, bool *done)
  1232. {
  1233. struct cf_socket_ctx *ctx = cf->ctx;
  1234. CURLcode result = CURLE_COULDNT_CONNECT;
  1235. int rc = 0;
  1236. (void)data;
  1237. if(cf->connected) {
  1238. *done = TRUE;
  1239. return CURLE_OK;
  1240. }
  1241. /* TODO: need to support blocking connect? */
  1242. if(blocking)
  1243. return CURLE_UNSUPPORTED_PROTOCOL;
  1244. *done = FALSE; /* a very negative world view is best */
  1245. if(ctx->sock == CURL_SOCKET_BAD) {
  1246. int error;
  1247. result = cf_socket_open(cf, data);
  1248. if(result)
  1249. goto out;
  1250. if(cf->connected) {
  1251. *done = TRUE;
  1252. return CURLE_OK;
  1253. }
  1254. /* Connect TCP socket */
  1255. rc = do_connect(cf, data, cf->conn->bits.tcp_fastopen);
  1256. error = SOCKERRNO;
  1257. set_local_ip(cf, data);
  1258. CURL_TRC_CF(data, cf, "local address %s port %d...",
  1259. ctx->ip.local_ip, ctx->ip.local_port);
  1260. if(-1 == rc) {
  1261. result = socket_connect_result(data, ctx->ip.remote_ip, error);
  1262. goto out;
  1263. }
  1264. }
  1265. #ifdef mpeix
  1266. /* Call this function once now, and ignore the results. We do this to
  1267. "clear" the error state on the socket so that we can later read it
  1268. reliably. This is reported necessary on the MPE/iX operating
  1269. system. */
  1270. (void)verifyconnect(ctx->sock, NULL);
  1271. #endif
  1272. /* check socket for connect */
  1273. rc = SOCKET_WRITABLE(ctx->sock, 0);
  1274. if(rc == 0) { /* no connection yet */
  1275. CURL_TRC_CF(data, cf, "not connected yet");
  1276. return CURLE_OK;
  1277. }
  1278. else if(rc == CURL_CSELECT_OUT || cf->conn->bits.tcp_fastopen) {
  1279. if(verifyconnect(ctx->sock, &ctx->error)) {
  1280. /* we are connected with TCP, awesome! */
  1281. ctx->connected_at = Curl_now();
  1282. set_local_ip(cf, data);
  1283. *done = TRUE;
  1284. cf->connected = TRUE;
  1285. CURL_TRC_CF(data, cf, "connected");
  1286. return CURLE_OK;
  1287. }
  1288. }
  1289. else if(rc & CURL_CSELECT_ERR) {
  1290. (void)verifyconnect(ctx->sock, &ctx->error);
  1291. result = CURLE_COULDNT_CONNECT;
  1292. }
  1293. out:
  1294. if(result) {
  1295. if(ctx->error) {
  1296. set_local_ip(cf, data);
  1297. data->state.os_errno = ctx->error;
  1298. SET_SOCKERRNO(ctx->error);
  1299. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1300. {
  1301. char buffer[STRERROR_LEN];
  1302. infof(data, "connect to %s port %u from %s port %d failed: %s",
  1303. ctx->ip.remote_ip, ctx->ip.remote_port,
  1304. ctx->ip.local_ip, ctx->ip.local_port,
  1305. Curl_strerror(ctx->error, buffer, sizeof(buffer)));
  1306. }
  1307. #endif
  1308. }
  1309. if(ctx->sock != CURL_SOCKET_BAD) {
  1310. socket_close(data, cf->conn, TRUE, ctx->sock);
  1311. ctx->sock = CURL_SOCKET_BAD;
  1312. }
  1313. *done = FALSE;
  1314. }
  1315. return result;
  1316. }
  1317. static void cf_socket_get_host(struct Curl_cfilter *cf,
  1318. struct Curl_easy *data,
  1319. const char **phost,
  1320. const char **pdisplay_host,
  1321. int *pport)
  1322. {
  1323. struct cf_socket_ctx *ctx = cf->ctx;
  1324. (void)data;
  1325. *phost = cf->conn->host.name;
  1326. *pdisplay_host = cf->conn->host.dispname;
  1327. *pport = ctx->ip.remote_port;
  1328. }
  1329. static void cf_socket_adjust_pollset(struct Curl_cfilter *cf,
  1330. struct Curl_easy *data,
  1331. struct easy_pollset *ps)
  1332. {
  1333. struct cf_socket_ctx *ctx = cf->ctx;
  1334. if(ctx->sock != CURL_SOCKET_BAD) {
  1335. if(!cf->connected) {
  1336. Curl_pollset_set_out_only(data, ps, ctx->sock);
  1337. CURL_TRC_CF(data, cf, "adjust_pollset, !connected, POLLOUT fd=%"
  1338. CURL_FORMAT_SOCKET_T, ctx->sock);
  1339. }
  1340. else if(!ctx->active) {
  1341. Curl_pollset_add_in(data, ps, ctx->sock);
  1342. CURL_TRC_CF(data, cf, "adjust_pollset, !active, POLLIN fd=%"
  1343. CURL_FORMAT_SOCKET_T, ctx->sock);
  1344. }
  1345. }
  1346. }
  1347. static bool cf_socket_data_pending(struct Curl_cfilter *cf,
  1348. const struct Curl_easy *data)
  1349. {
  1350. struct cf_socket_ctx *ctx = cf->ctx;
  1351. int readable;
  1352. (void)data;
  1353. if(!Curl_bufq_is_empty(&ctx->recvbuf))
  1354. return TRUE;
  1355. readable = SOCKET_READABLE(ctx->sock, 0);
  1356. return (readable > 0 && (readable & CURL_CSELECT_IN));
  1357. }
  1358. #ifdef USE_WINSOCK
  1359. #ifndef SIO_IDEAL_SEND_BACKLOG_QUERY
  1360. #define SIO_IDEAL_SEND_BACKLOG_QUERY 0x4004747B
  1361. #endif
  1362. static void win_update_sndbuf_size(struct cf_socket_ctx *ctx)
  1363. {
  1364. ULONG ideal;
  1365. DWORD ideallen;
  1366. struct curltime n = Curl_now();
  1367. if(Curl_timediff(n, ctx->last_sndbuf_query_at) > 1000) {
  1368. if(!WSAIoctl(ctx->sock, SIO_IDEAL_SEND_BACKLOG_QUERY, 0, 0,
  1369. &ideal, sizeof(ideal), &ideallen, 0, 0) &&
  1370. ideal != ctx->sndbuf_size &&
  1371. !setsockopt(ctx->sock, SOL_SOCKET, SO_SNDBUF,
  1372. (const char *)&ideal, sizeof(ideal))) {
  1373. ctx->sndbuf_size = ideal;
  1374. }
  1375. ctx->last_sndbuf_query_at = n;
  1376. }
  1377. }
  1378. #endif /* USE_WINSOCK */
  1379. static ssize_t cf_socket_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  1380. const void *buf, size_t len, CURLcode *err)
  1381. {
  1382. struct cf_socket_ctx *ctx = cf->ctx;
  1383. curl_socket_t fdsave;
  1384. ssize_t nwritten;
  1385. size_t orig_len = len;
  1386. *err = CURLE_OK;
  1387. fdsave = cf->conn->sock[cf->sockindex];
  1388. cf->conn->sock[cf->sockindex] = ctx->sock;
  1389. #ifdef DEBUGBUILD
  1390. /* simulate network blocking/partial writes */
  1391. if(ctx->wblock_percent > 0) {
  1392. unsigned char c = 0;
  1393. Curl_rand(data, &c, 1);
  1394. if(c >= ((100-ctx->wblock_percent)*256/100)) {
  1395. CURL_TRC_CF(data, cf, "send(len=%zu) SIMULATE EWOULDBLOCK", orig_len);
  1396. *err = CURLE_AGAIN;
  1397. nwritten = -1;
  1398. cf->conn->sock[cf->sockindex] = fdsave;
  1399. return nwritten;
  1400. }
  1401. }
  1402. if(cf->cft != &Curl_cft_udp && ctx->wpartial_percent > 0 && len > 8) {
  1403. len = len * ctx->wpartial_percent / 100;
  1404. if(!len)
  1405. len = 1;
  1406. CURL_TRC_CF(data, cf, "send(len=%zu) SIMULATE partial write of %zu bytes",
  1407. orig_len, len);
  1408. }
  1409. #endif
  1410. #if defined(MSG_FASTOPEN) && !defined(TCP_FASTOPEN_CONNECT) /* Linux */
  1411. if(cf->conn->bits.tcp_fastopen) {
  1412. nwritten = sendto(ctx->sock, buf, len, MSG_FASTOPEN,
  1413. &cf->conn->remote_addr->sa_addr,
  1414. cf->conn->remote_addr->addrlen);
  1415. cf->conn->bits.tcp_fastopen = FALSE;
  1416. }
  1417. else
  1418. #endif
  1419. nwritten = swrite(ctx->sock, buf, len);
  1420. if(-1 == nwritten) {
  1421. int sockerr = SOCKERRNO;
  1422. if(
  1423. #ifdef WSAEWOULDBLOCK
  1424. /* This is how Windows does it */
  1425. (WSAEWOULDBLOCK == sockerr)
  1426. #else
  1427. /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
  1428. due to its inability to send off data without blocking. We therefore
  1429. treat both error codes the same here */
  1430. (EWOULDBLOCK == sockerr) || (EAGAIN == sockerr) || (EINTR == sockerr) ||
  1431. (EINPROGRESS == sockerr)
  1432. #endif
  1433. ) {
  1434. /* this is just a case of EWOULDBLOCK */
  1435. *err = CURLE_AGAIN;
  1436. }
  1437. else {
  1438. char buffer[STRERROR_LEN];
  1439. failf(data, "Send failure: %s",
  1440. Curl_strerror(sockerr, buffer, sizeof(buffer)));
  1441. data->state.os_errno = sockerr;
  1442. *err = CURLE_SEND_ERROR;
  1443. }
  1444. }
  1445. #if defined(USE_WINSOCK)
  1446. if(!*err)
  1447. win_update_sndbuf_size(ctx);
  1448. #endif
  1449. CURL_TRC_CF(data, cf, "send(len=%zu) -> %d, err=%d",
  1450. orig_len, (int)nwritten, *err);
  1451. cf->conn->sock[cf->sockindex] = fdsave;
  1452. return nwritten;
  1453. }
  1454. static ssize_t cf_socket_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1455. char *buf, size_t len, CURLcode *err)
  1456. {
  1457. struct cf_socket_ctx *ctx = cf->ctx;
  1458. curl_socket_t fdsave;
  1459. ssize_t nread;
  1460. *err = CURLE_OK;
  1461. fdsave = cf->conn->sock[cf->sockindex];
  1462. cf->conn->sock[cf->sockindex] = ctx->sock;
  1463. #ifdef DEBUGBUILD
  1464. /* simulate network blocking/partial reads */
  1465. if(cf->cft != &Curl_cft_udp && ctx->rblock_percent > 0) {
  1466. unsigned char c = 0;
  1467. Curl_rand(data, &c, 1);
  1468. if(c >= ((100-ctx->rblock_percent)*256/100)) {
  1469. CURL_TRC_CF(data, cf, "recv(len=%zu) SIMULATE EWOULDBLOCK", len);
  1470. *err = CURLE_AGAIN;
  1471. nread = -1;
  1472. cf->conn->sock[cf->sockindex] = fdsave;
  1473. return nread;
  1474. }
  1475. }
  1476. if(cf->cft != &Curl_cft_udp && ctx->recv_max && ctx->recv_max < len) {
  1477. size_t orig_len = len;
  1478. len = ctx->recv_max;
  1479. CURL_TRC_CF(data, cf, "recv(len=%zu) SIMULATE max read of %zu bytes",
  1480. orig_len, len);
  1481. }
  1482. #endif
  1483. if(ctx->buffer_recv && !Curl_bufq_is_empty(&ctx->recvbuf)) {
  1484. CURL_TRC_CF(data, cf, "recv from buffer");
  1485. nread = Curl_bufq_read(&ctx->recvbuf, (unsigned char *)buf, len, err);
  1486. }
  1487. else {
  1488. struct reader_ctx rctx;
  1489. rctx.cf = cf;
  1490. rctx.data = data;
  1491. /* "small" reads may trigger filling our buffer, "large" reads
  1492. * are probably not worth the additional copy */
  1493. if(ctx->buffer_recv && len < NW_SMALL_READS) {
  1494. ssize_t nwritten;
  1495. nwritten = Curl_bufq_slurp(&ctx->recvbuf, nw_in_read, &rctx, err);
  1496. if(nwritten < 0 && !Curl_bufq_is_empty(&ctx->recvbuf)) {
  1497. /* we have a partial read with an error. need to deliver
  1498. * what we got, return the error later. */
  1499. CURL_TRC_CF(data, cf, "partial read: empty buffer first");
  1500. nread = Curl_bufq_read(&ctx->recvbuf, (unsigned char *)buf, len, err);
  1501. }
  1502. else if(nwritten < 0) {
  1503. nread = -1;
  1504. goto out;
  1505. }
  1506. else if(nwritten == 0) {
  1507. /* eof */
  1508. *err = CURLE_OK;
  1509. nread = 0;
  1510. }
  1511. else {
  1512. CURL_TRC_CF(data, cf, "buffered %zd additional bytes", nwritten);
  1513. nread = Curl_bufq_read(&ctx->recvbuf, (unsigned char *)buf, len, err);
  1514. }
  1515. }
  1516. else {
  1517. nread = nw_in_read(&rctx, (unsigned char *)buf, len, err);
  1518. }
  1519. }
  1520. out:
  1521. CURL_TRC_CF(data, cf, "recv(len=%zu) -> %d, err=%d", len, (int)nread,
  1522. *err);
  1523. if(nread > 0 && !ctx->got_first_byte) {
  1524. ctx->first_byte_at = Curl_now();
  1525. ctx->got_first_byte = TRUE;
  1526. }
  1527. cf->conn->sock[cf->sockindex] = fdsave;
  1528. return nread;
  1529. }
  1530. static void cf_socket_active(struct Curl_cfilter *cf, struct Curl_easy *data)
  1531. {
  1532. struct cf_socket_ctx *ctx = cf->ctx;
  1533. /* use this socket from now on */
  1534. cf->conn->sock[cf->sockindex] = ctx->sock;
  1535. set_local_ip(cf, data);
  1536. if(cf->sockindex == SECONDARYSOCKET)
  1537. cf->conn->secondary = ctx->ip;
  1538. else
  1539. cf->conn->primary = ctx->ip;
  1540. /* the first socket info gets some specials */
  1541. if(cf->sockindex == FIRSTSOCKET) {
  1542. cf->conn->remote_addr = &ctx->addr;
  1543. #ifdef USE_IPV6
  1544. cf->conn->bits.ipv6 = (ctx->addr.family == AF_INET6)? TRUE : FALSE;
  1545. #endif
  1546. Curl_persistconninfo(data, cf->conn, &ctx->ip);
  1547. /* buffering is currently disabled by default because we have stalls
  1548. * in parallel transfers where not all buffered data is consumed and no
  1549. * socket events happen.
  1550. */
  1551. ctx->buffer_recv = FALSE;
  1552. }
  1553. ctx->active = TRUE;
  1554. }
  1555. static CURLcode cf_socket_cntrl(struct Curl_cfilter *cf,
  1556. struct Curl_easy *data,
  1557. int event, int arg1, void *arg2)
  1558. {
  1559. struct cf_socket_ctx *ctx = cf->ctx;
  1560. (void)arg1;
  1561. (void)arg2;
  1562. switch(event) {
  1563. case CF_CTRL_CONN_INFO_UPDATE:
  1564. cf_socket_active(cf, data);
  1565. break;
  1566. case CF_CTRL_DATA_SETUP:
  1567. Curl_persistconninfo(data, cf->conn, &ctx->ip);
  1568. break;
  1569. case CF_CTRL_FORGET_SOCKET:
  1570. ctx->sock = CURL_SOCKET_BAD;
  1571. break;
  1572. }
  1573. return CURLE_OK;
  1574. }
  1575. static bool cf_socket_conn_is_alive(struct Curl_cfilter *cf,
  1576. struct Curl_easy *data,
  1577. bool *input_pending)
  1578. {
  1579. struct cf_socket_ctx *ctx = cf->ctx;
  1580. struct pollfd pfd[1];
  1581. int r;
  1582. *input_pending = FALSE;
  1583. (void)data;
  1584. if(!ctx || ctx->sock == CURL_SOCKET_BAD)
  1585. return FALSE;
  1586. /* Check with 0 timeout if there are any events pending on the socket */
  1587. pfd[0].fd = ctx->sock;
  1588. pfd[0].events = POLLRDNORM|POLLIN|POLLRDBAND|POLLPRI;
  1589. pfd[0].revents = 0;
  1590. r = Curl_poll(pfd, 1, 0);
  1591. if(r < 0) {
  1592. CURL_TRC_CF(data, cf, "is_alive: poll error, assume dead");
  1593. return FALSE;
  1594. }
  1595. else if(r == 0) {
  1596. CURL_TRC_CF(data, cf, "is_alive: poll timeout, assume alive");
  1597. return TRUE;
  1598. }
  1599. else if(pfd[0].revents & (POLLERR|POLLHUP|POLLPRI|POLLNVAL)) {
  1600. CURL_TRC_CF(data, cf, "is_alive: err/hup/etc events, assume dead");
  1601. return FALSE;
  1602. }
  1603. CURL_TRC_CF(data, cf, "is_alive: valid events, looks alive");
  1604. *input_pending = TRUE;
  1605. return TRUE;
  1606. }
  1607. static CURLcode cf_socket_query(struct Curl_cfilter *cf,
  1608. struct Curl_easy *data,
  1609. int query, int *pres1, void *pres2)
  1610. {
  1611. struct cf_socket_ctx *ctx = cf->ctx;
  1612. switch(query) {
  1613. case CF_QUERY_SOCKET:
  1614. DEBUGASSERT(pres2);
  1615. *((curl_socket_t *)pres2) = ctx->sock;
  1616. return CURLE_OK;
  1617. case CF_QUERY_CONNECT_REPLY_MS:
  1618. if(ctx->got_first_byte) {
  1619. timediff_t ms = Curl_timediff(ctx->first_byte_at, ctx->started_at);
  1620. *pres1 = (ms < INT_MAX)? (int)ms : INT_MAX;
  1621. }
  1622. else
  1623. *pres1 = -1;
  1624. return CURLE_OK;
  1625. case CF_QUERY_TIMER_CONNECT: {
  1626. struct curltime *when = pres2;
  1627. switch(ctx->transport) {
  1628. case TRNSPRT_UDP:
  1629. case TRNSPRT_QUIC:
  1630. /* Since UDP connected sockets work different from TCP, we use the
  1631. * time of the first byte from the peer as the "connect" time. */
  1632. if(ctx->got_first_byte) {
  1633. *when = ctx->first_byte_at;
  1634. break;
  1635. }
  1636. FALLTHROUGH();
  1637. default:
  1638. *when = ctx->connected_at;
  1639. break;
  1640. }
  1641. return CURLE_OK;
  1642. }
  1643. default:
  1644. break;
  1645. }
  1646. return cf->next?
  1647. cf->next->cft->query(cf->next, data, query, pres1, pres2) :
  1648. CURLE_UNKNOWN_OPTION;
  1649. }
  1650. struct Curl_cftype Curl_cft_tcp = {
  1651. "TCP",
  1652. CF_TYPE_IP_CONNECT,
  1653. CURL_LOG_LVL_NONE,
  1654. cf_socket_destroy,
  1655. cf_tcp_connect,
  1656. cf_socket_close,
  1657. cf_socket_shutdown,
  1658. cf_socket_get_host,
  1659. cf_socket_adjust_pollset,
  1660. cf_socket_data_pending,
  1661. cf_socket_send,
  1662. cf_socket_recv,
  1663. cf_socket_cntrl,
  1664. cf_socket_conn_is_alive,
  1665. Curl_cf_def_conn_keep_alive,
  1666. cf_socket_query,
  1667. };
  1668. CURLcode Curl_cf_tcp_create(struct Curl_cfilter **pcf,
  1669. struct Curl_easy *data,
  1670. struct connectdata *conn,
  1671. const struct Curl_addrinfo *ai,
  1672. int transport)
  1673. {
  1674. struct cf_socket_ctx *ctx = NULL;
  1675. struct Curl_cfilter *cf = NULL;
  1676. CURLcode result;
  1677. (void)data;
  1678. (void)conn;
  1679. DEBUGASSERT(transport == TRNSPRT_TCP);
  1680. ctx = calloc(1, sizeof(*ctx));
  1681. if(!ctx) {
  1682. result = CURLE_OUT_OF_MEMORY;
  1683. goto out;
  1684. }
  1685. cf_socket_ctx_init(ctx, ai, transport);
  1686. result = Curl_cf_create(&cf, &Curl_cft_tcp, ctx);
  1687. out:
  1688. *pcf = (!result)? cf : NULL;
  1689. if(result) {
  1690. Curl_safefree(cf);
  1691. Curl_safefree(ctx);
  1692. }
  1693. return result;
  1694. }
  1695. static CURLcode cf_udp_setup_quic(struct Curl_cfilter *cf,
  1696. struct Curl_easy *data)
  1697. {
  1698. struct cf_socket_ctx *ctx = cf->ctx;
  1699. int rc;
  1700. int one = 1;
  1701. (void)one;
  1702. /* QUIC needs a connected socket, nonblocking */
  1703. DEBUGASSERT(ctx->sock != CURL_SOCKET_BAD);
  1704. rc = connect(ctx->sock, &ctx->addr.sa_addr,
  1705. (curl_socklen_t)ctx->addr.addrlen);
  1706. if(-1 == rc) {
  1707. return socket_connect_result(data, ctx->ip.remote_ip, SOCKERRNO);
  1708. }
  1709. ctx->sock_connected = TRUE;
  1710. set_local_ip(cf, data);
  1711. CURL_TRC_CF(data, cf, "%s socket %" CURL_FORMAT_SOCKET_T
  1712. " connected: [%s:%d] -> [%s:%d]",
  1713. (ctx->transport == TRNSPRT_QUIC)? "QUIC" : "UDP",
  1714. ctx->sock, ctx->ip.local_ip, ctx->ip.local_port,
  1715. ctx->ip.remote_ip, ctx->ip.remote_port);
  1716. /* Currently, cf->ctx->sock is always non-blocking because the only
  1717. * caller to cf_udp_setup_quic() is cf_udp_connect() that passes the
  1718. * non-blocking socket created by cf_socket_open() to it. Thus, we
  1719. * don't need to call curlx_nonblock() in cf_udp_setup_quic() anymore.
  1720. */
  1721. switch(ctx->addr.family) {
  1722. #if defined(__linux__) && defined(IP_MTU_DISCOVER)
  1723. case AF_INET: {
  1724. int val = IP_PMTUDISC_DO;
  1725. (void)setsockopt(ctx->sock, IPPROTO_IP, IP_MTU_DISCOVER, &val,
  1726. sizeof(val));
  1727. break;
  1728. }
  1729. #endif
  1730. #if defined(__linux__) && defined(IPV6_MTU_DISCOVER)
  1731. case AF_INET6: {
  1732. int val = IPV6_PMTUDISC_DO;
  1733. (void)setsockopt(ctx->sock, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val,
  1734. sizeof(val));
  1735. break;
  1736. }
  1737. #endif
  1738. }
  1739. #if defined(__linux__) && defined(UDP_GRO) && \
  1740. (defined(HAVE_SENDMMSG) || defined(HAVE_SENDMSG)) && \
  1741. ((defined(USE_NGTCP2) && defined(USE_NGHTTP3)) || defined(USE_QUICHE))
  1742. (void)setsockopt(ctx->sock, IPPROTO_UDP, UDP_GRO, &one,
  1743. (socklen_t)sizeof(one));
  1744. #endif
  1745. return CURLE_OK;
  1746. }
  1747. static CURLcode cf_udp_connect(struct Curl_cfilter *cf,
  1748. struct Curl_easy *data,
  1749. bool blocking, bool *done)
  1750. {
  1751. struct cf_socket_ctx *ctx = cf->ctx;
  1752. CURLcode result = CURLE_COULDNT_CONNECT;
  1753. (void)blocking;
  1754. if(cf->connected) {
  1755. *done = TRUE;
  1756. return CURLE_OK;
  1757. }
  1758. *done = FALSE;
  1759. if(ctx->sock == CURL_SOCKET_BAD) {
  1760. result = cf_socket_open(cf, data);
  1761. if(result) {
  1762. CURL_TRC_CF(data, cf, "cf_udp_connect(), open failed -> %d", result);
  1763. goto out;
  1764. }
  1765. if(ctx->transport == TRNSPRT_QUIC) {
  1766. result = cf_udp_setup_quic(cf, data);
  1767. if(result)
  1768. goto out;
  1769. CURL_TRC_CF(data, cf, "cf_udp_connect(), opened socket=%"
  1770. CURL_FORMAT_SOCKET_T " (%s:%d)",
  1771. ctx->sock, ctx->ip.local_ip, ctx->ip.local_port);
  1772. }
  1773. else {
  1774. CURL_TRC_CF(data, cf, "cf_udp_connect(), opened socket=%"
  1775. CURL_FORMAT_SOCKET_T " (unconnected)", ctx->sock);
  1776. }
  1777. *done = TRUE;
  1778. cf->connected = TRUE;
  1779. }
  1780. out:
  1781. return result;
  1782. }
  1783. struct Curl_cftype Curl_cft_udp = {
  1784. "UDP",
  1785. CF_TYPE_IP_CONNECT,
  1786. CURL_LOG_LVL_NONE,
  1787. cf_socket_destroy,
  1788. cf_udp_connect,
  1789. cf_socket_close,
  1790. cf_socket_shutdown,
  1791. cf_socket_get_host,
  1792. cf_socket_adjust_pollset,
  1793. cf_socket_data_pending,
  1794. cf_socket_send,
  1795. cf_socket_recv,
  1796. cf_socket_cntrl,
  1797. cf_socket_conn_is_alive,
  1798. Curl_cf_def_conn_keep_alive,
  1799. cf_socket_query,
  1800. };
  1801. CURLcode Curl_cf_udp_create(struct Curl_cfilter **pcf,
  1802. struct Curl_easy *data,
  1803. struct connectdata *conn,
  1804. const struct Curl_addrinfo *ai,
  1805. int transport)
  1806. {
  1807. struct cf_socket_ctx *ctx = NULL;
  1808. struct Curl_cfilter *cf = NULL;
  1809. CURLcode result;
  1810. (void)data;
  1811. (void)conn;
  1812. DEBUGASSERT(transport == TRNSPRT_UDP || transport == TRNSPRT_QUIC);
  1813. ctx = calloc(1, sizeof(*ctx));
  1814. if(!ctx) {
  1815. result = CURLE_OUT_OF_MEMORY;
  1816. goto out;
  1817. }
  1818. cf_socket_ctx_init(ctx, ai, transport);
  1819. result = Curl_cf_create(&cf, &Curl_cft_udp, ctx);
  1820. out:
  1821. *pcf = (!result)? cf : NULL;
  1822. if(result) {
  1823. Curl_safefree(cf);
  1824. Curl_safefree(ctx);
  1825. }
  1826. return result;
  1827. }
  1828. /* this is the TCP filter which can also handle this case */
  1829. struct Curl_cftype Curl_cft_unix = {
  1830. "UNIX",
  1831. CF_TYPE_IP_CONNECT,
  1832. CURL_LOG_LVL_NONE,
  1833. cf_socket_destroy,
  1834. cf_tcp_connect,
  1835. cf_socket_close,
  1836. cf_socket_shutdown,
  1837. cf_socket_get_host,
  1838. cf_socket_adjust_pollset,
  1839. cf_socket_data_pending,
  1840. cf_socket_send,
  1841. cf_socket_recv,
  1842. cf_socket_cntrl,
  1843. cf_socket_conn_is_alive,
  1844. Curl_cf_def_conn_keep_alive,
  1845. cf_socket_query,
  1846. };
  1847. CURLcode Curl_cf_unix_create(struct Curl_cfilter **pcf,
  1848. struct Curl_easy *data,
  1849. struct connectdata *conn,
  1850. const struct Curl_addrinfo *ai,
  1851. int transport)
  1852. {
  1853. struct cf_socket_ctx *ctx = NULL;
  1854. struct Curl_cfilter *cf = NULL;
  1855. CURLcode result;
  1856. (void)data;
  1857. (void)conn;
  1858. DEBUGASSERT(transport == TRNSPRT_UNIX);
  1859. ctx = calloc(1, sizeof(*ctx));
  1860. if(!ctx) {
  1861. result = CURLE_OUT_OF_MEMORY;
  1862. goto out;
  1863. }
  1864. cf_socket_ctx_init(ctx, ai, transport);
  1865. result = Curl_cf_create(&cf, &Curl_cft_unix, ctx);
  1866. out:
  1867. *pcf = (!result)? cf : NULL;
  1868. if(result) {
  1869. Curl_safefree(cf);
  1870. Curl_safefree(ctx);
  1871. }
  1872. return result;
  1873. }
  1874. static CURLcode cf_tcp_accept_connect(struct Curl_cfilter *cf,
  1875. struct Curl_easy *data,
  1876. bool blocking, bool *done)
  1877. {
  1878. /* we start accepted, if we ever close, we cannot go on */
  1879. (void)data;
  1880. (void)blocking;
  1881. if(cf->connected) {
  1882. *done = TRUE;
  1883. return CURLE_OK;
  1884. }
  1885. return CURLE_FAILED_INIT;
  1886. }
  1887. struct Curl_cftype Curl_cft_tcp_accept = {
  1888. "TCP-ACCEPT",
  1889. CF_TYPE_IP_CONNECT,
  1890. CURL_LOG_LVL_NONE,
  1891. cf_socket_destroy,
  1892. cf_tcp_accept_connect,
  1893. cf_socket_close,
  1894. cf_socket_shutdown,
  1895. cf_socket_get_host, /* TODO: not accurate */
  1896. cf_socket_adjust_pollset,
  1897. cf_socket_data_pending,
  1898. cf_socket_send,
  1899. cf_socket_recv,
  1900. cf_socket_cntrl,
  1901. cf_socket_conn_is_alive,
  1902. Curl_cf_def_conn_keep_alive,
  1903. cf_socket_query,
  1904. };
  1905. CURLcode Curl_conn_tcp_listen_set(struct Curl_easy *data,
  1906. struct connectdata *conn,
  1907. int sockindex, curl_socket_t *s)
  1908. {
  1909. CURLcode result;
  1910. struct Curl_cfilter *cf = NULL;
  1911. struct cf_socket_ctx *ctx = NULL;
  1912. /* replace any existing */
  1913. Curl_conn_cf_discard_all(data, conn, sockindex);
  1914. DEBUGASSERT(conn->sock[sockindex] == CURL_SOCKET_BAD);
  1915. ctx = calloc(1, sizeof(*ctx));
  1916. if(!ctx) {
  1917. result = CURLE_OUT_OF_MEMORY;
  1918. goto out;
  1919. }
  1920. ctx->transport = conn->transport;
  1921. ctx->sock = *s;
  1922. ctx->accepted = FALSE;
  1923. result = Curl_cf_create(&cf, &Curl_cft_tcp_accept, ctx);
  1924. if(result)
  1925. goto out;
  1926. Curl_conn_cf_add(data, conn, sockindex, cf);
  1927. conn->sock[sockindex] = ctx->sock;
  1928. set_local_ip(cf, data);
  1929. ctx->active = TRUE;
  1930. ctx->connected_at = Curl_now();
  1931. cf->connected = TRUE;
  1932. CURL_TRC_CF(data, cf, "Curl_conn_tcp_listen_set(%"
  1933. CURL_FORMAT_SOCKET_T ")", ctx->sock);
  1934. out:
  1935. if(result) {
  1936. Curl_safefree(cf);
  1937. Curl_safefree(ctx);
  1938. }
  1939. return result;
  1940. }
  1941. static void set_accepted_remote_ip(struct Curl_cfilter *cf,
  1942. struct Curl_easy *data)
  1943. {
  1944. struct cf_socket_ctx *ctx = cf->ctx;
  1945. #ifdef HAVE_GETPEERNAME
  1946. char buffer[STRERROR_LEN];
  1947. struct Curl_sockaddr_storage ssrem;
  1948. curl_socklen_t plen;
  1949. ctx->ip.remote_ip[0] = 0;
  1950. ctx->ip.remote_port = 0;
  1951. plen = sizeof(ssrem);
  1952. memset(&ssrem, 0, plen);
  1953. if(getpeername(ctx->sock, (struct sockaddr*) &ssrem, &plen)) {
  1954. int error = SOCKERRNO;
  1955. failf(data, "getpeername() failed with errno %d: %s",
  1956. error, Curl_strerror(error, buffer, sizeof(buffer)));
  1957. return;
  1958. }
  1959. if(!Curl_addr2string((struct sockaddr*)&ssrem, plen,
  1960. ctx->ip.remote_ip, &ctx->ip.remote_port)) {
  1961. failf(data, "ssrem inet_ntop() failed with errno %d: %s",
  1962. errno, Curl_strerror(errno, buffer, sizeof(buffer)));
  1963. return;
  1964. }
  1965. #else
  1966. ctx->ip.remote_ip[0] = 0;
  1967. ctx->ip.remote_port = 0;
  1968. (void)data;
  1969. #endif
  1970. }
  1971. CURLcode Curl_conn_tcp_accepted_set(struct Curl_easy *data,
  1972. struct connectdata *conn,
  1973. int sockindex, curl_socket_t *s)
  1974. {
  1975. struct Curl_cfilter *cf = NULL;
  1976. struct cf_socket_ctx *ctx = NULL;
  1977. cf = conn->cfilter[sockindex];
  1978. if(!cf || cf->cft != &Curl_cft_tcp_accept)
  1979. return CURLE_FAILED_INIT;
  1980. ctx = cf->ctx;
  1981. /* discard the listen socket */
  1982. socket_close(data, conn, TRUE, ctx->sock);
  1983. ctx->sock = *s;
  1984. conn->sock[sockindex] = ctx->sock;
  1985. set_accepted_remote_ip(cf, data);
  1986. set_local_ip(cf, data);
  1987. ctx->active = TRUE;
  1988. ctx->accepted = TRUE;
  1989. ctx->connected_at = Curl_now();
  1990. cf->connected = TRUE;
  1991. CURL_TRC_CF(data, cf, "accepted_set(sock=%" CURL_FORMAT_SOCKET_T
  1992. ", remote=%s port=%d)",
  1993. ctx->sock, ctx->ip.remote_ip, ctx->ip.remote_port);
  1994. return CURLE_OK;
  1995. }
  1996. /**
  1997. * Return TRUE iff `cf` is a socket filter.
  1998. */
  1999. static bool cf_is_socket(struct Curl_cfilter *cf)
  2000. {
  2001. return cf && (cf->cft == &Curl_cft_tcp ||
  2002. cf->cft == &Curl_cft_udp ||
  2003. cf->cft == &Curl_cft_unix ||
  2004. cf->cft == &Curl_cft_tcp_accept);
  2005. }
  2006. CURLcode Curl_cf_socket_peek(struct Curl_cfilter *cf,
  2007. struct Curl_easy *data,
  2008. curl_socket_t *psock,
  2009. const struct Curl_sockaddr_ex **paddr,
  2010. struct ip_quadruple *pip)
  2011. {
  2012. (void)data;
  2013. if(cf_is_socket(cf) && cf->ctx) {
  2014. struct cf_socket_ctx *ctx = cf->ctx;
  2015. if(psock)
  2016. *psock = ctx->sock;
  2017. if(paddr)
  2018. *paddr = &ctx->addr;
  2019. if(pip)
  2020. *pip = ctx->ip;
  2021. return CURLE_OK;
  2022. }
  2023. return CURLE_FAILED_INIT;
  2024. }