multi.c 113 KB

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