multi.c 73 KB

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