~ubuntu-branches/ubuntu/lucid/curl/lucid-201101212007

« back to all changes in this revision

Viewing changes to lib/connect.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-02-08 11:20:41 UTC
  • mto: (3.1.1 lenny) (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 26.
  • Revision ID: james.westby@ubuntu.com-20080208112041-hed7sb5r6ghmjf8v
Tags: upstream-7.18.0
ImportĀ upstreamĀ versionĀ 7.18.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: connect.c,v 1.184 2007-10-17 00:44:48 yangtse Exp $
 
21
 * $Id: connect.c,v 1.186 2008-01-08 01:05:50 yangtse Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
116
116
  int flags;
117
117
 
118
118
  flags = fcntl(sockfd, F_GETFL, 0);
119
 
  if (FALSE != nonblock)
 
119
  if(FALSE != nonblock)
120
120
    return fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
121
121
  else
122
122
    return fcntl(sockfd, F_SETFL, flags & (~O_NONBLOCK));
165
165
#define SETBLOCK 6
166
166
#endif
167
167
 
168
 
#if (SETBLOCK == 0)
 
168
#if(SETBLOCK == 0)
169
169
#error "no non-blocking method was found/used/set"
170
170
#endif
171
171
}
233
233
  /*************************************************************
234
234
   * Select device to bind socket to
235
235
   *************************************************************/
