2
0

multi.c 112 KB

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