fix getaddrinfo(3) and getnameinfo(3).

getaddrinfo(3): result from gethostby* will be broken if we call it again.
deep-copy them.  we have sa_len so configure them.
getnameinfo(3): we have sa_len so configure them.
This commit is contained in:
itojun 1999-12-13 16:22:56 +00:00
parent 46fb1c9fce
commit 58481c1c06
2 changed files with 47 additions and 8 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: getaddrinfo.c,v 1.14 1999/12/13 14:18:32 itojun Exp $ */
/* $NetBSD: getaddrinfo.c,v 1.15 1999/12/13 16:22:56 itojun Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@ -51,6 +51,8 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#else
#define HAVE_SOCKADDR_SA_LEN
#endif
#include <sys/types.h>
@ -512,7 +514,7 @@ explore_fqdn(pai, hostname, servname, res)
struct hostent *hp;
int h_error;
int af;
char *ap;
char *ap = NULL;
struct addrinfo sentinel, *cur;
int i;
const struct afd *afd;
@ -580,9 +582,19 @@ explore_fqdn(pai, hostname, servname, res)
if (hp == NULL)
goto free;
ap = malloc(hp->h_length);
if (ap == NULL) {
error = EAI_MEMORY;
goto free;
}
for (i = 0; hp->h_addr_list[i] != NULL; i++) {
/*
* hp will be overwritten if we use gethostbyname2().
* always deep copy for simplification.
*/
af = hp->h_addrtype;
ap = hp->h_addr_list[i];
memcpy(ap, hp->h_addr_list[i], hp->h_length);
if (af == AF_INET6
&& IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) {
af = AF_INET;
@ -623,6 +635,8 @@ free:
if (hp)
freehostent(hp);
#endif
if (ap)
free(ap);
if (sentinel.ai_next)
freeaddrinfo(sentinel.ai_next);
return error;
@ -861,9 +875,10 @@ get_name(addr, afd, res, numaddr, pai, servname)
const struct addrinfo *pai;
const char *servname;
{
struct hostent *hp;
struct addrinfo *cur;
struct hostent *hp = NULL;
struct addrinfo *cur = NULL;
int error = 0;
char *ap = NULL, *cn = NULL;
#ifdef USE_GETIPNODEBY
int h_error;
@ -872,9 +887,28 @@ get_name(addr, afd, res, numaddr, pai, servname)
hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
#endif
if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
#ifdef USE_GETIPNODEBY
GET_AI(cur, afd, hp->h_addr_list[0]);
GET_PORT(cur, servname);
GET_CANONNAME(cur, hp->h_name);
#else
/* hp will be damaged if we use gethostbyaddr() */
if ((ap = (char *)malloc(hp->h_length)) == NULL) {
error = EAI_MEMORY;
goto free;
}
memcpy(ap, hp->h_addr_list[0], hp->h_length);
if ((cn = strdup(hp->h_name)) == NULL) {
error = EAI_MEMORY;
goto free;
}
GET_AI(cur, afd, ap);
GET_PORT(cur, servname);
GET_CANONNAME(cur, cn);
free(ap); ap = NULL;
free(cn); cn = NULL;
#endif
} else {
GET_AI(cur, afd, numaddr);
GET_PORT(cur, servname);
@ -889,11 +923,14 @@ get_name(addr, afd, res, numaddr, pai, servname)
free:
if (cur)
freeaddrinfo(cur);
if (ap)
free(ap);
if (cn)
free(cn);
#ifdef USE_GETIPNODEBY
if (hp)
freehostent(hp);
#endif
/* bad: */
*res = NULL;
return error;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: getnameinfo.c,v 1.5 1999/12/13 14:18:32 itojun Exp $ */
/* $NetBSD: getnameinfo.c,v 1.6 1999/12/13 16:22:56 itojun Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@ -43,6 +43,8 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#else
#define HAVE_SA_LEN
#endif
#include <sys/types.h>
@ -124,7 +126,7 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
if (sa == NULL)
return ENI_NOSOCKET;
#ifdef HAVE_SA_LEN /*XXX*/
#ifdef HAVE_SA_LEN
if (sa->sa_len != salen)
return ENI_SALEN;
#endif