multi.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifdef HAVE_SYS_SOCKET_H
  25. #include <sys/socket.h>
  26. #endif
  27. #ifdef HAVE_UNISTD_H
  28. #include <unistd.h>
  29. #endif
  30. #include <curl/curl.h>
  31. #include "urldata.h"
  32. #include "transfer.h"
  33. #include "url.h"
  34. #include "connect.h"
  35. #include "progress.h"
  36. #include "easyif.h"
  37. #include "multiif.h"
  38. #include "sendf.h"
  39. #include "timeval.h"
  40. #include "http.h"
  41. #define _MPRINTF_REPLACE /* use our functions only */
  42. #include <curl/mprintf.h>
  43. #include "curl_memory.h"
  44. /* The last #include file should be: */
  45. #include "memdebug.h"
  46. /*
  47. CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97
  48. to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every
  49. CURL handle takes 45-50 K memory, therefore this 3K are not significant.
  50. */
  51. #ifndef CURL_SOCKET_HASH_TABLE_SIZE
  52. #define CURL_SOCKET_HASH_TABLE_SIZE 911
  53. #endif
  54. struct Curl_message {
  55. /* the 'CURLMsg' is the part that is visible to the external user */
  56. struct CURLMsg extmsg;
  57. struct Curl_message *next;
  58. };
  59. /* NOTE: if you add a state here, add the name to the statename[] array as
  60. well!
  61. */
  62. typedef enum {
  63. CURLM_STATE_INIT, /* start in this state */
  64. CURLM_STATE_CONNECT, /* resolve/connect has been sent off */
  65. CURLM_STATE_WAITRESOLVE, /* awaiting the resolve to finalize */
  66. CURLM_STATE_WAITCONNECT, /* awaiting the connect to finalize */
  67. CURLM_STATE_WAITPROXYCONNECT, /* awaiting proxy CONNECT to finalize */
  68. CURLM_STATE_PROTOCONNECT, /* completing the protocol-specific connect phase */
  69. CURLM_STATE_WAITDO, /* wait for our turn to send the request */
  70. CURLM_STATE_DO, /* start send off the request (part 1) */
  71. CURLM_STATE_DOING, /* sending off the request (part 1) */
  72. CURLM_STATE_DO_MORE, /* send off the request (part 2) */
  73. CURLM_STATE_DO_DONE, /* done sending off request */
  74. CURLM_STATE_WAITPERFORM, /* wait for our turn to read the response */
  75. CURLM_STATE_PERFORM, /* transfer data */
  76. CURLM_STATE_TOOFAST, /* wait because limit-rate exceeded */
  77. CURLM_STATE_DONE, /* post data transfer operation */
  78. CURLM_STATE_COMPLETED, /* operation complete */
  79. CURLM_STATE_LAST /* not a true state, never use this */
  80. } CURLMstate;
  81. /* we support N sockets per easy handle. Set the corresponding bit to what
  82. action we should wait for */
  83. #define MAX_SOCKSPEREASYHANDLE 5
  84. #define GETSOCK_READABLE (0x00ff)
  85. #define GETSOCK_WRITABLE (0xff00)
  86. struct closure {
  87. struct closure *next; /* a simple one-way list of structs */
  88. struct SessionHandle *easy_handle;
  89. };
  90. struct Curl_one_easy {
  91. /* first, two fields for the linked list of these */
  92. struct Curl_one_easy *next;
  93. struct Curl_one_easy *prev;
  94. struct SessionHandle *easy_handle; /* the easy handle for this unit */
  95. struct connectdata *easy_conn; /* the "unit's" connection */
  96. CURLMstate state; /* the handle's state */
  97. CURLcode result; /* previous result */
  98. struct Curl_message *msg; /* A pointer to one single posted message.
  99. Cleanup should be done on this pointer NOT on
  100. the linked list in Curl_multi. This message
  101. will be deleted when this handle is removed
  102. from the multi-handle */
  103. int msg_num; /* number of messages left in 'msg' to return */
  104. /* Array with the plain socket numbers this handle takes care of, in no
  105. particular order. Note that all sockets are added to the sockhash, where
  106. the state etc are also kept. This array is mostly used to detect when a
  107. socket is to be removed from the hash. See singlesocket(). */
  108. curl_socket_t sockets[MAX_SOCKSPEREASYHANDLE];
  109. int numsocks;
  110. };
  111. #define CURL_MULTI_HANDLE 0x000bab1e
  112. #define GOOD_MULTI_HANDLE(x) \
  113. ((x)&&(((struct Curl_multi *)(x))->type == CURL_MULTI_HANDLE))
  114. #define GOOD_EASY_HANDLE(x) \
  115. (((struct SessionHandle *)(x))->magic == CURLEASY_MAGIC_NUMBER)
  116. /* This is the struct known as CURLM on the outside */
  117. struct Curl_multi {
  118. /* First a simple identifier to easier detect if a user mix up
  119. this multi handle with an easy handle. Set this to CURL_MULTI_HANDLE. */
  120. long type;
  121. /* We have a linked list with easy handles */
  122. struct Curl_one_easy easy;
  123. int num_easy; /* amount of entries in the linked list above. */
  124. int num_msgs; /* amount of messages in the easy handles */
  125. int num_alive; /* amount of easy handles that are added but have not yet
  126. reached COMPLETE state */
  127. /* callback function and user data pointer for the *socket() API */
  128. curl_socket_callback socket_cb;
  129. void *socket_userp;
  130. /* Hostname cache */
  131. struct curl_hash *hostcache;
  132. /* timetree points to the splay-tree of time nodes to figure out expire
  133. times of all currently set timers */
  134. struct Curl_tree *timetree;
  135. /* 'sockhash' is the lookup hash for socket descriptor => easy handles (note
  136. the pluralis form, there can be more than one easy handle waiting on the
  137. same actual socket) */
  138. struct curl_hash *sockhash;
  139. /* Whether pipelining is enabled for this multi handle */
  140. bool pipelining_enabled;
  141. /* shared connection cache */
  142. struct conncache *connc;
  143. long maxconnects; /* if >0, a fixed limit of the maximum number of entries
  144. we're allowed to grow the connection cache to */
  145. /* list of easy handles kept around for doing nice connection closures */
  146. struct closure *closure;
  147. /* timer callback and user data pointer for the *socket() API */
  148. curl_multi_timer_callback timer_cb;
  149. void *timer_userp;
  150. struct timeval timer_lastcall; /* the fixed time for the timeout for the
  151. previous callback */
  152. };
  153. static struct connectdata *conn_using(struct Curl_multi *multi,
  154. struct SessionHandle *data);
  155. static void singlesocket(struct Curl_multi *multi,
  156. struct Curl_one_easy *easy);
  157. static void add_closure(struct Curl_multi *multi,
  158. struct SessionHandle *data);
  159. static int update_timer(struct Curl_multi *multi);
  160. static CURLcode addHandleToSendOrPendPipeline(struct SessionHandle *handle,
  161. struct connectdata *conn);
  162. static int checkPendPipeline(struct connectdata *conn);
  163. static void moveHandleFromSendToRecvPipeline(struct SessionHandle *handle,
  164. struct connectdata *conn);
  165. static void moveHandleFromRecvToDonePipeline(struct SessionHandle *handle,
  166. struct connectdata *conn);
  167. static bool isHandleAtHead(struct SessionHandle *handle,
  168. struct curl_llist *pipeline);
  169. #ifdef DEBUGBUILD
  170. static const char * const statename[]={
  171. "INIT",
  172. "CONNECT",
  173. "WAITRESOLVE",
  174. "WAITCONNECT",
  175. "WAITPROXYCONNECT",
  176. "PROTOCONNECT",
  177. "WAITDO",
  178. "DO",
  179. "DOING",
  180. "DO_MORE",
  181. "DO_DONE",
  182. "WAITPERFORM",
  183. "PERFORM",
  184. "TOOFAST",
  185. "DONE",
  186. "COMPLETED",
  187. };
  188. #endif
  189. /* always use this function to change state, to make debugging easier */
  190. static void multistate(struct Curl_one_easy *easy, CURLMstate state)
  191. {
  192. #ifdef DEBUGBUILD
  193. long connectindex = -5000;
  194. #endif
  195. CURLMstate oldstate = easy->state;
  196. if(oldstate == state)
  197. /* don't bother when the new state is the same as the old state */
  198. return;
  199. easy->state = state;
  200. #ifdef DEBUGBUILD
  201. if(easy->easy_conn) {
  202. if(easy->state > CURLM_STATE_CONNECT &&
  203. easy->state < CURLM_STATE_COMPLETED)
  204. connectindex = easy->easy_conn->connectindex;
  205. infof(easy->easy_handle,
  206. "STATE: %s => %s handle %p; (connection #%ld) \n",
  207. statename[oldstate], statename[easy->state],
  208. (char *)easy, connectindex);
  209. }
  210. #endif
  211. if(state == CURLM_STATE_COMPLETED)
  212. /* changing to COMPLETED means there's one less easy handle 'alive' */
  213. easy->easy_handle->multi->num_alive--;
  214. }
  215. /*
  216. * We add one of these structs to the sockhash for a particular socket
  217. */
  218. struct Curl_sh_entry {
  219. struct SessionHandle *easy;
  220. time_t timestamp;
  221. int action; /* what action READ/WRITE this socket waits for */
  222. curl_socket_t socket; /* mainly to ease debugging */
  223. void *socketp; /* settable by users with curl_multi_assign() */
  224. };
  225. /* bits for 'action' having no bits means this socket is not expecting any
  226. action */
  227. #define SH_READ 1
  228. #define SH_WRITE 2
  229. /* make sure this socket is present in the hash for this handle */
  230. static struct Curl_sh_entry *sh_addentry(struct curl_hash *sh,
  231. curl_socket_t s,
  232. struct SessionHandle *data)
  233. {
  234. struct Curl_sh_entry *there =
  235. Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
  236. struct Curl_sh_entry *check;
  237. if(there)
  238. /* it is present, return fine */
  239. return there;
  240. /* not present, add it */
  241. check = calloc(1, sizeof(struct Curl_sh_entry));
  242. if(!check)
  243. return NULL; /* major failure */
  244. check->easy = data;
  245. check->socket = s;
  246. /* make/add new hash entry */
  247. if(NULL == Curl_hash_add(sh, (char *)&s, sizeof(curl_socket_t), check)) {
  248. free(check);
  249. return NULL; /* major failure */
  250. }
  251. return check; /* things are good in sockhash land */
  252. }
  253. /* delete the given socket + handle from the hash */
  254. static void sh_delentry(struct curl_hash *sh, curl_socket_t s)
  255. {
  256. struct Curl_sh_entry *there =
  257. Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
  258. if(there) {
  259. /* this socket is in the hash */
  260. /* We remove the hash entry. (This'll end up in a call to
  261. sh_freeentry().) */
  262. Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t));
  263. }
  264. }
  265. /*
  266. * free a sockhash entry
  267. */
  268. static void sh_freeentry(void *freethis)
  269. {
  270. struct Curl_sh_entry *p = (struct Curl_sh_entry *) freethis;
  271. if(p)
  272. free(p);
  273. }
  274. static size_t fd_key_compare(void*k1, size_t k1_len, void*k2, size_t k2_len)
  275. {
  276. (void) k1_len; (void) k2_len;
  277. return (*((int* ) k1)) == (*((int* ) k2));
  278. }
  279. static size_t hash_fd(void* key, size_t key_length, size_t slots_num)
  280. {
  281. int fd = * ((int* ) key);
  282. (void) key_length;
  283. return (fd % (int)slots_num);
  284. }
  285. /*
  286. * sh_init() creates a new socket hash and returns the handle for it.
  287. *
  288. * Quote from README.multi_socket:
  289. *
  290. * "Some tests at 7000 and 9000 connections showed that the socket hash lookup
  291. * is somewhat of a bottle neck. Its current implementation may be a bit too
  292. * limiting. It simply has a fixed-size array, and on each entry in the array
  293. * it has a linked list with entries. So the hash only checks which list to
  294. * scan through. The code I had used so for used a list with merely 7 slots
  295. * (as that is what the DNS hash uses) but with 7000 connections that would
  296. * make an average of 1000 nodes in each list to run through. I upped that to
  297. * 97 slots (I believe a prime is suitable) and noticed a significant speed
  298. * increase. I need to reconsider the hash implementation or use a rather
  299. * large default value like this. At 9000 connections I was still below 10us
  300. * per call."
  301. *
  302. */
  303. static struct curl_hash *sh_init(void)
  304. {
  305. return Curl_hash_alloc(CURL_SOCKET_HASH_TABLE_SIZE, hash_fd, fd_key_compare,
  306. sh_freeentry);
  307. }
  308. CURLM *curl_multi_init(void)
  309. {
  310. struct Curl_multi *multi = calloc(1, sizeof(struct Curl_multi));
  311. if(!multi)
  312. return NULL;
  313. multi->type = CURL_MULTI_HANDLE;
  314. multi->hostcache = Curl_mk_dnscache();
  315. if(!multi->hostcache) {
  316. /* failure, free mem and bail out */
  317. free(multi);
  318. return NULL;
  319. }
  320. multi->sockhash = sh_init();
  321. if(!multi->sockhash) {
  322. /* failure, free mem and bail out */
  323. Curl_hash_destroy(multi->hostcache);
  324. free(multi);
  325. return NULL;
  326. }
  327. multi->connc = Curl_mk_connc(CONNCACHE_MULTI, -1L);
  328. if(!multi->connc) {
  329. Curl_hash_destroy(multi->sockhash);
  330. Curl_hash_destroy(multi->hostcache);
  331. free(multi);
  332. return NULL;
  333. }
  334. /* Let's make the doubly-linked list a circular list. This makes
  335. the linked list code simpler and allows inserting at the end
  336. with less work (we didn't keep a tail pointer before). */
  337. multi->easy.next = &multi->easy;
  338. multi->easy.prev = &multi->easy;
  339. return (CURLM *) multi;
  340. }
  341. CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  342. CURL *easy_handle)
  343. {
  344. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  345. struct Curl_one_easy *easy;
  346. struct closure *cl;
  347. struct closure *prev=NULL;
  348. /* First, make some basic checks that the CURLM handle is a good handle */
  349. if(!GOOD_MULTI_HANDLE(multi))
  350. return CURLM_BAD_HANDLE;
  351. /* Verify that we got a somewhat good easy handle too */
  352. if(!GOOD_EASY_HANDLE(easy_handle))
  353. return CURLM_BAD_EASY_HANDLE;
  354. /* Prevent users to add the same handle more than once! */
  355. if(((struct SessionHandle *)easy_handle)->multi)
  356. /* possibly we should create a new unique error code for this condition */
  357. return CURLM_BAD_EASY_HANDLE;
  358. /* Now, time to add an easy handle to the multi stack */
  359. easy = calloc(1, sizeof(struct Curl_one_easy));
  360. if(!easy)
  361. return CURLM_OUT_OF_MEMORY;
  362. cl = multi->closure;
  363. while(cl) {
  364. struct closure *next = cl->next;
  365. if(cl->easy_handle == (struct SessionHandle *)easy_handle) {
  366. /* remove this handle from the closure list */
  367. free(cl);
  368. if(prev)
  369. prev->next = next;
  370. else
  371. multi->closure = next;
  372. break; /* no need to continue since this handle can only be present once
  373. in the list */
  374. }
  375. prev = cl;
  376. cl = next;
  377. }
  378. /* set the easy handle */
  379. easy->easy_handle = easy_handle;
  380. multistate(easy, CURLM_STATE_INIT);
  381. /* set the back pointer to one_easy to assist in removal */
  382. easy->easy_handle->multi_pos = easy;
  383. /* for multi interface connections, we share DNS cache automatically if the
  384. easy handle's one is currently private. */
  385. if(easy->easy_handle->dns.hostcache &&
  386. (easy->easy_handle->dns.hostcachetype == HCACHE_PRIVATE)) {
  387. Curl_hash_destroy(easy->easy_handle->dns.hostcache);
  388. easy->easy_handle->dns.hostcache = NULL;
  389. easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
  390. }
  391. if(!easy->easy_handle->dns.hostcache ||
  392. (easy->easy_handle->dns.hostcachetype == HCACHE_NONE)) {
  393. easy->easy_handle->dns.hostcache = multi->hostcache;
  394. easy->easy_handle->dns.hostcachetype = HCACHE_MULTI;
  395. }
  396. if(easy->easy_handle->state.connc) {
  397. if(easy->easy_handle->state.connc->type == CONNCACHE_PRIVATE) {
  398. /* kill old private version */
  399. Curl_rm_connc(easy->easy_handle->state.connc);
  400. /* point out our shared one instead */
  401. easy->easy_handle->state.connc = multi->connc;
  402. }
  403. /* else it is already using multi? */
  404. }
  405. else
  406. /* point out our shared one */
  407. easy->easy_handle->state.connc = multi->connc;
  408. /* Make sure the type is setup correctly */
  409. easy->easy_handle->state.connc->type = CONNCACHE_MULTI;
  410. /* This adds the new entry at the back of the list
  411. to try and maintain a FIFO queue so the pipelined
  412. requests are in order. */
  413. /* We add this new entry last in the list. We make our 'next' point to the
  414. 'first' struct and our 'prev' point to the previous 'prev' */
  415. easy->next = &multi->easy;
  416. easy->prev = multi->easy.prev;
  417. /* make 'easy' the last node in the chain */
  418. multi->easy.prev = easy;
  419. /* if there was a prev node, make sure its 'next' pointer links to
  420. the new node */
  421. easy->prev->next = easy;
  422. Curl_easy_addmulti(easy_handle, multi_handle);
  423. /* make the SessionHandle struct refer back to this struct */
  424. easy->easy_handle->set.one_easy = easy;
  425. /* Set the timeout for this handle to expire really soon so that it will
  426. be taken care of even when this handle is added in the midst of operation
  427. when only the curl_multi_socket() API is used. During that flow, only
  428. sockets that time-out or have actions will be dealt with. Since this
  429. handle has no action yet, we make sure it times out to get things to
  430. happen. */
  431. Curl_expire(easy->easy_handle, 1);
  432. /* increase the node-counter */
  433. multi->num_easy++;
  434. if((multi->num_easy * 4) > multi->connc->num) {
  435. /* We want the connection cache to have plenty room. Before we supported
  436. the shared cache every single easy handle had 5 entries in their cache
  437. by default. */
  438. long newmax = multi->num_easy * 4;
  439. if(multi->maxconnects && (multi->maxconnects < newmax))
  440. /* don't grow beyond the allowed size */
  441. newmax = multi->maxconnects;
  442. if(newmax > multi->connc->num) {
  443. /* we only do this is we can in fact grow the cache */
  444. CURLcode res = Curl_ch_connc(easy_handle, multi->connc, newmax);
  445. if(res != CURLE_OK) {
  446. /* FIXME: may need to do more cleanup here */
  447. curl_multi_remove_handle(multi_handle, easy_handle);
  448. return CURLM_OUT_OF_MEMORY;
  449. }
  450. }
  451. }
  452. /* increase the alive-counter */
  453. multi->num_alive++;
  454. /* A somewhat crude work-around for a little glitch in update_timer() that
  455. happens if the lastcall time is set to the same time when the handle is
  456. removed as when the next handle is added, as then the check in
  457. update_timer() that prevents calling the application multiple times with
  458. the same timer infor will not trigger and then the new handle's timeout
  459. will not be notified to the app.
  460. The work-around is thus simply to clear the 'lastcall' variable to force
  461. update_timer() to always trigger a callback to the app when a new easy
  462. handle is added */
  463. memset(&multi->timer_lastcall, 0, sizeof(multi->timer_lastcall));
  464. update_timer(multi);
  465. return CURLM_OK;
  466. }
  467. #if 0
  468. /* Debug-function, used like this:
  469. *
  470. * Curl_hash_print(multi->sockhash, debug_print_sock_hash);
  471. *
  472. * Enable the hash print function first by editing hash.c
  473. */
  474. static void debug_print_sock_hash(void *p)
  475. {
  476. struct Curl_sh_entry *sh = (struct Curl_sh_entry *)p;
  477. fprintf(stderr, " [easy %p/magic %x/socket %d]",
  478. (void *)sh->easy, sh->easy->magic, sh->socket);
  479. }
  480. #endif
  481. CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
  482. CURL *curl_handle)
  483. {
  484. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  485. struct Curl_one_easy *easy;
  486. struct connectdata *conn;
  487. /* First, make some basic checks that the CURLM handle is a good handle */
  488. if(!GOOD_MULTI_HANDLE(multi))
  489. return CURLM_BAD_HANDLE;
  490. /* Verify that we got a somewhat good easy handle too */
  491. if(!GOOD_EASY_HANDLE(curl_handle))
  492. return CURLM_BAD_EASY_HANDLE;
  493. /* pick-up from the 'curl_handle' the kept position in the list */
  494. easy = ((struct SessionHandle *)curl_handle)->multi_pos;
  495. if(easy) {
  496. bool premature = (bool)(easy->state != CURLM_STATE_COMPLETED);
  497. bool easy_owns_conn = (bool)(easy->easy_conn &&
  498. (easy->easy_conn->data == easy->easy_handle));
  499. /* If the 'state' is not INIT or COMPLETED, we might need to do something
  500. nice to put the easy_handle in a good known state when this returns. */
  501. if(premature)
  502. /* this handle is "alive" so we need to count down the total number of
  503. alive connections when this is removed */
  504. multi->num_alive--;
  505. if(easy->easy_conn &&
  506. (easy->easy_conn->send_pipe->size +
  507. easy->easy_conn->recv_pipe->size > 1) &&
  508. easy->state > CURLM_STATE_WAITDO &&
  509. easy->state < CURLM_STATE_COMPLETED) {
  510. /* If the handle is in a pipeline and has started sending off its
  511. request but not received its reponse yet, we need to close
  512. connection. */
  513. easy->easy_conn->bits.close = TRUE;
  514. /* Set connection owner so that Curl_done() closes it.
  515. We can sefely do this here since connection is killed. */
  516. easy->easy_conn->data = easy->easy_handle;
  517. }
  518. /* The timer must be shut down before easy->multi is set to NULL,
  519. else the timenode will remain in the splay tree after
  520. curl_easy_cleanup is called. */
  521. Curl_expire(easy->easy_handle, 0);
  522. if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
  523. /* clear out the usage of the shared DNS cache */
  524. easy->easy_handle->dns.hostcache = NULL;
  525. easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
  526. }
  527. if(easy->easy_conn) {
  528. /* we must call Curl_done() here (if we still "own it") so that we don't
  529. leave a half-baked one around */
  530. if (easy_owns_conn) {
  531. /* Curl_done() clears the conn->data field to lose the association
  532. between the easy handle and the connection
  533. Note that this ignores the return code simply because there's
  534. nothing really useful to do with it anyway! */
  535. (void)Curl_done(&easy->easy_conn, easy->result, premature);
  536. if(easy->easy_conn)
  537. /* the connection is still alive, set back the association to enable
  538. the check below to trigger TRUE */
  539. easy->easy_conn->data = easy->easy_handle;
  540. }
  541. else
  542. /* Clear connection pipelines, if Curl_done above was not called */
  543. Curl_getoff_all_pipelines(easy->easy_handle, easy->easy_conn);
  544. }
  545. /* figure out if the easy handle is used by a connection in the cache */
  546. conn = conn_using(multi, easy->easy_handle);
  547. /* If this easy_handle was the last one in charge for one or more
  548. connections in the shared connection cache, we might need to keep this
  549. handle around until either A) the connection is closed and killed
  550. properly, or B) another easy_handle uses the connection.
  551. The reason why we need to have a easy_handle associated with a live
  552. connection is simply that some connections will need a handle to get
  553. closed down properly. Currently, the only connections that need to keep
  554. a easy_handle handle around are using FTP(S). Such connections have
  555. the PROT_CLOSEACTION bit set.
  556. Thus, we need to check for all connections in the shared cache that
  557. points to this handle and are using PROT_CLOSEACTION. If there's any,
  558. we need to add this handle to the list of "easy handles kept around for
  559. nice connection closures".
  560. */
  561. if(conn) {
  562. if(conn->protocol & PROT_CLOSEACTION) {
  563. /* There's at least one CLOSEACTION connection using this handle so we
  564. must keep this handle around. We also keep the connection cache
  565. pointer pointing to the shared one since that will be used on close
  566. as well. */
  567. easy->easy_handle->state.shared_conn = multi;
  568. /* this handle is still being used by a shared connection cache and
  569. thus we leave it around for now */
  570. add_closure(multi, easy->easy_handle);
  571. }
  572. else
  573. /* disconect the easy handle from the connection since the connection
  574. will now remain but this easy handle is going */
  575. conn->data = NULL;
  576. }
  577. if(easy->easy_handle->state.connc->type == CONNCACHE_MULTI) {
  578. /* if this was using the shared connection cache we clear the pointer
  579. to that since we're not part of that handle anymore */
  580. easy->easy_handle->state.connc = NULL;
  581. /* Modify the connectindex since this handle can't point to the
  582. connection cache anymore.
  583. TODO: consider if this is really what we want. The connection cache
  584. is within the multi handle and that owns the connections so we should
  585. not need to touch connections like this when we just remove an easy
  586. handle...
  587. */
  588. if(easy->easy_conn && easy_owns_conn &&
  589. (easy->easy_conn->send_pipe->size +
  590. easy->easy_conn->recv_pipe->size == 0))
  591. easy->easy_conn->connectindex = -1;
  592. }
  593. /* change state without using multistate(), only to make singlesocket() do
  594. what we want */
  595. easy->state = CURLM_STATE_COMPLETED;
  596. singlesocket(multi, easy); /* to let the application know what sockets
  597. that vanish with this handle */
  598. Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association
  599. to this multi handle */
  600. /* make the previous node point to our next */
  601. if(easy->prev)
  602. easy->prev->next = easy->next;
  603. /* make our next point to our previous node */
  604. if(easy->next)
  605. easy->next->prev = easy->prev;
  606. easy->easy_handle->set.one_easy = NULL; /* detached */
  607. /* Null the position in the controlling structure */
  608. easy->easy_handle->multi_pos = NULL;
  609. /* NOTE NOTE NOTE
  610. We do not touch the easy handle here! */
  611. if(easy->msg)
  612. free(easy->msg);
  613. free(easy);
  614. multi->num_easy--; /* one less to care about now */
  615. update_timer(multi);
  616. return CURLM_OK;
  617. }
  618. else
  619. return CURLM_BAD_EASY_HANDLE; /* twasn't found */
  620. }
  621. bool Curl_multi_canPipeline(const struct Curl_multi* multi)
  622. {
  623. return multi->pipelining_enabled;
  624. }
  625. void Curl_multi_handlePipeBreak(struct SessionHandle *data)
  626. {
  627. struct Curl_one_easy *one_easy = data->set.one_easy;
  628. if(one_easy)
  629. one_easy->easy_conn = NULL;
  630. }
  631. static int waitconnect_getsock(struct connectdata *conn,
  632. curl_socket_t *sock,
  633. int numsocks)
  634. {
  635. if(!numsocks)
  636. return GETSOCK_BLANK;
  637. sock[0] = conn->sock[FIRSTSOCKET];
  638. /* when we've sent a CONNECT to a proxy, we should rather wait for the
  639. socket to become readable to be able to get the response headers */
  640. if(conn->bits.tunnel_connecting)
  641. return GETSOCK_READSOCK(0);
  642. return GETSOCK_WRITESOCK(0);
  643. }
  644. static int domore_getsock(struct connectdata *conn,
  645. curl_socket_t *sock,
  646. int numsocks)
  647. {
  648. if(!numsocks)
  649. return GETSOCK_BLANK;
  650. /* When in DO_MORE state, we could be either waiting for us
  651. to connect to a remote site, or we could wait for that site
  652. to connect to us. It makes a difference in the way: if we
  653. connect to the site we wait for the socket to become writable, if
  654. the site connects to us we wait for it to become readable */
  655. sock[0] = conn->sock[SECONDARYSOCKET];
  656. return GETSOCK_WRITESOCK(0);
  657. }
  658. /* returns bitmapped flags for this handle and its sockets */
  659. static int multi_getsock(struct Curl_one_easy *easy,
  660. curl_socket_t *socks, /* points to numsocks number
  661. of sockets */
  662. int numsocks)
  663. {
  664. /* If the pipe broke, or if there's no connection left for this easy handle,
  665. then we MUST bail out now with no bitmask set. The no connection case can
  666. happen when this is called from curl_multi_remove_handle() =>
  667. singlesocket() => multi_getsock().
  668. */
  669. if(easy->easy_handle->state.pipe_broke || !easy->easy_conn)
  670. return 0;
  671. if(easy->state > CURLM_STATE_CONNECT &&
  672. easy->state < CURLM_STATE_COMPLETED) {
  673. /* Set up ownership correctly */
  674. easy->easy_conn->data = easy->easy_handle;
  675. }
  676. switch(easy->state) {
  677. default:
  678. #if 0 /* switch back on these cases to get the compiler to check for all enums
  679. to be present */
  680. case CURLM_STATE_TOOFAST: /* returns 0, so will not select. */
  681. case CURLM_STATE_COMPLETED:
  682. case CURLM_STATE_INIT:
  683. case CURLM_STATE_CONNECT:
  684. case CURLM_STATE_WAITDO:
  685. case CURLM_STATE_DONE:
  686. case CURLM_STATE_LAST:
  687. /* this will get called with CURLM_STATE_COMPLETED when a handle is
  688. removed */
  689. #endif
  690. return 0;
  691. case CURLM_STATE_WAITRESOLVE:
  692. return Curl_resolv_getsock(easy->easy_conn, socks, numsocks);
  693. case CURLM_STATE_PROTOCONNECT:
  694. return Curl_protocol_getsock(easy->easy_conn, socks, numsocks);
  695. case CURLM_STATE_DO:
  696. case CURLM_STATE_DOING:
  697. return Curl_doing_getsock(easy->easy_conn, socks, numsocks);
  698. case CURLM_STATE_WAITPROXYCONNECT:
  699. case CURLM_STATE_WAITCONNECT:
  700. return waitconnect_getsock(easy->easy_conn, socks, numsocks);
  701. case CURLM_STATE_DO_MORE:
  702. return domore_getsock(easy->easy_conn, socks, numsocks);
  703. case CURLM_STATE_DO_DONE: /* since is set after DO is completed, we switch
  704. to waiting for the same as the *PERFORM states */
  705. case CURLM_STATE_PERFORM:
  706. case CURLM_STATE_WAITPERFORM:
  707. return Curl_single_getsock(easy->easy_conn, socks, numsocks);
  708. }
  709. }
  710. CURLMcode curl_multi_fdset(CURLM *multi_handle,
  711. fd_set *read_fd_set, fd_set *write_fd_set,
  712. fd_set *exc_fd_set, int *max_fd)
  713. {
  714. /* Scan through all the easy handles to get the file descriptors set.
  715. Some easy handles may not have connected to the remote host yet,
  716. and then we must make sure that is done. */
  717. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  718. struct Curl_one_easy *easy;
  719. int this_max_fd=-1;
  720. curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
  721. int bitmap;
  722. int i;
  723. (void)exc_fd_set; /* not used */
  724. if(!GOOD_MULTI_HANDLE(multi))
  725. return CURLM_BAD_HANDLE;
  726. easy=multi->easy.next;
  727. while(easy != &multi->easy) {
  728. bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);
  729. for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
  730. curl_socket_t s = CURL_SOCKET_BAD;
  731. if(bitmap & GETSOCK_READSOCK(i)) {
  732. FD_SET(sockbunch[i], read_fd_set);
  733. s = sockbunch[i];
  734. }
  735. if(bitmap & GETSOCK_WRITESOCK(i)) {
  736. FD_SET(sockbunch[i], write_fd_set);
  737. s = sockbunch[i];
  738. }
  739. if(s == CURL_SOCKET_BAD)
  740. /* this socket is unused, break out of loop */
  741. break;
  742. else {
  743. if((int)s > this_max_fd)
  744. this_max_fd = (int)s;
  745. }
  746. }
  747. easy = easy->next; /* check next handle */
  748. }
  749. *max_fd = this_max_fd;
  750. return CURLM_OK;
  751. }
  752. static CURLMcode multi_runsingle(struct Curl_multi *multi,
  753. struct Curl_one_easy *easy)
  754. {
  755. struct Curl_message *msg = NULL;
  756. bool connected;
  757. bool async;
  758. bool protocol_connect = FALSE;
  759. bool dophase_done;
  760. bool done = FALSE;
  761. CURLMcode result = CURLM_OK;
  762. struct SingleRequest *k;
  763. if(!GOOD_EASY_HANDLE(easy->easy_handle))
  764. return CURLM_BAD_EASY_HANDLE;
  765. do {
  766. /* this is a do-while loop just to allow a break to skip to the end
  767. of it */
  768. bool disconnect_conn = FALSE;
  769. /* Handle the case when the pipe breaks, i.e., the connection
  770. we're using gets cleaned up and we're left with nothing. */
  771. if(easy->easy_handle->state.pipe_broke) {
  772. infof(easy->easy_handle, "Pipe broke: handle 0x%p, url = %s\n",
  773. easy, easy->easy_handle->state.path);
  774. if(easy->state != CURLM_STATE_COMPLETED) {
  775. /* Head back to the CONNECT state */
  776. multistate(easy, CURLM_STATE_CONNECT);
  777. result = CURLM_CALL_MULTI_PERFORM;
  778. easy->result = CURLE_OK;
  779. }
  780. easy->easy_handle->state.pipe_broke = FALSE;
  781. easy->easy_conn = NULL;
  782. break;
  783. }
  784. if(easy->easy_conn && easy->state > CURLM_STATE_CONNECT &&
  785. easy->state < CURLM_STATE_COMPLETED)
  786. /* Make sure we set the connection's current owner */
  787. easy->easy_conn->data = easy->easy_handle;
  788. switch(easy->state) {
  789. case CURLM_STATE_INIT:
  790. /* init this transfer. */
  791. easy->result=Curl_pretransfer(easy->easy_handle);
  792. if(CURLE_OK == easy->result) {
  793. /* after init, go CONNECT */
  794. multistate(easy, CURLM_STATE_CONNECT);
  795. result = CURLM_CALL_MULTI_PERFORM;
  796. easy->easy_handle->state.used_interface = Curl_if_multi;
  797. }
  798. break;
  799. case CURLM_STATE_CONNECT:
  800. /* Connect. We get a connection identifier filled in. */
  801. Curl_pgrsTime(easy->easy_handle, TIMER_STARTSINGLE);
  802. easy->result = Curl_connect(easy->easy_handle, &easy->easy_conn,
  803. &async, &protocol_connect);
  804. if(CURLE_OK == easy->result) {
  805. /* Add this handle to the send or pend pipeline */
  806. easy->result = addHandleToSendOrPendPipeline(easy->easy_handle,
  807. easy->easy_conn);
  808. if(CURLE_OK == easy->result) {
  809. if(async)
  810. /* We're now waiting for an asynchronous name lookup */
  811. multistate(easy, CURLM_STATE_WAITRESOLVE);
  812. else {
  813. /* after the connect has been sent off, go WAITCONNECT unless the
  814. protocol connect is already done and we can go directly to
  815. WAITDO or DO! */
  816. result = CURLM_CALL_MULTI_PERFORM;
  817. if(protocol_connect)
  818. multistate(easy, multi->pipelining_enabled?
  819. CURLM_STATE_WAITDO:CURLM_STATE_DO);
  820. else {
  821. #ifndef CURL_DISABLE_HTTP
  822. if(easy->easy_conn->bits.tunnel_connecting)
  823. multistate(easy, CURLM_STATE_WAITPROXYCONNECT);
  824. else
  825. #endif
  826. multistate(easy, CURLM_STATE_WAITCONNECT);
  827. }
  828. }
  829. }
  830. }
  831. break;
  832. case CURLM_STATE_WAITRESOLVE:
  833. /* awaiting an asynch name resolve to complete */
  834. {
  835. struct Curl_dns_entry *dns = NULL;
  836. /* check if we have the name resolved by now */
  837. easy->result = Curl_is_resolved(easy->easy_conn, &dns);
  838. if(dns) {
  839. /* Update sockets here. Mainly because the socket(s) may have been
  840. closed and the application thus needs to be told, even if it is
  841. likely that the same socket(s) will again be used further down. */
  842. singlesocket(multi, easy);
  843. /* Perform the next step in the connection phase, and then move on
  844. to the WAITCONNECT state */
  845. easy->result = Curl_async_resolved(easy->easy_conn,
  846. &protocol_connect);
  847. if(CURLE_OK != easy->result)
  848. /* if Curl_async_resolved() returns failure, the connection struct
  849. is already freed and gone */
  850. easy->easy_conn = NULL; /* no more connection */
  851. else {
  852. /* call again please so that we get the next socket setup */
  853. result = CURLM_CALL_MULTI_PERFORM;
  854. if(protocol_connect)
  855. multistate(easy, multi->pipelining_enabled?
  856. CURLM_STATE_WAITDO:CURLM_STATE_DO);
  857. else {
  858. #ifndef CURL_DISABLE_HTTP
  859. if(easy->easy_conn->bits.tunnel_connecting)
  860. multistate(easy, CURLM_STATE_WAITPROXYCONNECT);
  861. else
  862. #endif
  863. multistate(easy, CURLM_STATE_WAITCONNECT);
  864. }
  865. }
  866. }
  867. if(CURLE_OK != easy->result) {
  868. /* failure detected */
  869. disconnect_conn = TRUE;
  870. break;
  871. }
  872. }
  873. break;
  874. #ifndef CURL_DISABLE_HTTP
  875. case CURLM_STATE_WAITPROXYCONNECT:
  876. /* this is HTTP-specific, but sending CONNECT to a proxy is HTTP... */
  877. easy->result = Curl_http_connect(easy->easy_conn, &protocol_connect);
  878. if(easy->easy_conn->bits.proxy_connect_closed) {
  879. /* reset the error buffer */
  880. if(easy->easy_handle->set.errorbuffer)
  881. easy->easy_handle->set.errorbuffer[0] = '\0';
  882. easy->easy_handle->state.errorbuf = FALSE;
  883. easy->result = CURLE_OK;
  884. result = CURLM_CALL_MULTI_PERFORM;
  885. multistate(easy, CURLM_STATE_CONNECT);
  886. }
  887. else if (CURLE_OK == easy->result) {
  888. if(!easy->easy_conn->bits.tunnel_connecting)
  889. multistate(easy, CURLM_STATE_WAITCONNECT);
  890. }
  891. break;
  892. #endif
  893. case CURLM_STATE_WAITCONNECT:
  894. /* awaiting a completion of an asynch connect */
  895. easy->result = Curl_is_connected(easy->easy_conn,
  896. FIRSTSOCKET,
  897. &connected);
  898. if(connected) {
  899. /* see if we need to do any proxy magic first once we connected */
  900. easy->result = Curl_connected_proxy(easy->easy_conn);
  901. if(!easy->result)
  902. /* if everything is still fine we do the protocol-specific connect
  903. setup */
  904. easy->result = Curl_protocol_connect(easy->easy_conn,
  905. &protocol_connect);
  906. }
  907. if(CURLE_OK != easy->result) {
  908. /* failure detected */
  909. /* Just break, the cleaning up is handled all in one place */
  910. disconnect_conn = TRUE;
  911. break;
  912. }
  913. if(connected) {
  914. if(!protocol_connect) {
  915. /* We have a TCP connection, but 'protocol_connect' may be false
  916. and then we continue to 'STATE_PROTOCONNECT'. If protocol
  917. connect is TRUE, we move on to STATE_DO.
  918. BUT if we are using a proxy we must change to WAITPROXYCONNECT
  919. */
  920. #ifndef CURL_DISABLE_HTTP
  921. if(easy->easy_conn->bits.tunnel_connecting)
  922. multistate(easy, CURLM_STATE_WAITPROXYCONNECT);
  923. else
  924. #endif
  925. multistate(easy, CURLM_STATE_PROTOCONNECT);
  926. }
  927. else
  928. /* after the connect has completed, go WAITDO or DO */
  929. multistate(easy, multi->pipelining_enabled?
  930. CURLM_STATE_WAITDO:CURLM_STATE_DO);
  931. result = CURLM_CALL_MULTI_PERFORM;
  932. }
  933. break;
  934. case CURLM_STATE_PROTOCONNECT:
  935. /* protocol-specific connect phase */
  936. easy->result = Curl_protocol_connecting(easy->easy_conn,
  937. &protocol_connect);
  938. if((easy->result == CURLE_OK) && protocol_connect) {
  939. /* after the connect has completed, go WAITDO or DO */
  940. multistate(easy, multi->pipelining_enabled?
  941. CURLM_STATE_WAITDO:CURLM_STATE_DO);
  942. result = CURLM_CALL_MULTI_PERFORM;
  943. }
  944. else if(easy->result) {
  945. /* failure detected */
  946. Curl_posttransfer(easy->easy_handle);
  947. Curl_done(&easy->easy_conn, easy->result, FALSE);
  948. disconnect_conn = TRUE;
  949. }
  950. break;
  951. case CURLM_STATE_WAITDO:
  952. /* Wait for our turn to DO when we're pipelining requests */
  953. #ifdef DEBUGBUILD
  954. infof(easy->easy_handle, "Conn %ld send pipe %d inuse %d athead %d\n",
  955. easy->easy_conn->connectindex,
  956. easy->easy_conn->send_pipe->size,
  957. easy->easy_conn->writechannel_inuse,
  958. isHandleAtHead(easy->easy_handle,
  959. easy->easy_conn->send_pipe));
  960. #endif
  961. if(!easy->easy_conn->writechannel_inuse &&
  962. isHandleAtHead(easy->easy_handle,
  963. easy->easy_conn->send_pipe)) {
  964. /* Grab the channel */
  965. easy->easy_conn->writechannel_inuse = TRUE;
  966. multistate(easy, CURLM_STATE_DO);
  967. result = CURLM_CALL_MULTI_PERFORM;
  968. }
  969. break;
  970. case CURLM_STATE_DO:
  971. if(easy->easy_handle->set.connect_only) {
  972. /* keep connection open for application to use the socket */
  973. easy->easy_conn->bits.close = FALSE;
  974. multistate(easy, CURLM_STATE_DONE);
  975. easy->result = CURLE_OK;
  976. result = CURLM_OK;
  977. }
  978. else {
  979. /* Perform the protocol's DO action */
  980. easy->result = Curl_do(&easy->easy_conn,
  981. &dophase_done);
  982. if(CURLE_OK == easy->result) {
  983. if(!dophase_done) {
  984. /* DO was not completed in one function call, we must continue
  985. DOING... */
  986. multistate(easy, CURLM_STATE_DOING);
  987. result = CURLM_OK;
  988. }
  989. /* after DO, go DO_DONE... or DO_MORE */
  990. else if(easy->easy_conn->bits.do_more) {
  991. /* we're supposed to do more, but we need to sit down, relax
  992. and wait a little while first */
  993. multistate(easy, CURLM_STATE_DO_MORE);
  994. result = CURLM_OK;
  995. }
  996. else {
  997. /* we're done with the DO, now DO_DONE */
  998. multistate(easy, CURLM_STATE_DO_DONE);
  999. result = CURLM_CALL_MULTI_PERFORM;
  1000. }
  1001. }
  1002. else if ((CURLE_SEND_ERROR == easy->result) &&
  1003. easy->easy_conn->bits.reuse) {
  1004. /*
  1005. * In this situation, a connection that we were trying to use
  1006. * may have unexpectedly died. If possible, send the connection
  1007. * back to the CONNECT phase so we can try again.
  1008. */
  1009. char *newurl = NULL;
  1010. followtype follow=FOLLOW_NONE;
  1011. CURLcode drc;
  1012. bool retry = FALSE;
  1013. drc = Curl_retry_request(easy->easy_conn, &newurl);
  1014. if(drc) {
  1015. /* a failure here pretty much implies an out of memory */
  1016. easy->result = drc;
  1017. disconnect_conn = TRUE;
  1018. }
  1019. else
  1020. retry = newurl?TRUE:FALSE;
  1021. Curl_posttransfer(easy->easy_handle);
  1022. drc = Curl_done(&easy->easy_conn, easy->result, FALSE);
  1023. /* When set to retry the connection, we must to go back to
  1024. * the CONNECT state */
  1025. if(retry) {
  1026. if ((drc == CURLE_OK) || (drc == CURLE_SEND_ERROR)) {
  1027. follow = FOLLOW_RETRY;
  1028. drc = Curl_follow(easy->easy_handle, newurl, follow);
  1029. if(drc == CURLE_OK) {
  1030. multistate(easy, CURLM_STATE_CONNECT);
  1031. result = CURLM_CALL_MULTI_PERFORM;
  1032. easy->result = CURLE_OK;
  1033. }
  1034. else {
  1035. /* Follow failed */
  1036. easy->result = drc;
  1037. free(newurl);
  1038. }
  1039. }
  1040. else {
  1041. /* done didn't return OK or SEND_ERROR */
  1042. easy->result = drc;
  1043. free(newurl);
  1044. }
  1045. }
  1046. else {
  1047. /* Have error handler disconnect conn if we can't retry */
  1048. disconnect_conn = TRUE;
  1049. }
  1050. }
  1051. else {
  1052. /* failure detected */
  1053. Curl_posttransfer(easy->easy_handle);
  1054. Curl_done(&easy->easy_conn, easy->result, FALSE);
  1055. disconnect_conn = TRUE;
  1056. }
  1057. }
  1058. break;
  1059. case CURLM_STATE_DOING:
  1060. /* we continue DOING until the DO phase is complete */
  1061. easy->result = Curl_protocol_doing(easy->easy_conn,
  1062. &dophase_done);
  1063. if(CURLE_OK == easy->result) {
  1064. if(dophase_done) {
  1065. /* after DO, go PERFORM... or DO_MORE */
  1066. if(easy->easy_conn->bits.do_more) {
  1067. /* we're supposed to do more, but we need to sit down, relax
  1068. and wait a little while first */
  1069. multistate(easy, CURLM_STATE_DO_MORE);
  1070. result = CURLM_OK;
  1071. }
  1072. else {
  1073. /* we're done with the DO, now DO_DONE */
  1074. multistate(easy, CURLM_STATE_DO_DONE);
  1075. result = CURLM_CALL_MULTI_PERFORM;
  1076. }
  1077. } /* dophase_done */
  1078. }
  1079. else {
  1080. /* failure detected */
  1081. Curl_posttransfer(easy->easy_handle);
  1082. Curl_done(&easy->easy_conn, easy->result, FALSE);
  1083. disconnect_conn = TRUE;
  1084. }
  1085. break;
  1086. case CURLM_STATE_DO_MORE:
  1087. /* Ready to do more? */
  1088. easy->result = Curl_is_connected(easy->easy_conn,
  1089. SECONDARYSOCKET,
  1090. &connected);
  1091. if(connected) {
  1092. /*
  1093. * When we are connected, DO MORE and then go DO_DONE
  1094. */
  1095. easy->result = Curl_do_more(easy->easy_conn);
  1096. /* No need to remove ourselves from the send pipeline here since that
  1097. is done for us in Curl_done() */
  1098. if(CURLE_OK == easy->result) {
  1099. multistate(easy, CURLM_STATE_DO_DONE);
  1100. result = CURLM_CALL_MULTI_PERFORM;
  1101. }
  1102. else {
  1103. /* failure detected */
  1104. Curl_posttransfer(easy->easy_handle);
  1105. Curl_done(&easy->easy_conn, easy->result, FALSE);
  1106. disconnect_conn = TRUE;
  1107. }
  1108. }
  1109. break;
  1110. case CURLM_STATE_DO_DONE:
  1111. /* Move ourselves from the send to recv pipeline */
  1112. moveHandleFromSendToRecvPipeline(easy->easy_handle, easy->easy_conn);
  1113. /* Check if we can move pending requests to send pipe */
  1114. checkPendPipeline(easy->easy_conn);
  1115. multistate(easy, CURLM_STATE_WAITPERFORM);
  1116. result = CURLM_CALL_MULTI_PERFORM;
  1117. break;
  1118. case CURLM_STATE_WAITPERFORM:
  1119. /* Wait for our turn to PERFORM */
  1120. if(!easy->easy_conn->readchannel_inuse &&
  1121. isHandleAtHead(easy->easy_handle,
  1122. easy->easy_conn->recv_pipe)) {
  1123. /* Grab the channel */
  1124. easy->easy_conn->readchannel_inuse = TRUE;
  1125. multistate(easy, CURLM_STATE_PERFORM);
  1126. result = CURLM_CALL_MULTI_PERFORM;
  1127. }
  1128. #ifdef DEBUGBUILD
  1129. else {
  1130. infof(easy->easy_handle, "Conn %ld recv pipe %d inuse %d athead %d\n",
  1131. easy->easy_conn->connectindex,
  1132. easy->easy_conn->recv_pipe->size,
  1133. easy->easy_conn->readchannel_inuse,
  1134. isHandleAtHead(easy->easy_handle,
  1135. easy->easy_conn->recv_pipe));
  1136. }
  1137. #endif
  1138. break;
  1139. case CURLM_STATE_TOOFAST: /* limit-rate exceeded in either direction */
  1140. /* if both rates are within spec, resume transfer */
  1141. Curl_pgrsUpdate(easy->easy_conn);
  1142. if( ( ( easy->easy_handle->set.max_send_speed == 0 ) ||
  1143. ( easy->easy_handle->progress.ulspeed <
  1144. easy->easy_handle->set.max_send_speed ) ) &&
  1145. ( ( easy->easy_handle->set.max_recv_speed == 0 ) ||
  1146. ( easy->easy_handle->progress.dlspeed <
  1147. easy->easy_handle->set.max_recv_speed ) )
  1148. )
  1149. multistate(easy, CURLM_STATE_PERFORM);
  1150. break;
  1151. case CURLM_STATE_PERFORM:
  1152. /* check if over speed */
  1153. if( ( ( easy->easy_handle->set.max_send_speed > 0 ) &&
  1154. ( easy->easy_handle->progress.ulspeed >
  1155. easy->easy_handle->set.max_send_speed ) ) ||
  1156. ( ( easy->easy_handle->set.max_recv_speed > 0 ) &&
  1157. ( easy->easy_handle->progress.dlspeed >
  1158. easy->easy_handle->set.max_recv_speed ) )
  1159. ) {
  1160. /* Transfer is over the speed limit. Change state. TODO: Call
  1161. * Curl_expire() with the time left until we're targeted to be below
  1162. * the speed limit again. */
  1163. multistate(easy, CURLM_STATE_TOOFAST );
  1164. break;
  1165. }
  1166. /* read/write data if it is ready to do so */
  1167. easy->result = Curl_readwrite(easy->easy_conn, &done);
  1168. k = &easy->easy_handle->req;
  1169. if(!(k->keepon & KEEP_RECV)) {
  1170. /* We're done receiving */
  1171. easy->easy_conn->readchannel_inuse = FALSE;
  1172. }
  1173. if(!(k->keepon & KEEP_SEND)) {
  1174. /* We're done sending */
  1175. easy->easy_conn->writechannel_inuse = FALSE;
  1176. }
  1177. if(easy->result) {
  1178. /* The transfer phase returned error, we mark the connection to get
  1179. * closed to prevent being re-used. This is because we can't possibly
  1180. * know if the connection is in a good shape or not now. Unless it is
  1181. * a protocol which uses two "channels" like FTP, as then the error
  1182. * happened in the data connection.
  1183. */
  1184. if(!(easy->easy_conn->protocol & PROT_DUALCHANNEL))
  1185. easy->easy_conn->bits.close = TRUE;
  1186. Curl_posttransfer(easy->easy_handle);
  1187. Curl_done(&easy->easy_conn, easy->result, FALSE);
  1188. }
  1189. else if(TRUE == done) {
  1190. char *newurl = NULL;
  1191. bool retry = FALSE;
  1192. followtype follow=FOLLOW_NONE;
  1193. easy->result = Curl_retry_request(easy->easy_conn, &newurl);
  1194. if(!easy->result)
  1195. retry = newurl?TRUE:FALSE;
  1196. /* call this even if the readwrite function returned error */
  1197. Curl_posttransfer(easy->easy_handle);
  1198. /* we're no longer receving */
  1199. moveHandleFromRecvToDonePipeline(easy->easy_handle,
  1200. easy->easy_conn);
  1201. /* expire the new receiving pipeline head */
  1202. if(easy->easy_conn->recv_pipe->head)
  1203. Curl_expire(easy->easy_conn->recv_pipe->head->ptr, 1);
  1204. /* Check if we can move pending requests to send pipe */
  1205. checkPendPipeline(easy->easy_conn);
  1206. /* When we follow redirects or is set to retry the connection, we must
  1207. to go back to the CONNECT state */
  1208. if(easy->easy_handle->req.newurl || retry) {
  1209. if(!retry) {
  1210. /* if the URL is a follow-location and not just a retried request
  1211. then figure out the URL here */
  1212. newurl = easy->easy_handle->req.newurl;
  1213. easy->easy_handle->req.newurl = NULL;
  1214. follow = FOLLOW_REDIR;
  1215. }
  1216. else
  1217. follow = FOLLOW_RETRY;
  1218. easy->result = Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
  1219. if(easy->result == CURLE_OK)
  1220. easy->result = Curl_follow(easy->easy_handle, newurl, follow);
  1221. if(CURLE_OK == easy->result) {
  1222. multistate(easy, CURLM_STATE_CONNECT);
  1223. result = CURLM_CALL_MULTI_PERFORM;
  1224. }
  1225. else if(newurl)
  1226. /* Since we "took it", we are in charge of freeing this on
  1227. failure */
  1228. free(newurl);
  1229. }
  1230. else {
  1231. /* after the transfer is done, go DONE */
  1232. /* but first check to see if we got a location info even though we're
  1233. not following redirects */
  1234. if (easy->easy_handle->req.location) {
  1235. newurl = easy->easy_handle->req.location;
  1236. easy->easy_handle->req.location = NULL;
  1237. easy->result = Curl_follow(easy->easy_handle, newurl, FOLLOW_FAKE);
  1238. if (easy->result)
  1239. free(newurl);
  1240. }
  1241. multistate(easy, CURLM_STATE_DONE);
  1242. result = CURLM_CALL_MULTI_PERFORM;
  1243. }
  1244. }
  1245. break;
  1246. case CURLM_STATE_DONE:
  1247. if(easy->easy_conn) {
  1248. /* Remove ourselves from the receive and done pipelines. Handle
  1249. should be on one of these lists, depending upon how we got here. */
  1250. Curl_removeHandleFromPipeline(easy->easy_handle,
  1251. easy->easy_conn->recv_pipe);
  1252. Curl_removeHandleFromPipeline(easy->easy_handle,
  1253. easy->easy_conn->done_pipe);
  1254. /* Check if we can move pending requests to send pipe */
  1255. checkPendPipeline(easy->easy_conn);
  1256. if(easy->easy_conn->bits.stream_was_rewound) {
  1257. /* This request read past its response boundary so we quickly let
  1258. the other requests consume those bytes since there is no
  1259. guarantee that the socket will become active again */
  1260. result = CURLM_CALL_MULTI_PERFORM;
  1261. }
  1262. /* post-transfer command */
  1263. easy->result = Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
  1264. /*
  1265. * If there are other handles on the pipeline, Curl_done won't set
  1266. * easy_conn to NULL. In such a case, curl_multi_remove_handle() can
  1267. * access free'd data, if the connection is free'd and the handle
  1268. * removed before we perform the processing in CURLM_STATE_COMPLETED
  1269. */
  1270. if (easy->easy_conn)
  1271. easy->easy_conn = NULL;
  1272. }
  1273. /* after we have DONE what we're supposed to do, go COMPLETED, and
  1274. it doesn't matter what the Curl_done() returned! */
  1275. multistate(easy, CURLM_STATE_COMPLETED);
  1276. break;
  1277. case CURLM_STATE_COMPLETED:
  1278. /* this is a completed transfer, it is likely to still be connected */
  1279. /* This node should be delinked from the list now and we should post
  1280. an information message that we are complete. */
  1281. /* Important: reset the conn pointer so that we don't point to memory
  1282. that could be freed anytime */
  1283. easy->easy_conn = NULL;
  1284. break;
  1285. default:
  1286. return CURLM_INTERNAL_ERROR;
  1287. }
  1288. if(CURLM_STATE_COMPLETED != easy->state) {
  1289. if(CURLE_OK != easy->result) {
  1290. /*
  1291. * If an error was returned, and we aren't in completed state now,
  1292. * then we go to completed and consider this transfer aborted.
  1293. */
  1294. /* NOTE: no attempt to disconnect connections must be made
  1295. in the case blocks above - cleanup happens only here */
  1296. easy->easy_handle->state.pipe_broke = FALSE;
  1297. if(easy->easy_conn) {
  1298. /* if this has a connection, unsubscribe from the pipelines */
  1299. easy->easy_conn->writechannel_inuse = FALSE;
  1300. easy->easy_conn->readchannel_inuse = FALSE;
  1301. Curl_removeHandleFromPipeline(easy->easy_handle,
  1302. easy->easy_conn->send_pipe);
  1303. Curl_removeHandleFromPipeline(easy->easy_handle,
  1304. easy->easy_conn->recv_pipe);
  1305. Curl_removeHandleFromPipeline(easy->easy_handle,
  1306. easy->easy_conn->done_pipe);
  1307. /* Check if we can move pending requests to send pipe */
  1308. checkPendPipeline(easy->easy_conn);
  1309. }
  1310. if(disconnect_conn) {
  1311. Curl_disconnect(easy->easy_conn); /* disconnect properly */
  1312. /* This is where we make sure that the easy_conn pointer is reset.
  1313. We don't have to do this in every case block above where a
  1314. failure is detected */
  1315. easy->easy_conn = NULL;
  1316. }
  1317. multistate(easy, CURLM_STATE_COMPLETED);
  1318. }
  1319. }
  1320. } while(0);
  1321. if((CURLM_STATE_COMPLETED == easy->state) && !easy->msg) {
  1322. if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
  1323. /* clear out the usage of the shared DNS cache */
  1324. easy->easy_handle->dns.hostcache = NULL;
  1325. easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
  1326. }
  1327. /* now add a node to the Curl_message linked list with this info */
  1328. msg = malloc(sizeof(struct Curl_message));
  1329. if(!msg)
  1330. return CURLM_OUT_OF_MEMORY;
  1331. msg->extmsg.msg = CURLMSG_DONE;
  1332. msg->extmsg.easy_handle = easy->easy_handle;
  1333. msg->extmsg.data.result = easy->result;
  1334. msg->next = NULL;
  1335. easy->msg = msg;
  1336. easy->msg_num = 1; /* there is one unread message here */
  1337. multi->num_msgs++; /* increase message counter */
  1338. }
  1339. return result;
  1340. }
  1341. CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
  1342. {
  1343. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1344. struct Curl_one_easy *easy;
  1345. CURLMcode returncode=CURLM_OK;
  1346. struct Curl_tree *t;
  1347. if(!GOOD_MULTI_HANDLE(multi))
  1348. return CURLM_BAD_HANDLE;
  1349. easy=multi->easy.next;
  1350. while(easy != &multi->easy) {
  1351. CURLMcode result;
  1352. do
  1353. result = multi_runsingle(multi, easy);
  1354. while (CURLM_CALL_MULTI_PERFORM == result);
  1355. if(result)
  1356. returncode = result;
  1357. easy = easy->next; /* operate on next handle */
  1358. }
  1359. /*
  1360. * Simply remove all expired timers from the splay since handles are dealt
  1361. * with unconditionally by this function and curl_multi_timeout() requires
  1362. * that already passed/handled expire times are removed from the splay.
  1363. */
  1364. do {
  1365. struct timeval now = Curl_tvnow();
  1366. multi->timetree = Curl_splaygetbest(now, multi->timetree, &t);
  1367. if(t) {
  1368. struct SessionHandle *d = t->payload;
  1369. struct timeval* tv = &d->state.expiretime;
  1370. /* clear the expire times within the handles that we remove from the
  1371. splay tree */
  1372. tv->tv_sec = 0;
  1373. tv->tv_usec = 0;
  1374. }
  1375. } while(t);
  1376. *running_handles = multi->num_alive;
  1377. if( CURLM_OK >= returncode )
  1378. update_timer(multi);
  1379. return returncode;
  1380. }
  1381. /* This is called when an easy handle is cleanup'ed that is part of a multi
  1382. handle */
  1383. void Curl_multi_rmeasy(void *multi_handle, CURL *easy_handle)
  1384. {
  1385. curl_multi_remove_handle(multi_handle, easy_handle);
  1386. }
  1387. CURLMcode curl_multi_cleanup(CURLM *multi_handle)
  1388. {
  1389. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1390. struct Curl_one_easy *easy;
  1391. struct Curl_one_easy *nexteasy;
  1392. int i;
  1393. struct closure *cl;
  1394. struct closure *n;
  1395. if(GOOD_MULTI_HANDLE(multi)) {
  1396. multi->type = 0; /* not good anymore */
  1397. Curl_hash_destroy(multi->hostcache);
  1398. Curl_hash_destroy(multi->sockhash);
  1399. multi->hostcache = NULL;
  1400. multi->sockhash = NULL;
  1401. /* go over all connections that have close actions */
  1402. for(i=0; i< multi->connc->num; i++) {
  1403. if(multi->connc->connects[i] &&
  1404. multi->connc->connects[i]->protocol & PROT_CLOSEACTION) {
  1405. Curl_disconnect(multi->connc->connects[i]);
  1406. multi->connc->connects[i] = NULL;
  1407. }
  1408. }
  1409. /* now walk through the list of handles we kept around only to be
  1410. able to close connections "properly" */
  1411. cl = multi->closure;
  1412. while(cl) {
  1413. cl->easy_handle->state.shared_conn = NULL; /* no more shared */
  1414. if(cl->easy_handle->state.closed)
  1415. /* close handle only if curl_easy_cleanup() already has been called
  1416. for this easy handle */
  1417. Curl_close(cl->easy_handle);
  1418. n = cl->next;
  1419. free(cl);
  1420. cl= n;
  1421. }
  1422. Curl_rm_connc(multi->connc);
  1423. /* remove all easy handles */
  1424. easy = multi->easy.next;
  1425. while(easy != &multi->easy) {
  1426. nexteasy=easy->next;
  1427. if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
  1428. /* clear out the usage of the shared DNS cache */
  1429. easy->easy_handle->dns.hostcache = NULL;
  1430. easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
  1431. }
  1432. /* Clear the pointer to the connection cache */
  1433. easy->easy_handle->state.connc = NULL;
  1434. Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association */
  1435. if(easy->msg)
  1436. free(easy->msg);
  1437. free(easy);
  1438. easy = nexteasy;
  1439. }
  1440. free(multi);
  1441. return CURLM_OK;
  1442. }
  1443. else
  1444. return CURLM_BAD_HANDLE;
  1445. }
  1446. CURLMsg *curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
  1447. {
  1448. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1449. *msgs_in_queue = 0; /* default to none */
  1450. if(GOOD_MULTI_HANDLE(multi)) {
  1451. struct Curl_one_easy *easy;
  1452. if(!multi->num_msgs)
  1453. return NULL; /* no messages left to return */
  1454. easy=multi->easy.next;
  1455. while(easy != &multi->easy) {
  1456. if(easy->msg_num) {
  1457. easy->msg_num--;
  1458. break;
  1459. }
  1460. easy = easy->next;
  1461. }
  1462. if(!easy)
  1463. return NULL; /* this means internal count confusion really */
  1464. multi->num_msgs--;
  1465. *msgs_in_queue = multi->num_msgs;
  1466. return &easy->msg->extmsg;
  1467. }
  1468. else
  1469. return NULL;
  1470. }
  1471. /*
  1472. * singlesocket() checks what sockets we deal with and their "action state"
  1473. * and if we have a different state in any of those sockets from last time we
  1474. * call the callback accordingly.
  1475. */
  1476. static void singlesocket(struct Curl_multi *multi,
  1477. struct Curl_one_easy *easy)
  1478. {
  1479. curl_socket_t socks[MAX_SOCKSPEREASYHANDLE];
  1480. int i;
  1481. struct Curl_sh_entry *entry;
  1482. curl_socket_t s;
  1483. int num;
  1484. unsigned int curraction;
  1485. struct Curl_one_easy *easy_by_hash;
  1486. bool remove_sock_from_hash;
  1487. for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++)
  1488. socks[i] = CURL_SOCKET_BAD;
  1489. /* Fill in the 'current' struct with the state as it is now: what sockets to
  1490. supervise and for what actions */
  1491. curraction = multi_getsock(easy, socks, MAX_SOCKSPEREASYHANDLE);
  1492. /* We have 0 .. N sockets already and we get to know about the 0 .. M
  1493. sockets we should have from now on. Detect the differences, remove no
  1494. longer supervised ones and add new ones */
  1495. /* walk over the sockets we got right now */
  1496. for(i=0; (i< MAX_SOCKSPEREASYHANDLE) &&
  1497. (curraction & (GETSOCK_READSOCK(i) | GETSOCK_WRITESOCK(i)));
  1498. i++) {
  1499. int action = CURL_POLL_NONE;
  1500. s = socks[i];
  1501. /* get it from the hash */
  1502. entry = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
  1503. if(curraction & GETSOCK_READSOCK(i))
  1504. action |= CURL_POLL_IN;
  1505. if(curraction & GETSOCK_WRITESOCK(i))
  1506. action |= CURL_POLL_OUT;
  1507. if(entry) {
  1508. /* yeps, already present so check if it has the same action set */
  1509. if(entry->action == action)
  1510. /* same, continue */
  1511. continue;
  1512. }
  1513. else {
  1514. /* this is a socket we didn't have before, add it! */
  1515. entry = sh_addentry(multi->sockhash, s, easy->easy_handle);
  1516. if(!entry)
  1517. /* fatal */
  1518. return;
  1519. }
  1520. multi->socket_cb(easy->easy_handle,
  1521. s,
  1522. action,
  1523. multi->socket_userp,
  1524. entry ? entry->socketp : NULL);
  1525. entry->action = action; /* store the current action state */
  1526. }
  1527. num = i; /* number of sockets */
  1528. /* when we've walked over all the sockets we should have right now, we must
  1529. make sure to detect sockets that are removed */
  1530. for(i=0; i< easy->numsocks; i++) {
  1531. int j;
  1532. s = easy->sockets[i];
  1533. for(j=0; j<num; j++) {
  1534. if(s == socks[j]) {
  1535. /* this is still supervised */
  1536. s = CURL_SOCKET_BAD;
  1537. break;
  1538. }
  1539. }
  1540. if(s != CURL_SOCKET_BAD) {
  1541. /* this socket has been removed. Tell the app to remove it */
  1542. remove_sock_from_hash = TRUE;
  1543. entry = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
  1544. if(entry) {
  1545. /* check if the socket to be removed serves a connection which has
  1546. other easy-s in a pipeline. In this case the socket should not be
  1547. removed. */
  1548. struct connectdata *easy_conn;
  1549. easy_by_hash = entry->easy->multi_pos;
  1550. easy_conn = easy_by_hash->easy_conn;
  1551. if(easy_conn) {
  1552. if (easy_conn->recv_pipe && easy_conn->recv_pipe->size > 1) {
  1553. /* the handle should not be removed from the pipe yet */
  1554. remove_sock_from_hash = FALSE;
  1555. /* Update the sockhash entry to instead point to the next in line
  1556. for the recv_pipe, or the first (in case this particular easy
  1557. isn't already) */
  1558. if (entry->easy == easy->easy_handle) {
  1559. if (isHandleAtHead(easy->easy_handle, easy_conn->recv_pipe))
  1560. entry->easy = easy_conn->recv_pipe->head->next->ptr;
  1561. else
  1562. entry->easy = easy_conn->recv_pipe->head->ptr;
  1563. }
  1564. }
  1565. if (easy_conn->send_pipe && easy_conn->send_pipe->size > 1) {
  1566. /* the handle should not be removed from the pipe yet */
  1567. remove_sock_from_hash = FALSE;
  1568. /* Update the sockhash entry to instead point to the next in line
  1569. for the send_pipe, or the first (in case this particular easy
  1570. isn't already) */
  1571. if (entry->easy == easy->easy_handle) {
  1572. if (isHandleAtHead(easy->easy_handle, easy_conn->send_pipe))
  1573. entry->easy = easy_conn->send_pipe->head->next->ptr;
  1574. else
  1575. entry->easy = easy_conn->send_pipe->head->ptr;
  1576. }
  1577. }
  1578. /* Don't worry about overwriting recv_pipe head with send_pipe_head,
  1579. when action will be asked on the socket (see multi_socket()), the
  1580. head of the correct pipe will be taken according to the
  1581. action. */
  1582. }
  1583. }
  1584. else
  1585. /* just a precaution, this socket really SHOULD be in the hash already
  1586. but in case it isn't, we don't have to tell the app to remove it
  1587. either since it never got to know about it */
  1588. remove_sock_from_hash = FALSE;
  1589. if (remove_sock_from_hash) {
  1590. multi->socket_cb(easy->easy_handle,
  1591. s,
  1592. CURL_POLL_REMOVE,
  1593. multi->socket_userp,
  1594. entry ? entry->socketp : NULL);
  1595. sh_delentry(multi->sockhash, s);
  1596. }
  1597. }
  1598. }
  1599. memcpy(easy->sockets, socks, num*sizeof(curl_socket_t));
  1600. easy->numsocks = num;
  1601. }
  1602. static CURLMcode multi_socket(struct Curl_multi *multi,
  1603. bool checkall,
  1604. curl_socket_t s,
  1605. int ev_bitmask,
  1606. int *running_handles)
  1607. {
  1608. CURLMcode result = CURLM_OK;
  1609. struct SessionHandle *data = NULL;
  1610. struct Curl_tree *t;
  1611. if(checkall) {
  1612. struct Curl_one_easy *easyp;
  1613. /* *perform() deals with running_handles on its own */
  1614. result = curl_multi_perform(multi, running_handles);
  1615. /* walk through each easy handle and do the socket state change magic
  1616. and callbacks */
  1617. easyp=multi->easy.next;
  1618. while(easyp != &multi->easy) {
  1619. singlesocket(multi, easyp);
  1620. easyp = easyp->next;
  1621. }
  1622. /* or should we fall-through and do the timer-based stuff? */
  1623. return result;
  1624. }
  1625. else if(s != CURL_SOCKET_TIMEOUT) {
  1626. struct Curl_sh_entry *entry =
  1627. Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
  1628. if(!entry)
  1629. /* Unmatched socket, we can't act on it but we ignore this fact. In
  1630. real-world tests it has been proved that libevent can in fact give
  1631. the application actions even though the socket was just previously
  1632. asked to get removed, so thus we better survive stray socket actions
  1633. and just move on. */
  1634. ;
  1635. else {
  1636. data = entry->easy;
  1637. if(data->magic != CURLEASY_MAGIC_NUMBER)
  1638. /* bad bad bad bad bad bad bad */
  1639. return CURLM_INTERNAL_ERROR;
  1640. /* If the pipeline is enabled, take the handle which is in the head of
  1641. the pipeline. If we should write into the socket, take the send_pipe
  1642. head. If we should read from the socket, take the recv_pipe head. */
  1643. if(data->set.one_easy->easy_conn) {
  1644. if ((ev_bitmask & CURL_POLL_OUT) &&
  1645. data->set.one_easy->easy_conn->send_pipe &&
  1646. data->set.one_easy->easy_conn->send_pipe->head)
  1647. data = data->set.one_easy->easy_conn->send_pipe->head->ptr;
  1648. else if ((ev_bitmask & CURL_POLL_IN) &&
  1649. data->set.one_easy->easy_conn->recv_pipe &&
  1650. data->set.one_easy->easy_conn->recv_pipe->head)
  1651. data = data->set.one_easy->easy_conn->recv_pipe->head->ptr;
  1652. }
  1653. if(data->set.one_easy->easy_conn) /* set socket event bitmask */
  1654. data->set.one_easy->easy_conn->cselect_bits = ev_bitmask;
  1655. do
  1656. result = multi_runsingle(multi, data->set.one_easy);
  1657. while (CURLM_CALL_MULTI_PERFORM == result);
  1658. if(data->set.one_easy->easy_conn)
  1659. data->set.one_easy->easy_conn->cselect_bits = 0;
  1660. if(CURLM_OK >= result)
  1661. /* get the socket(s) and check if the state has been changed since
  1662. last */
  1663. singlesocket(multi, data->set.one_easy);
  1664. /* Now we fall-through and do the timer-based stuff, since we don't want
  1665. to force the user to have to deal with timeouts as long as at least
  1666. one connection in fact has traffic. */
  1667. data = NULL; /* set data to NULL again to avoid calling
  1668. multi_runsingle() in case there's no need to */
  1669. }
  1670. }
  1671. /*
  1672. * The loop following here will go on as long as there are expire-times left
  1673. * to process in the splay and 'data' will be re-assigned for every expired
  1674. * handle we deal with.
  1675. */
  1676. do {
  1677. struct timeval now;
  1678. /* the first loop lap 'data' can be NULL */
  1679. if(data) {
  1680. do
  1681. result = multi_runsingle(multi, data->set.one_easy);
  1682. while (CURLM_CALL_MULTI_PERFORM == result);
  1683. if(CURLM_OK >= result)
  1684. /* get the socket(s) and check if the state has been changed since
  1685. last */
  1686. singlesocket(multi, data->set.one_easy);
  1687. }
  1688. /* Check if there's one (more) expired timer to deal with! This function
  1689. extracts a matching node if there is one */
  1690. now = Curl_tvnow();
  1691. now.tv_usec += 1000; /* to compensate for the truncating of 999us to 0ms,
  1692. we always add time here to make the comparison
  1693. below better */
  1694. multi->timetree = Curl_splaygetbest(now, multi->timetree, &t);
  1695. if(t) {
  1696. /* assign 'data' to be the easy handle we just removed from the splay
  1697. tree */
  1698. data = t->payload;
  1699. /* clear the expire time within the handle we removed from the
  1700. splay tree */
  1701. data->state.expiretime.tv_sec = 0;
  1702. data->state.expiretime.tv_usec = 0;
  1703. }
  1704. } while(t);
  1705. *running_handles = multi->num_alive;
  1706. return result;
  1707. }
  1708. #undef curl_multi_setopt
  1709. CURLMcode curl_multi_setopt(CURLM *multi_handle,
  1710. CURLMoption option, ...)
  1711. {
  1712. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1713. CURLMcode res = CURLM_OK;
  1714. va_list param;
  1715. if(!GOOD_MULTI_HANDLE(multi))
  1716. return CURLM_BAD_HANDLE;
  1717. va_start(param, option);
  1718. switch(option) {
  1719. case CURLMOPT_SOCKETFUNCTION:
  1720. multi->socket_cb = va_arg(param, curl_socket_callback);
  1721. break;
  1722. case CURLMOPT_SOCKETDATA:
  1723. multi->socket_userp = va_arg(param, void *);
  1724. break;
  1725. case CURLMOPT_PIPELINING:
  1726. multi->pipelining_enabled = (bool)(0 != va_arg(param, long));
  1727. break;
  1728. case CURLMOPT_TIMERFUNCTION:
  1729. multi->timer_cb = va_arg(param, curl_multi_timer_callback);
  1730. break;
  1731. case CURLMOPT_TIMERDATA:
  1732. multi->timer_userp = va_arg(param, void *);
  1733. break;
  1734. case CURLMOPT_MAXCONNECTS:
  1735. multi->maxconnects = va_arg(param, long);
  1736. break;
  1737. default:
  1738. res = CURLM_UNKNOWN_OPTION;
  1739. break;
  1740. }
  1741. va_end(param);
  1742. return res;
  1743. }
  1744. /* we define curl_multi_socket() in the public multi.h header */
  1745. #undef curl_multi_socket
  1746. CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
  1747. int *running_handles)
  1748. {
  1749. CURLMcode result = multi_socket((struct Curl_multi *)multi_handle, FALSE, s,
  1750. 0, running_handles);
  1751. if(CURLM_OK >= result)
  1752. update_timer((struct Curl_multi *)multi_handle);
  1753. return result;
  1754. }
  1755. CURLMcode curl_multi_socket_action(CURLM *multi_handle, curl_socket_t s,
  1756. int ev_bitmask, int *running_handles)
  1757. {
  1758. CURLMcode result = multi_socket((struct Curl_multi *)multi_handle, FALSE, s,
  1759. ev_bitmask, running_handles);
  1760. if(CURLM_OK >= result)
  1761. update_timer((struct Curl_multi *)multi_handle);
  1762. return result;
  1763. }
  1764. CURLMcode curl_multi_socket_all(CURLM *multi_handle, int *running_handles)
  1765. {
  1766. CURLMcode result = multi_socket((struct Curl_multi *)multi_handle,
  1767. TRUE, CURL_SOCKET_BAD, 0, running_handles);
  1768. if(CURLM_OK >= result)
  1769. update_timer((struct Curl_multi *)multi_handle);
  1770. return result;
  1771. }
  1772. static CURLMcode multi_timeout(struct Curl_multi *multi,
  1773. long *timeout_ms)
  1774. {
  1775. static struct timeval tv_zero = {0,0};
  1776. if(multi->timetree) {
  1777. /* we have a tree of expire times */
  1778. struct timeval now = Curl_tvnow();
  1779. /* splay the lowest to the bottom */
  1780. multi->timetree = Curl_splay(tv_zero, multi->timetree);
  1781. if(Curl_splaycomparekeys(multi->timetree->key, now) > 0) {
  1782. /* some time left before expiration */
  1783. *timeout_ms = curlx_tvdiff(multi->timetree->key, now);
  1784. if(!*timeout_ms)
  1785. /*
  1786. * Since we only provide millisecond resolution on the returned value
  1787. * and the diff might be less than one millisecond here, we don't
  1788. * return zero as that may cause short bursts of busyloops on fast
  1789. * processors while the diff is still present but less than one
  1790. * millisecond! instead we return 1 until the time is ripe.
  1791. */
  1792. *timeout_ms=1;
  1793. }
  1794. else
  1795. /* 0 means immediately */
  1796. *timeout_ms = 0;
  1797. }
  1798. else
  1799. *timeout_ms = -1;
  1800. return CURLM_OK;
  1801. }
  1802. CURLMcode curl_multi_timeout(CURLM *multi_handle,
  1803. long *timeout_ms)
  1804. {
  1805. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1806. /* First, make some basic checks that the CURLM handle is a good handle */
  1807. if(!GOOD_MULTI_HANDLE(multi))
  1808. return CURLM_BAD_HANDLE;
  1809. return multi_timeout(multi, timeout_ms);
  1810. }
  1811. /*
  1812. * Tell the application it should update its timers, if it subscribes to the
  1813. * update timer callback.
  1814. */
  1815. static int update_timer(struct Curl_multi *multi)
  1816. {
  1817. long timeout_ms;
  1818. if(!multi->timer_cb)
  1819. return 0;
  1820. if( multi_timeout(multi, &timeout_ms) != CURLM_OK )
  1821. return -1;
  1822. if( timeout_ms < 0 )
  1823. return 0;
  1824. /* When multi_timeout() is done, multi->timetree points to the node with the
  1825. * timeout we got the (relative) time-out time for. We can thus easily check
  1826. * if this is the same (fixed) time as we got in a previous call and then
  1827. * avoid calling the callback again. */
  1828. if(Curl_splaycomparekeys(multi->timetree->key, multi->timer_lastcall) == 0)
  1829. return 0;
  1830. multi->timer_lastcall = multi->timetree->key;
  1831. return multi->timer_cb((CURLM*)multi, timeout_ms, multi->timer_userp);
  1832. }
  1833. static CURLcode addHandleToSendOrPendPipeline(struct SessionHandle *handle,
  1834. struct connectdata *conn)
  1835. {
  1836. size_t pipeLen = conn->send_pipe->size + conn->recv_pipe->size;
  1837. struct curl_llist *pipeline;
  1838. if(!Curl_isPipeliningEnabled(handle) ||
  1839. pipeLen == 0)
  1840. pipeline = conn->send_pipe;
  1841. else {
  1842. if(conn->server_supports_pipelining &&
  1843. pipeLen < MAX_PIPELINE_LENGTH)
  1844. pipeline = conn->send_pipe;
  1845. else
  1846. pipeline = conn->pend_pipe;
  1847. }
  1848. return Curl_addHandleToPipeline(handle, pipeline);
  1849. }
  1850. static int checkPendPipeline(struct connectdata *conn)
  1851. {
  1852. int result = 0;
  1853. struct curl_llist_element *sendhead = conn->send_pipe->head;
  1854. size_t pipeLen = conn->send_pipe->size + conn->recv_pipe->size;
  1855. if (conn->server_supports_pipelining || pipeLen == 0) {
  1856. struct curl_llist_element *curr = conn->pend_pipe->head;
  1857. const size_t maxPipeLen =
  1858. conn->server_supports_pipelining ? MAX_PIPELINE_LENGTH : 1;
  1859. while(pipeLen < maxPipeLen && curr) {
  1860. Curl_llist_move(conn->pend_pipe, curr,
  1861. conn->send_pipe, conn->send_pipe->tail);
  1862. Curl_pgrsTime(curr->ptr, TIMER_PRETRANSFER);
  1863. ++result; /* count how many handles we moved */
  1864. curr = conn->pend_pipe->head;
  1865. ++pipeLen;
  1866. }
  1867. }
  1868. if (result) {
  1869. conn->now = Curl_tvnow();
  1870. /* something moved, check for a new send pipeline leader */
  1871. if(sendhead != conn->send_pipe->head) {
  1872. /* this is a new one as head, expire it */
  1873. conn->writechannel_inuse = FALSE; /* not in use yet */
  1874. infof(conn->data, "%p is at send pipe head!\n",
  1875. conn->send_pipe->head->ptr);
  1876. Curl_expire(conn->send_pipe->head->ptr, 1);
  1877. }
  1878. }
  1879. return result;
  1880. }
  1881. /* Move this transfer from the sending list to the receiving list.
  1882. Pay special attention to the new sending list "leader" as it needs to get
  1883. checked to update what sockets it acts on.
  1884. */
  1885. static void moveHandleFromSendToRecvPipeline(struct SessionHandle *handle,
  1886. struct connectdata *conn)
  1887. {
  1888. struct curl_llist_element *curr;
  1889. curr = conn->send_pipe->head;
  1890. while(curr) {
  1891. if(curr->ptr == handle) {
  1892. Curl_llist_move(conn->send_pipe, curr,
  1893. conn->recv_pipe, conn->recv_pipe->tail);
  1894. if(conn->send_pipe->head) {
  1895. /* Since there's a new easy handle at the start of the send pipeline,
  1896. set its timeout value to 1ms to make it trigger instantly */
  1897. conn->writechannel_inuse = FALSE; /* not used now */
  1898. infof(conn->data, "%p is at send pipe head B!\n",
  1899. conn->send_pipe->head->ptr);
  1900. Curl_expire(conn->send_pipe->head->ptr, 1);
  1901. }
  1902. /* The receiver's list is not really interesting here since either this
  1903. handle is now first in the list and we'll deal with it soon, or
  1904. another handle is already first and thus is already taken care of */
  1905. break; /* we're done! */
  1906. }
  1907. curr = curr->next;
  1908. }
  1909. }
  1910. static void moveHandleFromRecvToDonePipeline(struct SessionHandle *handle,
  1911. struct connectdata *conn)
  1912. {
  1913. struct curl_llist_element *curr;
  1914. curr = conn->recv_pipe->head;
  1915. while(curr) {
  1916. if(curr->ptr == handle) {
  1917. Curl_llist_move(conn->recv_pipe, curr,
  1918. conn->done_pipe, conn->done_pipe->tail);
  1919. break;
  1920. }
  1921. curr = curr->next;
  1922. }
  1923. }
  1924. static bool isHandleAtHead(struct SessionHandle *handle,
  1925. struct curl_llist *pipeline)
  1926. {
  1927. struct curl_llist_element *curr = pipeline->head;
  1928. if(curr)
  1929. return (bool)(curr->ptr == handle);
  1930. return FALSE;
  1931. }
  1932. /* given a number of milliseconds from now to use to set the 'act before
  1933. this'-time for the transfer, to be extracted by curl_multi_timeout()
  1934. Pass zero to clear the timeout value for this handle.
  1935. */
  1936. void Curl_expire(struct SessionHandle *data, long milli)
  1937. {
  1938. struct Curl_multi *multi = data->multi;
  1939. struct timeval *nowp = &data->state.expiretime;
  1940. int rc;
  1941. /* this is only interesting for multi-interface using libcurl, and only
  1942. while there is still a multi interface struct remaining! */
  1943. if(!multi)
  1944. return;
  1945. if(!milli) {
  1946. /* No timeout, clear the time data. */
  1947. if(nowp->tv_sec || nowp->tv_usec) {
  1948. /* Since this is an cleared time, we must remove the previous entry from
  1949. the splay tree */
  1950. rc = Curl_splayremovebyaddr(multi->timetree,
  1951. &data->state.timenode,
  1952. &multi->timetree);
  1953. if(rc)
  1954. infof(data, "Internal error clearing splay node = %d\n", rc);
  1955. infof(data, "Expire cleared\n");
  1956. nowp->tv_sec = 0;
  1957. nowp->tv_usec = 0;
  1958. }
  1959. }
  1960. else {
  1961. struct timeval set;
  1962. int rest;
  1963. set = Curl_tvnow();
  1964. set.tv_sec += milli/1000;
  1965. set.tv_usec += (milli%1000)*1000;
  1966. rest = (int)(set.tv_usec - 1000000);
  1967. if(rest > 0) {
  1968. /* bigger than a full microsec */
  1969. set.tv_sec++;
  1970. set.tv_usec -= 1000000;
  1971. }
  1972. if(nowp->tv_sec || nowp->tv_usec) {
  1973. /* This means that the struct is added as a node in the splay tree.
  1974. Compare if the new time is earlier, and only remove-old/add-new if it
  1975. is. */
  1976. long diff = curlx_tvdiff(set, *nowp);
  1977. if(diff > 0)
  1978. /* the new expire time was later so we don't change this */
  1979. return;
  1980. /* Since this is an updated time, we must remove the previous entry from
  1981. the splay tree first and then re-add the new value */
  1982. rc = Curl_splayremovebyaddr(multi->timetree,
  1983. &data->state.timenode,
  1984. &multi->timetree);
  1985. if(rc)
  1986. infof(data, "Internal error removing splay node = %d\n", rc);
  1987. }
  1988. *nowp = set;
  1989. #if 0
  1990. infof(data, "Expire at %ld / %ld (%ldms) %p\n",
  1991. (long)nowp->tv_sec, (long)nowp->tv_usec, milli, data);
  1992. #endif
  1993. data->state.timenode.payload = data;
  1994. multi->timetree = Curl_splayinsert(*nowp,
  1995. multi->timetree,
  1996. &data->state.timenode);
  1997. }
  1998. #if 0
  1999. Curl_splayprint(multi->timetree, 0, TRUE);
  2000. #endif
  2001. }
  2002. CURLMcode curl_multi_assign(CURLM *multi_handle,
  2003. curl_socket_t s, void *hashp)
  2004. {
  2005. struct Curl_sh_entry *there = NULL;
  2006. struct Curl_multi *multi = (struct Curl_multi *)multi_handle;
  2007. if(s != CURL_SOCKET_BAD)
  2008. there = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(curl_socket_t));
  2009. if(!there)
  2010. return CURLM_BAD_SOCKET;
  2011. there->socketp = hashp;
  2012. return CURLM_OK;
  2013. }
  2014. static struct connectdata *conn_using(struct Curl_multi *multi,
  2015. struct SessionHandle *data)
  2016. {
  2017. /* a connection in the connection cache pointing to the given 'data' ? */
  2018. int i;
  2019. for(i=0; i< multi->connc->num; i++) {
  2020. if(multi->connc->connects[i] &&
  2021. (multi->connc->connects[i]->data == data))
  2022. return multi->connc->connects[i];
  2023. }
  2024. return NULL;
  2025. }
  2026. /* Add the given data pointer to the list of 'closure handles' that are kept
  2027. around only to be able to close some connections nicely - just make sure
  2028. that this handle isn't already added, like for the cases when an easy
  2029. handle is removed, added and removed again... */
  2030. static void add_closure(struct Curl_multi *multi,
  2031. struct SessionHandle *data)
  2032. {
  2033. int i;
  2034. struct closure *cl = calloc(1, sizeof(struct closure));
  2035. struct closure *p=NULL;
  2036. struct closure *n;
  2037. if(cl) {
  2038. cl->easy_handle = data;
  2039. cl->next = multi->closure;
  2040. multi->closure = cl;
  2041. }
  2042. p = multi->closure;
  2043. cl = p->next; /* start immediately on the second since the first is the one
  2044. we just added and it is _very_ likely to actually exist
  2045. used in the cache since that's the whole purpose of adding
  2046. it to this list! */
  2047. /* When adding, scan through all the other currently kept handles and see if
  2048. there are any connections still referring to them and kill them if not. */
  2049. while(cl) {
  2050. bool inuse = FALSE;
  2051. for(i=0; i< multi->connc->num; i++) {
  2052. if(multi->connc->connects[i] &&
  2053. (multi->connc->connects[i]->data == cl->easy_handle)) {
  2054. inuse = TRUE;
  2055. break;
  2056. }
  2057. }
  2058. n = cl->next;
  2059. if(!inuse) {
  2060. /* cl->easy_handle is now killable */
  2061. infof(data, "Delayed kill of easy handle %p\n", cl->easy_handle);
  2062. /* unmark it as not having a connection around that uses it anymore */
  2063. cl->easy_handle->state.shared_conn= NULL;
  2064. Curl_close(cl->easy_handle);
  2065. if(p)
  2066. p->next = n;
  2067. else
  2068. multi->closure = n;
  2069. free(cl);
  2070. }
  2071. else
  2072. p = cl;
  2073. cl = n;
  2074. }
  2075. }
  2076. #ifdef DEBUGBUILD
  2077. void Curl_multi_dump(const struct Curl_multi *multi_handle)
  2078. {
  2079. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  2080. struct Curl_one_easy *easy;
  2081. int i;
  2082. fprintf(stderr, "* Multi status: %d handles, %d alive\n",
  2083. multi->num_easy, multi->num_alive);
  2084. for(easy=multi->easy.next; easy != &multi->easy; easy = easy->next) {
  2085. if(easy->state != CURLM_STATE_COMPLETED) {
  2086. /* only display handles that are not completed */
  2087. fprintf(stderr, "handle %p, state %s, %d sockets\n",
  2088. (void *)easy->easy_handle,
  2089. statename[easy->state], easy->numsocks);
  2090. for(i=0; i < easy->numsocks; i++) {
  2091. curl_socket_t s = easy->sockets[i];
  2092. struct Curl_sh_entry *entry =
  2093. Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
  2094. fprintf(stderr, "%d ", (int)s);
  2095. if(!entry) {
  2096. fprintf(stderr, "INTERNAL CONFUSION\n");
  2097. continue;
  2098. }
  2099. fprintf(stderr, "[%s %s] ",
  2100. entry->action&CURL_POLL_IN?"RECVING":"",
  2101. entry->action&CURL_POLL_OUT?"SENDING":"");
  2102. }
  2103. if(easy->numsocks)
  2104. fprintf(stderr, "\n");
  2105. }
  2106. }
  2107. }
  2108. #endif