connect.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * $Id: connect.c,v 1.82 2004/03/17 12:46:45 bagder Exp $
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef WIN32
  25. /* headers for non-win32 */
  26. #include <sys/time.h>
  27. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.h>
  29. #endif
  30. #ifdef HAVE_SYS_SOCKET_H
  31. #include <sys/socket.h>
  32. #endif
  33. #include <sys/ioctl.h>
  34. #ifdef HAVE_UNISTD_H
  35. #include <unistd.h>
  36. #endif
  37. #ifdef HAVE_NETDB_H
  38. #include <netdb.h>
  39. #endif
  40. #ifdef HAVE_FCNTL_H
  41. #include <fcntl.h>
  42. #endif
  43. #ifdef HAVE_NETINET_IN_H
  44. #include <netinet/in.h>
  45. #endif
  46. #ifdef HAVE_ARPA_INET_H
  47. #include <arpa/inet.h>
  48. #endif
  49. #ifdef HAVE_STDLIB_H
  50. #include <stdlib.h> /* required for free() prototype, without it, this crashes
  51. on macos 68K */
  52. #endif
  53. #if (defined(HAVE_FIONBIO) && defined(__NOVELL_LIBC__))
  54. #include <sys/filio.h>
  55. #endif
  56. #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
  57. #undef in_addr_t
  58. #define in_addr_t unsigned long
  59. #endif
  60. #ifdef VMS
  61. #include <in.h>
  62. #include <inet.h>
  63. #endif
  64. #endif
  65. #include <stdio.h>
  66. #include <errno.h>
  67. #include <string.h>
  68. #ifndef TRUE
  69. #define TRUE 1
  70. #define FALSE 0
  71. #endif
  72. #ifdef WIN32
  73. #include <windows.h>
  74. #define EINPROGRESS WSAEINPROGRESS
  75. #define EWOULDBLOCK WSAEWOULDBLOCK
  76. #define EISCONN WSAEISCONN
  77. #define ENOTSOCK WSAENOTSOCK
  78. #define ECONNREFUSED WSAECONNREFUSED
  79. #endif
  80. #include "urldata.h"
  81. #include "sendf.h"
  82. #include "if2ip.h"
  83. #include "connect.h"
  84. /* The last #include file should be: */
  85. #ifdef CURLDEBUG
  86. #include "memdebug.h"
  87. #endif
  88. static bool verifyconnect(curl_socket_t sockfd);
  89. int Curl_ourerrno(void)
  90. {
  91. #ifdef WIN32
  92. return (int)GetLastError();
  93. #else
  94. return errno;
  95. #endif
  96. }
  97. /*************************************************************************
  98. * Curl_nonblock
  99. *
  100. * Description:
  101. * Set the socket to either blocking or non-blocking mode.
  102. */
  103. int Curl_nonblock(curl_socket_t sockfd, /* operate on this */
  104. int nonblock /* TRUE or FALSE */)
  105. {
  106. #undef SETBLOCK
  107. #ifdef HAVE_O_NONBLOCK
  108. /* most recent unix versions */
  109. int flags;
  110. flags = fcntl(sockfd, F_GETFL, 0);
  111. if (TRUE == nonblock)
  112. return fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
  113. else
  114. return fcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
  115. #define SETBLOCK 1
  116. #endif
  117. #ifdef HAVE_FIONBIO
  118. /* older unix versions */
  119. int flags;
  120. flags = nonblock;
  121. return ioctl(sockfd, FIONBIO, &flags);
  122. #define SETBLOCK 2
  123. #endif
  124. #ifdef HAVE_IOCTLSOCKET
  125. /* Windows? */
  126. int flags;
  127. flags = nonblock;
  128. return ioctlsocket(sockfd, FIONBIO, &flags);
  129. #define SETBLOCK 3
  130. #endif
  131. #ifdef HAVE_IOCTLSOCKET_CASE
  132. /* presumably for Amiga */
  133. return IoctlSocket(sockfd, FIONBIO, (long)nonblock);
  134. #define SETBLOCK 4
  135. #endif
  136. #ifdef HAVE_SO_NONBLOCK
  137. /* BeOS */
  138. long b = nonblock ? 1 : 0;
  139. return setsockopt(sockfd, SOL_SOCKET, SO_NONBLOCK, &b, sizeof(b));
  140. #define SETBLOCK 5
  141. #endif
  142. #ifdef HAVE_DISABLED_NONBLOCKING
  143. return 0; /* returns success */
  144. #define SETBLOCK 6
  145. #endif
  146. #ifndef SETBLOCK
  147. #error "no non-blocking method was found/used/set"
  148. #endif
  149. }
  150. /*
  151. * waitconnect() returns:
  152. * 0 fine connect
  153. * -1 select() error
  154. * 1 select() timeout
  155. * 2 select() returned with an error condition
  156. */
  157. static
  158. int waitconnect(curl_socket_t sockfd, /* socket */
  159. long timeout_msec)
  160. {
  161. fd_set fd;
  162. fd_set errfd;
  163. struct timeval interval;
  164. int rc;
  165. #ifdef mpeix
  166. /* Call this function once now, and ignore the results. We do this to
  167. "clear" the error state on the socket so that we can later read it
  168. reliably. This is reported necessary on the MPE/iX operating system. */
  169. verifyconnect(sockfd);
  170. #endif
  171. /* now select() until we get connect or timeout */
  172. FD_ZERO(&fd);
  173. FD_SET(sockfd, &fd);
  174. FD_ZERO(&errfd);
  175. FD_SET(sockfd, &errfd);
  176. interval.tv_sec = timeout_msec/1000;
  177. timeout_msec -= interval.tv_sec*1000;
  178. interval.tv_usec = timeout_msec*1000;
  179. rc = select(sockfd+1, NULL, &fd, &errfd, &interval);
  180. if(-1 == rc)
  181. /* error, no connect here, try next */
  182. return -1;
  183. else if(0 == rc)
  184. /* timeout, no connect today */
  185. return 1;
  186. if(FD_ISSET(sockfd, &errfd))
  187. /* error condition caught */
  188. return 2;
  189. /* we have a connect! */
  190. return 0;
  191. }
  192. static CURLcode bindlocal(struct connectdata *conn,
  193. curl_socket_t sockfd)
  194. {
  195. #ifdef HAVE_INET_NTOA
  196. bool bindworked = FALSE;
  197. struct SessionHandle *data = conn->data;
  198. /*************************************************************
  199. * Select device to bind socket to
  200. *************************************************************/
  201. if (strlen(data->set.device)<255) {
  202. struct Curl_dns_entry *h=NULL;
  203. size_t size;
  204. char myhost[256] = "";
  205. in_addr_t in;
  206. int rc;
  207. bool was_iface = FALSE;
  208. /* First check if the given name is an IP address */
  209. in=inet_addr(data->set.device);
  210. if((in == CURL_INADDR_NONE) &&
  211. Curl_if2ip(data->set.device, myhost, sizeof(myhost))) {
  212. /*
  213. * We now have the numerical IPv4-style x.y.z.w in the 'myhost' buffer
  214. */
  215. rc = Curl_resolv(conn, myhost, 0, &h);
  216. if(rc == 1)
  217. (void)Curl_wait_for_resolv(conn, &h);
  218. if(h)
  219. was_iface = TRUE;
  220. }
  221. if(!was_iface) {
  222. /*
  223. * This was not an interface, resolve the name as a host name
  224. * or IP number
  225. */
  226. rc = Curl_resolv(conn, data->set.device, 0, &h);
  227. if(rc == 1)
  228. (void)Curl_wait_for_resolv(conn, &h);
  229. if(h)
  230. /* we know data->set.device is shorter than the myhost array */
  231. strcpy(myhost, data->set.device);
  232. }
  233. if(! *myhost) {
  234. /* need to fix this
  235. h=Curl_gethost(data,
  236. getmyhost(*myhost,sizeof(myhost)),
  237. hostent_buf,
  238. sizeof(hostent_buf));
  239. */
  240. failf(data, "Couldn't bind to '%s'", data->set.device);
  241. return CURLE_HTTP_PORT_FAILED;
  242. }
  243. infof(data, "We bind local end to %s\n", myhost);
  244. #ifdef SO_BINDTODEVICE
  245. /* I am not sure any other OSs than Linux that provide this feature, and
  246. * at the least I cannot test. --Ben
  247. *
  248. * This feature allows one to tightly bind the local socket to a
  249. * particular interface. This will force even requests to other local
  250. * interfaces to go out the external interface.
  251. *
  252. */
  253. if (was_iface) {
  254. /* Only bind to the interface when specified as interface, not just as a
  255. * hostname or ip address.
  256. */
  257. if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
  258. data->set.device, strlen(data->set.device)+1) != 0) {
  259. /* printf("Failed to BINDTODEVICE, socket: %d device: %s error: %s\n",
  260. sockfd, data->set.device, strerror(errno)); */
  261. infof(data, "SO_BINDTODEVICE %s failed\n",
  262. data->set.device);
  263. /* This is typiclally "errno 1, error: Operation not permitted" if
  264. you're not running as root or another suitable privileged user */
  265. }
  266. }
  267. #endif
  268. in=inet_addr(myhost);
  269. if (CURL_INADDR_NONE != in) {
  270. if ( h ) {
  271. Curl_addrinfo *addr = h->addr;
  272. Curl_resolv_unlock(data, h);
  273. /* we don't need it anymore after this function has returned */
  274. #ifdef ENABLE_IPV6
  275. if( bind(sockfd, addr->ai_addr, addr->ai_addrlen) >= 0) {
  276. /* we succeeded to bind */
  277. struct sockaddr_in6 add;
  278. bindworked = TRUE;
  279. size = sizeof(add);
  280. if(getsockname(sockfd, (struct sockaddr *) &add,
  281. (socklen_t *)&size)<0) {
  282. failf(data, "getsockname() failed");
  283. return CURLE_HTTP_PORT_FAILED;
  284. }
  285. }
  286. #else
  287. {
  288. struct sockaddr_in sa;
  289. memset((char *)&sa, 0, sizeof(sa));
  290. memcpy((char *)&sa.sin_addr, addr->h_addr, addr->h_length);
  291. sa.sin_family = AF_INET;
  292. sa.sin_addr.s_addr = in;
  293. sa.sin_port = 0; /* get any port */
  294. if( bind(sockfd, (struct sockaddr *)&sa, sizeof(sa)) >= 0) {
  295. /* we succeeded to bind */
  296. struct sockaddr_in add;
  297. bindworked = TRUE;
  298. size = sizeof(add);
  299. if(getsockname(sockfd, (struct sockaddr *) &add,
  300. (socklen_t *)&size)<0) {
  301. failf(data, "getsockname() failed");
  302. return CURLE_HTTP_PORT_FAILED;
  303. }
  304. }
  305. }
  306. #endif
  307. if(!bindworked) {
  308. switch(errno) {
  309. case EBADF:
  310. failf(data, "Invalid descriptor: %d", errno);
  311. break;
  312. case EINVAL:
  313. failf(data, "Invalid request: %d", errno);
  314. break;
  315. case EACCES:
  316. failf(data, "Address is protected, user not superuser: %d", errno);
  317. break;
  318. case ENOTSOCK:
  319. failf(data,
  320. "Argument is a descriptor for a file, not a socket: %d",
  321. errno);
  322. break;
  323. case EFAULT:
  324. failf(data, "Inaccessable memory error: %d", errno);
  325. break;
  326. case ENAMETOOLONG:
  327. failf(data, "Address too long: %d", errno);
  328. break;
  329. case ENOMEM:
  330. failf(data, "Insufficient kernel memory was available: %d", errno);
  331. break;
  332. default:
  333. failf(data, "errno %d", errno);
  334. break;
  335. } /* end of switch(errno) */
  336. return CURLE_HTTP_PORT_FAILED;
  337. } /* end of else */
  338. } /* end of if h */
  339. else {
  340. failf(data,"could't find my own IP address (%s)", myhost);
  341. return CURLE_HTTP_PORT_FAILED;
  342. }
  343. } /* end of inet_addr */
  344. else {
  345. failf(data, "could't find my own IP address (%s)", myhost);
  346. return CURLE_HTTP_PORT_FAILED;
  347. }
  348. return CURLE_OK;
  349. } /* end of device selection support */
  350. #endif /* end of HAVE_INET_NTOA */
  351. return CURLE_HTTP_PORT_FAILED;
  352. }
  353. /*
  354. * verifyconnect() returns TRUE if the connect really has happened.
  355. */
  356. static bool verifyconnect(curl_socket_t sockfd)
  357. {
  358. #if defined(SO_ERROR) && !defined(WIN32)
  359. int err = 0;
  360. socklen_t errSize = sizeof(err);
  361. if( -1 == getsockopt(sockfd, SOL_SOCKET, SO_ERROR,
  362. (void *)&err, &errSize))
  363. err = Curl_ourerrno();
  364. if ((0 == err) || (EISCONN == err))
  365. /* we are connected, awesome! */
  366. return TRUE;
  367. /* This wasn't a successful connect */
  368. return FALSE;
  369. #else
  370. (void)sockfd;
  371. return TRUE;
  372. #endif
  373. }
  374. /*
  375. * Curl_is_connected() is used from the multi interface to check if the
  376. * firstsocket has connected.
  377. */
  378. CURLcode Curl_is_connected(struct connectdata *conn,
  379. curl_socket_t sockfd,
  380. bool *connected)
  381. {
  382. int rc;
  383. struct SessionHandle *data = conn->data;
  384. *connected = FALSE; /* a very negative world view is best */
  385. if(data->set.timeout || data->set.connecttimeout) {
  386. /* there is a timeout set */
  387. /* Evaluate in milliseconds how much time that has passed */
  388. long has_passed = Curl_tvdiff(Curl_tvnow(), data->progress.start);
  389. /* subtract the most strict timeout of the ones */
  390. if(data->set.timeout && data->set.connecttimeout) {
  391. if (data->set.timeout < data->set.connecttimeout)
  392. has_passed -= data->set.timeout*1000;
  393. else
  394. has_passed -= data->set.connecttimeout*1000;
  395. }
  396. else if(data->set.timeout)
  397. has_passed -= data->set.timeout*1000;
  398. else
  399. has_passed -= data->set.connecttimeout*1000;
  400. if(has_passed > 0 ) {
  401. /* time-out, bail out, go home */
  402. failf(data, "Connection time-out");
  403. return CURLE_OPERATION_TIMEOUTED;
  404. }
  405. }
  406. if(conn->bits.tcpconnect) {
  407. /* we are connected already! */
  408. *connected = TRUE;
  409. return CURLE_OK;
  410. }
  411. /* check for connect without timeout as we want to return immediately */
  412. rc = waitconnect(sockfd, 0);
  413. if(0 == rc) {
  414. if (verifyconnect(sockfd)) {
  415. /* we are connected, awesome! */
  416. *connected = TRUE;
  417. return CURLE_OK;
  418. }
  419. /* nope, not connected for real */
  420. failf(data, "Connection failed");
  421. return CURLE_COULDNT_CONNECT;
  422. }
  423. else if(1 != rc) {
  424. int error = Curl_ourerrno();
  425. failf(data, "Failed connect to %s:%d, errno: %d",
  426. conn->hostname, conn->port, error);
  427. return CURLE_COULDNT_CONNECT;
  428. }
  429. /*
  430. * If the connection phase is "done" here, we should attempt to connect
  431. * to the "next address" in the Curl_hostaddr structure that we resolved
  432. * before. But we don't have that struct around anymore and we can't just
  433. * keep a pointer since the cache might in fact have gotten pruned by the
  434. * time we want to read this... Alas, we don't do this yet.
  435. */
  436. return CURLE_OK;
  437. }
  438. /*
  439. * TCP connect to the given host with timeout, proxy or remote doesn't matter.
  440. * There might be more than one IP address to try out. Fill in the passed
  441. * pointer with the connected socket.
  442. */
  443. CURLcode Curl_connecthost(struct connectdata *conn, /* context */
  444. struct Curl_dns_entry *remotehost, /* use this one */
  445. int port, /* connect to this */
  446. curl_socket_t *sockconn, /* the connected socket */
  447. Curl_ipconnect **addr, /* the one we used */
  448. bool *connected) /* really connected? */
  449. {
  450. struct SessionHandle *data = conn->data;
  451. int rc;
  452. curl_socket_t sockfd= CURL_SOCKET_BAD;
  453. int aliasindex=0;
  454. char *hostname;
  455. struct timeval after;
  456. struct timeval before = Curl_tvnow();
  457. #ifdef ENABLE_IPV6
  458. struct addrinfo *ai;
  459. #endif
  460. /*************************************************************
  461. * Figure out what maximum time we have left
  462. *************************************************************/
  463. long timeout_ms=300000; /* milliseconds, default to five minutes */
  464. *connected = FALSE; /* default to not connected */
  465. if(data->set.timeout || data->set.connecttimeout) {
  466. double has_passed;
  467. /* Evaluate in milliseconds how much time that has passed */
  468. has_passed = Curl_tvdiff(Curl_tvnow(), data->progress.start);
  469. #ifndef min
  470. #define min(a, b) ((a) < (b) ? (a) : (b))
  471. #endif
  472. /* get the most strict timeout of the ones converted to milliseconds */
  473. if(data->set.timeout && data->set.connecttimeout) {
  474. if (data->set.timeout < data->set.connecttimeout)
  475. timeout_ms = data->set.timeout*1000;
  476. else
  477. timeout_ms = data->set.connecttimeout*1000;
  478. }
  479. else if(data->set.timeout)
  480. timeout_ms = data->set.timeout*1000;
  481. else
  482. timeout_ms = data->set.connecttimeout*1000;
  483. /* subtract the passed time */
  484. timeout_ms -= (long)has_passed;
  485. if(timeout_ms < 0) {
  486. /* a precaution, no need to continue if time already is up */
  487. failf(data, "Connection time-out");
  488. return CURLE_OPERATION_TIMEOUTED;
  489. }
  490. }
  491. hostname = data->change.proxy?conn->proxyhost:conn->hostname;
  492. infof(data, "About to connect() to %s port %d\n",
  493. hostname, port);
  494. #ifdef ENABLE_IPV6
  495. /*
  496. * Connecting with a getaddrinfo chain
  497. */
  498. for (ai = remotehost->addr; ai; ai = ai->ai_next, aliasindex++) {
  499. sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  500. if (sockfd == CURL_SOCKET_BAD)
  501. continue;
  502. #else
  503. /*
  504. * Connecting with old style IPv4-only support
  505. */
  506. /* This is the loop that attempts to connect to all IP-addresses we
  507. know for the given host. One by one. */
  508. for(rc=-1, aliasindex=0;
  509. rc && (struct in_addr *)remotehost->addr->h_addr_list[aliasindex];
  510. aliasindex++) {
  511. struct sockaddr_in serv_addr;
  512. /* create an IPv4 TCP socket */
  513. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  514. if(CURL_SOCKET_BAD == sockfd) {
  515. failf(data, "couldn't create socket");
  516. return CURLE_COULDNT_CONNECT; /* big time error */
  517. }
  518. /* nasty address work before connect can be made */
  519. memset((char *) &serv_addr, '\0', sizeof(serv_addr));
  520. memcpy((char *)&(serv_addr.sin_addr),
  521. (struct in_addr *)remotehost->addr->h_addr_list[aliasindex],
  522. sizeof(struct in_addr));
  523. serv_addr.sin_family = remotehost->addr->h_addrtype;
  524. serv_addr.sin_port = htons((unsigned short)port);
  525. #endif
  526. if(conn->data->set.device) {
  527. /* user selected to bind the outgoing socket to a specified "device"
  528. before doing connect */
  529. CURLcode res = bindlocal(conn, sockfd);
  530. if(res)
  531. return res;
  532. }
  533. /* set socket non-blocking */
  534. Curl_nonblock(sockfd, TRUE);
  535. /* do not use #ifdef within the function arguments below, as connect() is
  536. a defined macro on some platforms and some compilers don't like to mix
  537. #ifdefs with macro usage! (AmigaOS is one such platform) */
  538. #ifdef ENABLE_IPV6
  539. rc = connect(sockfd, ai->ai_addr, ai->ai_addrlen);
  540. #else
  541. rc = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  542. #endif
  543. if(-1 == rc) {
  544. int error=Curl_ourerrno();
  545. switch (error) {
  546. case EINPROGRESS:
  547. case EWOULDBLOCK:
  548. #if defined(EAGAIN) && EAGAIN != EWOULDBLOCK
  549. /* On some platforms EAGAIN and EWOULDBLOCK are the
  550. * same value, and on others they are different, hence
  551. * the odd #if
  552. */
  553. case EAGAIN:
  554. #endif
  555. /* asynchronous connect, wait for connect or timeout */
  556. if(data->state.used_interface == Curl_if_multi)
  557. /* don't hang when doing multi */
  558. timeout_ms = 0;
  559. rc = waitconnect(sockfd, timeout_ms);
  560. break;
  561. default:
  562. /* unknown error, fallthrough and try another address! */
  563. failf(data, "Failed to connect to %s IP number %d: %d",
  564. hostname, aliasindex+1, error);
  565. break;
  566. }
  567. }
  568. /* The '1 == rc' comes from the waitconnect(), and not from connect().
  569. We can be sure of this since connect() cannot return 1. */
  570. if((1 == rc) && (data->state.used_interface == Curl_if_multi)) {
  571. /* Timeout when running the multi interface, we return here with a
  572. CURLE_OK return code. */
  573. rc = 0;
  574. break;
  575. }
  576. if(0 == rc) {
  577. if (verifyconnect(sockfd)) {
  578. /* we are connected, awesome! */
  579. *connected = TRUE; /* this is a true connect */
  580. break;
  581. }
  582. /* nope, not connected for real */
  583. rc = -1;
  584. }
  585. /* connect failed or timed out */
  586. sclose(sockfd);
  587. sockfd = -1;
  588. /* get a new timeout for next attempt */
  589. after = Curl_tvnow();
  590. timeout_ms -= Curl_tvdiff(after, before);
  591. if(timeout_ms < 0) {
  592. failf(data, "connect() timed out!");
  593. return CURLE_OPERATION_TIMEOUTED;
  594. }
  595. before = after;
  596. }
  597. if (sockfd == CURL_SOCKET_BAD) {
  598. /* no good connect was made */
  599. *sockconn = -1;
  600. failf(data, "Connect failed");
  601. return CURLE_COULDNT_CONNECT;
  602. }
  603. /* leave the socket in non-blocking mode */
  604. /* store the address we use */
  605. if(addr) {
  606. #ifdef ENABLE_IPV6
  607. *addr = ai;
  608. #else
  609. *addr = (struct in_addr *)remotehost->addr->h_addr_list[aliasindex];
  610. #endif
  611. }
  612. /* allow NULL-pointers to get passed in */
  613. if(sockconn)
  614. *sockconn = sockfd; /* the socket descriptor we've connected */
  615. return CURLE_OK;
  616. }