1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

Have a go at fixing various outstanding portability issues in code that

was modified for IPv6.  Use a robust definition of struct sockaddr_storage,
do a proper configure test to see if ss_len exists, don't assume that
getnameinfo() will handle AF_UNIX sockets, don't trust getaddrinfo to
return the protocol we ask for, etc.  This incorporates several outstanding
patches from Kurt Roeckx, but I'm to blame for anything that doesn't
work ...
This commit is contained in:
Tom Lane
2003-07-23 23:30:41 +00:00
parent 93395de092
commit df63503dc2
15 changed files with 571 additions and 266 deletions

View File

@ -3,12 +3,16 @@
* getaddrinfo.c
* Support getaddrinfo() on platforms that don't have it.
*
* We also supply getnameinfo() here, assuming that the platform will have
* it if and only if it has getaddrinfo(). If this proves false on some
* platform, we'll need to split this file and provide a separate configure
* test for getnameinfo().
*
*
* Copyright (c) 2003, PostgreSQL Global Development Group
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/port/getaddrinfo.c,v 1.9 2003/06/23 23:52:00 momjian Exp $
* $Header: /cvsroot/pgsql/src/port/getaddrinfo.c,v 1.10 2003/07/23 23:30:41 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -22,9 +26,6 @@
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef HAVE_UNIX_SOCKETS
#include <sys/un.h>
#endif
#endif
#include "getaddrinfo.h"
@ -124,8 +125,9 @@ getaddrinfo(const char *node, const char *service,
if (service)
sin.sin_port = htons((unsigned short) atoi(service));
#if SALEN
sin.sin_len = sizeof(sin);
#ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
sin.sin_len = sizeof(sin);
#endif
ai = malloc(sizeof(*ai));
@ -209,7 +211,7 @@ gai_strerror(int errcode)
}
/*
* Convert an address to a hostname.
* Convert an ipv4 address to a hostname.
*
* Bugs: - Only supports NI_NUMERICHOST and NI_NUMERICSERV
* It will never resolv a hostname.
@ -217,11 +219,9 @@ gai_strerror(int errcode)
*/
int
getnameinfo(const struct sockaddr *sa, int salen,
char *node, int nodelen,
char *service, int servicelen, int flags)
char *node, int nodelen,
char *service, int servicelen, int flags)
{
int ret = -1;
/* Invalid arguments. */
if (sa == NULL || (node == NULL && service == NULL))
{
@ -242,45 +242,36 @@ getnameinfo(const struct sockaddr *sa, int salen,
}
#endif
if (service)
{
if (sa->sa_family == AF_INET)
{
ret = snprintf(service, servicelen, "%d",
ntohs(((struct sockaddr_in *)sa)->sin_port));
}
#ifdef HAVE_UNIX_SOCKETS
else if (sa->sa_family == AF_UNIX)
{
ret = snprintf(service, servicelen, "%s",
((struct sockaddr_un *)sa)->sun_path);
}
#endif
if (ret == -1 || ret > servicelen)
{
return EAI_MEMORY;
}
}
if (node)
{
int ret = -1;
if (sa->sa_family == AF_INET)
{
char *p;
p = inet_ntoa(((struct sockaddr_in *)sa)->sin_addr);
ret = snprintf(node, nodelen, "%s", p);
}
#ifdef HAVE_UNIX_SOCKETS
else if (sa->sa_family == AF_UNIX)
{
ret = snprintf(node, nodelen, "%s", "localhost");
}
#endif
if (ret == -1 || ret > nodelen)
{
return EAI_MEMORY;
}
}
if (service)
{
int ret = -1;
if (sa->sa_family == AF_INET)
{
ret = snprintf(service, servicelen, "%d",
ntohs(((struct sockaddr_in *)sa)->sin_port));
}
if (ret == -1 || ret > servicelen)
{
return EAI_MEMORY;
}
}
return 0;
}