Newer
Older
Daniel Stenberg
committed
*connected = TRUE; /* this is a true connect */
infof(data, "connected\n");
#ifdef ENABLE_IPV6
conn->bits.ipv6 = (addr.family == AF_INET6)?TRUE:FALSE;
#endif
*sockp = sockfd;
return CURLE_OK;
Daniel Stenberg
committed
}
else if(WAITCONN_TIMEOUT == rc)
infof(data, "Timeout\n");
Daniel Stenberg
committed
else {
data->state.os_errno = error;
Daniel Stenberg
committed
infof(data, "%s\n", Curl_strerror(conn, error));
Daniel Stenberg
committed
}
Daniel Stenberg
committed
/* connect failed or timed out */
Daniel Stenberg
committed
return CURLE_OK;
Daniel Stenberg
committed
}
/*
* TCP connect to the given host with timeout, proxy or remote doesn't matter.
* There might be more than one IP address to try out. Fill in the passed
* pointer with the connected socket.
*/
CURLcode Curl_connecthost(struct connectdata *conn, /* context */
const struct Curl_dns_entry *remotehost,
curl_socket_t *sockconn, /* the connected socket */
Daniel Stenberg
committed
Curl_addrinfo **addr, /* the one we used */
bool *connected) /* really connected? */
{
struct SessionHandle *data = conn->data;
Daniel Stenberg
committed
curl_socket_t sockfd = CURL_SOCKET_BAD;
Daniel Stenberg
committed
Curl_addrinfo *ai;
Curl_addrinfo *curr_addr;
struct timeval after;
struct timeval before = Curl_tvnow();
/*************************************************************
* Figure out what maximum time we have left
*************************************************************/
Daniel Stenberg
committed
DEBUGASSERT(sockconn);
Daniel Stenberg
committed
*connected = FALSE; /* default to not connected */
Daniel Stenberg
committed
/* get the timeout left */
timeout_ms = Curl_timeleft(data, &before, TRUE);
Daniel Stenberg
committed
if(timeout_ms < 0) {
/* a precaution, no need to continue if time already is up */
failf(data, "Connection time-out");
return CURLE_OPERATION_TIMEDOUT;
conn->num_addr = Curl_num_addresses(remotehost->addr);
Daniel Stenberg
committed
Daniel Stenberg
committed
ai = remotehost->addr;
Daniel Stenberg
committed
/* Below is the loop that attempts to connect to all IP-addresses we
Daniel Stenberg
committed
* know for the given host. One by one until one IP succeeds.
Daniel Stenberg
committed
*/
Daniel Stenberg
committed
Daniel Stenberg
committed
* Connecting with a Curl_addrinfo chain
for(curr_addr = ai; curr_addr; curr_addr = curr_addr->ai_next) {
Daniel Stenberg
committed
/* Max time for the next address */
conn->timeoutms_per_addr = curr_addr->ai_next == NULL ?
timeout_ms : timeout_ms / 2;
/* start connecting to the IP curr_addr points to */
res = singleipconnect(conn, curr_addr,
/* don't hang when doing multi */
(data->state.used_interface == Curl_if_multi)?0:
conn->timeoutms_per_addr, &sockfd, connected);
if(res)
return res;
Daniel Stenberg
committed
Daniel Stenberg
committed
if(sockfd != CURL_SOCKET_BAD)
Daniel Stenberg
committed
break;
/* get a new timeout for next attempt */
after = Curl_tvnow();
timeout_ms -= Curl_tvdiff(after, before);
if(timeout_ms < 0) {
failf(data, "connect() timed out!");
return CURLE_OPERATION_TIMEDOUT;
before = after;
Daniel Stenberg
committed
} /* end of connect-to-each-address loop */
*sockconn = sockfd; /* the socket descriptor we've connected */
Daniel Stenberg
committed
if(sockfd == CURL_SOCKET_BAD) {
failf(data, "couldn't connect to %s at %s:%d",
conn->bits.proxy?"proxy":"host",
conn->bits.proxy?conn->proxy.name:conn->host.name, conn->port);
return CURLE_COULDNT_CONNECT;
}
/* leave the socket in non-blocking mode */
/* store the address we use */
Daniel Stenberg
committed
if(addr)
*addr = curr_addr;
data->info.numconnects++; /* to track the number of connections made */
Daniel Stenberg
committed
/*
* Used to extract socket and connectdata struct for the most recent
* transfer on the given SessionHandle.
*
* The returned socket will be CURL_SOCKET_BAD in case of failure!
Daniel Stenberg
committed
*/
curl_socket_t Curl_getconnectinfo(struct SessionHandle *data,
struct connectdata **connp)
Daniel Stenberg
committed
{
curl_socket_t sockfd;
DEBUGASSERT(data);
Daniel Stenberg
committed
if((data->state.lastconnect != -1) &&
(data->state.connc->connects[data->state.lastconnect] != NULL)) {
struct connectdata *c =
data->state.connc->connects[data->state.lastconnect];
if(connp)
/* only store this if the caller cares for it */
*connp = c;
sockfd = c->sock[FIRSTSOCKET];
Daniel Stenberg
committed
/* we have a socket connected, let's determine if the server shut down */
/* determine if ssl */
if(c->ssl[FIRSTSOCKET].use) {
/* use the SSL context */
if(!Curl_ssl_check_cxn(c))
return CURL_SOCKET_BAD; /* FIN received */
Daniel Stenberg
committed
}
/* Minix 3.1 doesn't support any flags on recv; just assume socket is OK */
#ifdef MSG_PEEK
else {
/* use the socket */
char buf;
if(recv((RECV_TYPE_ARG1)c->sock[FIRSTSOCKET], (RECV_TYPE_ARG2)&buf,
(RECV_TYPE_ARG3)1, (RECV_TYPE_ARG4)MSG_PEEK) == 0) {
return CURL_SOCKET_BAD; /* FIN received */
Daniel Stenberg
committed
}
}
#endif
}
else
return CURL_SOCKET_BAD;
Daniel Stenberg
committed
Daniel Stenberg
committed
}
/*
* Close a socket.
*
* 'conn' can be NULL, beware!
*/
int Curl_closesocket(struct connectdata *conn,
curl_socket_t sock)
{
if((sock == conn->sock[SECONDARYSOCKET]) &&
conn->sock_accepted[SECONDARYSOCKET])
/* if this socket matches the second socket, and that was created with
accept, then we MUST NOT call the callback but clear the accepted
status */
conn->sock_accepted[SECONDARYSOCKET] = FALSE;
else
return conn->fclosesocket(conn->closesocket_client, sock);
}
return sclose(sock);
/*
* Create a socket based on info from 'conn' and 'ai'.
*
* 'addr' should be a pointer to the correct struct to get data back, or NULL.
* 'sockfd' must be a pointer to a socket descriptor.
*
* If the open socket callback is set, used that!
*
*/
CURLcode Curl_socket(struct connectdata *conn,
const Curl_addrinfo *ai,
struct Curl_sockaddr_ex *addr,
curl_socket_t *sockfd)
{
struct SessionHandle *data = conn->data;
struct Curl_sockaddr_ex dummy;
if(!addr)
/* if the caller doesn't want info back, use a local temp copy */
addr = &dummy;
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
/*
* The Curl_sockaddr_ex structure is basically libcurl's external API
* curl_sockaddr structure with enough space available to directly hold
* any protocol-specific address structures. The variable declared here
* will be used to pass / receive data to/from the fopensocket callback
* if this has been set, before that, it is initialized from parameters.
*/
addr->family = ai->ai_family;
addr->socktype = conn->socktype;
addr->protocol = conn->socktype==SOCK_DGRAM?IPPROTO_UDP:ai->ai_protocol;
addr->addrlen = ai->ai_addrlen;
if(addr->addrlen > sizeof(struct Curl_sockaddr_storage))
addr->addrlen = sizeof(struct Curl_sockaddr_storage);
memcpy(&addr->sa_addr, ai->ai_addr, addr->addrlen);
if(data->set.fopensocket)
/*
* If the opensocket callback is set, all the destination address
* information is passed to the callback. Depending on this information the
* callback may opt to abort the connection, this is indicated returning
* CURL_SOCKET_BAD; otherwise it will return a not-connected socket. When
* the callback returns a valid socket the destination address information
* might have been changed and this 'new' address will actually be used
* here to connect.
*/
*sockfd = data->set.fopensocket(data->set.opensocket_client,
CURLSOCKTYPE_IPCXN,
(struct curl_sockaddr *)addr);
else
/* opensocket callback not set, so simply create the socket now */
*sockfd = socket(addr->family, addr->socktype, addr->protocol);
if(*sockfd == CURL_SOCKET_BAD)
/* no socket, no connection */
return CURLE_COULDNT_CONNECT;
#if defined(ENABLE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
if(conn->scope && (addr->family == AF_INET6)) {
struct sockaddr_in6 * const sa6 = (void *)&addr->sa_addr;
sa6->sin6_scope_id = conn->scope;
#endif
return CURLE_OK;
}