mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-03-22 06:38:37 -05:00
Cleanups to sk_namelookup(). In particular, it now doesn't segfault
if you explicitly specify IPv6 and then try to look up a hostname which doesn't have an IPv6 address. [originally from svn r5082]
This commit is contained in:
parent
d72eb8f6db
commit
79629c729c
@ -306,6 +306,8 @@ SockAddr sk_namelookup(const char *host, char **canonicalname,
|
|||||||
unsigned long a;
|
unsigned long a;
|
||||||
struct hostent *h = NULL;
|
struct hostent *h = NULL;
|
||||||
char realhost[8192];
|
char realhost[8192];
|
||||||
|
int ret_family;
|
||||||
|
int err;
|
||||||
|
|
||||||
/* Clear the structure and default to IPv4. */
|
/* Clear the structure and default to IPv4. */
|
||||||
memset(ret, 0, sizeof(struct SockAddr_tag));
|
memset(ret, 0, sizeof(struct SockAddr_tag));
|
||||||
@ -314,6 +316,7 @@ SockAddr sk_namelookup(const char *host, char **canonicalname,
|
|||||||
address_family == ADDRTYPE_IPV6 ? AF_INET6 :
|
address_family == ADDRTYPE_IPV6 ? AF_INET6 :
|
||||||
#endif
|
#endif
|
||||||
AF_UNSPEC);
|
AF_UNSPEC);
|
||||||
|
ret_family = AF_UNSPEC;
|
||||||
*realhost = '\0';
|
*realhost = '\0';
|
||||||
|
|
||||||
if ((a = p_inet_addr(host)) == (unsigned long) INADDR_NONE) {
|
if ((a = p_inet_addr(host)) == (unsigned long) INADDR_NONE) {
|
||||||
@ -324,8 +327,7 @@ SockAddr sk_namelookup(const char *host, char **canonicalname,
|
|||||||
* it will fallback to IPv4. */
|
* it will fallback to IPv4. */
|
||||||
typedef int (CALLBACK * FGETADDRINFO) (const char *nodename,
|
typedef int (CALLBACK * FGETADDRINFO) (const char *nodename,
|
||||||
const char *servname,
|
const char *servname,
|
||||||
const struct addrinfo *
|
const struct addrinfo *hints,
|
||||||
hints,
|
|
||||||
struct addrinfo **res);
|
struct addrinfo **res);
|
||||||
FGETADDRINFO fGetAddrInfo = NULL;
|
FGETADDRINFO fGetAddrInfo = NULL;
|
||||||
|
|
||||||
@ -341,8 +343,8 @@ SockAddr sk_namelookup(const char *host, char **canonicalname,
|
|||||||
struct addrinfo hints;
|
struct addrinfo hints;
|
||||||
memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = ret->family;
|
hints.ai_family = ret->family;
|
||||||
if (fGetAddrInfo(host, NULL, &hints, &ret->ai) == 0)
|
if ((err = fGetAddrInfo(host, NULL, &hints, &ret->ai)) == 0)
|
||||||
ret->family = ret->ai->ai_family;
|
ret_family = ret->ai->ai_family;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
@ -350,24 +352,23 @@ SockAddr sk_namelookup(const char *host, char **canonicalname,
|
|||||||
* Otherwise use the IPv4-only gethostbyname...
|
* Otherwise use the IPv4-only gethostbyname...
|
||||||
* (NOTE: we don't use gethostbyname as a fallback!)
|
* (NOTE: we don't use gethostbyname as a fallback!)
|
||||||
*/
|
*/
|
||||||
if (ret->family == 0) {
|
|
||||||
if ( (h = p_gethostbyname(host)) )
|
if ( (h = p_gethostbyname(host)) )
|
||||||
ret->family = AF_INET;
|
ret_family = AF_INET;
|
||||||
}
|
else
|
||||||
|
err = p_WSAGetLastError();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret->family == AF_UNSPEC) {
|
if (ret_family == AF_UNSPEC) {
|
||||||
DWORD err = p_WSAGetLastError();
|
|
||||||
ret->error = (err == WSAENETDOWN ? "Network is down" :
|
ret->error = (err == WSAENETDOWN ? "Network is down" :
|
||||||
err ==
|
err == WSAHOST_NOT_FOUND ? "Host does not exist" :
|
||||||
WSAHOST_NOT_FOUND ? "Host does not exist" : err
|
err == WSATRY_AGAIN ? "Host not found" :
|
||||||
== WSATRY_AGAIN ? "Host not found" :
|
|
||||||
#ifndef NO_IPV6
|
#ifndef NO_IPV6
|
||||||
fGetAddrInfo ? "getaddrinfo: unknown error" :
|
fGetAddrInfo ? "getaddrinfo: unknown error" :
|
||||||
#endif
|
#endif
|
||||||
"gethostbyname: unknown error");
|
"gethostbyname: unknown error");
|
||||||
} else {
|
} else {
|
||||||
ret->error = NULL;
|
ret->error = NULL;
|
||||||
|
ret->family = ret_family;
|
||||||
|
|
||||||
#ifndef NO_IPV6
|
#ifndef NO_IPV6
|
||||||
/* If we got an address info use that... */
|
/* If we got an address info use that... */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user