cf-socket.c 65 KB

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