~ubuntu-branches/ubuntu/saucy/curl/saucy-201307251546

« back to all changes in this revision

Viewing changes to lib/connect.c

  • Committer: Bazaar Package Importer
  • Author(s): Ramakrishnan Muthukrishnan
  • Date: 2010-10-18 11:13:17 UTC
  • mto: (3.6.1 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20101018111317-9rkas34ecwtq0upn
Tags: upstream-7.21.2
ImportĀ upstreamĀ versionĀ 7.21.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
174
174
 
175
175
  /* substract elapsed time */
176
176
  timeout_ms -= Curl_tvdiff(*nowp, data->progress.t_startsingle);
 
177
  if(!timeout_ms)
 
178
    /* avoid returning 0 as that means no timeout! */
 
179
    return -1;
177
180
 
178
181
  return timeout_ms;
179
182
}
627
630
  CURLcode code = CURLE_OK;
628
631
  curl_socket_t sockfd = conn->sock[sockindex];
629
632
  long allow = DEFAULT_CONNECT_TIMEOUT;
 
633
  int error = 0;
630
634
 
631
635
  DEBUGASSERT(sockindex >= FIRSTSOCKET && sockindex <= SECONDARYSOCKET);
632
636
 
634
638
 
635
639
  if(conn->bits.tcpconnect) {
636
640
    /* we are connected already! */
637
 
    long allow_total = 0;
638
 
 
639
 
    /* subtract the most strict timeout of the ones */
640
 
    if(data->set.timeout)
641
 
      allow_total = data->set.timeout;
642
 
 
643
 
    Curl_expire(data, allow_total);
644
641
    *connected = TRUE;
645
642
    return CURLE_OK;
646
643
  }
654
651
    return CURLE_OPERATION_TIMEDOUT;
655
652
  }
656
653
 
657
 
  Curl_expire(data, allow);
658
 
 
659
654
  /* check for connect without timeout as we want to return immediately */
660
655
  rc = waitconnect(conn, sockfd, 0);
 
656
  if(WAITCONN_TIMEOUT == rc)
 
657
    /* not an error, but also no connection yet */
 
658
    return code;
661
659
 
662
660
  if(WAITCONN_CONNECTED == rc) {
663
 
    int error;
664
661
    if(verifyconnect(sockfd, &error)) {
665
662
      /* we are connected, awesome! */
666
663
      conn->bits.tcpconnect = TRUE;
672
669
      return CURLE_OK;
673
670
    }
674
671
    /* nope, not connected for real */
675
 
    data->state.os_errno = error;
676
 
    infof(data, "Connection failed\n");
677
 
    code = trynextip(conn, sockindex, connected);
678
 
    if(code)
679
 
      failf(data, "Failed connect to %s:%ld; %s",
680
 
            conn->host.name, conn->port, Curl_strerror(conn, error));
681
672
  }
682
 
  else if(WAITCONN_TIMEOUT != rc) {
683
 
    int error = 0;
684
 
 
 
673
  else {
685
674
    /* nope, not connected  */
686
675
    if(WAITCONN_FDSET_ERROR == rc) {
687
676
      (void)verifyconnect(sockfd, &error);
688
 
      data->state.os_errno = error;
689
 
      infof(data, "%s\n",Curl_strerror(conn,error));
 
677
      infof(data, "%s\n",Curl_strerror(conn, error));
690
678
    }
691
679
    else
692
680
      infof(data, "Connection failed\n");
693
 
 
694
 
    code = trynextip(conn, sockindex, connected);
695
 
 
696
 
    if(code) {
697
 
      error = SOCKERRNO;
698
 
      data->state.os_errno = error;
699
 
      failf(data, "Failed connect to %s:%ld; %s",
700
 
            conn->host.name, conn->port, Curl_strerror(conn, error));
701
 
    }
702
681
  }
 
682
 
703
683
  /*
704
 
   * If the connection failed here, we should attempt to connect to the "next
705
 
   * address" for the given host.
 
684
   * The connection failed here, we should attempt to connect to the "next
 
685
   * address" for the given host. But first remember the latest error.
706
686
   */
 
687
  if(error) {
 
688
    data->state.os_errno = error;
 
689
    SET_SOCKERRNO(error);
 
690
  }
 
691
 
 
692
  code = trynextip(conn, sockindex, connected);
 
693
 
 
694
  if(code) {
 
695
    error = SOCKERRNO;
 
696
    data->state.os_errno = error;
 
697
    failf(data, "Failed connect to %s:%ld; %s",
 
698
          conn->host.name, conn->port, Curl_strerror(conn, error));
 
699
  }
707
700
 
708
701
  return code;
709
702
}
1029
1022
    failf(data, "Connection time-out");
1030
1023
    return CURLE_OPERATION_TIMEDOUT;
1031
1024
  }
1032
 
  Curl_expire(data, timeout_ms);
1033
1025
 
1034
1026
  /* Max time for each address */
1035
1027
  num_addr = Curl_num_addresses(remotehost->addr);
1094
1086
 * Used to extract socket and connectdata struct for the most recent
1095
1087
 * transfer on the given SessionHandle.
1096
1088
 *
1097
 
 * The socket 'long' will be -1 in case of failure!
 
1089
 * The returned socket will be CURL_SOCKET_BAD in case of failure!
1098
1090
 */
1099
 
CURLcode Curl_getconnectinfo(struct SessionHandle *data,
1100
 
                             long *param_longp,
1101
 
                             struct connectdata **connp)
 
1091
curl_socket_t Curl_getconnectinfo(struct SessionHandle *data,
 
1092
                                  struct connectdata **connp)
1102
1093
{
 
1094
  curl_socket_t sockfd;
1103
1095
  if((data->state.lastconnect != -1) &&
1104
1096
     (data->state.connc->connects[data->state.lastconnect] != NULL)) {
1105
1097
    struct connectdata *c =
1107
1099
    if(connp)
1108
1100
      /* only store this if the caller cares for it */
1109
1101
      *connp = c;
1110
 
    *param_longp = c->sock[FIRSTSOCKET];
 
1102
    sockfd = c->sock[FIRSTSOCKET];
1111
1103
    /* we have a socket connected, let's determine if the server shut down */
1112
1104
    /* determine if ssl */
1113
1105
    if(c->ssl[FIRSTSOCKET].use) {
1114
1106
      /* use the SSL context */
1115
1107
      if(!Curl_ssl_check_cxn(c))
1116
 
        *param_longp = -1;   /* FIN received */
 
1108
        return CURL_SOCKET_BAD;   /* FIN received */
1117
1109
    }
1118
1110
/* Minix 3.1 doesn't support any flags on recv; just assume socket is OK */
1119
1111
#ifdef MSG_PEEK
1122
1114
      char buf;
1123
1115
      if(recv((RECV_TYPE_ARG1)c->sock[FIRSTSOCKET], (RECV_TYPE_ARG2)&buf,
1124
1116
              (RECV_TYPE_ARG3)1, (RECV_TYPE_ARG4)MSG_PEEK) == 0) {
1125
 
        *param_longp = -1;   /* FIN received */
 
1117
        return CURL_SOCKET_BAD;   /* FIN received */
1126
1118
      }
1127
1119
    }
1128
1120
#endif
1129
1121
  }
1130
1122
  else
1131
 
    *param_longp = -1;
 
1123
    return CURL_SOCKET_BAD;
1132
1124
 
1133
 
  return CURLE_OK;
 
1125
  return sockfd;
1134
1126
}