61
57
#define CURL_SOCKET_HASH_TABLE_SIZE 911
65
/* the 'CURLMsg' is the part that is visible to the external user */
66
struct CURLMsg extmsg;
69
/* NOTE: if you add a state here, add the name to the statename[] array as
73
CURLM_STATE_INIT, /* 0 - start in this state */
74
CURLM_STATE_CONNECT, /* 1 - resolve/connect has been sent off */
75
CURLM_STATE_WAITRESOLVE, /* 2 - awaiting the resolve to finalize */
76
CURLM_STATE_WAITCONNECT, /* 3 - awaiting the connect to finalize */
77
CURLM_STATE_WAITPROXYCONNECT, /* 4 - awaiting proxy CONNECT to finalize */
78
CURLM_STATE_PROTOCONNECT, /* 5 - completing the protocol-specific connect
80
CURLM_STATE_WAITDO, /* 6 - wait for our turn to send the request */
81
CURLM_STATE_DO, /* 7 - start send off the request (part 1) */
82
CURLM_STATE_DOING, /* 8 - sending off the request (part 1) */
83
CURLM_STATE_DO_MORE, /* 9 - send off the request (part 2) */
84
CURLM_STATE_DO_DONE, /* 10 - done sending off request */
85
CURLM_STATE_WAITPERFORM, /* 11 - wait for our turn to read the response */
86
CURLM_STATE_PERFORM, /* 12 - transfer data */
87
CURLM_STATE_TOOFAST, /* 13 - wait because limit-rate exceeded */
88
CURLM_STATE_DONE, /* 14 - post data transfer operation */
89
CURLM_STATE_COMPLETED, /* 15 - operation complete */
90
CURLM_STATE_MSGSENT, /* 16 - the operation complete message is sent */
91
CURLM_STATE_LAST /* 17 - not a true state, never use this */
94
/* we support N sockets per easy handle. Set the corresponding bit to what
95
action we should wait for */
96
#define MAX_SOCKSPEREASYHANDLE 5
97
#define GETSOCK_READABLE (0x00ff)
98
#define GETSOCK_WRITABLE (0xff00)
101
struct closure *next; /* a simple one-way list of structs */
102
struct SessionHandle *easy_handle;
105
struct Curl_one_easy {
106
/* first, two fields for the linked list of these */
107
struct Curl_one_easy *next;
108
struct Curl_one_easy *prev;
110
struct SessionHandle *easy_handle; /* the easy handle for this unit */
111
struct connectdata *easy_conn; /* the "unit's" connection */
113
CURLMstate state; /* the handle's state */
114
CURLcode result; /* previous result */
116
struct Curl_message msg; /* A single posted message. */
118
/* Array with the plain socket numbers this handle takes care of, in no
119
particular order. Note that all sockets are added to the sockhash, where
120
the state etc are also kept. This array is mostly used to detect when a
121
socket is to be removed from the hash. See singlesocket(). */
122
curl_socket_t sockets[MAX_SOCKSPEREASYHANDLE];
126
61
#define CURL_MULTI_HANDLE 0x000bab1e
130
65
#define GOOD_EASY_HANDLE(x) \
131
66
((x) && (((struct SessionHandle *)(x))->magic == CURLEASY_MAGIC_NUMBER))
133
/* This is the struct known as CURLM on the outside */
135
/* First a simple identifier to easier detect if a user mix up
136
this multi handle with an easy handle. Set this to CURL_MULTI_HANDLE. */
139
/* We have a doubly-linked circular list with easy handles */
140
struct Curl_one_easy easy;
142
int num_easy; /* amount of entries in the linked list above. */
143
int num_alive; /* amount of easy handles that are added but have not yet
144
reached COMPLETE state */
146
struct curl_llist *msglist; /* a list of messages from completed transfers */
148
/* callback function and user data pointer for the *socket() API */
149
curl_socket_callback socket_cb;
153
struct curl_hash *hostcache;
155
/* timetree points to the splay-tree of time nodes to figure out expire
156
times of all currently set timers */
157
struct Curl_tree *timetree;
159
/* 'sockhash' is the lookup hash for socket descriptor => easy handles (note
160
the pluralis form, there can be more than one easy handle waiting on the
161
same actual socket) */
162
struct curl_hash *sockhash;
164
/* Whether pipelining is enabled for this multi handle */
165
bool pipelining_enabled;
167
/* shared connection cache */
168
struct conncache *connc;
169
long maxconnects; /* if >0, a fixed limit of the maximum number of entries
170
we're allowed to grow the connection cache to */
172
/* list of easy handles kept around for doing nice connection closures */
173
struct closure *closure;
175
/* timer callback and user data pointer for the *socket() API */
176
curl_multi_timer_callback timer_cb;
178
struct timeval timer_lastcall; /* the fixed time for the timeout for the
182
static void multi_connc_remove_handle(struct Curl_multi *multi,
183
struct SessionHandle *data);
184
68
static void singlesocket(struct Curl_multi *multi,
185
69
struct Curl_one_easy *easy);
186
static CURLMcode add_closure(struct Curl_multi *multi,
187
struct SessionHandle *data);
188
70
static int update_timer(struct Curl_multi *multi);
190
72
static CURLcode addHandleToSendOrPendPipeline(struct SessionHandle *handle,
242
128
if(easy->easy_conn) {
243
129
if(easy->state > CURLM_STATE_CONNECT &&
244
130
easy->state < CURLM_STATE_COMPLETED)
245
connectindex = easy->easy_conn->connectindex;
131
connection_id = easy->easy_conn->connection_id;
247
133
infof(easy->easy_handle,
248
"STATE: %s => %s handle %p; (connection #%ld) \n",
134
"STATE: %s => %s handle %p; line %d (connection #%ld) \n",
249
135
statename[oldstate], statename[easy->state],
250
(char *)easy, connectindex);
136
(char *)easy, lineno, connection_id);
253
139
if(state == CURLM_STATE_COMPLETED)
462
353
/* possibly we should create a new unique error code for this condition */
463
354
return CURLM_BAD_EASY_HANDLE;
465
/* We want the connection cache to have plenty of room. Before we supported
466
the shared cache every single easy handle had 5 entries in their cache
468
if(((multi->num_easy + 1) * 4) > multi->connc->num) {
469
long newmax = (multi->num_easy + 1) * 4;
471
if(multi->maxconnects && (newmax > multi->maxconnects))
472
/* don't grow beyond the allowed size */
473
newmax = multi->maxconnects;
475
if(newmax > multi->connc->num) {
476
/* we only do this is we can in fact grow the cache */
477
CURLcode res = Curl_ch_connc(data, multi->connc, newmax);
479
return CURLM_OUT_OF_MEMORY;
483
356
/* Allocate and initialize timeout list for easy handle */
484
357
timeoutlist = Curl_llist_alloc(multi_freetimeout);
493
366
return CURLM_OUT_OF_MEMORY;
369
/* In case multi handle has no hostcache yet, allocate one */
370
if(!multi->hostcache) {
371
hostcache = Curl_mk_dnscache();
374
Curl_llist_destroy(timeoutlist, NULL);
375
return CURLM_OUT_OF_MEMORY;
379
/* In case multi handle has no closure_handle yet, allocate
380
a new easy handle to use when closing cached connections */
381
if(!multi->closure_handle) {
382
new_closure = (struct SessionHandle *)curl_easy_init();
384
Curl_hash_destroy(hostcache);
386
Curl_llist_destroy(timeoutlist, NULL);
387
return CURLM_OUT_OF_MEMORY;
497
392
** No failure allowed in this function beyond this point. And
498
393
** no modification of easy nor multi handle allowed before this
500
395
** won't be undone in this function no matter what.
398
/* In case a new closure handle has been initialized above, it
399
is associated now with the multi handle which lacked one. */
401
multi->closure_handle = new_closure;
402
Curl_easy_addmulti(multi->closure_handle, multi_handle);
403
multi->closure_handle->state.conn_cache = multi->conn_cache;
406
/* In case hostcache has been allocated above,
407
it is associated now with the multi handle. */
409
multi->hostcache = hostcache;
503
411
/* Make easy handle use timeout list initialized above */
504
412
data->state.timeoutlist = timeoutlist;
505
413
timeoutlist = NULL;
507
/* Remove handle from the list of 'closure handles' in case it is there */
510
struct closure *next = cl->next;
511
if(cl->easy_handle == data) {
512
/* Remove node from list */
517
multi->closure = next;
518
/* removed from closure list now, this might reuse an existing
519
existing connection but we don't know that at this point */
520
data->state.shared_conn = NULL;
521
/* No need to continue, handle can only be present once in the list */
528
415
/* set the easy handle */
529
416
easy->easy_handle = data;
530
417
multistate(easy, CURLM_STATE_INIT);
533
420
easy->easy_handle->multi_pos = easy;
535
422
/* for multi interface connections, we share DNS cache automatically if the
536
easy handle's one is currently private. */
537
if(easy->easy_handle->dns.hostcache &&
538
(easy->easy_handle->dns.hostcachetype == HCACHE_PRIVATE)) {
539
Curl_hash_destroy(easy->easy_handle->dns.hostcache);
540
easy->easy_handle->dns.hostcache = NULL;
541
easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
423
easy handle's one is currently not set. */
544
424
if(!easy->easy_handle->dns.hostcache ||
545
425
(easy->easy_handle->dns.hostcachetype == HCACHE_NONE)) {
546
426
easy->easy_handle->dns.hostcache = multi->hostcache;
547
427
easy->easy_handle->dns.hostcachetype = HCACHE_MULTI;
550
/* On a multi stack the connection cache, owned by the multi handle,
551
is shared between all easy handles within the multi handle. */
552
if(easy->easy_handle->state.connc &&
553
(easy->easy_handle->state.connc->type == CONNCACHE_PRIVATE)) {
554
/* kill old private connection cache */
555
Curl_rm_connc(easy->easy_handle->state.connc);
556
easy->easy_handle->state.connc = NULL;
558
/* Point now to this multi's connection cache */
559
easy->easy_handle->state.connc = multi->connc;
560
easy->easy_handle->state.connc->type = CONNCACHE_MULTI;
430
/* Point to the multi's connection cache */
431
easy->easy_handle->state.conn_cache = multi->conn_cache;
562
433
/* This adds the new entry at the 'end' of the doubly-linked circular
563
434
list of Curl_one_easy structs to try and maintain a FIFO queue so
701
572
Note that this ignores the return code simply because there's
702
573
nothing really useful to do with it anyway! */
703
574
(void)Curl_done(&easy->easy_conn, easy->result, premature);
706
/* the connection is still alive, set back the association to enable
707
the check below to trigger TRUE */
708
easy->easy_conn->data = easy->easy_handle;
711
577
/* Clear connection pipelines, if Curl_done above was not called */
712
578
Curl_getoff_all_pipelines(easy->easy_handle, easy->easy_conn);
715
/* figure out if the easy handle is used by one or more connections in the
717
multi_connc_remove_handle(multi, easy->easy_handle);
719
if(easy->easy_handle->state.connc->type == CONNCACHE_MULTI) {
720
/* if this was using the shared connection cache we clear the pointer
721
to that since we're not part of that handle anymore */
722
easy->easy_handle->state.connc = NULL;
724
/* Since we return the connection back to the communal connection pool
725
we mark the last connection as inaccessible */
726
easy->easy_handle->state.lastconnect = -1;
728
/* Modify the connectindex since this handle can't point to the
729
connection cache anymore.
731
TODO: consider if this is really what we want. The connection cache
732
is within the multi handle and that owns the connections so we should
733
not need to touch connections like this when we just remove an easy
736
if(easy->easy_conn && easy_owns_conn &&
737
(easy->easy_conn->send_pipe->size +
738
easy->easy_conn->recv_pipe->size == 0))
739
easy->easy_conn->connectindex = -1;
581
/* as this was using a shared connection cache we clear the pointer
582
to that since we're not part of that multi handle anymore */
583
easy->easy_handle->state.conn_cache = NULL;
742
585
/* change state without using multistate(), only to make singlesocket() do
1017
870
/* Add external file descriptions from poll-like struct curl_waitfd */
1018
871
for(i = 0; i < extra_nfds; i++) {
1019
872
ufds[nfds].fd = extra_fds[i].fd;
1020
ufds[nfds].events = (short) (
1021
((extra_fds[i].events & CURL_WAIT_POLLIN) ? POLLIN : 0) |
1022
((extra_fds[i].events & CURL_WAIT_POLLPRI) ? POLLPRI : 0) |
1023
((extra_fds[i].events & CURL_WAIT_POLLOUT) ? POLLOUT : 0) );
873
ufds[nfds].events = 0;
874
if(extra_fds[i].events & CURL_WAIT_POLLIN)
875
ufds[nfds].events |= POLLIN;
876
if(extra_fds[i].events & CURL_WAIT_POLLPRI)
877
ufds[nfds].events |= POLLPRI;
878
if(extra_fds[i].events & CURL_WAIT_POLLOUT)
879
ufds[nfds].events |= POLLOUT;
1321
1175
case CURLM_STATE_WAITDO:
1322
1176
/* Wait for our turn to DO when we're pipelining requests */
1323
1177
#ifdef DEBUGBUILD
1324
infof(data, "Conn %ld send pipe %zu inuse %d athead %d\n",
1325
easy->easy_conn->connectindex,
1178
infof(data, "WAITDO: Conn %ld send pipe %zu inuse %d athead %d\n",
1179
easy->easy_conn->connection_id,
1326
1180
easy->easy_conn->send_pipe->size,
1327
1181
easy->easy_conn->writechannel_inuse?1:0,
1328
1182
isHandleAtHead(data,
1583
1441
easy->easy_conn->writechannel_inuse = FALSE;
1444
if(done || (easy->result == CURLE_RECV_ERROR)) {
1445
/* If CURLE_RECV_ERROR happens early enough, we assume it was a race
1446
* condition and the server closed the re-used connection exactly when
1447
* we wanted to use it, so figure out if that is indeed the case.
1449
CURLcode ret = Curl_retry_request(easy->easy_conn, &newurl);
1451
retry = (newurl)?TRUE:FALSE;
1454
/* if we are to retry, set the result to OK and consider the
1456
easy->result = CURLE_OK;
1586
1461
if(easy->result) {
1587
/* The transfer phase returned error, we mark the connection to get
1463
* The transfer phase returned error, we mark the connection to get
1588
1464
* closed to prevent being re-used. This is because we can't possibly
1589
1465
* know if the connection is in a good shape or not now. Unless it is
1590
1466
* a protocol which uses two "channels" like FTP, as then the error
1591
1467
* happened in the data connection.
1593
1470
if(!(easy->easy_conn->handler->flags & PROTOPT_DUAL))
1594
1471
easy->easy_conn->bits.close = TRUE;
1877
1746
return returncode;
1749
static void close_all_connections(struct Curl_multi *multi)
1751
struct connectdata *conn;
1753
conn = Curl_conncache_find_first_connection(multi->conn_cache);
1755
conn->data = multi->closure_handle;
1757
/* This will remove the connection from the cache */
1758
(void)Curl_disconnect(conn, FALSE);
1760
conn = Curl_conncache_find_first_connection(multi->conn_cache);
1880
1764
CURLMcode curl_multi_cleanup(CURLM *multi_handle)
1882
1766
struct Curl_multi *multi=(struct Curl_multi *)multi_handle;
1883
1767
struct Curl_one_easy *easy;
1884
1768
struct Curl_one_easy *nexteasy;
1889
1770
if(GOOD_MULTI_HANDLE(multi)) {
1890
1771
multi->type = 0; /* not good anymore */
1892
/* go over all connections that have close actions */
1893
for(i=0; i< multi->connc->num; i++) {
1894
if(multi->connc->connects[i] &&
1895
multi->connc->connects[i]->handler->flags & PROTOPT_CLOSEACTION) {
1896
Curl_disconnect(multi->connc->connects[i], FALSE);
1897
multi->connc->connects[i] = NULL;
1900
/* now walk through the list of handles we kept around only to be
1901
able to close connections "properly" */
1902
cl = multi->closure;
1904
cl->easy_handle->state.shared_conn = NULL; /* allow cleanup */
1905
if(cl->easy_handle->state.closed)
1906
/* close handle only if curl_easy_cleanup() already has been called
1907
for this easy handle */
1908
Curl_close(cl->easy_handle);
1914
Curl_hash_destroy(multi->hostcache);
1915
multi->hostcache = NULL;
1773
/* Close all the connections in the connection cache */
1774
close_all_connections(multi);
1776
multi->closure_handle->dns.hostcache = multi->hostcache;
1777
Curl_hostcache_clean(multi->closure_handle);
1779
Curl_close(multi->closure_handle);
1780
multi->closure_handle = NULL;
1917
1782
Curl_hash_destroy(multi->sockhash);
1918
1783
multi->sockhash = NULL;
1920
Curl_rm_connc(multi->connc);
1921
multi->connc = NULL;
1785
Curl_conncache_destroy(multi->conn_cache);
1786
multi->conn_cache = NULL;
1923
1788
/* remove the pending list of messages */
1924
1789
Curl_llist_destroy(multi->msglist, NULL);
1930
1795
nexteasy=easy->next;
1931
1796
if(easy->easy_handle->dns.hostcachetype == HCACHE_MULTI) {
1932
1797
/* clear out the usage of the shared DNS cache */
1798
Curl_hostcache_clean(easy->easy_handle);
1933
1799
easy->easy_handle->dns.hostcache = NULL;
1934
1800
easy->easy_handle->dns.hostcachetype = HCACHE_NONE;
1937
1803
/* Clear the pointer to the connection cache */
1938
easy->easy_handle->state.connc = NULL;
1804
easy->easy_handle->state.conn_cache = NULL;
1940
1806
Curl_easy_addmulti(easy->easy_handle, NULL); /* clear the association */
2788
2657
return CURLM_OK;
2791
static void multi_connc_remove_handle(struct Curl_multi *multi,
2792
struct SessionHandle *data)
2794
/* a connection in the connection cache pointing to the given 'data' ? */
2797
for(i=0; i< multi->connc->num; i++) {
2798
struct connectdata * conn = multi->connc->connects[i];
2800
if(conn && conn->data == data) {
2801
/* If this easy_handle was the last one in charge for one or more
2802
connections in the shared connection cache, we might need to keep
2803
this handle around until either A) the connection is closed and
2804
killed properly, or B) another easy_handle uses the connection.
2806
The reason why we need to have a easy_handle associated with a live
2807
connection is simply that some connections will need a handle to get
2808
closed down properly. Currently, the only connections that need to
2809
keep a easy_handle handle around are using FTP(S). Such connections
2810
have the PROT_CLOSEACTION bit set.
2812
Thus, we need to check for all connections in the shared cache that
2813
points to this handle and are using PROT_CLOSEACTION. If there's any,
2814
we need to add this handle to the list of "easy handles kept around
2815
for nice connection closures".
2818
if(conn->handler->flags & PROTOPT_CLOSEACTION) {
2819
/* this handle is still being used by a shared connection and
2820
thus we leave it around for now */
2821
if(add_closure(multi, data) == CURLM_OK)
2822
data->state.shared_conn = multi;
2824
/* out of memory - so much for graceful shutdown */
2825
Curl_disconnect(conn, /* dead_connection */ FALSE);
2826
multi->connc->connects[i] = NULL;
2827
data->state.shared_conn = NULL;
2831
/* disconect the easy handle from the connection since the connection
2832
will now remain but this easy handle is going */
2833
data->state.shared_conn = NULL;
2840
/* Add the given data pointer to the list of 'closure handles' that are kept
2841
around only to be able to close some connections nicely - just make sure
2842
that this handle isn't already added, like for the cases when an easy
2843
handle is removed, added and removed again... */
2844
static CURLMcode add_closure(struct Curl_multi *multi,
2845
struct SessionHandle *data)
2847
struct closure *cl = multi->closure;
2848
struct closure *p = NULL;
2851
/* Before adding, scan through all the other currently kept handles and see
2852
if there are any connections still referring to them and kill them if
2859
for(i=0; i< multi->connc->num; i++) {
2860
if(multi->connc->connects[i] &&
2861
(multi->connc->connects[i]->data == cl->easy_handle)) {
2870
/* cl->easy_handle is now killable */
2872
/* unmark it as not having a connection around that uses it anymore */
2873
cl->easy_handle->state.shared_conn= NULL;
2875
if(cl->easy_handle->state.closed) {
2876
infof(data, "Delayed kill of easy handle %p\n", cl->easy_handle);
2877
/* close handle only if curl_easy_cleanup() already has been called
2878
for this easy handle */
2879
Curl_close(cl->easy_handle);
2888
if(cl->easy_handle == data)
2898
cl = calloc(1, sizeof(struct closure));
2900
return CURLM_OUT_OF_MEMORY;
2902
cl->easy_handle = data;
2903
cl->next = multi->closure;
2904
multi->closure = cl;
2910
2660
#ifdef DEBUGBUILD
2911
2661
void Curl_multi_dump(const struct Curl_multi *multi_handle)