From f737e94164fd33cbb03a559ff51d4b4db59902ea Mon Sep 17 00:00:00 2001 From: Yang Tse Date: Mon, 7 Jun 2010 16:53:31 +0200 Subject: Curl_updateconninfo() error handling fix --- lib/connect.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++------------- lib/connect.h | 2 +- lib/url.c | 2 +- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/lib/connect.c b/lib/connect.c index 207956693..51d117389 100644 --- a/lib/connect.c +++ b/lib/connect.c @@ -524,9 +524,10 @@ static bool trynextip(struct connectdata *conn, } /* retrieves ip address and port from a sockaddr structure */ -static void getaddressinfo(struct sockaddr* sa, char* addr, +static bool getaddressinfo(struct sockaddr* sa, char* addr, long* port) { + unsigned short us_port; struct sockaddr_in* si = NULL; #ifdef ENABLE_IPV6 struct sockaddr_in6* si6 = NULL; @@ -535,36 +536,71 @@ static void getaddressinfo(struct sockaddr* sa, char* addr, switch (sa->sa_family) { case AF_INET: si = (struct sockaddr_in*) sa; - Curl_inet_ntop(sa->sa_family, &(si->sin_addr), addr, MAX_IPADR_LEN); - *port = ntohs(si->sin_port); + if(Curl_inet_ntop(sa->sa_family, &si->sin_addr, + addr, MAX_IPADR_LEN) != 0) + return FALSE; + us_port = ntohs(si->sin_port); + *port = us_port; break; #ifdef ENABLE_IPV6 case AF_INET6: si6 = (struct sockaddr_in6*)sa; - Curl_inet_ntop(sa->sa_family, &(si6->sin6_addr), addr, MAX_IPADR_LEN); - *port = ntohs(si6->sin6_port); + if(Curl_inet_ntop(sa->sa_family, &si6->sin6_addr, + addr, MAX_IPADR_LEN) != 0) + return FALSE; + us_port = ntohs(si6->sin6_port); + *port = us_port; break; #endif default: addr[0] = '\0'; *port = 0; } + return TRUE; } /* retrieves the start/end point information of a socket of an established connection */ -void Curl_updateconninfo(curl_socket_t sockfd, struct PureInfo* info) +void Curl_updateconninfo(struct connectdata *conn, curl_socket_t sockfd) { + int error; + curl_socklen_t len; struct Curl_sockaddr_storage ssrem; struct Curl_sockaddr_storage ssloc; + struct SessionHandle *data = conn->data; + struct PureInfo *info = &conn->data->info; + + len = sizeof(struct Curl_sockaddr_storage); + if(getpeername(sockfd, (struct sockaddr*) &ssrem, &len)) { + error = SOCKERRNO; + failf(data, "getpeername() failed with errno %d: %s", + error, Curl_strerror(conn, error)); + return; + } - curl_socklen_t len = sizeof(struct Curl_sockaddr_storage); + len = sizeof(struct Curl_sockaddr_storage); + if(getsockname(sockfd, (struct sockaddr*) &ssloc, &len)) { + error = SOCKERRNO; + failf(data, "getsockname() failed with errno %d: %s", + error, Curl_strerror(conn, error)); + return; + } - getpeername(sockfd, (struct sockaddr*) &ssrem, &len); - getsockname(sockfd, (struct sockaddr*) &ssloc, &len); + if(!getaddressinfo((struct sockaddr*)&ssrem, + info->ip, &info->port)) { + error = ERRNO; + failf(data, "ssrem inet_ntop() failed with errno %d: %s", + error, Curl_strerror(conn, error)); + return; + } - getaddressinfo((struct sockaddr*)&ssrem, info->ip, &info->port); - getaddressinfo((struct sockaddr*)&ssloc, info->localip, &info->localport); + if(!getaddressinfo((struct sockaddr*)&ssloc, + info->localip, &info->localport)) { + error = ERRNO; + failf(data, "ssloc inet_ntop() failed with errno %d: %s", + error, Curl_strerror(conn, error)); + return; + } } /* @@ -621,7 +657,7 @@ CURLcode Curl_is_connected(struct connectdata *conn, *connected = TRUE; Curl_pgrsTime(data, TIMER_CONNECT); /* connect done */ Curl_verboseconnect(conn); - Curl_updateconninfo(sockfd, &(data->info)); + Curl_updateconninfo(conn, sockfd); return CURLE_OK; } @@ -912,7 +948,7 @@ singleipconnect(struct connectdata *conn, /* we are connected, awesome! */ *connected = TRUE; /* this is a true connect */ infof(data, "connected\n"); - Curl_updateconninfo(sockfd, &(data->info)); + Curl_updateconninfo(conn, sockfd); return sockfd; } else if(WAITCONN_TIMEOUT == rc) diff --git a/lib/connect.h b/lib/connect.h index b365f7d0c..57b1c2f2d 100644 --- a/lib/connect.h +++ b/lib/connect.h @@ -68,6 +68,6 @@ void Curl_sndbufset(curl_socket_t sockfd); #define Curl_sndbufset(y) #endif -void Curl_updateconninfo(curl_socket_t sockfd, struct PureInfo* info); +void Curl_updateconninfo(struct connectdata *conn, curl_socket_t sockfd); #endif diff --git a/lib/url.c b/lib/url.c index 8a59be2d4..897ff2c12 100644 --- a/lib/url.c +++ b/lib/url.c @@ -5031,7 +5031,7 @@ static CURLcode setup_conn(struct connectdata *conn, conn->bits.tcpconnect = TRUE; *protocol_done = TRUE; Curl_verboseconnect(conn); - Curl_updateconninfo(conn->sock[FIRSTSOCKET], &data->info); + Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]); } /* Stop the loop now */ break; -- cgit v1.2.3