Newer
Older
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://curl.haxx.se/docs/copyright.html.
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h> /* <netinet/tcp.h> may need it */
#endif
Daniel Stenberg
committed
#ifdef HAVE_SYS_UN_H
#include <sys/un.h> /* for sockaddr_un */
#endif
#ifdef HAVE_NETINET_TCP_H
#include <netinet/tcp.h> /* for TCP_NODELAY */
#ifdef HAVE_SYS_IOCTL_H
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#if (defined(HAVE_IOCTL_FIONBIO) && defined(NETWARE))
#undef in_addr_t
#define in_addr_t unsigned long
#endif
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
#include "urldata.h"
#include "sendf.h"
#include "connect.h"
#include "url.h" /* for Curl_safefree() */
Daniel Stenberg
committed
#include "multiif.h"
Daniel Stenberg
committed
#include "sockaddr.h" /* required for Curl_sockaddr_storage */
#include "inet_ntop.h"
Daniel Stenberg
committed
#include "sslgen.h" /* for Curl_ssl_check_cxn() */
#include "progress.h"
/* The last #include file should be: */
#include "memdebug.h"
#ifdef __SYMBIAN32__
/* This isn't actually supported under Symbian OS */
#undef SO_NOSIGPIPE
#endif
static bool verifyconnect(curl_socket_t sockfd, int *error);
Daniel Stenberg
committed
Daniel Stenberg
committed
singleipconnect(struct connectdata *conn,
const Curl_addrinfo *ai, /* start connecting to this */
Daniel Stenberg
committed
long timeout_ms,
curl_socket_t *sock,
Daniel Stenberg
committed
bool *connected);
Daniel Stenberg
committed
/*
* Curl_timeleft() returns the amount of milliseconds left allowed for the
* transfer/connection. If the value is negative, the timeout time has already
* elapsed.
*
* The start time is stored in progress.t_startsingle - as set with
* Curl_pgrsTime(..., TIMER_STARTSINGLE);
*
Daniel Stenberg
committed
* If 'nowp' is non-NULL, it points to the current time.
* 'duringconnect' is FALSE if not during a connect, as then of course the
* connect timeout is not taken into account!
Daniel Stenberg
committed
*/
long Curl_timeleft(struct SessionHandle *data,
Daniel Stenberg
committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
struct timeval *nowp,
bool duringconnect)
{
int timeout_set = 0;
long timeout_ms = duringconnect?DEFAULT_CONNECT_TIMEOUT:0;
struct timeval now;
/* if a timeout is set, use the most restrictive one */
if(data->set.timeout > 0)
timeout_set |= 1;
if(duringconnect && (data->set.connecttimeout > 0))
timeout_set |= 2;
switch (timeout_set) {
case 1:
timeout_ms = data->set.timeout;
break;
case 2:
timeout_ms = data->set.connecttimeout;
break;
case 3:
if(data->set.timeout < data->set.connecttimeout)
timeout_ms = data->set.timeout;
else
timeout_ms = data->set.connecttimeout;
break;
default:
/* use the default */
if(!duringconnect)
/* if we're not during connect, there's no default timeout so if we're
at zero we better just return zero and not make it a negative number
by the math below */
return 0;
break;
}
if(!nowp) {
now = Curl_tvnow();
nowp = &now;
}
Daniel Stenberg
committed
timeout_ms -= Curl_tvdiff(*nowp, data->progress.t_startsingle);
if(!timeout_ms)
/* avoid returning 0 as that means no timeout! */
return -1;
Daniel Stenberg
committed
return timeout_ms;
}
* waitconnect() waits for a TCP connect on the given socket for the specified
* number if milliseconds. It returns:
#define WAITCONN_CONNECTED 0
#define WAITCONN_SELECT_ERROR -1
#define WAITCONN_TIMEOUT 1
#define WAITCONN_FDSET_ERROR 2
#define WAITCONN_ABORTED 3
int waitconnect(struct connectdata *conn,
curl_socket_t sockfd, /* socket */
/* Call this function once now, and ignore the results. We do this to
"clear" the error state on the socket so that we can later read it
reliably. This is reported necessary on the MPE/iX operating system. */
/* now select() until we get connect or timeout */
rc = Curl_socket_ready(CURL_SOCKET_BAD, sockfd, timeout_msec>1000?
1000:timeout_msec);
if(Curl_pgrsUpdate(conn))
return WAITCONN_ABORTED;
if(-1 == rc)
/* error, no connect here, try next */
Loading full blame...