236
 
  if (dev && (strlen(dev)<255) ) {
 
236
  if(dev && (strlen(dev)<255) ) {
237
237
    struct Curl_dns_entry *h=NULL;
238
238
    char myhost[256] = "";
239
239
    in_addr_t in;
303
303
     * interfaces to go out the external interface.
304
304
     *
305
305
     */
306
 
    if (was_iface) {
 
306
    if(was_iface) {
307
307
      /* Only bind to the interface when specified as interface, not just as a
308
308
       * hostname or ip address.
309
309
       */
310
 
      if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
 
310
      if(setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
311
311
                     dev, strlen(dev)+1) != 0) {
312
312
        /* printf("Failed to BINDTODEVICE, socket: %d  device: %s error: %s\n",
313
313
           sockfd, dev, Curl_strerror(SOCKERRNO)); */
323
323
#ifdef ENABLE_IPV6
324
324
    in6 = Curl_inet_pton (AF_INET6, myhost, (void *)&ipv6_addr);
325
325
#endif
326
 
    if (CURL_INADDR_NONE == in && -1 == in6) {
 
326
    if(CURL_INADDR_NONE == in && -1 == in6) {
327
327
      failf(data,"couldn't find my own IP address (%s)", myhost);
328
328
      return CURLE_INTERFACE_FAILED;
329
329
    } /* end of inet_addr */
330
330
 
331
 
    if ( h ) {
 
331
    if( h ) {
332
332
      Curl_addrinfo *addr = h->addr;
333
333
      sock = addr->ai_addr;
334
334
      socksize = addr->ai_addrlen;
433
433
 
434
434
#endif
435
435
 
436
 
  if (0 != getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&err, &errSize))
 
436
  if(0 != getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&err, &errSize))
437
437
    err = SOCKERRNO;
438
438
#ifdef _WIN32_WCE
439
439
  /* Old WinCE versions don't support SO_ERROR */
440
 
  if (WSAENOPROTOOPT == err) {
 
440
  if(WSAENOPROTOOPT == err) {
441
441
    SET_SOCKERRNO(0);
442
442
    err = 0;
443
443
  }
444
444
#endif
445
445
#ifdef __minix
446
446
  /* Minix 3.1.x doesn't support getsockopt on UDP sockets */
447
 
  if (EBADIOCTL == err) {
 
447
  if(EBADIOCTL == err) {
448
448
    SET_SOCKERRNO(0);
449
449
    err = 0;
450
450
  }
451
451
#endif
452
 
  if ((0 == err) || (EISCONN == err))
 
452
  if((0 == err) || (EISCONN == err))
453
453
    /* we are connected, awesome! */
454
454
    rc = TRUE;
455
455
  else
456
456
    /* This wasn't a successful connect */
457
457
    rc = FALSE;
458
 
  if (error)
 
458
  if(error)
459
459
    *error = err;
460
460
#else
461
461
  (void)sockfd;
462
 
  if (error)
 
462
  if(error)
463
463
    *error = SOCKERRNO;
464
464
#endif
465
465
  return rc;
504
504
  /* try the next address */
505
505
  ai = conn->ip_addr->ai_next;
506
506
 
507
 
  while (ai) {
 
507
  while(ai) {
508
508
    sockfd = singleipconnect(conn, ai, 0L, connected);
509
509
    if(sockfd != CURL_SOCKET_BAD) {
510
510
      /* store the new socket descriptor */
545
545
 
546
546
  /* subtract the most strict timeout of the ones */
547
547
  if(data->set.timeout && data->set.connecttimeout) {
548
 
    if (data->set.timeout < data->set.connecttimeout)
 
548
    if(data->set.timeout < data->set.connecttimeout)
549
549
      allow_total = allow = data->set.timeout;
550
550
    else
551
551
      allow = data->set.connecttimeout;
576
576
 
577
577
  if(WAITCONN_CONNECTED == rc) {
578
578
    int error;
579
 
    if (verifyconnect(sockfd, &error)) {
 
579
    if(verifyconnect(sockfd, &error)) {
580
580
      /* we are connected, awesome! */
581
581
      *connected = TRUE;
582
582
      return CURLE_OK;
594
594
    int error = 0;
595
595
 
596
596
    /* nope, not connected  */
597
 
    if (WAITCONN_FDSET_ERROR == rc) {
 
597
    if(WAITCONN_FDSET_ERROR == rc) {
598
598
      (void)verifyconnect(sockfd, &error);
599
599
      data->state.os_errno = error;
600
600
      infof(data, "%s\n",Curl_strerror(conn,error));
628
628
 
629
629
#ifdef HAVE_GETPROTOBYNAME
630
630
  struct protoent *pe = getprotobyname("tcp");
631
 
  if (pe)
 
631
  if(pe)
632
632
    proto = pe->p_proto;
633
633
#endif
634
634
 
694
694
  addr->protocol=ai->ai_protocol;
695
695
  addr->addrlen =
696
696
    (ai->ai_addrlen < (socklen_t)sizeof(struct Curl_sockaddr_storage)) ?
697
 
     ai->ai_addrlen : (socklen_t)sizeof(struct Curl_sockaddr_storage);
 
697
     (unsigned int)ai->ai_addrlen : sizeof(struct Curl_sockaddr_storage);
698
698
  memcpy(&addr->addr, ai->ai_addr, addr->addrlen);
699
699
 
700
700
  /* If set, use opensocket callback to get the socket */
703
703
                                   CURLSOCKTYPE_IPCXN, addr);
704
704
  else
705
705
    sockfd = socket(addr->family, addr->socktype, addr->protocol);
706
 
  if (sockfd == CURL_SOCKET_BAD)
 
706
  if(sockfd == CURL_SOCKET_BAD)
707
707
    return CURL_SOCKET_BAD;
708
708
 
709
709
  *connected = FALSE; /* default is not connected */
728
728
    error = data->set.fsockopt(data->set.sockopt_client,
729
729
                               sockfd,
730
730
                               CURLSOCKTYPE_IPCXN);
731
 
    if (error) {
 
731
    if(error) {
732
732
      sclose(sockfd); /* close the socket and bail out */
733
733
      return CURL_SOCKET_BAD;
734
734
    }
836
836
 
837
837
  /* if a timeout is set, use the most restrictive one */
838
838
 
839
 
  if (data->set.timeout > 0)
 
839
  if(data->set.timeout > 0)
840
840
    timeout_set += 1;
841
 
  if (data->set.connecttimeout > 0)
 
841
  if(data->set.connecttimeout > 0)
842
842
    timeout_set += 2;
843
843
 
844
844
  switch (timeout_set) {
849
849
    timeout_ms = data->set.connecttimeout;
850
850
    break;
851
851
  case 3:
852
 
    if (data->set.timeout < data->set.connecttimeout)
 
852
    if(data->set.timeout < data->set.connecttimeout)
853
853
      timeout_ms = data->set.timeout;
854
854
    else
855
855
      timeout_ms = data->set.connecttimeout;
859
859
    break;
860
860
  }
861
861
 
862
 
  if (timeout_set > 0) {
 
862
  if(timeout_set > 0) {
863
863
    /* if a timeout was already set, substract elapsed time */
864
864
    timeout_ms -= Curl_tvdiff(before, data->progress.t_startsingle);
865
865
    if(timeout_ms < 0) {
906
906
    before = after;
907
907
  }  /* end of connect-to-each-address loop */
908
908
 
909
 
  if (sockfd == CURL_SOCKET_BAD) {
 
909
  if(sockfd == CURL_SOCKET_BAD) {
910
910
    /* no good connect was made */
911
911
    *sockconn = CURL_SOCKET_BAD;
912
912
    failf(data, "couldn't connect to host");