cf-socket.c 68 KB

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