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
2001
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;
2003
struct Curl_share *set;
2004
set = va_arg(param, struct Curl_share *);
2006
/* disconnect from old share, if any */
2008
Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2010
if(data->dns.hostcachetype == HCACHE_SHARED) {
2011
data->dns.hostcache = NULL;
2012
data->dns.hostcachetype = HCACHE_NONE;
2015
if(data->share->cookies == data->cookies)
2016
data->cookies = NULL;
2018
data->share->dirty--;
2020
Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2024
/* use new share if it set */
2028
Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2030
data->share->dirty++;
2032
if(data->share->hostcache) {
2033
/* use shared host cache, first free the private one if any */
2034
if(data->dns.hostcachetype == HCACHE_PRIVATE)
2035
Curl_hash_destroy(data->dns.hostcache);
2037
data->dns.hostcache = data->share->hostcache;
2038
data->dns.hostcachetype = HCACHE_SHARED;
1881
2040
#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;
2041
if(data->share->cookies) {
2042
/* use shared cookie list, first free own one if any */
2044
Curl_cookie_cleanup(data->cookies);
2045
/* enable cookies since we now use a share that uses cookies! */
2046
data->cookies = data->share->cookies;
1889
2048
#endif /* CURL_DISABLE_HTTP */
1890
Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2049
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);
2052
/* check for host cache not needed,
2053
* it will be done by curl_easy_perform */
1905
2057
case CURLOPT_PRIVATE:
3157
3443
/* The protocol was not found in the table, but we don't have to assign it
3158
3444
to anything since it is already assigned to a dummy-struct in the
3159
CreateConnection() function when the connectdata struct is allocated. */
3445
create_conn() function when the connectdata struct is allocated. */
3160
3446
failf(data, "Protocol %s not supported or disabled in " LIBCURL_NAME,
3161
3447
conn->protostr);
3162
3448
return CURLE_UNSUPPORTED_PROTOCOL;
3451
#ifndef CURL_DISABLE_PROXY
3452
/****************************************************************
3453
* Checks if the host is in the noproxy list. returns true if it matches
3454
* and therefore the proxy should NOT be used.
3455
****************************************************************/
3456
static bool check_noproxy(const char* name, const char* no_proxy)
3458
/* no_proxy=domain1.dom,host.domain2.dom
3459
* (a comma-separated list of hosts which should
3460
* not be proxied, or an asterisk to override
3461
* all proxy variables)
3465
const char* separator = ", ";
3466
size_t no_proxy_len;
3470
if(no_proxy && no_proxy[0]) {
3471
if(Curl_raw_equal("*", no_proxy)) {
3475
/* NO_PROXY was specified and it wasn't just an asterisk */
3477
no_proxy_len = strlen(no_proxy);
3478
endptr = strchr(name, ':');
3480
namelen = endptr - name;
3482
namelen = strlen(name);
3485
for (tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
3486
while (tok_start < no_proxy_len &&
3487
strchr(separator, no_proxy[tok_start]) != NULL) {
3488
/* Look for the beginning of the token. */
3492
if(tok_start == no_proxy_len)
3493
break; /* It was all trailing separator chars, no more tokens. */
3495
for (tok_end = tok_start; tok_end < no_proxy_len &&
3496
strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end) {
3497
/* Look for the end of the token. */
3500
/* To match previous behaviour, where it was necessary to specify
3501
* ".local.com" to prevent matching "notlocal.com", we will leave
3504
if(no_proxy[tok_start] == '.')
3507
if((tok_end - tok_start) <= namelen) {
3508
/* Match the last part of the name to the domain we are checking. */
3509
const char *checkn = name + namelen - (tok_end - tok_start);
3510
if(Curl_raw_nequal(no_proxy + tok_start, checkn, tok_end - tok_start)) {
3511
if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
3512
/* We either have an exact match, or the previous character is a .
3513
* so it is within the same domain, so no proxy for this host.
3518
} /* if((tok_end - tok_start) <= namelen) */
3519
} /* for (tok_start = 0; tok_start < no_proxy_len;
3520
tok_start = tok_end + 1) */
3521
} /* NO_PROXY was specified and it wasn't just an asterisk */
3165
3526
/****************************************************************
3166
3527
* Detect what (if any) proxy to use. Remember that this selects a host
3167
3528
* name and is not limited to HTTP proxies only.
3190
3551
* checked if the lowercase versions don't exist.
3192
3553
char *no_proxy=NULL;
3193
char *no_proxy_tok_buf;
3194
3554
char proxy_env[128];
3196
3556
no_proxy=curl_getenv("no_proxy");
3198
3558
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: */
3560
if(!check_noproxy(conn->host.name, no_proxy)) {
3561
/* It was not listed as without proxy */
3562
char *protop = conn->protostr;
3563
char *envp = proxy_env;
3566
/* Now, build <protocol>_proxy and check for such a one to use */
3568
*envp++ = (char)tolower((int)*protop++);
3571
strcpy(envp, "_proxy");
3573
/* read the protocol proxy: */
3574
prox=curl_getenv(proxy_env);
3577
* We don't try the uppercase version of HTTP_PROXY because of
3580
* When curl is used in a webserver application
3581
* environment (cgi or php), this environment variable can
3582
* be controlled by the web server user by setting the
3583
* http header 'Proxy:' to some value.
3585
* This can cause 'internal' http/ftp requests to be
3586
* arbitrarily redirected by any external attacker.
3588
if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
3589
/* There was no lowercase variable, try the uppercase version: */
3590
Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
3237
3591
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 '*' */
3594
if(prox && *prox) { /* don't count "" strings */
3595
proxy = prox; /* use this */
3598
proxy = curl_getenv("all_proxy"); /* default proxy to use */
3600
proxy=curl_getenv("ALL_PROXY");
3602
} /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
3279
3605
free(no_proxy);
3435
3771
return CURLE_OK;
3773
#endif /* CURL_DISABLE_PROXY */
3777
* Parse a user name and password in the URL and strip it out of the host name
3779
* Inputs: data->set.use_netrc (CURLOPT_NETRC)
3782
* Outputs: (almost :- all currently undefined)
3783
* conn->bits.user_passwd - non-zero if non-default passwords exist
3784
* user - non-zero length if defined
3786
* conn->host.name - remove user name and password
3788
static CURLcode parse_url_userpass(struct SessionHandle *data,
3789
struct connectdata *conn,
3790
char *user, char *passwd)
3792
/* At this point, we're hoping all the other special cases have
3793
* been taken care of, so conn->host.name is at most
3794
* [user[:password]]@]hostname
3796
* We need somewhere to put the embedded details, so do that first.
3799
user[0] =0; /* to make everything well-defined */
3802
if(conn->protocol & (PROT_FTP|PROT_HTTP|PROT_SCP|PROT_SFTP)) {
3803
/* This is a FTP, HTTP, SCP or SFTP URL, we will now try to extract the
3804
* possible user+password pair in a string like:
3805
* ftp://user:password@ftp.my.site:8021/README */
3806
char *ptr=strchr(conn->host.name, '@');
3807
char *userpass = conn->host.name;
3809
/* there's a user+password given here, to the left of the @ */
3811
conn->host.name = ++ptr;
3813
/* So the hostname is sane. Only bother interpreting the
3814
* results if we could care. It could still be wasted
3815
* work because it might be overtaken by the programmatically
3816
* set user/passwd, but doing that first adds more cases here :-(
3819
if(data->set.use_netrc != CURL_NETRC_REQUIRED) {
3820
/* We could use the one in the URL */
3822
conn->bits.user_passwd = 1; /* enable user+password */
3824
if(*userpass != ':') {
3825
/* the name is given, get user+password */
3826
sscanf(userpass, "%" MAX_CURL_USER_LENGTH_TXT "[^:@]:"
3827
"%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
3831
/* no name given, get the password only */
3832
sscanf(userpass, ":%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]", passwd);
3835
char *newname=curl_easy_unescape(data, user, 0, NULL);
3837
return CURLE_OUT_OF_MEMORY;
3838
if(strlen(newname) < MAX_CURL_USER_LENGTH)
3839
strcpy(user, newname);
3841
/* if the new name is longer than accepted, then just use
3842
the unconverted name, it'll be wrong but what the heck */
3846
/* we have a password found in the URL, decode it! */
3847
char *newpasswd=curl_easy_unescape(data, passwd, 0, NULL);
3849
return CURLE_OUT_OF_MEMORY;
3850
if(strlen(newpasswd) < MAX_CURL_PASSWORD_LENGTH)
3851
strcpy(passwd, newpasswd);
3861
/*************************************************************
3862
* Figure out the remote port number and fix it in the URL
3864
* No matter if we use a proxy or not, we have to figure out the remote
3865
* port number of various reasons.
3867
* To be able to detect port number flawlessly, we must not confuse them
3868
* IPv6-specified addresses in the [0::1] style. (RFC2732)
3870
* The conn->host.name is currently [user:passwd@]host[:port] where host
3871
* could be a hostname, IPv4 address or IPv6 address.
3873
* The port number embedded in the URL is replaced, if necessary.
3874
*************************************************************/
3875
static CURLcode parse_remote_port(struct SessionHandle *data,
3876
struct connectdata *conn)
3881
/* Note that at this point, the IPv6 address cannot contain any scope
3882
suffix as that has already been removed in the ParseURLAndFillConnection()
3884
if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
3886
(']' == endbracket)) {
3887
/* this is a RFC2732-style specified IP-address */
3888
conn->bits.ipv6_ip = TRUE;
3890
conn->host.name++; /* skip over the starting bracket */
3891
portptr = strchr(conn->host.name, ']');
3892
*portptr++ = 0; /* zero terminate, killing the bracket */
3894
portptr = NULL; /* no port number available */
3897
portptr = strrchr(conn->host.name, ':');
3899
if(data->set.use_port && data->state.allow_port) {
3900
/* if set, we use this and ignore the port possibly given in the URL */
3901
conn->remote_port = (unsigned short)data->set.use_port;
3903
*portptr = '\0'; /* cut off the name there anyway - if there was a port
3904
number - since the port number is to be ignored! */
3905
if(conn->bits.httpproxy) {
3906
/* we need to create new URL with the new port number */
3908
bool isftp = (bool)(Curl_raw_equal("ftp", conn->protostr) ||
3909
Curl_raw_equal("ftps", conn->protostr));
3912
* This synthesized URL isn't always right--suffixes like ;type=A
3913
* are stripped off. It would be better to work directly from the
3914
* original URL and simply replace the port part of it.
3916
url = aprintf("%s://%s%s%s:%d%s%s", conn->protostr,
3917
conn->bits.ipv6_ip?"[":"", conn->host.name,
3918
conn->bits.ipv6_ip?"]":"", conn->remote_port,
3919
isftp?"/":"", data->state.path);
3921
return CURLE_OUT_OF_MEMORY;
3923
if(data->change.url_alloc)
3924
free(data->change.url);
3926
data->change.url = url;
3927
data->change.url_alloc = TRUE;
3931
/* no CURLOPT_PORT given, extract the one from the URL */
3936
port=strtoul(portptr+1, &rest, 10); /* Port number must be decimal */
3938
if(rest != (portptr+1) && *rest == '\0') {
3939
/* The colon really did have only digits after it,
3940
* so it is either a port number or a mistake */
3942
if(port > 0xffff) { /* Single unix standard says port numbers are
3944
failf(data, "Port number too large: %lu", port);
3945
return CURLE_URL_MALFORMAT;
3948
*portptr = '\0'; /* cut off the name there */
3949
conn->remote_port = (unsigned short)port;
3956
* Override a user name and password from the URL with that in the
3957
* CURLOPT_USERPWD option or a .netrc file, if applicable.
3959
static void override_userpass(struct SessionHandle *data,
3960
struct connectdata *conn,
3961
char *user, char *passwd)
3963
if(data->set.str[STRING_USERNAME] != NULL) {
3964
strncpy(user, data->set.str[STRING_USERNAME], MAX_CURL_USER_LENGTH);
3965
user[MAX_CURL_USER_LENGTH-1] = '\0'; /*To be on safe side*/
3967
if(data->set.str[STRING_PASSWORD] != NULL) {
3968
strncpy(passwd, data->set.str[STRING_PASSWORD], MAX_CURL_PASSWORD_LENGTH);
3969
passwd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
3972
conn->bits.netrc = FALSE;
3973
if(data->set.use_netrc != CURL_NETRC_IGNORED) {
3974
if(Curl_parsenetrc(conn->host.name,
3976
data->set.str[STRING_NETRC_FILE])) {
3977
infof(data, "Couldn't find host %s in the "
3978
DOT_CHAR "netrc file; using defaults\n",
3982
/* set bits.netrc TRUE to remember that we got the name from a .netrc
3983
file, so that it is safe to use even if we followed a Location: to a
3984
different host or similar. */
3985
conn->bits.netrc = TRUE;
3987
conn->bits.user_passwd = 1; /* enable user+password */
3993
* Set password so it's available in the connection.
3995
static CURLcode set_userpass(struct connectdata *conn,
3996
const char *user, const char *passwd)
3998
/* If our protocol needs a password and we have none, use the defaults */
3999
if( (conn->protocol & PROT_FTP) &&
4000
!conn->bits.user_passwd) {
4002
conn->user = strdup(CURL_DEFAULT_USER);
4003
conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
4004
/* This is the default password, so DON'T set conn->bits.user_passwd */
4007
/* store user + password, zero-length if not set */
4008
conn->user = strdup(user);
4009
conn->passwd = strdup(passwd);
4011
if(!conn->user || !conn->passwd)
4012
return CURLE_OUT_OF_MEMORY;
4017
/*************************************************************
4018
* Resolve the address of the server or proxy
4019
*************************************************************/
4020
static CURLcode resolve_server(struct SessionHandle *data,
4021
struct connectdata *conn,
4022
struct Curl_dns_entry **addr,
4025
CURLcode result=CURLE_OK;
4026
long shortest = 0; /* default to no timeout */
4028
/*************************************************************
4029
* Set timeout if that is being used
4030
*************************************************************/
4031
if(data->set.timeout || data->set.connecttimeout) {
4033
/* We set the timeout on the name resolving phase first, separately from
4034
* the download/upload part to allow a maximum time on everything. This is
4035
* a signal-based timeout, why it won't work and shouldn't be used in
4036
* multi-threaded environments. */
4038
shortest = data->set.timeout; /* default to this timeout value */
4039
if(shortest && data->set.connecttimeout &&
4040
(data->set.connecttimeout < shortest))
4041
/* if both are set, pick the shortest */
4042
shortest = data->set.connecttimeout;
4044
/* if timeout is not set, use the connect timeout */
4045
shortest = data->set.connecttimeout;
4046
/* We can expect the conn->created time to be "now", as that was just
4047
recently set in the beginning of this function and nothing slow
4048
has been done since then until now. */
4051
/*************************************************************
4052
* Resolve the name of the server or proxy
4053
*************************************************************/
4054
if(conn->bits.reuse) {
4055
/* re-used connection, no resolving is necessary */
4057
/* we'll need to clear conn->dns_entry later in Curl_disconnect() */
4059
if(conn->bits.proxy)
4060
fix_hostname(data, conn, &conn->host);
4063
/* this is a fresh connect */
4065
struct Curl_dns_entry *hostaddr;
4067
/* set a pointer to the hostname we display */
4068
fix_hostname(data, conn, &conn->host);
4070
if(!conn->proxy.name || !*conn->proxy.name) {
4071
/* If not connecting via a proxy, extract the port from the URL, if it is
4072
* there, thus overriding any defaults that might have been set above. */
4073
conn->port = conn->remote_port; /* it is the same port */
4075
/* Resolve target host right on */
4076
rc = Curl_resolv_timeout(conn, conn->host.name, (int)conn->port,
4077
&hostaddr, shortest);
4078
if(rc == CURLRESOLV_PENDING)
4081
else if (rc == CURLRESOLV_TIMEDOUT)
4082
result = CURLE_OPERATION_TIMEDOUT;
4084
else if(!hostaddr) {
4085
failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
4086
result = CURLE_COULDNT_RESOLVE_HOST;
4087
/* don't return yet, we need to clean up the timeout first */
4091
/* This is a proxy that hasn't been resolved yet. */
4093
/* IDN-fix the proxy name */
4094
fix_hostname(data, conn, &conn->proxy);
4097
rc = Curl_resolv_timeout(conn, conn->proxy.name, (int)conn->port,
4098
&hostaddr, shortest);
4100
if(rc == CURLRESOLV_PENDING)
4103
else if (rc == CURLRESOLV_TIMEDOUT)
4104
result = CURLE_OPERATION_TIMEDOUT;
4106
else if(!hostaddr) {
4107
failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
4108
result = CURLE_COULDNT_RESOLVE_PROXY;
4109
/* don't return yet, we need to clean up the timeout first */
4119
* Cleanup the connection just allocated before we can move along and use the
4120
* previously existing one. All relevant data is copied over and old_conn is
4121
* ready for freeing once this function returns.
4123
static void reuse_conn(struct connectdata *old_conn,
4124
struct connectdata *conn)
4126
if(old_conn->proxy.rawalloc)
4127
free(old_conn->proxy.rawalloc);
4129
/* free the SSL config struct from this connection struct as this was
4130
allocated in vain and is targeted for destruction */
4131
Curl_free_ssl_config(&old_conn->ssl_config);
4133
conn->data = old_conn->data;
4135
/* get the user+password information from the old_conn struct since it may
4136
* be new for this request even when we re-use an existing connection */
4137
conn->bits.user_passwd = old_conn->bits.user_passwd;
4138
if(conn->bits.user_passwd) {
4139
/* use the new user name and password though */
4140
Curl_safefree(conn->user);
4141
Curl_safefree(conn->passwd);
4142
conn->user = old_conn->user;
4143
conn->passwd = old_conn->passwd;
4144
old_conn->user = NULL;
4145
old_conn->passwd = NULL;
4148
conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
4149
if(conn->bits.proxy_user_passwd) {
4150
/* use the new proxy user name and proxy password though */
4151
Curl_safefree(conn->proxyuser);
4152
Curl_safefree(conn->proxypasswd);
4153
conn->proxyuser = old_conn->proxyuser;
4154
conn->proxypasswd = old_conn->proxypasswd;
4155
old_conn->proxyuser = NULL;
4156
old_conn->proxypasswd = NULL;
4159
/* host can change, when doing keepalive with a proxy ! */
4160
if(conn->bits.proxy) {
4161
free(conn->host.rawalloc);
4162
conn->host=old_conn->host;
4165
free(old_conn->host.rawalloc); /* free the newly allocated name buffer */
4168
conn->bits.reuse = TRUE; /* yes, we're re-using here */
4170
Curl_safefree(old_conn->user);
4171
Curl_safefree(old_conn->passwd);
4172
Curl_safefree(old_conn->proxyuser);
4173
Curl_safefree(old_conn->proxypasswd);
4174
Curl_llist_destroy(old_conn->send_pipe, NULL);
4175
Curl_llist_destroy(old_conn->recv_pipe, NULL);
4176
Curl_llist_destroy(old_conn->pend_pipe, NULL);
4177
Curl_safefree(old_conn->master_buffer);
3439
* CreateConnection() sets up a new connectdata struct, or re-uses an already
4181
* create_conn() sets up a new connectdata struct, or re-uses an already
3440
4182
* existing one, and resolves host name.
3442
4184
* if this function returns CURLE_OK and *async is set to TRUE, the resolve
3725
4470
conn->bits.tunnel_proxy = TRUE;
3727
4472
/*************************************************************
3728
* Take care of user and password authentication stuff
4473
* Figure out the remote port number and fix it in the URL
3729
4474
*************************************************************/
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);
4475
result = parse_remote_port(data, conn);
4476
if(result != CURLE_OK)
3813
4479
/*************************************************************
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.
4480
* Check for an overridden user name and password, then set it
3824
4482
*************************************************************/
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;
4483
override_userpass(data, conn, user, passwd);
4484
result = set_userpass(conn, user, passwd);
4485
if(result != CURLE_OK)
3937
4488
/*************************************************************
3938
4489
* Check the current list of connections to see if we can
3974
4527
* just allocated before we can move along and use the previously
3975
4528
* 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! */
4530
reuse_conn(conn, conn_temp);
4531
free(conn); /* we don't need this anymore */
4038
4534
infof(data, "Re-using existing connection! (#%ld) with host %s\n",
4039
4535
conn->connectindex,
4040
4536
conn->proxy.name?conn->proxy.dispname:conn->host.dispname);
4537
/* copy this IP address to the common buffer for the easy handle so that
4538
the address can actually survice the removal of this connection. strcpy
4539
is safe since the target buffer is big enough to hold the largest
4540
possible IP address */
4541
strcpy(data->info.ip, conn->ip_addr_str);
4054
4559
/* Continue connectdata initialization here. */
4058
4562
* Inherit the proper values from the urldata struct AFTER we have arranged
4059
* the persistent connection stuff */
4563
* the persistent connection stuff
4060
4565
conn->fread_func = data->set.fread_func;
4061
4566
conn->fread_in = data->set.in;
4062
4567
conn->seek_func = data->set.seek_func;
4063
4568
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 */
4570
/*************************************************************
4571
* Resolve the address of the server or proxy
4572
*************************************************************/
4573
result = resolve_server(data, conn, addr, async);
4230
/* SetupConnection() is called after the name resolve initiated in
4231
* CreateConnection() is all done.
4578
/* setup_conn() is called after the name resolve initiated in
4579
* create_conn() is all done.
4233
4581
* NOTE: the argument 'hostaddr' is NULL when this function is called for a
4234
4582
* re-used connection.
4236
* conn->data MUST already have been setup fine (in CreateConnection)
4584
* 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)
4587
static CURLcode setup_conn(struct connectdata *conn,
4588
struct Curl_dns_entry *hostaddr,
4589
bool *protocol_done)
4243
4591
CURLcode result=CURLE_OK;
4244
4592
struct SessionHandle *data = conn->data;