multi.c 98 KB

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