multi.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2007, 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. #include <stdlib.h>
  25. #include <string.h>
  26. #ifdef HAVE_SYS_TYPES_H
  27. #include <sys/types.h>
  28. #endif
  29. #ifdef HAVE_SYS_SOCKET_H
  30. #include <sys/socket.h>
  31. #endif
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #include <curl/curl.h>
  36. #include "urldata.h"
  37. #include "transfer.h"
  38. #include "url.h"
  39. #include "connect.h"
  40. #include "progress.h"
  41. #include "memory.h"
  42. #include "easyif.h"
  43. #include "multiif.h"
  44. #include "sendf.h"
  45. #include "timeval.h"
  46. /* The last #include file should be: */
  47. #include "memdebug.h"
  48. struct Curl_message {
  49. /* the 'CURLMsg' is the part that is visible to the external user */
  50. struct CURLMsg extmsg;
  51. struct Curl_message *next;
  52. };
  53. typedef enum {
  54. CURLM_STATE_INIT, /* start in this state */
  55. CURLM_STATE_CONNECT, /* resolve/connect has been sent off */
  56. CURLM_STATE_WAITRESOLVE, /* awaiting the resolve to finalize */
  57. CURLM_STATE_WAITCONNECT, /* awaiting the connect to finalize */
  58. CURLM_STATE_PROTOCONNECT, /* completing the protocol-specific connect
  59. phase */
  60. CURLM_STATE_WAITDO, /* wait for our turn to send the request */
  61. CURLM_STATE_DO, /* start send off the request (part 1) */
  62. CURLM_STATE_DOING, /* sending off the request (part 1) */
  63. CURLM_STATE_DO_MORE, /* send off the request (part 2) */
  64. CURLM_STATE_DO_DONE, /* done sending off request */
  65. CURLM_STATE_WAITPERFORM, /* wait for our turn to read the response */
  66. CURLM_STATE_PERFORM, /* transfer data */
  67. CURLM_STATE_TOOFAST, /* wait because limit-rate exceeded */
  68. CURLM_STATE_DONE, /* post data transfer operation */
  69. CURLM_STATE_COMPLETED, /* operation complete */
  70. CURLM_STATE_CANCELLED, /* cancelled */
  71. CURLM_STATE_LAST /* not a true state, never use this */
  72. } CURLMstate;
  73. /* we support N sockets per easy handle. Set the corresponding bit to what
  74. action we should wait for */
  75. #define MAX_SOCKSPEREASYHANDLE 5
  76. #define GETSOCK_READABLE (0x00ff)
  77. #define GETSOCK_WRITABLE (0xff00)
  78. struct closure {
  79. struct closure *next; /* a simple one-way list of structs */
  80. struct SessionHandle *easy_handle;
  81. };
  82. struct Curl_one_easy {
  83. /* first, two fields for the linked list of these */
  84. struct Curl_one_easy *next;
  85. struct Curl_one_easy *prev;
  86. struct SessionHandle *easy_handle; /* the easy handle for this unit */
  87. struct connectdata *easy_conn; /* the "unit's" connection */
  88. CURLMstate state; /* the handle's state */
  89. CURLcode result; /* previous result */
  90. struct Curl_message *msg; /* A pointer to one single posted message.
  91. Cleanup should be done on this pointer NOT on
  92. the linked list in Curl_multi. This message
  93. will be deleted when this handle is removed
  94. from the multi-handle */
  95. int msg_num; /* number of messages left in 'msg' to return */
  96. /* Array with the plain socket numbers this handle takes care of, in no
  97. particular order. Note that all sockets are added to the sockhash, where
  98. the state etc are also kept. This array is mostly used to detect when a
  99. socket is to be removed from the hash. See singlesocket(). */
  100. curl_socket_t sockets[MAX_SOCKSPEREASYHANDLE];
  101. int numsocks;
  102. };
  103. #define CURL_MULTI_HANDLE 0x000bab1e
  104. #define GOOD_MULTI_HANDLE(x) \
  105. ((x)&&(((struct Curl_multi *)x)->type == CURL_MULTI_HANDLE))
  106. #define GOOD_EASY_HANDLE(x) \
  107. (((struct SessionHandle *)x)->magic == CURLEASY_MAGIC_NUMBER)
  108. /* This is the struct known as CURLM on the outside */
  109. struct Curl_multi {
  110. /* First a simple identifier to easier detect if a user mix up
  111. this multi handle with an easy handle. Set this to CURL_MULTI_HANDLE. */
  112. long type;
  113. /* We have a linked list with easy handles */
  114. struct Curl_one_easy easy;
  115. int num_easy; /* amount of entries in the linked list above. */
  116. int num_msgs; /* amount of messages in the easy handles */
  117. int num_alive; /* amount of easy handles that are added but have not yet
  118. reached COMPLETE state */
  119. /* callback function and user data pointer for the *socket() API */
  120. curl_socket_callback socket_cb;
  121. void *socket_userp;
  122. /* Hostname cache */
  123. struct curl_hash *hostcache;
  124. /* timetree points to the splay-tree of time nodes to figure out expire
  125. times of all currently set timers */
  126. struct Curl_tree *timetree;
  127. /* 'sockhash' is the lookup hash for socket descriptor => easy handles (note
  128. the pluralis form, there can be more than one easy handle waiting on the
  129. same actual socket) */
  130. struct curl_hash *sockhash;
  131. /* Whether pipelining is enabled for this multi handle */
  132. bool pipelining_enabled;
  133. /* shared connection cache */
  134. struct conncache *connc;
  135. /* list of easy handles kept around for doing nice connection closures */
  136. struct closure *closure;
  137. /* timer callback and user data pointer for the *socket() API */
  138. curl_multi_timer_callback timer_cb;
  139. void *timer_userp;
  140. time_t timer_lastcall; /* the fixed time for the timeout for the previous
  141. callback */
  142. };
  143. static bool multi_conn_using(struct Curl_multi *multi,
  144. struct SessionHandle *data);
  145. static void singlesocket(struct Curl_multi *multi,
  146. struct Curl_one_easy *easy);
  147. static void add_closure(struct Curl_multi *multi,
  148. struct SessionHandle *data);
  149. static int update_timer(struct Curl_multi *multi);
  150. #ifdef CURLDEBUG
  151. static const char *statename[]={
  152. "INIT",
  153. "CONNECT",
  154. "WAITRESOLVE",
  155. "WAITCONNECT",
  156. "PROTOCONNECT",
  157. "WAITDO",
  158. "DO",
  159. "DOING",
  160. "DO_MORE",
  161. "DO_DONE",
  162. "WAITPERFORM",
  163. "PERFORM",
  164. "TOOFAST",
  165. "DONE",
  166. "COMPLETED",
  167. "CANCELLED"
  168. };
  169. void curl_multi_dump(CURLM *multi_handle);
  170. #endif
  171. /* always use this function to change state, to make debugging easier */
  172. static void multistate(struct Curl_one_easy *easy, CURLMstate state)
  173. {
  174. #ifdef CURLDEBUG
  175. long index = -1;
  176. #endif
  177. CURLMstate oldstate = easy->state;
  178. if(oldstate == state)
  179. /* don't bother when the new state is the same as the old state */
  180. return;
  181. easy->state = state;
  182. #ifdef CURLDEBUG
  183. if(easy->state > CURLM_STATE_CONNECT &&
  184. easy->state < CURLM_STATE_COMPLETED)
  185. index = easy->easy_conn->connectindex;
  186. infof(easy->easy_handle,
  187. "STATE: %s => %s handle %p; (connection #%ld) \n",
  188. statename[oldstate], statename[easy->state],
  189. (char *)easy, index);
  190. #endif
  191. if(state == CURLM_STATE_COMPLETED)
  192. /* changing to COMPLETED means there's one less easy handle 'alive' */
  193. easy->easy_handle->multi->num_alive--;
  194. }
  195. /*
  196. * We add one of these structs to the sockhash for a particular socket
  197. */
  198. struct Curl_sh_entry {
  199. struct SessionHandle *easy;
  200. time_t timestamp;
  201. long inuse;
  202. int action; /* what action READ/WRITE this socket waits for */
  203. curl_socket_t socket; /* mainly to ease debugging */
  204. void *socketp; /* settable by users with curl_multi_assign() */
  205. };
  206. /* bits for 'action' having no bits means this socket is not expecting any
  207. action */
  208. #define SH_READ 1
  209. #define SH_WRITE 2
  210. /* make sure this socket is present in the hash for this handle */
  211. static struct Curl_sh_entry *sh_addentry(struct curl_hash *sh,
  212. curl_socket_t s,
  213. struct SessionHandle *data)
  214. {
  215. struct Curl_sh_entry *there =
  216. Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
  217. struct Curl_sh_entry *check;
  218. if(there)
  219. /* it is present, return fine */
  220. return there;
  221. /* not present, add it */
  222. check = calloc(sizeof(struct Curl_sh_entry), 1);
  223. if(!check)
  224. return NULL; /* major failure */
  225. check->easy = data;
  226. check->socket = s;
  227. /* make/add new hash entry */
  228. if(NULL == Curl_hash_add(sh, (char *)&s, sizeof(curl_socket_t), check)) {
  229. free(check);
  230. return NULL; /* major failure */
  231. }
  232. return check; /* things are good in sockhash land */
  233. }
  234. /* delete the given socket + handle from the hash */
  235. static void sh_delentry(struct curl_hash *sh, curl_socket_t s)
  236. {
  237. struct Curl_sh_entry *there =
  238. Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
  239. if(there) {
  240. /* this socket is in the hash */
  241. /* We remove the hash entry. (This'll end up in a call to
  242. sh_freeentry().) */
  243. Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t));
  244. }
  245. }
  246. /*
  247. * free a sockhash entry
  248. */
  249. static void sh_freeentry(void *freethis)
  250. {
  251. struct Curl_sh_entry *p = (struct Curl_sh_entry *) freethis;
  252. free(p);
  253. }
  254. /*
  255. * sh_init() creates a new socket hash and returns the handle for it.
  256. *
  257. * Quote from README.multi_socket:
  258. *
  259. * "Some tests at 7000 and 9000 connections showed that the socket hash lookup
  260. * is somewhat of a bottle neck. Its current implementation may be a bit too
  261. * limiting. It simply has a fixed-size array, and on each entry in the array
  262. * it has a linked list with entries. So the hash only checks which list to
  263. * scan through. The code I had used so for used a list with merely 7 slots
  264. * (as that is what the DNS hash uses) but with 7000 connections that would
  265. * make an average of 1000 nodes in each list to run through. I upped that to
  266. * 97 slots (I believe a prime is suitable) and noticed a significant speed
  267. * increase. I need to reconsider the hash implementation or use a rather
  268. * large default value like this. At 9000 connections I was still below 10us
  269. * per call."
  270. *
  271. */
  272. static struct curl_hash *sh_init(void)
  273. {
  274. return Curl_hash_alloc(97, sh_freeentry);
  275. }
  276. CURLM *curl_multi_init(void)
  277. {
  278. struct Curl_multi *multi = (void *)calloc(sizeof(struct Curl_multi), 1);
  279. if(!multi)
  280. return NULL;
  281. multi->type = CURL_MULTI_HANDLE;
  282. multi->hostcache = Curl_mk_dnscache();
  283. if(!multi->hostcache) {
  284. /* failure, free mem and bail out */
  285. free(multi);
  286. return NULL;
  287. }
  288. multi->sockhash = sh_init();
  289. if(!multi->sockhash) {
  290. /* failure, free mem and bail out */
  291. Curl_hash_destroy(multi->hostcache);
  292. free(multi);
  293. return NULL;
  294. }
  295. multi->connc = Curl_mk_connc(CONNCACHE_MULTI, -1);
  296. if(!multi->connc) {
  297. Curl_hash_destroy(multi->hostcache);
  298. free(multi);
  299. return NULL;
  300. }
  301. return (CURLM *) multi;
  302. }
  303. CURLMcode curl_multi_add_handle(CURLM *multi_handle,
  304. CURL *easy_handle)
  305. {
  306. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  307. struct Curl_one_easy *easy;
  308. struct closure *cl;
  309. struct closure *prev=NULL;
  310. /* First, make some basic checks that the CURLM handle is a good handle */
  311. if(!GOOD_MULTI_HANDLE(multi))
  312. return CURLM_BAD_HANDLE;
  313. /* Verify that we got a somewhat good easy handle too */
  314. if(!GOOD_EASY_HANDLE(easy_handle))
  315. return CURLM_BAD_EASY_HANDLE;
  316. /* Prevent users to add the same handle more than once! */
  317. if(((struct SessionHandle *)easy_handle)->multi)
  318. /* possibly we should create a new unique error code for this condition */
  319. return CURLM_BAD_EASY_HANDLE;
  320. /* Now, time to add an easy handle to the multi stack */
  321. easy = (struct Curl_one_easy *)calloc(sizeof(struct Curl_one_easy), 1);
  322. if(!easy)
  323. return CURLM_OUT_OF_MEMORY;
  324. cl = multi->closure;
  325. while(cl) {
  326. struct closure *next = cl->next;
  327. if(cl->easy_handle == (struct SessionHandle *)easy_handle) {
  328. /* remove this handle from the closure list */
  329. free(cl);
  330. if(prev)
  331. prev->next = next;
  332. else
  333. multi->closure = next;
  334. break; /* no need to continue since this handle can only be present once
  335. in the list */
  336. }
  337. cl = next;
  338. }
  339. /* set the easy handle */
  340. easy->easy_handle = easy_handle;
  341. multistate(easy, CURLM_STATE_INIT);
  342. /* for multi interface connections, we share DNS cache automatically if the
  343. easy handle's one is currently private. */
  344. if (easy->easy_handle->dns.hostcache &&
  345. (easy->easy_handle->dns.hostcachetype == HCACHE_PRIVATE)) {
  346. Curl_hash_destroy(easy->easy_handle->dns.hostcache);
  347. easy->easy_handle->dns.hostcache = NULL;
  348. easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
  349. }
  350. if (!easy->easy_handle->dns.hostcache ||
  351. (easy->easy_handle->dns.hostcachetype == HCACHE_NONE)) {
  352. easy->easy_handle->dns.hostcache = multi->hostcache;
  353. easy->easy_handle->dns.hostcachetype = HCACHE_MULTI;
  354. }
  355. if(easy->easy_handle->state.connc) {
  356. if(easy->easy_handle->state.connc->type == CONNCACHE_PRIVATE) {
  357. /* kill old private version */
  358. Curl_rm_connc(easy->easy_handle->state.connc);
  359. /* point out our shared one instead */
  360. easy->easy_handle->state.connc = multi->connc;
  361. }
  362. /* else it is already using multi? */
  363. }
  364. else
  365. /* point out our shared one */
  366. easy->easy_handle->state.connc = multi->connc;
  367. /* Make sure the type is setup correctly */
  368. easy->easy_handle->state.connc->type = CONNCACHE_MULTI;
  369. /* We add this new entry first in the list. We make our 'next' point to the
  370. previous next and our 'prev' point back to the 'first' struct */
  371. easy->next = multi->easy.next;
  372. easy->prev = &multi->easy;
  373. /* make 'easy' the first node in the chain */
  374. multi->easy.next = easy;
  375. /* if there was a next node, make sure its 'prev' pointer links back to
  376. the new node */
  377. if(easy->next)
  378. easy->next->prev = easy;
  379. Curl_easy_addmulti(easy_handle, multi_handle);
  380. /* make the SessionHandle struct refer back to this struct */
  381. easy->easy_handle->set.one_easy = easy;
  382. /* increase the node-counter */
  383. multi->num_easy++;
  384. if((multi->num_easy * 4) > multi->connc->num) {
  385. /* We want the connection cache to have plenty room. Before we supported
  386. the shared cache every single easy handle had 5 entries in their cache
  387. by default. */
  388. CURLcode res = Curl_ch_connc(easy_handle, multi->connc,
  389. multi->connc->num*4);
  390. if(res != CURLE_OK)
  391. /* TODO: we need to do some cleaning up here! */
  392. return CURLM_OUT_OF_MEMORY;
  393. }
  394. /* increase the alive-counter */
  395. multi->num_alive++;
  396. update_timer(multi);
  397. return CURLM_OK;
  398. }
  399. #if 0
  400. /* Debug-function, used like this:
  401. *
  402. * Curl_hash_print(multi->sockhash, debug_print_sock_hash);
  403. *
  404. * Enable the hash print function first by editing hash.c
  405. */
  406. static void debug_print_sock_hash(void *p)
  407. {
  408. struct Curl_sh_entry *sh = (struct Curl_sh_entry *)p;
  409. fprintf(stderr, " [easy %p/magic %x/socket %d]",
  410. (void *)sh->easy, sh->easy->magic, sh->socket);
  411. }
  412. #endif
  413. CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
  414. CURL *curl_handle)
  415. {
  416. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  417. struct Curl_one_easy *easy;
  418. /* First, make some basic checks that the CURLM handle is a good handle */
  419. if(!GOOD_MULTI_HANDLE(multi))
  420. return CURLM_BAD_HANDLE;
  421. /* Verify that we got a somewhat good easy handle too */
  422. if(!GOOD_EASY_HANDLE(curl_handle))
  423. return CURLM_BAD_EASY_HANDLE;
  424. /* scan through the list and remove the 'curl_handle' */
  425. easy = multi->easy.next;
  426. while(easy) {
  427. if(easy->easy_handle == (struct SessionHandle *)curl_handle)
  428. break;
  429. easy=easy->next;
  430. }
  431. if(easy) {
  432. bool premature = easy->state != CURLM_STATE_COMPLETED;
  433. /* If the 'state' is not INIT or COMPLETED, we might need to do something
  434. nice to put the easy_handle in a good known state when this returns. */
  435. if(premature)
  436. /* this handle is "alive" so we need to count down the total number of
  437. alive connections when this is removed */
  438. multi->num_alive--;
  439. if (easy->easy_handle->state.is_in_pipeline &&
  440. easy->state > CURLM_STATE_DO) {
  441. /* If the handle is in a pipeline and has finished sending off its
  442. request, we need to remember the fact that we want to remove this
  443. handle but do the actual removal at a later time */
  444. easy->easy_handle->state.cancelled = TRUE;
  445. return CURLM_OK;
  446. }
  447. /* The timer must be shut down before easy->multi is set to NULL,
  448. else the timenode will remain in the splay tree after
  449. curl_easy_cleanup is called. */
  450. Curl_expire(easy->easy_handle, 0);
  451. if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
  452. /* clear out the usage of the shared DNS cache */
  453. easy->easy_handle->dns.hostcache = NULL;
  454. easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
  455. }
  456. /* if we have a connection we must call Curl_done() here so that we
  457. don't leave a half-baked one around */
  458. if(easy->easy_conn) {
  459. /* Set up the association right */
  460. easy->easy_conn->data = easy->easy_handle;
  461. /* Curl_done() clears the conn->data field to lose the association
  462. between the easy handle and the connection */
  463. Curl_done(&easy->easy_conn, easy->result, premature);
  464. if(easy->easy_conn)
  465. /* the connection is still alive, set back the association to enable
  466. the check below to trigger TRUE */
  467. easy->easy_conn->data = easy->easy_handle;
  468. }
  469. /* If this easy_handle was the last one in charge for one or more
  470. connections a the shared connection cache, we might need to keep this
  471. handle around until either A) the connection is closed and killed
  472. properly, or B) another easy_handle uses the connection.
  473. The reason why we need to have a easy_handle associated with a live
  474. connection is simply that some connections will need a handle to get
  475. closed down properly. Currently, the only connections that need to keep
  476. a easy_handle handle around are using FTP(S). Such connections have
  477. the PROT_CLOSEACTION bit set.
  478. Thus, we need to check for all connections in the shared cache that
  479. points to this handle and are using PROT_CLOSEACTION. If there's any,
  480. we need to add this handle to the list of "easy handles kept around for
  481. nice connection closures".
  482. */
  483. if(multi_conn_using(multi, easy->easy_handle)) {
  484. /* There's at least one connection using this handle so we must keep
  485. this handle around. We also keep the connection cache pointer
  486. pointing to the shared one since that will be used on close as
  487. well. */
  488. easy->easy_handle->state.shared_conn = multi;
  489. /* this handle is still being used by a shared connection cache and
  490. thus we leave it around for now */
  491. add_closure(multi, easy->easy_handle);
  492. }
  493. if(easy->easy_handle->state.connc->type == CONNCACHE_MULTI) {
  494. /* if this was using the shared connection cache we clear the pointer
  495. to that since we're not part of that handle anymore */
  496. easy->easy_handle->state.connc = NULL;
  497. /* and modify the connectindex since this handle can't point to the
  498. connection cache anymore */
  499. if(easy->easy_conn)
  500. easy->easy_conn->connectindex = -1;
  501. }
  502. /* change state without using multistate(), only to make singlesocket() do
  503. what we want */
  504. easy->state = CURLM_STATE_COMPLETED;
  505. singlesocket(multi, easy); /* to let the application know what sockets
  506. that vanish with this handle */
  507. Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association
  508. to this multi handle */
  509. /* make the previous node point to our next */
  510. if(easy->prev)
  511. easy->prev->next = easy->next;
  512. /* make our next point to our previous node */
  513. if(easy->next)
  514. easy->next->prev = easy->prev;
  515. easy->easy_handle->set.one_easy = NULL; /* detached */
  516. /* NOTE NOTE NOTE
  517. We do not touch the easy handle here! */
  518. if (easy->msg)
  519. free(easy->msg);
  520. free(easy);
  521. multi->num_easy--; /* one less to care about now */
  522. update_timer(multi);
  523. return CURLM_OK;
  524. }
  525. else
  526. return CURLM_BAD_EASY_HANDLE; /* twasn't found */
  527. }
  528. bool Curl_multi_canPipeline(struct Curl_multi* multi)
  529. {
  530. return multi->pipelining_enabled;
  531. }
  532. static int waitconnect_getsock(struct connectdata *conn,
  533. curl_socket_t *sock,
  534. int numsocks)
  535. {
  536. if(!numsocks)
  537. return GETSOCK_BLANK;
  538. sock[0] = conn->sock[FIRSTSOCKET];
  539. return GETSOCK_WRITESOCK(0);
  540. }
  541. static int domore_getsock(struct connectdata *conn,
  542. curl_socket_t *sock,
  543. int numsocks)
  544. {
  545. if(!numsocks)
  546. return GETSOCK_BLANK;
  547. /* When in DO_MORE state, we could be either waiting for us
  548. to connect to a remote site, or we could wait for that site
  549. to connect to us. It makes a difference in the way: if we
  550. connect to the site we wait for the socket to become writable, if
  551. the site connects to us we wait for it to become readable */
  552. sock[0] = conn->sock[SECONDARYSOCKET];
  553. return GETSOCK_WRITESOCK(0);
  554. }
  555. /* returns bitmapped flags for this handle and its sockets */
  556. static int multi_getsock(struct Curl_one_easy *easy,
  557. curl_socket_t *socks, /* points to numsocks number
  558. of sockets */
  559. int numsocks)
  560. {
  561. if (easy->easy_handle->state.pipe_broke) {
  562. return 0;
  563. }
  564. if (easy->state > CURLM_STATE_CONNECT &&
  565. easy->state < CURLM_STATE_COMPLETED) {
  566. /* Set up ownership correctly */
  567. easy->easy_conn->data = easy->easy_handle;
  568. }
  569. switch(easy->state) {
  570. case CURLM_STATE_TOOFAST: /* returns 0, so will not select. */
  571. default:
  572. /* this will get called with CURLM_STATE_COMPLETED when a handle is
  573. removed */
  574. return 0;
  575. case CURLM_STATE_WAITRESOLVE:
  576. return Curl_resolv_getsock(easy->easy_conn, socks, numsocks);
  577. case CURLM_STATE_PROTOCONNECT:
  578. return Curl_protocol_getsock(easy->easy_conn, socks, numsocks);
  579. case CURLM_STATE_DOING:
  580. return Curl_doing_getsock(easy->easy_conn, socks, numsocks);
  581. case CURLM_STATE_WAITCONNECT:
  582. return waitconnect_getsock(easy->easy_conn, socks, numsocks);
  583. case CURLM_STATE_DO_MORE:
  584. return domore_getsock(easy->easy_conn, socks, numsocks);
  585. case CURLM_STATE_PERFORM:
  586. case CURLM_STATE_WAITPERFORM:
  587. return Curl_single_getsock(easy->easy_conn, socks, numsocks);
  588. }
  589. }
  590. CURLMcode curl_multi_fdset(CURLM *multi_handle,
  591. fd_set *read_fd_set, fd_set *write_fd_set,
  592. fd_set *exc_fd_set, int *max_fd)
  593. {
  594. /* Scan through all the easy handles to get the file descriptors set.
  595. Some easy handles may not have connected to the remote host yet,
  596. and then we must make sure that is done. */
  597. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  598. struct Curl_one_easy *easy;
  599. int this_max_fd=-1;
  600. curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
  601. int bitmap;
  602. int i;
  603. (void)exc_fd_set; /* not used */
  604. if(!GOOD_MULTI_HANDLE(multi))
  605. return CURLM_BAD_HANDLE;
  606. easy=multi->easy.next;
  607. while(easy) {
  608. bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);
  609. for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
  610. curl_socket_t s = CURL_SOCKET_BAD;
  611. if(bitmap & GETSOCK_READSOCK(i)) {
  612. FD_SET(sockbunch[i], read_fd_set);
  613. s = sockbunch[i];
  614. }
  615. if(bitmap & GETSOCK_WRITESOCK(i)) {
  616. FD_SET(sockbunch[i], write_fd_set);
  617. s = sockbunch[i];
  618. }
  619. if(s == CURL_SOCKET_BAD)
  620. /* this socket is unused, break out of loop */
  621. break;
  622. else {
  623. if((int)s > this_max_fd)
  624. this_max_fd = (int)s;
  625. }
  626. }
  627. easy = easy->next; /* check next handle */
  628. }
  629. *max_fd = this_max_fd;
  630. return CURLM_OK;
  631. }
  632. static CURLMcode multi_runsingle(struct Curl_multi *multi,
  633. struct Curl_one_easy *easy)
  634. {
  635. struct Curl_message *msg = NULL;
  636. bool connected;
  637. bool async;
  638. bool protocol_connect;
  639. bool dophase_done;
  640. bool done;
  641. CURLMcode result = CURLM_OK;
  642. struct Curl_transfer_keeper *k;
  643. do {
  644. if(!GOOD_EASY_HANDLE(easy->easy_handle))
  645. return CURLM_BAD_EASY_HANDLE;
  646. if (easy->easy_handle->state.pipe_broke) {
  647. infof(easy->easy_handle, "Pipe broke: handle 0x%x, url = %s\n",
  648. easy, easy->easy_handle->reqdata.path);
  649. if(easy->easy_handle->state.is_in_pipeline) {
  650. /* Head back to the CONNECT state */
  651. multistate(easy, CURLM_STATE_CONNECT);
  652. result = CURLM_CALL_MULTI_PERFORM;
  653. easy->result = CURLE_OK;
  654. } else {
  655. easy->result = CURLE_COULDNT_CONNECT;
  656. multistate(easy, CURLM_STATE_COMPLETED);
  657. }
  658. easy->easy_handle->state.pipe_broke = FALSE;
  659. easy->easy_conn = NULL;
  660. break;
  661. }
  662. if (easy->state > CURLM_STATE_CONNECT &&
  663. easy->state < CURLM_STATE_COMPLETED) {
  664. /* Make sure we set the connection's current owner */
  665. easy->easy_conn->data = easy->easy_handle;
  666. }
  667. if (CURLM_STATE_WAITCONNECT <= easy->state &&
  668. easy->state <= CURLM_STATE_DO &&
  669. easy->easy_handle->change.url_changed) {
  670. char *gotourl;
  671. Curl_posttransfer(easy->easy_handle);
  672. easy->result = Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
  673. /* We make sure that the pipe broken flag is reset
  674. because in this case, it isn't an actual break */
  675. easy->easy_handle->state.pipe_broke = FALSE;
  676. if(CURLE_OK == easy->result) {
  677. gotourl = strdup(easy->easy_handle->change.url);
  678. if(gotourl) {
  679. easy->easy_handle->change.url_changed = FALSE;
  680. easy->result = Curl_follow(easy->easy_handle, gotourl, FALSE);
  681. if(CURLE_OK == easy->result)
  682. multistate(easy, CURLM_STATE_CONNECT);
  683. else
  684. free(gotourl);
  685. }
  686. else {
  687. easy->result = CURLE_OUT_OF_MEMORY;
  688. multistate(easy, CURLM_STATE_COMPLETED);
  689. break;
  690. }
  691. }
  692. }
  693. easy->easy_handle->change.url_changed = FALSE;
  694. switch(easy->state) {
  695. case CURLM_STATE_INIT:
  696. /* init this transfer. */
  697. easy->result=Curl_pretransfer(easy->easy_handle);
  698. if(CURLE_OK == easy->result) {
  699. /* after init, go CONNECT */
  700. multistate(easy, CURLM_STATE_CONNECT);
  701. result = CURLM_CALL_MULTI_PERFORM;
  702. easy->easy_handle->state.used_interface = Curl_if_multi;
  703. }
  704. break;
  705. case CURLM_STATE_CONNECT:
  706. /* Connect. We get a connection identifier filled in. */
  707. Curl_pgrsTime(easy->easy_handle, TIMER_STARTSINGLE);
  708. easy->result = Curl_connect(easy->easy_handle, &easy->easy_conn,
  709. &async, &protocol_connect);
  710. if(CURLE_OK == easy->result) {
  711. /* Add this handle to the send pipeline */
  712. Curl_addHandleToPipeline(easy->easy_handle,
  713. easy->easy_conn->send_pipe);
  714. if(async)
  715. /* We're now waiting for an asynchronous name lookup */
  716. multistate(easy, CURLM_STATE_WAITRESOLVE);
  717. else {
  718. /* after the connect has been sent off, go WAITCONNECT unless the
  719. protocol connect is already done and we can go directly to
  720. WAITDO! */
  721. result = CURLM_CALL_MULTI_PERFORM;
  722. if(protocol_connect) {
  723. multistate(easy, CURLM_STATE_WAITDO);
  724. } else {
  725. multistate(easy, CURLM_STATE_WAITCONNECT);
  726. }
  727. }
  728. }
  729. break;
  730. case CURLM_STATE_WAITRESOLVE:
  731. /* awaiting an asynch name resolve to complete */
  732. {
  733. struct Curl_dns_entry *dns = NULL;
  734. /* check if we have the name resolved by now */
  735. easy->result = Curl_is_resolved(easy->easy_conn, &dns);
  736. if(dns) {
  737. /* Perform the next step in the connection phase, and then move on
  738. to the WAITCONNECT state */
  739. easy->result = Curl_async_resolved(easy->easy_conn,
  740. &protocol_connect);
  741. if(CURLE_OK != easy->result)
  742. /* if Curl_async_resolved() returns failure, the connection struct
  743. is already freed and gone */
  744. easy->easy_conn = NULL; /* no more connection */
  745. else {
  746. /* call again please so that we get the next socket setup */
  747. result = CURLM_CALL_MULTI_PERFORM;
  748. if(protocol_connect)
  749. multistate(easy, CURLM_STATE_DO);
  750. else
  751. multistate(easy, CURLM_STATE_WAITCONNECT);
  752. }
  753. }
  754. if(CURLE_OK != easy->result) {
  755. /* failure detected */
  756. Curl_disconnect(easy->easy_conn); /* disconnect properly */
  757. easy->easy_conn = NULL; /* no more connection */
  758. break;
  759. }
  760. }
  761. break;
  762. case CURLM_STATE_WAITCONNECT:
  763. /* awaiting a completion of an asynch connect */
  764. easy->result = Curl_is_connected(easy->easy_conn,
  765. FIRSTSOCKET,
  766. &connected);
  767. if(connected)
  768. easy->result = Curl_protocol_connect(easy->easy_conn,
  769. &protocol_connect);
  770. if(CURLE_OK != easy->result) {
  771. /* failure detected */
  772. Curl_disconnect(easy->easy_conn); /* close the connection */
  773. easy->easy_conn = NULL; /* no more connection */
  774. break;
  775. }
  776. if(connected) {
  777. if(!protocol_connect) {
  778. /* We have a TCP connection, but 'protocol_connect' may be false
  779. and then we continue to 'STATE_PROTOCONNECT'. If protocol
  780. connect is TRUE, we move on to STATE_DO. */
  781. multistate(easy, CURLM_STATE_PROTOCONNECT);
  782. }
  783. else {
  784. /* after the connect has completed, go WAITDO */
  785. multistate(easy, CURLM_STATE_WAITDO);
  786. result = CURLM_CALL_MULTI_PERFORM;
  787. }
  788. }
  789. break;
  790. case CURLM_STATE_PROTOCONNECT:
  791. /* protocol-specific connect phase */
  792. easy->result = Curl_protocol_connecting(easy->easy_conn,
  793. &protocol_connect);
  794. if(protocol_connect) {
  795. /* after the connect has completed, go WAITDO */
  796. multistate(easy, CURLM_STATE_WAITDO);
  797. result = CURLM_CALL_MULTI_PERFORM;
  798. }
  799. else if(easy->result) {
  800. /* failure detected */
  801. Curl_posttransfer(easy->easy_handle);
  802. Curl_done(&easy->easy_conn, easy->result, FALSE);
  803. Curl_disconnect(easy->easy_conn); /* close the connection */
  804. easy->easy_conn = NULL; /* no more connection */
  805. }
  806. break;
  807. case CURLM_STATE_WAITDO:
  808. /* Wait for our turn to DO when we're pipelining requests */
  809. #ifdef CURLDEBUG
  810. infof(easy->easy_handle, "Conn %d send pipe %d inuse %d athead %d\n",
  811. easy->easy_conn->connectindex,
  812. easy->easy_conn->send_pipe->size,
  813. easy->easy_conn->writechannel_inuse,
  814. Curl_isHandleAtHead(easy->easy_handle,
  815. easy->easy_conn->send_pipe));
  816. #endif
  817. if (!easy->easy_conn->writechannel_inuse &&
  818. Curl_isHandleAtHead(easy->easy_handle,
  819. easy->easy_conn->send_pipe)) {
  820. /* Grab the channel */
  821. easy->easy_conn->writechannel_inuse = TRUE;
  822. multistate(easy, CURLM_STATE_DO);
  823. result = CURLM_CALL_MULTI_PERFORM;
  824. }
  825. break;
  826. case CURLM_STATE_DO:
  827. if(easy->easy_handle->set.connect_only) {
  828. /* keep connection open for application to use the socket */
  829. easy->easy_conn->bits.close = FALSE;
  830. multistate(easy, CURLM_STATE_DONE);
  831. easy->result = CURLE_OK;
  832. result = CURLM_OK;
  833. }
  834. else {
  835. /* Perform the protocol's DO action */
  836. easy->result = Curl_do(&easy->easy_conn,
  837. &dophase_done);
  838. if(CURLE_OK == easy->result) {
  839. if(!dophase_done) {
  840. /* DO was not completed in one function call, we must continue
  841. DOING... */
  842. multistate(easy, CURLM_STATE_DOING);
  843. result = CURLM_OK;
  844. }
  845. /* after DO, go DO_DONE... or DO_MORE */
  846. else if(easy->easy_conn->bits.do_more) {
  847. /* we're supposed to do more, but we need to sit down, relax
  848. and wait a little while first */
  849. multistate(easy, CURLM_STATE_DO_MORE);
  850. result = CURLM_OK;
  851. }
  852. else {
  853. /* we're done with the DO, now DO_DONE */
  854. easy->result = Curl_readwrite_init(easy->easy_conn);
  855. if(CURLE_OK == easy->result) {
  856. multistate(easy, CURLM_STATE_DO_DONE);
  857. result = CURLM_CALL_MULTI_PERFORM;
  858. }
  859. }
  860. }
  861. else {
  862. /* failure detected */
  863. Curl_posttransfer(easy->easy_handle);
  864. Curl_done(&easy->easy_conn, easy->result, FALSE);
  865. Curl_disconnect(easy->easy_conn); /* close the connection */
  866. easy->easy_conn = NULL; /* no more connection */
  867. }
  868. }
  869. break;
  870. case CURLM_STATE_DOING:
  871. /* we continue DOING until the DO phase is complete */
  872. easy->result = Curl_protocol_doing(easy->easy_conn,
  873. &dophase_done);
  874. if(CURLE_OK == easy->result) {
  875. if(dophase_done) {
  876. /* after DO, go PERFORM... or DO_MORE */
  877. if(easy->easy_conn->bits.do_more) {
  878. /* we're supposed to do more, but we need to sit down, relax
  879. and wait a little while first */
  880. multistate(easy, CURLM_STATE_DO_MORE);
  881. result = CURLM_OK;
  882. }
  883. else {
  884. /* we're done with the DO, now DO_DONE */
  885. easy->result = Curl_readwrite_init(easy->easy_conn);
  886. if(CURLE_OK == easy->result) {
  887. multistate(easy, CURLM_STATE_DO_DONE);
  888. result = CURLM_CALL_MULTI_PERFORM;
  889. }
  890. }
  891. } /* dophase_done */
  892. }
  893. else {
  894. /* failure detected */
  895. Curl_posttransfer(easy->easy_handle);
  896. Curl_done(&easy->easy_conn, easy->result, FALSE);
  897. Curl_disconnect(easy->easy_conn); /* close the connection */
  898. easy->easy_conn = NULL; /* no more connection */
  899. }
  900. break;
  901. case CURLM_STATE_DO_MORE:
  902. /* Ready to do more? */
  903. easy->result = Curl_is_connected(easy->easy_conn,
  904. SECONDARYSOCKET,
  905. &connected);
  906. if(connected) {
  907. /*
  908. * When we are connected, DO MORE and then go DO_DONE
  909. */
  910. easy->result = Curl_do_more(easy->easy_conn);
  911. if(CURLE_OK == easy->result)
  912. easy->result = Curl_readwrite_init(easy->easy_conn);
  913. else
  914. /* Remove ourselves from the send pipeline */
  915. Curl_removeHandleFromPipeline(easy->easy_handle,
  916. easy->easy_conn->send_pipe);
  917. if(CURLE_OK == easy->result) {
  918. multistate(easy, CURLM_STATE_DO_DONE);
  919. result = CURLM_CALL_MULTI_PERFORM;
  920. }
  921. }
  922. break;
  923. case CURLM_STATE_DO_DONE:
  924. /* Remove ourselves from the send pipeline */
  925. Curl_removeHandleFromPipeline(easy->easy_handle,
  926. easy->easy_conn->send_pipe);
  927. /* Add ourselves to the recv pipeline */
  928. Curl_addHandleToPipeline(easy->easy_handle,
  929. easy->easy_conn->recv_pipe);
  930. multistate(easy, CURLM_STATE_WAITPERFORM);
  931. result = CURLM_CALL_MULTI_PERFORM;
  932. break;
  933. case CURLM_STATE_WAITPERFORM:
  934. #ifdef CURLDEBUG
  935. infof(easy->easy_handle, "Conn %d recv pipe %d inuse %d athead %d\n",
  936. easy->easy_conn->connectindex,
  937. easy->easy_conn->recv_pipe->size,
  938. easy->easy_conn->readchannel_inuse,
  939. Curl_isHandleAtHead(easy->easy_handle,
  940. easy->easy_conn->recv_pipe));
  941. #endif
  942. /* Wait for our turn to PERFORM */
  943. if (!easy->easy_conn->readchannel_inuse &&
  944. Curl_isHandleAtHead(easy->easy_handle,
  945. easy->easy_conn->recv_pipe)) {
  946. /* Grab the channel */
  947. easy->easy_conn->readchannel_inuse = TRUE;
  948. multistate(easy, CURLM_STATE_PERFORM);
  949. result = CURLM_CALL_MULTI_PERFORM;
  950. }
  951. break;
  952. case CURLM_STATE_TOOFAST: /* limit-rate exceeded in either direction */
  953. /* if both rates are within spec, resume transfer */
  954. Curl_pgrsUpdate(easy->easy_conn);
  955. if ( ( ( easy->easy_handle->set.max_send_speed == 0 ) ||
  956. ( easy->easy_handle->progress.ulspeed <
  957. easy->easy_handle->set.max_send_speed ) ) &&
  958. ( ( easy->easy_handle->set.max_recv_speed == 0 ) ||
  959. ( easy->easy_handle->progress.dlspeed <
  960. easy->easy_handle->set.max_recv_speed ) )
  961. )
  962. multistate(easy, CURLM_STATE_PERFORM);
  963. break;
  964. case CURLM_STATE_PERFORM:
  965. /* check if over speed */
  966. if ( ( ( easy->easy_handle->set.max_send_speed > 0 ) &&
  967. ( easy->easy_handle->progress.ulspeed >
  968. easy->easy_handle->set.max_send_speed ) ) ||
  969. ( ( easy->easy_handle->set.max_recv_speed > 0 ) &&
  970. ( easy->easy_handle->progress.dlspeed >
  971. easy->easy_handle->set.max_recv_speed ) )
  972. ) {
  973. /* Transfer is over the speed limit. Change state. TODO: Call
  974. * Curl_expire() with the time left until we're targeted to be below
  975. * the speed limit again. */
  976. multistate(easy, CURLM_STATE_TOOFAST );
  977. break;
  978. }
  979. /* read/write data if it is ready to do so */
  980. easy->result = Curl_readwrite(easy->easy_conn, &done);
  981. k = &easy->easy_handle->reqdata.keep;
  982. if (!(k->keepon & KEEP_READ)) {
  983. /* We're done reading */
  984. easy->easy_conn->readchannel_inuse = FALSE;
  985. }
  986. if (!(k->keepon & KEEP_WRITE)) {
  987. /* We're done writing */
  988. easy->easy_conn->writechannel_inuse = FALSE;
  989. }
  990. if(easy->result) {
  991. /* The transfer phase returned error, we mark the connection to get
  992. * closed to prevent being re-used. This is becasue we can't
  993. * possibly know if the connection is in a good shape or not now. */
  994. easy->easy_conn->bits.close = TRUE;
  995. if(CURL_SOCKET_BAD != easy->easy_conn->sock[SECONDARYSOCKET]) {
  996. /* if we failed anywhere, we must clean up the secondary socket if
  997. it was used */
  998. sclose(easy->easy_conn->sock[SECONDARYSOCKET]);
  999. easy->easy_conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
  1000. }
  1001. Curl_posttransfer(easy->easy_handle);
  1002. Curl_done(&easy->easy_conn, easy->result, FALSE);
  1003. }
  1004. else if(TRUE == done) {
  1005. char *newurl;
  1006. bool retry = Curl_retry_request(easy->easy_conn, &newurl);
  1007. /* call this even if the readwrite function returned error */
  1008. Curl_posttransfer(easy->easy_handle);
  1009. /* When we follow redirects, must to go back to the CONNECT state */
  1010. if(easy->easy_handle->reqdata.newurl || retry) {
  1011. Curl_removeHandleFromPipeline(easy->easy_handle,
  1012. easy->easy_conn->recv_pipe);
  1013. if(!retry) {
  1014. /* if the URL is a follow-location and not just a retried request
  1015. then figure out the URL here */
  1016. newurl = easy->easy_handle->reqdata.newurl;
  1017. easy->easy_handle->reqdata.newurl = NULL;
  1018. }
  1019. easy->result = Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
  1020. if(easy->result == CURLE_OK)
  1021. easy->result = Curl_follow(easy->easy_handle, newurl, retry);
  1022. if(CURLE_OK == easy->result) {
  1023. multistate(easy, CURLM_STATE_CONNECT);
  1024. result = CURLM_CALL_MULTI_PERFORM;
  1025. }
  1026. else
  1027. /* Since we "took it", we are in charge of freeing this on
  1028. failure */
  1029. free(newurl);
  1030. }
  1031. else {
  1032. /* after the transfer is done, go DONE */
  1033. multistate(easy, CURLM_STATE_DONE);
  1034. result = CURLM_CALL_MULTI_PERFORM;
  1035. }
  1036. }
  1037. break;
  1038. case CURLM_STATE_DONE:
  1039. /* Remove ourselves from the receive pipeline */
  1040. Curl_removeHandleFromPipeline(easy->easy_handle,
  1041. easy->easy_conn->recv_pipe);
  1042. easy->easy_handle->state.is_in_pipeline = FALSE;
  1043. if (easy->easy_conn->bits.stream_was_rewound) {
  1044. /* This request read past its response boundary so we quickly
  1045. let the other requests consume those bytes since there is no
  1046. guarantee that the socket will become active again */
  1047. result = CURLM_CALL_MULTI_PERFORM;
  1048. }
  1049. if (!easy->easy_handle->state.cancelled) {
  1050. /* post-transfer command */
  1051. easy->result = Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
  1052. /* after we have DONE what we're supposed to do, go COMPLETED, and
  1053. it doesn't matter what the Curl_done() returned! */
  1054. multistate(easy, CURLM_STATE_COMPLETED);
  1055. }
  1056. break;
  1057. case CURLM_STATE_COMPLETED:
  1058. if (easy->easy_handle->state.cancelled)
  1059. /* Go into the CANCELLED state if we were cancelled */
  1060. multistate(easy, CURLM_STATE_CANCELLED);
  1061. /* this is a completed transfer, it is likely to still be connected */
  1062. /* This node should be delinked from the list now and we should post
  1063. an information message that we are complete. */
  1064. break;
  1065. case CURLM_STATE_CANCELLED:
  1066. /* Cancelled transfer, wait to be cleaned up */
  1067. break;
  1068. default:
  1069. return CURLM_INTERNAL_ERROR;
  1070. }
  1071. if(CURLM_STATE_COMPLETED != easy->state) {
  1072. if(CURLE_OK != easy->result) {
  1073. /*
  1074. * If an error was returned, and we aren't in completed state now,
  1075. * then we go to completed and consider this transfer aborted.
  1076. */
  1077. easy->easy_handle->state.is_in_pipeline = FALSE;
  1078. easy->easy_handle->state.pipe_broke = FALSE;
  1079. if(easy->easy_conn) {
  1080. /* if this has a connection, unsubscribe from the pipelines */
  1081. easy->easy_conn->writechannel_inuse = FALSE;
  1082. easy->easy_conn->readchannel_inuse = FALSE;
  1083. }
  1084. multistate(easy, CURLM_STATE_COMPLETED);
  1085. }
  1086. }
  1087. } while (easy->easy_handle->change.url_changed);
  1088. if ((CURLM_STATE_COMPLETED == easy->state) && !easy->msg) {
  1089. if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
  1090. /* clear out the usage of the shared DNS cache */
  1091. easy->easy_handle->dns.hostcache = NULL;
  1092. easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
  1093. }
  1094. /* now add a node to the Curl_message linked list with this info */
  1095. msg = (struct Curl_message *)malloc(sizeof(struct Curl_message));
  1096. if(!msg)
  1097. return CURLM_OUT_OF_MEMORY;
  1098. msg->extmsg.msg = CURLMSG_DONE;
  1099. msg->extmsg.easy_handle = easy->easy_handle;
  1100. msg->extmsg.data.result = easy->result;
  1101. msg->next = NULL;
  1102. easy->msg = msg;
  1103. easy->msg_num = 1; /* there is one unread message here */
  1104. multi->num_msgs++; /* increase message counter */
  1105. }
  1106. return result;
  1107. }
  1108. CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
  1109. {
  1110. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1111. struct Curl_one_easy *easy;
  1112. CURLMcode returncode=CURLM_OK;
  1113. struct Curl_tree *t;
  1114. if(!GOOD_MULTI_HANDLE(multi))
  1115. return CURLM_BAD_HANDLE;
  1116. easy=multi->easy.next;
  1117. while(easy) {
  1118. CURLMcode result;
  1119. if (easy->easy_handle->state.cancelled &&
  1120. easy->state == CURLM_STATE_CANCELLED) {
  1121. /* Remove cancelled handles once it's safe to do so */
  1122. Curl_multi_rmeasy(multi_handle, easy->easy_handle);
  1123. easy->easy_handle = NULL;
  1124. easy = easy->next;
  1125. continue;
  1126. }
  1127. result = multi_runsingle(multi, easy);
  1128. if(result)
  1129. returncode = result;
  1130. easy = easy->next; /* operate on next handle */
  1131. }
  1132. /*
  1133. * Simply remove all expired timers from the splay since handles are dealt
  1134. * with unconditionally by this function and curl_multi_timeout() requires
  1135. * that already passed/handled expire times are removed from the splay.
  1136. */
  1137. do {
  1138. struct timeval now = Curl_tvnow();
  1139. int key = now.tv_sec; /* drop the usec part */
  1140. multi->timetree = Curl_splaygetbest(key, multi->timetree, &t);
  1141. if (t) {
  1142. struct SessionHandle *d = t->payload;
  1143. struct timeval* tv = &d->state.expiretime;
  1144. /* clear the expire times within the handles that we remove from the
  1145. splay tree */
  1146. tv->tv_sec = 0;
  1147. tv->tv_usec = 0;
  1148. }
  1149. } while(t);
  1150. *running_handles = multi->num_alive;
  1151. if ( CURLM_OK == returncode )
  1152. update_timer(multi);
  1153. return returncode;
  1154. }
  1155. /* This is called when an easy handle is cleanup'ed that is part of a multi
  1156. handle */
  1157. void Curl_multi_rmeasy(void *multi_handle, CURL *easy_handle)
  1158. {
  1159. curl_multi_remove_handle(multi_handle, easy_handle);
  1160. }
  1161. CURLMcode curl_multi_cleanup(CURLM *multi_handle)
  1162. {
  1163. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1164. struct Curl_one_easy *easy;
  1165. struct Curl_one_easy *nexteasy;
  1166. int i;
  1167. struct closure *cl;
  1168. struct closure *n;
  1169. if(GOOD_MULTI_HANDLE(multi)) {
  1170. multi->type = 0; /* not good anymore */
  1171. Curl_hash_destroy(multi->hostcache);
  1172. Curl_hash_destroy(multi->sockhash);
  1173. /* go over all connections that have close actions */
  1174. for(i=0; i< multi->connc->num; i++) {
  1175. if(multi->connc->connects[i] &&
  1176. multi->connc->connects[i]->protocol & PROT_CLOSEACTION) {
  1177. Curl_disconnect(multi->connc->connects[i]);
  1178. multi->connc->connects[i] = NULL;
  1179. }
  1180. }
  1181. /* now walk through the list of handles we kept around only to be
  1182. able to close connections "properly" */
  1183. cl = multi->closure;
  1184. while(cl) {
  1185. cl->easy_handle->state.shared_conn = NULL; /* no more shared */
  1186. if(cl->easy_handle->state.closed)
  1187. /* close handle only if curl_easy_cleanup() already has been called
  1188. for this easy handle */
  1189. Curl_close(cl->easy_handle);
  1190. n = cl->next;
  1191. free(cl);
  1192. cl= n;
  1193. }
  1194. Curl_rm_connc(multi->connc);
  1195. /* remove all easy handles */
  1196. easy = multi->easy.next;
  1197. while(easy) {
  1198. nexteasy=easy->next;
  1199. if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
  1200. /* clear out the usage of the shared DNS cache */
  1201. easy->easy_handle->dns.hostcache = NULL;
  1202. easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
  1203. }
  1204. /* Clear the pointer to the connection cache */
  1205. easy->easy_handle->state.connc = NULL;
  1206. Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association */
  1207. if (easy->msg)
  1208. free(easy->msg);
  1209. free(easy);
  1210. easy = nexteasy;
  1211. }
  1212. free(multi);
  1213. return CURLM_OK;
  1214. }
  1215. else
  1216. return CURLM_BAD_HANDLE;
  1217. }
  1218. CURLMsg *curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
  1219. {
  1220. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1221. *msgs_in_queue = 0; /* default to none */
  1222. if(GOOD_MULTI_HANDLE(multi)) {
  1223. struct Curl_one_easy *easy;
  1224. if(!multi->num_msgs)
  1225. return NULL; /* no messages left to return */
  1226. easy=multi->easy.next;
  1227. while(easy) {
  1228. if(easy->msg_num) {
  1229. easy->msg_num--;
  1230. break;
  1231. }
  1232. easy = easy->next;
  1233. }
  1234. if(!easy)
  1235. return NULL; /* this means internal count confusion really */
  1236. multi->num_msgs--;
  1237. *msgs_in_queue = multi->num_msgs;
  1238. return &easy->msg->extmsg;
  1239. }
  1240. else
  1241. return NULL;
  1242. }
  1243. /*
  1244. * singlesocket() checks what sockets we deal with and their "action state"
  1245. * and if we have a different state in any of those sockets from last time we
  1246. * call the callback accordingly.
  1247. */
  1248. static void singlesocket(struct Curl_multi *multi,
  1249. struct Curl_one_easy *easy)
  1250. {
  1251. curl_socket_t socks[MAX_SOCKSPEREASYHANDLE];
  1252. int i;
  1253. struct Curl_sh_entry *entry;
  1254. curl_socket_t s;
  1255. int num;
  1256. unsigned int curraction;
  1257. memset(&socks, 0, sizeof(socks));
  1258. for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++)
  1259. socks[i] = CURL_SOCKET_BAD;
  1260. /* Fill in the 'current' struct with the state as it is now: what sockets to
  1261. supervise and for what actions */
  1262. curraction = multi_getsock(easy, socks, MAX_SOCKSPEREASYHANDLE);
  1263. /* We have 0 .. N sockets already and we get to know about the 0 .. M
  1264. sockets we should have from now on. Detect the differences, remove no
  1265. longer supervised ones and add new ones */
  1266. /* walk over the sockets we got right now */
  1267. for(i=0; (i< MAX_SOCKSPEREASYHANDLE) &&
  1268. (curraction & (GETSOCK_READSOCK(i) | GETSOCK_WRITESOCK(i)));
  1269. i++) {
  1270. int action = CURL_POLL_NONE;
  1271. s = socks[i];
  1272. /* get it from the hash */
  1273. entry = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
  1274. if(curraction & GETSOCK_READSOCK(i))
  1275. action |= CURL_POLL_IN;
  1276. if(curraction & GETSOCK_WRITESOCK(i))
  1277. action |= CURL_POLL_OUT;
  1278. if(entry) {
  1279. /* yeps, already present so check if it has the same action set */
  1280. if(entry->action == action)
  1281. /* same, continue */
  1282. continue;
  1283. }
  1284. else {
  1285. /* this is a socket we didn't have before, add it! */
  1286. entry = sh_addentry(multi->sockhash, s, easy->easy_handle);
  1287. if(!entry)
  1288. /* fatal */
  1289. return;
  1290. }
  1291. multi->socket_cb(easy->easy_handle,
  1292. s,
  1293. action,
  1294. multi->socket_userp,
  1295. entry ? entry->socketp : NULL);
  1296. entry->action = action; /* store the current action state */
  1297. }
  1298. num = i; /* number of sockets */
  1299. /* when we've walked over all the sockets we should have right now, we must
  1300. make sure to detect sockets that are removed */
  1301. for(i=0; i< easy->numsocks; i++) {
  1302. int j;
  1303. s = easy->sockets[i];
  1304. for(j=0; j<num; j++) {
  1305. if(s == socks[j]) {
  1306. /* this is still supervised */
  1307. s = CURL_SOCKET_BAD;
  1308. break;
  1309. }
  1310. }
  1311. if(s != CURL_SOCKET_BAD) {
  1312. /* this socket has been removed. Remove it */
  1313. entry = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
  1314. if(entry) {
  1315. /* just a precaution, this socket really SHOULD be in the hash already
  1316. but in case it isn't, we don't have to tell the app to remove it
  1317. either since it never got to know about it */
  1318. multi->socket_cb(easy->easy_handle,
  1319. s,
  1320. CURL_POLL_REMOVE,
  1321. multi->socket_userp,
  1322. entry ? entry->socketp : NULL);
  1323. sh_delentry(multi->sockhash, s);
  1324. }
  1325. }
  1326. }
  1327. memcpy(easy->sockets, socks, num*sizeof(curl_socket_t));
  1328. easy->numsocks = num;
  1329. }
  1330. static CURLMcode multi_socket(struct Curl_multi *multi,
  1331. bool checkall,
  1332. curl_socket_t s,
  1333. int *running_handles)
  1334. {
  1335. CURLMcode result = CURLM_OK;
  1336. struct SessionHandle *data = NULL;
  1337. struct Curl_tree *t;
  1338. if(checkall) {
  1339. struct Curl_one_easy *easyp;
  1340. /* *perform() deals with running_handles on its own */
  1341. result = curl_multi_perform(multi, running_handles);
  1342. /* walk through each easy handle and do the socket state change magic
  1343. and callbacks */
  1344. easyp=multi->easy.next;
  1345. while(easyp) {
  1346. singlesocket(multi, easyp);
  1347. easyp = easyp->next;
  1348. }
  1349. /* or should we fall-through and do the timer-based stuff? */
  1350. return result;
  1351. }
  1352. else if (s != CURL_SOCKET_TIMEOUT) {
  1353. struct Curl_sh_entry *entry =
  1354. Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
  1355. if(!entry)
  1356. /* unmatched socket, major problemo! */
  1357. return CURLM_BAD_SOCKET; /* better return code? */
  1358. data = entry->easy;
  1359. if(data->magic != CURLEASY_MAGIC_NUMBER)
  1360. /* bad bad bad bad bad bad bad */
  1361. return CURLM_INTERNAL_ERROR;
  1362. result = multi_runsingle(multi, data->set.one_easy);
  1363. if(result == CURLM_OK)
  1364. /* get the socket(s) and check if the state has been changed since
  1365. last */
  1366. singlesocket(multi, data->set.one_easy);
  1367. /* Now we fall-through and do the timer-based stuff, since we don't want
  1368. to force the user to have to deal with timeouts as long as at least one
  1369. connection in fact has traffic. */
  1370. data = NULL; /* set data to NULL again to avoid calling multi_runsingle()
  1371. in case there's no need to */
  1372. }
  1373. /*
  1374. * The loop following here will go on as long as there are expire-times left
  1375. * to process in the splay and 'data' will be re-assigned for every expired
  1376. * handle we deal with.
  1377. */
  1378. do {
  1379. int key;
  1380. struct timeval now;
  1381. /* the first loop lap 'data' can be NULL */
  1382. if(data) {
  1383. result = multi_runsingle(multi, data->set.one_easy);
  1384. if(result == CURLM_OK)
  1385. /* get the socket(s) and check if the state has been changed since
  1386. last */
  1387. singlesocket(multi, data->set.one_easy);
  1388. }
  1389. /* Check if there's one (more) expired timer to deal with! This function
  1390. extracts a matching node if there is one */
  1391. now = Curl_tvnow();
  1392. key = now.tv_sec; /* drop the usec part */
  1393. multi->timetree = Curl_splaygetbest(key, multi->timetree, &t);
  1394. if(t) {
  1395. /* assign 'data' to be the easy handle we just removed from the splay
  1396. tree */
  1397. data = t->payload;
  1398. /* clear the expire time within the handle we removed from the
  1399. splay tree */
  1400. data->state.expiretime.tv_sec = 0;
  1401. data->state.expiretime.tv_usec = 0;
  1402. }
  1403. } while(t);
  1404. *running_handles = multi->num_alive;
  1405. return result;
  1406. }
  1407. CURLMcode curl_multi_setopt(CURLM *multi_handle,
  1408. CURLMoption option, ...)
  1409. {
  1410. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1411. CURLMcode res = CURLM_OK;
  1412. va_list param;
  1413. if(!GOOD_MULTI_HANDLE(multi))
  1414. return CURLM_BAD_HANDLE;
  1415. va_start(param, option);
  1416. switch(option) {
  1417. case CURLMOPT_SOCKETFUNCTION:
  1418. multi->socket_cb = va_arg(param, curl_socket_callback);
  1419. break;
  1420. case CURLMOPT_SOCKETDATA:
  1421. multi->socket_userp = va_arg(param, void *);
  1422. break;
  1423. case CURLMOPT_PIPELINING:
  1424. multi->pipelining_enabled = (bool)(0 != va_arg(param, long));
  1425. break;
  1426. case CURLMOPT_TIMERFUNCTION:
  1427. multi->timer_cb = va_arg(param, curl_multi_timer_callback);
  1428. break;
  1429. case CURLMOPT_TIMERDATA:
  1430. multi->timer_userp = va_arg(param, void *);
  1431. break;
  1432. default:
  1433. res = CURLM_UNKNOWN_OPTION;
  1434. break;
  1435. }
  1436. va_end(param);
  1437. return res;
  1438. }
  1439. CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
  1440. int *running_handles)
  1441. {
  1442. CURLMcode result = multi_socket((struct Curl_multi *)multi_handle, FALSE, s,
  1443. running_handles);
  1444. if (CURLM_OK == result)
  1445. update_timer((struct Curl_multi *)multi_handle);
  1446. return result;
  1447. }
  1448. CURLMcode curl_multi_socket_all(CURLM *multi_handle, int *running_handles)
  1449. {
  1450. CURLMcode result = multi_socket((struct Curl_multi *)multi_handle,
  1451. TRUE, CURL_SOCKET_BAD, running_handles);
  1452. if (CURLM_OK == result)
  1453. update_timer((struct Curl_multi *)multi_handle);
  1454. return result;
  1455. }
  1456. static CURLMcode multi_timeout(struct Curl_multi *multi,
  1457. long *timeout_ms)
  1458. {
  1459. if(multi->timetree) {
  1460. /* we have a tree of expire times */
  1461. struct timeval now = Curl_tvnow();
  1462. /* splay the lowest to the bottom */
  1463. multi->timetree = Curl_splay(0, multi->timetree);
  1464. /* At least currently, the splay key is a time_t for the expire time */
  1465. *timeout_ms = (multi->timetree->key - now.tv_sec) * 1000 -
  1466. now.tv_usec/1000;
  1467. if(*timeout_ms < 0)
  1468. /* 0 means immediately */
  1469. *timeout_ms = 0;
  1470. }
  1471. else
  1472. *timeout_ms = -1;
  1473. return CURLM_OK;
  1474. }
  1475. CURLMcode curl_multi_timeout(CURLM *multi_handle,
  1476. long *timeout_ms)
  1477. {
  1478. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1479. /* First, make some basic checks that the CURLM handle is a good handle */
  1480. if(!GOOD_MULTI_HANDLE(multi))
  1481. return CURLM_BAD_HANDLE;
  1482. return multi_timeout(multi, timeout_ms);
  1483. }
  1484. /*
  1485. * Tell the application it should update its timers, if it subscribes to the
  1486. * update timer callback.
  1487. */
  1488. static int update_timer(struct Curl_multi *multi)
  1489. {
  1490. long timeout_ms;
  1491. if (!multi->timer_cb)
  1492. return 0;
  1493. if ( multi_timeout(multi, &timeout_ms) != CURLM_OK )
  1494. return -1;
  1495. if ( timeout_ms < 0 )
  1496. return 0;
  1497. /* When multi_timeout() is done, multi->timetree points to the node with the
  1498. * timeout we got the (relative) time-out time for. We can thus easily check
  1499. * if this is the same (fixed) time as we got in a previous call and then
  1500. * avoid calling the callback again. */
  1501. if(multi->timetree->key == multi->timer_lastcall)
  1502. return 0;
  1503. multi->timer_lastcall = multi->timetree->key;
  1504. return multi->timer_cb((CURLM*)multi, timeout_ms, multi->timer_userp);
  1505. }
  1506. /* given a number of milliseconds from now to use to set the 'act before
  1507. this'-time for the transfer, to be extracted by curl_multi_timeout() */
  1508. void Curl_expire(struct SessionHandle *data, long milli)
  1509. {
  1510. struct Curl_multi *multi = data->multi;
  1511. struct timeval *nowp = &data->state.expiretime;
  1512. int rc;
  1513. /* this is only interesting for multi-interface using libcurl, and only
  1514. while there is still a multi interface struct remaining! */
  1515. if(!multi)
  1516. return;
  1517. if(!milli) {
  1518. /* No timeout, clear the time data. */
  1519. if(nowp->tv_sec) {
  1520. /* Since this is an cleared time, we must remove the previous entry from
  1521. the splay tree */
  1522. rc = Curl_splayremovebyaddr(multi->timetree,
  1523. &data->state.timenode,
  1524. &multi->timetree);
  1525. if(rc)
  1526. infof(data, "Internal error clearing splay node = %d\n", rc);
  1527. infof(data, "Expire cleared\n");
  1528. nowp->tv_sec = 0;
  1529. nowp->tv_usec = 0;
  1530. }
  1531. }
  1532. else {
  1533. struct timeval set;
  1534. int rest;
  1535. set = Curl_tvnow();
  1536. set.tv_sec += milli/1000;
  1537. set.tv_usec += (milli%1000)*1000;
  1538. rest = (int)(set.tv_usec - 1000000);
  1539. if(rest > 0) {
  1540. /* bigger than a full microsec */
  1541. set.tv_sec++;
  1542. set.tv_usec -= 1000000;
  1543. }
  1544. if(nowp->tv_sec) {
  1545. /* This means that the struct is added as a node in the splay tree.
  1546. Compare if the new time is earlier, and only remove-old/add-new if it
  1547. is. */
  1548. long diff = curlx_tvdiff(set, *nowp);
  1549. if(diff > 0)
  1550. /* the new expire time was later so we don't change this */
  1551. return;
  1552. /* Since this is an updated time, we must remove the previous entry from
  1553. the splay tree first and then re-add the new value */
  1554. rc = Curl_splayremovebyaddr(multi->timetree,
  1555. &data->state.timenode,
  1556. &multi->timetree);
  1557. if(rc)
  1558. infof(data, "Internal error removing splay node = %d\n", rc);
  1559. }
  1560. *nowp = set;
  1561. #if 0
  1562. infof(data, "Expire at %ld / %ld (%ldms)\n",
  1563. (long)nowp->tv_sec, (long)nowp->tv_usec, milli);
  1564. #endif
  1565. data->state.timenode.payload = data;
  1566. multi->timetree = Curl_splayinsert((int)nowp->tv_sec,
  1567. multi->timetree,
  1568. &data->state.timenode);
  1569. }
  1570. #if 0
  1571. Curl_splayprint(multi->timetree, 0, TRUE);
  1572. #endif
  1573. }
  1574. CURLMcode curl_multi_assign(CURLM *multi_handle,
  1575. curl_socket_t s, void *hashp)
  1576. {
  1577. struct Curl_sh_entry *there = NULL;
  1578. struct Curl_multi *multi = (struct Curl_multi *)multi_handle;
  1579. if(s != CURL_SOCKET_BAD)
  1580. there = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(curl_socket_t));
  1581. if(!there)
  1582. return CURLM_BAD_SOCKET;
  1583. there->socketp = hashp;
  1584. return CURLM_OK;
  1585. }
  1586. static bool multi_conn_using(struct Curl_multi *multi,
  1587. struct SessionHandle *data)
  1588. {
  1589. /* any live CLOSEACTION-connections pointing to the give 'data' ? */
  1590. int i;
  1591. for(i=0; i< multi->connc->num; i++) {
  1592. if(multi->connc->connects[i] &&
  1593. (multi->connc->connects[i]->data == data) &&
  1594. multi->connc->connects[i]->protocol & PROT_CLOSEACTION)
  1595. return TRUE;
  1596. }
  1597. return FALSE;
  1598. }
  1599. /* Add the given data pointer to the list of 'closure handles' that are kept
  1600. around only to be able to close some connections nicely - just make sure
  1601. that this handle isn't already added, like for the cases when an easy
  1602. handle is removed, added and removed again... */
  1603. static void add_closure(struct Curl_multi *multi,
  1604. struct SessionHandle *data)
  1605. {
  1606. int i;
  1607. struct closure *cl = (struct closure *)calloc(sizeof(struct closure), 1);
  1608. struct closure *p=NULL;
  1609. struct closure *n;
  1610. if(cl) {
  1611. cl->easy_handle = data;
  1612. cl->next = multi->closure;
  1613. multi->closure = cl;
  1614. }
  1615. p = multi->closure;
  1616. cl = p->next; /* start immediately on the second since the first is the one
  1617. we just added and it is _very_ likely to actually exist
  1618. used in the cache since that's the whole purpose of adding
  1619. it to this list! */
  1620. /* When adding, scan through all the other currently kept handles and see if
  1621. there are any connections still referring to them and kill them if not. */
  1622. while(cl) {
  1623. bool inuse = FALSE;
  1624. for(i=0; i< multi->connc->num; i++) {
  1625. if(multi->connc->connects[i] &&
  1626. (multi->connc->connects[i]->data == cl->easy_handle)) {
  1627. inuse = TRUE;
  1628. break;
  1629. }
  1630. }
  1631. n = cl->next;
  1632. if(!inuse) {
  1633. /* cl->easy_handle is now killable */
  1634. infof(data, "Delayed kill of easy handle %p\n", cl->easy_handle);
  1635. /* unmark it as not having a connection around that uses it anymore */
  1636. cl->easy_handle->state.shared_conn= NULL;
  1637. Curl_close(cl->easy_handle);
  1638. if(p)
  1639. p->next = n;
  1640. else
  1641. multi->closure = n;
  1642. free(cl);
  1643. }
  1644. else
  1645. p = cl;
  1646. cl = n;
  1647. }
  1648. }
  1649. #ifdef CURLDEBUG
  1650. void curl_multi_dump(CURLM *multi_handle)
  1651. {
  1652. struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
  1653. struct Curl_one_easy *easy;
  1654. int i;
  1655. fprintf(stderr, "* Multi status: %d handles, %d alive\n",
  1656. multi->num_easy, multi->num_alive);
  1657. for(easy=multi->easy.next; easy; easy = easy->next) {
  1658. if(easy->state != CURLM_STATE_COMPLETED) {
  1659. /* only display handles that are not completed */
  1660. fprintf(stderr, "handle %p, state %s, %d sockets\n",
  1661. (void *)easy->easy_handle,
  1662. statename[easy->state], easy->numsocks);
  1663. for(i=0; i < easy->numsocks; i++) {
  1664. curl_socket_t s = easy->sockets[i];
  1665. struct Curl_sh_entry *entry =
  1666. Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
  1667. fprintf(stderr, "%d ", (int)s);
  1668. if(!entry) {
  1669. fprintf(stderr, "INTERNAL CONFUSION\n");
  1670. continue;
  1671. }
  1672. fprintf(stderr, "[%s %s] ",
  1673. entry->action&CURL_POLL_IN?"RECVING":"",
  1674. entry->action&CURL_POLL_OUT?"SENDING":"");
  1675. }
  1676. if(easy->numsocks)
  1677. fprintf(stderr, "\n");
  1678. }
  1679. }
  1680. }
  1681. #endif