626
* Initialize the UserDefined fields within a SessionHandle.
627
* This may be safely called on a new or existing SessionHandle.
629
CURLcode Curl_init_userdefined(struct UserDefined *set)
631
CURLcode res = CURLE_OK;
633
set->out = stdout; /* default output to stdout */
634
set->in = stdin; /* default input from stdin */
635
set->err = stderr; /* default stderr to stderr */
637
/* use fwrite as default function to store output */
638
set->fwrite_func = (curl_write_callback)fwrite;
640
/* use fread as default function to read input */
641
set->fread_func = (curl_read_callback)fread;
643
set->seek_func = ZERO_NULL;
644
set->seek_client = ZERO_NULL;
646
/* conversion callbacks for non-ASCII hosts */
647
set->convfromnetwork = ZERO_NULL;
648
set->convtonetwork = ZERO_NULL;
649
set->convfromutf8 = ZERO_NULL;
651
set->infilesize = -1; /* we don't know any size */
652
set->postfieldsize = -1; /* unknown size */
653
set->maxredirs = -1; /* allow any amount by default */
655
set->httpreq = HTTPREQ_GET; /* Default HTTP request */
656
set->ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
657
set->ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
658
set->ftp_filemethod = FTPFILE_MULTICWD;
660
set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
662
/* Set the default size of the SSL session ID cache */
663
set->ssl.numsessions = 5;
665
set->proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
666
set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
667
set->httpauth = CURLAUTH_BASIC; /* defaults to basic */
668
set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
670
/* make libcurl quiet by default: */
671
set->hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
674
* libcurl 7.10 introduced SSL verification *by default*! This needs to be
675
* switched off unless wanted.
677
set->ssl.verifypeer = TRUE;
678
set->ssl.verifyhost = 2;
679
set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
681
set->ssl.sessionid = TRUE; /* session ID caching enabled by default */
683
set->new_file_perms = 0644; /* Default permissions */
684
set->new_directory_perms = 0755; /* Default permissions */
686
/* for the *protocols fields we don't use the CURLPROTO_ALL convenience
687
define since we internally only use the lower 16 bits for the passed
688
in bitmask to not conflict with the private bits */
689
set->allowed_protocols = PROT_EXTMASK;
690
set->redir_protocols =
691
PROT_EXTMASK & ~(CURLPROTO_FILE|CURLPROTO_SCP); /* not FILE or SCP */
693
#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
695
* disallow unprotected protection negotiation NEC reference implementation
696
* seem not to follow rfc1961 section 4.3/4.4
698
set->socks5_gssapi_nec = FALSE;
699
/* set default gssapi service name */
700
res = setstropt(&set->str[STRING_SOCKS5_GSSAPI_SERVICE],
701
(char *) CURL_DEFAULT_SOCKS5_GSSAPI_SERVICE);
706
/* This is our preferred CA cert bundle/path since install time */
707
#if defined(CURL_CA_BUNDLE)
708
res = setstropt(&set->str[STRING_SSL_CAFILE], (char *) CURL_CA_BUNDLE);
709
#elif defined(CURL_CA_PATH)
710
res = setstropt(&set->str[STRING_SSL_CAPATH], (char *) CURL_CA_PATH);
1842
2005
case CURLOPT_SHARE:
1844
struct Curl_share *set;
1845
set = va_arg(param, struct Curl_share *);
1847
/* disconnect from old share, if any */
1849
Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
1851
if(data->dns.hostcachetype == HCACHE_SHARED) {
1852
data->dns.hostcache = NULL;
1853
data->dns.hostcachetype = HCACHE_NONE;
1856
if(data->share->cookies == data->cookies)
1857
data->cookies = NULL;
1859
data->share->dirty--;
1861
Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
1865
/* use new share if it set */
1869
Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
1871
data->share->dirty++;
1873
if(data->share->hostcache) {
1874
/* use shared host cache, first free the private one if any */
1875
if(data->dns.hostcachetype == HCACHE_PRIVATE)
1876
Curl_hash_destroy(data->dns.hostcache);
1878
data->dns.hostcache = data->share->hostcache;
1879
data->dns.hostcachetype = HCACHE_SHARED;
2007
struct Curl_share *set;
2008
set = va_arg(param, struct Curl_share *);
2010
/* disconnect from old share, if any */
2012
Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2014
if(data->dns.hostcachetype == HCACHE_SHARED) {
2015
data->dns.hostcache = NULL;
2016
data->dns.hostcachetype = HCACHE_NONE;
2019
if(data->share->cookies == data->cookies)
2020
data->cookies = NULL;
2022
data->share->dirty--;
2024
Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2028
/* use new share if it set */
2032
Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2034
data->share->dirty++;
2036
if(data->share->hostcache) {
2037
/* use shared host cache, first free the private one if any */
2038
if(data->dns.hostcachetype == HCACHE_PRIVATE)
2039
Curl_hash_destroy(data->dns.hostcache);
2041
data->dns.hostcache = data->share->hostcache;
2042
data->dns.hostcachetype = HCACHE_SHARED;
1881
2044
#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
1882
if(data->share->cookies) {
1883
/* use shared cookie list, first free own one if any */
1885
Curl_cookie_cleanup(data->cookies);
1886
/* enable cookies since we now use a share that uses cookies! */
1887
data->cookies = data->share->cookies;
2045
if(data->share->cookies) {
2046
/* use shared cookie list, first free own one if any */
2048
Curl_cookie_cleanup(data->cookies);
2049
/* enable cookies since we now use a share that uses cookies! */
2050
data->cookies = data->share->cookies;
1889
2052
#endif /* CURL_DISABLE_HTTP */
1890
Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2053
Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
1893
/* check for host cache not needed,
1894
* it will be done by curl_easy_perform */
1898
case CURLOPT_PROXYTYPE:
1900
* Set proxy type. HTTP/SOCKS4/SOCKS4a/SOCKS5/SOCKS5_HOSTNAME
1902
data->set.proxytype = (curl_proxytype)va_arg(param, long);
2056
/* check for host cache not needed,
2057
* it will be done by curl_easy_perform */
1905
2061
case CURLOPT_PRIVATE:
3157
3426
/* The protocol was not found in the table, but we don't have to assign it
3158
3427
to anything since it is already assigned to a dummy-struct in the
3159
CreateConnection() function when the connectdata struct is allocated. */
3428
create_conn() function when the connectdata struct is allocated. */
3160
3429
failf(data, "Protocol %s not supported or disabled in " LIBCURL_NAME,
3161
3430
conn->protostr);
3162
3431
return CURLE_UNSUPPORTED_PROTOCOL;
3434
#ifndef CURL_DISABLE_PROXY
3435
/****************************************************************
3436
* Checks if the host is in the noproxy list. returns true if it matches
3437
* and therefore the proxy should NOT be used.
3438
****************************************************************/
3439
static bool check_noproxy(const char* name, const char* no_proxy)
3441
/* no_proxy=domain1.dom,host.domain2.dom
3442
* (a comma-separated list of hosts which should
3443
* not be proxied, or an asterisk to override
3444
* all proxy variables)
3448
const char* separator = ", ";
3449
size_t no_proxy_len;
3453
if(no_proxy && no_proxy[0]) {
3454
if(Curl_raw_equal("*", no_proxy)) {
3458
/* NO_PROXY was specified and it wasn't just an asterisk */
3460
no_proxy_len = strlen(no_proxy);
3461
endptr = strchr(name, ':');
3463
namelen = endptr - name;
3465
namelen = strlen(name);
3468
for (tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
3469
while (tok_start < no_proxy_len &&
3470
strchr(separator, no_proxy[tok_start]) != NULL) {
3471
/* Look for the beginning of the token. */
3475
if(tok_start == no_proxy_len)
3476
break; /* It was all trailing separator chars, no more tokens. */
3478
for (tok_end = tok_start; tok_end < no_proxy_len &&
3479
strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end) {
3480
/* Look for the end of the token. */
3483
/* To match previous behaviour, where it was necessary to specify
3484
* ".local.com" to prevent matching "notlocal.com", we will leave
3487
if(no_proxy[tok_start] == '.')
3490
if((tok_end - tok_start) <= namelen) {
3491
/* Match the last part of the name to the domain we are checking. */
3492
const char *checkn = name + namelen - (tok_end - tok_start);
3493
if(Curl_raw_nequal(no_proxy + tok_start, checkn, tok_end - tok_start)) {
3494
if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
3495
/* We either have an exact match, or the previous character is a .
3496
* so it is within the same domain, so no proxy for this host.
3501
} /* if((tok_end - tok_start) <= namelen) */
3502
} /* for (tok_start = 0; tok_start < no_proxy_len;
3503
tok_start = tok_end + 1) */
3504
} /* NO_PROXY was specified and it wasn't just an asterisk */
3165
3509
/****************************************************************
3166
3510
* Detect what (if any) proxy to use. Remember that this selects a host
3167
3511
* name and is not limited to HTTP proxies only.
3190
3534
* checked if the lowercase versions don't exist.
3192
3536
char *no_proxy=NULL;
3193
char *no_proxy_tok_buf;
3194
3537
char proxy_env[128];
3196
3539
no_proxy=curl_getenv("no_proxy");
3198
3541
no_proxy=curl_getenv("NO_PROXY");
3200
if(!no_proxy || !strequal("*", no_proxy)) {
3201
/* NO_PROXY wasn't specified or it wasn't just an asterisk */
3204
nope=no_proxy?strtok_r(no_proxy, ", ", &no_proxy_tok_buf):NULL;
3207
char *endptr = strchr(conn->host.name, ':');
3209
namelen=endptr-conn->host.name;
3211
namelen=strlen(conn->host.name);
3213
if(strlen(nope) <= namelen) {
3215
conn->host.name + namelen - strlen(nope);
3216
if(checkprefix(nope, checkn)) {
3217
/* no proxy for this host! */
3221
nope=strtok_r(NULL, ", ", &no_proxy_tok_buf);
3224
/* It was not listed as without proxy */
3225
char *protop = conn->protostr;
3226
char *envp = proxy_env;
3229
/* Now, build <protocol>_proxy and check for such a one to use */
3231
*envp++ = (char)tolower((int)*protop++);
3234
strcpy(envp, "_proxy");
3236
/* read the protocol proxy: */
3543
if(!check_noproxy(conn->host.name, no_proxy)) {
3544
/* It was not listed as without proxy */
3545
char *protop = conn->protostr;
3546
char *envp = proxy_env;
3549
/* Now, build <protocol>_proxy and check for such a one to use */
3551
*envp++ = (char)tolower((int)*protop++);
3554
strcpy(envp, "_proxy");
3556
/* read the protocol proxy: */
3557
prox=curl_getenv(proxy_env);
3560
* We don't try the uppercase version of HTTP_PROXY because of
3563
* When curl is used in a webserver application
3564
* environment (cgi or php), this environment variable can
3565
* be controlled by the web server user by setting the
3566
* http header 'Proxy:' to some value.
3568
* This can cause 'internal' http/ftp requests to be
3569
* arbitrarily redirected by any external attacker.
3571
if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
3572
/* There was no lowercase variable, try the uppercase version: */
3573
Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
3237
3574
prox=curl_getenv(proxy_env);
3240
* We don't try the uppercase version of HTTP_PROXY because of
3243
* When curl is used in a webserver application
3244
* environment (cgi or php), this environment variable can
3245
* be controlled by the web server user by setting the
3246
* http header 'Proxy:' to some value.
3248
* This can cause 'internal' http/ftp requests to be
3249
* arbitrarily redirected by any external attacker.
3251
if(!prox && !strequal("http_proxy", proxy_env)) {
3252
/* There was no lowercase variable, try the uppercase version: */
3253
for(envp = proxy_env; *envp; envp++)
3254
*envp = (char)toupper((int)*envp);
3255
prox=curl_getenv(proxy_env);
3258
if(prox && *prox) { /* don't count "" strings */
3259
proxy = prox; /* use this */
3262
proxy = curl_getenv("all_proxy"); /* default proxy to use */
3264
proxy=curl_getenv("ALL_PROXY");
3267
if(proxy && *proxy) {
3268
long bits = conn->protocol & (PROT_HTTPS|PROT_SSL|PROT_MISSING);
3270
if(conn->proxytype == CURLPROXY_HTTP) {
3271
/* force this connection's protocol to become HTTP */
3272
conn->protocol = PROT_HTTP | bits;
3273
conn->bits.proxy = conn->bits.httpproxy = TRUE;
3276
} /* if(!nope) - it wasn't specified non-proxy */
3277
} /* NO_PROXY wasn't specified or '*' */
3577
if(prox && *prox) { /* don't count "" strings */
3578
proxy = prox; /* use this */
3581
proxy = curl_getenv("all_proxy"); /* default proxy to use */
3583
proxy=curl_getenv("ALL_PROXY");
3585
} /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
3279
3588
free(no_proxy);
3435
3754
return CURLE_OK;
3756
#endif /* CURL_DISABLE_PROXY */
3760
* Parse a user name and password in the URL and strip it out of the host name
3762
* Inputs: data->set.use_netrc (CURLOPT_NETRC)
3765
* Outputs: (almost :- all currently undefined)
3766
* conn->bits.user_passwd - non-zero if non-default passwords exist
3767
* user - non-zero length if defined
3769
* conn->host.name - remove user name and password
3771
static CURLcode parse_url_userpass(struct SessionHandle *data,
3772
struct connectdata *conn,
3773
char *user, char *passwd)
3775
/* At this point, we're hoping all the other special cases have
3776
* been taken care of, so conn->host.name is at most
3777
* [user[:password]]@]hostname
3779
* We need somewhere to put the embedded details, so do that first.
3782
user[0] =0; /* to make everything well-defined */
3785
if(conn->protocol & (PROT_FTP|PROT_HTTP|PROT_SCP|PROT_SFTP)) {
3786
/* This is a FTP, HTTP, SCP or SFTP URL, we will now try to extract the
3787
* possible user+password pair in a string like:
3788
* ftp://user:password@ftp.my.site:8021/README */
3789
char *ptr=strchr(conn->host.name, '@');
3790
char *userpass = conn->host.name;
3792
/* there's a user+password given here, to the left of the @ */
3794
conn->host.name = ++ptr;
3796
/* So the hostname is sane. Only bother interpreting the
3797
* results if we could care. It could still be wasted
3798
* work because it might be overtaken by the programmatically
3799
* set user/passwd, but doing that first adds more cases here :-(
3802
if(data->set.use_netrc != CURL_NETRC_REQUIRED) {
3803
/* We could use the one in the URL */
3805
conn->bits.user_passwd = 1; /* enable user+password */
3807
if(*userpass != ':') {
3808
/* the name is given, get user+password */
3809
sscanf(userpass, "%" MAX_CURL_USER_LENGTH_TXT "[^:@]:"
3810
"%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
3814
/* no name given, get the password only */
3815
sscanf(userpass, ":%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]", passwd);
3818
char *newname=curl_easy_unescape(data, user, 0, NULL);
3820
return CURLE_OUT_OF_MEMORY;
3821
if(strlen(newname) < MAX_CURL_USER_LENGTH)
3822
strcpy(user, newname);
3824
/* if the new name is longer than accepted, then just use
3825
the unconverted name, it'll be wrong but what the heck */
3829
/* we have a password found in the URL, decode it! */
3830
char *newpasswd=curl_easy_unescape(data, passwd, 0, NULL);
3832
return CURLE_OUT_OF_MEMORY;
3833
if(strlen(newpasswd) < MAX_CURL_PASSWORD_LENGTH)
3834
strcpy(passwd, newpasswd);
3844
/*************************************************************
3845
* Figure out the remote port number and fix it in the URL
3847
* No matter if we use a proxy or not, we have to figure out the remote
3848
* port number of various reasons.
3850
* To be able to detect port number flawlessly, we must not confuse them
3851
* IPv6-specified addresses in the [0::1] style. (RFC2732)
3853
* The conn->host.name is currently [user:passwd@]host[:port] where host
3854
* could be a hostname, IPv4 address or IPv6 address.
3856
* The port number embedded in the URL is replaced, if necessary.
3857
*************************************************************/
3858
static CURLcode parse_remote_port(struct SessionHandle *data,
3859
struct connectdata *conn)
3864
/* Note that at this point, the IPv6 address cannot contain any scope
3865
suffix as that has already been removed in the ParseURLAndFillConnection()
3867
if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
3869
(']' == endbracket)) {
3870
/* this is a RFC2732-style specified IP-address */
3871
conn->bits.ipv6_ip = TRUE;
3873
conn->host.name++; /* skip over the starting bracket */
3874
portptr = strchr(conn->host.name, ']');
3875
*portptr++ = 0; /* zero terminate, killing the bracket */
3877
portptr = NULL; /* no port number available */
3880
portptr = strrchr(conn->host.name, ':');
3882
if(data->set.use_port && data->state.allow_port) {
3883
/* if set, we use this and ignore the port possibly given in the URL */
3884
conn->remote_port = (unsigned short)data->set.use_port;
3886
*portptr = '\0'; /* cut off the name there anyway - if there was a port
3887
number - since the port number is to be ignored! */
3888
if(conn->bits.httpproxy) {
3889
/* we need to create new URL with the new port number */
3891
bool isftp = (bool)(Curl_raw_equal("ftp", conn->protostr) ||
3892
Curl_raw_equal("ftps", conn->protostr));
3895
* This synthesized URL isn't always right--suffixes like ;type=A
3896
* are stripped off. It would be better to work directly from the
3897
* original URL and simply replace the port part of it.
3899
url = aprintf("%s://%s%s%s:%d%s%s", conn->protostr,
3900
conn->bits.ipv6_ip?"[":"", conn->host.name,
3901
conn->bits.ipv6_ip?"]":"", conn->remote_port,
3902
isftp?"/":"", data->state.path);
3904
return CURLE_OUT_OF_MEMORY;
3906
if(data->change.url_alloc)
3907
free(data->change.url);
3909
data->change.url = url;
3910
data->change.url_alloc = TRUE;
3914
/* no CURLOPT_PORT given, extract the one from the URL */
3919
port=strtoul(portptr+1, &rest, 10); /* Port number must be decimal */
3921
if(rest != (portptr+1) && *rest == '\0') {
3922
/* The colon really did have only digits after it,
3923
* so it is either a port number or a mistake */
3925
if(port > 0xffff) { /* Single unix standard says port numbers are
3927
failf(data, "Port number too large: %lu", port);
3928
return CURLE_URL_MALFORMAT;
3931
*portptr = '\0'; /* cut off the name there */
3932
conn->remote_port = (unsigned short)port;
3939
* Override a user name and password from the URL with that in the
3940
* CURLOPT_USERPWD option or a .netrc file, if applicable.
3942
static void override_userpass(struct SessionHandle *data,
3943
struct connectdata *conn,
3944
char *user, char *passwd)
3946
if(data->set.str[STRING_USERNAME] != NULL) {
3947
strncpy(user, data->set.str[STRING_USERNAME], MAX_CURL_USER_LENGTH);
3948
user[MAX_CURL_USER_LENGTH-1] = '\0'; /*To be on safe side*/
3950
if(data->set.str[STRING_PASSWORD] != NULL) {
3951
strncpy(passwd, data->set.str[STRING_PASSWORD], MAX_CURL_PASSWORD_LENGTH);
3952
passwd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
3955
conn->bits.netrc = FALSE;
3956
if(data->set.use_netrc != CURL_NETRC_IGNORED) {
3957
if(Curl_parsenetrc(conn->host.name,
3959
data->set.str[STRING_NETRC_FILE])) {
3960
infof(data, "Couldn't find host %s in the "
3961
DOT_CHAR "netrc file; using defaults\n",
3965
/* set bits.netrc TRUE to remember that we got the name from a .netrc
3966
file, so that it is safe to use even if we followed a Location: to a
3967
different host or similar. */
3968
conn->bits.netrc = TRUE;
3970
conn->bits.user_passwd = 1; /* enable user+password */
3976
* Set password so it's available in the connection.
3978
static CURLcode set_userpass(struct connectdata *conn,
3979
const char *user, const char *passwd)
3981
/* If our protocol needs a password and we have none, use the defaults */
3982
if( (conn->protocol & PROT_FTP) &&
3983
!conn->bits.user_passwd) {
3985
conn->user = strdup(CURL_DEFAULT_USER);
3986
conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
3987
/* This is the default password, so DON'T set conn->bits.user_passwd */
3990
/* store user + password, zero-length if not set */
3991
conn->user = strdup(user);
3992
conn->passwd = strdup(passwd);
3994
if(!conn->user || !conn->passwd)
3995
return CURLE_OUT_OF_MEMORY;
4000
/*************************************************************
4001
* Resolve the address of the server or proxy
4002
*************************************************************/
4003
static CURLcode resolve_server(struct SessionHandle *data,
4004
struct connectdata *conn,
4005
struct Curl_dns_entry **addr,
4008
CURLcode result=CURLE_OK;
4009
long shortest = 0; /* default to no timeout */
4011
/*************************************************************
4012
* Set timeout if that is being used
4013
*************************************************************/
4014
if(data->set.timeout || data->set.connecttimeout) {
4016
/* We set the timeout on the name resolving phase first, separately from
4017
* the download/upload part to allow a maximum time on everything. This is
4018
* a signal-based timeout, why it won't work and shouldn't be used in
4019
* multi-threaded environments. */
4021
shortest = data->set.timeout; /* default to this timeout value */
4022
if(shortest && data->set.connecttimeout &&
4023
(data->set.connecttimeout < shortest))
4024
/* if both are set, pick the shortest */
4025
shortest = data->set.connecttimeout;
4027
/* if timeout is not set, use the connect timeout */
4028
shortest = data->set.connecttimeout;
4029
/* We can expect the conn->created time to be "now", as that was just
4030
recently set in the beginning of this function and nothing slow
4031
has been done since then until now. */
4034
/*************************************************************
4035
* Resolve the name of the server or proxy
4036
*************************************************************/
4037
if(conn->bits.reuse) {
4038
/* re-used connection, no resolving is necessary */
4040
/* we'll need to clear conn->dns_entry later in Curl_disconnect() */
4042
if(conn->bits.proxy)
4043
fix_hostname(data, conn, &conn->host);
4046
/* this is a fresh connect */
4048
struct Curl_dns_entry *hostaddr;
4050
/* set a pointer to the hostname we display */
4051
fix_hostname(data, conn, &conn->host);
4053
if(!conn->proxy.name || !*conn->proxy.name) {
4054
/* If not connecting via a proxy, extract the port from the URL, if it is
4055
* there, thus overriding any defaults that might have been set above. */
4056
conn->port = conn->remote_port; /* it is the same port */
4058
/* Resolve target host right on */
4059
rc = Curl_resolv_timeout(conn, conn->host.name, (int)conn->port,
4060
&hostaddr, shortest);
4061
if(rc == CURLRESOLV_PENDING)
4064
else if (rc == CURLRESOLV_TIMEDOUT)
4065
result = CURLE_OPERATION_TIMEDOUT;
4067
else if(!hostaddr) {
4068
failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
4069
result = CURLE_COULDNT_RESOLVE_HOST;
4070
/* don't return yet, we need to clean up the timeout first */
4074
/* This is a proxy that hasn't been resolved yet. */
4076
/* IDN-fix the proxy name */
4077
fix_hostname(data, conn, &conn->proxy);
4080
rc = Curl_resolv_timeout(conn, conn->proxy.name, (int)conn->port,
4081
&hostaddr, shortest);
4083
if(rc == CURLRESOLV_PENDING)
4086
else if (rc == CURLRESOLV_TIMEDOUT)
4087
result = CURLE_OPERATION_TIMEDOUT;
4089
else if(!hostaddr) {
4090
failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
4091
result = CURLE_COULDNT_RESOLVE_PROXY;
4092
/* don't return yet, we need to clean up the timeout first */
4102
* Cleanup the connection just allocated before we can move along and use the
4103
* previously existing one. All relevant data is copied over and old_conn is
4104
* ready for freeing once this function returns.
4106
static void reuse_conn(struct connectdata *old_conn,
4107
struct connectdata *conn)
4109
if(old_conn->proxy.rawalloc)
4110
free(old_conn->proxy.rawalloc);
4112
/* free the SSL config struct from this connection struct as this was
4113
allocated in vain and is targeted for destruction */
4114
Curl_free_ssl_config(&old_conn->ssl_config);
4116
conn->data = old_conn->data;
4118
/* get the user+password information from the old_conn struct since it may
4119
* be new for this request even when we re-use an existing connection */
4120
conn->bits.user_passwd = old_conn->bits.user_passwd;
4121
if(conn->bits.user_passwd) {
4122
/* use the new user name and password though */
4123
Curl_safefree(conn->user);
4124
Curl_safefree(conn->passwd);
4125
conn->user = old_conn->user;
4126
conn->passwd = old_conn->passwd;
4127
old_conn->user = NULL;
4128
old_conn->passwd = NULL;
4131
conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
4132
if(conn->bits.proxy_user_passwd) {
4133
/* use the new proxy user name and proxy password though */
4134
Curl_safefree(conn->proxyuser);
4135
Curl_safefree(conn->proxypasswd);
4136
conn->proxyuser = old_conn->proxyuser;
4137
conn->proxypasswd = old_conn->proxypasswd;
4138
old_conn->proxyuser = NULL;
4139
old_conn->proxypasswd = NULL;
4142
/* host can change, when doing keepalive with a proxy ! */
4143
if(conn->bits.proxy) {
4144
free(conn->host.rawalloc);
4145
conn->host=old_conn->host;
4148
free(old_conn->host.rawalloc); /* free the newly allocated name buffer */
4151
conn->bits.reuse = TRUE; /* yes, we're re-using here */
4153
Curl_safefree(old_conn->user);
4154
Curl_safefree(old_conn->passwd);
4155
Curl_safefree(old_conn->proxyuser);
4156
Curl_safefree(old_conn->proxypasswd);
4157
Curl_llist_destroy(old_conn->send_pipe, NULL);
4158
Curl_llist_destroy(old_conn->recv_pipe, NULL);
4159
Curl_llist_destroy(old_conn->pend_pipe, NULL);
4160
Curl_safefree(old_conn->master_buffer);
3439
* CreateConnection() sets up a new connectdata struct, or re-uses an already
4164
* create_conn() sets up a new connectdata struct, or re-uses an already
3440
4165
* existing one, and resolves host name.
3442
4167
* if this function returns CURLE_OK and *async is set to TRUE, the resolve
3725
4446
conn->bits.tunnel_proxy = TRUE;
3727
4448
/*************************************************************
3728
* Take care of user and password authentication stuff
3729
*************************************************************/
3732
* Inputs: data->set.userpwd (CURLOPT_USERPWD)
3733
* data->set.fpasswd (CURLOPT_PASSWDFUNCTION)
3734
* data->set.use_netrc (CURLOPT_NETRC)
3737
* hard-coded defaults
3739
* Outputs: (almost :- all currently undefined)
3740
* conn->bits.user_passwd - non-zero if non-default passwords exist
3741
* conn->user - non-zero length if defined
3742
* conn->passwd - ditto
3743
* conn->host.name - remove user name and password
3746
/* At this point, we're hoping all the other special cases have
3747
* been taken care of, so conn->host.name is at most
3748
* [user[:password]]@]hostname
3750
* We need somewhere to put the embedded details, so do that first.
3753
user[0] =0; /* to make everything well-defined */
3756
if(conn->protocol & (PROT_FTP|PROT_HTTP|PROT_SCP|PROT_SFTP)) {
3757
/* This is a FTP, HTTP, SCP or SFTP URL, we will now try to extract the
3758
* possible user+password pair in a string like:
3759
* ftp://user:password@ftp.my.site:8021/README */
3760
char *ptr=strchr(conn->host.name, '@');
3761
char *userpass = conn->host.name;
3763
/* there's a user+password given here, to the left of the @ */
3765
conn->host.name = ++ptr;
3767
/* So the hostname is sane. Only bother interpreting the
3768
* results if we could care. It could still be wasted
3769
* work because it might be overtaken by the programmatically
3770
* set user/passwd, but doing that first adds more cases here :-(
3773
if(data->set.use_netrc != CURL_NETRC_REQUIRED) {
3774
/* We could use the one in the URL */
3776
conn->bits.user_passwd = 1; /* enable user+password */
3778
if(*userpass != ':') {
3779
/* the name is given, get user+password */
3780
sscanf(userpass, "%" MAX_CURL_USER_LENGTH_TXT "[^:@]:"
3781
"%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
3785
/* no name given, get the password only */
3786
sscanf(userpass, ":%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]", passwd);
3789
char *newname=curl_easy_unescape(data, user, 0, NULL);
3791
return CURLE_OUT_OF_MEMORY;
3792
if(strlen(newname) < sizeof(user))
3793
strcpy(user, newname);
3795
/* if the new name is longer than accepted, then just use
3796
the unconverted name, it'll be wrong but what the heck */
3800
/* we have a password found in the URL, decode it! */
3801
char *newpasswd=curl_easy_unescape(data, passwd, 0, NULL);
3803
return CURLE_OUT_OF_MEMORY;
3804
if(strlen(newpasswd) < sizeof(passwd))
3805
strcpy(passwd, newpasswd);
3813
/*************************************************************
3814
* Figure out the remote port number
3816
* No matter if we use a proxy or not, we have to figure out the remote
3817
* port number of various reasons.
3819
* To be able to detect port number flawlessly, we must not confuse them
3820
* IPv6-specified addresses in the [0::1] style. (RFC2732)
3822
* The conn->host.name is currently [user:passwd@]host[:port] where host
3823
* could be a hostname, IPv4 address or IPv6 address.
3824
*************************************************************/
3825
if((1 == sscanf(conn->host.name, "[%*39[0-9a-fA-F:.]%c", &endbracket)) &&
3826
(']' == endbracket)) {
3827
/* this is a RFC2732-style specified IP-address */
3828
conn->bits.ipv6_ip = TRUE;
3830
conn->host.name++; /* pass the starting bracket */
3831
tmp = strchr(conn->host.name, ']');
3832
*tmp = 0; /* zero terminate */
3833
tmp++; /* pass the ending bracket */
3835
tmp = NULL; /* no port number available */
3838
tmp = strrchr(conn->host.name, ':');
3840
if(data->set.use_port && data->state.allow_port) {
3841
/* if set, we use this and ignore the port possibly given in the URL */
3842
conn->remote_port = (unsigned short)data->set.use_port;
3844
*tmp = '\0'; /* cut off the name there anyway - if there was a port
3845
number - since the port number is to be ignored! */
3846
if(conn->bits.httpproxy) {
3847
/* we need to create new URL with the new port number */
3850
url = aprintf("%s://%s:%d%s", conn->protostr, conn->host.name,
3851
conn->remote_port, data->state.path);
3853
return CURLE_OUT_OF_MEMORY;
3855
if(data->change.url_alloc)
3856
free(data->change.url);
3858
data->change.url = url;
3859
data->change.url_alloc = TRUE;
3863
/* no CURLOPT_PORT given, extract the one from the URL */
3868
port=strtoul(tmp+1, &rest, 10); /* Port number must be decimal */
3870
if(rest != (tmp+1) && *rest == '\0') {
3871
/* The colon really did have only digits after it,
3872
* so it is either a port number or a mistake */
3874
if(port > 0xffff) { /* Single unix standard says port numbers are
3876
failf(data, "Port number too large: %lu", port);
3877
return CURLE_URL_MALFORMAT;
3880
*tmp = '\0'; /* cut off the name there */
3881
conn->remote_port = (unsigned short)port;
3885
/* Programmatically set password:
3886
* - always applies, if available
3887
* - takes precedence over the values we just set above
3888
* so scribble it over the top.
3889
* User-supplied passwords are assumed not to need unescaping.
3891
* user_password is set in "inherit initial knowledge' above,
3892
* so it doesn't have to be set in this block
3894
if(data->set.str[STRING_USERPWD] != NULL) {
3895
/* the name is given, get user+password */
3896
sscanf(data->set.str[STRING_USERPWD],
3897
"%" MAX_CURL_USER_LENGTH_TXT "[^:]:"
3898
"%" MAX_CURL_PASSWORD_LENGTH_TXT "[^\n]",
3902
conn->bits.netrc = FALSE;
3903
if(data->set.use_netrc != CURL_NETRC_IGNORED) {
3904
if(Curl_parsenetrc(conn->host.name,
3906
data->set.str[STRING_NETRC_FILE])) {
3907
infof(data, "Couldn't find host %s in the " DOT_CHAR
3908
"netrc file, using defaults\n",
3912
/* set bits.netrc TRUE to remember that we got the name from a .netrc
3913
file, so that it is safe to use even if we followed a Location: to a
3914
different host or similar. */
3915
conn->bits.netrc = TRUE;
3917
conn->bits.user_passwd = 1; /* enable user+password */
3921
/* If our protocol needs a password and we have none, use the defaults */
3922
if( (conn->protocol & PROT_FTP) &&
3923
!conn->bits.user_passwd) {
3925
conn->user = strdup(CURL_DEFAULT_USER);
3926
conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
3927
/* This is the default password, so DON'T set conn->bits.user_passwd */
3930
/* store user + password, zero-length if not set */
3931
conn->user = strdup(user);
3932
conn->passwd = strdup(passwd);
3934
if(!conn->user || !conn->passwd)
3935
return CURLE_OUT_OF_MEMORY;
4449
* Parse a user name and password in the URL and strip it out
4451
*************************************************************/
4452
result = parse_url_userpass(data, conn, user, passwd);
4453
if(result != CURLE_OK)
4456
/*************************************************************
4457
* Figure out the remote port number and fix it in the URL
4458
*************************************************************/
4459
result = parse_remote_port(data, conn);
4460
if(result != CURLE_OK)
4463
/*************************************************************
4464
* Check for an overridden user name and password, then set it
4466
*************************************************************/
4467
override_userpass(data, conn, user, passwd);
4468
result = set_userpass(conn, user, passwd);
4469
if(result != CURLE_OK)
3937
4472
/*************************************************************
3938
4473
* Check the current list of connections to see if we can
3974
4511
* just allocated before we can move along and use the previously
3975
4512
* existing one.
3977
struct connectdata *old_conn = conn;
3979
if(old_conn->proxy.rawalloc)
3980
free(old_conn->proxy.rawalloc);
3982
/* free the SSL config struct from this connection struct as this was
3983
allocated in vain and is targeted for destruction */
3984
Curl_free_ssl_config(&conn->ssl_config);
3986
conn = conn_temp; /* use this connection from now on */
3988
conn->data = old_conn->data;
3990
/* get the user+password information from the old_conn struct since it may
3991
* be new for this request even when we re-use an existing connection */
3992
conn->bits.user_passwd = old_conn->bits.user_passwd;
3993
if(conn->bits.user_passwd) {
3994
/* use the new user namd and password though */
3995
Curl_safefree(conn->user);
3996
Curl_safefree(conn->passwd);
3997
conn->user = old_conn->user;
3998
conn->passwd = old_conn->passwd;
3999
old_conn->user = NULL;
4000
old_conn->passwd = NULL;
4003
conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
4004
if(conn->bits.proxy_user_passwd) {
4005
/* use the new proxy user name and proxy password though */
4006
Curl_safefree(conn->proxyuser);
4007
Curl_safefree(conn->proxypasswd);
4008
conn->proxyuser = old_conn->proxyuser;
4009
conn->proxypasswd = old_conn->proxypasswd;
4010
old_conn->proxyuser = NULL;
4011
old_conn->proxypasswd = NULL;
4014
/* host can change, when doing keepalive with a proxy ! */
4015
if(conn->bits.proxy) {
4016
free(conn->host.rawalloc);
4017
conn->host=old_conn->host;
4020
free(old_conn->host.rawalloc); /* free the newly allocated name buffer */
4023
conn->bits.reuse = TRUE; /* yes, we're re-using here */
4025
Curl_safefree(old_conn->user);
4026
Curl_safefree(old_conn->passwd);
4027
Curl_safefree(old_conn->proxyuser);
4028
Curl_safefree(old_conn->proxypasswd);
4029
Curl_llist_destroy(old_conn->send_pipe, NULL);
4030
Curl_llist_destroy(old_conn->recv_pipe, NULL);
4031
Curl_llist_destroy(old_conn->pend_pipe, NULL);
4032
Curl_safefree(old_conn->master_buffer);
4034
free(old_conn); /* we don't need this anymore */
4036
*in_connect = conn; /* return this instead! */
4514
reuse_conn(conn, conn_temp);
4515
free(conn); /* we don't need this anymore */
4038
4518
infof(data, "Re-using existing connection! (#%ld) with host %s\n",
4039
4519
conn->connectindex,
4040
4520
conn->proxy.name?conn->proxy.dispname:conn->host.dispname);
4521
/* copy this IP address to the common buffer for the easy handle so that
4522
the address can actually survice the removal of this connection. strcpy
4523
is safe since the target buffer is big enough to hold the largest
4524
possible IP address */
4525
strcpy(data->info.ip, conn->ip_addr_str);
4054
4543
/* Continue connectdata initialization here. */
4058
4546
* Inherit the proper values from the urldata struct AFTER we have arranged
4059
* the persistent connection stuff */
4547
* the persistent connection stuff
4060
4549
conn->fread_func = data->set.fread_func;
4061
4550
conn->fread_in = data->set.in;
4062
4551
conn->seek_func = data->set.seek_func;
4063
4552
conn->seek_client = data->set.seek_client;
4066
/*************************************************************
4067
* Set timeout if that is being used, and we're not using an asynchronous
4069
*************************************************************/
4070
if((data->set.timeout || data->set.connecttimeout) && !data->set.no_signal) {
4071
/*************************************************************
4072
* Set signal handler to catch SIGALRM
4073
* Store the old value to be able to set it back later!
4074
*************************************************************/
4080
#ifdef HAVE_SIGACTION
4081
struct sigaction sigact;
4082
sigaction(SIGALRM, NULL, &sigact);
4083
keep_sigact = sigact;
4084
keep_copysig = TRUE; /* yes, we have a copy */
4085
sigact.sa_handler = alarmfunc;
4087
/* HPUX doesn't have SA_RESTART but defaults to that behaviour! */
4088
sigact.sa_flags &= ~SA_RESTART;
4090
/* now set the new struct */
4091
sigaction(SIGALRM, &sigact, NULL);
4092
#else /* HAVE_SIGACTION */
4093
/* no sigaction(), revert to the much lamer signal() */
4095
keep_sigact = signal(SIGALRM, alarmfunc);
4097
#endif /* HAVE_SIGACTION */
4099
/* We set the timeout on the name resolving phase first, separately from
4100
* the download/upload part to allow a maximum time on everything. This is
4101
* a signal-based timeout, why it won't work and shouldn't be used in
4102
* multi-threaded environments. */
4105
shortest = data->set.timeout; /* default to this timeout value */
4106
if(shortest && data->set.connecttimeout &&
4107
(data->set.connecttimeout < shortest))
4108
/* if both are set, pick the shortest */
4109
shortest = data->set.connecttimeout;
4111
/* if timeout is not set, use the connect timeout */
4112
shortest = data->set.connecttimeout;
4115
/* the alarm() function only provide integer second resolution, so if
4116
we want to wait less than one second we must bail out already now. */
4117
return CURLE_OPERATION_TIMEDOUT;
4119
/* alarm() makes a signal get sent when the timeout fires off, and that
4120
will abort system calls */
4121
prev_alarm = alarm((unsigned int) (shortest ? shortest/1000L : shortest));
4122
/* We can expect the conn->created time to be "now", as that was just
4123
recently set in the beginning of this function and nothing slow
4124
has been done since then until now. */
4126
#endif /* SIGALRM */
4128
#endif /* USE_ARES */
4130
/*************************************************************
4131
* Resolve the name of the server or proxy
4132
*************************************************************/
4133
if(conn->bits.reuse) {
4134
/* re-used connection, no resolving is necessary */
4136
/* we'll need to clear conn->dns_entry later in Curl_disconnect() */
4138
if(conn->bits.proxy)
4139
fix_hostname(data, conn, &conn->host);
4142
/* this is a fresh connect */
4144
/* set a pointer to the hostname we display */
4145
fix_hostname(data, conn, &conn->host);
4147
if(!conn->proxy.name || !*conn->proxy.name) {
4148
/* If not connecting via a proxy, extract the port from the URL, if it is
4149
* there, thus overriding any defaults that might have been set above. */
4150
conn->port = conn->remote_port; /* it is the same port */
4152
/* Resolve target host right on */
4153
rc = Curl_resolv(conn, conn->host.name, (int)conn->port, &hostaddr);
4154
if(rc == CURLRESOLV_PENDING)
4157
else if(!hostaddr) {
4158
failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
4159
result = CURLE_COULDNT_RESOLVE_HOST;
4160
/* don't return yet, we need to clean up the timeout first */
4164
/* This is a proxy that hasn't been resolved yet. */
4166
/* IDN-fix the proxy name */
4167
fix_hostname(data, conn, &conn->proxy);
4170
rc = Curl_resolv(conn, conn->proxy.name, (int)conn->port, &hostaddr);
4172
if(rc == CURLRESOLV_PENDING)
4175
else if(!hostaddr) {
4176
failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
4177
result = CURLE_COULDNT_RESOLVE_PROXY;
4178
/* don't return yet, we need to clean up the timeout first */
4184
#if defined(HAVE_ALARM) && defined(SIGALRM) && !defined(USE_ARES)
4185
if((data->set.timeout || data->set.connecttimeout) && !data->set.no_signal) {
4186
#ifdef HAVE_SIGACTION
4188
/* we got a struct as it looked before, now put that one back nice
4190
sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */
4194
/* restore the previous SIGALRM handler */
4195
signal(SIGALRM, keep_sigact);
4197
#endif /* HAVE_SIGACTION */
4199
/* switch back the alarm() to either zero or to what it was before minus
4200
the time we spent until now! */
4202
/* there was an alarm() set before us, now put it back */
4203
unsigned long elapsed_ms = Curl_tvdiff(Curl_tvnow(), conn->created);
4204
unsigned long alarm_set;
4206
/* the alarm period is counted in even number of seconds */
4207
alarm_set = prev_alarm - elapsed_ms/1000;
4210
((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) {
4211
/* if the alarm time-left reached zero or turned "negative" (counted
4212
with unsigned values), we should fire off a SIGALRM here, but we
4213
won't, and zero would be to switch it off so we never set it to
4216
result = CURLE_OPERATION_TIMEDOUT;
4217
failf(data, "Previous alarm fired off!");
4220
alarm((unsigned int)alarm_set);
4223
alarm(0); /* just shut it off */
4554
/*************************************************************
4555
* Resolve the address of the server or proxy
4556
*************************************************************/
4557
result = resolve_server(data, conn, addr, async);
4230
/* SetupConnection() is called after the name resolve initiated in
4231
* CreateConnection() is all done.
4562
/* setup_conn() is called after the name resolve initiated in
4563
* create_conn() is all done.
4233
4565
* NOTE: the argument 'hostaddr' is NULL when this function is called for a
4234
4566
* re-used connection.
4236
* conn->data MUST already have been setup fine (in CreateConnection)
4568
* conn->data MUST already have been setup fine (in create_conn)
4239
static CURLcode SetupConnection(struct connectdata *conn,
4240
struct Curl_dns_entry *hostaddr,
4241
bool *protocol_done)
4571
static CURLcode setup_conn(struct connectdata *conn,
4572
struct Curl_dns_entry *hostaddr,
4573
bool *protocol_done)
4243
4575
CURLcode result=CURLE_OK;
4244
4576
struct SessionHandle *data = conn->data;