00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "setup.h"
00025
00026 #include <stdlib.h>
00027 #include <string.h>
00028
00029 #ifdef HAVE_SYS_SOCKET_H
00030 #include <sys/socket.h>
00031 #endif
00032 #ifdef HAVE_UNISTD_H
00033 #include <unistd.h>
00034 #endif
00035
00036 #include <curl/curl.h>
00037
00038 #include "urldata.h"
00039 #include "transfer.h"
00040 #include "url.h"
00041 #include "connect.h"
00042 #include "progress.h"
00043 #include "memory.h"
00044 #include "easyif.h"
00045 #include "multiif.h"
00046 #include "sendf.h"
00047 #include "timeval.h"
00048 #include "http.h"
00049
00050
00051 #include "memdebug.h"
00052
00053
00054
00055
00056
00057
00058 #ifndef CURL_SOCKET_HASH_TABLE_SIZE
00059 #define CURL_SOCKET_HASH_TABLE_SIZE 911
00060 #endif
00061
00062 struct Curl_message {
00063
00064 struct CURLMsg extmsg;
00065 struct Curl_message *next;
00066 };
00067
00068
00069
00070
00071 typedef enum {
00072 CURLM_STATE_INIT,
00073 CURLM_STATE_CONNECT,
00074 CURLM_STATE_WAITRESOLVE,
00075 CURLM_STATE_WAITCONNECT,
00076 CURLM_STATE_WAITPROXYCONNECT,
00077 CURLM_STATE_PROTOCONNECT,
00078 CURLM_STATE_WAITDO,
00079 CURLM_STATE_DO,
00080 CURLM_STATE_DOING,
00081 CURLM_STATE_DO_MORE,
00082 CURLM_STATE_DO_DONE,
00083 CURLM_STATE_WAITPERFORM,
00084 CURLM_STATE_PERFORM,
00085 CURLM_STATE_TOOFAST,
00086 CURLM_STATE_DONE,
00087 CURLM_STATE_COMPLETED,
00088 CURLM_STATE_CANCELLED,
00089
00090 CURLM_STATE_LAST
00091 } CURLMstate;
00092
00093
00094
00095 #define MAX_SOCKSPEREASYHANDLE 5
00096 #define GETSOCK_READABLE (0x00ff)
00097 #define GETSOCK_WRITABLE (0xff00)
00098
00099 struct closure {
00100 struct closure *next;
00101 struct SessionHandle *easy_handle;
00102 };
00103
00104 struct Curl_one_easy {
00105
00106 struct Curl_one_easy *next;
00107 struct Curl_one_easy *prev;
00108
00109 struct SessionHandle *easy_handle;
00110 struct connectdata *easy_conn;
00111
00112 CURLMstate state;
00113 CURLcode result;
00114
00115 struct Curl_message *msg;
00116
00117
00118
00119
00120 int msg_num;
00121
00122
00123
00124
00125
00126 curl_socket_t sockets[MAX_SOCKSPEREASYHANDLE];
00127 int numsocks;
00128 };
00129
00130 #define CURL_MULTI_HANDLE 0x000bab1e
00131
00132 #define GOOD_MULTI_HANDLE(x) \
00133 ((x)&&(((struct Curl_multi *)(x))->type == CURL_MULTI_HANDLE))
00134 #define GOOD_EASY_HANDLE(x) \
00135 (((struct SessionHandle *)(x))->magic == CURLEASY_MAGIC_NUMBER)
00136
00137
00138 struct Curl_multi {
00139
00140
00141 long type;
00142
00143
00144 struct Curl_one_easy easy;
00145
00146 int num_easy;
00147 int num_msgs;
00148 int num_alive;
00149
00150
00151
00152 curl_socket_callback socket_cb;
00153 void *socket_userp;
00154
00155
00156 struct curl_hash *hostcache;
00157
00158
00159
00160 struct Curl_tree *timetree;
00161
00162
00163
00164
00165 struct curl_hash *sockhash;
00166
00167
00168 bool pipelining_enabled;
00169
00170
00171 struct conncache *connc;
00172 long maxconnects;
00173
00174
00175
00176 struct closure *closure;
00177
00178
00179 curl_multi_timer_callback timer_cb;
00180 void *timer_userp;
00181 time_t timer_lastcall;
00182
00183 };
00184
00185 static bool multi_conn_using(struct Curl_multi *multi,
00186 struct SessionHandle *data);
00187 static void singlesocket(struct Curl_multi *multi,
00188 struct Curl_one_easy *easy);
00189 static void add_closure(struct Curl_multi *multi,
00190 struct SessionHandle *data);
00191 static int update_timer(struct Curl_multi *multi);
00192
00193 #ifdef CURLDEBUG
00194 static const char * const statename[]={
00195 "INIT",
00196 "CONNECT",
00197 "WAITRESOLVE",
00198 "WAITCONNECT",
00199 "WAITPROXYCONNECT",
00200 "PROTOCONNECT",
00201 "WAITDO",
00202 "DO",
00203 "DOING",
00204 "DO_MORE",
00205 "DO_DONE",
00206 "WAITPERFORM",
00207 "PERFORM",
00208 "TOOFAST",
00209 "DONE",
00210 "COMPLETED",
00211 "CANCELLED"
00212 };
00213
00214 void curl_multi_dump(CURLM *multi_handle);
00215 #endif
00216
00217
00218 static void multistate(struct Curl_one_easy *easy, CURLMstate state)
00219 {
00220 #ifdef CURLDEBUG
00221 long index = -5000;
00222 #endif
00223 CURLMstate oldstate = easy->state;
00224
00225 if(oldstate == state)
00226
00227 return;
00228
00229 easy->state = state;
00230
00231 #ifdef CURLDEBUG
00232 if(easy->state > CURLM_STATE_CONNECT &&
00233 easy->state < CURLM_STATE_COMPLETED)
00234 index = easy->easy_conn->connectindex;
00235
00236 infof(easy->easy_handle,
00237 "STATE: %s => %s handle %p; (connection #%ld) \n",
00238 statename[oldstate], statename[easy->state],
00239 (char *)easy, index);
00240 #endif
00241 if(state == CURLM_STATE_COMPLETED)
00242
00243 easy->easy_handle->multi->num_alive--;
00244 }
00245
00246
00247
00248
00249
00250 struct Curl_sh_entry {
00251 struct SessionHandle *easy;
00252 time_t timestamp;
00253 long inuse;
00254 int action;
00255 curl_socket_t socket;
00256 void *socketp;
00257 };
00258
00259
00260 #define SH_READ 1
00261 #define SH_WRITE 2
00262
00263
00264 static struct Curl_sh_entry *sh_addentry(struct curl_hash *sh,
00265 curl_socket_t s,
00266 struct SessionHandle *data)
00267 {
00268 struct Curl_sh_entry *there =
00269 Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
00270 struct Curl_sh_entry *check;
00271
00272 if(there)
00273
00274 return there;
00275
00276
00277 check = calloc(sizeof(struct Curl_sh_entry), 1);
00278 if(!check)
00279 return NULL;
00280 check->easy = data;
00281 check->socket = s;
00282
00283
00284 if(NULL == Curl_hash_add(sh, (char *)&s, sizeof(curl_socket_t), check)) {
00285 free(check);
00286 return NULL;
00287 }
00288
00289 return check;
00290 }
00291
00292
00293
00294 static void sh_delentry(struct curl_hash *sh, curl_socket_t s)
00295 {
00296 struct Curl_sh_entry *there =
00297 Curl_hash_pick(sh, (char *)&s, sizeof(curl_socket_t));
00298
00299 if(there) {
00300
00301
00302
00303 Curl_hash_delete(sh, (char *)&s, sizeof(curl_socket_t));
00304 }
00305 }
00306
00307
00308
00309
00310 static void sh_freeentry(void *freethis)
00311 {
00312 struct Curl_sh_entry *p = (struct Curl_sh_entry *) freethis;
00313
00314 free(p);
00315 }
00316
00317 static size_t fd_key_compare(void*k1, size_t k1_len, void*k2, size_t k2_len)
00318 {
00319 (void) k1_len; (void) k2_len;
00320
00321 return ((*((int* ) k1)) == (*((int* ) k2))) ? 1 : 0;
00322 }
00323
00324 static size_t hash_fd(void* key, size_t key_length, size_t slots_num)
00325 {
00326 int fd = * ((int* ) key);
00327 (void) key_length;
00328
00329 return (fd % (int)slots_num);
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350 static struct curl_hash *sh_init(void)
00351 {
00352 return Curl_hash_alloc(CURL_SOCKET_HASH_TABLE_SIZE, hash_fd, fd_key_compare,
00353 sh_freeentry);
00354 }
00355
00356 CURLM *curl_multi_init(void)
00357 {
00358 struct Curl_multi *multi = (void *)calloc(sizeof(struct Curl_multi), 1);
00359
00360 if(!multi)
00361 return NULL;
00362
00363 multi->type = CURL_MULTI_HANDLE;
00364
00365 multi->hostcache = Curl_mk_dnscache();
00366 if(!multi->hostcache) {
00367
00368 free(multi);
00369 return NULL;
00370 }
00371
00372 multi->sockhash = sh_init();
00373 if(!multi->sockhash) {
00374
00375 Curl_hash_destroy(multi->hostcache);
00376 free(multi);
00377 return NULL;
00378 }
00379
00380 multi->connc = Curl_mk_connc(CONNCACHE_MULTI, -1);
00381 if(!multi->connc) {
00382 Curl_hash_destroy(multi->sockhash);
00383 Curl_hash_destroy(multi->hostcache);
00384 free(multi);
00385 return NULL;
00386 }
00387
00388
00389
00390
00391 multi->easy.next = &multi->easy;
00392 multi->easy.prev = &multi->easy;
00393
00394 return (CURLM *) multi;
00395 }
00396
00397 CURLMcode curl_multi_add_handle(CURLM *multi_handle,
00398 CURL *easy_handle)
00399 {
00400 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
00401 struct Curl_one_easy *easy;
00402 struct closure *cl;
00403 struct closure *prev=NULL;
00404
00405
00406 if(!GOOD_MULTI_HANDLE(multi))
00407 return CURLM_BAD_HANDLE;
00408
00409
00410 if(!GOOD_EASY_HANDLE(easy_handle))
00411 return CURLM_BAD_EASY_HANDLE;
00412
00413
00414 if(((struct SessionHandle *)easy_handle)->multi)
00415
00416 return CURLM_BAD_EASY_HANDLE;
00417
00418
00419 easy = (struct Curl_one_easy *)calloc(sizeof(struct Curl_one_easy), 1);
00420 if(!easy)
00421 return CURLM_OUT_OF_MEMORY;
00422
00423 cl = multi->closure;
00424 while(cl) {
00425 struct closure *next = cl->next;
00426 if(cl->easy_handle == (struct SessionHandle *)easy_handle) {
00427
00428 free(cl);
00429 if(prev)
00430 prev->next = next;
00431 else
00432 multi->closure = next;
00433 break;
00434
00435 }
00436 prev = cl;
00437 cl = next;
00438 }
00439
00440
00441 easy->easy_handle = easy_handle;
00442 multistate(easy, CURLM_STATE_INIT);
00443
00444
00445 easy->easy_handle->multi_pos = easy;
00446
00447
00448
00449 if (easy->easy_handle->dns.hostcache &&
00450 (easy->easy_handle->dns.hostcachetype == HCACHE_PRIVATE)) {
00451 Curl_hash_destroy(easy->easy_handle->dns.hostcache);
00452 easy->easy_handle->dns.hostcache = NULL;
00453 easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
00454 }
00455
00456 if (!easy->easy_handle->dns.hostcache ||
00457 (easy->easy_handle->dns.hostcachetype == HCACHE_NONE)) {
00458 easy->easy_handle->dns.hostcache = multi->hostcache;
00459 easy->easy_handle->dns.hostcachetype = HCACHE_MULTI;
00460 }
00461
00462 if(easy->easy_handle->state.connc) {
00463 if(easy->easy_handle->state.connc->type == CONNCACHE_PRIVATE) {
00464
00465 Curl_rm_connc(easy->easy_handle->state.connc);
00466
00467 easy->easy_handle->state.connc = multi->connc;
00468 }
00469
00470 }
00471 else
00472
00473 easy->easy_handle->state.connc = multi->connc;
00474
00475
00476 easy->easy_handle->state.connc->type = CONNCACHE_MULTI;
00477
00478
00479
00480
00481
00482
00483
00484 easy->next = &multi->easy;
00485 easy->prev = multi->easy.prev;
00486
00487
00488 multi->easy.prev = easy;
00489
00490
00491
00492 easy->prev->next = easy;
00493
00494 Curl_easy_addmulti(easy_handle, multi_handle);
00495
00496
00497 easy->easy_handle->set.one_easy = easy;
00498
00499
00500
00501
00502
00503
00504
00505 Curl_expire(easy->easy_handle, 10);
00506
00507
00508 multi->num_easy++;
00509
00510 if((multi->num_easy * 4) > multi->connc->num) {
00511
00512
00513
00514 int newmax = multi->num_easy * 4;
00515
00516 if(multi->maxconnects && (multi->maxconnects < newmax))
00517
00518 newmax = multi->maxconnects;
00519
00520 if(newmax > multi->connc->num) {
00521
00522 CURLcode res = Curl_ch_connc(easy_handle, multi->connc, newmax);
00523 if(res != CURLE_OK)
00524
00525 return CURLM_OUT_OF_MEMORY;
00526 }
00527 }
00528
00529
00530 multi->num_alive++;
00531
00532 update_timer(multi);
00533 return CURLM_OK;
00534 }
00535
00536 #if 0
00537
00538
00539
00540
00541
00542
00543 static void debug_print_sock_hash(void *p)
00544 {
00545 struct Curl_sh_entry *sh = (struct Curl_sh_entry *)p;
00546
00547 fprintf(stderr, " [easy %p/magic %x/socket %d]",
00548 (void *)sh->easy, sh->easy->magic, sh->socket);
00549 }
00550 #endif
00551
00552 CURLMcode curl_multi_remove_handle(CURLM *multi_handle,
00553 CURL *curl_handle)
00554 {
00555 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
00556 struct Curl_one_easy *easy;
00557
00558
00559 if(!GOOD_MULTI_HANDLE(multi))
00560 return CURLM_BAD_HANDLE;
00561
00562
00563 if(!GOOD_EASY_HANDLE(curl_handle))
00564 return CURLM_BAD_EASY_HANDLE;
00565
00566
00567 easy = ((struct SessionHandle *)curl_handle)->multi_pos;
00568
00569 if(easy) {
00570 bool premature = (bool)(easy->state != CURLM_STATE_COMPLETED);
00571
00572
00573
00574 if(premature)
00575
00576
00577 multi->num_alive--;
00578
00579 if (easy->easy_handle->state.is_in_pipeline &&
00580 easy->state > CURLM_STATE_DO &&
00581 easy->state < CURLM_STATE_COMPLETED) {
00582
00583
00584
00585
00586 easy->easy_handle->state.cancelled = TRUE;
00587 return CURLM_OK;
00588 }
00589
00590
00591
00592
00593 Curl_expire(easy->easy_handle, 0);
00594
00595 if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
00596
00597 easy->easy_handle->dns.hostcache = NULL;
00598 easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
00599 }
00600
00601
00602
00603 if(easy->easy_conn &&
00604 (easy->easy_conn->data == easy->easy_handle)) {
00605
00606
00607
00608 Curl_done(&easy->easy_conn, easy->result, premature);
00609
00610 if(easy->easy_conn)
00611
00612
00613 easy->easy_conn->data = easy->easy_handle;
00614 }
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632 if(multi_conn_using(multi, easy->easy_handle)) {
00633
00634
00635
00636
00637 easy->easy_handle->state.shared_conn = multi;
00638
00639
00640
00641 add_closure(multi, easy->easy_handle);
00642 }
00643
00644 if(easy->easy_handle->state.connc->type == CONNCACHE_MULTI) {
00645
00646
00647 easy->easy_handle->state.connc = NULL;
00648
00649
00650
00651 if(easy->easy_conn &&
00652 (easy->easy_conn->send_pipe->size +
00653 easy->easy_conn->recv_pipe->size == 0))
00654 easy->easy_conn->connectindex = -1;
00655 }
00656
00657
00658
00659 easy->state = CURLM_STATE_COMPLETED;
00660 singlesocket(multi, easy);
00661
00662
00663 Curl_easy_addmulti(easy->easy_handle, NULL);
00664
00665
00666
00667 if(easy->prev)
00668 easy->prev->next = easy->next;
00669
00670 if(easy->next)
00671 easy->next->prev = easy->prev;
00672
00673 easy->easy_handle->set.one_easy = NULL;
00674
00675
00676 easy->easy_handle->multi_pos = NULL;
00677
00678
00679
00680 if (easy->msg)
00681 free(easy->msg);
00682 free(easy);
00683
00684 multi->num_easy--;
00685
00686 update_timer(multi);
00687 return CURLM_OK;
00688 }
00689 else
00690 return CURLM_BAD_EASY_HANDLE;
00691 }
00692
00693 bool Curl_multi_canPipeline(struct Curl_multi* multi)
00694 {
00695 return multi->pipelining_enabled;
00696 }
00697
00698 void Curl_multi_handlePipeBreak(struct SessionHandle *data)
00699 {
00700 struct Curl_one_easy *one_easy = data->set.one_easy;
00701
00702 if (one_easy)
00703 one_easy->easy_conn = NULL;
00704 }
00705
00706 static int waitconnect_getsock(struct connectdata *conn,
00707 curl_socket_t *sock,
00708 int numsocks)
00709 {
00710 if(!numsocks)
00711 return GETSOCK_BLANK;
00712
00713 sock[0] = conn->sock[FIRSTSOCKET];
00714 return GETSOCK_WRITESOCK(0);
00715 }
00716
00717 static int domore_getsock(struct connectdata *conn,
00718 curl_socket_t *sock,
00719 int numsocks)
00720 {
00721 if(!numsocks)
00722 return GETSOCK_BLANK;
00723
00724
00725
00726
00727
00728
00729 sock[0] = conn->sock[SECONDARYSOCKET];
00730
00731 return GETSOCK_WRITESOCK(0);
00732 }
00733
00734
00735 static int multi_getsock(struct Curl_one_easy *easy,
00736 curl_socket_t *socks,
00737
00738 int numsocks)
00739 {
00740
00741
00742
00743
00744
00745
00746 if (easy->easy_handle->state.pipe_broke ||
00747 !easy->easy_conn) {
00748 return 0;
00749 }
00750
00751 if (easy->state > CURLM_STATE_CONNECT &&
00752 easy->state < CURLM_STATE_COMPLETED) {
00753
00754 easy->easy_conn->data = easy->easy_handle;
00755 }
00756
00757 switch(easy->state) {
00758 case CURLM_STATE_TOOFAST:
00759 default:
00760
00761
00762 return 0;
00763
00764 case CURLM_STATE_WAITRESOLVE:
00765 return Curl_resolv_getsock(easy->easy_conn, socks, numsocks);
00766
00767 case CURLM_STATE_PROTOCONNECT:
00768 return Curl_protocol_getsock(easy->easy_conn, socks, numsocks);
00769
00770 case CURLM_STATE_DOING:
00771 return Curl_doing_getsock(easy->easy_conn, socks, numsocks);
00772
00773 case CURLM_STATE_WAITCONNECT:
00774 return waitconnect_getsock(easy->easy_conn, socks, numsocks);
00775
00776 case CURLM_STATE_DO_MORE:
00777 return domore_getsock(easy->easy_conn, socks, numsocks);
00778
00779 case CURLM_STATE_PERFORM:
00780 case CURLM_STATE_WAITPERFORM:
00781 return Curl_single_getsock(easy->easy_conn, socks, numsocks);
00782 }
00783
00784 }
00785
00786 CURLMcode curl_multi_fdset(CURLM *multi_handle,
00787 fd_set *read_fd_set, fd_set *write_fd_set,
00788 fd_set *exc_fd_set, int *max_fd)
00789 {
00790
00791
00792
00793 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
00794 struct Curl_one_easy *easy;
00795 int this_max_fd=-1;
00796 curl_socket_t sockbunch[MAX_SOCKSPEREASYHANDLE];
00797 int bitmap;
00798 int i;
00799 (void)exc_fd_set;
00800
00801 if(!GOOD_MULTI_HANDLE(multi))
00802 return CURLM_BAD_HANDLE;
00803
00804 easy=multi->easy.next;
00805 while(easy != &multi->easy) {
00806 bitmap = multi_getsock(easy, sockbunch, MAX_SOCKSPEREASYHANDLE);
00807
00808 for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++) {
00809 curl_socket_t s = CURL_SOCKET_BAD;
00810
00811 if(bitmap & GETSOCK_READSOCK(i)) {
00812 FD_SET(sockbunch[i], read_fd_set);
00813 s = sockbunch[i];
00814 }
00815 if(bitmap & GETSOCK_WRITESOCK(i)) {
00816 FD_SET(sockbunch[i], write_fd_set);
00817 s = sockbunch[i];
00818 }
00819 if(s == CURL_SOCKET_BAD)
00820
00821 break;
00822 else {
00823 if((int)s > this_max_fd)
00824 this_max_fd = (int)s;
00825 }
00826 }
00827
00828 easy = easy->next;
00829 }
00830
00831 *max_fd = this_max_fd;
00832
00833 return CURLM_OK;
00834 }
00835
00836 static CURLMcode multi_runsingle(struct Curl_multi *multi,
00837 struct Curl_one_easy *easy)
00838 {
00839 struct Curl_message *msg = NULL;
00840 bool connected;
00841 bool async;
00842 bool protocol_connect = FALSE;
00843 bool dophase_done;
00844 bool done;
00845 CURLMcode result = CURLM_OK;
00846 struct Curl_transfer_keeper *k;
00847
00848 do {
00849 bool disconnect_conn = FALSE;
00850
00851 if(!GOOD_EASY_HANDLE(easy->easy_handle))
00852 return CURLM_BAD_EASY_HANDLE;
00853
00854
00855
00856 if (easy->easy_handle->state.pipe_broke) {
00857 infof(easy->easy_handle, "Pipe broke: handle 0x%x, url = %s\n",
00858 easy, easy->easy_handle->reqdata.path);
00859
00860 if(easy->easy_handle->state.is_in_pipeline) {
00861
00862 multistate(easy, CURLM_STATE_CONNECT);
00863 result = CURLM_CALL_MULTI_PERFORM;
00864 easy->result = CURLE_OK;
00865 }
00866 else {
00867 easy->result = CURLE_COULDNT_CONNECT;
00868 multistate(easy, CURLM_STATE_COMPLETED);
00869 }
00870
00871 easy->easy_handle->state.pipe_broke = FALSE;
00872 easy->easy_conn = NULL;
00873 break;
00874 }
00875
00876 if (easy->state > CURLM_STATE_CONNECT &&
00877 easy->state < CURLM_STATE_COMPLETED) {
00878
00879 easy->easy_conn->data = easy->easy_handle;
00880 }
00881
00882 if (CURLM_STATE_WAITCONNECT <= easy->state &&
00883 easy->state <= CURLM_STATE_DO &&
00884 easy->easy_handle->change.url_changed) {
00885 char *gotourl;
00886 Curl_posttransfer(easy->easy_handle);
00887
00888 easy->result = Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
00889
00890
00891 easy->easy_handle->state.pipe_broke = FALSE;
00892 if(CURLE_OK == easy->result) {
00893 gotourl = strdup(easy->easy_handle->change.url);
00894 if(gotourl) {
00895 easy->easy_handle->change.url_changed = FALSE;
00896 easy->result = Curl_follow(easy->easy_handle, gotourl, FALSE);
00897 if(CURLE_OK == easy->result)
00898 multistate(easy, CURLM_STATE_CONNECT);
00899 else
00900 free(gotourl);
00901 }
00902 else {
00903 easy->result = CURLE_OUT_OF_MEMORY;
00904 multistate(easy, CURLM_STATE_COMPLETED);
00905 break;
00906 }
00907 }
00908 }
00909
00910 easy->easy_handle->change.url_changed = FALSE;
00911
00912 switch(easy->state) {
00913 case CURLM_STATE_INIT:
00914
00915 easy->result=Curl_pretransfer(easy->easy_handle);
00916
00917 if(CURLE_OK == easy->result) {
00918
00919 multistate(easy, CURLM_STATE_CONNECT);
00920 result = CURLM_CALL_MULTI_PERFORM;
00921
00922 easy->easy_handle->state.used_interface = Curl_if_multi;
00923 }
00924 break;
00925
00926 case CURLM_STATE_CONNECT:
00927
00928 Curl_pgrsTime(easy->easy_handle, TIMER_STARTSINGLE);
00929 easy->result = Curl_connect(easy->easy_handle, &easy->easy_conn,
00930 &async, &protocol_connect);
00931
00932 if(CURLE_OK == easy->result) {
00933
00934 easy->result = Curl_addHandleToPipeline(easy->easy_handle,
00935 easy->easy_conn->send_pipe);
00936 if(CURLE_OK == easy->result) {
00937 if(async)
00938
00939 multistate(easy, CURLM_STATE_WAITRESOLVE);
00940 else {
00941
00942
00943
00944 result = CURLM_CALL_MULTI_PERFORM;
00945
00946 if(protocol_connect)
00947 multistate(easy, CURLM_STATE_WAITDO);
00948 else {
00949 #ifndef CURL_DISABLE_HTTP
00950 if (easy->easy_conn->bits.tunnel_connecting)
00951 multistate(easy, CURLM_STATE_WAITPROXYCONNECT);
00952 else
00953 #endif
00954 multistate(easy, CURLM_STATE_WAITCONNECT);
00955 }
00956 }
00957 }
00958 }
00959 break;
00960
00961 case CURLM_STATE_WAITRESOLVE:
00962
00963 {
00964 struct Curl_dns_entry *dns = NULL;
00965
00966
00967 easy->result = Curl_is_resolved(easy->easy_conn, &dns);
00968
00969 if(dns) {
00970
00971
00972 easy->result = Curl_async_resolved(easy->easy_conn,
00973 &protocol_connect);
00974
00975 if(CURLE_OK != easy->result)
00976
00977
00978 easy->easy_conn = NULL;
00979 else {
00980
00981 result = CURLM_CALL_MULTI_PERFORM;
00982 if(protocol_connect)
00983 multistate(easy, CURLM_STATE_WAITDO);
00984 else {
00985 #ifndef CURL_DISABLE_HTTP
00986 if (easy->easy_conn->bits.tunnel_connecting)
00987 multistate(easy, CURLM_STATE_WAITPROXYCONNECT);
00988 else
00989 #endif
00990 multistate(easy, CURLM_STATE_WAITCONNECT);
00991 }
00992 }
00993 }
00994
00995 if(CURLE_OK != easy->result) {
00996
00997 disconnect_conn = TRUE;
00998 break;
00999 }
01000 }
01001 break;
01002
01003 #ifndef CURL_DISABLE_HTTP
01004 case CURLM_STATE_WAITPROXYCONNECT:
01005
01006 easy->result = Curl_http_connect(easy->easy_conn, &protocol_connect);
01007
01008 if(CURLE_OK == easy->result) {
01009 if (!easy->easy_conn->bits.tunnel_connecting)
01010 multistate(easy, CURLM_STATE_WAITCONNECT);
01011 }
01012 break;
01013 #endif
01014
01015 case CURLM_STATE_WAITCONNECT:
01016
01017 easy->result = Curl_is_connected(easy->easy_conn,
01018 FIRSTSOCKET,
01019 &connected);
01020 if(connected)
01021 easy->result = Curl_protocol_connect(easy->easy_conn,
01022 &protocol_connect);
01023
01024 if(CURLE_OK != easy->result) {
01025
01026
01027 disconnect_conn = TRUE;
01028 break;
01029 }
01030
01031 if(connected) {
01032 if(!protocol_connect) {
01033
01034
01035
01036
01037
01038 #ifndef CURL_DISABLE_HTTP
01039 if (easy->easy_conn->bits.tunnel_connecting)
01040 multistate(easy, CURLM_STATE_WAITPROXYCONNECT);
01041 else
01042 #endif
01043 multistate(easy, CURLM_STATE_PROTOCONNECT);
01044 }
01045 else {
01046
01047 multistate(easy, CURLM_STATE_WAITDO);
01048
01049 result = CURLM_CALL_MULTI_PERFORM;
01050 }
01051 }
01052 break;
01053
01054 case CURLM_STATE_PROTOCONNECT:
01055
01056 easy->result = Curl_protocol_connecting(easy->easy_conn,
01057 &protocol_connect);
01058 if(protocol_connect) {
01059
01060 multistate(easy, CURLM_STATE_WAITDO);
01061 result = CURLM_CALL_MULTI_PERFORM;
01062 }
01063 else if(easy->result) {
01064
01065 Curl_posttransfer(easy->easy_handle);
01066 Curl_done(&easy->easy_conn, easy->result, FALSE);
01067 disconnect_conn = TRUE;
01068 }
01069 break;
01070
01071 case CURLM_STATE_WAITDO:
01072
01073 #ifdef CURLDEBUG
01074 infof(easy->easy_handle, "Conn %d send pipe %d inuse %d athead %d\n",
01075 easy->easy_conn->connectindex,
01076 easy->easy_conn->send_pipe->size,
01077 easy->easy_conn->writechannel_inuse,
01078 Curl_isHandleAtHead(easy->easy_handle,
01079 easy->easy_conn->send_pipe));
01080 #endif
01081 if (!easy->easy_conn->writechannel_inuse &&
01082 Curl_isHandleAtHead(easy->easy_handle,
01083 easy->easy_conn->send_pipe)) {
01084
01085 easy->easy_conn->writechannel_inuse = TRUE;
01086 multistate(easy, CURLM_STATE_DO);
01087 result = CURLM_CALL_MULTI_PERFORM;
01088 }
01089 break;
01090
01091 case CURLM_STATE_DO:
01092 if(easy->easy_handle->set.connect_only) {
01093
01094 easy->easy_conn->bits.close = FALSE;
01095 multistate(easy, CURLM_STATE_DONE);
01096 easy->result = CURLE_OK;
01097 result = CURLM_OK;
01098 }
01099 else {
01100
01101 easy->result = Curl_do(&easy->easy_conn,
01102 &dophase_done);
01103
01104 if(CURLE_OK == easy->result) {
01105
01106 if(!dophase_done) {
01107
01108
01109 multistate(easy, CURLM_STATE_DOING);
01110 result = CURLM_OK;
01111 }
01112
01113
01114 else if(easy->easy_conn->bits.do_more) {
01115
01116
01117 multistate(easy, CURLM_STATE_DO_MORE);
01118 result = CURLM_OK;
01119 }
01120 else {
01121
01122 easy->result = Curl_readwrite_init(easy->easy_conn);
01123 if(CURLE_OK == easy->result) {
01124 multistate(easy, CURLM_STATE_DO_DONE);
01125 result = CURLM_CALL_MULTI_PERFORM;
01126 }
01127 }
01128 }
01129 else {
01130
01131 Curl_posttransfer(easy->easy_handle);
01132 Curl_done(&easy->easy_conn, easy->result, FALSE);
01133 disconnect_conn = TRUE;
01134 }
01135 }
01136 break;
01137
01138 case CURLM_STATE_DOING:
01139
01140 easy->result = Curl_protocol_doing(easy->easy_conn,
01141 &dophase_done);
01142 if(CURLE_OK == easy->result) {
01143 if(dophase_done) {
01144
01145 if(easy->easy_conn->bits.do_more) {
01146
01147
01148 multistate(easy, CURLM_STATE_DO_MORE);
01149 result = CURLM_OK;
01150 }
01151 else {
01152
01153 easy->result = Curl_readwrite_init(easy->easy_conn);
01154 if(CURLE_OK == easy->result) {
01155 multistate(easy, CURLM_STATE_DO_DONE);
01156 result = CURLM_CALL_MULTI_PERFORM;
01157 }
01158 }
01159 }
01160 }
01161 else {
01162
01163 Curl_posttransfer(easy->easy_handle);
01164 Curl_done(&easy->easy_conn, easy->result, FALSE);
01165 disconnect_conn = TRUE;
01166 }
01167 break;
01168
01169 case CURLM_STATE_DO_MORE:
01170
01171 easy->result = Curl_is_connected(easy->easy_conn,
01172 SECONDARYSOCKET,
01173 &connected);
01174 if(connected) {
01175
01176
01177
01178 easy->result = Curl_do_more(easy->easy_conn);
01179
01180 if(CURLE_OK == easy->result)
01181 easy->result = Curl_readwrite_init(easy->easy_conn);
01182 else
01183
01184 Curl_removeHandleFromPipeline(easy->easy_handle,
01185 easy->easy_conn->send_pipe);
01186
01187 if(CURLE_OK == easy->result) {
01188 multistate(easy, CURLM_STATE_DO_DONE);
01189 result = CURLM_CALL_MULTI_PERFORM;
01190 }
01191 }
01192 break;
01193
01194 case CURLM_STATE_DO_DONE:
01195
01196 Curl_removeHandleFromPipeline(easy->easy_handle,
01197 easy->easy_conn->send_pipe);
01198
01199 easy->result = Curl_addHandleToPipeline(easy->easy_handle,
01200 easy->easy_conn->recv_pipe);
01201 multistate(easy, CURLM_STATE_WAITPERFORM);
01202 result = CURLM_CALL_MULTI_PERFORM;
01203
01204 Curl_pre_readwrite(easy->easy_conn);
01205
01206 break;
01207
01208 case CURLM_STATE_WAITPERFORM:
01209 #ifdef CURLDEBUG
01210 infof(easy->easy_handle, "Conn %d recv pipe %d inuse %d athead %d\n",
01211 easy->easy_conn->connectindex,
01212 easy->easy_conn->recv_pipe->size,
01213 easy->easy_conn->readchannel_inuse,
01214 Curl_isHandleAtHead(easy->easy_handle,
01215 easy->easy_conn->recv_pipe));
01216 #endif
01217
01218 if (!easy->easy_conn->readchannel_inuse &&
01219 Curl_isHandleAtHead(easy->easy_handle,
01220 easy->easy_conn->recv_pipe)) {
01221
01222 easy->easy_conn->readchannel_inuse = TRUE;
01223 multistate(easy, CURLM_STATE_PERFORM);
01224 result = CURLM_CALL_MULTI_PERFORM;
01225 }
01226 break;
01227
01228 case CURLM_STATE_TOOFAST:
01229
01230 Curl_pgrsUpdate(easy->easy_conn);
01231 if ( ( ( easy->easy_handle->set.max_send_speed == 0 ) ||
01232 ( easy->easy_handle->progress.ulspeed <
01233 easy->easy_handle->set.max_send_speed ) ) &&
01234 ( ( easy->easy_handle->set.max_recv_speed == 0 ) ||
01235 ( easy->easy_handle->progress.dlspeed <
01236 easy->easy_handle->set.max_recv_speed ) )
01237 )
01238 multistate(easy, CURLM_STATE_PERFORM);
01239 break;
01240
01241 case CURLM_STATE_PERFORM:
01242
01243 if ( ( ( easy->easy_handle->set.max_send_speed > 0 ) &&
01244 ( easy->easy_handle->progress.ulspeed >
01245 easy->easy_handle->set.max_send_speed ) ) ||
01246 ( ( easy->easy_handle->set.max_recv_speed > 0 ) &&
01247 ( easy->easy_handle->progress.dlspeed >
01248 easy->easy_handle->set.max_recv_speed ) )
01249 ) {
01250
01251
01252
01253 multistate(easy, CURLM_STATE_TOOFAST );
01254 break;
01255 }
01256
01257
01258 easy->result = Curl_readwrite(easy->easy_conn, &done);
01259
01260 k = &easy->easy_handle->reqdata.keep;
01261
01262 if (!(k->keepon & KEEP_READ)) {
01263
01264 easy->easy_conn->readchannel_inuse = FALSE;
01265 }
01266
01267 if (!(k->keepon & KEEP_WRITE)) {
01268
01269 easy->easy_conn->writechannel_inuse = FALSE;
01270 }
01271
01272 if(easy->result) {
01273
01274
01275
01276 easy->easy_conn->bits.close = TRUE;
01277 Curl_removeHandleFromPipeline(easy->easy_handle,
01278 easy->easy_conn->recv_pipe);
01279
01280 if(CURL_SOCKET_BAD != easy->easy_conn->sock[SECONDARYSOCKET]) {
01281
01282
01283 sclose(easy->easy_conn->sock[SECONDARYSOCKET]);
01284 easy->easy_conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
01285 }
01286 Curl_posttransfer(easy->easy_handle);
01287 Curl_done(&easy->easy_conn, easy->result, FALSE);
01288 }
01289 else if(TRUE == done) {
01290 char *newurl;
01291 bool retry = Curl_retry_request(easy->easy_conn, &newurl);
01292
01293
01294 Curl_posttransfer(easy->easy_handle);
01295
01296
01297 if(easy->easy_handle->reqdata.newurl || retry) {
01298 Curl_removeHandleFromPipeline(easy->easy_handle,
01299 easy->easy_conn->recv_pipe);
01300 if(!retry) {
01301
01302
01303 newurl = easy->easy_handle->reqdata.newurl;
01304 easy->easy_handle->reqdata.newurl = NULL;
01305 }
01306 easy->result = Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
01307 if(easy->result == CURLE_OK)
01308 easy->result = Curl_follow(easy->easy_handle, newurl, retry);
01309 if(CURLE_OK == easy->result) {
01310 multistate(easy, CURLM_STATE_CONNECT);
01311 result = CURLM_CALL_MULTI_PERFORM;
01312 }
01313 else
01314
01315
01316 free(newurl);
01317 }
01318 else {
01319
01320 multistate(easy, CURLM_STATE_DONE);
01321 result = CURLM_CALL_MULTI_PERFORM;
01322 }
01323 }
01324
01325 break;
01326
01327 case CURLM_STATE_DONE:
01328
01329 Curl_removeHandleFromPipeline(easy->easy_handle,
01330 easy->easy_conn->recv_pipe);
01331 easy->easy_handle->state.is_in_pipeline = FALSE;
01332
01333 if (easy->easy_conn->bits.stream_was_rewound) {
01334
01335
01336
01337 result = CURLM_CALL_MULTI_PERFORM;
01338 }
01339
01340 if (!easy->easy_handle->state.cancelled) {
01341
01342 easy->result = Curl_done(&easy->easy_conn, CURLE_OK, FALSE);
01343
01344
01345
01346 multistate(easy, CURLM_STATE_COMPLETED);
01347 }
01348
01349 break;
01350
01351 case CURLM_STATE_COMPLETED:
01352 if (easy->easy_handle->state.cancelled)
01353
01354 multistate(easy, CURLM_STATE_CANCELLED);
01355
01356
01357
01358
01359
01360
01361
01362
01363 easy->easy_conn = NULL;
01364 break;
01365
01366 case CURLM_STATE_CANCELLED:
01367
01368
01369
01370 easy->easy_conn = NULL;
01371 break;
01372
01373 default:
01374 return CURLM_INTERNAL_ERROR;
01375 }
01376
01377 if(CURLM_STATE_COMPLETED != easy->state) {
01378 if(CURLE_OK != easy->result) {
01379
01380
01381
01382
01383
01384
01385
01386
01387 easy->easy_handle->state.is_in_pipeline = FALSE;
01388 easy->easy_handle->state.pipe_broke = FALSE;
01389
01390 if(easy->easy_conn) {
01391
01392 easy->easy_conn->writechannel_inuse = FALSE;
01393 easy->easy_conn->readchannel_inuse = FALSE;
01394 Curl_removeHandleFromPipeline(easy->easy_handle,
01395 easy->easy_conn->send_pipe);
01396 Curl_removeHandleFromPipeline(easy->easy_handle,
01397 easy->easy_conn->recv_pipe);
01398 }
01399
01400 if (disconnect_conn) {
01401 Curl_disconnect(easy->easy_conn);
01402
01403
01404
01405
01406 easy->easy_conn = NULL;
01407 }
01408
01409 multistate(easy, CURLM_STATE_COMPLETED);
01410 }
01411 }
01412
01413 } while (easy->easy_handle->change.url_changed);
01414
01415 if ((CURLM_STATE_COMPLETED == easy->state) && !easy->msg) {
01416 if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
01417
01418 easy->easy_handle->dns.hostcache = NULL;
01419 easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
01420 }
01421
01422
01423 msg = (struct Curl_message *)malloc(sizeof(struct Curl_message));
01424
01425 if(!msg)
01426 return CURLM_OUT_OF_MEMORY;
01427
01428 msg->extmsg.msg = CURLMSG_DONE;
01429 msg->extmsg.easy_handle = easy->easy_handle;
01430 msg->extmsg.data.result = easy->result;
01431 msg->next = NULL;
01432
01433 easy->msg = msg;
01434 easy->msg_num = 1;
01435
01436 multi->num_msgs++;
01437 }
01438
01439 if(CURLM_CALL_MULTI_PERFORM == result)
01440
01441
01442
01443
01444
01445
01446
01447
01448 Curl_expire(easy->easy_handle, 10);
01449
01450 return result;
01451 }
01452
01453
01454 CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
01455 {
01456 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
01457 struct Curl_one_easy *easy;
01458 CURLMcode returncode=CURLM_OK;
01459 struct Curl_tree *t;
01460
01461 if(!GOOD_MULTI_HANDLE(multi))
01462 return CURLM_BAD_HANDLE;
01463
01464 easy=multi->easy.next;
01465 while(easy != &multi->easy) {
01466 CURLMcode result;
01467
01468 if (easy->easy_handle->state.cancelled &&
01469 easy->state == CURLM_STATE_CANCELLED) {
01470
01471 Curl_multi_rmeasy(multi_handle, easy->easy_handle);
01472 easy->easy_handle = NULL;
01473 easy = easy->next;
01474 continue;
01475 }
01476
01477 result = multi_runsingle(multi, easy);
01478 if(result)
01479 returncode = result;
01480
01481 easy = easy->next;
01482 }
01483
01484
01485
01486
01487
01488
01489 do {
01490 struct timeval now = Curl_tvnow();
01491 int key = now.tv_sec;
01492
01493 multi->timetree = Curl_splaygetbest(key, multi->timetree, &t);
01494 if (t) {
01495 struct SessionHandle *d = t->payload;
01496 struct timeval* tv = &d->state.expiretime;
01497
01498
01499
01500 tv->tv_sec = 0;
01501 tv->tv_usec = 0;
01502 }
01503
01504 } while(t);
01505
01506 *running_handles = multi->num_alive;
01507
01508 if ( CURLM_OK >= returncode )
01509 update_timer(multi);
01510 return returncode;
01511 }
01512
01513
01514
01515 void Curl_multi_rmeasy(void *multi_handle, CURL *easy_handle)
01516 {
01517 curl_multi_remove_handle(multi_handle, easy_handle);
01518 }
01519
01520
01521 CURLMcode curl_multi_cleanup(CURLM *multi_handle)
01522 {
01523 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
01524 struct Curl_one_easy *easy;
01525 struct Curl_one_easy *nexteasy;
01526 int i;
01527 struct closure *cl;
01528 struct closure *n;
01529
01530 if(GOOD_MULTI_HANDLE(multi)) {
01531 multi->type = 0;
01532 Curl_hash_destroy(multi->hostcache);
01533 Curl_hash_destroy(multi->sockhash);
01534
01535
01536 for(i=0; i< multi->connc->num; i++) {
01537 if(multi->connc->connects[i] &&
01538 multi->connc->connects[i]->protocol & PROT_CLOSEACTION) {
01539 Curl_disconnect(multi->connc->connects[i]);
01540 multi->connc->connects[i] = NULL;
01541 }
01542 }
01543
01544
01545 cl = multi->closure;
01546 while(cl) {
01547 cl->easy_handle->state.shared_conn = NULL;
01548 if(cl->easy_handle->state.closed)
01549
01550
01551 Curl_close(cl->easy_handle);
01552 n = cl->next;
01553 free(cl);
01554 cl= n;
01555 }
01556
01557 Curl_rm_connc(multi->connc);
01558
01559
01560 easy = multi->easy.next;
01561 while(easy != &multi->easy) {
01562 nexteasy=easy->next;
01563 if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
01564
01565 easy->easy_handle->dns.hostcache = NULL;
01566 easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
01567 }
01568
01569
01570 easy->easy_handle->state.connc = NULL;
01571
01572 Curl_easy_addmulti(easy->easy_handle, NULL);
01573
01574 if (easy->msg)
01575 free(easy->msg);
01576 free(easy);
01577 easy = nexteasy;
01578 }
01579
01580 free(multi);
01581
01582 return CURLM_OK;
01583 }
01584 else
01585 return CURLM_BAD_HANDLE;
01586 }
01587
01588 CURLMsg *curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
01589 {
01590 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
01591
01592 *msgs_in_queue = 0;
01593
01594 if(GOOD_MULTI_HANDLE(multi)) {
01595 struct Curl_one_easy *easy;
01596
01597 if(!multi->num_msgs)
01598 return NULL;
01599
01600 easy=multi->easy.next;
01601 while(easy != &multi->easy) {
01602 if(easy->msg_num) {
01603 easy->msg_num--;
01604 break;
01605 }
01606 easy = easy->next;
01607 }
01608 if(!easy)
01609 return NULL;
01610
01611 multi->num_msgs--;
01612 *msgs_in_queue = multi->num_msgs;
01613
01614 return &easy->msg->extmsg;
01615 }
01616 else
01617 return NULL;
01618 }
01619
01620
01621
01622
01623
01624
01625 static void singlesocket(struct Curl_multi *multi,
01626 struct Curl_one_easy *easy)
01627 {
01628 curl_socket_t socks[MAX_SOCKSPEREASYHANDLE];
01629 int i;
01630 struct Curl_sh_entry *entry;
01631 curl_socket_t s;
01632 int num;
01633 unsigned int curraction;
01634
01635 memset(&socks, 0, sizeof(socks));
01636 for(i=0; i< MAX_SOCKSPEREASYHANDLE; i++)
01637 socks[i] = CURL_SOCKET_BAD;
01638
01639
01640
01641 curraction = multi_getsock(easy, socks, MAX_SOCKSPEREASYHANDLE);
01642
01643
01644
01645
01646
01647
01648 for(i=0; (i< MAX_SOCKSPEREASYHANDLE) &&
01649 (curraction & (GETSOCK_READSOCK(i) | GETSOCK_WRITESOCK(i)));
01650 i++) {
01651 int action = CURL_POLL_NONE;
01652
01653 s = socks[i];
01654
01655
01656 entry = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
01657
01658 if(curraction & GETSOCK_READSOCK(i))
01659 action |= CURL_POLL_IN;
01660 if(curraction & GETSOCK_WRITESOCK(i))
01661 action |= CURL_POLL_OUT;
01662
01663 if(entry) {
01664
01665 if(entry->action == action)
01666
01667 continue;
01668 }
01669 else {
01670
01671 entry = sh_addentry(multi->sockhash, s, easy->easy_handle);
01672 if(!entry)
01673
01674 return;
01675 }
01676
01677 multi->socket_cb(easy->easy_handle,
01678 s,
01679 action,
01680 multi->socket_userp,
01681 entry ? entry->socketp : NULL);
01682
01683 entry->action = action;
01684 }
01685
01686 num = i;
01687
01688
01689
01690 for(i=0; i< easy->numsocks; i++) {
01691 int j;
01692 s = easy->sockets[i];
01693 for(j=0; j<num; j++) {
01694 if(s == socks[j]) {
01695
01696 s = CURL_SOCKET_BAD;
01697 break;
01698 }
01699 }
01700 if(s != CURL_SOCKET_BAD) {
01701
01702
01703 entry = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
01704 if(entry) {
01705
01706
01707
01708 multi->socket_cb(easy->easy_handle,
01709 s,
01710 CURL_POLL_REMOVE,
01711 multi->socket_userp,
01712 entry ? entry->socketp : NULL);
01713
01714 sh_delentry(multi->sockhash, s);
01715 }
01716 }
01717 }
01718
01719 memcpy(easy->sockets, socks, num*sizeof(curl_socket_t));
01720 easy->numsocks = num;
01721 }
01722
01723 static CURLMcode multi_socket(struct Curl_multi *multi,
01724 bool checkall,
01725 curl_socket_t s,
01726 int ev_bitmask,
01727 int *running_handles)
01728 {
01729 CURLMcode result = CURLM_OK;
01730 struct SessionHandle *data = NULL;
01731 struct Curl_tree *t;
01732
01733 if(checkall) {
01734 struct Curl_one_easy *easyp;
01735
01736 result = curl_multi_perform(multi, running_handles);
01737
01738
01739
01740 easyp=multi->easy.next;
01741 while(easyp != &multi->easy) {
01742 singlesocket(multi, easyp);
01743 easyp = easyp->next;
01744 }
01745
01746
01747 return result;
01748 }
01749 else if (s != CURL_SOCKET_TIMEOUT) {
01750
01751 struct Curl_sh_entry *entry =
01752 Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
01753
01754 if(!entry)
01755
01756 return CURLM_BAD_SOCKET;
01757
01758 data = entry->easy;
01759
01760 if(data->magic != CURLEASY_MAGIC_NUMBER)
01761
01762 return CURLM_INTERNAL_ERROR;
01763
01764 if (data->set.one_easy->easy_conn)
01765 data->set.one_easy->easy_conn->cselect_bits = ev_bitmask;
01766
01767 result = multi_runsingle(multi, data->set.one_easy);
01768
01769 if (data->set.one_easy->easy_conn)
01770 data->set.one_easy->easy_conn->cselect_bits = 0;
01771
01772 if(CURLM_OK >= result)
01773
01774
01775 singlesocket(multi, data->set.one_easy);
01776
01777
01778
01779
01780
01781 data = NULL;
01782
01783 }
01784
01785
01786
01787
01788
01789
01790 do {
01791 int key;
01792 struct timeval now;
01793
01794
01795 if(data) {
01796 result = multi_runsingle(multi, data->set.one_easy);
01797
01798 if(CURLM_OK >= result)
01799
01800
01801 singlesocket(multi, data->set.one_easy);
01802 }
01803
01804
01805
01806
01807 now = Curl_tvnow();
01808 key = now.tv_sec;
01809
01810 multi->timetree = Curl_splaygetbest(key, multi->timetree, &t);
01811 if(t) {
01812
01813
01814 data = t->payload;
01815
01816
01817 data->state.expiretime.tv_sec = 0;
01818 data->state.expiretime.tv_usec = 0;
01819 }
01820
01821 } while(t);
01822
01823 *running_handles = multi->num_alive;
01824 return result;
01825 }
01826
01827 CURLMcode curl_multi_setopt(CURLM *multi_handle,
01828 CURLMoption option, ...)
01829 {
01830 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
01831 CURLMcode res = CURLM_OK;
01832 va_list param;
01833
01834 if(!GOOD_MULTI_HANDLE(multi))
01835 return CURLM_BAD_HANDLE;
01836
01837 va_start(param, option);
01838
01839 switch(option) {
01840 case CURLMOPT_SOCKETFUNCTION:
01841 multi->socket_cb = va_arg(param, curl_socket_callback);
01842 break;
01843 case CURLMOPT_SOCKETDATA:
01844 multi->socket_userp = va_arg(param, void *);
01845 break;
01846 case CURLMOPT_PIPELINING:
01847 multi->pipelining_enabled = (bool)(0 != va_arg(param, long));
01848 break;
01849 case CURLMOPT_TIMERFUNCTION:
01850 multi->timer_cb = va_arg(param, curl_multi_timer_callback);
01851 break;
01852 case CURLMOPT_TIMERDATA:
01853 multi->timer_userp = va_arg(param, void *);
01854 break;
01855 case CURLMOPT_MAXCONNECTS:
01856 multi->maxconnects = va_arg(param, long);
01857 break;
01858 default:
01859 res = CURLM_UNKNOWN_OPTION;
01860 break;
01861 }
01862 va_end(param);
01863 return res;
01864 }
01865
01866
01867 #undef curl_multi_socket
01868
01869 CURLMcode curl_multi_socket(CURLM *multi_handle, curl_socket_t s,
01870 int *running_handles)
01871 {
01872 CURLMcode result = multi_socket((struct Curl_multi *)multi_handle, FALSE, s,
01873 0, running_handles);
01874 if (CURLM_OK >= result)
01875 update_timer((struct Curl_multi *)multi_handle);
01876 return result;
01877 }
01878
01879 CURLMcode curl_multi_socket_action(CURLM *multi_handle, curl_socket_t s,
01880 int ev_bitmask, int *running_handles)
01881 {
01882 CURLMcode result = multi_socket((struct Curl_multi *)multi_handle, FALSE, s,
01883 ev_bitmask, running_handles);
01884 if (CURLM_OK >= result)
01885 update_timer((struct Curl_multi *)multi_handle);
01886 return result;
01887 }
01888
01889 CURLMcode curl_multi_socket_all(CURLM *multi_handle, int *running_handles)
01890
01891 {
01892 CURLMcode result = multi_socket((struct Curl_multi *)multi_handle,
01893 TRUE, CURL_SOCKET_BAD, 0, running_handles);
01894 if (CURLM_OK >= result)
01895 update_timer((struct Curl_multi *)multi_handle);
01896 return result;
01897 }
01898
01899 static CURLMcode multi_timeout(struct Curl_multi *multi,
01900 long *timeout_ms)
01901 {
01902 if(multi->timetree) {
01903
01904 struct timeval now = Curl_tvnow();
01905
01906
01907 multi->timetree = Curl_splay(0, multi->timetree);
01908
01909
01910 *timeout_ms = (multi->timetree->key - now.tv_sec) * 1000 -
01911 now.tv_usec/1000;
01912 if(*timeout_ms < 0)
01913
01914 *timeout_ms = 0;
01915 }
01916 else
01917 *timeout_ms = -1;
01918
01919 return CURLM_OK;
01920 }
01921
01922 CURLMcode curl_multi_timeout(CURLM *multi_handle,
01923 long *timeout_ms)
01924 {
01925 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
01926
01927
01928 if(!GOOD_MULTI_HANDLE(multi))
01929 return CURLM_BAD_HANDLE;
01930
01931 return multi_timeout(multi, timeout_ms);
01932 }
01933
01934
01935
01936
01937
01938 static int update_timer(struct Curl_multi *multi)
01939 {
01940 long timeout_ms;
01941 if (!multi->timer_cb)
01942 return 0;
01943 if ( multi_timeout(multi, &timeout_ms) != CURLM_OK )
01944 return -1;
01945 if ( timeout_ms < 0 )
01946 return 0;
01947
01948
01949
01950
01951
01952 if(multi->timetree->key == multi->timer_lastcall)
01953 return 0;
01954
01955 multi->timer_lastcall = multi->timetree->key;
01956
01957 return multi->timer_cb((CURLM*)multi, timeout_ms, multi->timer_userp);
01958 }
01959
01960
01961
01962 void Curl_expire(struct SessionHandle *data, long milli)
01963 {
01964 struct Curl_multi *multi = data->multi;
01965 struct timeval *nowp = &data->state.expiretime;
01966 int rc;
01967
01968
01969
01970 if(!multi)
01971 return;
01972
01973 if(!milli) {
01974
01975 if(nowp->tv_sec) {
01976
01977
01978 rc = Curl_splayremovebyaddr(multi->timetree,
01979 &data->state.timenode,
01980 &multi->timetree);
01981 if(rc)
01982 infof(data, "Internal error clearing splay node = %d\n", rc);
01983 infof(data, "Expire cleared\n");
01984 nowp->tv_sec = 0;
01985 nowp->tv_usec = 0;
01986 }
01987 }
01988 else {
01989 struct timeval set;
01990 int rest;
01991
01992 set = Curl_tvnow();
01993 set.tv_sec += milli/1000;
01994 set.tv_usec += (milli%1000)*1000;
01995
01996 rest = (int)(set.tv_usec - 1000000);
01997 if(rest > 0) {
01998
01999 set.tv_sec++;
02000 set.tv_usec -= 1000000;
02001 }
02002
02003 if(nowp->tv_sec) {
02004
02005
02006
02007 long diff = curlx_tvdiff(set, *nowp);
02008 if(diff > 0)
02009
02010 return;
02011
02012
02013
02014 rc = Curl_splayremovebyaddr(multi->timetree,
02015 &data->state.timenode,
02016 &multi->timetree);
02017 if(rc)
02018 infof(data, "Internal error removing splay node = %d\n", rc);
02019 }
02020
02021 *nowp = set;
02022 #if 0
02023 infof(data, "Expire at %ld / %ld (%ldms)\n",
02024 (long)nowp->tv_sec, (long)nowp->tv_usec, milli);
02025 #endif
02026 data->state.timenode.payload = data;
02027 multi->timetree = Curl_splayinsert((int)nowp->tv_sec,
02028 multi->timetree,
02029 &data->state.timenode);
02030 }
02031 #if 0
02032 Curl_splayprint(multi->timetree, 0, TRUE);
02033 #endif
02034 }
02035
02036 CURLMcode curl_multi_assign(CURLM *multi_handle,
02037 curl_socket_t s, void *hashp)
02038 {
02039 struct Curl_sh_entry *there = NULL;
02040 struct Curl_multi *multi = (struct Curl_multi *)multi_handle;
02041
02042 if(s != CURL_SOCKET_BAD)
02043 there = Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(curl_socket_t));
02044
02045 if(!there)
02046 return CURLM_BAD_SOCKET;
02047
02048 there->socketp = hashp;
02049
02050 return CURLM_OK;
02051 }
02052
02053 static bool multi_conn_using(struct Curl_multi *multi,
02054 struct SessionHandle *data)
02055 {
02056
02057 int i;
02058
02059 for(i=0; i< multi->connc->num; i++) {
02060 if(multi->connc->connects[i] &&
02061 (multi->connc->connects[i]->data == data) &&
02062 multi->connc->connects[i]->protocol & PROT_CLOSEACTION)
02063 return TRUE;
02064 }
02065
02066 return FALSE;
02067 }
02068
02069
02070
02071
02072
02073 static void add_closure(struct Curl_multi *multi,
02074 struct SessionHandle *data)
02075 {
02076 int i;
02077 struct closure *cl = (struct closure *)calloc(sizeof(struct closure), 1);
02078 struct closure *p=NULL;
02079 struct closure *n;
02080 if(cl) {
02081 cl->easy_handle = data;
02082 cl->next = multi->closure;
02083 multi->closure = cl;
02084 }
02085
02086 p = multi->closure;
02087 cl = p->next;
02088
02089
02090
02091
02092
02093
02094 while(cl) {
02095 bool inuse = FALSE;
02096 for(i=0; i< multi->connc->num; i++) {
02097 if(multi->connc->connects[i] &&
02098 (multi->connc->connects[i]->data == cl->easy_handle)) {
02099 inuse = TRUE;
02100 break;
02101 }
02102 }
02103
02104 n = cl->next;
02105
02106 if(!inuse) {
02107
02108 infof(data, "Delayed kill of easy handle %p\n", cl->easy_handle);
02109
02110 cl->easy_handle->state.shared_conn= NULL;
02111 Curl_close(cl->easy_handle);
02112 if(p)
02113 p->next = n;
02114 else
02115 multi->closure = n;
02116 free(cl);
02117 }
02118 else
02119 p = cl;
02120
02121 cl = n;
02122 }
02123
02124 }
02125
02126 #ifdef CURLDEBUG
02127 void curl_multi_dump(CURLM *multi_handle)
02128 {
02129 struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
02130 struct Curl_one_easy *easy;
02131 int i;
02132 fprintf(stderr, "* Multi status: %d handles, %d alive\n",
02133 multi->num_easy, multi->num_alive);
02134 for(easy=multi->easy.next; easy != &multi->easy; easy = easy->next) {
02135 if(easy->state != CURLM_STATE_COMPLETED) {
02136
02137 fprintf(stderr, "handle %p, state %s, %d sockets\n",
02138 (void *)easy->easy_handle,
02139 statename[easy->state], easy->numsocks);
02140 for(i=0; i < easy->numsocks; i++) {
02141 curl_socket_t s = easy->sockets[i];
02142 struct Curl_sh_entry *entry =
02143 Curl_hash_pick(multi->sockhash, (char *)&s, sizeof(s));
02144
02145 fprintf(stderr, "%d ", (int)s);
02146 if(!entry) {
02147 fprintf(stderr, "INTERNAL CONFUSION\n");
02148 continue;
02149 }
02150 fprintf(stderr, "[%s %s] ",
02151 entry->action&CURL_POLL_IN?"RECVING":"",
02152 entry->action&CURL_POLL_OUT?"SENDING":"");
02153 }
02154 if(easy->numsocks)
02155 fprintf(stderr, "\n");
02156 }
02157 }
02158 }
02159 #endif