Fix fake getnameinfo() and check more arguments.

This commit is contained in:
Guus Sliepen 2003-08-17 08:32:39 +00:00
parent f4e80cc5e0
commit 2236e05e51
3 changed files with 24 additions and 9 deletions

View file

@ -5,10 +5,11 @@
* See getaddrinfo.c and getnameinfo.c. * See getaddrinfo.c and getnameinfo.c.
*/ */
/* $Id: fake-gai-errnos.h,v 1.1.2.2 2003/07/12 17:41:45 guus Exp $ */ /* $Id: fake-gai-errnos.h,v 1.1.2.3 2003/08/17 08:32:38 guus Exp $ */
/* for old netdb.h */ /* for old netdb.h */
#ifndef EAI_NODATA #ifndef EAI_NODATA
#define EAI_NODATA 1 #define EAI_NODATA 1
#define EAI_MEMORY 2 #define EAI_MEMORY 2
#define EAI_FAMILY 3
#endif #endif

View file

@ -23,6 +23,8 @@ char *gai_strerror(int ecode)
return "No address associated with hostname"; return "No address associated with hostname";
case EAI_MEMORY: case EAI_MEMORY:
return "Memory allocation failure"; return "Memory allocation failure";
case EAI_FAMILY:
return "Address family not supported";
default: default:
return "Unknown error"; return "Unknown error";
} }
@ -67,6 +69,9 @@ int getaddrinfo(const char *hostname, const char *servname, const struct addrinf
int i; int i;
uint16_t port = 0; uint16_t port = 0;
if(hints && hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC)
return EAI_FAMILY;
if (servname) if (servname)
port = htons(atoi(servname)); port = htons(atoi(servname));
@ -82,7 +87,7 @@ int getaddrinfo(const char *hostname, const char *servname, const struct addrinf
hp = gethostbyname(hostname); hp = gethostbyname(hostname);
if(!hp || !hp->h_addr_list[0]) if(!hp || !hp->h_addr_list || !hp->h_addr_list[0])
return EAI_NODATA; return EAI_NODATA;
for (i = 0; hp->h_addr_list[i]; i++) { for (i = 0; hp->h_addr_list[i]; i++) {

View file

@ -20,27 +20,36 @@ int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t host
{ {
struct sockaddr_in *sin = (struct sockaddr_in *)sa; struct sockaddr_in *sin = (struct sockaddr_in *)sa;
struct hostent *hp; struct hostent *hp;
int len;
if(serv) if(sa->sa_family != AF_INET)
snprintf(serv, sizeof(tmpserv), "%d", ntohs(sin->sin_port)); return EAI_FAMILY;
if(!host) if(serv && servlen) {
len = snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
if(len < 0 || len >= servlen)
return EAI_MEMORY;
}
if(!host || !hostlen)
return 0; return 0;
if(flags & NI_NUMERICHOST) { if(flags & NI_NUMERICHOST) {
strncpy(host, inet_ntoa(sin->sin_addr), sizeof(host)); len = snprintf((host, hostlen, "%s", inet_ntoa(sin->sin_addr));
if(len < 0 || len >= hostlen)
return EAI_MEMORY;
return 0; return 0;
} }
hp = gethostbyaddr((char *)&sin->sin_addr, sizeof(struct in_addr), AF_INET); hp = gethostbyaddr((char *)&sin->sin_addr, sizeof(struct in_addr), AF_INET);
if(!hp || !hp->h_name) if(!hp || !hp->h_name || !hp->h_name[0])
return EAI_NODATA; return EAI_NODATA;
if(strlen(hp->h_name) >= hostlen) len = snprintf((host, hostlen, "%s", hp->h_name);
if(len < 0 || len >= hostlen)
return EAI_MEMORY; return EAI_MEMORY;
strncpy(host, hp->h_name, hostlen);
return 0; return 0;
} }
#endif /* !HAVE_GETNAMEINFO */ #endif /* !HAVE_GETNAMEINFO */