2
0

multi.c 112 KB

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