multi.c 97 KB

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