multi.c 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "transfer.h"
  28. #include "url.h"
  29. #include "connect.h"
  30. #include "progress.h"
  31. #include "easyif.h"
  32. #include "share.h"
  33. #include "psl.h"
  34. #include "multiif.h"
  35. #include "sendf.h"
  36. #include "timeval.h"
  37. #include "http.h"
  38. #include "select.h"
  39. #include "warnless.h"
  40. #include "speedcheck.h"
  41. #include "conncache.h"
  42. #include "multihandle.h"
  43. #include "sigpipe.h"
  44. #include "vtls/vtls.h"
  45. #include "http_proxy.h"
  46. #include "http2.h"
  47. #include "socketpair.h"
  48. #include "socks.h"
  49. /* The last 3 #include files should be in this order */
  50. #include "curl_printf.h"
  51. #include "curl_memory.h"
  52. #include "memdebug.h"
  53. #ifdef __APPLE__
  54. #define wakeup_write write
  55. #define wakeup_read read
  56. #define wakeup_close close
  57. #define wakeup_create pipe
  58. #else /* __APPLE__ */
  59. #define wakeup_write swrite
  60. #define wakeup_read sread
  61. #define wakeup_close sclose
  62. #define wakeup_create(p) Curl_socketpair(AF_UNIX, SOCK_STREAM, 0, p)
  63. #endif /* __APPLE__ */
  64. /*
  65. CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97
  66. to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every
  67. CURL handle takes 45-50 K memory, therefore this 3K are not significant.
  68. */
  69. #ifndef CURL_SOCKET_HASH_TABLE_SIZE
  70. #define CURL_SOCKET_HASH_TABLE_SIZE 911
  71. #endif
  72. #ifndef CURL_CONNECTION_HASH_SIZE
  73. #define CURL_CONNECTION_HASH_SIZE 97
  74. #endif
  75. #ifndef CURL_DNS_HASH_SIZE
  76. #define CURL_DNS_HASH_SIZE 71
  77. #endif
  78. #define CURL_MULTI_HANDLE 0x000bab1e
  79. #define GOOD_MULTI_HANDLE(x) \
  80. ((x) && (x)->magic == CURL_MULTI_HANDLE)
  81. static CURLMcode singlesocket(struct Curl_multi *multi,
  82. struct Curl_easy *data);
  83. static CURLMcode add_next_timeout(struct curltime now,
  84. struct Curl_multi *multi,
  85. struct Curl_easy *d);
  86. static CURLMcode multi_timeout(struct Curl_multi *multi,
  87. long *timeout_ms);
  88. static void process_pending_handles(struct Curl_multi *multi);
  89. #ifdef DEBUGBUILD
  90. static const char * const statename[]={
  91. "INIT",
  92. "PENDING",
  93. "CONNECT",
  94. "RESOLVING",
  95. "CONNECTING",
  96. "TUNNELING",
  97. "PROTOCONNECT",
  98. "PROTOCONNECTING",
  99. "DO",
  100. "DOING",
  101. "DOING_MORE",
  102. "DID",
  103. "PERFORMING",
  104. "RATELIMITING",
  105. "DONE",
  106. "COMPLETED",
  107. "MSGSENT",
  108. };
  109. #endif
  110. /* function pointer called once when switching TO a state */
  111. typedef void (*init_multistate_func)(struct Curl_easy *data);
  112. /* called in DID state, before PERFORMING state */
  113. static void before_perform(struct Curl_easy *data)
  114. {
  115. data->req.chunk = FALSE;
  116. Curl_pgrsTime(data, TIMER_PRETRANSFER);
  117. }
  118. static void init_completed(struct Curl_easy *data)
  119. {
  120. /* this is a completed transfer */
  121. /* Important: reset the conn pointer so that we don't point to memory
  122. that could be freed anytime */
  123. Curl_detach_connection(data);
  124. Curl_expire_clear(data); /* stop all timers */
  125. }
  126. /* always use this function to change state, to make debugging easier */
  127. static void mstate(struct Curl_easy *data, CURLMstate state
  128. #ifdef DEBUGBUILD
  129. , int lineno
  130. #endif
  131. )
  132. {
  133. CURLMstate oldstate = data->mstate;
  134. static const init_multistate_func finit[MSTATE_LAST] = {
  135. NULL, /* INIT */
  136. NULL, /* PENDING */
  137. Curl_init_CONNECT, /* CONNECT */
  138. NULL, /* RESOLVING */
  139. NULL, /* CONNECTING */
  140. NULL, /* TUNNELING */
  141. NULL, /* PROTOCONNECT */
  142. NULL, /* PROTOCONNECTING */
  143. Curl_connect_free, /* DO */
  144. NULL, /* DOING */
  145. NULL, /* DOING_MORE */
  146. before_perform, /* DID */
  147. NULL, /* PERFORMING */
  148. NULL, /* RATELIMITING */
  149. NULL, /* DONE */
  150. init_completed, /* COMPLETED */
  151. NULL /* MSGSENT */
  152. };
  153. #if defined(DEBUGBUILD) && defined(CURL_DISABLE_VERBOSE_STRINGS)
  154. (void) lineno;
  155. #endif
  156. if(oldstate == state)
  157. /* don't bother when the new state is the same as the old state */
  158. return;
  159. data->mstate = state;
  160. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  161. if(data->mstate >= MSTATE_PENDING &&
  162. data->mstate < MSTATE_COMPLETED) {
  163. long connection_id = -5000;
  164. if(data->conn)
  165. connection_id = data->conn->connection_id;
  166. infof(data,
  167. "STATE: %s => %s handle %p; line %d (connection #%ld)",
  168. statename[oldstate], statename[data->mstate],
  169. (void *)data, lineno, connection_id);
  170. }
  171. #endif
  172. if(state == MSTATE_COMPLETED) {
  173. /* changing to COMPLETED means there's one less easy handle 'alive' */
  174. DEBUGASSERT(data->multi->num_alive > 0);
  175. data->multi->num_alive--;
  176. }
  177. /* if this state has an init-function, run it */
  178. if(finit[state])
  179. finit[state](data);
  180. }
  181. #ifndef DEBUGBUILD
  182. #define multistate(x,y) mstate(x,y)
  183. #else
  184. #define multistate(x,y) mstate(x,y, __LINE__)
  185. #endif
  186. /*
  187. * We add one of these structs to the sockhash for each socket
  188. */
  189. struct Curl_sh_entry {
  190. struct Curl_hash transfers; /* hash of transfers using this socket */
  191. unsigned int action; /* what combined action READ/WRITE this socket waits
  192. for */
  193. unsigned int users; /* number of transfers using this */
  194. void *socketp; /* settable by users with curl_multi_assign() */
  195. unsigned int readers; /* this many transfers want to read */
  196. unsigned int writers; /* this many transfers want to write */
  197. };
  198. /* bits for 'action' having no bits means this socket is not expecting any
  199. action */
  200. #define SH_READ 1
  201. #define SH_WRITE 2
  202. /* look up a given socket in the socket hash, skip invalid sockets */
  203. static struct Curl_sh_entry *sh_getentry(struct Curl_hash *sh,
  204. curl_socket_t s)
  205. {
  206. if(s != CURL_SOCKET_BAD) {
  207. /* only look for proper sockets */
  208. return Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
  209. }
  210. return NULL;
  211. }
  212. #define TRHASH_SIZE 13
  213. static size_t trhash(void *key, size_t key_length, size_t slots_num)
  214. {
  215. size_t keyval = (size_t)*(struct Curl_easy **)key;
  216. (void) key_length;
  217. return (keyval % slots_num);
  218. }
  219. static size_t trhash_compare(void *k1, size_t k1_len, void *k2, size_t k2_len)
  220. {
  221. (void)k1_len;
  222. (void)k2_len;
  223. return *(struct Curl_easy **)k1 == *(struct Curl_easy **)k2;
  224. }
  225. static void trhash_dtor(void *nada)
  226. {
  227. (void)nada;
  228. }
  229. /*
  230. * The sockhash has its own separate subhash in each entry that need to be
  231. * safely destroyed first.
  232. */
  233. static void sockhash_destroy(struct Curl_hash *h)
  234. {
  235. struct Curl_hash_iterator iter;
  236. struct Curl_hash_element *he;
  237. DEBUGASSERT(h);
  238. Curl_hash_start_iterate(h, &iter);
  239. he = Curl_hash_next_element(&iter);
  240. while(he) {
  241. struct Curl_sh_entry *sh = (struct Curl_sh_entry *)he->ptr;
  242. Curl_hash_destroy(&sh->transfers);
  243. he = Curl_hash_next_element(&iter);
  244. }
  245. Curl_hash_destroy(h);
  246. }
  247. /* make sure this socket is present in the hash for this handle */
  248. static struct Curl_sh_entry *sh_addentry(struct Curl_hash *sh,
  249. curl_socket_t s)
  250. {
  251. struct Curl_sh_entry *there = sh_getentry(sh, s);
  252. struct Curl_sh_entry *check;
  253. if(there) {
  254. /* it is present, return fine */
  255. return there;
  256. }
  257. /* not present, add it */
  258. check = calloc(1, sizeof(struct Curl_sh_entry));
  259. if(!check)
  260. return NULL; /* major failure */
  261. Curl_hash_init(&check->transfers, TRHASH_SIZE, trhash, trhash_compare,
  262. trhash_dtor);
  263. /* make/add new hash entry */
  264. if(!Curl_hash_add(sh, (char *)&s, sizeof(curl_socket_t), check)) {
  265. Curl_hash_destroy(&check->transfers);
  266. free(check);
  267. return NULL; /* major failure */
  268. }
  269. return check; /* things are good in sockhash land */
  270. }
  271. /* delete the given socket + handle from the hash */
  272. static void sh_delentry(struct Curl_sh_entry *entry,
  273. struct Curl_hash *sh, curl_socket_t s)
  274. {
  275. Curl_hash_destroy(&entry->transfers);
  276. /* We remove the hash entry. This will end up in a call to
  277. sh_freeentry(). */
  278. Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t));
  279. }
  280. /*
  281. * free a sockhash entry
  282. */
  283. static void sh_freeentry(void *freethis)
  284. {
  285. struct Curl_sh_entry *p = (struct Curl_sh_entry *) freethis;
  286. free(p);
  287. }
  288. static size_t fd_key_compare(void *k1, size_t k1_len, void *k2, size_t k2_len)
  289. {
  290. (void) k1_len; (void) k2_len;
  291. return (*((curl_socket_t *) k1)) == (*((curl_socket_t *) k2));
  292. }
  293. static size_t hash_fd(void *key, size_t key_length, size_t slots_num)
  294. {
  295. curl_socket_t fd = *((curl_socket_t *) key);
  296. (void) key_length;
  297. return (fd % slots_num);
  298. }
  299. /*
  300. * sh_init() creates a new socket hash and returns the handle for it.
  301. *
  302. * Quote from README.multi_socket:
  303. *
  304. * "Some tests at 7000 and 9000 connections showed that the socket hash lookup
  305. * is somewhat of a bottle neck. Its current implementation may be a bit too
  306. * limiting. It simply has a fixed-size array, and on each entry in the array
  307. * it has a linked list with entries. So the hash only checks which list to
  308. * scan through. The code I had used so for used a list with merely 7 slots
  309. * (as that is what the DNS hash uses) but with 7000 connections that would
  310. * make an average of 1000 nodes in each list to run through. I upped that to
  311. * 97 slots (I believe a prime is suitable) and noticed a significant speed
  312. * increase. I need to reconsider the hash implementation or use a rather
  313. * large default value like this. At 9000 connections I was still below 10us
  314. * per call."
  315. *
  316. */
  317. static void sh_init(struct Curl_hash *hash, int hashsize)
  318. {
  319. Curl_hash_init(hash, hashsize, hash_fd, fd_key_compare,
  320. sh_freeentry);
  321. }
  322. /*
  323. * multi_addmsg()
  324. *
  325. * Called when a transfer is completed. Adds the given msg pointer to
  326. * the list kept in the multi handle.
  327. */
  328. static CURLMcode multi_addmsg(struct Curl_multi *multi,
  329. struct Curl_message *msg)
  330. {
  331. Curl_llist_insert_next(&multi->msglist, multi->msglist.tail, msg,
  332. &msg->list);
  333. return CURLM_OK;
  334. }
  335. struct Curl_multi *Curl_multi_handle(int hashsize, /* socket hash */
  336. int chashsize, /* connection hash */
  337. int dnssize) /* dns hash */
  338. {
  339. struct Curl_multi *multi = calloc(1, sizeof(struct Curl_multi));
  340. if(!multi)
  341. return NULL;
  342. multi->magic = CURL_MULTI_HANDLE;
  343. Curl_init_dnscache(&multi->hostcache, dnssize);
  344. sh_init(&multi->sockhash, hashsize);
  345. if(Curl_conncache_init(&multi->conn_cache, chashsize))
  346. goto error;
  347. Curl_llist_init(&multi->msglist, NULL);
  348. Curl_llist_init(&multi->pending, NULL);
  349. multi->multiplexing = TRUE;
  350. /* -1 means it not set by user, use the default value */
  351. multi->maxconnects = -1;
  352. multi->max_concurrent_streams = 100;
  353. #ifdef USE_WINSOCK
  354. multi->wsa_event = WSACreateEvent();
  355. if(multi->wsa_event == WSA_INVALID_EVENT)
  356. goto error;
  357. #else
  358. #ifdef ENABLE_WAKEUP
  359. if(wakeup_create(multi->wakeup_pair) < 0) {
  360. multi->wakeup_pair[0] = CURL_SOCKET_BAD;
  361. multi->wakeup_pair[1] = CURL_SOCKET_BAD;
  362. }
  363. else if(curlx_nonblock(multi->wakeup_pair[0], TRUE) < 0 ||
  364. curlx_nonblock(multi->wakeup_pair[1], TRUE) < 0) {
  365. wakeup_close(multi->wakeup_pair[0]);
  366. wakeup_close(multi->wakeup_pair[1]);
  367. multi->wakeup_pair[0] = CURL_SOCKET_BAD;
  368. multi->wakeup_pair[1] = CURL_SOCKET_BAD;
  369. }
  370. #endif
  371. #endif
  372. return multi;
  373. error:
  374. sockhash_destroy(&multi->sockhash);
  375. Curl_hash_destroy(&multi->hostcache);
  376. Curl_conncache_destroy(&multi->conn_cache);
  377. Curl_llist_destroy(&multi->msglist, NULL);
  378. Curl_llist_destroy(&multi->pending, NULL);
  379. free(multi);
  380. return NULL;
  381. }
  382. struct Curl_multi *curl_multi_init(void)
  383. {
  384. return Curl_multi_handle(CURL_SOCKET_HASH_TABLE_SIZE,
  385. CURL_CONNECTION_HASH_SIZE,
  386. CURL_DNS_HASH_SIZE);
  387. }
  388. CURLMcode curl_multi_add_handle(struct Curl_multi *multi,
  389. struct Curl_easy *data)
  390. {
  391. CURLMcode rc;
  392. /* First, make some basic checks that the CURLM handle is a good handle */
  393. if(!GOOD_MULTI_HANDLE(multi))
  394. return CURLM_BAD_HANDLE;
  395. /* Verify that we got a somewhat good easy handle too */
  396. if(!GOOD_EASY_HANDLE(data))
  397. return CURLM_BAD_EASY_HANDLE;
  398. /* Prevent users from adding same easy handle more than once and prevent
  399. adding to more than one multi stack */
  400. if(data->multi)
  401. return CURLM_ADDED_ALREADY;
  402. if(multi->in_callback)
  403. return CURLM_RECURSIVE_API_CALL;
  404. if(multi->dead) {
  405. /* a "dead" handle cannot get added transfers while any existing easy
  406. handles are still alive - but if there are none alive anymore, it is
  407. fine to start over and unmark the "deadness" of this handle */
  408. if(multi->num_alive)
  409. return CURLM_ABORTED_BY_CALLBACK;
  410. multi->dead = FALSE;
  411. }
  412. /* Initialize timeout list for this handle */
  413. Curl_llist_init(&data->state.timeoutlist, NULL);
  414. /*
  415. * No failure allowed in this function beyond this point. And no
  416. * modification of easy nor multi handle allowed before this except for
  417. * potential multi's connection cache growing which won't be undone in this
  418. * function no matter what.
  419. */
  420. if(data->set.errorbuffer)
  421. data->set.errorbuffer[0] = 0;
  422. /* make the Curl_easy refer back to this multi handle - before Curl_expire()
  423. is called. */
  424. data->multi = multi;
  425. /* Set the timeout for this handle to expire really soon so that it will
  426. be taken care of even when this handle is added in the midst of operation
  427. when only the curl_multi_socket() API is used. During that flow, only
  428. sockets that time-out or have actions will be dealt with. Since this
  429. handle has no action yet, we make sure it times out to get things to
  430. happen. */
  431. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  432. /* A somewhat crude work-around for a little glitch in Curl_update_timer()
  433. that happens if the lastcall time is set to the same time when the handle
  434. is removed as when the next handle is added, as then the check in
  435. Curl_update_timer() that prevents calling the application multiple times
  436. with the same timer info will not trigger and then the new handle's
  437. timeout will not be notified to the app.
  438. The work-around is thus simply to clear the 'lastcall' variable to force
  439. Curl_update_timer() to always trigger a callback to the app when a new
  440. easy handle is added */
  441. memset(&multi->timer_lastcall, 0, sizeof(multi->timer_lastcall));
  442. rc = Curl_update_timer(multi);
  443. if(rc)
  444. return rc;
  445. /* set the easy handle */
  446. multistate(data, MSTATE_INIT);
  447. /* for multi interface connections, we share DNS cache automatically if the
  448. easy handle's one is currently not set. */
  449. if(!data->dns.hostcache ||
  450. (data->dns.hostcachetype == HCACHE_NONE)) {
  451. data->dns.hostcache = &multi->hostcache;
  452. data->dns.hostcachetype = HCACHE_MULTI;
  453. }
  454. /* Point to the shared or multi handle connection cache */
  455. if(data->share && (data->share->specifier & (1<< CURL_LOCK_DATA_CONNECT)))
  456. data->state.conn_cache = &data->share->conn_cache;
  457. else
  458. data->state.conn_cache = &multi->conn_cache;
  459. data->state.lastconnect_id = -1;
  460. #ifdef USE_LIBPSL
  461. /* Do the same for PSL. */
  462. if(data->share && (data->share->specifier & (1 << CURL_LOCK_DATA_PSL)))
  463. data->psl = &data->share->psl;
  464. else
  465. data->psl = &multi->psl;
  466. #endif
  467. /* We add the new entry last in the list. */
  468. data->next = NULL; /* end of the line */
  469. if(multi->easyp) {
  470. struct Curl_easy *last = multi->easylp;
  471. last->next = data;
  472. data->prev = last;
  473. multi->easylp = data; /* the new last node */
  474. }
  475. else {
  476. /* first node, make prev NULL! */
  477. data->prev = NULL;
  478. multi->easylp = multi->easyp = data; /* both first and last */
  479. }
  480. /* increase the node-counter */
  481. multi->num_easy++;
  482. /* increase the alive-counter */
  483. multi->num_alive++;
  484. CONNCACHE_LOCK(data);
  485. /* The closure handle only ever has default timeouts set. To improve the
  486. state somewhat we clone the timeouts from each added handle so that the
  487. closure handle always has the same timeouts as the most recently added
  488. easy handle. */
  489. data->state.conn_cache->closure_handle->set.timeout = data->set.timeout;
  490. data->state.conn_cache->closure_handle->set.server_response_timeout =
  491. data->set.server_response_timeout;
  492. data->state.conn_cache->closure_handle->set.no_signal =
  493. data->set.no_signal;
  494. CONNCACHE_UNLOCK(data);
  495. return CURLM_OK;
  496. }
  497. #if 0
  498. /* Debug-function, used like this:
  499. *
  500. * Curl_hash_print(&multi->sockhash, debug_print_sock_hash);
  501. *
  502. * Enable the hash print function first by editing hash.c
  503. */
  504. static void debug_print_sock_hash(void *p)
  505. {
  506. struct Curl_sh_entry *sh = (struct Curl_sh_entry *)p;
  507. fprintf(stderr, " [readers %u][writers %u]",
  508. sh->readers, sh->writers);
  509. }
  510. #endif
  511. static CURLcode multi_done(struct Curl_easy *data,
  512. CURLcode status, /* an error if this is called
  513. after an error was detected */
  514. bool premature)
  515. {
  516. CURLcode result;
  517. struct connectdata *conn = data->conn;
  518. unsigned int i;
  519. DEBUGF(infof(data, "multi_done: status: %d prem: %d done: %d",
  520. (int)status, (int)premature, data->state.done));
  521. if(data->state.done)
  522. /* Stop if multi_done() has already been called */
  523. return CURLE_OK;
  524. /* Stop the resolver and free its own resources (but not dns_entry yet). */
  525. Curl_resolver_kill(data);
  526. /* Cleanup possible redirect junk */
  527. Curl_safefree(data->req.newurl);
  528. Curl_safefree(data->req.location);
  529. switch(status) {
  530. case CURLE_ABORTED_BY_CALLBACK:
  531. case CURLE_READ_ERROR:
  532. case CURLE_WRITE_ERROR:
  533. /* When we're aborted due to a callback return code it basically have to
  534. be counted as premature as there is trouble ahead if we don't. We have
  535. many callbacks and protocols work differently, we could potentially do
  536. this more fine-grained in the future. */
  537. premature = TRUE;
  538. default:
  539. break;
  540. }
  541. /* this calls the protocol-specific function pointer previously set */
  542. if(conn->handler->done)
  543. result = conn->handler->done(data, status, premature);
  544. else
  545. result = status;
  546. if(CURLE_ABORTED_BY_CALLBACK != result) {
  547. /* avoid this if we already aborted by callback to avoid this calling
  548. another callback */
  549. int rc = Curl_pgrsDone(data);
  550. if(!result && rc)
  551. result = CURLE_ABORTED_BY_CALLBACK;
  552. }
  553. process_pending_handles(data->multi); /* connection / multiplex */
  554. CONNCACHE_LOCK(data);
  555. Curl_detach_connection(data);
  556. if(CONN_INUSE(conn)) {
  557. /* Stop if still used. */
  558. CONNCACHE_UNLOCK(data);
  559. DEBUGF(infof(data, "Connection still in use %zu, "
  560. "no more multi_done now!",
  561. conn->easyq.size));
  562. return CURLE_OK;
  563. }
  564. data->state.done = TRUE; /* called just now! */
  565. if(conn->dns_entry) {
  566. Curl_resolv_unlock(data, conn->dns_entry); /* done with this */
  567. conn->dns_entry = NULL;
  568. }
  569. Curl_hostcache_prune(data);
  570. Curl_safefree(data->state.ulbuf);
  571. /* if the transfer was completed in a paused state there can be buffered
  572. data left to free */
  573. for(i = 0; i < data->state.tempcount; i++) {
  574. Curl_dyn_free(&data->state.tempwrite[i].b);
  575. }
  576. data->state.tempcount = 0;
  577. /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
  578. forced us to close this connection. This is ignored for requests taking
  579. place in a NTLM/NEGOTIATE authentication handshake
  580. if conn->bits.close is TRUE, it means that the connection should be
  581. closed in spite of all our efforts to be nice, due to protocol
  582. restrictions in our or the server's end
  583. if premature is TRUE, it means this connection was said to be DONE before
  584. the entire request operation is complete and thus we can't know in what
  585. state it is for re-using, so we're forced to close it. In a perfect world
  586. we can add code that keep track of if we really must close it here or not,
  587. but currently we have no such detail knowledge.
  588. */
  589. if((data->set.reuse_forbid
  590. #if defined(USE_NTLM)
  591. && !(conn->http_ntlm_state == NTLMSTATE_TYPE2 ||
  592. conn->proxy_ntlm_state == NTLMSTATE_TYPE2)
  593. #endif
  594. #if defined(USE_SPNEGO)
  595. && !(conn->http_negotiate_state == GSS_AUTHRECV ||
  596. conn->proxy_negotiate_state == GSS_AUTHRECV)
  597. #endif
  598. ) || conn->bits.close
  599. || (premature && !(conn->handler->flags & PROTOPT_STREAM))) {
  600. connclose(conn, "disconnecting");
  601. Curl_conncache_remove_conn(data, conn, FALSE);
  602. CONNCACHE_UNLOCK(data);
  603. Curl_disconnect(data, conn, premature);
  604. }
  605. else {
  606. char buffer[256];
  607. const char *host =
  608. #ifndef CURL_DISABLE_PROXY
  609. conn->bits.socksproxy ?
  610. conn->socks_proxy.host.dispname :
  611. conn->bits.httpproxy ? conn->http_proxy.host.dispname :
  612. #endif
  613. conn->bits.conn_to_host ? conn->conn_to_host.dispname :
  614. conn->host.dispname;
  615. /* create string before returning the connection */
  616. long connection_id = conn->connection_id;
  617. msnprintf(buffer, sizeof(buffer),
  618. "Connection #%ld to host %s left intact",
  619. connection_id, host);
  620. /* the connection is no longer in use by this transfer */
  621. CONNCACHE_UNLOCK(data);
  622. if(Curl_conncache_return_conn(data, conn)) {
  623. /* remember the most recently used connection */
  624. data->state.lastconnect_id = connection_id;
  625. infof(data, "%s", buffer);
  626. }
  627. else
  628. data->state.lastconnect_id = -1;
  629. }
  630. Curl_safefree(data->state.buffer);
  631. return result;
  632. }
  633. static int close_connect_only(struct Curl_easy *data,
  634. struct connectdata *conn, void *param)
  635. {
  636. (void)param;
  637. if(data->state.lastconnect_id != conn->connection_id)
  638. return 0;
  639. if(!conn->connect_only)
  640. return 1;
  641. connclose(conn, "Removing connect-only easy handle");
  642. return 1;
  643. }
  644. CURLMcode curl_multi_remove_handle(struct Curl_multi *multi,
  645. struct Curl_easy *data)
  646. {
  647. struct Curl_easy *easy = data;
  648. bool premature;
  649. struct Curl_llist_element *e;
  650. CURLMcode rc;
  651. /* First, make some basic checks that the CURLM handle is a good handle */
  652. if(!GOOD_MULTI_HANDLE(multi))
  653. return CURLM_BAD_HANDLE;
  654. /* Verify that we got a somewhat good easy handle too */
  655. if(!GOOD_EASY_HANDLE(data))
  656. return CURLM_BAD_EASY_HANDLE;
  657. /* Prevent users from trying to remove same easy handle more than once */
  658. if(!data->multi)
  659. return CURLM_OK; /* it is already removed so let's say it is fine! */
  660. /* Prevent users from trying to remove an easy handle from the wrong multi */
  661. if(data->multi != multi)
  662. return CURLM_BAD_EASY_HANDLE;
  663. if(multi->in_callback)
  664. return CURLM_RECURSIVE_API_CALL;
  665. premature = (data->mstate < MSTATE_COMPLETED) ? TRUE : FALSE;
  666. /* If the 'state' is not INIT or COMPLETED, we might need to do something
  667. nice to put the easy_handle in a good known state when this returns. */
  668. if(premature) {
  669. /* this handle is "alive" so we need to count down the total number of
  670. alive connections when this is removed */
  671. multi->num_alive--;
  672. }
  673. if(data->conn &&
  674. data->mstate > MSTATE_DO &&
  675. data->mstate < MSTATE_COMPLETED) {
  676. /* Set connection owner so that the DONE function closes it. We can
  677. safely do this here since connection is killed. */
  678. streamclose(data->conn, "Removed with partial response");
  679. }
  680. if(data->conn) {
  681. /* multi_done() clears the association between the easy handle and the
  682. connection.
  683. Note that this ignores the return code simply because there's
  684. nothing really useful to do with it anyway! */
  685. (void)multi_done(data, data->result, premature);
  686. }
  687. /* The timer must be shut down before data->multi is set to NULL, else the
  688. timenode will remain in the splay tree after curl_easy_cleanup is
  689. called. Do it after multi_done() in case that sets another time! */
  690. Curl_expire_clear(data);
  691. if(data->connect_queue.ptr)
  692. /* the handle was in the pending list waiting for an available connection,
  693. so go ahead and remove it */
  694. Curl_llist_remove(&multi->pending, &data->connect_queue, NULL);
  695. if(data->dns.hostcachetype == HCACHE_MULTI) {
  696. /* stop using the multi handle's DNS cache, *after* the possible
  697. multi_done() call above */
  698. data->dns.hostcache = NULL;
  699. data->dns.hostcachetype = HCACHE_NONE;
  700. }
  701. Curl_wildcard_dtor(&data->wildcard);
  702. /* destroy the timeout list that is held in the easy handle, do this *after*
  703. multi_done() as that may actually call Curl_expire that uses this */
  704. Curl_llist_destroy(&data->state.timeoutlist, NULL);
  705. /* change state without using multistate(), only to make singlesocket() do
  706. what we want */
  707. data->mstate = MSTATE_COMPLETED;
  708. /* This ignores the return code even in case of problems because there's
  709. nothing more to do about that, here */
  710. (void)singlesocket(multi, easy); /* to let the application know what sockets
  711. that vanish with this handle */
  712. /* Remove the association between the connection and the handle */
  713. Curl_detach_connection(data);
  714. if(data->set.connect_only && !data->multi_easy) {
  715. /* This removes a handle that was part the multi interface that used
  716. CONNECT_ONLY, that connection is now left alive but since this handle
  717. has bits.close set nothing can use that transfer anymore and it is
  718. forbidden from reuse. And this easy handle cannot find the connection
  719. anymore once removed from the multi handle
  720. Better close the connection here, at once.
  721. */
  722. struct connectdata *c;
  723. curl_socket_t s;
  724. s = Curl_getconnectinfo(data, &c);
  725. if((s != CURL_SOCKET_BAD) && c) {
  726. Curl_conncache_remove_conn(data, c, TRUE);
  727. Curl_disconnect(data, c, TRUE);
  728. }
  729. }
  730. if(data->state.lastconnect_id != -1) {
  731. /* Mark any connect-only connection for closure */
  732. Curl_conncache_foreach(data, data->state.conn_cache,
  733. NULL, close_connect_only);
  734. }
  735. #ifdef USE_LIBPSL
  736. /* Remove the PSL association. */
  737. if(data->psl == &multi->psl)
  738. data->psl = NULL;
  739. #endif
  740. /* as this was using a shared connection cache we clear the pointer to that
  741. since we're not part of that multi handle anymore */
  742. data->state.conn_cache = NULL;
  743. data->multi = NULL; /* clear the association to this multi handle */
  744. /* make sure there's no pending message in the queue sent from this easy
  745. handle */
  746. for(e = multi->msglist.head; e; e = e->next) {
  747. struct Curl_message *msg = e->ptr;
  748. if(msg->extmsg.easy_handle == easy) {
  749. Curl_llist_remove(&multi->msglist, e, NULL);
  750. /* there can only be one from this specific handle */
  751. break;
  752. }
  753. }
  754. /* Remove from the pending list if it is there. Otherwise this will
  755. remain on the pending list forever due to the state change. */
  756. for(e = multi->pending.head; e; e = e->next) {
  757. struct Curl_easy *curr_data = e->ptr;
  758. if(curr_data == data) {
  759. Curl_llist_remove(&multi->pending, e, NULL);
  760. break;
  761. }
  762. }
  763. /* make the previous node point to our next */
  764. if(data->prev)
  765. data->prev->next = data->next;
  766. else
  767. multi->easyp = data->next; /* point to first node */
  768. /* make our next point to our previous node */
  769. if(data->next)
  770. data->next->prev = data->prev;
  771. else
  772. multi->easylp = data->prev; /* point to last node */
  773. /* NOTE NOTE NOTE
  774. We do not touch the easy handle here! */
  775. multi->num_easy--; /* one less to care about now */
  776. process_pending_handles(multi);
  777. rc = Curl_update_timer(multi);
  778. if(rc)
  779. return rc;
  780. return CURLM_OK;
  781. }
  782. /* Return TRUE if the application asked for multiplexing */
  783. bool Curl_multiplex_wanted(const struct Curl_multi *multi)
  784. {
  785. return (multi && (multi->multiplexing));
  786. }
  787. /*
  788. * Curl_detach_connection() removes the given transfer from the connection.
  789. *
  790. * This is the only function that should clear data->conn. This will
  791. * occasionally be called with the data->conn pointer already cleared.
  792. */
  793. void Curl_detach_connection(struct Curl_easy *data)
  794. {
  795. struct connectdata *conn = data->conn;
  796. if(conn) {
  797. Curl_connect_done(data); /* if mid-CONNECT, shut it down */
  798. Curl_llist_remove(&conn->easyq, &data->conn_queue, NULL);
  799. Curl_ssl_detach_conn(data, conn);
  800. }
  801. data->conn = NULL;
  802. }
  803. /*
  804. * Curl_attach_connection() attaches this transfer to this connection.
  805. *
  806. * This is the only function that should assign data->conn
  807. */
  808. void Curl_attach_connection(struct Curl_easy *data,
  809. struct connectdata *conn)
  810. {
  811. DEBUGASSERT(!data->conn);
  812. DEBUGASSERT(conn);
  813. data->conn = conn;
  814. Curl_llist_insert_next(&conn->easyq, conn->easyq.tail, data,
  815. &data->conn_queue);
  816. if(conn->handler->attach)
  817. conn->handler->attach(data, conn);
  818. Curl_ssl_associate_conn(data, conn);
  819. }
  820. static int waitconnect_getsock(struct connectdata *conn,
  821. curl_socket_t *sock)
  822. {
  823. int i;
  824. int s = 0;
  825. int rc = 0;
  826. #ifdef USE_SSL
  827. #ifndef CURL_DISABLE_PROXY
  828. if(CONNECT_FIRSTSOCKET_PROXY_SSL())
  829. return Curl_ssl->getsock(conn, sock);
  830. #endif
  831. #endif
  832. if(SOCKS_STATE(conn->cnnct.state))
  833. return Curl_SOCKS_getsock(conn, sock, FIRSTSOCKET);
  834. for(i = 0; i<2; i++) {
  835. if(conn->tempsock[i] != CURL_SOCKET_BAD) {
  836. sock[s] = conn->tempsock[i];
  837. rc |= GETSOCK_WRITESOCK(s);
  838. #ifdef ENABLE_QUIC
  839. if(conn->transport == TRNSPRT_QUIC)
  840. /* when connecting QUIC, we want to read the socket too */
  841. rc |= GETSOCK_READSOCK(s);
  842. #endif
  843. s++;
  844. }
  845. }
  846. return rc;
  847. }
  848. static int waitproxyconnect_getsock(struct connectdata *conn,
  849. curl_socket_t *sock)
  850. {
  851. sock[0] = conn->sock[FIRSTSOCKET];
  852. if(conn->connect_state)
  853. return Curl_connect_getsock(conn);
  854. return GETSOCK_WRITESOCK(0);
  855. }
  856. static int domore_getsock(struct Curl_easy *data,
  857. struct connectdata *conn,
  858. curl_socket_t *socks)
  859. {
  860. if(conn && conn->handler->domore_getsock)
  861. return conn->handler->domore_getsock(data, conn, socks);
  862. return GETSOCK_BLANK;
  863. }
  864. static int doing_getsock(struct Curl_easy *data,
  865. struct connectdata *conn,
  866. curl_socket_t *socks)
  867. {
  868. if(conn && conn->handler->doing_getsock)
  869. return conn->handler->doing_getsock(data, conn, socks);
  870. return GETSOCK_BLANK;
  871. }
  872. static int protocol_getsock(struct Curl_easy *data,
  873. struct connectdata *conn,
  874. curl_socket_t *socks)
  875. {
  876. if(conn->handler->proto_getsock)
  877. return conn->handler->proto_getsock(data, conn, socks);
  878. /* Backup getsock logic. Since there is a live socket in use, we must wait
  879. for it or it will be removed from watching when the multi_socket API is
  880. used. */
  881. socks[0] = conn->sock[FIRSTSOCKET];
  882. return GETSOCK_READSOCK(0) | GETSOCK_WRITESOCK(0);
  883. }
  884. /* returns bitmapped flags for this handle and its sockets. The 'socks[]'
  885. array contains MAX_SOCKSPEREASYHANDLE entries. */
  886. static int multi_getsock(struct Curl_easy *data,
  887. curl_socket_t *socks)
  888. {
  889. struct connectdata *conn = data->conn;
  890. /* The no connection case can happen when this is called from
  891. curl_multi_remove_handle() => singlesocket() => multi_getsock().
  892. */
  893. if(!conn)
  894. return 0;
  895. switch(data->mstate) {
  896. default:
  897. return 0;
  898. case MSTATE_RESOLVING:
  899. return Curl_resolv_getsock(data, socks);
  900. case MSTATE_PROTOCONNECTING:
  901. case MSTATE_PROTOCONNECT:
  902. return protocol_getsock(data, conn, socks);
  903. case MSTATE_DO:
  904. case MSTATE_DOING:
  905. return doing_getsock(data, conn, socks);
  906. case MSTATE_TUNNELING:
  907. return waitproxyconnect_getsock(conn, socks);
  908. case MSTATE_CONNECTING:
  909. return waitconnect_getsock(conn, socks);
  910. case MSTATE_DOING_MORE:
  911. return domore_getsock(data, conn, socks);
  912. case MSTATE_DID: /* since is set after DO is completed, we switch to
  913. waiting for the same as the PERFORMING state */
  914. case MSTATE_PERFORMING:
  915. return Curl_single_getsock(data, conn, socks);
  916. }
  917. }
  918. CURLMcode curl_multi_fdset(struct Curl_multi *multi,
  919. fd_set *read_fd_set, fd_set *write_fd_set,
  920. fd_set *exc_fd_set, int *max_fd)
  921. {
  922. /* Scan through all the easy handles to get the file descriptors set.
  923. Some easy handles may not have connected to the remote host yet,
  924. and then we must make sure that is done. */
  925. struct Curl_easy *data;
  926. int this_max_fd = -1;
  927. curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
  928. int i;
  929. (void)exc_fd_set; /* not used */
  930. if(!GOOD_MULTI_HANDLE(multi))
  931. return CURLM_BAD_HANDLE;
  932. if(multi->in_callback)
  933. return CURLM_RECURSIVE_API_CALL;
  934. data = multi->easyp;
  935. while(data) {
  936. int bitmap;
  937. #ifdef __clang_analyzer_
  938. /* to prevent "The left operand of '>=' is a garbage value" warnings */
  939. memset(sockbunch, 0, sizeof(sockbunch));
  940. #endif
  941. bitmap = multi_getsock(data, sockbunch);
  942. for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++) {
  943. curl_socket_t s = CURL_SOCKET_BAD;
  944. if((bitmap & GETSOCK_READSOCK(i)) && VALID_SOCK(sockbunch[i])) {
  945. if(!FDSET_SOCK(sockbunch[i]))
  946. /* pretend it doesn't exist */
  947. continue;
  948. FD_SET(sockbunch[i], read_fd_set);
  949. s = sockbunch[i];
  950. }
  951. if((bitmap & GETSOCK_WRITESOCK(i)) && VALID_SOCK(sockbunch[i])) {
  952. if(!FDSET_SOCK(sockbunch[i]))
  953. /* pretend it doesn't exist */
  954. continue;
  955. FD_SET(sockbunch[i], write_fd_set);
  956. s = sockbunch[i];
  957. }
  958. if(s == CURL_SOCKET_BAD)
  959. /* this socket is unused, break out of loop */
  960. break;
  961. if((int)s > this_max_fd)
  962. this_max_fd = (int)s;
  963. }
  964. data = data->next; /* check next handle */
  965. }
  966. *max_fd = this_max_fd;
  967. return CURLM_OK;
  968. }
  969. #define NUM_POLLS_ON_STACK 10
  970. static CURLMcode multi_wait(struct Curl_multi *multi,
  971. struct curl_waitfd extra_fds[],
  972. unsigned int extra_nfds,
  973. int timeout_ms,
  974. int *ret,
  975. bool extrawait, /* when no socket, wait */
  976. bool use_wakeup)
  977. {
  978. struct Curl_easy *data;
  979. curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
  980. int bitmap;
  981. unsigned int i;
  982. unsigned int nfds = 0;
  983. unsigned int curlfds;
  984. long timeout_internal;
  985. int retcode = 0;
  986. struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK];
  987. struct pollfd *ufds = &a_few_on_stack[0];
  988. bool ufds_malloc = FALSE;
  989. #ifdef USE_WINSOCK
  990. WSANETWORKEVENTS wsa_events;
  991. DEBUGASSERT(multi->wsa_event != WSA_INVALID_EVENT);
  992. #endif
  993. #ifndef ENABLE_WAKEUP
  994. (void)use_wakeup;
  995. #endif
  996. if(!GOOD_MULTI_HANDLE(multi))
  997. return CURLM_BAD_HANDLE;
  998. if(multi->in_callback)
  999. return CURLM_RECURSIVE_API_CALL;
  1000. if(timeout_ms < 0)
  1001. return CURLM_BAD_FUNCTION_ARGUMENT;
  1002. /* Count up how many fds we have from the multi handle */
  1003. data = multi->easyp;
  1004. while(data) {
  1005. bitmap = multi_getsock(data, sockbunch);
  1006. for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++) {
  1007. curl_socket_t s = CURL_SOCKET_BAD;
  1008. if((bitmap & GETSOCK_READSOCK(i)) && VALID_SOCK((sockbunch[i]))) {
  1009. ++nfds;
  1010. s = sockbunch[i];
  1011. }
  1012. if((bitmap & GETSOCK_WRITESOCK(i)) && VALID_SOCK((sockbunch[i]))) {
  1013. ++nfds;
  1014. s = sockbunch[i];
  1015. }
  1016. if(s == CURL_SOCKET_BAD) {
  1017. break;
  1018. }
  1019. }
  1020. data = data->next; /* check next handle */
  1021. }
  1022. /* If the internally desired timeout is actually shorter than requested from
  1023. the outside, then use the shorter time! But only if the internal timer
  1024. is actually larger than -1! */
  1025. (void)multi_timeout(multi, &timeout_internal);
  1026. if((timeout_internal >= 0) && (timeout_internal < (long)timeout_ms))
  1027. timeout_ms = (int)timeout_internal;
  1028. curlfds = nfds; /* number of internal file descriptors */
  1029. nfds += extra_nfds; /* add the externally provided ones */
  1030. #ifdef ENABLE_WAKEUP
  1031. #ifdef USE_WINSOCK
  1032. if(use_wakeup) {
  1033. #else
  1034. if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) {
  1035. #endif
  1036. ++nfds;
  1037. }
  1038. #endif
  1039. if(nfds > NUM_POLLS_ON_STACK) {
  1040. /* 'nfds' is a 32 bit value and 'struct pollfd' is typically 8 bytes
  1041. big, so at 2^29 sockets this value might wrap. When a process gets
  1042. the capability to actually handle over 500 million sockets this
  1043. calculation needs a integer overflow check. */
  1044. ufds = malloc(nfds * sizeof(struct pollfd));
  1045. if(!ufds)
  1046. return CURLM_OUT_OF_MEMORY;
  1047. ufds_malloc = TRUE;
  1048. }
  1049. nfds = 0;
  1050. /* only do the second loop if we found descriptors in the first stage run
  1051. above */
  1052. if(curlfds) {
  1053. /* Add the curl handles to our pollfds first */
  1054. data = multi->easyp;
  1055. while(data) {
  1056. bitmap = multi_getsock(data, sockbunch);
  1057. for(i = 0; i < MAX_SOCKSPEREASYHANDLE; i++) {
  1058. curl_socket_t s = CURL_SOCKET_BAD;
  1059. #ifdef USE_WINSOCK
  1060. long mask = 0;
  1061. #endif
  1062. if((bitmap & GETSOCK_READSOCK(i)) && VALID_SOCK((sockbunch[i]))) {
  1063. s = sockbunch[i];
  1064. #ifdef USE_WINSOCK
  1065. mask |= FD_READ|FD_ACCEPT|FD_CLOSE;
  1066. #endif
  1067. ufds[nfds].fd = s;
  1068. ufds[nfds].events = POLLIN;
  1069. ++nfds;
  1070. }
  1071. if((bitmap & GETSOCK_WRITESOCK(i)) && VALID_SOCK((sockbunch[i]))) {
  1072. s = sockbunch[i];
  1073. #ifdef USE_WINSOCK
  1074. mask |= FD_WRITE|FD_CONNECT|FD_CLOSE;
  1075. send(s, NULL, 0, 0); /* reset FD_WRITE */
  1076. #endif
  1077. ufds[nfds].fd = s;
  1078. ufds[nfds].events = POLLOUT;
  1079. ++nfds;
  1080. }
  1081. /* s is only set if either being readable or writable is checked */
  1082. if(s == CURL_SOCKET_BAD) {
  1083. /* break on entry not checked for being readable or writable */
  1084. break;
  1085. }
  1086. #ifdef USE_WINSOCK
  1087. if(WSAEventSelect(s, multi->wsa_event, mask) != 0) {
  1088. if(ufds_malloc)
  1089. free(ufds);
  1090. return CURLM_INTERNAL_ERROR;
  1091. }
  1092. #endif
  1093. }
  1094. data = data->next; /* check next handle */
  1095. }
  1096. }
  1097. /* Add external file descriptions from poll-like struct curl_waitfd */
  1098. for(i = 0; i < extra_nfds; i++) {
  1099. #ifdef USE_WINSOCK
  1100. long mask = 0;
  1101. if(extra_fds[i].events & CURL_WAIT_POLLIN)
  1102. mask |= FD_READ|FD_ACCEPT|FD_CLOSE;
  1103. if(extra_fds[i].events & CURL_WAIT_POLLPRI)
  1104. mask |= FD_OOB;
  1105. if(extra_fds[i].events & CURL_WAIT_POLLOUT) {
  1106. mask |= FD_WRITE|FD_CONNECT|FD_CLOSE;
  1107. send(extra_fds[i].fd, NULL, 0, 0); /* reset FD_WRITE */
  1108. }
  1109. if(WSAEventSelect(extra_fds[i].fd, multi->wsa_event, mask) != 0) {
  1110. if(ufds_malloc)
  1111. free(ufds);
  1112. return CURLM_INTERNAL_ERROR;
  1113. }
  1114. #endif
  1115. ufds[nfds].fd = extra_fds[i].fd;
  1116. ufds[nfds].events = 0;
  1117. if(extra_fds[i].events & CURL_WAIT_POLLIN)
  1118. ufds[nfds].events |= POLLIN;
  1119. if(extra_fds[i].events & CURL_WAIT_POLLPRI)
  1120. ufds[nfds].events |= POLLPRI;
  1121. if(extra_fds[i].events & CURL_WAIT_POLLOUT)
  1122. ufds[nfds].events |= POLLOUT;
  1123. ++nfds;
  1124. }
  1125. #ifdef ENABLE_WAKEUP
  1126. #ifndef USE_WINSOCK
  1127. if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) {
  1128. ufds[nfds].fd = multi->wakeup_pair[0];
  1129. ufds[nfds].events = POLLIN;
  1130. ++nfds;
  1131. }
  1132. #endif
  1133. #endif
  1134. #if defined(ENABLE_WAKEUP) && defined(USE_WINSOCK)
  1135. if(nfds || use_wakeup) {
  1136. #else
  1137. if(nfds) {
  1138. #endif
  1139. int pollrc;
  1140. #ifdef USE_WINSOCK
  1141. if(nfds)
  1142. pollrc = Curl_poll(ufds, nfds, 0); /* just pre-check with WinSock */
  1143. else
  1144. pollrc = 0;
  1145. #else
  1146. pollrc = Curl_poll(ufds, nfds, timeout_ms); /* wait... */
  1147. #endif
  1148. if(pollrc < 0)
  1149. return CURLM_UNRECOVERABLE_POLL;
  1150. if(pollrc > 0) {
  1151. retcode = pollrc;
  1152. #ifdef USE_WINSOCK
  1153. }
  1154. else { /* now wait... if not ready during the pre-check (pollrc == 0) */
  1155. WSAWaitForMultipleEvents(1, &multi->wsa_event, FALSE, timeout_ms, FALSE);
  1156. }
  1157. /* With WinSock, we have to run the following section unconditionally
  1158. to call WSAEventSelect(fd, event, 0) on all the sockets */
  1159. {
  1160. #endif
  1161. /* copy revents results from the poll to the curl_multi_wait poll
  1162. struct, the bit values of the actual underlying poll() implementation
  1163. may not be the same as the ones in the public libcurl API! */
  1164. for(i = 0; i < extra_nfds; i++) {
  1165. unsigned r = ufds[curlfds + i].revents;
  1166. unsigned short mask = 0;
  1167. #ifdef USE_WINSOCK
  1168. curl_socket_t s = extra_fds[i].fd;
  1169. wsa_events.lNetworkEvents = 0;
  1170. if(WSAEnumNetworkEvents(s, NULL, &wsa_events) == 0) {
  1171. if(wsa_events.lNetworkEvents & (FD_READ|FD_ACCEPT|FD_CLOSE))
  1172. mask |= CURL_WAIT_POLLIN;
  1173. if(wsa_events.lNetworkEvents & (FD_WRITE|FD_CONNECT|FD_CLOSE))
  1174. mask |= CURL_WAIT_POLLOUT;
  1175. if(wsa_events.lNetworkEvents & FD_OOB)
  1176. mask |= CURL_WAIT_POLLPRI;
  1177. if(ret && !pollrc && wsa_events.lNetworkEvents)
  1178. retcode++;
  1179. }
  1180. WSAEventSelect(s, multi->wsa_event, 0);
  1181. if(!pollrc) {
  1182. extra_fds[i].revents = mask;
  1183. continue;
  1184. }
  1185. #endif
  1186. if(r & POLLIN)
  1187. mask |= CURL_WAIT_POLLIN;
  1188. if(r & POLLOUT)
  1189. mask |= CURL_WAIT_POLLOUT;
  1190. if(r & POLLPRI)
  1191. mask |= CURL_WAIT_POLLPRI;
  1192. extra_fds[i].revents = mask;
  1193. }
  1194. #ifdef USE_WINSOCK
  1195. /* Count up all our own sockets that had activity,
  1196. and remove them from the event. */
  1197. if(curlfds) {
  1198. data = multi->easyp;
  1199. while(data) {
  1200. bitmap = multi_getsock(data, sockbunch);
  1201. for(i = 0; i < MAX_SOCKSPEREASYHANDLE; i++) {
  1202. if(bitmap & (GETSOCK_READSOCK(i) | GETSOCK_WRITESOCK(i))) {
  1203. wsa_events.lNetworkEvents = 0;
  1204. if(WSAEnumNetworkEvents(sockbunch[i], NULL, &wsa_events) == 0) {
  1205. if(ret && !pollrc && wsa_events.lNetworkEvents)
  1206. retcode++;
  1207. }
  1208. WSAEventSelect(sockbunch[i], multi->wsa_event, 0);
  1209. }
  1210. else {
  1211. /* break on entry not checked for being readable or writable */
  1212. break;
  1213. }
  1214. }
  1215. data = data->next;
  1216. }
  1217. }
  1218. WSAResetEvent(multi->wsa_event);
  1219. #else
  1220. #ifdef ENABLE_WAKEUP
  1221. if(use_wakeup && multi->wakeup_pair[0] != CURL_SOCKET_BAD) {
  1222. if(ufds[curlfds + extra_nfds].revents & POLLIN) {
  1223. char buf[64];
  1224. ssize_t nread;
  1225. while(1) {
  1226. /* the reading socket is non-blocking, try to read
  1227. data from it until it receives an error (except EINTR).
  1228. In normal cases it will get EAGAIN or EWOULDBLOCK
  1229. when there is no more data, breaking the loop. */
  1230. nread = wakeup_read(multi->wakeup_pair[0], buf, sizeof(buf));
  1231. if(nread <= 0) {
  1232. if(nread < 0 && EINTR == SOCKERRNO)
  1233. continue;
  1234. break;
  1235. }
  1236. }
  1237. /* do not count the wakeup socket into the returned value */
  1238. retcode--;
  1239. }
  1240. }
  1241. #endif
  1242. #endif
  1243. }
  1244. }
  1245. if(ufds_malloc)
  1246. free(ufds);
  1247. if(ret)
  1248. *ret = retcode;
  1249. #if defined(ENABLE_WAKEUP) && defined(USE_WINSOCK)
  1250. if(extrawait && !nfds && !use_wakeup) {
  1251. #else
  1252. if(extrawait && !nfds) {
  1253. #endif
  1254. long sleep_ms = 0;
  1255. /* Avoid busy-looping when there's nothing particular to wait for */
  1256. if(!curl_multi_timeout(multi, &sleep_ms) && sleep_ms) {
  1257. if(sleep_ms > timeout_ms)
  1258. sleep_ms = timeout_ms;
  1259. /* when there are no easy handles in the multi, this holds a -1
  1260. timeout */
  1261. else if(sleep_ms < 0)
  1262. sleep_ms = timeout_ms;
  1263. Curl_wait_ms(sleep_ms);
  1264. }
  1265. }
  1266. return CURLM_OK;
  1267. }
  1268. CURLMcode curl_multi_wait(struct Curl_multi *multi,
  1269. struct curl_waitfd extra_fds[],
  1270. unsigned int extra_nfds,
  1271. int timeout_ms,
  1272. int *ret)
  1273. {
  1274. return multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, FALSE,
  1275. FALSE);
  1276. }
  1277. CURLMcode curl_multi_poll(struct Curl_multi *multi,
  1278. struct curl_waitfd extra_fds[],
  1279. unsigned int extra_nfds,
  1280. int timeout_ms,
  1281. int *ret)
  1282. {
  1283. return multi_wait(multi, extra_fds, extra_nfds, timeout_ms, ret, TRUE,
  1284. TRUE);
  1285. }
  1286. CURLMcode curl_multi_wakeup(struct Curl_multi *multi)
  1287. {
  1288. /* this function is usually called from another thread,
  1289. it has to be careful only to access parts of the
  1290. Curl_multi struct that are constant */
  1291. /* GOOD_MULTI_HANDLE can be safely called */
  1292. if(!GOOD_MULTI_HANDLE(multi))
  1293. return CURLM_BAD_HANDLE;
  1294. #ifdef ENABLE_WAKEUP
  1295. #ifdef USE_WINSOCK
  1296. if(WSASetEvent(multi->wsa_event))
  1297. return CURLM_OK;
  1298. #else
  1299. /* the wakeup_pair variable is only written during init and cleanup,
  1300. making it safe to access from another thread after the init part
  1301. and before cleanup */
  1302. if(multi->wakeup_pair[1] != CURL_SOCKET_BAD) {
  1303. char buf[1];
  1304. buf[0] = 1;
  1305. while(1) {
  1306. /* swrite() is not thread-safe in general, because concurrent calls
  1307. can have their messages interleaved, but in this case the content
  1308. of the messages does not matter, which makes it ok to call.
  1309. The write socket is set to non-blocking, this way this function
  1310. cannot block, making it safe to call even from the same thread
  1311. that will call curl_multi_wait(). If swrite() returns that it
  1312. would block, it's considered successful because it means that
  1313. previous calls to this function will wake up the poll(). */
  1314. if(wakeup_write(multi->wakeup_pair[1], buf, sizeof(buf)) < 0) {
  1315. int err = SOCKERRNO;
  1316. int return_success;
  1317. #ifdef USE_WINSOCK
  1318. return_success = WSAEWOULDBLOCK == err;
  1319. #else
  1320. if(EINTR == err)
  1321. continue;
  1322. return_success = EWOULDBLOCK == err || EAGAIN == err;
  1323. #endif
  1324. if(!return_success)
  1325. return CURLM_WAKEUP_FAILURE;
  1326. }
  1327. return CURLM_OK;
  1328. }
  1329. }
  1330. #endif
  1331. #endif
  1332. return CURLM_WAKEUP_FAILURE;
  1333. }
  1334. /*
  1335. * multi_ischanged() is called
  1336. *
  1337. * Returns TRUE/FALSE whether the state is changed to trigger a CONNECT_PEND
  1338. * => CONNECT action.
  1339. *
  1340. * Set 'clear' to TRUE to have it also clear the state variable.
  1341. */
  1342. static bool multi_ischanged(struct Curl_multi *multi, bool clear)
  1343. {
  1344. bool retval = multi->recheckstate;
  1345. if(clear)
  1346. multi->recheckstate = FALSE;
  1347. return retval;
  1348. }
  1349. CURLMcode Curl_multi_add_perform(struct Curl_multi *multi,
  1350. struct Curl_easy *data,
  1351. struct connectdata *conn)
  1352. {
  1353. CURLMcode rc;
  1354. if(multi->in_callback)
  1355. return CURLM_RECURSIVE_API_CALL;
  1356. rc = curl_multi_add_handle(multi, data);
  1357. if(!rc) {
  1358. struct SingleRequest *k = &data->req;
  1359. /* pass in NULL for 'conn' here since we don't want to init the
  1360. connection, only this transfer */
  1361. Curl_init_do(data, NULL);
  1362. /* take this handle to the perform state right away */
  1363. multistate(data, MSTATE_PERFORMING);
  1364. Curl_attach_connection(data, conn);
  1365. k->keepon |= KEEP_RECV; /* setup to receive! */
  1366. }
  1367. return rc;
  1368. }
  1369. static CURLcode multi_do(struct Curl_easy *data, bool *done)
  1370. {
  1371. CURLcode result = CURLE_OK;
  1372. struct connectdata *conn = data->conn;
  1373. DEBUGASSERT(conn);
  1374. DEBUGASSERT(conn->handler);
  1375. if(conn->handler->do_it)
  1376. /* generic protocol-specific function pointer set in curl_connect() */
  1377. result = conn->handler->do_it(data, done);
  1378. return result;
  1379. }
  1380. /*
  1381. * multi_do_more() is called during the DO_MORE multi state. It is basically a
  1382. * second stage DO state which (wrongly) was introduced to support FTP's
  1383. * second connection.
  1384. *
  1385. * 'complete' can return 0 for incomplete, 1 for done and -1 for go back to
  1386. * DOING state there's more work to do!
  1387. */
  1388. static CURLcode multi_do_more(struct Curl_easy *data, int *complete)
  1389. {
  1390. CURLcode result = CURLE_OK;
  1391. struct connectdata *conn = data->conn;
  1392. *complete = 0;
  1393. if(conn->handler->do_more)
  1394. result = conn->handler->do_more(data, complete);
  1395. return result;
  1396. }
  1397. /*
  1398. * Check whether a timeout occurred, and handle it if it did
  1399. */
  1400. static bool multi_handle_timeout(struct Curl_easy *data,
  1401. struct curltime *now,
  1402. bool *stream_error,
  1403. CURLcode *result,
  1404. bool connect_timeout)
  1405. {
  1406. timediff_t timeout_ms;
  1407. timeout_ms = Curl_timeleft(data, now, connect_timeout);
  1408. if(timeout_ms < 0) {
  1409. /* Handle timed out */
  1410. if(data->mstate == MSTATE_RESOLVING)
  1411. failf(data, "Resolving timed out after %" CURL_FORMAT_TIMEDIFF_T
  1412. " milliseconds",
  1413. Curl_timediff(*now, data->progress.t_startsingle));
  1414. else if(data->mstate == MSTATE_CONNECTING)
  1415. failf(data, "Connection timed out after %" CURL_FORMAT_TIMEDIFF_T
  1416. " milliseconds",
  1417. Curl_timediff(*now, data->progress.t_startsingle));
  1418. else {
  1419. struct SingleRequest *k = &data->req;
  1420. if(k->size != -1) {
  1421. failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T
  1422. " milliseconds with %" CURL_FORMAT_CURL_OFF_T " out of %"
  1423. CURL_FORMAT_CURL_OFF_T " bytes received",
  1424. Curl_timediff(*now, data->progress.t_startsingle),
  1425. k->bytecount, k->size);
  1426. }
  1427. else {
  1428. failf(data, "Operation timed out after %" CURL_FORMAT_TIMEDIFF_T
  1429. " milliseconds with %" CURL_FORMAT_CURL_OFF_T
  1430. " bytes received",
  1431. Curl_timediff(*now, data->progress.t_startsingle),
  1432. k->bytecount);
  1433. }
  1434. }
  1435. /* Force connection closed if the connection has indeed been used */
  1436. if(data->mstate > MSTATE_DO) {
  1437. streamclose(data->conn, "Disconnected with pending data");
  1438. *stream_error = TRUE;
  1439. }
  1440. *result = CURLE_OPERATION_TIMEDOUT;
  1441. (void)multi_done(data, *result, TRUE);
  1442. }
  1443. return (timeout_ms < 0);
  1444. }
  1445. /*
  1446. * We are doing protocol-specific connecting and this is being called over and
  1447. * over from the multi interface until the connection phase is done on
  1448. * protocol layer.
  1449. */
  1450. static CURLcode protocol_connecting(struct Curl_easy *data, bool *done)
  1451. {
  1452. CURLcode result = CURLE_OK;
  1453. struct connectdata *conn = data->conn;
  1454. if(conn && conn->handler->connecting) {
  1455. *done = FALSE;
  1456. result = conn->handler->connecting(data, done);
  1457. }
  1458. else
  1459. *done = TRUE;
  1460. return result;
  1461. }
  1462. /*
  1463. * We are DOING this is being called over and over from the multi interface
  1464. * until the DOING phase is done on protocol layer.
  1465. */
  1466. static CURLcode protocol_doing(struct Curl_easy *data, bool *done)
  1467. {
  1468. CURLcode result = CURLE_OK;
  1469. struct connectdata *conn = data->conn;
  1470. if(conn && conn->handler->doing) {
  1471. *done = FALSE;
  1472. result = conn->handler->doing(data, done);
  1473. }
  1474. else
  1475. *done = TRUE;
  1476. return result;
  1477. }
  1478. /*
  1479. * We have discovered that the TCP connection has been successful, we can now
  1480. * proceed with some action.
  1481. *
  1482. */
  1483. static CURLcode protocol_connect(struct Curl_easy *data,
  1484. bool *protocol_done)
  1485. {
  1486. CURLcode result = CURLE_OK;
  1487. struct connectdata *conn = data->conn;
  1488. DEBUGASSERT(conn);
  1489. DEBUGASSERT(protocol_done);
  1490. *protocol_done = FALSE;
  1491. if(conn->bits.tcpconnect[FIRSTSOCKET] && conn->bits.protoconnstart) {
  1492. /* We already are connected, get back. This may happen when the connect
  1493. worked fine in the first call, like when we connect to a local server
  1494. or proxy. Note that we don't know if the protocol is actually done.
  1495. Unless this protocol doesn't have any protocol-connect callback, as
  1496. then we know we're done. */
  1497. if(!conn->handler->connecting)
  1498. *protocol_done = TRUE;
  1499. return CURLE_OK;
  1500. }
  1501. if(!conn->bits.protoconnstart) {
  1502. #ifndef CURL_DISABLE_PROXY
  1503. result = Curl_proxy_connect(data, FIRSTSOCKET);
  1504. if(result)
  1505. return result;
  1506. if(CONNECT_FIRSTSOCKET_PROXY_SSL())
  1507. /* wait for HTTPS proxy SSL initialization to complete */
  1508. return CURLE_OK;
  1509. if(conn->bits.tunnel_proxy && conn->bits.httpproxy &&
  1510. Curl_connect_ongoing(conn))
  1511. /* when using an HTTP tunnel proxy, await complete tunnel establishment
  1512. before proceeding further. Return CURLE_OK so we'll be called again */
  1513. return CURLE_OK;
  1514. #endif
  1515. if(conn->handler->connect_it) {
  1516. /* is there a protocol-specific connect() procedure? */
  1517. /* Call the protocol-specific connect function */
  1518. result = conn->handler->connect_it(data, protocol_done);
  1519. }
  1520. else
  1521. *protocol_done = TRUE;
  1522. /* it has started, possibly even completed but that knowledge isn't stored
  1523. in this bit! */
  1524. if(!result)
  1525. conn->bits.protoconnstart = TRUE;
  1526. }
  1527. return result; /* pass back status */
  1528. }
  1529. /*
  1530. * Curl_preconnect() is called immediately before a connect starts. When a
  1531. * redirect is followed, this is then called multiple times during a single
  1532. * transfer.
  1533. */
  1534. CURLcode Curl_preconnect(struct Curl_easy *data)
  1535. {
  1536. if(!data->state.buffer) {
  1537. data->state.buffer = malloc(data->set.buffer_size + 1);
  1538. if(!data->state.buffer)
  1539. return CURLE_OUT_OF_MEMORY;
  1540. }
  1541. return CURLE_OK;
  1542. }
  1543. static void set_in_callback(struct Curl_multi *multi, bool value)
  1544. {
  1545. multi->in_callback = value;
  1546. }
  1547. static CURLMcode multi_runsingle(struct Curl_multi *multi,
  1548. struct curltime *nowp,
  1549. struct Curl_easy *data)
  1550. {
  1551. struct Curl_message *msg = NULL;
  1552. bool connected;
  1553. bool async;
  1554. bool protocol_connected = FALSE;
  1555. bool dophase_done = FALSE;
  1556. bool done = FALSE;
  1557. CURLMcode rc;
  1558. CURLcode result = CURLE_OK;
  1559. timediff_t recv_timeout_ms;
  1560. timediff_t send_timeout_ms;
  1561. int control;
  1562. if(!GOOD_EASY_HANDLE(data))
  1563. return CURLM_BAD_EASY_HANDLE;
  1564. if(multi->dead) {
  1565. /* a multi-level callback returned error before, meaning every individual
  1566. transfer now has failed */
  1567. result = CURLE_ABORTED_BY_CALLBACK;
  1568. Curl_posttransfer(data);
  1569. multi_done(data, result, FALSE);
  1570. multistate(data, MSTATE_COMPLETED);
  1571. }
  1572. do {
  1573. /* A "stream" here is a logical stream if the protocol can handle that
  1574. (HTTP/2), or the full connection for older protocols */
  1575. bool stream_error = FALSE;
  1576. rc = CURLM_OK;
  1577. if(multi_ischanged(multi, TRUE)) {
  1578. DEBUGF(infof(data, "multi changed, check CONNECT_PEND queue"));
  1579. process_pending_handles(multi); /* multiplexed */
  1580. }
  1581. if(data->mstate > MSTATE_CONNECT &&
  1582. data->mstate < MSTATE_COMPLETED) {
  1583. /* Make sure we set the connection's current owner */
  1584. DEBUGASSERT(data->conn);
  1585. if(!data->conn)
  1586. return CURLM_INTERNAL_ERROR;
  1587. }
  1588. if(data->conn &&
  1589. (data->mstate >= MSTATE_CONNECT) &&
  1590. (data->mstate < MSTATE_COMPLETED)) {
  1591. /* Check for overall operation timeout here but defer handling the
  1592. * connection timeout to later, to allow for a connection to be set up
  1593. * in the window since we last checked timeout. This prevents us
  1594. * tearing down a completed connection in the case where we were slow
  1595. * to check the timeout (e.g. process descheduled during this loop).
  1596. * We set connect_timeout=FALSE to do this. */
  1597. /* we need to wait for the connect state as only then is the start time
  1598. stored, but we must not check already completed handles */
  1599. if(multi_handle_timeout(data, nowp, &stream_error, &result, FALSE)) {
  1600. /* Skip the statemachine and go directly to error handling section. */
  1601. goto statemachine_end;
  1602. }
  1603. }
  1604. switch(data->mstate) {
  1605. case MSTATE_INIT:
  1606. /* init this transfer. */
  1607. result = Curl_pretransfer(data);
  1608. if(!result) {
  1609. /* after init, go CONNECT */
  1610. multistate(data, MSTATE_CONNECT);
  1611. *nowp = Curl_pgrsTime(data, TIMER_STARTOP);
  1612. rc = CURLM_CALL_MULTI_PERFORM;
  1613. }
  1614. break;
  1615. case MSTATE_PENDING:
  1616. /* We will stay here until there is a connection available. Then
  1617. we try again in the MSTATE_CONNECT state. */
  1618. break;
  1619. case MSTATE_CONNECT:
  1620. /* Connect. We want to get a connection identifier filled in. */
  1621. /* init this transfer. */
  1622. result = Curl_preconnect(data);
  1623. if(result)
  1624. break;
  1625. *nowp = Curl_pgrsTime(data, TIMER_STARTSINGLE);
  1626. if(data->set.timeout)
  1627. Curl_expire(data, data->set.timeout, EXPIRE_TIMEOUT);
  1628. if(data->set.connecttimeout)
  1629. Curl_expire(data, data->set.connecttimeout, EXPIRE_CONNECTTIMEOUT);
  1630. result = Curl_connect(data, &async, &protocol_connected);
  1631. if(CURLE_NO_CONNECTION_AVAILABLE == result) {
  1632. /* There was no connection available. We will go to the pending
  1633. state and wait for an available connection. */
  1634. multistate(data, MSTATE_PENDING);
  1635. /* add this handle to the list of connect-pending handles */
  1636. Curl_llist_insert_next(&multi->pending, multi->pending.tail, data,
  1637. &data->connect_queue);
  1638. result = CURLE_OK;
  1639. break;
  1640. }
  1641. else if(data->state.previouslypending) {
  1642. /* this transfer comes from the pending queue so try move another */
  1643. infof(data, "Transfer was pending, now try another");
  1644. process_pending_handles(data->multi);
  1645. }
  1646. if(!result) {
  1647. if(async)
  1648. /* We're now waiting for an asynchronous name lookup */
  1649. multistate(data, MSTATE_RESOLVING);
  1650. else {
  1651. /* after the connect has been sent off, go WAITCONNECT unless the
  1652. protocol connect is already done and we can go directly to
  1653. WAITDO or DO! */
  1654. rc = CURLM_CALL_MULTI_PERFORM;
  1655. if(protocol_connected)
  1656. multistate(data, MSTATE_DO);
  1657. else {
  1658. #ifndef CURL_DISABLE_HTTP
  1659. if(Curl_connect_ongoing(data->conn))
  1660. multistate(data, MSTATE_TUNNELING);
  1661. else
  1662. #endif
  1663. multistate(data, MSTATE_CONNECTING);
  1664. }
  1665. }
  1666. }
  1667. break;
  1668. case MSTATE_RESOLVING:
  1669. /* awaiting an asynch name resolve to complete */
  1670. {
  1671. struct Curl_dns_entry *dns = NULL;
  1672. struct connectdata *conn = data->conn;
  1673. const char *hostname;
  1674. DEBUGASSERT(conn);
  1675. #ifndef CURL_DISABLE_PROXY
  1676. if(conn->bits.httpproxy)
  1677. hostname = conn->http_proxy.host.name;
  1678. else
  1679. #endif
  1680. if(conn->bits.conn_to_host)
  1681. hostname = conn->conn_to_host.name;
  1682. else
  1683. hostname = conn->host.name;
  1684. /* check if we have the name resolved by now */
  1685. dns = Curl_fetch_addr(data, hostname, (int)conn->port);
  1686. if(dns) {
  1687. #ifdef CURLRES_ASYNCH
  1688. data->state.async.dns = dns;
  1689. data->state.async.done = TRUE;
  1690. #endif
  1691. result = CURLE_OK;
  1692. infof(data, "Hostname '%s' was found in DNS cache", hostname);
  1693. }
  1694. if(!dns)
  1695. result = Curl_resolv_check(data, &dns);
  1696. /* Update sockets here, because the socket(s) may have been
  1697. closed and the application thus needs to be told, even if it
  1698. is likely that the same socket(s) will again be used further
  1699. down. If the name has not yet been resolved, it is likely
  1700. that new sockets have been opened in an attempt to contact
  1701. another resolver. */
  1702. rc = singlesocket(multi, data);
  1703. if(rc)
  1704. return rc;
  1705. if(dns) {
  1706. /* Perform the next step in the connection phase, and then move on
  1707. to the WAITCONNECT state */
  1708. result = Curl_once_resolved(data, &protocol_connected);
  1709. if(result)
  1710. /* if Curl_once_resolved() returns failure, the connection struct
  1711. is already freed and gone */
  1712. data->conn = NULL; /* no more connection */
  1713. else {
  1714. /* call again please so that we get the next socket setup */
  1715. rc = CURLM_CALL_MULTI_PERFORM;
  1716. if(protocol_connected)
  1717. multistate(data, MSTATE_DO);
  1718. else {
  1719. #ifndef CURL_DISABLE_HTTP
  1720. if(Curl_connect_ongoing(data->conn))
  1721. multistate(data, MSTATE_TUNNELING);
  1722. else
  1723. #endif
  1724. multistate(data, MSTATE_CONNECTING);
  1725. }
  1726. }
  1727. }
  1728. if(result) {
  1729. /* failure detected */
  1730. stream_error = TRUE;
  1731. break;
  1732. }
  1733. }
  1734. break;
  1735. #ifndef CURL_DISABLE_HTTP
  1736. case MSTATE_TUNNELING:
  1737. /* this is HTTP-specific, but sending CONNECT to a proxy is HTTP... */
  1738. DEBUGASSERT(data->conn);
  1739. result = Curl_http_connect(data, &protocol_connected);
  1740. #ifndef CURL_DISABLE_PROXY
  1741. if(data->conn->bits.proxy_connect_closed) {
  1742. rc = CURLM_CALL_MULTI_PERFORM;
  1743. /* connect back to proxy again */
  1744. result = CURLE_OK;
  1745. multi_done(data, CURLE_OK, FALSE);
  1746. multistate(data, MSTATE_CONNECT);
  1747. }
  1748. else
  1749. #endif
  1750. if(!result) {
  1751. if(
  1752. #ifndef CURL_DISABLE_PROXY
  1753. (data->conn->http_proxy.proxytype != CURLPROXY_HTTPS ||
  1754. data->conn->bits.proxy_ssl_connected[FIRSTSOCKET]) &&
  1755. #endif
  1756. Curl_connect_complete(data->conn)) {
  1757. rc = CURLM_CALL_MULTI_PERFORM;
  1758. /* initiate protocol connect phase */
  1759. multistate(data, MSTATE_PROTOCONNECT);
  1760. }
  1761. }
  1762. else
  1763. stream_error = TRUE;
  1764. break;
  1765. #endif
  1766. case MSTATE_CONNECTING:
  1767. /* awaiting a completion of an asynch TCP connect */
  1768. DEBUGASSERT(data->conn);
  1769. result = Curl_is_connected(data, data->conn, FIRSTSOCKET, &connected);
  1770. if(connected && !result) {
  1771. #ifndef CURL_DISABLE_HTTP
  1772. if(
  1773. #ifndef CURL_DISABLE_PROXY
  1774. (data->conn->http_proxy.proxytype == CURLPROXY_HTTPS &&
  1775. !data->conn->bits.proxy_ssl_connected[FIRSTSOCKET]) ||
  1776. #endif
  1777. Curl_connect_ongoing(data->conn)) {
  1778. multistate(data, MSTATE_TUNNELING);
  1779. break;
  1780. }
  1781. #endif
  1782. rc = CURLM_CALL_MULTI_PERFORM;
  1783. #ifndef CURL_DISABLE_PROXY
  1784. multistate(data,
  1785. data->conn->bits.tunnel_proxy?
  1786. MSTATE_TUNNELING : MSTATE_PROTOCONNECT);
  1787. #else
  1788. multistate(data, MSTATE_PROTOCONNECT);
  1789. #endif
  1790. }
  1791. else if(result) {
  1792. /* failure detected */
  1793. Curl_posttransfer(data);
  1794. multi_done(data, result, TRUE);
  1795. stream_error = TRUE;
  1796. break;
  1797. }
  1798. break;
  1799. case MSTATE_PROTOCONNECT:
  1800. result = protocol_connect(data, &protocol_connected);
  1801. if(!result && !protocol_connected)
  1802. /* switch to waiting state */
  1803. multistate(data, MSTATE_PROTOCONNECTING);
  1804. else if(!result) {
  1805. /* protocol connect has completed, go WAITDO or DO */
  1806. multistate(data, MSTATE_DO);
  1807. rc = CURLM_CALL_MULTI_PERFORM;
  1808. }
  1809. else {
  1810. /* failure detected */
  1811. Curl_posttransfer(data);
  1812. multi_done(data, result, TRUE);
  1813. stream_error = TRUE;
  1814. }
  1815. break;
  1816. case MSTATE_PROTOCONNECTING:
  1817. /* protocol-specific connect phase */
  1818. result = protocol_connecting(data, &protocol_connected);
  1819. if(!result && protocol_connected) {
  1820. /* after the connect has completed, go WAITDO or DO */
  1821. multistate(data, MSTATE_DO);
  1822. rc = CURLM_CALL_MULTI_PERFORM;
  1823. }
  1824. else if(result) {
  1825. /* failure detected */
  1826. Curl_posttransfer(data);
  1827. multi_done(data, result, TRUE);
  1828. stream_error = TRUE;
  1829. }
  1830. break;
  1831. case MSTATE_DO:
  1832. if(data->set.fprereq) {
  1833. int prereq_rc;
  1834. /* call the prerequest callback function */
  1835. Curl_set_in_callback(data, true);
  1836. prereq_rc = data->set.fprereq(data->set.prereq_userp,
  1837. data->info.conn_primary_ip,
  1838. data->info.conn_local_ip,
  1839. data->info.conn_primary_port,
  1840. data->info.conn_local_port);
  1841. Curl_set_in_callback(data, false);
  1842. if(prereq_rc != CURL_PREREQFUNC_OK) {
  1843. failf(data, "operation aborted by pre-request callback");
  1844. /* failure in pre-request callback - don't do any other processing */
  1845. result = CURLE_ABORTED_BY_CALLBACK;
  1846. Curl_posttransfer(data);
  1847. multi_done(data, result, FALSE);
  1848. stream_error = TRUE;
  1849. break;
  1850. }
  1851. }
  1852. if(data->set.connect_only == 1) {
  1853. /* keep connection open for application to use the socket */
  1854. connkeep(data->conn, "CONNECT_ONLY");
  1855. multistate(data, MSTATE_DONE);
  1856. result = CURLE_OK;
  1857. rc = CURLM_CALL_MULTI_PERFORM;
  1858. }
  1859. else {
  1860. /* Perform the protocol's DO action */
  1861. result = multi_do(data, &dophase_done);
  1862. /* When multi_do() returns failure, data->conn might be NULL! */
  1863. if(!result) {
  1864. if(!dophase_done) {
  1865. #ifndef CURL_DISABLE_FTP
  1866. /* some steps needed for wildcard matching */
  1867. if(data->state.wildcardmatch) {
  1868. struct WildcardData *wc = &data->wildcard;
  1869. if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) {
  1870. /* skip some states if it is important */
  1871. multi_done(data, CURLE_OK, FALSE);
  1872. /* if there's no connection left, skip the DONE state */
  1873. multistate(data, data->conn ?
  1874. MSTATE_DONE : MSTATE_COMPLETED);
  1875. rc = CURLM_CALL_MULTI_PERFORM;
  1876. break;
  1877. }
  1878. }
  1879. #endif
  1880. /* DO was not completed in one function call, we must continue
  1881. DOING... */
  1882. multistate(data, MSTATE_DOING);
  1883. rc = CURLM_OK;
  1884. }
  1885. /* after DO, go DO_DONE... or DO_MORE */
  1886. else if(data->conn->bits.do_more) {
  1887. /* we're supposed to do more, but we need to sit down, relax
  1888. and wait a little while first */
  1889. multistate(data, MSTATE_DOING_MORE);
  1890. rc = CURLM_OK;
  1891. }
  1892. else {
  1893. /* we're done with the DO, now DID */
  1894. multistate(data, MSTATE_DID);
  1895. rc = CURLM_CALL_MULTI_PERFORM;
  1896. }
  1897. }
  1898. else if((CURLE_SEND_ERROR == result) &&
  1899. data->conn->bits.reuse) {
  1900. /*
  1901. * In this situation, a connection that we were trying to use
  1902. * may have unexpectedly died. If possible, send the connection
  1903. * back to the CONNECT phase so we can try again.
  1904. */
  1905. char *newurl = NULL;
  1906. followtype follow = FOLLOW_NONE;
  1907. CURLcode drc;
  1908. drc = Curl_retry_request(data, &newurl);
  1909. if(drc) {
  1910. /* a failure here pretty much implies an out of memory */
  1911. result = drc;
  1912. stream_error = TRUE;
  1913. }
  1914. Curl_posttransfer(data);
  1915. drc = multi_done(data, result, FALSE);
  1916. /* When set to retry the connection, we must go back to the CONNECT
  1917. * state */
  1918. if(newurl) {
  1919. if(!drc || (drc == CURLE_SEND_ERROR)) {
  1920. follow = FOLLOW_RETRY;
  1921. drc = Curl_follow(data, newurl, follow);
  1922. if(!drc) {
  1923. multistate(data, MSTATE_CONNECT);
  1924. rc = CURLM_CALL_MULTI_PERFORM;
  1925. result = CURLE_OK;
  1926. }
  1927. else {
  1928. /* Follow failed */
  1929. result = drc;
  1930. }
  1931. }
  1932. else {
  1933. /* done didn't return OK or SEND_ERROR */
  1934. result = drc;
  1935. }
  1936. }
  1937. else {
  1938. /* Have error handler disconnect conn if we can't retry */
  1939. stream_error = TRUE;
  1940. }
  1941. free(newurl);
  1942. }
  1943. else {
  1944. /* failure detected */
  1945. Curl_posttransfer(data);
  1946. if(data->conn)
  1947. multi_done(data, result, FALSE);
  1948. stream_error = TRUE;
  1949. }
  1950. }
  1951. break;
  1952. case MSTATE_DOING:
  1953. /* we continue DOING until the DO phase is complete */
  1954. DEBUGASSERT(data->conn);
  1955. result = protocol_doing(data, &dophase_done);
  1956. if(!result) {
  1957. if(dophase_done) {
  1958. /* after DO, go DO_DONE or DO_MORE */
  1959. multistate(data, data->conn->bits.do_more?
  1960. MSTATE_DOING_MORE : MSTATE_DID);
  1961. rc = CURLM_CALL_MULTI_PERFORM;
  1962. } /* dophase_done */
  1963. }
  1964. else {
  1965. /* failure detected */
  1966. Curl_posttransfer(data);
  1967. multi_done(data, result, FALSE);
  1968. stream_error = TRUE;
  1969. }
  1970. break;
  1971. case MSTATE_DOING_MORE:
  1972. /*
  1973. * When we are connected, DOING MORE and then go DID
  1974. */
  1975. DEBUGASSERT(data->conn);
  1976. result = multi_do_more(data, &control);
  1977. if(!result) {
  1978. if(control) {
  1979. /* if positive, advance to DO_DONE
  1980. if negative, go back to DOING */
  1981. multistate(data, control == 1?
  1982. MSTATE_DID : MSTATE_DOING);
  1983. rc = CURLM_CALL_MULTI_PERFORM;
  1984. }
  1985. else
  1986. /* stay in DO_MORE */
  1987. rc = CURLM_OK;
  1988. }
  1989. else {
  1990. /* failure detected */
  1991. Curl_posttransfer(data);
  1992. multi_done(data, result, FALSE);
  1993. stream_error = TRUE;
  1994. }
  1995. break;
  1996. case MSTATE_DID:
  1997. DEBUGASSERT(data->conn);
  1998. if(data->conn->bits.multiplex)
  1999. /* Check if we can move pending requests to send pipe */
  2000. process_pending_handles(multi); /* multiplexed */
  2001. /* Only perform the transfer if there's a good socket to work with.
  2002. Having both BAD is a signal to skip immediately to DONE */
  2003. if((data->conn->sockfd != CURL_SOCKET_BAD) ||
  2004. (data->conn->writesockfd != CURL_SOCKET_BAD))
  2005. multistate(data, MSTATE_PERFORMING);
  2006. else {
  2007. #ifndef CURL_DISABLE_FTP
  2008. if(data->state.wildcardmatch &&
  2009. ((data->conn->handler->flags & PROTOPT_WILDCARD) == 0)) {
  2010. data->wildcard.state = CURLWC_DONE;
  2011. }
  2012. #endif
  2013. multistate(data, MSTATE_DONE);
  2014. }
  2015. rc = CURLM_CALL_MULTI_PERFORM;
  2016. break;
  2017. case MSTATE_RATELIMITING: /* limit-rate exceeded in either direction */
  2018. DEBUGASSERT(data->conn);
  2019. /* if both rates are within spec, resume transfer */
  2020. if(Curl_pgrsUpdate(data))
  2021. result = CURLE_ABORTED_BY_CALLBACK;
  2022. else
  2023. result = Curl_speedcheck(data, *nowp);
  2024. if(result) {
  2025. if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
  2026. result != CURLE_HTTP2_STREAM)
  2027. streamclose(data->conn, "Transfer returned error");
  2028. Curl_posttransfer(data);
  2029. multi_done(data, result, TRUE);
  2030. }
  2031. else {
  2032. send_timeout_ms = 0;
  2033. if(data->set.max_send_speed)
  2034. send_timeout_ms =
  2035. Curl_pgrsLimitWaitTime(data->progress.uploaded,
  2036. data->progress.ul_limit_size,
  2037. data->set.max_send_speed,
  2038. data->progress.ul_limit_start,
  2039. *nowp);
  2040. recv_timeout_ms = 0;
  2041. if(data->set.max_recv_speed)
  2042. recv_timeout_ms =
  2043. Curl_pgrsLimitWaitTime(data->progress.downloaded,
  2044. data->progress.dl_limit_size,
  2045. data->set.max_recv_speed,
  2046. data->progress.dl_limit_start,
  2047. *nowp);
  2048. if(!send_timeout_ms && !recv_timeout_ms) {
  2049. multistate(data, MSTATE_PERFORMING);
  2050. Curl_ratelimit(data, *nowp);
  2051. }
  2052. else if(send_timeout_ms >= recv_timeout_ms)
  2053. Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST);
  2054. else
  2055. Curl_expire(data, recv_timeout_ms, EXPIRE_TOOFAST);
  2056. }
  2057. break;
  2058. case MSTATE_PERFORMING:
  2059. {
  2060. char *newurl = NULL;
  2061. bool retry = FALSE;
  2062. bool comeback = FALSE;
  2063. DEBUGASSERT(data->state.buffer);
  2064. /* check if over send speed */
  2065. send_timeout_ms = 0;
  2066. if(data->set.max_send_speed)
  2067. send_timeout_ms = Curl_pgrsLimitWaitTime(data->progress.uploaded,
  2068. data->progress.ul_limit_size,
  2069. data->set.max_send_speed,
  2070. data->progress.ul_limit_start,
  2071. *nowp);
  2072. /* check if over recv speed */
  2073. recv_timeout_ms = 0;
  2074. if(data->set.max_recv_speed)
  2075. recv_timeout_ms = Curl_pgrsLimitWaitTime(data->progress.downloaded,
  2076. data->progress.dl_limit_size,
  2077. data->set.max_recv_speed,
  2078. data->progress.dl_limit_start,
  2079. *nowp);
  2080. if(send_timeout_ms || recv_timeout_ms) {
  2081. Curl_ratelimit(data, *nowp);
  2082. multistate(data, MSTATE_RATELIMITING);
  2083. if(send_timeout_ms >= recv_timeout_ms)
  2084. Curl_expire(data, send_timeout_ms, EXPIRE_TOOFAST);
  2085. else
  2086. Curl_expire(data, recv_timeout_ms, EXPIRE_TOOFAST);
  2087. break;
  2088. }
  2089. /* read/write data if it is ready to do so */
  2090. result = Curl_readwrite(data->conn, data, &done, &comeback);
  2091. if(done || (result == CURLE_RECV_ERROR)) {
  2092. /* If CURLE_RECV_ERROR happens early enough, we assume it was a race
  2093. * condition and the server closed the re-used connection exactly when
  2094. * we wanted to use it, so figure out if that is indeed the case.
  2095. */
  2096. CURLcode ret = Curl_retry_request(data, &newurl);
  2097. if(!ret)
  2098. retry = (newurl)?TRUE:FALSE;
  2099. else if(!result)
  2100. result = ret;
  2101. if(retry) {
  2102. /* if we are to retry, set the result to OK and consider the
  2103. request as done */
  2104. result = CURLE_OK;
  2105. done = TRUE;
  2106. }
  2107. }
  2108. else if((CURLE_HTTP2_STREAM == result) &&
  2109. Curl_h2_http_1_1_error(data)) {
  2110. CURLcode ret = Curl_retry_request(data, &newurl);
  2111. if(!ret) {
  2112. infof(data, "Downgrades to HTTP/1.1");
  2113. streamclose(data->conn, "Disconnect HTTP/2 for HTTP/1");
  2114. data->state.httpwant = CURL_HTTP_VERSION_1_1;
  2115. /* clear the error message bit too as we ignore the one we got */
  2116. data->state.errorbuf = FALSE;
  2117. if(!newurl)
  2118. /* typically for HTTP_1_1_REQUIRED error on first flight */
  2119. newurl = strdup(data->state.url);
  2120. /* if we are to retry, set the result to OK and consider the request
  2121. as done */
  2122. retry = TRUE;
  2123. result = CURLE_OK;
  2124. done = TRUE;
  2125. }
  2126. else
  2127. result = ret;
  2128. }
  2129. if(result) {
  2130. /*
  2131. * The transfer phase returned error, we mark the connection to get
  2132. * closed to prevent being re-used. This is because we can't possibly
  2133. * know if the connection is in a good shape or not now. Unless it is
  2134. * a protocol which uses two "channels" like FTP, as then the error
  2135. * happened in the data connection.
  2136. */
  2137. if(!(data->conn->handler->flags & PROTOPT_DUAL) &&
  2138. result != CURLE_HTTP2_STREAM)
  2139. streamclose(data->conn, "Transfer returned error");
  2140. Curl_posttransfer(data);
  2141. multi_done(data, result, TRUE);
  2142. }
  2143. else if(done) {
  2144. /* call this even if the readwrite function returned error */
  2145. Curl_posttransfer(data);
  2146. /* When we follow redirects or is set to retry the connection, we must
  2147. to go back to the CONNECT state */
  2148. if(data->req.newurl || retry) {
  2149. followtype follow = FOLLOW_NONE;
  2150. if(!retry) {
  2151. /* if the URL is a follow-location and not just a retried request
  2152. then figure out the URL here */
  2153. free(newurl);
  2154. newurl = data->req.newurl;
  2155. data->req.newurl = NULL;
  2156. follow = FOLLOW_REDIR;
  2157. }
  2158. else
  2159. follow = FOLLOW_RETRY;
  2160. (void)multi_done(data, CURLE_OK, FALSE);
  2161. /* multi_done() might return CURLE_GOT_NOTHING */
  2162. result = Curl_follow(data, newurl, follow);
  2163. if(!result) {
  2164. multistate(data, MSTATE_CONNECT);
  2165. rc = CURLM_CALL_MULTI_PERFORM;
  2166. }
  2167. free(newurl);
  2168. }
  2169. else {
  2170. /* after the transfer is done, go DONE */
  2171. /* but first check to see if we got a location info even though we're
  2172. not following redirects */
  2173. if(data->req.location) {
  2174. free(newurl);
  2175. newurl = data->req.location;
  2176. data->req.location = NULL;
  2177. result = Curl_follow(data, newurl, FOLLOW_FAKE);
  2178. free(newurl);
  2179. if(result) {
  2180. stream_error = TRUE;
  2181. result = multi_done(data, result, TRUE);
  2182. }
  2183. }
  2184. if(!result) {
  2185. multistate(data, MSTATE_DONE);
  2186. rc = CURLM_CALL_MULTI_PERFORM;
  2187. }
  2188. }
  2189. }
  2190. else if(comeback) {
  2191. /* This avoids CURLM_CALL_MULTI_PERFORM so that a very fast transfer
  2192. won't get stuck on this transfer at the expense of other concurrent
  2193. transfers */
  2194. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  2195. rc = CURLM_OK;
  2196. }
  2197. break;
  2198. }
  2199. case MSTATE_DONE:
  2200. /* this state is highly transient, so run another loop after this */
  2201. rc = CURLM_CALL_MULTI_PERFORM;
  2202. if(data->conn) {
  2203. CURLcode res;
  2204. if(data->conn->bits.multiplex)
  2205. /* Check if we can move pending requests to connection */
  2206. process_pending_handles(multi); /* multiplexing */
  2207. /* post-transfer command */
  2208. res = multi_done(data, result, FALSE);
  2209. /* allow a previously set error code take precedence */
  2210. if(!result)
  2211. result = res;
  2212. }
  2213. #ifndef CURL_DISABLE_FTP
  2214. if(data->state.wildcardmatch) {
  2215. if(data->wildcard.state != CURLWC_DONE) {
  2216. /* if a wildcard is set and we are not ending -> lets start again
  2217. with MSTATE_INIT */
  2218. multistate(data, MSTATE_INIT);
  2219. break;
  2220. }
  2221. }
  2222. #endif
  2223. /* after we have DONE what we're supposed to do, go COMPLETED, and
  2224. it doesn't matter what the multi_done() returned! */
  2225. multistate(data, MSTATE_COMPLETED);
  2226. break;
  2227. case MSTATE_COMPLETED:
  2228. break;
  2229. case MSTATE_MSGSENT:
  2230. data->result = result;
  2231. return CURLM_OK; /* do nothing */
  2232. default:
  2233. return CURLM_INTERNAL_ERROR;
  2234. }
  2235. if(data->conn &&
  2236. data->mstate >= MSTATE_CONNECT &&
  2237. data->mstate < MSTATE_DO &&
  2238. rc != CURLM_CALL_MULTI_PERFORM &&
  2239. !multi_ischanged(multi, false)) {
  2240. /* We now handle stream timeouts if and only if this will be the last
  2241. * loop iteration. We only check this on the last iteration to ensure
  2242. * that if we know we have additional work to do immediately
  2243. * (i.e. CURLM_CALL_MULTI_PERFORM == TRUE) then we should do that before
  2244. * declaring the connection timed out as we may almost have a completed
  2245. * connection. */
  2246. multi_handle_timeout(data, nowp, &stream_error, &result, TRUE);
  2247. }
  2248. statemachine_end:
  2249. if(data->mstate < MSTATE_COMPLETED) {
  2250. if(result) {
  2251. /*
  2252. * If an error was returned, and we aren't in completed state now,
  2253. * then we go to completed and consider this transfer aborted.
  2254. */
  2255. /* NOTE: no attempt to disconnect connections must be made
  2256. in the case blocks above - cleanup happens only here */
  2257. /* Check if we can move pending requests to send pipe */
  2258. process_pending_handles(multi); /* connection */
  2259. if(data->conn) {
  2260. if(stream_error) {
  2261. /* Don't attempt to send data over a connection that timed out */
  2262. bool dead_connection = result == CURLE_OPERATION_TIMEDOUT;
  2263. struct connectdata *conn = data->conn;
  2264. /* This is where we make sure that the conn pointer is reset.
  2265. We don't have to do this in every case block above where a
  2266. failure is detected */
  2267. Curl_detach_connection(data);
  2268. /* remove connection from cache */
  2269. Curl_conncache_remove_conn(data, conn, TRUE);
  2270. /* disconnect properly */
  2271. Curl_disconnect(data, conn, dead_connection);
  2272. }
  2273. }
  2274. else if(data->mstate == MSTATE_CONNECT) {
  2275. /* Curl_connect() failed */
  2276. (void)Curl_posttransfer(data);
  2277. }
  2278. multistate(data, MSTATE_COMPLETED);
  2279. rc = CURLM_CALL_MULTI_PERFORM;
  2280. }
  2281. /* if there's still a connection to use, call the progress function */
  2282. else if(data->conn && Curl_pgrsUpdate(data)) {
  2283. /* aborted due to progress callback return code must close the
  2284. connection */
  2285. result = CURLE_ABORTED_BY_CALLBACK;
  2286. streamclose(data->conn, "Aborted by callback");
  2287. /* if not yet in DONE state, go there, otherwise COMPLETED */
  2288. multistate(data, (data->mstate < MSTATE_DONE)?
  2289. MSTATE_DONE: MSTATE_COMPLETED);
  2290. rc = CURLM_CALL_MULTI_PERFORM;
  2291. }
  2292. }
  2293. if(MSTATE_COMPLETED == data->mstate) {
  2294. if(data->set.fmultidone) {
  2295. /* signal via callback instead */
  2296. data->set.fmultidone(data, result);
  2297. }
  2298. else {
  2299. /* now fill in the Curl_message with this info */
  2300. msg = &data->msg;
  2301. msg->extmsg.msg = CURLMSG_DONE;
  2302. msg->extmsg.easy_handle = data;
  2303. msg->extmsg.data.result = result;
  2304. rc = multi_addmsg(multi, msg);
  2305. DEBUGASSERT(!data->conn);
  2306. }
  2307. multistate(data, MSTATE_MSGSENT);
  2308. }
  2309. } while((rc == CURLM_CALL_MULTI_PERFORM) || multi_ischanged(multi, FALSE));
  2310. data->result = result;
  2311. return rc;
  2312. }
  2313. CURLMcode curl_multi_perform(struct Curl_multi *multi, int *running_handles)
  2314. {
  2315. struct Curl_easy *data;
  2316. CURLMcode returncode = CURLM_OK;
  2317. struct Curl_tree *t;
  2318. struct curltime now = Curl_now();
  2319. if(!GOOD_MULTI_HANDLE(multi))
  2320. return CURLM_BAD_HANDLE;
  2321. if(multi->in_callback)
  2322. return CURLM_RECURSIVE_API_CALL;
  2323. data = multi->easyp;
  2324. while(data) {
  2325. CURLMcode result;
  2326. SIGPIPE_VARIABLE(pipe_st);
  2327. sigpipe_ignore(data, &pipe_st);
  2328. result = multi_runsingle(multi, &now, data);
  2329. sigpipe_restore(&pipe_st);
  2330. if(result)
  2331. returncode = result;
  2332. data = data->next; /* operate on next handle */
  2333. }
  2334. /*
  2335. * Simply remove all expired timers from the splay since handles are dealt
  2336. * with unconditionally by this function and curl_multi_timeout() requires
  2337. * that already passed/handled expire times are removed from the splay.
  2338. *
  2339. * It is important that the 'now' value is set at the entry of this function
  2340. * and not for the current time as it may have ticked a little while since
  2341. * then and then we risk this loop to remove timers that actually have not
  2342. * been handled!
  2343. */
  2344. do {
  2345. multi->timetree = Curl_splaygetbest(now, multi->timetree, &t);
  2346. if(t)
  2347. /* the removed may have another timeout in queue */
  2348. (void)add_next_timeout(now, multi, t->payload);
  2349. } while(t);
  2350. *running_handles = multi->num_alive;
  2351. if(CURLM_OK >= returncode)
  2352. returncode = Curl_update_timer(multi);
  2353. return returncode;
  2354. }
  2355. CURLMcode curl_multi_cleanup(struct Curl_multi *multi)
  2356. {
  2357. struct Curl_easy *data;
  2358. struct Curl_easy *nextdata;
  2359. if(GOOD_MULTI_HANDLE(multi)) {
  2360. if(multi->in_callback)
  2361. return CURLM_RECURSIVE_API_CALL;
  2362. multi->magic = 0; /* not good anymore */
  2363. /* First remove all remaining easy handles */
  2364. data = multi->easyp;
  2365. while(data) {
  2366. nextdata = data->next;
  2367. if(!data->state.done && data->conn)
  2368. /* if DONE was never called for this handle */
  2369. (void)multi_done(data, CURLE_OK, TRUE);
  2370. if(data->dns.hostcachetype == HCACHE_MULTI) {
  2371. /* clear out the usage of the shared DNS cache */
  2372. Curl_hostcache_clean(data, data->dns.hostcache);
  2373. data->dns.hostcache = NULL;
  2374. data->dns.hostcachetype = HCACHE_NONE;
  2375. }
  2376. /* Clear the pointer to the connection cache */
  2377. data->state.conn_cache = NULL;
  2378. data->multi = NULL; /* clear the association */
  2379. #ifdef USE_LIBPSL
  2380. if(data->psl == &multi->psl)
  2381. data->psl = NULL;
  2382. #endif
  2383. data = nextdata;
  2384. }
  2385. /* Close all the connections in the connection cache */
  2386. Curl_conncache_close_all_connections(&multi->conn_cache);
  2387. sockhash_destroy(&multi->sockhash);
  2388. Curl_conncache_destroy(&multi->conn_cache);
  2389. Curl_llist_destroy(&multi->msglist, NULL);
  2390. Curl_llist_destroy(&multi->pending, NULL);
  2391. Curl_hash_destroy(&multi->hostcache);
  2392. Curl_psl_destroy(&multi->psl);
  2393. #ifdef USE_WINSOCK
  2394. WSACloseEvent(multi->wsa_event);
  2395. #else
  2396. #ifdef ENABLE_WAKEUP
  2397. wakeup_close(multi->wakeup_pair[0]);
  2398. wakeup_close(multi->wakeup_pair[1]);
  2399. #endif
  2400. #endif
  2401. free(multi);
  2402. return CURLM_OK;
  2403. }
  2404. return CURLM_BAD_HANDLE;
  2405. }
  2406. /*
  2407. * curl_multi_info_read()
  2408. *
  2409. * This function is the primary way for a multi/multi_socket application to
  2410. * figure out if a transfer has ended. We MUST make this function as fast as
  2411. * possible as it will be polled frequently and we MUST NOT scan any lists in
  2412. * here to figure out things. We must scale fine to thousands of handles and
  2413. * beyond. The current design is fully O(1).
  2414. */
  2415. CURLMsg *curl_multi_info_read(struct Curl_multi *multi, int *msgs_in_queue)
  2416. {
  2417. struct Curl_message *msg;
  2418. *msgs_in_queue = 0; /* default to none */
  2419. if(GOOD_MULTI_HANDLE(multi) &&
  2420. !multi->in_callback &&
  2421. Curl_llist_count(&multi->msglist)) {
  2422. /* there is one or more messages in the list */
  2423. struct Curl_llist_element *e;
  2424. /* extract the head of the list to return */
  2425. e = multi->msglist.head;
  2426. msg = e->ptr;
  2427. /* remove the extracted entry */
  2428. Curl_llist_remove(&multi->msglist, e, NULL);
  2429. *msgs_in_queue = curlx_uztosi(Curl_llist_count(&multi->msglist));
  2430. return &msg->extmsg;
  2431. }
  2432. return NULL;
  2433. }
  2434. /*
  2435. * singlesocket() checks what sockets we deal with and their "action state"
  2436. * and if we have a different state in any of those sockets from last time we
  2437. * call the callback accordingly.
  2438. */
  2439. static CURLMcode singlesocket(struct Curl_multi *multi,
  2440. struct Curl_easy *data)
  2441. {
  2442. curl_socket_t socks[MAX_SOCKSPEREASYHANDLE];
  2443. int i;
  2444. struct Curl_sh_entry *entry;
  2445. curl_socket_t s;
  2446. int num;
  2447. unsigned int curraction;
  2448. unsigned char actions[MAX_SOCKSPEREASYHANDLE];
  2449. int rc;
  2450. for(i = 0; i< MAX_SOCKSPEREASYHANDLE; i++)
  2451. socks[i] = CURL_SOCKET_BAD;
  2452. /* Fill in the 'current' struct with the state as it is now: what sockets to
  2453. supervise and for what actions */
  2454. curraction = multi_getsock(data, socks);
  2455. /* We have 0 .. N sockets already and we get to know about the 0 .. M
  2456. sockets we should have from now on. Detect the differences, remove no
  2457. longer supervised ones and add new ones */
  2458. /* walk over the sockets we got right now */
  2459. for(i = 0; (i< MAX_SOCKSPEREASYHANDLE) &&
  2460. (curraction & (GETSOCK_READSOCK(i) | GETSOCK_WRITESOCK(i)));
  2461. i++) {
  2462. unsigned char action = CURL_POLL_NONE;
  2463. unsigned char prevaction = 0;
  2464. int comboaction;
  2465. bool sincebefore = FALSE;
  2466. s = socks[i];
  2467. /* get it from the hash */
  2468. entry = sh_getentry(&multi->sockhash, s);
  2469. if(curraction & GETSOCK_READSOCK(i))
  2470. action |= CURL_POLL_IN;
  2471. if(curraction & GETSOCK_WRITESOCK(i))
  2472. action |= CURL_POLL_OUT;
  2473. actions[i] = action;
  2474. if(entry) {
  2475. /* check if new for this transfer */
  2476. int j;
  2477. for(j = 0; j< data->numsocks; j++) {
  2478. if(s == data->sockets[j]) {
  2479. prevaction = data->actions[j];
  2480. sincebefore = TRUE;
  2481. break;
  2482. }
  2483. }
  2484. }
  2485. else {
  2486. /* this is a socket we didn't have before, add it to the hash! */
  2487. entry = sh_addentry(&multi->sockhash, s);
  2488. if(!entry)
  2489. /* fatal */
  2490. return CURLM_OUT_OF_MEMORY;
  2491. }
  2492. if(sincebefore && (prevaction != action)) {
  2493. /* Socket was used already, but different action now */
  2494. if(prevaction & CURL_POLL_IN)
  2495. entry->readers--;
  2496. if(prevaction & CURL_POLL_OUT)
  2497. entry->writers--;
  2498. if(action & CURL_POLL_IN)
  2499. entry->readers++;
  2500. if(action & CURL_POLL_OUT)
  2501. entry->writers++;
  2502. }
  2503. else if(!sincebefore) {
  2504. /* a new user */
  2505. entry->users++;
  2506. if(action & CURL_POLL_IN)
  2507. entry->readers++;
  2508. if(action & CURL_POLL_OUT)
  2509. entry->writers++;
  2510. /* add 'data' to the transfer hash on this socket! */
  2511. if(!Curl_hash_add(&entry->transfers, (char *)&data, /* hash key */
  2512. sizeof(struct Curl_easy *), data)) {
  2513. Curl_hash_destroy(&entry->transfers);
  2514. return CURLM_OUT_OF_MEMORY;
  2515. }
  2516. }
  2517. comboaction = (entry->writers? CURL_POLL_OUT : 0) |
  2518. (entry->readers ? CURL_POLL_IN : 0);
  2519. /* socket existed before and has the same action set as before */
  2520. if(sincebefore && ((int)entry->action == comboaction))
  2521. /* same, continue */
  2522. continue;
  2523. if(multi->socket_cb) {
  2524. set_in_callback(multi, TRUE);
  2525. rc = multi->socket_cb(data, s, comboaction, multi->socket_userp,
  2526. entry->socketp);
  2527. set_in_callback(multi, FALSE);
  2528. if(rc == -1) {
  2529. multi->dead = TRUE;
  2530. return CURLM_ABORTED_BY_CALLBACK;
  2531. }
  2532. }
  2533. entry->action = comboaction; /* store the current action state */
  2534. }
  2535. num = i; /* number of sockets */
  2536. /* when we've walked over all the sockets we should have right now, we must
  2537. make sure to detect sockets that are removed */
  2538. for(i = 0; i< data->numsocks; i++) {
  2539. int j;
  2540. bool stillused = FALSE;
  2541. s = data->sockets[i];
  2542. for(j = 0; j < num; j++) {
  2543. if(s == socks[j]) {
  2544. /* this is still supervised */
  2545. stillused = TRUE;
  2546. break;
  2547. }
  2548. }
  2549. if(stillused)
  2550. continue;
  2551. entry = sh_getentry(&multi->sockhash, s);
  2552. /* if this is NULL here, the socket has been closed and notified so
  2553. already by Curl_multi_closed() */
  2554. if(entry) {
  2555. unsigned char oldactions = data->actions[i];
  2556. /* this socket has been removed. Decrease user count */
  2557. entry->users--;
  2558. if(oldactions & CURL_POLL_OUT)
  2559. entry->writers--;
  2560. if(oldactions & CURL_POLL_IN)
  2561. entry->readers--;
  2562. if(!entry->users) {
  2563. if(multi->socket_cb) {
  2564. set_in_callback(multi, TRUE);
  2565. rc = multi->socket_cb(data, s, CURL_POLL_REMOVE,
  2566. multi->socket_userp, entry->socketp);
  2567. set_in_callback(multi, FALSE);
  2568. if(rc == -1) {
  2569. multi->dead = TRUE;
  2570. return CURLM_ABORTED_BY_CALLBACK;
  2571. }
  2572. }
  2573. sh_delentry(entry, &multi->sockhash, s);
  2574. }
  2575. else {
  2576. /* still users, but remove this handle as a user of this socket */
  2577. if(Curl_hash_delete(&entry->transfers, (char *)&data,
  2578. sizeof(struct Curl_easy *))) {
  2579. DEBUGASSERT(NULL);
  2580. }
  2581. }
  2582. }
  2583. } /* for loop over numsocks */
  2584. memcpy(data->sockets, socks, num*sizeof(curl_socket_t));
  2585. memcpy(data->actions, actions, num*sizeof(char));
  2586. data->numsocks = num;
  2587. return CURLM_OK;
  2588. }
  2589. CURLcode Curl_updatesocket(struct Curl_easy *data)
  2590. {
  2591. if(singlesocket(data->multi, data))
  2592. return CURLE_ABORTED_BY_CALLBACK;
  2593. return CURLE_OK;
  2594. }
  2595. /*
  2596. * Curl_multi_closed()
  2597. *
  2598. * Used by the connect code to tell the multi_socket code that one of the
  2599. * sockets we were using is about to be closed. This function will then
  2600. * remove it from the sockethash for this handle to make the multi_socket API
  2601. * behave properly, especially for the case when libcurl will create another
  2602. * socket again and it gets the same file descriptor number.
  2603. */
  2604. void Curl_multi_closed(struct Curl_easy *data, curl_socket_t s)
  2605. {
  2606. if(data) {
  2607. /* if there's still an easy handle associated with this connection */
  2608. struct Curl_multi *multi = data->multi;
  2609. if(multi) {
  2610. /* this is set if this connection is part of a handle that is added to
  2611. a multi handle, and only then this is necessary */
  2612. struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);
  2613. if(entry) {
  2614. int rc = 0;
  2615. if(multi->socket_cb) {
  2616. set_in_callback(multi, TRUE);
  2617. rc = multi->socket_cb(data, s, CURL_POLL_REMOVE,
  2618. multi->socket_userp, entry->socketp);
  2619. set_in_callback(multi, FALSE);
  2620. }
  2621. /* now remove it from the socket hash */
  2622. sh_delentry(entry, &multi->sockhash, s);
  2623. if(rc == -1)
  2624. /* This just marks the multi handle as "dead" without returning an
  2625. error code primarily because this function is used from many
  2626. places where propagating an error back is tricky. */
  2627. multi->dead = TRUE;
  2628. }
  2629. }
  2630. }
  2631. }
  2632. /*
  2633. * add_next_timeout()
  2634. *
  2635. * Each Curl_easy has a list of timeouts. The add_next_timeout() is called
  2636. * when it has just been removed from the splay tree because the timeout has
  2637. * expired. This function is then to advance in the list to pick the next
  2638. * timeout to use (skip the already expired ones) and add this node back to
  2639. * the splay tree again.
  2640. *
  2641. * The splay tree only has each sessionhandle as a single node and the nearest
  2642. * timeout is used to sort it on.
  2643. */
  2644. static CURLMcode add_next_timeout(struct curltime now,
  2645. struct Curl_multi *multi,
  2646. struct Curl_easy *d)
  2647. {
  2648. struct curltime *tv = &d->state.expiretime;
  2649. struct Curl_llist *list = &d->state.timeoutlist;
  2650. struct Curl_llist_element *e;
  2651. struct time_node *node = NULL;
  2652. /* move over the timeout list for this specific handle and remove all
  2653. timeouts that are now passed tense and store the next pending
  2654. timeout in *tv */
  2655. for(e = list->head; e;) {
  2656. struct Curl_llist_element *n = e->next;
  2657. timediff_t diff;
  2658. node = (struct time_node *)e->ptr;
  2659. diff = Curl_timediff(node->time, now);
  2660. if(diff <= 0)
  2661. /* remove outdated entry */
  2662. Curl_llist_remove(list, e, NULL);
  2663. else
  2664. /* the list is sorted so get out on the first mismatch */
  2665. break;
  2666. e = n;
  2667. }
  2668. e = list->head;
  2669. if(!e) {
  2670. /* clear the expire times within the handles that we remove from the
  2671. splay tree */
  2672. tv->tv_sec = 0;
  2673. tv->tv_usec = 0;
  2674. }
  2675. else {
  2676. /* copy the first entry to 'tv' */
  2677. memcpy(tv, &node->time, sizeof(*tv));
  2678. /* Insert this node again into the splay. Keep the timer in the list in
  2679. case we need to recompute future timers. */
  2680. multi->timetree = Curl_splayinsert(*tv, multi->timetree,
  2681. &d->state.timenode);
  2682. }
  2683. return CURLM_OK;
  2684. }
  2685. static CURLMcode multi_socket(struct Curl_multi *multi,
  2686. bool checkall,
  2687. curl_socket_t s,
  2688. int ev_bitmask,
  2689. int *running_handles)
  2690. {
  2691. CURLMcode result = CURLM_OK;
  2692. struct Curl_easy *data = NULL;
  2693. struct Curl_tree *t;
  2694. struct curltime now = Curl_now();
  2695. if(checkall) {
  2696. /* *perform() deals with running_handles on its own */
  2697. result = curl_multi_perform(multi, running_handles);
  2698. /* walk through each easy handle and do the socket state change magic
  2699. and callbacks */
  2700. if(result != CURLM_BAD_HANDLE) {
  2701. data = multi->easyp;
  2702. while(data && !result) {
  2703. result = singlesocket(multi, data);
  2704. data = data->next;
  2705. }
  2706. }
  2707. /* or should we fall-through and do the timer-based stuff? */
  2708. return result;
  2709. }
  2710. if(s != CURL_SOCKET_TIMEOUT) {
  2711. struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);
  2712. if(!entry)
  2713. /* Unmatched socket, we can't act on it but we ignore this fact. In
  2714. real-world tests it has been proved that libevent can in fact give
  2715. the application actions even though the socket was just previously
  2716. asked to get removed, so thus we better survive stray socket actions
  2717. and just move on. */
  2718. ;
  2719. else {
  2720. struct Curl_hash_iterator iter;
  2721. struct Curl_hash_element *he;
  2722. /* the socket can be shared by many transfers, iterate */
  2723. Curl_hash_start_iterate(&entry->transfers, &iter);
  2724. for(he = Curl_hash_next_element(&iter); he;
  2725. he = Curl_hash_next_element(&iter)) {
  2726. data = (struct Curl_easy *)he->ptr;
  2727. DEBUGASSERT(data);
  2728. DEBUGASSERT(data->magic == CURLEASY_MAGIC_NUMBER);
  2729. if(data->conn && !(data->conn->handler->flags & PROTOPT_DIRLOCK))
  2730. /* set socket event bitmask if they're not locked */
  2731. data->conn->cselect_bits = ev_bitmask;
  2732. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  2733. }
  2734. /* Now we fall-through and do the timer-based stuff, since we don't want
  2735. to force the user to have to deal with timeouts as long as at least
  2736. one connection in fact has traffic. */
  2737. data = NULL; /* set data to NULL again to avoid calling
  2738. multi_runsingle() in case there's no need to */
  2739. now = Curl_now(); /* get a newer time since the multi_runsingle() loop
  2740. may have taken some time */
  2741. }
  2742. }
  2743. else {
  2744. /* Asked to run due to time-out. Clear the 'lastcall' variable to force
  2745. Curl_update_timer() to trigger a callback to the app again even if the
  2746. same timeout is still the one to run after this call. That handles the
  2747. case when the application asks libcurl to run the timeout
  2748. prematurely. */
  2749. memset(&multi->timer_lastcall, 0, sizeof(multi->timer_lastcall));
  2750. }
  2751. /*
  2752. * The loop following here will go on as long as there are expire-times left
  2753. * to process in the splay and 'data' will be re-assigned for every expired
  2754. * handle we deal with.
  2755. */
  2756. do {
  2757. /* the first loop lap 'data' can be NULL */
  2758. if(data) {
  2759. SIGPIPE_VARIABLE(pipe_st);
  2760. sigpipe_ignore(data, &pipe_st);
  2761. result = multi_runsingle(multi, &now, data);
  2762. sigpipe_restore(&pipe_st);
  2763. if(CURLM_OK >= result) {
  2764. /* get the socket(s) and check if the state has been changed since
  2765. last */
  2766. result = singlesocket(multi, data);
  2767. if(result)
  2768. return result;
  2769. }
  2770. }
  2771. /* Check if there's one (more) expired timer to deal with! This function
  2772. extracts a matching node if there is one */
  2773. multi->timetree = Curl_splaygetbest(now, multi->timetree, &t);
  2774. if(t) {
  2775. data = t->payload; /* assign this for next loop */
  2776. (void)add_next_timeout(now, multi, t->payload);
  2777. }
  2778. } while(t);
  2779. *running_handles = multi->num_alive;
  2780. return result;
  2781. }
  2782. #undef curl_multi_setopt
  2783. CURLMcode curl_multi_setopt(struct Curl_multi *multi,
  2784. CURLMoption option, ...)
  2785. {
  2786. CURLMcode res = CURLM_OK;
  2787. va_list param;
  2788. if(!GOOD_MULTI_HANDLE(multi))
  2789. return CURLM_BAD_HANDLE;
  2790. if(multi->in_callback)
  2791. return CURLM_RECURSIVE_API_CALL;
  2792. va_start(param, option);
  2793. switch(option) {
  2794. case CURLMOPT_SOCKETFUNCTION:
  2795. multi->socket_cb = va_arg(param, curl_socket_callback);
  2796. break;
  2797. case CURLMOPT_SOCKETDATA:
  2798. multi->socket_userp = va_arg(param, void *);
  2799. break;
  2800. case CURLMOPT_PUSHFUNCTION:
  2801. multi->push_cb = va_arg(param, curl_push_callback);
  2802. break;
  2803. case CURLMOPT_PUSHDATA:
  2804. multi->push_userp = va_arg(param, void *);
  2805. break;
  2806. case CURLMOPT_PIPELINING:
  2807. multi->multiplexing = va_arg(param, long) & CURLPIPE_MULTIPLEX;
  2808. break;
  2809. case CURLMOPT_TIMERFUNCTION:
  2810. multi->timer_cb = va_arg(param, curl_multi_timer_callback);
  2811. break;
  2812. case CURLMOPT_TIMERDATA:
  2813. multi->timer_userp = va_arg(param, void *);
  2814. break;
  2815. case CURLMOPT_MAXCONNECTS:
  2816. multi->maxconnects = va_arg(param, long);
  2817. break;
  2818. case CURLMOPT_MAX_HOST_CONNECTIONS:
  2819. multi->max_host_connections = va_arg(param, long);
  2820. break;
  2821. case CURLMOPT_MAX_TOTAL_CONNECTIONS:
  2822. multi->max_total_connections = va_arg(param, long);
  2823. break;
  2824. /* options formerly used for pipelining */
  2825. case CURLMOPT_MAX_PIPELINE_LENGTH:
  2826. break;
  2827. case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE:
  2828. break;
  2829. case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE:
  2830. break;
  2831. case CURLMOPT_PIPELINING_SITE_BL:
  2832. break;
  2833. case CURLMOPT_PIPELINING_SERVER_BL:
  2834. break;
  2835. case CURLMOPT_MAX_CONCURRENT_STREAMS:
  2836. {
  2837. long streams = va_arg(param, long);
  2838. if(streams < 1)
  2839. streams = 100;
  2840. multi->max_concurrent_streams = curlx_sltoui(streams);
  2841. }
  2842. break;
  2843. default:
  2844. res = CURLM_UNKNOWN_OPTION;
  2845. break;
  2846. }
  2847. va_end(param);
  2848. return res;
  2849. }
  2850. /* we define curl_multi_socket() in the public multi.h header */
  2851. #undef curl_multi_socket
  2852. CURLMcode curl_multi_socket(struct Curl_multi *multi, curl_socket_t s,
  2853. int *running_handles)
  2854. {
  2855. CURLMcode result;
  2856. if(multi->in_callback)
  2857. return CURLM_RECURSIVE_API_CALL;
  2858. result = multi_socket(multi, FALSE, s, 0, running_handles);
  2859. if(CURLM_OK >= result)
  2860. result = Curl_update_timer(multi);
  2861. return result;
  2862. }
  2863. CURLMcode curl_multi_socket_action(struct Curl_multi *multi, curl_socket_t s,
  2864. int ev_bitmask, int *running_handles)
  2865. {
  2866. CURLMcode result;
  2867. if(multi->in_callback)
  2868. return CURLM_RECURSIVE_API_CALL;
  2869. result = multi_socket(multi, FALSE, s, ev_bitmask, running_handles);
  2870. if(CURLM_OK >= result)
  2871. result = Curl_update_timer(multi);
  2872. return result;
  2873. }
  2874. CURLMcode curl_multi_socket_all(struct Curl_multi *multi, int *running_handles)
  2875. {
  2876. CURLMcode result;
  2877. if(multi->in_callback)
  2878. return CURLM_RECURSIVE_API_CALL;
  2879. result = multi_socket(multi, TRUE, CURL_SOCKET_BAD, 0, running_handles);
  2880. if(CURLM_OK >= result)
  2881. result = Curl_update_timer(multi);
  2882. return result;
  2883. }
  2884. static CURLMcode multi_timeout(struct Curl_multi *multi,
  2885. long *timeout_ms)
  2886. {
  2887. static const struct curltime tv_zero = {0, 0};
  2888. if(multi->dead) {
  2889. *timeout_ms = 0;
  2890. return CURLM_OK;
  2891. }
  2892. if(multi->timetree) {
  2893. /* we have a tree of expire times */
  2894. struct curltime now = Curl_now();
  2895. /* splay the lowest to the bottom */
  2896. multi->timetree = Curl_splay(tv_zero, multi->timetree);
  2897. if(Curl_splaycomparekeys(multi->timetree->key, now) > 0) {
  2898. /* some time left before expiration */
  2899. timediff_t diff = Curl_timediff(multi->timetree->key, now);
  2900. if(diff <= 0)
  2901. /*
  2902. * Since we only provide millisecond resolution on the returned value
  2903. * and the diff might be less than one millisecond here, we don't
  2904. * return zero as that may cause short bursts of busyloops on fast
  2905. * processors while the diff is still present but less than one
  2906. * millisecond! instead we return 1 until the time is ripe.
  2907. */
  2908. *timeout_ms = 1;
  2909. else
  2910. /* this should be safe even on 64 bit archs, as we don't use that
  2911. overly long timeouts */
  2912. *timeout_ms = (long)diff;
  2913. }
  2914. else
  2915. /* 0 means immediately */
  2916. *timeout_ms = 0;
  2917. }
  2918. else
  2919. *timeout_ms = -1;
  2920. return CURLM_OK;
  2921. }
  2922. CURLMcode curl_multi_timeout(struct Curl_multi *multi,
  2923. long *timeout_ms)
  2924. {
  2925. /* First, make some basic checks that the CURLM handle is a good handle */
  2926. if(!GOOD_MULTI_HANDLE(multi))
  2927. return CURLM_BAD_HANDLE;
  2928. if(multi->in_callback)
  2929. return CURLM_RECURSIVE_API_CALL;
  2930. return multi_timeout(multi, timeout_ms);
  2931. }
  2932. /*
  2933. * Tell the application it should update its timers, if it subscribes to the
  2934. * update timer callback.
  2935. */
  2936. CURLMcode Curl_update_timer(struct Curl_multi *multi)
  2937. {
  2938. long timeout_ms;
  2939. int rc;
  2940. if(!multi->timer_cb || multi->dead)
  2941. return CURLM_OK;
  2942. if(multi_timeout(multi, &timeout_ms)) {
  2943. return CURLM_OK;
  2944. }
  2945. if(timeout_ms < 0) {
  2946. static const struct curltime none = {0, 0};
  2947. if(Curl_splaycomparekeys(none, multi->timer_lastcall)) {
  2948. multi->timer_lastcall = none;
  2949. /* there's no timeout now but there was one previously, tell the app to
  2950. disable it */
  2951. set_in_callback(multi, TRUE);
  2952. rc = multi->timer_cb(multi, -1, multi->timer_userp);
  2953. set_in_callback(multi, FALSE);
  2954. if(rc == -1) {
  2955. multi->dead = TRUE;
  2956. return CURLM_ABORTED_BY_CALLBACK;
  2957. }
  2958. return CURLM_OK;
  2959. }
  2960. return CURLM_OK;
  2961. }
  2962. /* When multi_timeout() is done, multi->timetree points to the node with the
  2963. * timeout we got the (relative) time-out time for. We can thus easily check
  2964. * if this is the same (fixed) time as we got in a previous call and then
  2965. * avoid calling the callback again. */
  2966. if(Curl_splaycomparekeys(multi->timetree->key, multi->timer_lastcall) == 0)
  2967. return CURLM_OK;
  2968. multi->timer_lastcall = multi->timetree->key;
  2969. set_in_callback(multi, TRUE);
  2970. rc = multi->timer_cb(multi, timeout_ms, multi->timer_userp);
  2971. set_in_callback(multi, FALSE);
  2972. if(rc == -1) {
  2973. multi->dead = TRUE;
  2974. return CURLM_ABORTED_BY_CALLBACK;
  2975. }
  2976. return CURLM_OK;
  2977. }
  2978. /*
  2979. * multi_deltimeout()
  2980. *
  2981. * Remove a given timestamp from the list of timeouts.
  2982. */
  2983. static void
  2984. multi_deltimeout(struct Curl_easy *data, expire_id eid)
  2985. {
  2986. struct Curl_llist_element *e;
  2987. struct Curl_llist *timeoutlist = &data->state.timeoutlist;
  2988. /* find and remove the specific node from the list */
  2989. for(e = timeoutlist->head; e; e = e->next) {
  2990. struct time_node *n = (struct time_node *)e->ptr;
  2991. if(n->eid == eid) {
  2992. Curl_llist_remove(timeoutlist, e, NULL);
  2993. return;
  2994. }
  2995. }
  2996. }
  2997. /*
  2998. * multi_addtimeout()
  2999. *
  3000. * Add a timestamp to the list of timeouts. Keep the list sorted so that head
  3001. * of list is always the timeout nearest in time.
  3002. *
  3003. */
  3004. static CURLMcode
  3005. multi_addtimeout(struct Curl_easy *data,
  3006. struct curltime *stamp,
  3007. expire_id eid)
  3008. {
  3009. struct Curl_llist_element *e;
  3010. struct time_node *node;
  3011. struct Curl_llist_element *prev = NULL;
  3012. size_t n;
  3013. struct Curl_llist *timeoutlist = &data->state.timeoutlist;
  3014. node = &data->state.expires[eid];
  3015. /* copy the timestamp and id */
  3016. memcpy(&node->time, stamp, sizeof(*stamp));
  3017. node->eid = eid; /* also marks it as in use */
  3018. n = Curl_llist_count(timeoutlist);
  3019. if(n) {
  3020. /* find the correct spot in the list */
  3021. for(e = timeoutlist->head; e; e = e->next) {
  3022. struct time_node *check = (struct time_node *)e->ptr;
  3023. timediff_t diff = Curl_timediff(check->time, node->time);
  3024. if(diff > 0)
  3025. break;
  3026. prev = e;
  3027. }
  3028. }
  3029. /* else
  3030. this is the first timeout on the list */
  3031. Curl_llist_insert_next(timeoutlist, prev, node, &node->list);
  3032. return CURLM_OK;
  3033. }
  3034. /*
  3035. * Curl_expire()
  3036. *
  3037. * given a number of milliseconds from now to use to set the 'act before
  3038. * this'-time for the transfer, to be extracted by curl_multi_timeout()
  3039. *
  3040. * The timeout will be added to a queue of timeouts if it defines a moment in
  3041. * time that is later than the current head of queue.
  3042. *
  3043. * Expire replaces a former timeout using the same id if already set.
  3044. */
  3045. void Curl_expire(struct Curl_easy *data, timediff_t milli, expire_id id)
  3046. {
  3047. struct Curl_multi *multi = data->multi;
  3048. struct curltime *nowp = &data->state.expiretime;
  3049. struct curltime set;
  3050. /* this is only interesting while there is still an associated multi struct
  3051. remaining! */
  3052. if(!multi)
  3053. return;
  3054. DEBUGASSERT(id < EXPIRE_LAST);
  3055. set = Curl_now();
  3056. set.tv_sec += (time_t)(milli/1000); /* might be a 64 to 32 bit conversion */
  3057. set.tv_usec += (unsigned int)(milli%1000)*1000;
  3058. if(set.tv_usec >= 1000000) {
  3059. set.tv_sec++;
  3060. set.tv_usec -= 1000000;
  3061. }
  3062. /* Remove any timer with the same id just in case. */
  3063. multi_deltimeout(data, id);
  3064. /* Add it to the timer list. It must stay in the list until it has expired
  3065. in case we need to recompute the minimum timer later. */
  3066. multi_addtimeout(data, &set, id);
  3067. if(nowp->tv_sec || nowp->tv_usec) {
  3068. /* This means that the struct is added as a node in the splay tree.
  3069. Compare if the new time is earlier, and only remove-old/add-new if it
  3070. is. */
  3071. timediff_t diff = Curl_timediff(set, *nowp);
  3072. int rc;
  3073. if(diff > 0) {
  3074. /* The current splay tree entry is sooner than this new expiry time.
  3075. We don't need to update our splay tree entry. */
  3076. return;
  3077. }
  3078. /* Since this is an updated time, we must remove the previous entry from
  3079. the splay tree first and then re-add the new value */
  3080. rc = Curl_splayremove(multi->timetree, &data->state.timenode,
  3081. &multi->timetree);
  3082. if(rc)
  3083. infof(data, "Internal error removing splay node = %d", rc);
  3084. }
  3085. /* Indicate that we are in the splay tree and insert the new timer expiry
  3086. value since it is our local minimum. */
  3087. *nowp = set;
  3088. data->state.timenode.payload = data;
  3089. multi->timetree = Curl_splayinsert(*nowp, multi->timetree,
  3090. &data->state.timenode);
  3091. }
  3092. /*
  3093. * Curl_expire_done()
  3094. *
  3095. * Removes the expire timer. Marks it as done.
  3096. *
  3097. */
  3098. void Curl_expire_done(struct Curl_easy *data, expire_id id)
  3099. {
  3100. /* remove the timer, if there */
  3101. multi_deltimeout(data, id);
  3102. }
  3103. /*
  3104. * Curl_expire_clear()
  3105. *
  3106. * Clear ALL timeout values for this handle.
  3107. */
  3108. void Curl_expire_clear(struct Curl_easy *data)
  3109. {
  3110. struct Curl_multi *multi = data->multi;
  3111. struct curltime *nowp = &data->state.expiretime;
  3112. /* this is only interesting while there is still an associated multi struct
  3113. remaining! */
  3114. if(!multi)
  3115. return;
  3116. if(nowp->tv_sec || nowp->tv_usec) {
  3117. /* Since this is an cleared time, we must remove the previous entry from
  3118. the splay tree */
  3119. struct Curl_llist *list = &data->state.timeoutlist;
  3120. int rc;
  3121. rc = Curl_splayremove(multi->timetree, &data->state.timenode,
  3122. &multi->timetree);
  3123. if(rc)
  3124. infof(data, "Internal error clearing splay node = %d", rc);
  3125. /* flush the timeout list too */
  3126. while(list->size > 0) {
  3127. Curl_llist_remove(list, list->tail, NULL);
  3128. }
  3129. #ifdef DEBUGBUILD
  3130. infof(data, "Expire cleared (transfer %p)", data);
  3131. #endif
  3132. nowp->tv_sec = 0;
  3133. nowp->tv_usec = 0;
  3134. }
  3135. }
  3136. CURLMcode curl_multi_assign(struct Curl_multi *multi, curl_socket_t s,
  3137. void *hashp)
  3138. {
  3139. struct Curl_sh_entry *there = NULL;
  3140. there = sh_getentry(&multi->sockhash, s);
  3141. if(!there)
  3142. return CURLM_BAD_SOCKET;
  3143. there->socketp = hashp;
  3144. return CURLM_OK;
  3145. }
  3146. size_t Curl_multi_max_host_connections(struct Curl_multi *multi)
  3147. {
  3148. return multi ? multi->max_host_connections : 0;
  3149. }
  3150. size_t Curl_multi_max_total_connections(struct Curl_multi *multi)
  3151. {
  3152. return multi ? multi->max_total_connections : 0;
  3153. }
  3154. /*
  3155. * When information about a connection has appeared, call this!
  3156. */
  3157. void Curl_multiuse_state(struct Curl_easy *data,
  3158. int bundlestate) /* use BUNDLE_* defines */
  3159. {
  3160. struct connectdata *conn;
  3161. DEBUGASSERT(data);
  3162. DEBUGASSERT(data->multi);
  3163. conn = data->conn;
  3164. DEBUGASSERT(conn);
  3165. DEBUGASSERT(conn->bundle);
  3166. conn->bundle->multiuse = bundlestate;
  3167. process_pending_handles(data->multi);
  3168. }
  3169. static void process_pending_handles(struct Curl_multi *multi)
  3170. {
  3171. struct Curl_llist_element *e = multi->pending.head;
  3172. if(e) {
  3173. struct Curl_easy *data = e->ptr;
  3174. DEBUGASSERT(data->mstate == MSTATE_PENDING);
  3175. multistate(data, MSTATE_CONNECT);
  3176. /* Remove this node from the list */
  3177. Curl_llist_remove(&multi->pending, e, NULL);
  3178. /* Make sure that the handle will be processed soonish. */
  3179. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  3180. /* mark this as having been in the pending queue */
  3181. data->state.previouslypending = TRUE;
  3182. }
  3183. }
  3184. void Curl_set_in_callback(struct Curl_easy *data, bool value)
  3185. {
  3186. /* might get called when there is no data pointer! */
  3187. if(data) {
  3188. if(data->multi_easy)
  3189. data->multi_easy->in_callback = value;
  3190. else if(data->multi)
  3191. data->multi->in_callback = value;
  3192. }
  3193. }
  3194. bool Curl_is_in_callback(struct Curl_easy *easy)
  3195. {
  3196. return ((easy->multi && easy->multi->in_callback) ||
  3197. (easy->multi_easy && easy->multi_easy->in_callback));
  3198. }
  3199. #ifdef DEBUGBUILD
  3200. void Curl_multi_dump(struct Curl_multi *multi)
  3201. {
  3202. struct Curl_easy *data;
  3203. int i;
  3204. fprintf(stderr, "* Multi status: %d handles, %d alive\n",
  3205. multi->num_easy, multi->num_alive);
  3206. for(data = multi->easyp; data; data = data->next) {
  3207. if(data->mstate < MSTATE_COMPLETED) {
  3208. /* only display handles that are not completed */
  3209. fprintf(stderr, "handle %p, state %s, %d sockets\n",
  3210. (void *)data,
  3211. statename[data->mstate], data->numsocks);
  3212. for(i = 0; i < data->numsocks; i++) {
  3213. curl_socket_t s = data->sockets[i];
  3214. struct Curl_sh_entry *entry = sh_getentry(&multi->sockhash, s);
  3215. fprintf(stderr, "%d ", (int)s);
  3216. if(!entry) {
  3217. fprintf(stderr, "INTERNAL CONFUSION\n");
  3218. continue;
  3219. }
  3220. fprintf(stderr, "[%s %s] ",
  3221. (entry->action&CURL_POLL_IN)?"RECVING":"",
  3222. (entry->action&CURL_POLL_OUT)?"SENDING":"");
  3223. }
  3224. if(data->numsocks)
  3225. fprintf(stderr, "\n");
  3226. }
  3227. }
  3228. }
  3229. #endif
  3230. unsigned int Curl_multi_max_concurrent_streams(struct Curl_multi *multi)
  3231. {
  3232. DEBUGASSERT(multi);
  3233. return multi->max_concurrent_streams;
  3234. }