multi.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066
  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. #include "urldata.h"
  26. #include "transfer.h"
  27. #include "url.h"
  28. #include "cfilters.h"
  29. #include "connect.h"
  30. #include "progress.h"
  31. #include "curl_share.h"
  32. #include "psl.h"
  33. #include "multiif.h"
  34. #include "multi_ev.h"
  35. #include "sendf.h"
  36. #include "curl_trc.h"
  37. #include "http.h"
  38. #include "select.h"
  39. #include "curlx/wait.h"
  40. #include "conncache.h"
  41. #include "multihandle.h"
  42. #include "sigpipe.h"
  43. #include "vtls/vtls.h"
  44. #include "vtls/vtls_scache.h"
  45. #include "http_proxy.h"
  46. #include "http2.h"
  47. #include "socketpair.h"
  48. #include "bufref.h"
  49. /* initial multi->xfers table size for a full multi */
  50. #define CURL_XFER_TABLE_SIZE 512
  51. /*
  52. CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97
  53. to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every
  54. curl handle takes 6K memory, therefore this 3K are not significant.
  55. */
  56. #ifndef CURL_SOCKET_HASH_TABLE_SIZE
  57. #define CURL_SOCKET_HASH_TABLE_SIZE 911
  58. #endif
  59. #ifndef CURL_CONNECTION_HASH_SIZE
  60. #define CURL_CONNECTION_HASH_SIZE 97
  61. #endif
  62. #ifndef CURL_DNS_HASH_SIZE
  63. #define CURL_DNS_HASH_SIZE 71
  64. #endif
  65. #ifndef CURL_TLS_SESSION_SIZE
  66. #define CURL_TLS_SESSION_SIZE 25
  67. #endif
  68. #define CURL_MULTI_HANDLE 0x000bab1e
  69. #ifdef DEBUGBUILD
  70. /* On a debug build, we want to fail hard on multi handles that
  71. * are not NULL, but no longer have the MAGIC touch. This gives
  72. * us early warning on things only discovered by valgrind otherwise. */
  73. #define GOOD_MULTI_HANDLE(x) \
  74. (((x) && (x)->magic == CURL_MULTI_HANDLE)? TRUE: \
  75. (DEBUGASSERT(!(x)), FALSE))
  76. #else
  77. #define GOOD_MULTI_HANDLE(x) \
  78. ((x) && (x)->magic == CURL_MULTI_HANDLE)
  79. #endif
  80. static void move_pending_to_connect(struct Curl_multi *multi,
  81. struct Curl_easy *data);
  82. static CURLMcode add_next_timeout(const struct curltime *pnow,
  83. struct Curl_multi *multi,
  84. struct Curl_easy *d);
  85. static void multi_timeout(struct Curl_multi *multi,
  86. struct curltime *expire_time,
  87. long *timeout_ms);
  88. static void process_pending_handles(struct Curl_multi *multi);
  89. static void multi_xfer_bufs_free(struct Curl_multi *multi);
  90. #ifdef DEBUGBUILD
  91. static void multi_xfer_tbl_dump(struct Curl_multi *multi);
  92. #endif
  93. static const struct curltime *multi_now(struct Curl_multi *multi)
  94. {
  95. curlx_pnow(&multi->now);
  96. return &multi->now;
  97. }
  98. /* function pointer called once when switching TO a state */
  99. typedef void (*init_multistate_func)(struct Curl_easy *data);
  100. /* called in DID state, before PERFORMING state */
  101. static void before_perform(struct Curl_easy *data)
  102. {
  103. data->req.chunk = FALSE;
  104. Curl_pgrsTime(data, TIMER_PRETRANSFER);
  105. }
  106. static void init_completed(struct Curl_easy *data)
  107. {
  108. /* this is a completed transfer */
  109. /* Important: reset the conn pointer so that we do not point to memory
  110. that could be freed anytime */
  111. Curl_detach_connection(data);
  112. Curl_expire_clear(data); /* stop all timers */
  113. }
  114. /* always use this function to change state, to make debugging easier */
  115. static void mstate(struct Curl_easy *data, CURLMstate state
  116. #ifdef DEBUGBUILD
  117. , int lineno
  118. #endif
  119. )
  120. {
  121. CURLMstate oldstate = data->mstate;
  122. static const init_multistate_func finit[MSTATE_LAST] = {
  123. NULL, /* INIT */
  124. NULL, /* PENDING */
  125. NULL, /* SETUP */
  126. Curl_init_CONNECT, /* CONNECT */
  127. NULL, /* RESOLVING */
  128. NULL, /* CONNECTING */
  129. NULL, /* PROTOCONNECT */
  130. NULL, /* PROTOCONNECTING */
  131. NULL, /* DO */
  132. NULL, /* DOING */
  133. NULL, /* DOING_MORE */
  134. before_perform, /* DID */
  135. NULL, /* PERFORMING */
  136. NULL, /* RATELIMITING */
  137. NULL, /* DONE */
  138. init_completed, /* COMPLETED */
  139. NULL /* MSGSENT */
  140. };
  141. if(oldstate == state)
  142. /* do not bother when the new state is the same as the old state */
  143. return;
  144. #ifdef DEBUGBUILD
  145. CURL_TRC_M(data, "-> [%s] (line %d)", CURL_MSTATE_NAME(state), lineno);
  146. #else
  147. CURL_TRC_M(data, "-> [%s]", CURL_MSTATE_NAME(state));
  148. #endif
  149. /* really switching state */
  150. data->mstate = state;
  151. switch(state) {
  152. case MSTATE_DONE:
  153. CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE);
  154. break;
  155. case MSTATE_COMPLETED:
  156. /* we sometimes directly jump to COMPLETED, trigger also a notification
  157. * in that case. */
  158. if(oldstate < MSTATE_DONE)
  159. CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE);
  160. /* changing to COMPLETED means it is in process and needs to go */
  161. DEBUGASSERT(Curl_uint32_bset_contains(&data->multi->process, data->mid));
  162. Curl_uint32_bset_remove(&data->multi->process, data->mid);
  163. Curl_uint32_bset_remove(&data->multi->pending, data->mid); /* to be sure */
  164. if(Curl_uint32_bset_empty(&data->multi->process)) {
  165. /* free the transfer buffer when we have no more active transfers */
  166. multi_xfer_bufs_free(data->multi);
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. /* if this state has an init-function, run it */
  173. if(finit[state])
  174. finit[state](data);
  175. }
  176. #ifndef DEBUGBUILD
  177. #define multistate(x, y) mstate(x, y)
  178. #else
  179. #define multistate(x, y) mstate(x, y, __LINE__)
  180. #endif
  181. /* multi->proto_hash destructor. Should never be called as elements
  182. * MUST be added with their own destructor */
  183. static void ph_freeentry(void *p)
  184. {
  185. (void)p;
  186. /* Will always be FALSE. Cannot use a 0 assert here since compilers
  187. * are not in agreement if they then want a NORETURN attribute or
  188. * not. *sigh* */
  189. DEBUGASSERT(p == NULL);
  190. }
  191. /*
  192. * multi_addmsg()
  193. *
  194. * Called when a transfer is completed. Adds the given msg pointer to
  195. * the list kept in the multi handle.
  196. */
  197. static void multi_addmsg(struct Curl_multi *multi, struct Curl_message *msg)
  198. {
  199. if(!Curl_llist_count(&multi->msglist))
  200. CURLM_NTFY(multi->admin, CURLMNOTIFY_INFO_READ);
  201. Curl_llist_append(&multi->msglist, msg, &msg->list);
  202. }
  203. struct Curl_multi *Curl_multi_handle(uint32_t xfer_table_size,
  204. size_t ev_hashsize, /* event hash */
  205. size_t chashsize, /* connection hash */
  206. size_t dnssize, /* dns hash */
  207. size_t sesssize) /* TLS session cache */
  208. {
  209. struct Curl_multi *multi = curlx_calloc(1, sizeof(struct Curl_multi));
  210. if(!multi)
  211. return NULL;
  212. multi->magic = CURL_MULTI_HANDLE;
  213. Curl_dnscache_init(&multi->dnscache, dnssize);
  214. Curl_mntfy_init(multi);
  215. Curl_multi_ev_init(multi, ev_hashsize);
  216. Curl_uint32_tbl_init(&multi->xfers, NULL);
  217. Curl_uint32_bset_init(&multi->process);
  218. Curl_uint32_bset_init(&multi->dirty);
  219. Curl_uint32_bset_init(&multi->pending);
  220. Curl_uint32_bset_init(&multi->msgsent);
  221. Curl_hash_init(&multi->proto_hash, 23,
  222. Curl_hash_str, curlx_str_key_compare, ph_freeentry);
  223. Curl_llist_init(&multi->msglist, NULL);
  224. multi->multiplexing = TRUE;
  225. multi->max_concurrent_streams = 100;
  226. multi->last_timeout_ms = -1;
  227. if(Curl_mntfy_resize(multi) ||
  228. Curl_uint32_bset_resize(&multi->process, xfer_table_size) ||
  229. Curl_uint32_bset_resize(&multi->pending, xfer_table_size) ||
  230. Curl_uint32_bset_resize(&multi->dirty, xfer_table_size) ||
  231. Curl_uint32_bset_resize(&multi->msgsent, xfer_table_size) ||
  232. Curl_uint32_tbl_resize(&multi->xfers, xfer_table_size))
  233. goto error;
  234. multi->admin = curl_easy_init();
  235. if(!multi->admin)
  236. goto error;
  237. /* Initialize admin handle to operate inside this multi */
  238. multi->admin->multi = multi;
  239. multi->admin->state.internal = TRUE;
  240. Curl_llist_init(&multi->admin->state.timeoutlist, NULL);
  241. #ifdef DEBUGBUILD
  242. if(getenv("CURL_DEBUG"))
  243. multi->admin->set.verbose = TRUE;
  244. #endif
  245. Curl_uint32_tbl_add(&multi->xfers, multi->admin, &multi->admin->mid);
  246. Curl_uint32_bset_add(&multi->process, multi->admin->mid);
  247. if(Curl_cshutdn_init(&multi->cshutdn, multi))
  248. goto error;
  249. Curl_cpool_init(&multi->cpool, multi->admin, NULL, chashsize);
  250. #ifdef USE_SSL
  251. if(Curl_ssl_scache_create(sesssize, 2, &multi->ssl_scache))
  252. goto error;
  253. #else
  254. (void)sesssize;
  255. #endif
  256. #ifdef USE_WINSOCK
  257. multi->wsa_event = WSACreateEvent();
  258. if(multi->wsa_event == WSA_INVALID_EVENT)
  259. goto error;
  260. #elif defined(ENABLE_WAKEUP)
  261. if(wakeup_create(multi->wakeup_pair, TRUE) < 0) {
  262. multi->wakeup_pair[0] = CURL_SOCKET_BAD;
  263. multi->wakeup_pair[1] = CURL_SOCKET_BAD;
  264. }
  265. #endif
  266. return multi;
  267. error:
  268. Curl_multi_ev_cleanup(multi);
  269. Curl_hash_destroy(&multi->proto_hash);
  270. Curl_dnscache_destroy(&multi->dnscache);
  271. Curl_cpool_destroy(&multi->cpool);
  272. Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
  273. #ifdef USE_SSL
  274. Curl_ssl_scache_destroy(multi->ssl_scache);
  275. #endif
  276. if(multi->admin) {
  277. multi->admin->multi = NULL;
  278. Curl_close(&multi->admin);
  279. }
  280. Curl_mntfy_cleanup(multi);
  281. Curl_uint32_bset_destroy(&multi->process);
  282. Curl_uint32_bset_destroy(&multi->dirty);
  283. Curl_uint32_bset_destroy(&multi->pending);
  284. Curl_uint32_bset_destroy(&multi->msgsent);
  285. Curl_uint32_tbl_destroy(&multi->xfers);
  286. curlx_free(multi);
  287. return NULL;
  288. }
  289. CURLM *curl_multi_init(void)
  290. {
  291. return Curl_multi_handle(CURL_XFER_TABLE_SIZE,
  292. CURL_SOCKET_HASH_TABLE_SIZE,
  293. CURL_CONNECTION_HASH_SIZE,
  294. CURL_DNS_HASH_SIZE,
  295. CURL_TLS_SESSION_SIZE);
  296. }
  297. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  298. static void multi_warn_debug(struct Curl_multi *multi, struct Curl_easy *data)
  299. {
  300. if(!multi->warned) {
  301. infof(data, "!!! WARNING !!!");
  302. infof(data, "This is a debug build of libcurl, "
  303. "do not use in production.");
  304. multi->warned = TRUE;
  305. }
  306. }
  307. #else
  308. #define multi_warn_debug(x, y) Curl_nop_stmt
  309. #endif
  310. static CURLMcode multi_xfers_add(struct Curl_multi *multi,
  311. struct Curl_easy *data)
  312. {
  313. uint32_t capacity = Curl_uint32_tbl_capacity(&multi->xfers);
  314. uint32_t new_size = 0;
  315. /* Prepare to make this into a CURLMOPT_MAX_TRANSFERS, because some
  316. * applications may want to prevent a run-away of their memory use. */
  317. /* UINT_MAX is our "invalid" id, do not let the table grow up to that. */
  318. const uint32_t max_capacity = UINT_MAX - 1;
  319. if(capacity < max_capacity) {
  320. /* We want `multi->xfers` to have "sufficient" free rows, so that we do
  321. * have to reuse the `mid` from a just removed easy right away.
  322. * Since uint_tbl and uint_bset are quite memory efficient,
  323. * regard less than 25% free as insufficient.
  324. * (for low capacities, e.g. multi_easy, 4 or less). */
  325. uint32_t used = Curl_uint32_tbl_count(&multi->xfers);
  326. uint32_t unused = capacity - used;
  327. uint32_t min_unused = CURLMAX(capacity >> 2, 4);
  328. if(unused <= min_unused) {
  329. /* Make sure the uint arithmetic here works on the corner
  330. * cases where we are close to max_capacity or UINT_MAX */
  331. if((min_unused >= max_capacity) ||
  332. ((max_capacity - min_unused) <= capacity) ||
  333. ((UINT_MAX - min_unused - 63) <= capacity)) {
  334. new_size = max_capacity; /* can not be larger than this */
  335. }
  336. else {
  337. /* make it a 64 multiple, since our bitsets frow by that and
  338. * small (easy_multi) grows to at least 64 on first resize. */
  339. new_size = (((used + min_unused) + 63) / 64) * 64;
  340. }
  341. }
  342. }
  343. if(new_size > capacity) {
  344. /* Grow the bitsets first. Should one fail, we do not need
  345. * to downsize the already resized ones. The sets continue
  346. * to work properly when larger than the table, but not
  347. * the other way around. */
  348. CURL_TRC_M(data, "increasing xfer table size to %u", new_size);
  349. if(Curl_uint32_bset_resize(&multi->process, new_size) ||
  350. Curl_uint32_bset_resize(&multi->dirty, new_size) ||
  351. Curl_uint32_bset_resize(&multi->pending, new_size) ||
  352. Curl_uint32_bset_resize(&multi->msgsent, new_size) ||
  353. Curl_uint32_tbl_resize(&multi->xfers, new_size))
  354. return CURLM_OUT_OF_MEMORY;
  355. }
  356. /* Insert the easy into the table now */
  357. if(!Curl_uint32_tbl_add(&multi->xfers, data, &data->mid)) {
  358. /* MUST only happen when table is full */
  359. DEBUGASSERT(Curl_uint32_tbl_capacity(&multi->xfers) <=
  360. Curl_uint32_tbl_count(&multi->xfers));
  361. return CURLM_OUT_OF_MEMORY;
  362. }
  363. return CURLM_OK;
  364. }
  365. CURLMcode curl_multi_add_handle(CURLM *m, CURL *d)
  366. {
  367. CURLMcode mresult;
  368. struct Curl_multi *multi = m;
  369. struct Curl_easy *data = d;
  370. /* First, make some basic checks that the CURLM handle is a good handle */
  371. if(!GOOD_MULTI_HANDLE(multi))
  372. return CURLM_BAD_HANDLE;
  373. /* Verify that we got a somewhat good easy handle too */
  374. if(!GOOD_EASY_HANDLE(data))
  375. return CURLM_BAD_EASY_HANDLE;
  376. /* Prevent users from adding same easy handle more than once and prevent
  377. adding to more than one multi stack */
  378. if(data->multi)
  379. return CURLM_ADDED_ALREADY;
  380. if(multi->in_callback)
  381. return CURLM_RECURSIVE_API_CALL;
  382. if(multi->dead) {
  383. /* a "dead" handle cannot get added transfers while any existing easy
  384. handles are still alive - but if there are none alive anymore, it is
  385. fine to start over and unmark the "deadness" of this handle.
  386. This means only the admin handle MUST be present. */
  387. if((Curl_uint32_tbl_count(&multi->xfers) != 1) ||
  388. !Curl_uint32_tbl_contains(&multi->xfers, 0))
  389. return CURLM_ABORTED_BY_CALLBACK;
  390. multi->dead = FALSE;
  391. Curl_uint32_bset_clear(&multi->process);
  392. Curl_uint32_bset_clear(&multi->dirty);
  393. Curl_uint32_bset_clear(&multi->pending);
  394. Curl_uint32_bset_clear(&multi->msgsent);
  395. }
  396. if(data->multi_easy) {
  397. /* if this easy handle was previously used for curl_easy_perform(), there
  398. is a private multi handle here that we can kill */
  399. curl_multi_cleanup(data->multi_easy);
  400. data->multi_easy = NULL;
  401. }
  402. /* Insert the easy into the multi->xfers table, assigning it a `mid`. */
  403. if(multi_xfers_add(multi, data))
  404. return CURLM_OUT_OF_MEMORY;
  405. /* Initialize timeout list for this handle */
  406. Curl_llist_init(&data->state.timeoutlist, NULL);
  407. /*
  408. * No failure allowed in this function beyond this point. No modification of
  409. * easy nor multi handle allowed before this except for potential multi's
  410. * connection pool growing which will not be undone in this function no
  411. * matter what.
  412. */
  413. if(data->set.errorbuffer)
  414. data->set.errorbuffer[0] = 0;
  415. data->state.os_errno = 0;
  416. /* make the Curl_easy refer back to this multi handle - before Curl_expire()
  417. is called. */
  418. data->multi = multi;
  419. /* set the easy handle */
  420. multistate(data, MSTATE_INIT);
  421. #ifdef USE_LIBPSL
  422. /* Do the same for PSL. */
  423. if(data->share && (data->share->specifier & (1 << CURL_LOCK_DATA_PSL)))
  424. data->psl = &data->share->psl;
  425. else
  426. data->psl = &multi->psl;
  427. #endif
  428. /* add the easy handle to the process set */
  429. Curl_uint32_bset_add(&multi->process, data->mid);
  430. ++multi->xfers_alive;
  431. ++multi->xfers_total_ever;
  432. Curl_cpool_xfer_init(data);
  433. multi_warn_debug(multi, data);
  434. /* Make sure the new handle will run */
  435. Curl_multi_mark_dirty(data);
  436. /* Necessary in event based processing, where dirty handles trigger
  437. * a timeout callback invocation. */
  438. mresult = Curl_update_timer(multi);
  439. if(mresult) {
  440. data->multi = NULL; /* not anymore */
  441. Curl_uint32_tbl_remove(&multi->xfers, data->mid);
  442. data->mid = UINT32_MAX;
  443. return mresult;
  444. }
  445. /* The admin handle only ever has default timeouts set. To improve the
  446. state somewhat we clone the timeouts from each added handle so that the
  447. admin handle always has the same timeouts as the most recently added
  448. easy handle. */
  449. multi->admin->set.timeout = data->set.timeout;
  450. multi->admin->set.server_response_timeout =
  451. data->set.server_response_timeout;
  452. multi->admin->set.no_signal = data->set.no_signal;
  453. CURL_TRC_M(data, "added to multi, mid=%u, running=%u, total=%u",
  454. data->mid, Curl_multi_xfers_running(multi),
  455. Curl_uint32_tbl_count(&multi->xfers));
  456. return CURLM_OK;
  457. }
  458. #if 0
  459. /* Debug-function, used like this:
  460. *
  461. * Curl_hash_print(&multi->sockhash, debug_print_sock_hash);
  462. *
  463. * Enable the hash print function first by editing hash.c
  464. */
  465. static void debug_print_sock_hash(void *p)
  466. {
  467. struct Curl_sh_entry *sh = (struct Curl_sh_entry *)p;
  468. curl_mfprintf(stderr, " [readers %u][writers %u]",
  469. sh->readers, sh->writers);
  470. }
  471. #endif
  472. struct multi_done_ctx {
  473. BIT(premature);
  474. };
  475. static bool multi_conn_should_close(struct connectdata *conn,
  476. struct Curl_easy *data,
  477. bool premature)
  478. {
  479. /* if conn->bits.close is TRUE, it means that the connection should be
  480. closed in spite of everything else. */
  481. if(conn->bits.close)
  482. return TRUE;
  483. /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
  484. forced us to close this connection. This is ignored for requests taking
  485. place in a NTLM/NEGOTIATE authentication handshake. */
  486. if(data->set.reuse_forbid
  487. #ifdef USE_NTLM
  488. && !(conn->http_ntlm_state == NTLMSTATE_TYPE2 ||
  489. conn->proxy_ntlm_state == NTLMSTATE_TYPE2)
  490. #endif
  491. #ifdef USE_SPNEGO
  492. && !(conn->http_negotiate_state == GSS_AUTHRECV ||
  493. conn->proxy_negotiate_state == GSS_AUTHRECV)
  494. #endif
  495. )
  496. return TRUE;
  497. /* Unless this connection is for a "connect-only" transfer, it
  498. * needs to be closed if the protocol handler does not support reuse. */
  499. if(!data->set.connect_only && conn->handler &&
  500. !(conn->handler->flags & PROTOPT_CONN_REUSE))
  501. return TRUE;
  502. /* if premature is TRUE, it means this connection was said to be DONE before
  503. the entire request operation is complete and thus we cannot know in what
  504. state it is for reusing, so we are forced to close it. In a perfect world
  505. we can add code that keep track of if we really must close it here or not,
  506. but currently we have no such detail knowledge. */
  507. if(premature && !Curl_conn_is_multiplex(conn, FIRSTSOCKET))
  508. return TRUE;
  509. return FALSE;
  510. }
  511. static void multi_done_locked(struct connectdata *conn,
  512. struct Curl_easy *data,
  513. void *userdata)
  514. {
  515. struct multi_done_ctx *mdctx = userdata;
  516. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  517. const char *host =
  518. #ifndef CURL_DISABLE_PROXY
  519. conn->bits.socksproxy ?
  520. conn->socks_proxy.host.dispname :
  521. conn->bits.httpproxy ? conn->http_proxy.host.dispname :
  522. #endif
  523. conn->bits.conn_to_host ? conn->conn_to_host.dispname :
  524. conn->host.dispname;
  525. int port =
  526. #ifndef CURL_DISABLE_PROXY
  527. conn->bits.httpproxy ? conn->http_proxy.port :
  528. #endif
  529. conn->bits.conn_to_port ? conn->conn_to_port :
  530. conn->remote_port;
  531. #endif
  532. Curl_detach_connection(data);
  533. CURL_TRC_M(data, "multi_done_locked, in use=%u", conn->attached_xfers);
  534. if(CONN_INUSE(conn)) {
  535. /* Stop if still used. */
  536. CURL_TRC_M(data, "Connection still in use %u, no more multi_done now!",
  537. conn->attached_xfers);
  538. return;
  539. }
  540. data->state.done = TRUE; /* called just now! */
  541. data->state.recent_conn_id = conn->connection_id;
  542. Curl_resolv_unlink(data, &data->state.dns[0]); /* done with this */
  543. Curl_resolv_unlink(data, &data->state.dns[1]);
  544. Curl_dnscache_prune(data);
  545. if(multi_conn_should_close(conn, data, mdctx->premature)) {
  546. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  547. CURL_TRC_M(data, "multi_done, terminating conn #%" FMT_OFF_T " to %s:%d, "
  548. "forbid=%d, close=%d, premature=%d, conn_multiplex=%d",
  549. conn->connection_id, host, port, data->set.reuse_forbid,
  550. conn->bits.close, mdctx->premature,
  551. Curl_conn_is_multiplex(conn, FIRSTSOCKET));
  552. #endif
  553. connclose(conn, "disconnecting");
  554. Curl_conn_terminate(data, conn, mdctx->premature);
  555. }
  556. else if(!Curl_conn_get_max_concurrent(data, conn, FIRSTSOCKET)) {
  557. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  558. CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s:%d was shutdown"
  559. " by server, not reusing", conn->connection_id, host, port);
  560. #endif
  561. connclose(conn, "server shutdown");
  562. Curl_conn_terminate(data, conn, mdctx->premature);
  563. }
  564. else {
  565. /* the connection is no longer in use by any transfer */
  566. if(Curl_cpool_conn_now_idle(data, conn)) {
  567. /* connection kept in the cpool */
  568. data->state.lastconnect_id = conn->connection_id;
  569. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  570. infof(data, "Connection #%" FMT_OFF_T " to host %s:%d left intact",
  571. conn->connection_id, host, port);
  572. #endif
  573. }
  574. else {
  575. /* connection was removed from the cpool and destroyed. */
  576. data->state.lastconnect_id = -1;
  577. }
  578. }
  579. }
  580. static CURLcode multi_done(struct Curl_easy *data,
  581. CURLcode status, /* an error if this is called
  582. after an error was detected */
  583. bool premature)
  584. {
  585. CURLcode result;
  586. struct connectdata *conn = data->conn;
  587. struct multi_done_ctx mdctx;
  588. memset(&mdctx, 0, sizeof(mdctx));
  589. CURL_TRC_M(data, "multi_done: status: %d prem: %d done: %d",
  590. (int)status, (int)premature, data->state.done);
  591. if(data->state.done)
  592. /* Stop if multi_done() has already been called */
  593. return CURLE_OK;
  594. /* Shut down any ongoing async resolver operation. */
  595. Curl_async_shutdown(data);
  596. /* Cleanup possible redirect junk */
  597. Curl_safefree(data->req.newurl);
  598. Curl_safefree(data->req.location);
  599. switch(status) {
  600. case CURLE_ABORTED_BY_CALLBACK:
  601. case CURLE_READ_ERROR:
  602. case CURLE_WRITE_ERROR:
  603. /* When we are aborted due to a callback return code it basically have to
  604. be counted as premature as there is trouble ahead if we do not. We have
  605. many callbacks and protocols work differently, we could potentially do
  606. this more fine-grained in the future. */
  607. premature = TRUE;
  608. FALLTHROUGH();
  609. default:
  610. break;
  611. }
  612. /* this calls the protocol-specific function pointer previously set */
  613. if(conn->handler->done && (data->mstate >= MSTATE_PROTOCONNECT))
  614. result = conn->handler->done(data, status, premature);
  615. else
  616. result = status;
  617. if(CURLE_ABORTED_BY_CALLBACK != result) {
  618. /* avoid this if we already aborted by callback to avoid this calling
  619. another callback */
  620. int rc = Curl_pgrsDone(data);
  621. if(!result && rc)
  622. result = CURLE_ABORTED_BY_CALLBACK;
  623. }
  624. /* Make sure that transfer client writes are really done now. */
  625. result = Curl_1st_err(result, Curl_xfer_write_done(data, premature));
  626. /* Inform connection filters that this transfer is done */
  627. Curl_conn_ev_data_done(data, premature);
  628. process_pending_handles(data->multi); /* connection / multiplex */
  629. if(!result)
  630. result = Curl_req_done(&data->req, data, premature);
  631. /* Under the potential connection pool's share lock, decide what to
  632. * do with the transfer's connection. */
  633. mdctx.premature = premature;
  634. Curl_cpool_do_locked(data, data->conn, multi_done_locked, &mdctx);
  635. /* flush the netrc cache */
  636. Curl_netrc_cleanup(&data->state.netrc);
  637. return result;
  638. }
  639. static void close_connect_only(struct connectdata *conn,
  640. struct Curl_easy *data,
  641. void *userdata)
  642. {
  643. (void)userdata;
  644. (void)data;
  645. if(conn->connect_only)
  646. connclose(conn, "Removing connect-only easy handle");
  647. }
  648. CURLMcode curl_multi_remove_handle(CURLM *m, CURL *d)
  649. {
  650. struct Curl_multi *multi = m;
  651. struct Curl_easy *data = d;
  652. bool premature;
  653. struct Curl_llist_node *e;
  654. CURLMcode mresult;
  655. bool removed_timer = FALSE;
  656. uint32_t mid;
  657. /* First, make some basic checks that the CURLM handle is a good handle */
  658. if(!GOOD_MULTI_HANDLE(multi))
  659. return CURLM_BAD_HANDLE;
  660. /* Verify that we got a somewhat good easy handle too */
  661. if(!GOOD_EASY_HANDLE(data))
  662. return CURLM_BAD_EASY_HANDLE;
  663. /* Prevent users from trying to remove same easy handle more than once */
  664. if(!data->multi)
  665. return CURLM_OK; /* it is already removed so let's say it is fine! */
  666. /* Prevent users from trying to remove an easy handle from the wrong multi */
  667. if(data->multi != multi)
  668. return CURLM_BAD_EASY_HANDLE;
  669. if(data->mid == UINT32_MAX) {
  670. DEBUGASSERT(0);
  671. return CURLM_INTERNAL_ERROR;
  672. }
  673. if(Curl_uint32_tbl_get(&multi->xfers, data->mid) != data) {
  674. DEBUGASSERT(0);
  675. return CURLM_INTERNAL_ERROR;
  676. }
  677. if(multi->in_callback)
  678. return CURLM_RECURSIVE_API_CALL;
  679. premature = (data->mstate < MSTATE_COMPLETED);
  680. /* If the 'state' is not INIT or COMPLETED, we might need to do something
  681. nice to put the easy_handle in a good known state when this returns. */
  682. if(data->conn &&
  683. data->mstate > MSTATE_DO &&
  684. data->mstate < MSTATE_COMPLETED) {
  685. /* Set connection owner so that the DONE function closes it. We can
  686. safely do this here since connection is killed. */
  687. streamclose(data->conn, "Removed with partial response");
  688. }
  689. if(data->conn) {
  690. /* multi_done() clears the association between the easy handle and the
  691. connection.
  692. Note that this ignores the return code simply because there is
  693. nothing really useful to do with it anyway! */
  694. (void)multi_done(data, data->result, premature);
  695. }
  696. /* The timer must be shut down before data->multi is set to NULL, else the
  697. timenode will remain in the splay tree after curl_easy_cleanup is
  698. called. Do it after multi_done() in case that sets another time! */
  699. removed_timer = Curl_expire_clear(data);
  700. /* If in `msgsent`, it was deducted from `multi->xfers_alive` already. */
  701. if(!Curl_uint32_bset_contains(&multi->msgsent, data->mid))
  702. --multi->xfers_alive;
  703. Curl_wildcard_dtor(&data->wildcard);
  704. data->mstate = MSTATE_COMPLETED;
  705. /* Remove the association between the connection and the handle */
  706. Curl_detach_connection(data);
  707. /* Tell event handling that this transfer is definitely going away */
  708. Curl_multi_ev_xfer_done(multi, data);
  709. if(data->set.connect_only && !data->multi_easy) {
  710. /* This removes a handle that was part the multi interface that used
  711. CONNECT_ONLY, that connection is now left alive but since this handle
  712. has bits.close set nothing can use that transfer anymore and it is
  713. forbidden from reuse. This easy handle cannot find the connection
  714. anymore once removed from the multi handle
  715. Better close the connection here, at once.
  716. */
  717. struct connectdata *c;
  718. curl_socket_t s;
  719. s = Curl_getconnectinfo(data, &c);
  720. if((s != CURL_SOCKET_BAD) && c) {
  721. Curl_conn_terminate(data, c, TRUE);
  722. }
  723. }
  724. if(data->state.lastconnect_id != -1) {
  725. /* Mark any connect-only connection for closure */
  726. Curl_cpool_do_by_id(data, data->state.lastconnect_id,
  727. close_connect_only, NULL);
  728. }
  729. #ifdef USE_LIBPSL
  730. /* Remove the PSL association. */
  731. if(data->psl == &multi->psl)
  732. data->psl = NULL;
  733. #endif
  734. /* make sure there is no pending message in the queue sent from this easy
  735. handle */
  736. for(e = Curl_llist_head(&multi->msglist); e; e = Curl_node_next(e)) {
  737. struct Curl_message *msg = Curl_node_elem(e);
  738. if(msg->extmsg.easy_handle == data) {
  739. Curl_node_remove(e);
  740. /* there can only be one from this specific handle */
  741. break;
  742. }
  743. }
  744. /* clear the association to this multi handle */
  745. mid = data->mid;
  746. DEBUGASSERT(Curl_uint32_tbl_contains(&multi->xfers, mid));
  747. Curl_uint32_tbl_remove(&multi->xfers, mid);
  748. Curl_uint32_bset_remove(&multi->process, mid);
  749. Curl_uint32_bset_remove(&multi->dirty, mid);
  750. Curl_uint32_bset_remove(&multi->pending, mid);
  751. Curl_uint32_bset_remove(&multi->msgsent, mid);
  752. data->multi = NULL;
  753. data->mid = UINT32_MAX;
  754. data->master_mid = UINT32_MAX;
  755. /* NOTE NOTE NOTE
  756. We do not touch the easy handle here! */
  757. process_pending_handles(multi);
  758. if(removed_timer) {
  759. mresult = Curl_update_timer(multi);
  760. if(mresult)
  761. return mresult;
  762. }
  763. CURL_TRC_M(data, "removed from multi, mid=%u, running=%u, total=%u",
  764. mid, Curl_multi_xfers_running(multi),
  765. Curl_uint32_tbl_count(&multi->xfers));
  766. return CURLM_OK;
  767. }
  768. /* Return TRUE if the application asked for multiplexing */
  769. bool Curl_multiplex_wanted(const struct Curl_multi *multi)
  770. {
  771. return multi && multi->multiplexing;
  772. }
  773. /*
  774. * Curl_detach_connection() removes the given transfer from the connection.
  775. *
  776. * This is the only function that should clear data->conn. This will
  777. * occasionally be called with the data->conn pointer already cleared.
  778. */
  779. void Curl_detach_connection(struct Curl_easy *data)
  780. {
  781. struct connectdata *conn = data->conn;
  782. if(conn) {
  783. /* this should never happen, prevent underflow */
  784. DEBUGASSERT(conn->attached_xfers);
  785. if(conn->attached_xfers) {
  786. conn->attached_xfers--;
  787. if(!conn->attached_xfers)
  788. conn->attached_multi = NULL;
  789. }
  790. }
  791. data->conn = NULL;
  792. }
  793. /*
  794. * Curl_attach_connection() attaches this transfer to this connection.
  795. *
  796. * This is the only function that should assign data->conn
  797. */
  798. void Curl_attach_connection(struct Curl_easy *data,
  799. struct connectdata *conn)
  800. {
  801. DEBUGASSERT(data);
  802. DEBUGASSERT(!data->conn);
  803. DEBUGASSERT(conn);
  804. DEBUGASSERT(conn->attached_xfers < UINT32_MAX);
  805. data->conn = conn;
  806. conn->attached_xfers++;
  807. /* all attached transfers must be from the same multi */
  808. if(!conn->attached_multi)
  809. conn->attached_multi = data->multi;
  810. DEBUGASSERT(conn->attached_multi == data->multi);
  811. if(conn->handler && conn->handler->attach)
  812. conn->handler->attach(data, conn);
  813. }
  814. /* adjust pollset for rate limits/pauses */
  815. static CURLcode multi_adjust_pollset(struct Curl_easy *data,
  816. struct easy_pollset *ps)
  817. {
  818. CURLcode result = CURLE_OK;
  819. if(ps->n) {
  820. const struct curltime *pnow = Curl_pgrs_now(data);
  821. bool send_blocked, recv_blocked;
  822. recv_blocked = (Curl_rlimit_avail(&data->progress.dl.rlimit, pnow) <= 0);
  823. send_blocked = (Curl_rlimit_avail(&data->progress.ul.rlimit, pnow) <= 0);
  824. if(send_blocked || recv_blocked) {
  825. int i;
  826. for(i = 0; i <= SECONDARYSOCKET; ++i) {
  827. curl_socket_t sock = data->conn->sock[i];
  828. if(sock == CURL_SOCKET_BAD)
  829. continue;
  830. if(recv_blocked && Curl_pollset_want_recv(data, ps, sock)) {
  831. result = Curl_pollset_remove_in(data, ps, sock);
  832. if(result)
  833. break;
  834. }
  835. if(send_blocked && Curl_pollset_want_send(data, ps, sock)) {
  836. result = Curl_pollset_remove_out(data, ps, sock);
  837. if(result)
  838. break;
  839. }
  840. }
  841. }
  842. /* Not blocked and wanting to receive. If there is data pending
  843. * in the connection filters, make transfer run again. */
  844. if(!recv_blocked &&
  845. ((Curl_pollset_want_recv(data, ps, data->conn->sock[FIRSTSOCKET]) &&
  846. Curl_conn_data_pending(data, FIRSTSOCKET)) ||
  847. (Curl_pollset_want_recv(data, ps, data->conn->sock[SECONDARYSOCKET]) &&
  848. Curl_conn_data_pending(data, SECONDARYSOCKET)))) {
  849. CURL_TRC_M(data, "pollset[] has POLLIN, but there is still "
  850. "buffered input -> mark as dirty");
  851. Curl_multi_mark_dirty(data);
  852. }
  853. }
  854. return result;
  855. }
  856. static CURLcode mstate_connecting_pollset(struct Curl_easy *data,
  857. struct easy_pollset *ps)
  858. {
  859. struct connectdata *conn = data->conn;
  860. curl_socket_t sockfd;
  861. CURLcode result = CURLE_OK;
  862. if(Curl_xfer_recv_is_paused(data))
  863. return CURLE_OK;
  864. /* If a socket is set, receiving is default. If the socket
  865. * has not been determined yet (eyeballing), always ask the
  866. * connection filters for what to monitor. */
  867. sockfd = Curl_conn_get_first_socket(data);
  868. if(sockfd != CURL_SOCKET_BAD) {
  869. result = Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0);
  870. if(!result)
  871. result = multi_adjust_pollset(data, ps);
  872. }
  873. if(!result)
  874. result = Curl_conn_adjust_pollset(data, conn, ps);
  875. return result;
  876. }
  877. static CURLcode mstate_protocol_pollset(struct Curl_easy *data,
  878. struct easy_pollset *ps)
  879. {
  880. struct connectdata *conn = data->conn;
  881. CURLcode result = CURLE_OK;
  882. if(conn->handler->proto_pollset)
  883. result = conn->handler->proto_pollset(data, ps);
  884. else {
  885. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  886. if(sockfd != CURL_SOCKET_BAD) {
  887. /* Default is to wait to something from the server */
  888. result = Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0);
  889. }
  890. }
  891. if(!result)
  892. result = multi_adjust_pollset(data, ps);
  893. if(!result)
  894. result = Curl_conn_adjust_pollset(data, conn, ps);
  895. return result;
  896. }
  897. static CURLcode mstate_do_pollset(struct Curl_easy *data,
  898. struct easy_pollset *ps)
  899. {
  900. struct connectdata *conn = data->conn;
  901. CURLcode result = CURLE_OK;
  902. if(conn->handler->doing_pollset)
  903. result = conn->handler->doing_pollset(data, ps);
  904. else if(CONN_SOCK_IDX_VALID(conn->send_idx)) {
  905. /* Default is that we want to send something to the server */
  906. result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
  907. }
  908. if(!result)
  909. result = multi_adjust_pollset(data, ps);
  910. if(!result)
  911. result = Curl_conn_adjust_pollset(data, conn, ps);
  912. return result;
  913. }
  914. static CURLcode mstate_domore_pollset(struct Curl_easy *data,
  915. struct easy_pollset *ps)
  916. {
  917. struct connectdata *conn = data->conn;
  918. CURLcode result = CURLE_OK;
  919. if(conn->handler->domore_pollset)
  920. result = conn->handler->domore_pollset(data, ps);
  921. else if(CONN_SOCK_IDX_VALID(conn->send_idx)) {
  922. /* Default is that we want to send something to the server */
  923. result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
  924. }
  925. if(!result)
  926. result = multi_adjust_pollset(data, ps);
  927. if(!result)
  928. result = Curl_conn_adjust_pollset(data, conn, ps);
  929. return result;
  930. }
  931. static CURLcode mstate_perform_pollset(struct Curl_easy *data,
  932. struct easy_pollset *ps)
  933. {
  934. struct connectdata *conn = data->conn;
  935. CURLcode result = CURLE_OK;
  936. if(conn->handler->perform_pollset)
  937. result = conn->handler->perform_pollset(data, ps);
  938. else {
  939. /* Default is to obey the data->req.keepon flags for send/recv */
  940. if(Curl_req_want_recv(data) && CONN_SOCK_IDX_VALID(conn->recv_idx)) {
  941. result = Curl_pollset_add_in(data, ps, conn->sock[conn->recv_idx]);
  942. }
  943. if(!result && Curl_req_want_send(data) &&
  944. CONN_SOCK_IDX_VALID(conn->send_idx)) {
  945. result = Curl_pollset_add_out(data, ps, conn->sock[conn->send_idx]);
  946. }
  947. }
  948. if(!result)
  949. result = multi_adjust_pollset(data, ps);
  950. if(!result)
  951. result = Curl_conn_adjust_pollset(data, conn, ps);
  952. return result;
  953. }
  954. /* Initializes `poll_set` with the current socket poll actions needed
  955. * for transfer `data`. */
  956. CURLMcode Curl_multi_pollset(struct Curl_easy *data,
  957. struct easy_pollset *ps)
  958. {
  959. CURLMcode mresult = CURLM_OK;
  960. CURLcode result = CURLE_OK;
  961. /* If the transfer has no connection, this is fine. Happens when
  962. called via curl_multi_remove_handle() => Curl_multi_ev_assess() =>
  963. Curl_multi_pollset(). */
  964. Curl_pollset_reset(ps);
  965. if(!data->conn)
  966. return CURLM_OK;
  967. switch(data->mstate) {
  968. case MSTATE_INIT:
  969. case MSTATE_PENDING:
  970. case MSTATE_SETUP:
  971. case MSTATE_CONNECT:
  972. /* nothing to poll for yet */
  973. break;
  974. case MSTATE_RESOLVING:
  975. result = Curl_resolv_pollset(data, ps);
  976. break;
  977. case MSTATE_CONNECTING:
  978. result = mstate_connecting_pollset(data, ps);
  979. break;
  980. case MSTATE_PROTOCONNECT:
  981. case MSTATE_PROTOCONNECTING:
  982. result = mstate_protocol_pollset(data, ps);
  983. break;
  984. case MSTATE_DO:
  985. case MSTATE_DOING:
  986. result = mstate_do_pollset(data, ps);
  987. break;
  988. case MSTATE_DOING_MORE:
  989. result = mstate_domore_pollset(data, ps);
  990. break;
  991. case MSTATE_DID: /* same as PERFORMING in regard to polling */
  992. case MSTATE_PERFORMING:
  993. result = mstate_perform_pollset(data, ps);
  994. break;
  995. case MSTATE_RATELIMITING:
  996. /* we need to let time pass, ignore socket(s) */
  997. break;
  998. case MSTATE_DONE:
  999. case MSTATE_COMPLETED:
  1000. case MSTATE_MSGSENT:
  1001. /* nothing more to poll for */
  1002. break;
  1003. default:
  1004. failf(data, "multi_getsock: unexpected multi state %d", data->mstate);
  1005. DEBUGASSERT(0);
  1006. break;
  1007. }
  1008. if(result) {
  1009. if(result == CURLE_OUT_OF_MEMORY)
  1010. mresult = CURLM_OUT_OF_MEMORY;
  1011. else {
  1012. failf(data, "error determining pollset: %d", result);
  1013. mresult = CURLM_INTERNAL_ERROR;
  1014. }
  1015. goto out;
  1016. }
  1017. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1018. if(CURL_TRC_M_is_verbose(data)) {
  1019. size_t timeout_count = Curl_llist_count(&data->state.timeoutlist);
  1020. switch(ps->n) {
  1021. case 0:
  1022. CURL_TRC_M(data, "pollset[], timeouts=%zu, paused %d/%d (r/w)",
  1023. timeout_count,
  1024. Curl_xfer_send_is_paused(data),
  1025. Curl_xfer_recv_is_paused(data));
  1026. break;
  1027. case 1:
  1028. CURL_TRC_M(data, "pollset[fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu",
  1029. ps->sockets[0],
  1030. (ps->actions[0] & CURL_POLL_IN) ? "IN" : "",
  1031. (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "",
  1032. timeout_count);
  1033. break;
  1034. case 2:
  1035. CURL_TRC_M(data, "pollset[fd=%" FMT_SOCKET_T " %s%s, "
  1036. "fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu",
  1037. ps->sockets[0],
  1038. (ps->actions[0] & CURL_POLL_IN) ? "IN" : "",
  1039. (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "",
  1040. ps->sockets[1],
  1041. (ps->actions[1] & CURL_POLL_IN) ? "IN" : "",
  1042. (ps->actions[1] & CURL_POLL_OUT) ? "OUT" : "",
  1043. timeout_count);
  1044. break;
  1045. default:
  1046. CURL_TRC_M(data, "pollset[fds=%u], timeouts=%zu", ps->n, timeout_count);
  1047. break;
  1048. }
  1049. CURL_TRC_EASY_TIMERS(data);
  1050. }
  1051. #endif
  1052. out:
  1053. return mresult;
  1054. }
  1055. CURLMcode curl_multi_fdset(CURLM *m,
  1056. fd_set *read_fd_set, fd_set *write_fd_set,
  1057. fd_set *exc_fd_set, int *max_fd)
  1058. {
  1059. /* Scan through all the easy handles to get the file descriptors set.
  1060. Some easy handles may not have connected to the remote host yet,
  1061. and then we must make sure that is done. */
  1062. int this_max_fd = -1;
  1063. struct Curl_multi *multi = m;
  1064. struct easy_pollset ps;
  1065. unsigned int i;
  1066. uint32_t mid;
  1067. (void)exc_fd_set;
  1068. if(!GOOD_MULTI_HANDLE(multi))
  1069. return CURLM_BAD_HANDLE;
  1070. if(multi->in_callback)
  1071. return CURLM_RECURSIVE_API_CALL;
  1072. Curl_pollset_init(&ps);
  1073. if(Curl_uint32_bset_first(&multi->process, &mid)) {
  1074. do {
  1075. struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
  1076. if(!data) {
  1077. DEBUGASSERT(0);
  1078. continue;
  1079. }
  1080. Curl_multi_pollset(data, &ps);
  1081. for(i = 0; i < ps.n; i++) {
  1082. if(!FDSET_SOCK(ps.sockets[i]))
  1083. /* pretend it does not exist */
  1084. continue;
  1085. #ifdef __DJGPP__
  1086. #pragma GCC diagnostic push
  1087. #pragma GCC diagnostic ignored "-Warith-conversion"
  1088. #endif
  1089. if(ps.actions[i] & CURL_POLL_IN)
  1090. FD_SET(ps.sockets[i], read_fd_set);
  1091. if(ps.actions[i] & CURL_POLL_OUT)
  1092. FD_SET(ps.sockets[i], write_fd_set);
  1093. #ifdef __DJGPP__
  1094. #pragma GCC diagnostic pop
  1095. #endif
  1096. if((int)ps.sockets[i] > this_max_fd)
  1097. this_max_fd = (int)ps.sockets[i];
  1098. }
  1099. } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
  1100. }
  1101. Curl_cshutdn_setfds(&multi->cshutdn, multi->admin,
  1102. read_fd_set, write_fd_set, &this_max_fd);
  1103. *max_fd = this_max_fd;
  1104. Curl_pollset_cleanup(&ps);
  1105. return CURLM_OK;
  1106. }
  1107. CURLMcode curl_multi_waitfds(CURLM *m,
  1108. struct curl_waitfd *ufds,
  1109. unsigned int size,
  1110. unsigned int *fd_count)
  1111. {
  1112. struct Curl_waitfds cwfds;
  1113. CURLMcode mresult = CURLM_OK;
  1114. struct Curl_multi *multi = m;
  1115. struct easy_pollset ps;
  1116. unsigned int need = 0;
  1117. uint32_t mid;
  1118. if(!ufds && (size || !fd_count))
  1119. return CURLM_BAD_FUNCTION_ARGUMENT;
  1120. if(!GOOD_MULTI_HANDLE(multi))
  1121. return CURLM_BAD_HANDLE;
  1122. if(multi->in_callback)
  1123. return CURLM_RECURSIVE_API_CALL;
  1124. Curl_pollset_init(&ps);
  1125. Curl_waitfds_init(&cwfds, ufds, size);
  1126. if(Curl_uint32_bset_first(&multi->process, &mid)) {
  1127. do {
  1128. struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
  1129. if(!data) {
  1130. DEBUGASSERT(0);
  1131. Curl_uint32_bset_remove(&multi->process, mid);
  1132. Curl_uint32_bset_remove(&multi->dirty, mid);
  1133. continue;
  1134. }
  1135. Curl_multi_pollset(data, &ps);
  1136. need += Curl_waitfds_add_ps(&cwfds, &ps);
  1137. } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
  1138. }
  1139. need += Curl_cshutdn_add_waitfds(&multi->cshutdn, multi->admin, &cwfds);
  1140. if(need != cwfds.n && ufds)
  1141. mresult = CURLM_OUT_OF_MEMORY;
  1142. if(fd_count)
  1143. *fd_count = need;
  1144. Curl_pollset_cleanup(&ps);
  1145. return mresult;
  1146. }
  1147. #ifdef USE_WINSOCK
  1148. /* Reset FD_WRITE for TCP sockets. Nothing is actually sent. UDP sockets cannot
  1149. * be reset this way because an empty datagram would be sent. #9203
  1150. *
  1151. * "On Windows the internal state of FD_WRITE as returned from
  1152. * WSAEnumNetworkEvents is only reset after successful send()."
  1153. */
  1154. static void reset_socket_fdwrite(curl_socket_t s)
  1155. {
  1156. int t;
  1157. int l = (int)sizeof(t);
  1158. if(!getsockopt(s, SOL_SOCKET, SO_TYPE, (char *)&t, &l) && t == SOCK_STREAM)
  1159. send(s, NULL, 0, 0);
  1160. }
  1161. #endif
  1162. #define NUM_POLLS_ON_STACK 10
  1163. static CURLMcode multi_wait(struct Curl_multi *multi,
  1164. struct curl_waitfd extra_fds[],
  1165. unsigned int extra_nfds,
  1166. int timeout_ms,
  1167. int *ret,
  1168. bool extrawait, /* when no socket, wait */
  1169. bool use_wakeup)
  1170. {
  1171. size_t i;
  1172. struct curltime expire_time;
  1173. long timeout_internal;
  1174. int retcode = 0;
  1175. struct easy_pollset ps;
  1176. struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
  1177. struct curl_pollfds cpfds;
  1178. unsigned int curl_nfds = 0; /* how many pfds are for curl transfers */
  1179. struct Curl_easy *data = NULL;
  1180. CURLMcode mresult = CURLM_OK;
  1181. uint32_t mid;
  1182. #ifdef USE_WINSOCK
  1183. WSANETWORKEVENTS wsa_events;
  1184. DEBUGASSERT(multi->wsa_event != WSA_INVALID_EVENT);
  1185. #endif
  1186. #ifndef ENABLE_WAKEUP
  1187. (void)use_wakeup;
  1188. #endif
  1189. if(!GOOD_MULTI_HANDLE(multi))
  1190. return CURLM_BAD_HANDLE;
  1191. if(multi->in_callback)
  1192. return CURLM_RECURSIVE_API_CALL;
  1193. if(timeout_ms < 0)
  1194. return CURLM_BAD_FUNCTION_ARGUMENT;
  1195. Curl_pollset_init(&ps);
  1196. Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK);
  1197. /* Add the curl handles to our pollfds first */
  1198. if(Curl_uint32_bset_first(&multi->process, &mid)) {
  1199. do {
  1200. data = Curl_multi_get_easy(multi, mid);
  1201. if(!data) {
  1202. DEBUGASSERT(0);
  1203. Curl_uint32_bset_remove(&multi->process, mid);
  1204. Curl_uint32_bset_remove(&multi->dirty, mid);
  1205. continue;
  1206. }
  1207. Curl_multi_pollset(data, &ps);
  1208. if(Curl_pollfds_add_ps(&cpfds, &ps)) {
  1209. mresult = CURLM_OUT_OF_MEMORY;
  1210. goto out;
  1211. }
  1212. } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
  1213. }
  1214. if(Curl_cshutdn_add_pollfds(&multi->cshutdn, multi->admin, &cpfds)) {
  1215. mresult = CURLM_OUT_OF_MEMORY;
  1216. goto out;
  1217. }
  1218. curl_nfds = cpfds.n; /* what curl internally uses in cpfds */
  1219. /* Add external file descriptions from poll-like struct curl_waitfd */
  1220. for(i = 0; i < extra_nfds; i++) {
  1221. unsigned short events = 0;
  1222. if(extra_fds[i].events & CURL_WAIT_POLLIN)
  1223. events |= POLLIN;
  1224. if(extra_fds[i].events & CURL_WAIT_POLLPRI)
  1225. events |= POLLPRI;
  1226. if(extra_fds[i].events & CURL_WAIT_POLLOUT)
  1227. events |= POLLOUT;
  1228. if(Curl_pollfds_add_sock(&cpfds, extra_fds[i].fd, events)) {
  1229. mresult = CURLM_OUT_OF_MEMORY;
  1230. goto out;
  1231. }
  1232. }
  1233. #ifdef USE_WINSOCK
  1234. /* Set the WSA events based on the collected pollds */
  1235. for(i = 0; i < cpfds.n; i++) {
  1236. long mask = 0;
  1237. if(cpfds.pfds[i].events & POLLIN)
  1238. mask |= FD_READ | FD_ACCEPT | FD_CLOSE;
  1239. if(cpfds.pfds[i].events & POLLPRI)
  1240. mask |= FD_OOB;
  1241. if(cpfds.pfds[i].events & POLLOUT) {
  1242. mask |= FD_WRITE | FD_CONNECT | FD_CLOSE;
  1243. reset_socket_fdwrite(cpfds.pfds[i].fd);
  1244. }
  1245. if(mask) {
  1246. if(WSAEventSelect(cpfds.pfds[i].fd, multi->wsa_event, mask) != 0) {
  1247. mresult = CURLM_OUT_OF_MEMORY;
  1248. goto out;
  1249. }
  1250. }
  1251. }
  1252. #endif
  1253. #ifdef ENABLE_WAKEUP
  1254. #ifndef USE_WINSOCK
  1255. if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) {
  1256. if(Curl_pollfds_add_sock(&cpfds, multi->wakeup_pair[0], POLLIN)) {
  1257. mresult = CURLM_OUT_OF_MEMORY;
  1258. goto out;
  1259. }
  1260. }
  1261. #endif
  1262. #endif
  1263. /* We check the internal timeout *AFTER* we collected all sockets to
  1264. * poll. Collecting the sockets may install new timers by protocols
  1265. * and connection filters.
  1266. * Use the shorter one of the internal and the caller requested timeout. */
  1267. multi_timeout(multi, &expire_time, &timeout_internal);
  1268. if((timeout_internal >= 0) && (timeout_internal < (long)timeout_ms))
  1269. timeout_ms = (int)timeout_internal;
  1270. if(data)
  1271. CURL_TRC_M(data, "multi_wait(fds=%d, timeout=%d) tinternal=%ld",
  1272. cpfds.n, timeout_ms, timeout_internal);
  1273. #if defined(ENABLE_WAKEUP) && defined(USE_WINSOCK)
  1274. if(cpfds.n || use_wakeup) {
  1275. #else
  1276. if(cpfds.n) {
  1277. #endif
  1278. int pollrc;
  1279. #ifdef USE_WINSOCK
  1280. if(cpfds.n) /* just pre-check with Winsock */
  1281. pollrc = Curl_poll(cpfds.pfds, cpfds.n, 0);
  1282. else
  1283. pollrc = 0;
  1284. #else
  1285. pollrc = Curl_poll(cpfds.pfds, cpfds.n, timeout_ms); /* wait... */
  1286. #endif
  1287. if(pollrc < 0) {
  1288. mresult = CURLM_UNRECOVERABLE_POLL;
  1289. goto out;
  1290. }
  1291. if(pollrc > 0) {
  1292. retcode = pollrc;
  1293. #ifdef USE_WINSOCK
  1294. }
  1295. else { /* now wait... if not ready during the pre-check (pollrc == 0) */
  1296. WSAWaitForMultipleEvents(1, &multi->wsa_event, FALSE, (DWORD)timeout_ms,
  1297. FALSE);
  1298. }
  1299. /* With Winsock, we have to run the following section unconditionally
  1300. to call WSAEventSelect(fd, event, 0) on all the sockets */
  1301. {
  1302. #endif
  1303. /* copy revents results from the poll to the curl_multi_wait poll
  1304. struct, the bit values of the actual underlying poll() implementation
  1305. may not be the same as the ones in the public libcurl API! */
  1306. for(i = 0; i < extra_nfds; i++) {
  1307. unsigned r = (unsigned)cpfds.pfds[curl_nfds + i].revents;
  1308. unsigned short mask = 0;
  1309. #ifdef USE_WINSOCK
  1310. curl_socket_t s = extra_fds[i].fd;
  1311. wsa_events.lNetworkEvents = 0;
  1312. if(WSAEnumNetworkEvents(s, NULL, &wsa_events) == 0) {
  1313. if(wsa_events.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE))
  1314. mask |= CURL_WAIT_POLLIN;
  1315. if(wsa_events.lNetworkEvents & (FD_WRITE | FD_CONNECT | FD_CLOSE))
  1316. mask |= CURL_WAIT_POLLOUT;
  1317. if(wsa_events.lNetworkEvents & FD_OOB)
  1318. mask |= CURL_WAIT_POLLPRI;
  1319. if(ret && !pollrc && wsa_events.lNetworkEvents)
  1320. retcode++;
  1321. }
  1322. WSAEventSelect(s, multi->wsa_event, 0);
  1323. if(!pollrc) {
  1324. extra_fds[i].revents = (short)mask;
  1325. continue;
  1326. }
  1327. #endif
  1328. if(r & POLLIN)
  1329. mask |= CURL_WAIT_POLLIN;
  1330. if(r & POLLOUT)
  1331. mask |= CURL_WAIT_POLLOUT;
  1332. if(r & POLLPRI)
  1333. mask |= CURL_WAIT_POLLPRI;
  1334. extra_fds[i].revents = (short)mask;
  1335. }
  1336. #ifdef USE_WINSOCK
  1337. /* Count up all our own sockets that had activity,
  1338. and remove them from the event. */
  1339. for(i = 0; i < curl_nfds; ++i) {
  1340. wsa_events.lNetworkEvents = 0;
  1341. if(WSAEnumNetworkEvents(cpfds.pfds[i].fd, NULL, &wsa_events) == 0) {
  1342. if(ret && !pollrc && wsa_events.lNetworkEvents)
  1343. retcode++;
  1344. }
  1345. WSAEventSelect(cpfds.pfds[i].fd, multi->wsa_event, 0);
  1346. }
  1347. WSAResetEvent(multi->wsa_event);
  1348. #else
  1349. #ifdef ENABLE_WAKEUP
  1350. if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) {
  1351. if(cpfds.pfds[curl_nfds + extra_nfds].revents & POLLIN) {
  1352. char buf[64];
  1353. ssize_t nread;
  1354. while(1) {
  1355. /* the reading socket is non-blocking, try to read
  1356. data from it until it receives an error (except EINTR).
  1357. In normal cases it will get EAGAIN or EWOULDBLOCK
  1358. when there is no more data, breaking the loop. */
  1359. nread = wakeup_read(multi->wakeup_pair[0], buf, sizeof(buf));
  1360. if(nread <= 0) {
  1361. if(nread < 0 && SOCKEINTR == SOCKERRNO)
  1362. continue;
  1363. break;
  1364. }
  1365. }
  1366. /* do not count the wakeup socket into the returned value */
  1367. retcode--;
  1368. }
  1369. }
  1370. #endif
  1371. #endif
  1372. }
  1373. }
  1374. if(ret)
  1375. *ret = retcode;
  1376. #if defined(ENABLE_WAKEUP) && defined(USE_WINSOCK)
  1377. if(extrawait && !cpfds.n && !use_wakeup) {
  1378. #else
  1379. if(extrawait && !cpfds.n) {
  1380. #endif
  1381. long sleep_ms = 0;
  1382. /* Avoid busy-looping when there is nothing particular to wait for */
  1383. multi_timeout(multi, &expire_time, &sleep_ms);
  1384. if(sleep_ms) {
  1385. if(sleep_ms > timeout_ms)
  1386. sleep_ms = timeout_ms;
  1387. /* when there are no easy handles in the multi, this holds a -1
  1388. timeout */
  1389. else if(sleep_ms < 0)
  1390. sleep_ms = timeout_ms;
  1391. curlx_wait_ms(sleep_ms);
  1392. }
  1393. }
  1394. out:
  1395. Curl_pollset_cleanup(&ps);
  1396. Curl_pollfds_cleanup(&cpfds);
  1397. return mresult;
  1398. }
  1399. CURLMcode curl_multi_wait(CURLM *multi,
  1400. struct curl_waitfd extra_fds[],
  1401. unsigned int extra_nfds,
  1402. int timeout_ms,
  1403. int *ret)
  1404. {
  1405. return multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, FALSE,
  1406. FALSE);
  1407. }
  1408. CURLMcode curl_multi_poll(CURLM *multi,
  1409. struct curl_waitfd extra_fds[],
  1410. unsigned int extra_nfds,
  1411. int timeout_ms,
  1412. int *ret)
  1413. {
  1414. return multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, TRUE, TRUE);
  1415. }
  1416. CURLMcode curl_multi_wakeup(CURLM *m)
  1417. {
  1418. /* this function is usually called from another thread,
  1419. it has to be careful only to access parts of the
  1420. Curl_multi struct that are constant */
  1421. struct Curl_multi *multi = m;
  1422. /* GOOD_MULTI_HANDLE can be safely called */
  1423. if(!GOOD_MULTI_HANDLE(multi))
  1424. return CURLM_BAD_HANDLE;
  1425. #ifdef ENABLE_WAKEUP
  1426. #ifdef USE_WINSOCK
  1427. if(WSASetEvent(multi->wsa_event))
  1428. return CURLM_OK;
  1429. #else
  1430. /* the wakeup_pair variable is only written during init and cleanup,
  1431. making it safe to access from another thread after the init part
  1432. and before cleanup */
  1433. if(multi->wakeup_pair[1] != CURL_SOCKET_BAD) {
  1434. while(1) {
  1435. #ifdef USE_EVENTFD
  1436. /* eventfd has a stringent rule of requiring the 8-byte buffer when
  1437. calling write(2) on it */
  1438. const uint64_t buf[1] = { 1 };
  1439. #else
  1440. const char buf[1] = { 1 };
  1441. #endif
  1442. /* swrite() is not thread-safe in general, because concurrent calls
  1443. can have their messages interleaved, but in this case the content
  1444. of the messages does not matter, which makes it ok to call.
  1445. The write socket is set to non-blocking, this way this function
  1446. cannot block, making it safe to call even from the same thread
  1447. that will call curl_multi_wait(). If swrite() returns that it
  1448. would block, it is considered successful because it means that
  1449. previous calls to this function will wake up the poll(). */
  1450. if(wakeup_write(multi->wakeup_pair[1], buf, sizeof(buf)) < 0) {
  1451. int err = SOCKERRNO;
  1452. int return_success;
  1453. #ifdef USE_WINSOCK
  1454. return_success = SOCKEWOULDBLOCK == err;
  1455. #else
  1456. if(SOCKEINTR == err)
  1457. continue;
  1458. return_success = SOCKEWOULDBLOCK == err || EAGAIN == err;
  1459. #endif
  1460. if(!return_success)
  1461. return CURLM_WAKEUP_FAILURE;
  1462. }
  1463. return CURLM_OK;
  1464. }
  1465. }
  1466. #endif
  1467. #endif
  1468. return CURLM_WAKEUP_FAILURE;
  1469. }
  1470. /*
  1471. * multi_ischanged() is called
  1472. *
  1473. * Returns TRUE/FALSE whether the state is changed to trigger a CONNECT_PEND
  1474. * => CONNECT action.
  1475. *
  1476. * Set 'clear' to TRUE to have it also clear the state variable.
  1477. */
  1478. static bool multi_ischanged(struct Curl_multi *multi, bool clear)
  1479. {
  1480. bool retval = multi->recheckstate;
  1481. if(clear)
  1482. multi->recheckstate = FALSE;
  1483. return retval;
  1484. }
  1485. /*
  1486. * Curl_multi_connchanged() is called to tell that there is a connection in
  1487. * this multi handle that has changed state (multiplexing become possible, the
  1488. * number of allowed streams changed or similar), and a subsequent use of this
  1489. * multi handle should move CONNECT_PEND handles back to CONNECT to have them
  1490. * retry.
  1491. */
  1492. void Curl_multi_connchanged(struct Curl_multi *multi)
  1493. {
  1494. multi->recheckstate = TRUE;
  1495. }
  1496. CURLMcode Curl_multi_add_perform(struct Curl_multi *multi,
  1497. struct Curl_easy *data,
  1498. struct connectdata *conn)
  1499. {
  1500. CURLMcode mresult;
  1501. if(multi->in_callback)
  1502. return CURLM_RECURSIVE_API_CALL;
  1503. mresult = curl_multi_add_handle(multi, data);
  1504. if(!mresult) {
  1505. struct SingleRequest *k = &data->req;
  1506. CURLcode result;
  1507. /* pass in NULL for 'conn' here since we do not want to init the
  1508. connection, only this transfer */
  1509. result = Curl_init_do(data, NULL);
  1510. if(result) {
  1511. curl_multi_remove_handle(multi, data);
  1512. return CURLM_INTERNAL_ERROR;
  1513. }
  1514. /* take this handle to the perform state right away */
  1515. multistate(data, MSTATE_PERFORMING);
  1516. Curl_attach_connection(data, conn);
  1517. k->keepon |= KEEP_RECV; /* setup to receive! */
  1518. }
  1519. return mresult;
  1520. }
  1521. static CURLcode multi_do(struct Curl_easy *data, bool *done)
  1522. {
  1523. CURLcode result = CURLE_OK;
  1524. struct connectdata *conn = data->conn;
  1525. DEBUGASSERT(conn);
  1526. DEBUGASSERT(conn->handler);
  1527. if(conn->handler->do_it)
  1528. result = conn->handler->do_it(data, done);
  1529. return result;
  1530. }
  1531. /*
  1532. * multi_do_more() is called during the DO_MORE multi state. It is basically a
  1533. * second stage DO state which (wrongly) was introduced to support FTP's
  1534. * second connection.
  1535. *
  1536. * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to
  1537. * DOING state there is more work to do!
  1538. */
  1539. static CURLcode multi_do_more(struct Curl_easy *data, int *complete)
  1540. {
  1541. CURLcode result = CURLE_OK;
  1542. struct connectdata *conn = data->conn;
  1543. *complete = 0;
  1544. if(conn->handler->do_more)
  1545. result = conn->handler->do_more(data, complete);
  1546. return result;
  1547. }
  1548. /*
  1549. * Check whether a timeout occurred, and handle it if it did
  1550. */
  1551. static bool multi_handle_timeout(struct Curl_easy *data,
  1552. bool *stream_error,
  1553. CURLcode *result)
  1554. {
  1555. bool connect_timeout = data->mstate < MSTATE_DO;
  1556. timediff_t timeout_ms;
  1557. timeout_ms = Curl_timeleft_ms(data, connect_timeout);
  1558. if(timeout_ms < 0) {
  1559. /* Handle timed out */
  1560. struct curltime since;
  1561. if(connect_timeout)
  1562. since = data->progress.t_startsingle;
  1563. else
  1564. since = data->progress.t_startop;
  1565. if(data->mstate == MSTATE_RESOLVING)
  1566. failf(data, "Resolving timed out after %" FMT_TIMEDIFF_T
  1567. " milliseconds",
  1568. curlx_ptimediff_ms(Curl_pgrs_now(data), &since));
  1569. else if(data->mstate == MSTATE_CONNECTING)
  1570. failf(data, "Connection timed out after %" FMT_TIMEDIFF_T
  1571. " milliseconds",
  1572. curlx_ptimediff_ms(Curl_pgrs_now(data), &since));
  1573. else {
  1574. struct SingleRequest *k = &data->req;
  1575. if(k->size != -1) {
  1576. failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
  1577. " milliseconds with %" FMT_OFF_T " out of %"
  1578. FMT_OFF_T " bytes received",
  1579. curlx_ptimediff_ms(Curl_pgrs_now(data), &since),
  1580. k->bytecount, k->size);
  1581. }
  1582. else {
  1583. failf(data, "Operation timed out after %" FMT_TIMEDIFF_T
  1584. " milliseconds with %" FMT_OFF_T " bytes received",
  1585. curlx_ptimediff_ms(Curl_pgrs_now(data), &since),
  1586. k->bytecount);
  1587. }
  1588. }
  1589. *result = CURLE_OPERATION_TIMEDOUT;
  1590. if(data->conn) {
  1591. /* Force connection closed if the connection has indeed been used */
  1592. if(data->mstate > MSTATE_DO) {
  1593. streamclose(data->conn, "Disconnect due to timeout");
  1594. *stream_error = TRUE;
  1595. }
  1596. (void)multi_done(data, *result, TRUE);
  1597. }
  1598. return TRUE;
  1599. }
  1600. return FALSE;
  1601. }
  1602. /*
  1603. * We are doing protocol-specific connecting and this is being called over and
  1604. * over from the multi interface until the connection phase is done on
  1605. * protocol layer.
  1606. */
  1607. static CURLcode protocol_connecting(struct Curl_easy *data, bool *done)
  1608. {
  1609. CURLcode result = CURLE_OK;
  1610. struct connectdata *conn = data->conn;
  1611. if(conn && conn->handler->connecting) {
  1612. *done = FALSE;
  1613. result = conn->handler->connecting(data, done);
  1614. }
  1615. else
  1616. *done = TRUE;
  1617. return result;
  1618. }
  1619. /*
  1620. * We are DOING this is being called over and over from the multi interface
  1621. * until the DOING phase is done on protocol layer.
  1622. */
  1623. static CURLcode protocol_doing(struct Curl_easy *data, bool *done)
  1624. {
  1625. CURLcode result = CURLE_OK;
  1626. struct connectdata *conn = data->conn;
  1627. if(conn && conn->handler->doing) {
  1628. *done = FALSE;
  1629. result = conn->handler->doing(data, done);
  1630. }
  1631. else
  1632. *done = TRUE;
  1633. return result;
  1634. }
  1635. /*
  1636. * We have discovered that the TCP connection has been successful, we can now
  1637. * proceed with some action.
  1638. *
  1639. */
  1640. static CURLcode protocol_connect(struct Curl_easy *data, bool *protocol_done)
  1641. {
  1642. struct connectdata *conn = data->conn;
  1643. CURLcode result = CURLE_OK;
  1644. DEBUGASSERT(conn);
  1645. DEBUGASSERT(protocol_done);
  1646. DEBUGASSERT(Curl_conn_is_connected(conn, FIRSTSOCKET));
  1647. *protocol_done = FALSE;
  1648. if(!conn->bits.protoconnstart) {
  1649. if(conn->handler->connect_it) {
  1650. /* Call the protocol-specific connect function */
  1651. result = conn->handler->connect_it(data, protocol_done);
  1652. if(result)
  1653. return result;
  1654. }
  1655. conn->bits.protoconnstart = TRUE;
  1656. }
  1657. /* Unless this protocol does not have any protocol-connect callback, as
  1658. then we know we are done. */
  1659. if(!conn->handler->connecting)
  1660. *protocol_done = TRUE;
  1661. return CURLE_OK;
  1662. }
  1663. static void set_in_callback(struct Curl_multi *multi, bool value)
  1664. {
  1665. multi->in_callback = value;
  1666. }
  1667. /*
  1668. * posttransfer() is called immediately after a transfer ends
  1669. */
  1670. static void multi_posttransfer(struct Curl_easy *data)
  1671. {
  1672. #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(HAVE_MSG_NOSIGNAL)
  1673. /* restore the signal handler for SIGPIPE before we get back */
  1674. if(!data->set.no_signal)
  1675. signal(SIGPIPE, data->state.prev_signal);
  1676. #else
  1677. (void)data;
  1678. #endif
  1679. }
  1680. /*
  1681. * multi_follow() handles the URL redirect magic. Pass in the 'newurl' string
  1682. * as given by the remote server and set up the new URL to request.
  1683. *
  1684. * This function DOES NOT FREE the given url.
  1685. */
  1686. static CURLcode multi_follow(struct Curl_easy *data,
  1687. const struct Curl_handler *handler,
  1688. const char *newurl, /* the Location: string */
  1689. followtype type) /* see transfer.h */
  1690. {
  1691. if(handler && handler->follow)
  1692. return handler->follow(data, newurl, type);
  1693. return CURLE_TOO_MANY_REDIRECTS;
  1694. }
  1695. static CURLcode mspeed_check(struct Curl_easy *data)
  1696. {
  1697. const struct curltime *pnow = Curl_pgrs_now(data);
  1698. timediff_t recv_wait_ms = 0;
  1699. timediff_t send_wait_ms = 0;
  1700. /* check if our send/recv limits require idle waits */
  1701. send_wait_ms = Curl_rlimit_wait_ms(&data->progress.ul.rlimit, pnow);
  1702. recv_wait_ms = Curl_rlimit_wait_ms(&data->progress.dl.rlimit, pnow);
  1703. if(send_wait_ms || recv_wait_ms) {
  1704. if(data->mstate != MSTATE_RATELIMITING) {
  1705. multistate(data, MSTATE_RATELIMITING);
  1706. }
  1707. Curl_expire(data, CURLMAX(send_wait_ms, recv_wait_ms), EXPIRE_TOOFAST);
  1708. Curl_multi_clear_dirty(data);
  1709. CURL_TRC_M(data, "[RLIMIT] waiting %" FMT_TIMEDIFF_T "ms",
  1710. CURLMAX(send_wait_ms, recv_wait_ms));
  1711. return CURLE_AGAIN;
  1712. }
  1713. else if(data->mstate != MSTATE_PERFORMING) {
  1714. CURL_TRC_M(data, "[RLIMIT] wait over, continue");
  1715. multistate(data, MSTATE_PERFORMING);
  1716. }
  1717. return CURLE_OK;
  1718. }
  1719. static CURLMcode state_performing(struct Curl_easy *data,
  1720. bool *stream_errorp,
  1721. CURLcode *resultp)
  1722. {
  1723. char *newurl = NULL;
  1724. bool retry = FALSE;
  1725. CURLMcode mresult = CURLM_OK;
  1726. CURLcode result = *resultp = CURLE_OK;
  1727. *stream_errorp = FALSE;
  1728. if(mspeed_check(data) == CURLE_AGAIN)
  1729. return CURLM_OK;
  1730. /* read/write data if it is ready to do so */
  1731. result = Curl_sendrecv(data);
  1732. if(data->req.done || (result == CURLE_RECV_ERROR)) {
  1733. /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
  1734. * condition and the server closed the reused connection exactly when we
  1735. * wanted to use it, so figure out if that is indeed the case.
  1736. */
  1737. CURLcode ret = Curl_retry_request(data, &newurl);
  1738. if(!ret)
  1739. retry = !!newurl;
  1740. else if(!result)
  1741. result = ret;
  1742. if(retry) {
  1743. /* if we are to retry, set the result to OK and consider the
  1744. request as done */
  1745. result = CURLE_OK;
  1746. data->req.done = TRUE;
  1747. }
  1748. }
  1749. #ifndef CURL_DISABLE_HTTP
  1750. else if((CURLE_HTTP2_STREAM == result) &&
  1751. Curl_h2_http_1_1_error(data)) {
  1752. CURLcode ret = Curl_retry_request(data, &newurl);
  1753. if(!ret) {
  1754. infof(data, "Downgrades to HTTP/1.1");
  1755. streamclose(data->conn, "Disconnect HTTP/2 for HTTP/1");
  1756. data->state.http_neg.wanted = CURL_HTTP_V1x;
  1757. data->state.http_neg.allowed = CURL_HTTP_V1x;
  1758. /* clear the error message bit too as we ignore the one we got */
  1759. data->state.errorbuf = FALSE;
  1760. if(!newurl)
  1761. /* typically for HTTP_1_1_REQUIRED error on first flight */
  1762. newurl = Curl_bufref_dup(&data->state.url);
  1763. if(!newurl) {
  1764. result = CURLE_OUT_OF_MEMORY;
  1765. }
  1766. else {
  1767. /* if we are to retry, set the result to OK and consider the request
  1768. as done */
  1769. retry = TRUE;
  1770. result = CURLE_OK;
  1771. data->req.done = TRUE;
  1772. }
  1773. }
  1774. else
  1775. result = ret;
  1776. }
  1777. #endif
  1778. if(result) {
  1779. /*
  1780. * The transfer phase returned error, we mark the connection to get closed
  1781. * to prevent being reused. This is because we cannot possibly know if the
  1782. * connection is in a good shape or not now. Unless it is a protocol which
  1783. * uses two "channels" like FTP, as then the error happened in the data
  1784. * connection.
  1785. */
  1786. if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
  1787. result != CURLE_HTTP2_STREAM)
  1788. streamclose(data->conn, "Transfer returned error");
  1789. multi_posttransfer(data);
  1790. multi_done(data, result, TRUE);
  1791. }
  1792. else if(data->req.done && !Curl_cwriter_is_paused(data)) {
  1793. const struct Curl_handler *handler = data->conn->handler;
  1794. /* call this even if the readwrite function returned error */
  1795. multi_posttransfer(data);
  1796. /* When we follow redirects or is set to retry the connection, we must to
  1797. go back to the CONNECT state */
  1798. if(data->req.newurl || retry) {
  1799. followtype follow = FOLLOW_NONE;
  1800. if(!retry) {
  1801. /* if the URL is a follow-location and not just a retried request then
  1802. figure out the URL here */
  1803. curlx_free(newurl);
  1804. newurl = data->req.newurl;
  1805. data->req.newurl = NULL;
  1806. follow = FOLLOW_REDIR;
  1807. }
  1808. else
  1809. follow = FOLLOW_RETRY;
  1810. (void)multi_done(data, CURLE_OK, FALSE);
  1811. /* multi_done() might return CURLE_GOT_NOTHING */
  1812. result = multi_follow(data, handler, newurl, follow);
  1813. if(!result) {
  1814. multistate(data, MSTATE_SETUP);
  1815. mresult = CURLM_CALL_MULTI_PERFORM;
  1816. }
  1817. }
  1818. else {
  1819. /* after the transfer is done, go DONE */
  1820. /* but first check to see if we got a location info even though we are
  1821. not following redirects */
  1822. if(data->req.location) {
  1823. curlx_free(newurl);
  1824. newurl = data->req.location;
  1825. data->req.location = NULL;
  1826. result = multi_follow(data, handler, newurl, FOLLOW_FAKE);
  1827. if(result) {
  1828. *stream_errorp = TRUE;
  1829. result = multi_done(data, result, TRUE);
  1830. }
  1831. }
  1832. if(!result) {
  1833. multistate(data, MSTATE_DONE);
  1834. mresult = CURLM_CALL_MULTI_PERFORM;
  1835. }
  1836. }
  1837. }
  1838. else { /* not errored, not done */
  1839. mspeed_check(data);
  1840. }
  1841. curlx_free(newurl);
  1842. *resultp = result;
  1843. return mresult;
  1844. }
  1845. static CURLMcode state_do(struct Curl_easy *data,
  1846. bool *stream_errorp,
  1847. CURLcode *resultp)
  1848. {
  1849. CURLMcode mresult = CURLM_OK;
  1850. CURLcode result = CURLE_OK;
  1851. if(data->set.fprereq) {
  1852. int prereq_rc;
  1853. /* call the prerequest callback function */
  1854. Curl_set_in_callback(data, TRUE);
  1855. prereq_rc = data->set.fprereq(data->set.prereq_userp,
  1856. data->info.primary.remote_ip,
  1857. data->info.primary.local_ip,
  1858. data->info.primary.remote_port,
  1859. data->info.primary.local_port);
  1860. Curl_set_in_callback(data, FALSE);
  1861. if(prereq_rc != CURL_PREREQFUNC_OK) {
  1862. failf(data, "operation aborted by pre-request callback");
  1863. /* failure in pre-request callback - do not do any other processing */
  1864. result = CURLE_ABORTED_BY_CALLBACK;
  1865. multi_posttransfer(data);
  1866. multi_done(data, result, FALSE);
  1867. *stream_errorp = TRUE;
  1868. goto end;
  1869. }
  1870. }
  1871. if(data->set.connect_only && !data->set.connect_only_ws) {
  1872. multistate(data, MSTATE_DONE);
  1873. mresult = CURLM_CALL_MULTI_PERFORM;
  1874. }
  1875. else {
  1876. bool dophase_done = FALSE;
  1877. /* Perform the protocol's DO action */
  1878. result = multi_do(data, &dophase_done);
  1879. /* When multi_do() returns failure, data->conn might be NULL! */
  1880. if(!result) {
  1881. if(!dophase_done) {
  1882. #ifndef CURL_DISABLE_FTP
  1883. /* some steps needed for wildcard matching */
  1884. if(data->state.wildcardmatch) {
  1885. struct WildcardData *wc = data->wildcard;
  1886. if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) {
  1887. /* skip some states if it is important */
  1888. multi_done(data, CURLE_OK, FALSE);
  1889. /* if there is no connection left, skip the DONE state */
  1890. multistate(data, data->conn ? MSTATE_DONE : MSTATE_COMPLETED);
  1891. mresult = CURLM_CALL_MULTI_PERFORM;
  1892. goto end;
  1893. }
  1894. }
  1895. #endif
  1896. /* DO was not completed in one function call, we must continue
  1897. DOING... */
  1898. multistate(data, MSTATE_DOING);
  1899. mresult = CURLM_CALL_MULTI_PERFORM;
  1900. }
  1901. /* after DO, go DO_DONE... or DO_MORE */
  1902. else if(data->conn->bits.do_more) {
  1903. /* we are supposed to do more, but we need to sit down, relax and wait
  1904. a little while first */
  1905. multistate(data, MSTATE_DOING_MORE);
  1906. mresult = CURLM_CALL_MULTI_PERFORM;
  1907. }
  1908. else {
  1909. /* we are done with the DO, now DID */
  1910. multistate(data, MSTATE_DID);
  1911. mresult = CURLM_CALL_MULTI_PERFORM;
  1912. }
  1913. }
  1914. else if((CURLE_SEND_ERROR == result) &&
  1915. data->conn->bits.reuse) {
  1916. /*
  1917. * In this situation, a connection that we were trying to use may have
  1918. * unexpectedly died. If possible, send the connection back to the
  1919. * CONNECT phase so we can try again.
  1920. */
  1921. const struct Curl_handler *handler = data->conn->handler;
  1922. char *newurl = NULL;
  1923. followtype follow = FOLLOW_NONE;
  1924. CURLcode drc;
  1925. drc = Curl_retry_request(data, &newurl);
  1926. if(drc) {
  1927. /* a failure here pretty much implies an out of memory */
  1928. result = drc;
  1929. *stream_errorp = TRUE;
  1930. }
  1931. multi_posttransfer(data);
  1932. drc = multi_done(data, result, FALSE);
  1933. /* When set to retry the connection, we must go back to the CONNECT
  1934. * state */
  1935. if(newurl) {
  1936. if(!drc || (drc == CURLE_SEND_ERROR)) {
  1937. follow = FOLLOW_RETRY;
  1938. drc = multi_follow(data, handler, newurl, follow);
  1939. if(!drc) {
  1940. multistate(data, MSTATE_SETUP);
  1941. mresult = CURLM_CALL_MULTI_PERFORM;
  1942. result = CURLE_OK;
  1943. }
  1944. else {
  1945. /* Follow failed */
  1946. result = drc;
  1947. }
  1948. }
  1949. else {
  1950. /* done did not return OK or SEND_ERROR */
  1951. result = drc;
  1952. }
  1953. }
  1954. else {
  1955. /* Have error handler disconnect conn if we cannot retry */
  1956. *stream_errorp = TRUE;
  1957. }
  1958. curlx_free(newurl);
  1959. }
  1960. else {
  1961. /* failure detected */
  1962. multi_posttransfer(data);
  1963. if(data->conn)
  1964. multi_done(data, result, FALSE);
  1965. *stream_errorp = TRUE;
  1966. }
  1967. }
  1968. end:
  1969. *resultp = result;
  1970. return mresult;
  1971. }
  1972. static CURLMcode state_ratelimiting(struct Curl_easy *data,
  1973. CURLcode *resultp)
  1974. {
  1975. CURLcode result = CURLE_OK;
  1976. CURLMcode mresult = CURLM_OK;
  1977. DEBUGASSERT(data->conn);
  1978. /* if both rates are within spec, resume transfer */
  1979. result = Curl_pgrsCheck(data);
  1980. if(result) {
  1981. if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
  1982. result != CURLE_HTTP2_STREAM)
  1983. streamclose(data->conn, "Transfer returned error");
  1984. multi_posttransfer(data);
  1985. multi_done(data, result, TRUE);
  1986. }
  1987. else {
  1988. if(!mspeed_check(data))
  1989. mresult = CURLM_CALL_MULTI_PERFORM;
  1990. }
  1991. *resultp = result;
  1992. return mresult;
  1993. }
  1994. static CURLMcode state_resolving(struct Curl_multi *multi,
  1995. struct Curl_easy *data,
  1996. bool *stream_errorp,
  1997. CURLcode *resultp)
  1998. {
  1999. struct Curl_dns_entry *dns = NULL;
  2000. CURLcode result;
  2001. CURLMcode mresult = CURLM_OK;
  2002. result = Curl_resolv_check(data, &dns);
  2003. CURL_TRC_DNS(data, "Curl_resolv_check() -> %d, %s",
  2004. result, dns ? "found" : "missing");
  2005. /* Update sockets here, because the socket(s) may have been closed and the
  2006. application thus needs to be told, even if it is likely that the same
  2007. socket(s) will again be used further down. If the name has not yet been
  2008. resolved, it is likely that new sockets have been opened in an attempt to
  2009. contact another resolver. */
  2010. mresult = Curl_multi_ev_assess_xfer(multi, data);
  2011. if(mresult)
  2012. return mresult;
  2013. if(dns) {
  2014. bool connected;
  2015. /* Perform the next step in the connection phase, and then move on to the
  2016. WAITCONNECT state */
  2017. result = Curl_once_resolved(data, dns, &connected);
  2018. if(result)
  2019. /* if Curl_once_resolved() returns failure, the connection struct is
  2020. already freed and gone */
  2021. data->conn = NULL; /* no more connection */
  2022. else {
  2023. /* call again please so that we get the next socket setup */
  2024. mresult = CURLM_CALL_MULTI_PERFORM;
  2025. if(connected)
  2026. multistate(data, MSTATE_PROTOCONNECT);
  2027. else {
  2028. multistate(data, MSTATE_CONNECTING);
  2029. }
  2030. }
  2031. }
  2032. if(result)
  2033. /* failure detected */
  2034. *stream_errorp = TRUE;
  2035. *resultp = result;
  2036. return mresult;
  2037. }
  2038. static CURLMcode state_connect(struct Curl_multi *multi,
  2039. struct Curl_easy *data,
  2040. CURLcode *resultp)
  2041. {
  2042. /* Connect. We want to get a connection identifier filled in. This state can
  2043. be entered from SETUP and from PENDING. */
  2044. bool connected;
  2045. bool async;
  2046. CURLMcode mresult = CURLM_OK;
  2047. CURLcode result = Curl_connect(data, &async, &connected);
  2048. if(CURLE_NO_CONNECTION_AVAILABLE == result) {
  2049. /* There was no connection available. We will go to the pending state and
  2050. wait for an available connection. */
  2051. multistate(data, MSTATE_PENDING);
  2052. /* move from process to pending set */
  2053. Curl_uint32_bset_remove(&multi->process, data->mid);
  2054. Curl_uint32_bset_remove(&multi->dirty, data->mid);
  2055. Curl_uint32_bset_add(&multi->pending, data->mid);
  2056. *resultp = CURLE_OK;
  2057. return mresult;
  2058. }
  2059. else
  2060. process_pending_handles(data->multi);
  2061. if(!result) {
  2062. if(async)
  2063. /* We are now waiting for an asynchronous name lookup */
  2064. multistate(data, MSTATE_RESOLVING);
  2065. else {
  2066. /* after the connect has been sent off, go WAITCONNECT unless the
  2067. protocol connect is already done and we can go directly to WAITDO or
  2068. DO! */
  2069. mresult = CURLM_CALL_MULTI_PERFORM;
  2070. if(connected) {
  2071. if(!data->conn->bits.reuse &&
  2072. Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
  2073. /* new connection, can multiplex, wake pending handles */
  2074. process_pending_handles(data->multi);
  2075. }
  2076. multistate(data, MSTATE_PROTOCONNECT);
  2077. }
  2078. else {
  2079. multistate(data, MSTATE_CONNECTING);
  2080. }
  2081. }
  2082. }
  2083. *resultp = result;
  2084. return mresult;
  2085. }
  2086. static CURLMcode multi_runsingle(struct Curl_multi *multi,
  2087. struct Curl_easy *data)
  2088. {
  2089. struct Curl_message *msg = NULL;
  2090. bool connected;
  2091. bool protocol_connected = FALSE;
  2092. bool dophase_done = FALSE;
  2093. CURLMcode mresult;
  2094. CURLcode result = CURLE_OK;
  2095. int control;
  2096. if(!GOOD_EASY_HANDLE(data))
  2097. return CURLM_BAD_EASY_HANDLE;
  2098. if(multi->dead) {
  2099. /* a multi-level callback returned error before, meaning every individual
  2100. transfer now has failed */
  2101. result = CURLE_ABORTED_BY_CALLBACK;
  2102. multi_posttransfer(data);
  2103. multi_done(data, result, FALSE);
  2104. multistate(data, MSTATE_COMPLETED);
  2105. }
  2106. multi_warn_debug(multi, data);
  2107. /* transfer runs now, clear the dirty bit. This may be set
  2108. * again during processing, triggering a re-run later. */
  2109. Curl_uint32_bset_remove(&multi->dirty, data->mid);
  2110. if(data == multi->admin) {
  2111. Curl_cshutdn_perform(&multi->cshutdn, multi->admin, CURL_SOCKET_TIMEOUT);
  2112. return CURLM_OK;
  2113. }
  2114. do {
  2115. /* A "stream" here is a logical stream if the protocol can handle that
  2116. (HTTP/2), or the full connection for older protocols */
  2117. bool stream_error = FALSE;
  2118. mresult = CURLM_OK;
  2119. if(multi_ischanged(multi, TRUE)) {
  2120. CURL_TRC_M(data, "multi changed, check CONNECT_PEND queue");
  2121. process_pending_handles(multi); /* multiplexed */
  2122. }
  2123. if(data->mstate > MSTATE_CONNECT &&
  2124. data->mstate < MSTATE_COMPLETED) {
  2125. /* Make sure we set the connection's current owner */
  2126. DEBUGASSERT(data->conn);
  2127. if(!data->conn)
  2128. return CURLM_INTERNAL_ERROR;
  2129. }
  2130. /* Wait for the connect state as only then is the start time stored, but
  2131. we must not check already completed handles */
  2132. if((data->mstate >= MSTATE_CONNECT) && (data->mstate < MSTATE_COMPLETED) &&
  2133. multi_handle_timeout(data, &stream_error, &result))
  2134. /* Skip the statemachine and go directly to error handling section. */
  2135. goto statemachine_end;
  2136. switch(data->mstate) {
  2137. case MSTATE_INIT:
  2138. /* Transitional state. init this transfer. A handle never comes back to
  2139. this state. */
  2140. result = Curl_pretransfer(data);
  2141. if(result)
  2142. break;
  2143. /* after init, go SETUP */
  2144. multistate(data, MSTATE_SETUP);
  2145. (void)Curl_pgrsTime(data, TIMER_STARTOP);
  2146. FALLTHROUGH();
  2147. case MSTATE_SETUP:
  2148. /* Transitional state. Setup things for a new transfer. The handle
  2149. can come back to this state on a redirect. */
  2150. Curl_pgrsTime(data, TIMER_STARTSINGLE);
  2151. if(data->set.timeout)
  2152. Curl_expire(data, data->set.timeout, EXPIRE_TIMEOUT);
  2153. if(data->set.connecttimeout)
  2154. /* Since a connection might go to pending and back to CONNECT several
  2155. times before it actually takes off, we need to set the timeout once
  2156. in SETUP before we enter CONNECT the first time. */
  2157. Curl_expire(data, data->set.connecttimeout, EXPIRE_CONNECTTIMEOUT);
  2158. multistate(data, MSTATE_CONNECT);
  2159. FALLTHROUGH();
  2160. case MSTATE_CONNECT:
  2161. mresult = state_connect(multi, data, &result);
  2162. break;
  2163. case MSTATE_RESOLVING:
  2164. /* awaiting an asynch name resolve to complete */
  2165. mresult = state_resolving(multi, data, &stream_error, &result);
  2166. break;
  2167. case MSTATE_CONNECTING:
  2168. /* awaiting a completion of an asynch TCP connect */
  2169. DEBUGASSERT(data->conn);
  2170. if(!Curl_xfer_recv_is_paused(data)) {
  2171. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &connected);
  2172. if(connected && !result) {
  2173. if(!data->conn->bits.reuse &&
  2174. Curl_conn_is_multiplex(data->conn, FIRSTSOCKET)) {
  2175. /* new connection, can multiplex, wake pending handles */
  2176. process_pending_handles(data->multi);
  2177. }
  2178. mresult = CURLM_CALL_MULTI_PERFORM;
  2179. multistate(data, MSTATE_PROTOCONNECT);
  2180. }
  2181. else if(result) {
  2182. /* failure detected */
  2183. multi_posttransfer(data);
  2184. multi_done(data, result, TRUE);
  2185. stream_error = TRUE;
  2186. break;
  2187. }
  2188. }
  2189. break;
  2190. case MSTATE_PROTOCONNECT:
  2191. if(!result && data->conn->bits.reuse) {
  2192. /* ftp seems to hang when protoconnect on reused connection since we
  2193. * handle PROTOCONNECT in general inside the filers, it seems wrong to
  2194. * restart this on a reused connection.
  2195. */
  2196. multistate(data, MSTATE_DO);
  2197. mresult = CURLM_CALL_MULTI_PERFORM;
  2198. break;
  2199. }
  2200. if(!result)
  2201. result = protocol_connect(data, &protocol_connected);
  2202. if(!result && !protocol_connected) {
  2203. /* switch to waiting state */
  2204. multistate(data, MSTATE_PROTOCONNECTING);
  2205. mresult = CURLM_CALL_MULTI_PERFORM;
  2206. }
  2207. else if(!result) {
  2208. /* protocol connect has completed, go WAITDO or DO */
  2209. multistate(data, MSTATE_DO);
  2210. mresult = CURLM_CALL_MULTI_PERFORM;
  2211. }
  2212. else {
  2213. /* failure detected */
  2214. multi_posttransfer(data);
  2215. multi_done(data, result, TRUE);
  2216. stream_error = TRUE;
  2217. }
  2218. break;
  2219. case MSTATE_PROTOCONNECTING:
  2220. /* protocol-specific connect phase */
  2221. result = protocol_connecting(data, &protocol_connected);
  2222. if(!result && protocol_connected) {
  2223. /* after the connect has completed, go WAITDO or DO */
  2224. multistate(data, MSTATE_DO);
  2225. mresult = CURLM_CALL_MULTI_PERFORM;
  2226. }
  2227. else if(result) {
  2228. /* failure detected */
  2229. multi_posttransfer(data);
  2230. multi_done(data, result, TRUE);
  2231. stream_error = TRUE;
  2232. }
  2233. break;
  2234. case MSTATE_DO:
  2235. mresult = state_do(data, &stream_error, &result);
  2236. break;
  2237. case MSTATE_DOING:
  2238. /* we continue DOING until the DO phase is complete */
  2239. DEBUGASSERT(data->conn);
  2240. result = protocol_doing(data, &dophase_done);
  2241. if(!result) {
  2242. if(dophase_done) {
  2243. /* after DO, go DO_DONE or DO_MORE */
  2244. multistate(data, data->conn->bits.do_more ?
  2245. MSTATE_DOING_MORE : MSTATE_DID);
  2246. mresult = CURLM_CALL_MULTI_PERFORM;
  2247. } /* dophase_done */
  2248. }
  2249. else {
  2250. /* failure detected */
  2251. multi_posttransfer(data);
  2252. multi_done(data, result, FALSE);
  2253. stream_error = TRUE;
  2254. }
  2255. break;
  2256. case MSTATE_DOING_MORE:
  2257. /*
  2258. * When we are connected, DOING MORE and then go DID
  2259. */
  2260. DEBUGASSERT(data->conn);
  2261. result = multi_do_more(data, &control);
  2262. if(!result) {
  2263. if(control) {
  2264. /* if positive, advance to DO_DONE
  2265. if negative, go back to DOING */
  2266. multistate(data, control == 1 ? MSTATE_DID : MSTATE_DOING);
  2267. mresult = CURLM_CALL_MULTI_PERFORM;
  2268. }
  2269. /* else
  2270. stay in DO_MORE */
  2271. }
  2272. else {
  2273. /* failure detected */
  2274. multi_posttransfer(data);
  2275. multi_done(data, result, FALSE);
  2276. stream_error = TRUE;
  2277. }
  2278. break;
  2279. case MSTATE_DID:
  2280. DEBUGASSERT(data->conn);
  2281. if(data->conn->bits.multiplex)
  2282. /* Check if we can move pending requests to send pipe */
  2283. process_pending_handles(multi); /* multiplexed */
  2284. /* Only perform the transfer if there is a good socket to work with.
  2285. Having both BAD is a signal to skip immediately to DONE */
  2286. if(CONN_SOCK_IDX_VALID(data->conn->recv_idx) ||
  2287. CONN_SOCK_IDX_VALID(data->conn->send_idx))
  2288. multistate(data, MSTATE_PERFORMING);
  2289. else {
  2290. #ifndef CURL_DISABLE_FTP
  2291. if(data->state.wildcardmatch &&
  2292. ((data->conn->handler->flags & PROTOPT_WILDCARD) == 0)) {
  2293. data->wildcard->state = CURLWC_DONE;
  2294. }
  2295. #endif
  2296. multistate(data, MSTATE_DONE);
  2297. }
  2298. mresult = CURLM_CALL_MULTI_PERFORM;
  2299. break;
  2300. case MSTATE_RATELIMITING: /* limit-rate exceeded in either direction */
  2301. mresult = state_ratelimiting(data, &result);
  2302. break;
  2303. case MSTATE_PERFORMING:
  2304. mresult = state_performing(data, &stream_error, &result);
  2305. break;
  2306. case MSTATE_DONE:
  2307. /* this state is highly transient, so run another loop after this */
  2308. mresult = CURLM_CALL_MULTI_PERFORM;
  2309. if(data->conn) {
  2310. CURLcode res;
  2311. /* post-transfer command */
  2312. res = multi_done(data, result, FALSE);
  2313. /* allow a previously set error code take precedence */
  2314. if(!result)
  2315. result = res;
  2316. }
  2317. #ifndef CURL_DISABLE_FTP
  2318. if(data->state.wildcardmatch) {
  2319. if(data->wildcard->state != CURLWC_DONE) {
  2320. /* if a wildcard is set and we are not ending -> lets start again
  2321. with MSTATE_INIT */
  2322. multistate(data, MSTATE_INIT);
  2323. break;
  2324. }
  2325. }
  2326. #endif
  2327. /* after we have DONE what we are supposed to do, go COMPLETED, and
  2328. it does not matter what the multi_done() returned! */
  2329. multistate(data, MSTATE_COMPLETED);
  2330. break;
  2331. case MSTATE_COMPLETED:
  2332. break;
  2333. case MSTATE_PENDING:
  2334. case MSTATE_MSGSENT:
  2335. /* handles in these states should NOT be in this list */
  2336. break;
  2337. default:
  2338. return CURLM_INTERNAL_ERROR;
  2339. }
  2340. if(data->mstate >= MSTATE_CONNECT &&
  2341. data->mstate < MSTATE_DO &&
  2342. mresult != CURLM_CALL_MULTI_PERFORM &&
  2343. !multi_ischanged(multi, FALSE)) {
  2344. /* We now handle stream timeouts if and only if this will be the last
  2345. * loop iteration. We only check this on the last iteration to ensure
  2346. * that if we know we have additional work to do immediately
  2347. * (i.e. CURLM_CALL_MULTI_PERFORM == TRUE) then we should do that before
  2348. * declaring the connection timed out as we may almost have a completed
  2349. * connection. */
  2350. multi_handle_timeout(data, &stream_error, &result);
  2351. }
  2352. statemachine_end:
  2353. if(data->mstate < MSTATE_COMPLETED) {
  2354. if(result) {
  2355. /*
  2356. * If an error was returned, and we are not in completed state now,
  2357. * then we go to completed and consider this transfer aborted.
  2358. */
  2359. /* NOTE: no attempt to disconnect connections must be made
  2360. in the case blocks above - cleanup happens only here */
  2361. /* Check if we can move pending requests to send pipe */
  2362. process_pending_handles(multi); /* connection */
  2363. if(data->conn) {
  2364. if(stream_error) {
  2365. /* Do not attempt to send data over a connection that timed out */
  2366. bool dead_connection = result == CURLE_OPERATION_TIMEDOUT;
  2367. struct connectdata *conn = data->conn;
  2368. /* This is where we make sure that the conn pointer is reset.
  2369. We do not have to do this in every case block above where a
  2370. failure is detected */
  2371. Curl_detach_connection(data);
  2372. Curl_conn_terminate(data, conn, dead_connection);
  2373. }
  2374. }
  2375. else if(data->mstate == MSTATE_CONNECT) {
  2376. /* Curl_connect() failed */
  2377. multi_posttransfer(data);
  2378. Curl_pgrsUpdate_nometer(data);
  2379. }
  2380. multistate(data, MSTATE_COMPLETED);
  2381. mresult = CURLM_CALL_MULTI_PERFORM;
  2382. }
  2383. /* if there is still a connection to use, call the progress function */
  2384. else if(data->conn) {
  2385. result = Curl_pgrsUpdate(data);
  2386. if(result) {
  2387. /* aborted due to progress callback return code must close the
  2388. connection */
  2389. streamclose(data->conn, "Aborted by callback");
  2390. /* if not yet in DONE state, go there, otherwise COMPLETED */
  2391. multistate(data, (data->mstate < MSTATE_DONE) ?
  2392. MSTATE_DONE : MSTATE_COMPLETED);
  2393. mresult = CURLM_CALL_MULTI_PERFORM;
  2394. }
  2395. }
  2396. }
  2397. if(MSTATE_COMPLETED == data->mstate) {
  2398. if(data->master_mid != UINT32_MAX) {
  2399. /* A sub transfer, not for msgsent to application */
  2400. struct Curl_easy *mdata;
  2401. CURL_TRC_M(data, "sub xfer done for master %u", data->master_mid);
  2402. mdata = Curl_multi_get_easy(multi, data->master_mid);
  2403. if(mdata) {
  2404. if(mdata->sub_xfer_done)
  2405. mdata->sub_xfer_done(mdata, data, result);
  2406. else
  2407. CURL_TRC_M(data, "master easy %u without sub_xfer_done callback.",
  2408. data->master_mid);
  2409. }
  2410. else {
  2411. CURL_TRC_M(data, "master easy %u already gone.", data->master_mid);
  2412. }
  2413. }
  2414. else {
  2415. /* now fill in the Curl_message with this info */
  2416. msg = &data->msg;
  2417. msg->extmsg.msg = CURLMSG_DONE;
  2418. msg->extmsg.easy_handle = data;
  2419. msg->extmsg.data.result = result;
  2420. multi_addmsg(multi, msg);
  2421. DEBUGASSERT(!data->conn);
  2422. }
  2423. multistate(data, MSTATE_MSGSENT);
  2424. /* remove from the other sets, add to msgsent */
  2425. Curl_uint32_bset_remove(&multi->process, data->mid);
  2426. Curl_uint32_bset_remove(&multi->dirty, data->mid);
  2427. Curl_uint32_bset_remove(&multi->pending, data->mid);
  2428. Curl_uint32_bset_add(&multi->msgsent, data->mid);
  2429. --multi->xfers_alive;
  2430. return CURLM_OK;
  2431. }
  2432. } while((mresult == CURLM_CALL_MULTI_PERFORM) ||
  2433. multi_ischanged(multi, FALSE));
  2434. data->result = result;
  2435. return mresult;
  2436. }
  2437. static CURLMcode multi_perform(struct Curl_multi *multi,
  2438. int *running_handles)
  2439. {
  2440. CURLMcode returncode = CURLM_OK;
  2441. struct curltime start = *multi_now(multi);
  2442. uint32_t mid;
  2443. SIGPIPE_VARIABLE(pipe_st);
  2444. if(multi->in_callback)
  2445. return CURLM_RECURSIVE_API_CALL;
  2446. if(multi->in_ntfy_callback)
  2447. return CURLM_RECURSIVE_API_CALL;
  2448. sigpipe_init(&pipe_st);
  2449. if(Curl_uint32_bset_first(&multi->process, &mid)) {
  2450. CURL_TRC_M(multi->admin, "multi_perform(running=%u)",
  2451. Curl_multi_xfers_running(multi));
  2452. do {
  2453. struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
  2454. CURLMcode mresult;
  2455. if(!data) {
  2456. DEBUGASSERT(0);
  2457. Curl_uint32_bset_remove(&multi->process, mid);
  2458. Curl_uint32_bset_remove(&multi->dirty, mid);
  2459. continue;
  2460. }
  2461. sigpipe_apply(data, &pipe_st);
  2462. mresult = multi_runsingle(multi, data);
  2463. if(mresult)
  2464. returncode = mresult;
  2465. } while(Curl_uint32_bset_next(&multi->process, mid, &mid));
  2466. }
  2467. sigpipe_restore(&pipe_st);
  2468. if(multi_ischanged(multi, TRUE))
  2469. process_pending_handles(multi);
  2470. if(!returncode && CURL_MNTFY_HAS_ENTRIES(multi))
  2471. returncode = Curl_mntfy_dispatch_all(multi);
  2472. /*
  2473. * Simply remove all expired timers from the splay since handles are dealt
  2474. * with unconditionally by this function and curl_multi_timeout() requires
  2475. * that already passed/handled expire times are removed from the splay.
  2476. *
  2477. * It is important that the 'now' value is set at the entry of this function
  2478. * and not for the current time as it may have ticked a little while since
  2479. * then and then we risk this loop to remove timers that actually have not
  2480. * been handled!
  2481. */
  2482. if(multi->timetree) {
  2483. struct Curl_tree *t = NULL;
  2484. do {
  2485. multi->timetree = Curl_splaygetbest(&start, multi->timetree, &t);
  2486. if(t) {
  2487. /* the removed may have another timeout in queue */
  2488. struct Curl_easy *data = Curl_splayget(t);
  2489. (void)add_next_timeout(&start, multi, data);
  2490. if(data->mstate == MSTATE_PENDING) {
  2491. bool stream_unused;
  2492. CURLcode result_unused;
  2493. if(multi_handle_timeout(data, &stream_unused, &result_unused)) {
  2494. infof(data, "PENDING handle timeout");
  2495. move_pending_to_connect(multi, data);
  2496. }
  2497. }
  2498. }
  2499. } while(t);
  2500. }
  2501. if(running_handles) {
  2502. unsigned int running = Curl_multi_xfers_running(multi);
  2503. *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
  2504. }
  2505. if(CURLM_OK >= returncode)
  2506. returncode = Curl_update_timer(multi);
  2507. return returncode;
  2508. }
  2509. CURLMcode curl_multi_perform(CURLM *m, int *running_handles)
  2510. {
  2511. struct Curl_multi *multi = m;
  2512. if(!GOOD_MULTI_HANDLE(multi))
  2513. return CURLM_BAD_HANDLE;
  2514. return multi_perform(multi, running_handles);
  2515. }
  2516. CURLMcode curl_multi_cleanup(CURLM *m)
  2517. {
  2518. struct Curl_multi *multi = m;
  2519. if(GOOD_MULTI_HANDLE(multi)) {
  2520. void *entry;
  2521. uint32_t mid;
  2522. if(multi->in_callback)
  2523. return CURLM_RECURSIVE_API_CALL;
  2524. if(multi->in_ntfy_callback)
  2525. return CURLM_RECURSIVE_API_CALL;
  2526. /* First remove all remaining easy handles,
  2527. * close internal ones. admin handle is special */
  2528. if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
  2529. do {
  2530. struct Curl_easy *data = entry;
  2531. if(!GOOD_EASY_HANDLE(data))
  2532. return CURLM_BAD_HANDLE;
  2533. #ifdef DEBUGBUILD
  2534. if(mid != data->mid) {
  2535. CURL_TRC_M(data, "multi_cleanup: still present with mid=%u, "
  2536. "but unexpected data->mid=%u\n", mid, data->mid);
  2537. DEBUGASSERT(0);
  2538. }
  2539. #endif
  2540. if(data == multi->admin)
  2541. continue;
  2542. if(!data->state.done && data->conn)
  2543. /* if DONE was never called for this handle */
  2544. (void)multi_done(data, CURLE_OK, TRUE);
  2545. data->multi = NULL; /* clear the association */
  2546. Curl_uint32_tbl_remove(&multi->xfers, mid);
  2547. data->mid = UINT32_MAX;
  2548. #ifdef USE_LIBPSL
  2549. if(data->psl == &multi->psl)
  2550. data->psl = NULL;
  2551. #endif
  2552. if(data->state.internal)
  2553. Curl_close(&data);
  2554. } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry));
  2555. }
  2556. Curl_cpool_destroy(&multi->cpool);
  2557. Curl_cshutdn_destroy(&multi->cshutdn, multi->admin);
  2558. if(multi->admin) {
  2559. CURL_TRC_M(multi->admin, "multi_cleanup, closing admin handle, done");
  2560. multi->admin->multi = NULL;
  2561. Curl_uint32_tbl_remove(&multi->xfers, multi->admin->mid);
  2562. Curl_close(&multi->admin);
  2563. }
  2564. multi->magic = 0; /* not good anymore */
  2565. Curl_multi_ev_cleanup(multi);
  2566. Curl_hash_destroy(&multi->proto_hash);
  2567. Curl_dnscache_destroy(&multi->dnscache);
  2568. Curl_psl_destroy(&multi->psl);
  2569. #ifdef USE_SSL
  2570. Curl_ssl_scache_destroy(multi->ssl_scache);
  2571. #endif
  2572. #ifdef USE_WINSOCK
  2573. WSACloseEvent(multi->wsa_event);
  2574. #else
  2575. #ifdef ENABLE_WAKEUP
  2576. wakeup_close(multi->wakeup_pair[0]);
  2577. #ifndef USE_EVENTFD
  2578. wakeup_close(multi->wakeup_pair[1]);
  2579. #endif
  2580. #endif
  2581. #endif
  2582. multi_xfer_bufs_free(multi);
  2583. Curl_mntfy_cleanup(multi);
  2584. #ifdef DEBUGBUILD
  2585. if(Curl_uint32_tbl_count(&multi->xfers)) {
  2586. multi_xfer_tbl_dump(multi);
  2587. DEBUGASSERT(0);
  2588. }
  2589. #endif
  2590. Curl_uint32_bset_destroy(&multi->process);
  2591. Curl_uint32_bset_destroy(&multi->dirty);
  2592. Curl_uint32_bset_destroy(&multi->pending);
  2593. Curl_uint32_bset_destroy(&multi->msgsent);
  2594. Curl_uint32_tbl_destroy(&multi->xfers);
  2595. curlx_free(multi);
  2596. return CURLM_OK;
  2597. }
  2598. return CURLM_BAD_HANDLE;
  2599. }
  2600. /*
  2601. * curl_multi_info_read()
  2602. *
  2603. * This function is the primary way for a multi/multi_socket application to
  2604. * figure out if a transfer has ended. We MUST make this function as fast as
  2605. * possible as it will be polled frequently and we MUST NOT scan any lists in
  2606. * here to figure out things. We must scale fine to thousands of handles and
  2607. * beyond. The current design is fully O(1).
  2608. */
  2609. CURLMsg *curl_multi_info_read(CURLM *m, int *msgs_in_queue)
  2610. {
  2611. struct Curl_message *msg;
  2612. struct Curl_multi *multi = m;
  2613. *msgs_in_queue = 0; /* default to none */
  2614. if(GOOD_MULTI_HANDLE(multi) &&
  2615. !multi->in_callback &&
  2616. Curl_llist_count(&multi->msglist)) {
  2617. /* there is one or more messages in the list */
  2618. struct Curl_llist_node *e;
  2619. /* extract the head of the list to return */
  2620. e = Curl_llist_head(&multi->msglist);
  2621. msg = Curl_node_elem(e);
  2622. /* remove the extracted entry */
  2623. Curl_node_remove(e);
  2624. *msgs_in_queue = curlx_uztosi(Curl_llist_count(&multi->msglist));
  2625. return &msg->extmsg;
  2626. }
  2627. return NULL;
  2628. }
  2629. void Curl_multi_will_close(struct Curl_easy *data, curl_socket_t s)
  2630. {
  2631. if(data) {
  2632. struct Curl_multi *multi = data->multi;
  2633. if(multi) {
  2634. CURL_TRC_M(data, "Curl_multi_will_close fd=%" FMT_SOCKET_T, s);
  2635. Curl_multi_ev_socket_done(multi, data, s);
  2636. }
  2637. }
  2638. }
  2639. /*
  2640. * add_next_timeout()
  2641. *
  2642. * Each Curl_easy has a list of timeouts. The add_next_timeout() is called
  2643. * when it has just been removed from the splay tree because the timeout has
  2644. * expired. This function is then to advance in the list to pick the next
  2645. * timeout to use (skip the already expired ones) and add this node back to
  2646. * the splay tree again.
  2647. *
  2648. * The splay tree only has each sessionhandle as a single node and the nearest
  2649. * timeout is used to sort it on.
  2650. */
  2651. static CURLMcode add_next_timeout(const struct curltime *pnow,
  2652. struct Curl_multi *multi,
  2653. struct Curl_easy *d)
  2654. {
  2655. struct curltime *tv = &d->state.expiretime;
  2656. struct Curl_llist *list = &d->state.timeoutlist;
  2657. struct Curl_llist_node *e;
  2658. /* move over the timeout list for this specific handle and remove all
  2659. timeouts that are now passed tense and store the next pending
  2660. timeout in *tv */
  2661. for(e = Curl_llist_head(list); e;) {
  2662. struct Curl_llist_node *n = Curl_node_next(e);
  2663. struct time_node *node = Curl_node_elem(e);
  2664. timediff_t diff = curlx_ptimediff_us(&node->time, pnow);
  2665. if(diff <= 0)
  2666. /* remove outdated entry */
  2667. Curl_node_remove(e);
  2668. else
  2669. /* the list is sorted so get out on the first mismatch */
  2670. break;
  2671. e = n;
  2672. }
  2673. e = Curl_llist_head(list);
  2674. if(!e) {
  2675. /* clear the expire times within the handles that we remove from the
  2676. splay tree */
  2677. tv->tv_sec = 0;
  2678. tv->tv_usec = 0;
  2679. }
  2680. else {
  2681. struct time_node *node = Curl_node_elem(e);
  2682. /* copy the first entry to 'tv' */
  2683. memcpy(tv, &node->time, sizeof(*tv));
  2684. /* Insert this node again into the splay. Keep the timer in the list in
  2685. case we need to recompute future timers. */
  2686. multi->timetree = Curl_splayinsert(tv, multi->timetree,
  2687. &d->state.timenode);
  2688. }
  2689. return CURLM_OK;
  2690. }
  2691. struct multi_run_ctx {
  2692. struct Curl_multi *multi;
  2693. size_t run_xfers;
  2694. SIGPIPE_MEMBER(pipe_st);
  2695. };
  2696. static void multi_mark_expired_as_dirty(struct multi_run_ctx *mrc,
  2697. const struct curltime *ts)
  2698. {
  2699. struct Curl_multi *multi = mrc->multi;
  2700. struct Curl_easy *data = NULL;
  2701. struct Curl_tree *t = NULL;
  2702. /*
  2703. * The loop following here will go on as long as there are expire-times left
  2704. * to process (compared to mrc->now) in the splay and 'data' will be
  2705. * re-assigned for every expired handle we deal with.
  2706. */
  2707. while(1) {
  2708. /* Check if there is one (more) expired timer to deal with! This function
  2709. extracts a matching node if there is one */
  2710. multi->timetree = Curl_splaygetbest(ts, multi->timetree, &t);
  2711. if(!t)
  2712. return;
  2713. data = Curl_splayget(t); /* assign this for next loop */
  2714. if(!data)
  2715. continue;
  2716. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  2717. if(CURL_TRC_TIMER_is_verbose(data)) {
  2718. struct Curl_llist_node *e = Curl_llist_head(&data->state.timeoutlist);
  2719. if(e) {
  2720. struct time_node *n = Curl_node_elem(e);
  2721. CURL_TRC_TIMER(data, n->eid, "has expired");
  2722. }
  2723. }
  2724. #endif
  2725. (void)add_next_timeout(ts, multi, data);
  2726. Curl_multi_mark_dirty(data);
  2727. }
  2728. }
  2729. static CURLMcode multi_run_dirty(struct multi_run_ctx *mrc)
  2730. {
  2731. struct Curl_multi *multi = mrc->multi;
  2732. CURLMcode mresult = CURLM_OK;
  2733. uint32_t mid;
  2734. if(Curl_uint32_bset_first(&multi->dirty, &mid)) {
  2735. do {
  2736. struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
  2737. if(data) {
  2738. CURL_TRC_M(data, "multi_run_dirty");
  2739. if(!Curl_uint32_bset_contains(&multi->process, mid)) {
  2740. /* We are no longer processing this transfer */
  2741. Curl_uint32_bset_remove(&multi->dirty, mid);
  2742. continue;
  2743. }
  2744. mrc->run_xfers++;
  2745. sigpipe_apply(data, &mrc->pipe_st);
  2746. /* runsingle() clears the dirty mid */
  2747. mresult = multi_runsingle(multi, data);
  2748. if(CURLM_OK >= mresult) {
  2749. /* reassess event handling of data */
  2750. mresult = Curl_multi_ev_assess_xfer(multi, data);
  2751. if(mresult)
  2752. goto out;
  2753. }
  2754. }
  2755. else {
  2756. CURL_TRC_M(multi->admin, "multi_run_dirty, %u no longer found", mid);
  2757. Curl_uint32_bset_remove(&multi->dirty, mid);
  2758. }
  2759. } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid));
  2760. }
  2761. out:
  2762. return mresult;
  2763. }
  2764. static CURLMcode multi_socket(struct Curl_multi *multi,
  2765. bool checkall,
  2766. curl_socket_t s,
  2767. int ev_bitmask,
  2768. int *running_handles)
  2769. {
  2770. CURLMcode mresult = CURLM_OK;
  2771. struct multi_run_ctx mrc;
  2772. (void)ev_bitmask;
  2773. memset(&mrc, 0, sizeof(mrc));
  2774. mrc.multi = multi;
  2775. sigpipe_init(&mrc.pipe_st);
  2776. if(checkall) {
  2777. /* *perform() deals with running_handles on its own */
  2778. mresult = multi_perform(multi, running_handles);
  2779. if(mresult != CURLM_BAD_HANDLE) {
  2780. /* Reassess event status of all active transfers */
  2781. mresult = Curl_multi_ev_assess_xfer_bset(multi, &multi->process);
  2782. }
  2783. goto out;
  2784. }
  2785. if(s != CURL_SOCKET_TIMEOUT) {
  2786. /* Mark all transfers of that socket as dirty */
  2787. Curl_multi_ev_dirty_xfers(multi, s);
  2788. }
  2789. else {
  2790. /* Asked to run due to time-out. Clear the 'last_expire_ts' variable to
  2791. force Curl_update_timer() to trigger a callback to the app again even
  2792. if the same timeout is still the one to run after this call. That
  2793. handles the case when the application asks libcurl to run the timeout
  2794. prematurely. */
  2795. memset(&multi->last_expire_ts, 0, sizeof(multi->last_expire_ts));
  2796. }
  2797. multi_mark_expired_as_dirty(&mrc, multi_now(multi));
  2798. mresult = multi_run_dirty(&mrc);
  2799. if(mresult)
  2800. goto out;
  2801. if(mrc.run_xfers) {
  2802. /* Running transfers takes time. With a new timestamp, we might catch
  2803. * other expires which are due now. Instead of telling the application
  2804. * to set a 0 timeout and call us again, we run them here.
  2805. * Do that only once or it might be unfair to transfers on other
  2806. * sockets. */
  2807. multi_mark_expired_as_dirty(&mrc, &multi->now);
  2808. mresult = multi_run_dirty(&mrc);
  2809. }
  2810. out:
  2811. sigpipe_restore(&mrc.pipe_st);
  2812. if(multi_ischanged(multi, TRUE))
  2813. process_pending_handles(multi);
  2814. if(!mresult && CURL_MNTFY_HAS_ENTRIES(multi))
  2815. mresult = Curl_mntfy_dispatch_all(multi);
  2816. if(running_handles) {
  2817. unsigned int running = Curl_multi_xfers_running(multi);
  2818. *running_handles = (running < INT_MAX) ? (int)running : INT_MAX;
  2819. }
  2820. if(CURLM_OK >= mresult)
  2821. mresult = Curl_update_timer(multi);
  2822. return mresult;
  2823. }
  2824. #undef curl_multi_setopt
  2825. CURLMcode curl_multi_setopt(CURLM *m, CURLMoption option, ...)
  2826. {
  2827. CURLMcode mresult = CURLM_OK;
  2828. va_list param;
  2829. unsigned long uarg;
  2830. struct Curl_multi *multi = m;
  2831. if(!GOOD_MULTI_HANDLE(multi))
  2832. return CURLM_BAD_HANDLE;
  2833. if(multi->in_callback)
  2834. return CURLM_RECURSIVE_API_CALL;
  2835. va_start(param, option);
  2836. switch(option) {
  2837. case CURLMOPT_SOCKETFUNCTION:
  2838. multi->socket_cb = va_arg(param, curl_socket_callback);
  2839. break;
  2840. case CURLMOPT_SOCKETDATA:
  2841. multi->socket_userp = va_arg(param, void *);
  2842. break;
  2843. case CURLMOPT_PUSHFUNCTION:
  2844. multi->push_cb = va_arg(param, curl_push_callback);
  2845. break;
  2846. case CURLMOPT_PUSHDATA:
  2847. multi->push_userp = va_arg(param, void *);
  2848. break;
  2849. case CURLMOPT_PIPELINING:
  2850. multi->multiplexing = va_arg(param, long) & CURLPIPE_MULTIPLEX ? 1 : 0;
  2851. break;
  2852. case CURLMOPT_TIMERFUNCTION:
  2853. multi->timer_cb = va_arg(param, curl_multi_timer_callback);
  2854. break;
  2855. case CURLMOPT_TIMERDATA:
  2856. multi->timer_userp = va_arg(param, void *);
  2857. break;
  2858. case CURLMOPT_MAXCONNECTS:
  2859. uarg = va_arg(param, unsigned long);
  2860. if(uarg <= UINT_MAX)
  2861. multi->maxconnects = (unsigned int)uarg;
  2862. break;
  2863. case CURLMOPT_MAX_HOST_CONNECTIONS:
  2864. if(!curlx_sltouz(va_arg(param, long), &multi->max_host_connections))
  2865. mresult = CURLM_BAD_FUNCTION_ARGUMENT;
  2866. break;
  2867. case CURLMOPT_MAX_TOTAL_CONNECTIONS:
  2868. if(!curlx_sltouz(va_arg(param, long), &multi->max_total_connections))
  2869. mresult = CURLM_BAD_FUNCTION_ARGUMENT;
  2870. break;
  2871. /* options formerly used for pipelining */
  2872. case CURLMOPT_MAX_PIPELINE_LENGTH:
  2873. break;
  2874. case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
  2875. break;
  2876. case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
  2877. break;
  2878. case CURLMOPT_PIPELINING_SITE_BL:
  2879. break;
  2880. case CURLMOPT_PIPELINING_SERVER_BL:
  2881. break;
  2882. case CURLMOPT_MAX_CONCURRENT_STREAMS: {
  2883. long streams = va_arg(param, long);
  2884. if((streams < 1) || (streams > INT_MAX))
  2885. streams = 100;
  2886. multi->max_concurrent_streams = (unsigned int)streams;
  2887. break;
  2888. }
  2889. case CURLMOPT_NETWORK_CHANGED: {
  2890. long val = va_arg(param, long);
  2891. if(val & CURLMNWC_CLEAR_DNS) {
  2892. Curl_dnscache_clear(multi->admin);
  2893. }
  2894. if(val & CURLMNWC_CLEAR_CONNS) {
  2895. Curl_cpool_nw_changed(multi->admin);
  2896. }
  2897. break;
  2898. }
  2899. case CURLMOPT_NOTIFYFUNCTION:
  2900. multi->ntfy.ntfy_cb = va_arg(param, curl_notify_callback);
  2901. break;
  2902. case CURLMOPT_NOTIFYDATA:
  2903. multi->ntfy.ntfy_cb_data = va_arg(param, void *);
  2904. break;
  2905. default:
  2906. mresult = CURLM_UNKNOWN_OPTION;
  2907. break;
  2908. }
  2909. va_end(param);
  2910. return mresult;
  2911. }
  2912. /* we define curl_multi_socket() in the public multi.h header */
  2913. #undef curl_multi_socket
  2914. CURLMcode curl_multi_socket(CURLM *m, curl_socket_t s, int *running_handles)
  2915. {
  2916. struct Curl_multi *multi = m;
  2917. if(multi->in_callback)
  2918. return CURLM_RECURSIVE_API_CALL;
  2919. if(multi->in_ntfy_callback)
  2920. return CURLM_RECURSIVE_API_CALL;
  2921. return multi_socket(multi, FALSE, s, 0, running_handles);
  2922. }
  2923. CURLMcode curl_multi_socket_action(CURLM *m, curl_socket_t s,
  2924. int ev_bitmask, int *running_handles)
  2925. {
  2926. struct Curl_multi *multi = m;
  2927. if(multi->in_callback)
  2928. return CURLM_RECURSIVE_API_CALL;
  2929. if(multi->in_ntfy_callback)
  2930. return CURLM_RECURSIVE_API_CALL;
  2931. return multi_socket(multi, FALSE, s, ev_bitmask, running_handles);
  2932. }
  2933. CURLMcode curl_multi_socket_all(CURLM *m, int *running_handles)
  2934. {
  2935. struct Curl_multi *multi = m;
  2936. if(multi->in_callback)
  2937. return CURLM_RECURSIVE_API_CALL;
  2938. if(multi->in_ntfy_callback)
  2939. return CURLM_RECURSIVE_API_CALL;
  2940. return multi_socket(multi, TRUE, CURL_SOCKET_BAD, 0, running_handles);
  2941. }
  2942. static bool multi_has_dirties(struct Curl_multi *multi)
  2943. {
  2944. uint32_t mid;
  2945. if(Curl_uint32_bset_first(&multi->dirty, &mid)) {
  2946. do {
  2947. struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
  2948. if(data) {
  2949. if(Curl_uint32_bset_contains(&multi->process, mid))
  2950. return TRUE;
  2951. /* We are no longer processing this transfer */
  2952. Curl_uint32_bset_remove(&multi->dirty, mid);
  2953. }
  2954. else {
  2955. CURL_TRC_M(multi->admin, "dirty transfer %u no longer found", mid);
  2956. Curl_uint32_bset_remove(&multi->dirty, mid);
  2957. }
  2958. } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid));
  2959. }
  2960. return FALSE;
  2961. }
  2962. static void multi_timeout(struct Curl_multi *multi,
  2963. struct curltime *expire_time,
  2964. long *timeout_ms)
  2965. {
  2966. static const struct curltime tv_zero = { 0, 0 };
  2967. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  2968. struct Curl_easy *data = NULL;
  2969. #endif
  2970. if(multi->dead) {
  2971. *timeout_ms = 0;
  2972. return;
  2973. }
  2974. if(multi_has_dirties(multi)) {
  2975. *expire_time = *multi_now(multi);
  2976. *timeout_ms = 0;
  2977. return;
  2978. }
  2979. else if(multi->timetree) {
  2980. const struct curltime *pnow = multi_now(multi);
  2981. /* splay the lowest to the bottom */
  2982. multi->timetree = Curl_splay(&tv_zero, multi->timetree);
  2983. /* this will not return NULL from a non-empty tree, but some compilers
  2984. * are not convinced of that. Analyzers are hard. */
  2985. *expire_time = multi->timetree ? multi->timetree->key : tv_zero;
  2986. /* 'multi->timetree' will be non-NULL here but the compilers sometimes
  2987. yell at us if we assume so */
  2988. if(multi->timetree &&
  2989. curlx_ptimediff_us(&multi->timetree->key, pnow) > 0) {
  2990. /* some time left before expiration */
  2991. timediff_t diff_ms =
  2992. curlx_timediff_ceil_ms(multi->timetree->key, *pnow);
  2993. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  2994. data = Curl_splayget(multi->timetree);
  2995. #endif
  2996. /* this should be safe even on 32-bit archs, as we do not use that
  2997. overly long timeouts */
  2998. *timeout_ms = (long)diff_ms;
  2999. }
  3000. else {
  3001. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  3002. if(multi->timetree) {
  3003. data = Curl_splayget(multi->timetree);
  3004. }
  3005. #endif
  3006. /* 0 means immediately */
  3007. *timeout_ms = 0;
  3008. }
  3009. }
  3010. else {
  3011. *expire_time = tv_zero;
  3012. *timeout_ms = -1;
  3013. }
  3014. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  3015. if(data && CURL_TRC_TIMER_is_verbose(data)) {
  3016. struct Curl_llist_node *e = Curl_llist_head(&data->state.timeoutlist);
  3017. if(e) {
  3018. struct time_node *n = Curl_node_elem(e);
  3019. CURL_TRC_TIMER(data, n->eid, "gives multi timeout in %ldms",
  3020. *timeout_ms);
  3021. }
  3022. }
  3023. #endif
  3024. }
  3025. CURLMcode curl_multi_timeout(CURLM *m,
  3026. long *timeout_ms)
  3027. {
  3028. struct curltime expire_time;
  3029. struct Curl_multi *multi = m;
  3030. /* First, make some basic checks that the CURLM handle is a good handle */
  3031. if(!GOOD_MULTI_HANDLE(multi))
  3032. return CURLM_BAD_HANDLE;
  3033. if(multi->in_callback)
  3034. return CURLM_RECURSIVE_API_CALL;
  3035. multi_timeout(multi, &expire_time, timeout_ms);
  3036. return CURLM_OK;
  3037. }
  3038. /*
  3039. * Tell the application it should update its timers, if it subscribes to the
  3040. * update timer callback.
  3041. */
  3042. CURLMcode Curl_update_timer(struct Curl_multi *multi)
  3043. {
  3044. struct curltime expire_ts;
  3045. long timeout_ms;
  3046. int rc;
  3047. bool set_value = FALSE;
  3048. if(!multi->timer_cb || multi->dead)
  3049. return CURLM_OK;
  3050. multi_timeout(multi, &expire_ts, &timeout_ms);
  3051. if(timeout_ms < 0 && multi->last_timeout_ms < 0) {
  3052. /* nothing to do */
  3053. }
  3054. else if(timeout_ms < 0) {
  3055. /* there is no timeout now but there was one previously */
  3056. CURL_TRC_M(multi->admin, "[TIMER] clear");
  3057. timeout_ms = -1; /* normalize */
  3058. set_value = TRUE;
  3059. }
  3060. else if(multi->last_timeout_ms < 0) {
  3061. CURL_TRC_M(multi->admin, "[TIMER] set %ldms, none before", timeout_ms);
  3062. set_value = TRUE;
  3063. }
  3064. else if(curlx_ptimediff_us(&multi->last_expire_ts, &expire_ts)) {
  3065. /* We had a timeout before and have one now, the absolute timestamp
  3066. * differs. The relative timeout_ms may be the same, but the starting
  3067. * point differs. Let the application restart its timer. */
  3068. CURL_TRC_M(multi->admin, "[TIMER] set %ldms, replace previous",
  3069. timeout_ms);
  3070. set_value = TRUE;
  3071. }
  3072. else {
  3073. /* We have same expire time as previously. Our relative 'timeout_ms'
  3074. * may be different now, but the application has the timer running
  3075. * and we do not to tell it to start this again. */
  3076. }
  3077. if(set_value) {
  3078. multi->last_expire_ts = expire_ts;
  3079. multi->last_timeout_ms = timeout_ms;
  3080. set_in_callback(multi, TRUE);
  3081. rc = multi->timer_cb(multi, timeout_ms, multi->timer_userp);
  3082. set_in_callback(multi, FALSE);
  3083. if(rc == -1) {
  3084. multi->dead = TRUE;
  3085. return CURLM_ABORTED_BY_CALLBACK;
  3086. }
  3087. }
  3088. return CURLM_OK;
  3089. }
  3090. /*
  3091. * multi_deltimeout()
  3092. *
  3093. * Remove a given timestamp from the list of timeouts.
  3094. */
  3095. static void multi_deltimeout(struct Curl_easy *data, expire_id eid)
  3096. {
  3097. struct Curl_llist_node *e;
  3098. struct Curl_llist *timeoutlist = &data->state.timeoutlist;
  3099. /* find and remove the specific node from the list */
  3100. for(e = Curl_llist_head(timeoutlist); e; e = Curl_node_next(e)) {
  3101. struct time_node *n = Curl_node_elem(e);
  3102. if(n->eid == eid) {
  3103. Curl_node_remove(e);
  3104. return;
  3105. }
  3106. }
  3107. }
  3108. /*
  3109. * multi_addtimeout()
  3110. *
  3111. * Add a timestamp to the list of timeouts. Keep the list sorted so that head
  3112. * of list is always the timeout nearest in time.
  3113. *
  3114. */
  3115. static CURLMcode multi_addtimeout(struct Curl_easy *data,
  3116. struct curltime *stamp,
  3117. expire_id eid)
  3118. {
  3119. struct Curl_llist_node *e;
  3120. struct time_node *node;
  3121. struct Curl_llist_node *prev = NULL;
  3122. size_t n;
  3123. struct Curl_llist *timeoutlist = &data->state.timeoutlist;
  3124. node = &data->state.expires[eid];
  3125. /* copy the timestamp and id */
  3126. memcpy(&node->time, stamp, sizeof(*stamp));
  3127. node->eid = eid; /* also marks it as in use */
  3128. n = Curl_llist_count(timeoutlist);
  3129. if(n) {
  3130. /* find the correct spot in the list */
  3131. for(e = Curl_llist_head(timeoutlist); e; e = Curl_node_next(e)) {
  3132. struct time_node *check = Curl_node_elem(e);
  3133. timediff_t diff = curlx_ptimediff_ms(&check->time, &node->time);
  3134. if(diff > 0)
  3135. break;
  3136. prev = e;
  3137. }
  3138. }
  3139. /* else
  3140. this is the first timeout on the list */
  3141. Curl_llist_insert_next(timeoutlist, prev, node, &node->list);
  3142. CURL_TRC_TIMER(data, eid, "set for %" FMT_TIMEDIFF_T "ns",
  3143. curlx_ptimediff_us(&node->time, Curl_pgrs_now(data)));
  3144. return CURLM_OK;
  3145. }
  3146. void Curl_expire_ex(struct Curl_easy *data,
  3147. timediff_t milli, expire_id id)
  3148. {
  3149. struct Curl_multi *multi = data->multi;
  3150. struct curltime *curr_expire = &data->state.expiretime;
  3151. struct curltime set;
  3152. /* this is only interesting while there is still an associated multi struct
  3153. remaining! */
  3154. if(!multi)
  3155. return;
  3156. DEBUGASSERT(id < EXPIRE_LAST);
  3157. set = *Curl_pgrs_now(data);
  3158. set.tv_sec += (time_t)(milli / 1000); /* may be a 64 to 32-bit conversion */
  3159. set.tv_usec += (int)(milli % 1000) * 1000;
  3160. if(set.tv_usec >= 1000000) {
  3161. set.tv_sec++;
  3162. set.tv_usec -= 1000000;
  3163. }
  3164. /* Remove any timer with the same id just in case. */
  3165. multi_deltimeout(data, id);
  3166. /* Add it to the timer list. It must stay in the list until it has expired
  3167. in case we need to recompute the minimum timer later. */
  3168. multi_addtimeout(data, &set, id);
  3169. if(curr_expire->tv_sec || curr_expire->tv_usec) {
  3170. /* This means that the struct is added as a node in the splay tree.
  3171. Compare if the new time is earlier, and only remove-old/add-new if it
  3172. is. */
  3173. timediff_t diff = curlx_ptimediff_ms(&set, curr_expire);
  3174. int rc;
  3175. if(diff > 0) {
  3176. /* The current splay tree entry is sooner than this new expiry time.
  3177. We do not need to update our splay tree entry. */
  3178. return;
  3179. }
  3180. /* Since this is an updated time, we must remove the previous entry from
  3181. the splay tree first and then re-add the new value */
  3182. rc = Curl_splayremove(multi->timetree, &data->state.timenode,
  3183. &multi->timetree);
  3184. if(rc)
  3185. infof(data, "Internal error removing splay node = %d", rc);
  3186. }
  3187. /* Indicate that we are in the splay tree and insert the new timer expiry
  3188. value since it is our local minimum. */
  3189. *curr_expire = set;
  3190. Curl_splayset(&data->state.timenode, data);
  3191. multi->timetree = Curl_splayinsert(curr_expire, multi->timetree,
  3192. &data->state.timenode);
  3193. }
  3194. /*
  3195. * Curl_expire()
  3196. *
  3197. * given a number of milliseconds from now to use to set the 'act before
  3198. * this'-time for the transfer, to be extracted by curl_multi_timeout()
  3199. *
  3200. * The timeout will be added to a queue of timeouts if it defines a moment in
  3201. * time that is later than the current head of queue.
  3202. *
  3203. * Expire replaces a former timeout using the same id if already set.
  3204. */
  3205. void Curl_expire(struct Curl_easy *data, timediff_t milli, expire_id id)
  3206. {
  3207. Curl_expire_ex(data, milli, id);
  3208. }
  3209. /*
  3210. * Curl_expire_done()
  3211. *
  3212. * Removes the expire timer. Marks it as done.
  3213. *
  3214. */
  3215. void Curl_expire_done(struct Curl_easy *data, expire_id eid)
  3216. {
  3217. /* remove the timer, if there */
  3218. multi_deltimeout(data, eid);
  3219. CURL_TRC_TIMER(data, eid, "cleared");
  3220. }
  3221. /*
  3222. * Curl_expire_clear()
  3223. *
  3224. * Clear ALL timeout values for this handle.
  3225. */
  3226. bool Curl_expire_clear(struct Curl_easy *data)
  3227. {
  3228. struct Curl_multi *multi = data->multi;
  3229. struct curltime *nowp = &data->state.expiretime;
  3230. /* this is only interesting while there is still an associated multi struct
  3231. remaining! */
  3232. if(!multi)
  3233. return FALSE;
  3234. if(nowp->tv_sec || nowp->tv_usec) {
  3235. /* Since this is an cleared time, we must remove the previous entry from
  3236. the splay tree */
  3237. struct Curl_llist *list = &data->state.timeoutlist;
  3238. int rc;
  3239. rc = Curl_splayremove(multi->timetree, &data->state.timenode,
  3240. &multi->timetree);
  3241. if(rc)
  3242. infof(data, "Internal error clearing splay node = %d", rc);
  3243. /* clear the timeout list too */
  3244. Curl_llist_destroy(list, NULL);
  3245. if(data->id >= 0)
  3246. CURL_TRC_M(data, "[TIMEOUT] all cleared");
  3247. nowp->tv_sec = 0;
  3248. nowp->tv_usec = 0;
  3249. return TRUE;
  3250. }
  3251. return FALSE;
  3252. }
  3253. CURLMcode curl_multi_assign(CURLM *m, curl_socket_t s,
  3254. void *hashp)
  3255. {
  3256. struct Curl_multi *multi = m;
  3257. if(!GOOD_MULTI_HANDLE(multi))
  3258. return CURLM_BAD_HANDLE;
  3259. return Curl_multi_ev_assign(multi, s, hashp);
  3260. }
  3261. static void move_pending_to_connect(struct Curl_multi *multi,
  3262. struct Curl_easy *data)
  3263. {
  3264. DEBUGASSERT(data->mstate == MSTATE_PENDING);
  3265. /* Remove this node from the pending set, add into process set */
  3266. Curl_uint32_bset_remove(&multi->pending, data->mid);
  3267. Curl_uint32_bset_add(&multi->process, data->mid);
  3268. multistate(data, MSTATE_CONNECT);
  3269. Curl_multi_mark_dirty(data); /* make it run */
  3270. }
  3271. /* process_pending_handles() moves a handle from PENDING back into the process
  3272. list and change state to CONNECT.
  3273. We do not move all transfers because that can be a significant amount.
  3274. Since this is tried every now and then doing too many too often becomes a
  3275. performance problem.
  3276. When there is a change for connection limits like max host connections etc,
  3277. this likely only allows one new transfer. When there is a pipewait change,
  3278. it can potentially allow hundreds of new transfers.
  3279. We could consider an improvement where we store the queue reason and allow
  3280. more pipewait rechecks than others.
  3281. */
  3282. static void process_pending_handles(struct Curl_multi *multi)
  3283. {
  3284. uint32_t mid;
  3285. if(Curl_uint32_bset_first(&multi->pending, &mid)) {
  3286. do {
  3287. struct Curl_easy *data = Curl_multi_get_easy(multi, mid);
  3288. if(data) {
  3289. move_pending_to_connect(multi, data);
  3290. break;
  3291. }
  3292. /* transfer no longer known, should not happen */
  3293. Curl_uint32_bset_remove(&multi->pending, mid);
  3294. DEBUGASSERT(0);
  3295. } while(Curl_uint32_bset_next(&multi->pending, mid, &mid));
  3296. }
  3297. }
  3298. void Curl_set_in_callback(struct Curl_easy *data, bool value)
  3299. {
  3300. if(data && data->multi)
  3301. data->multi->in_callback = value;
  3302. }
  3303. bool Curl_is_in_callback(struct Curl_easy *data)
  3304. {
  3305. return data && data->multi && data->multi->in_callback;
  3306. }
  3307. unsigned int Curl_multi_max_concurrent_streams(struct Curl_multi *multi)
  3308. {
  3309. DEBUGASSERT(multi);
  3310. return multi->max_concurrent_streams;
  3311. }
  3312. CURL **curl_multi_get_handles(CURLM *m)
  3313. {
  3314. struct Curl_multi *multi = m;
  3315. void *entry;
  3316. unsigned int count = Curl_uint32_tbl_count(&multi->xfers);
  3317. CURL **a = curlx_malloc(sizeof(struct Curl_easy *) * (count + 1));
  3318. if(a) {
  3319. unsigned int i = 0;
  3320. uint32_t mid;
  3321. if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
  3322. do {
  3323. struct Curl_easy *data = entry;
  3324. DEBUGASSERT(i < count);
  3325. if(!data->state.internal)
  3326. a[i++] = data;
  3327. } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry));
  3328. }
  3329. a[i] = NULL; /* last entry is a NULL */
  3330. }
  3331. return a;
  3332. }
  3333. CURLMcode curl_multi_get_offt(CURLM *m,
  3334. CURLMinfo_offt info,
  3335. curl_off_t *pvalue)
  3336. {
  3337. struct Curl_multi *multi = m;
  3338. uint32_t n;
  3339. if(!GOOD_MULTI_HANDLE(multi))
  3340. return CURLM_BAD_HANDLE;
  3341. if(!pvalue)
  3342. return CURLM_BAD_FUNCTION_ARGUMENT;
  3343. switch(info) {
  3344. case CURLMINFO_XFERS_CURRENT:
  3345. n = Curl_uint32_tbl_count(&multi->xfers);
  3346. if(n && multi->admin)
  3347. --n;
  3348. *pvalue = (curl_off_t)n;
  3349. return CURLM_OK;
  3350. case CURLMINFO_XFERS_RUNNING:
  3351. n = Curl_uint32_bset_count(&multi->process);
  3352. if(n && Curl_uint32_bset_contains(&multi->process, multi->admin->mid))
  3353. --n;
  3354. *pvalue = (curl_off_t)n;
  3355. return CURLM_OK;
  3356. case CURLMINFO_XFERS_PENDING:
  3357. *pvalue = (curl_off_t)Curl_uint32_bset_count(&multi->pending);
  3358. return CURLM_OK;
  3359. case CURLMINFO_XFERS_DONE:
  3360. *pvalue = (curl_off_t)Curl_uint32_bset_count(&multi->msgsent);
  3361. return CURLM_OK;
  3362. case CURLMINFO_XFERS_ADDED:
  3363. *pvalue = multi->xfers_total_ever;
  3364. return CURLM_OK;
  3365. default:
  3366. *pvalue = -1;
  3367. return CURLM_UNKNOWN_OPTION;
  3368. }
  3369. }
  3370. CURLcode Curl_multi_xfer_buf_borrow(struct Curl_easy *data,
  3371. char **pbuf, size_t *pbuflen)
  3372. {
  3373. DEBUGASSERT(data);
  3374. DEBUGASSERT(data->multi);
  3375. *pbuf = NULL;
  3376. *pbuflen = 0;
  3377. if(!data->multi) {
  3378. failf(data, "transfer has no multi handle");
  3379. return CURLE_FAILED_INIT;
  3380. }
  3381. if(!data->set.buffer_size) {
  3382. failf(data, "transfer buffer size is 0");
  3383. return CURLE_FAILED_INIT;
  3384. }
  3385. if(data->multi->xfer_buf_borrowed) {
  3386. failf(data, "attempt to borrow xfer_buf when already borrowed");
  3387. return CURLE_AGAIN;
  3388. }
  3389. if(data->multi->xfer_buf &&
  3390. data->set.buffer_size > data->multi->xfer_buf_len) {
  3391. /* not large enough, get a new one */
  3392. curlx_free(data->multi->xfer_buf);
  3393. data->multi->xfer_buf = NULL;
  3394. data->multi->xfer_buf_len = 0;
  3395. }
  3396. if(!data->multi->xfer_buf) {
  3397. data->multi->xfer_buf = curlx_malloc(curlx_uitouz(data->set.buffer_size));
  3398. if(!data->multi->xfer_buf) {
  3399. failf(data, "could not allocate xfer_buf of %u bytes",
  3400. data->set.buffer_size);
  3401. return CURLE_OUT_OF_MEMORY;
  3402. }
  3403. data->multi->xfer_buf_len = data->set.buffer_size;
  3404. }
  3405. data->multi->xfer_buf_borrowed = TRUE;
  3406. *pbuf = data->multi->xfer_buf;
  3407. *pbuflen = data->multi->xfer_buf_len;
  3408. return CURLE_OK;
  3409. }
  3410. void Curl_multi_xfer_buf_release(struct Curl_easy *data, char *buf)
  3411. {
  3412. (void)buf;
  3413. DEBUGASSERT(data);
  3414. DEBUGASSERT(data->multi);
  3415. DEBUGASSERT(!buf || data->multi->xfer_buf == buf);
  3416. data->multi->xfer_buf_borrowed = FALSE;
  3417. }
  3418. CURLcode Curl_multi_xfer_ulbuf_borrow(struct Curl_easy *data,
  3419. char **pbuf, size_t *pbuflen)
  3420. {
  3421. DEBUGASSERT(data);
  3422. DEBUGASSERT(data->multi);
  3423. *pbuf = NULL;
  3424. *pbuflen = 0;
  3425. if(!data->multi) {
  3426. failf(data, "transfer has no multi handle");
  3427. return CURLE_FAILED_INIT;
  3428. }
  3429. if(!data->set.upload_buffer_size) {
  3430. failf(data, "transfer upload buffer size is 0");
  3431. return CURLE_FAILED_INIT;
  3432. }
  3433. if(data->multi->xfer_ulbuf_borrowed) {
  3434. failf(data, "attempt to borrow xfer_ulbuf when already borrowed");
  3435. return CURLE_AGAIN;
  3436. }
  3437. if(data->multi->xfer_ulbuf &&
  3438. data->set.upload_buffer_size > data->multi->xfer_ulbuf_len) {
  3439. /* not large enough, get a new one */
  3440. curlx_free(data->multi->xfer_ulbuf);
  3441. data->multi->xfer_ulbuf = NULL;
  3442. data->multi->xfer_ulbuf_len = 0;
  3443. }
  3444. if(!data->multi->xfer_ulbuf) {
  3445. data->multi->xfer_ulbuf =
  3446. curlx_malloc(curlx_uitouz(data->set.upload_buffer_size));
  3447. if(!data->multi->xfer_ulbuf) {
  3448. failf(data, "could not allocate xfer_ulbuf of %u bytes",
  3449. data->set.upload_buffer_size);
  3450. return CURLE_OUT_OF_MEMORY;
  3451. }
  3452. data->multi->xfer_ulbuf_len = data->set.upload_buffer_size;
  3453. }
  3454. data->multi->xfer_ulbuf_borrowed = TRUE;
  3455. *pbuf = data->multi->xfer_ulbuf;
  3456. *pbuflen = data->multi->xfer_ulbuf_len;
  3457. return CURLE_OK;
  3458. }
  3459. void Curl_multi_xfer_ulbuf_release(struct Curl_easy *data, char *buf)
  3460. {
  3461. (void)buf;
  3462. DEBUGASSERT(data);
  3463. DEBUGASSERT(data->multi);
  3464. DEBUGASSERT(!buf || data->multi->xfer_ulbuf == buf);
  3465. data->multi->xfer_ulbuf_borrowed = FALSE;
  3466. }
  3467. CURLcode Curl_multi_xfer_sockbuf_borrow(struct Curl_easy *data,
  3468. size_t blen, char **pbuf)
  3469. {
  3470. DEBUGASSERT(data);
  3471. DEBUGASSERT(data->multi);
  3472. *pbuf = NULL;
  3473. if(!data->multi) {
  3474. failf(data, "transfer has no multi handle");
  3475. return CURLE_FAILED_INIT;
  3476. }
  3477. if(data->multi->xfer_sockbuf_borrowed) {
  3478. failf(data, "attempt to borrow xfer_sockbuf when already borrowed");
  3479. return CURLE_AGAIN;
  3480. }
  3481. if(data->multi->xfer_sockbuf && blen > data->multi->xfer_sockbuf_len) {
  3482. /* not large enough, get a new one */
  3483. curlx_free(data->multi->xfer_sockbuf);
  3484. data->multi->xfer_sockbuf = NULL;
  3485. data->multi->xfer_sockbuf_len = 0;
  3486. }
  3487. if(!data->multi->xfer_sockbuf) {
  3488. data->multi->xfer_sockbuf = curlx_malloc(blen);
  3489. if(!data->multi->xfer_sockbuf) {
  3490. failf(data, "could not allocate xfer_sockbuf of %zu bytes", blen);
  3491. return CURLE_OUT_OF_MEMORY;
  3492. }
  3493. data->multi->xfer_sockbuf_len = blen;
  3494. }
  3495. data->multi->xfer_sockbuf_borrowed = TRUE;
  3496. *pbuf = data->multi->xfer_sockbuf;
  3497. return CURLE_OK;
  3498. }
  3499. void Curl_multi_xfer_sockbuf_release(struct Curl_easy *data, char *buf)
  3500. {
  3501. (void)buf;
  3502. DEBUGASSERT(data);
  3503. DEBUGASSERT(data->multi);
  3504. DEBUGASSERT(!buf || data->multi->xfer_sockbuf == buf);
  3505. data->multi->xfer_sockbuf_borrowed = FALSE;
  3506. }
  3507. static void multi_xfer_bufs_free(struct Curl_multi *multi)
  3508. {
  3509. DEBUGASSERT(multi);
  3510. Curl_safefree(multi->xfer_buf);
  3511. multi->xfer_buf_len = 0;
  3512. multi->xfer_buf_borrowed = FALSE;
  3513. Curl_safefree(multi->xfer_ulbuf);
  3514. multi->xfer_ulbuf_len = 0;
  3515. multi->xfer_ulbuf_borrowed = FALSE;
  3516. Curl_safefree(multi->xfer_sockbuf);
  3517. multi->xfer_sockbuf_len = 0;
  3518. multi->xfer_sockbuf_borrowed = FALSE;
  3519. }
  3520. struct Curl_easy *Curl_multi_get_easy(struct Curl_multi *multi,
  3521. uint32_t mid)
  3522. {
  3523. struct Curl_easy *data = Curl_uint32_tbl_get(&multi->xfers, mid);
  3524. if(data && GOOD_EASY_HANDLE(data))
  3525. return data;
  3526. CURL_TRC_M(multi->admin, "invalid easy handle in xfer table for mid=%u",
  3527. mid);
  3528. Curl_uint32_tbl_remove(&multi->xfers, mid);
  3529. return NULL;
  3530. }
  3531. unsigned int Curl_multi_xfers_running(struct Curl_multi *multi)
  3532. {
  3533. return multi->xfers_alive;
  3534. }
  3535. void Curl_multi_mark_dirty(struct Curl_easy *data)
  3536. {
  3537. if(data->multi && data->mid != UINT32_MAX)
  3538. Curl_uint32_bset_add(&data->multi->dirty, data->mid);
  3539. }
  3540. void Curl_multi_clear_dirty(struct Curl_easy *data)
  3541. {
  3542. if(data->multi && data->mid != UINT32_MAX)
  3543. Curl_uint32_bset_remove(&data->multi->dirty, data->mid);
  3544. }
  3545. CURLMcode curl_multi_notify_enable(CURLM *m, unsigned int notification)
  3546. {
  3547. struct Curl_multi *multi = m;
  3548. if(!GOOD_MULTI_HANDLE(multi))
  3549. return CURLM_BAD_HANDLE;
  3550. return Curl_mntfy_enable(multi, notification);
  3551. }
  3552. CURLMcode curl_multi_notify_disable(CURLM *m, unsigned int notification)
  3553. {
  3554. struct Curl_multi *multi = m;
  3555. if(!GOOD_MULTI_HANDLE(multi))
  3556. return CURLM_BAD_HANDLE;
  3557. return Curl_mntfy_disable(multi, notification);
  3558. }
  3559. #ifdef DEBUGBUILD
  3560. static void multi_xfer_dump(struct Curl_multi *multi, uint32_t mid,
  3561. void *entry)
  3562. {
  3563. struct Curl_easy *data = entry;
  3564. (void)multi;
  3565. if(!data) {
  3566. curl_mfprintf(stderr, "mid=%u, entry=NULL, bug in xfer table?\n", mid);
  3567. }
  3568. else {
  3569. curl_mfprintf(stderr, "mid=%u, magic=%s, p=%p, id=%" FMT_OFF_T
  3570. ", url=%s\n",
  3571. mid,
  3572. (data->magic == CURLEASY_MAGIC_NUMBER) ? "GOOD" : "BAD!",
  3573. (void *)data, data->id, Curl_bufref_ptr(&data->state.url));
  3574. }
  3575. }
  3576. static void multi_xfer_tbl_dump(struct Curl_multi *multi)
  3577. {
  3578. uint32_t mid;
  3579. void *entry;
  3580. curl_mfprintf(stderr, "=== multi xfer table (count=%u, capacity=%u\n",
  3581. Curl_uint32_tbl_count(&multi->xfers),
  3582. Curl_uint32_tbl_capacity(&multi->xfers));
  3583. if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) {
  3584. multi_xfer_dump(multi, mid, entry);
  3585. while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry))
  3586. multi_xfer_dump(multi, mid, entry);
  3587. }
  3588. curl_mfprintf(stderr, "===\n");
  3589. fflush(stderr);
  3590. }
  3591. #endif /* DEBUGBUILD */