summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.global.in3
-rw-r--r--src/backend/libpq/ip.c49
-rw-r--r--src/backend/libpq/pqcomm.c61
-rw-r--r--src/include/getaddrinfo.h43
-rw-r--r--src/include/libpq/ip.h13
-rw-r--r--src/include/pg_config.h.in5
-rw-r--r--src/interfaces/libpq/Makefile8
-rw-r--r--src/interfaces/libpq/fe-connect.c121
-rw-r--r--src/port/getaddrinfo.c132
9 files changed, 221 insertions, 214 deletions
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index c0d2d202c25..29e48ca17bd 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -1,5 +1,5 @@
# -*-makefile-*-
-# $Header: /cvsroot/pgsql/src/Makefile.global.in,v 1.159 2003/01/06 03:18:26 momjian Exp $
+# $Header: /cvsroot/pgsql/src/Makefile.global.in,v 1.160 2003/03/29 11:31:51 petere Exp $
#------------------------------------------------------------------------------
# All PostgreSQL makefiles include this file and use the variables it sets,
@@ -277,7 +277,6 @@ ifeq ($(enable_rpath), yes)
LDFLAGS += $(rpath)
endif
-HAVE_IPV6 = @HAVE_IPV6@
##########################################################################
#
diff --git a/src/backend/libpq/ip.c b/src/backend/libpq/ip.c
index 20f5df311a8..3c3b872c1de 100644
--- a/src/backend/libpq/ip.c
+++ b/src/backend/libpq/ip.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.2 2003/01/09 14:35:03 petere Exp $
+ * $Header: /cvsroot/pgsql/src/backend/libpq/ip.c,v 1.3 2003/03/29 11:31:51 petere Exp $
*
* This file and the IPV6 implementation were initially provided by
* Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design
@@ -44,9 +44,9 @@
#define LOG stderr
#endif
-#if defined(HAVE_UNIX_SOCKETS) && defined(HAVE_IPV6)
+#if defined(HAVE_UNIX_SOCKETS)
static int getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
- struct addrinfo **result);
+ struct addrinfo **result);
#endif /* HAVE_UNIX_SOCKETS */
/*
@@ -54,48 +54,17 @@ static int getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
*/
int
getaddrinfo2(const char *hostname, const char *servname,
-#ifdef HAVE_IPV6
const struct addrinfo *hintp, struct addrinfo **result)
-#else
- int family, SockAddr *result)
-#endif
{
#ifdef HAVE_UNIX_SOCKETS
-#ifdef HAVE_IPV6
if (hintp != NULL && hintp->ai_family == AF_UNIX)
return getaddrinfo_unix(servname, hintp, result);
-#else
- if (family == AF_UNIX)
- return 0;
-#endif
else
{
#endif /* HAVE_UNIX_SOCKETS */
-#ifdef HAVE_IPV6
/* NULL has special meaning to getaddrinfo */
return getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
servname, hintp, result);
-#else
- if (hostname[0] == '\0')
- result->in.sin_addr.s_addr = htonl(INADDR_ANY);
- else
- {
- struct hostent *hp;
-
- hp = gethostbyname(hostname);
- if ((hp == NULL) || (hp->h_addrtype != AF_INET))
- {
- elog(LOG, "getaddrinfo2: gethostbyname(%s) failed\n", hostname);
- return STATUS_ERROR;
- }
- memmove((char *) &(result->in.sin_addr), (char *) hp->h_addr,
- hp->h_length);
- }
-
- result->in.sin_port = htons((unsigned short)atoi(servname));
- return 0;
-#endif /* HAVE_IPV6 */
-
#ifdef HAVE_UNIX_SOCKETS
}
#endif /* HAVE_UNIX_SOCKETS */
@@ -105,7 +74,6 @@ getaddrinfo2(const char *hostname, const char *servname,
/*
* freeaddrinfo2 - free IPv6 addrinfo structures
*/
-#ifdef HAVE_IPV6
void
freeaddrinfo2(int hint_ai_family, struct addrinfo *ai)
{
@@ -126,10 +94,9 @@ freeaddrinfo2(int hint_ai_family, struct addrinfo *ai)
#endif /* HAVE_UNIX_SOCKETS */
freeaddrinfo(ai);
}
-#endif
-#if defined(HAVE_UNIX_SOCKETS) && defined(HAVE_IPV6)
+#if defined(HAVE_UNIX_SOCKETS)
/* -------
* getaddrinfo_unix - get unix socket info using IPv6
*
@@ -140,7 +107,7 @@ freeaddrinfo2(int hint_ai_family, struct addrinfo *ai)
*/
static int
getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
- struct addrinfo **result)
+ struct addrinfo **result)
{
struct addrinfo hints;
struct addrinfo *aip;
@@ -159,9 +126,9 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
if (hints.ai_socktype == 0)
hints.ai_socktype = SOCK_STREAM;
- if (!(hints.ai_family == AF_UNIX))
+ if (hints.ai_family != AF_UNIX)
{
- elog(LOG, "hints.ai_family is invalied getaddrinfo_unix()\n");
+ elog(LOG, "hints.ai_family is invalid in getaddrinfo_unix()\n");
return EAI_ADDRFAMILY;
}
@@ -197,7 +164,7 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp,
return 0;
}
-#endif /* HAVE_UNIX_SOCKETS && HAVE_IPV6 */
+#endif /* HAVE_UNIX_SOCKETS */
/* ----------
* SockAddr_ntop - set IP address string from SockAddr
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 8c6e1dc6d0b..20954a4ecf1 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -29,7 +29,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: pqcomm.c,v 1.147 2003/01/25 05:19:46 tgl Exp $
+ * $Id: pqcomm.c,v 1.148 2003/03/29 11:31:51 petere Exp $
*
*-------------------------------------------------------------------------
*/
@@ -96,13 +96,6 @@ static int Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName);
static int Setup_AF_UNIX(void);
#endif /* HAVE_UNIX_SOCKETS */
-#ifdef HAVE_IPV6
-#define FREEADDRINFO2(family, addrs) freeaddrinfo2((family), (addrs))
-#else
-/* do nothing */
-#define FREEADDRINFO2(family, addrs) do {} while (0)
-#endif
-
/*
* Configuration options
@@ -208,13 +201,6 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
int ret;
char portNumberStr[64];
char *service;
-
- /*
- * IPv6 address lookups use a hint structure, while IPv4 creates an
- * address structure directly.
- */
-
-#ifdef HAVE_IPV6
struct addrinfo *addrs = NULL;
struct addrinfo hint;
@@ -225,16 +211,6 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
hint.ai_family = family;
hint.ai_flags = AI_PASSIVE;
hint.ai_socktype = SOCK_STREAM;
-#else
- SockAddr saddr;
- size_t len;
-
- Assert(family == AF_INET || family == AF_UNIX);
-
- /* Initialize address structure */
- MemSet((char *) &saddr, 0, sizeof(saddr));
- saddr.sa.sa_family = family;
-#endif /* HAVE_IPV6 */
#ifdef HAVE_UNIX_SOCKETS
if (family == AF_UNIX)
@@ -242,38 +218,21 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
if (Lock_AF_UNIX(portNumber, unixSocketName) != STATUS_OK)
return STATUS_ERROR;
service = sock_path;
-#ifndef HAVE_IPV6
- UNIXSOCK_PATH(saddr.un, portNumber, unixSocketName);
- len = UNIXSOCK_LEN(saddr.un);
-#endif
}
else
#endif /* HAVE_UNIX_SOCKETS */
{
snprintf(portNumberStr, sizeof(portNumberStr), "%d", portNumber);
service = portNumberStr;
-#ifndef HAVE_IPV6
- len = sizeof(saddr.in);
-#endif
}
- /* Look up name using IPv6 or IPv4 routines */
-#ifdef HAVE_IPV6
ret = getaddrinfo2(hostName, service, &hint, &addrs);
if (ret || addrs == NULL)
-#else
- ret = getaddrinfo2(hostName, service, family, &saddr);
- if (ret)
-#endif
{
elog(LOG, "server socket failure: getaddrinfo2()%s: %s",
-#ifdef HAVE_IPV6
(family == AF_INET6) ? " using IPv6" : "", gai_strerror(ret));
if (addrs != NULL)
- FREEADDRINFO2(hint.ai_family, addrs);
-#else
- "", hostName);
-#endif
+ freeaddrinfo2(hint.ai_family, addrs);
return STATUS_ERROR;
}
@@ -281,7 +240,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{
elog(LOG, "server socket failure: socket(): %s",
strerror(errno));
- FREEADDRINFO2(hint.ai_family, addrs);
+ freeaddrinfo2(hint.ai_family, addrs);
return STATUS_ERROR;
}
@@ -292,17 +251,13 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{
elog(LOG, "server socket failure: setsockopt(SO_REUSEADDR): %s",
strerror(errno));
- FREEADDRINFO2(hint.ai_family, addrs);
+ freeaddrinfo2(hint.ai_family, addrs);
return STATUS_ERROR;
}
}
-#ifdef HAVE_IPV6
Assert(addrs->ai_next == NULL && addrs->ai_family == family);
err = bind(fd, addrs->ai_addr, addrs->ai_addrlen);
-#else
- err = bind(fd, (struct sockaddr *) &saddr.sa, len);
-#endif
if (err < 0)
{
elog(LOG, "server socket failure: bind(): %s\n"
@@ -313,7 +268,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
sock_path);
else
elog(LOG, "\tIf not, wait a few seconds and retry.");
- FREEADDRINFO2(hint.ai_family, addrs);
+ freeaddrinfo2(hint.ai_family, addrs);
return STATUS_ERROR;
}
@@ -322,7 +277,7 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{
if (Setup_AF_UNIX() != STATUS_OK)
{
- FREEADDRINFO2(hint.ai_family, addrs);
+ freeaddrinfo2(hint.ai_family, addrs);
return STATUS_ERROR;
}
}
@@ -342,12 +297,12 @@ StreamServerPort(int family, char *hostName, unsigned short portNumber,
{
elog(LOG, "server socket failure: listen(): %s",
strerror(errno));
- FREEADDRINFO2(hint.ai_family, addrs);
+ freeaddrinfo2(hint.ai_family, addrs);
return STATUS_ERROR;
}
*fdP = fd;
- FREEADDRINFO2(hint.ai_family, addrs);
+ freeaddrinfo2(hint.ai_family, addrs);
return STATUS_OK;
}
diff --git a/src/include/getaddrinfo.h b/src/include/getaddrinfo.h
new file mode 100644
index 00000000000..7933f93d705
--- /dev/null
+++ b/src/include/getaddrinfo.h
@@ -0,0 +1,43 @@
+/* $Header: /cvsroot/pgsql/src/include/getaddrinfo.h,v 1.1 2003/03/29 11:31:51 petere Exp $ */
+
+#ifndef GETADDRINFO_H
+#define GETADDRINFO_H
+
+#include "c.h"
+#include <netdb.h>
+
+
+struct addrinfo {
+ int ai_flags;
+ int ai_family;
+ int ai_socktype;
+ int ai_protocol;
+ size_t ai_addrlen;
+ struct sockaddr *ai_addr;
+ char *ai_canonname;
+ struct addrinfo *ai_next;
+};
+
+
+int getaddrinfo(const char *node, const char *service,
+ const struct addrinfo *hints, struct addrinfo **res);
+void freeaddrinfo(struct addrinfo *res);
+const char *gai_strerror(int errcode);
+
+
+#define EAI_BADFLAGS -1
+#define EAI_NONAME -2
+#define EAI_AGAIN -3
+#define EAI_FAIL -4
+#define EAI_NODATA -5
+#define EAI_FAMILY -6
+#define EAI_SOCKTYPE -7
+#define EAI_SERVICE -8
+#define EAI_ADDRFAMILY -9
+#define EAI_MEMORY -10
+#define EAI_SYSTEM -11
+
+#define AI_PASSIVE 0x0001
+#define AI_NUMERICHOST 0x0004
+
+#endif /* GETADDRINFO_H */
diff --git a/src/include/libpq/ip.h b/src/include/libpq/ip.h
index c9d1b0e4667..ac782a03364 100644
--- a/src/include/libpq/ip.h
+++ b/src/include/libpq/ip.h
@@ -1,17 +1,16 @@
#ifndef IP_H
#define IP_H
+#include "c.h"
#include <sys/socket.h>
#include <netdb.h>
#include "libpq/pqcomm.h"
+#ifndef HAVE_GETADDRINFO
+#include "getaddrinfo.h"
+#endif
-#ifdef HAVE_IPV6
-void freeaddrinfo2(int hint_ai_family, struct addrinfo *ai);
int getaddrinfo2(const char *hostname, const char *servname,
- const struct addrinfo *hintp, struct addrinfo **result);
-#else
-int getaddrinfo2(const char *hostname, const char *servname,
- int family, SockAddr *result);
-#endif
+ const struct addrinfo *hintp, struct addrinfo **result);
+void freeaddrinfo2(int hint_ai_family, struct addrinfo *ai);
char *SockAddr_ntop(const SockAddr *sa, char *dst, size_t cnt, int v4conv);
int SockAddr_pton(SockAddr *sa, const char *src);
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 3c8e0b5d507..f515c6cff1a 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -8,7 +8,7 @@
* or in pg_config.h afterwards. Of course, if you edit pg_config.h, then your
* changes will be overwritten the next time you run configure.
*
- * $Id: pg_config.h.in,v 1.41 2003/03/06 03:16:55 tgl Exp $
+ * $Id: pg_config.h.in,v 1.42 2003/03/29 11:31:51 petere Exp $
*/
#ifndef PG_CONFIG_H
@@ -459,6 +459,9 @@
#undef HAVE_FP_CLASS_D
#undef HAVE_CLASS
+/* Set to 1 if you have getaddrinfo() */
+#undef HAVE_GETADDRINFO
+
/* Set to 1 if you have gethostname() */
#undef HAVE_GETHOSTNAME
diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile
index c431ca44b7f..16a5ff5ad14 100644
--- a/src/interfaces/libpq/Makefile
+++ b/src/interfaces/libpq/Makefile
@@ -4,7 +4,7 @@
#
# Copyright (c) 1994, Regents of the University of California
#
-# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.73 2003/02/03 14:24:07 momjian Exp $
+# $Header: /cvsroot/pgsql/src/interfaces/libpq/Makefile,v 1.74 2003/03/29 11:31:51 petere Exp $
#
#-------------------------------------------------------------------------
@@ -23,7 +23,7 @@ override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) -DFRONTEND -DSYSCONFDIR='"$(sysconf
OBJS= fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \
pqexpbuffer.o dllist.o pqsignal.o fe-secure.o wchar.o encnames.o ip.o \
md5.o \
- $(filter inet_aton.o snprintf.o strerror.o, $(LIBOBJS))
+ $(filter getaddrinfo.o inet_aton.o snprintf.o strerror.o, $(LIBOBJS))
# Add libraries that libpq depends (or might depend) on into the
@@ -54,7 +54,7 @@ ip.c: $(backend_src)/libpq/ip.c
# symlink the source files in here and build our own object file.
# this only gets done if configure finds system doesn't have inet_aton()
-inet_aton.c snprintf.c strerror.c: %.c : $(top_srcdir)/src/port/%.c
+getaddrinfo.c inet_aton.c snprintf.c strerror.c: %.c : $(top_srcdir)/src/port/%.c
rm -f $@ && $(LN_S) $< .
encnames.c wchar.c : % : $(backend_src)/utils/mb/%
@@ -75,4 +75,4 @@ uninstall: uninstall-lib
clean distclean maintainer-clean: clean-lib
rm -f $(OBJS) dllist.c md5.c ip.c wchar.c encnames.c
- rm -f $(OBJS) inet_aton.c snprintf.c strerror.c
+ rm -f $(OBJS) getaddrinfo.c inet_aton.c snprintf.c strerror.c
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 78d0e20188e..141c80e2765 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.228 2003/03/20 06:23:30 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.229 2003/03/29 11:31:51 petere Exp $
*
*-------------------------------------------------------------------------
*/
@@ -50,13 +50,6 @@
#include "mb/pg_wchar.h"
-#ifdef HAVE_IPV6
-#define FREEADDRINFO2(family, addrs) freeaddrinfo2((family), (addrs))
-#else
-/* do nothing */
-#define FREEADDRINFO2(family, addrs) do {} while (0)
-#endif
-
#ifdef WIN32
static int
inet_aton(const char *cp, struct in_addr * inp)
@@ -803,7 +796,6 @@ connectDBStart(PGconn *conn)
StartupPacket np; /* Used to negotiate SSL connection */
char SSLok;
#endif
-#ifdef HAVE_IPV6
struct addrinfo *addrs = NULL;
struct addrinfo *addr_cur = NULL;
struct addrinfo hint;
@@ -814,9 +806,6 @@ connectDBStart(PGconn *conn)
/* Initialize hint structure */
MemSet(&hint, 0, sizeof(hint));
hint.ai_socktype = SOCK_STREAM;
-#else
- int family = -1;
-#endif
if (!conn)
return 0;
@@ -835,11 +824,6 @@ connectDBStart(PGconn *conn)
/*
* Set up the connection to postmaster/backend.
- *
- * This code is confusing because IPv6 creates a hint structure
- * that is passed to getaddrinfo2(), which returns a list of address
- * structures that are looped through, while IPv4 creates an address
- * structure directly.
*/
MemSet((char *) &conn->raddr, 0, sizeof(conn->raddr));
@@ -853,73 +837,23 @@ connectDBStart(PGconn *conn)
if (conn->pghostaddr != NULL && conn->pghostaddr[0] != '\0')
{
-#ifdef HAVE_IPV6
+ /* Using pghostaddr avoids a hostname lookup */
node = conn->pghostaddr;
hint.ai_family = AF_UNSPEC;
-#else
- /* Using pghostaddr avoids a hostname lookup */
- struct in_addr addr;
-
- if (!inet_aton(conn->pghostaddr, &addr))
- {
- printfPQExpBuffer(&conn->errorMessage,
- libpq_gettext("invalid host address: %s\n"),
- conn->pghostaddr);
- goto connect_errReturn;
- }
-
- family = AF_INET;
-
- memcpy((char *) &(conn->raddr.in.sin_addr),
- (char *) &addr, sizeof(addr));
-#endif
+ hint.ai_flags = AI_NUMERICHOST;
}
else if (conn->pghost != NULL && conn->pghost[0] != '\0')
{
-#ifdef HAVE_IPV6
+ /* Using pghost, so we have to look-up the hostname */
node = conn->pghost;
hint.ai_family = AF_UNSPEC;
-#else
- /* Using pghost, so we have to look-up the hostname */
- if (getaddrinfo2(conn->pghost, portstr, family, &conn->raddr) != 0)
- goto connect_errReturn;
-
- family = AF_INET;
-#endif
}
else
{
+ /* pghostaddr and pghost are NULL, so use Unix domain socket */
#ifdef HAVE_UNIX_SOCKETS
-#ifdef HAVE_IPV6
node = unix_node;
hint.ai_family = AF_UNIX;
-#else
- /* pghostaddr and pghost are NULL, so use Unix domain socket */
- family = AF_UNIX;
-#endif
-#endif /* HAVE_UNIX_SOCKETS */
- }
-
-#ifndef HAVE_IPV6
- /* Set family */
- conn->raddr.sa.sa_family = family;
-#endif
-
-#ifdef HAVE_IPV6
- if (hint.ai_family == AF_UNSPEC)
- {
- /* do nothing */
- }
-#else
- if (family == AF_INET)
- {
- conn->raddr.in.sin_port = htons((unsigned short) (portnum));
- conn->raddr_len = sizeof(struct sockaddr_in);
- }
-#endif
- else
- {
-#ifdef HAVE_UNIX_SOCKETS
UNIXSOCK_PATH(conn->raddr.un, portnum, conn->pgunixsocket);
conn->raddr_len = UNIXSOCK_LEN(conn->raddr.un);
StrNCpy(portstr, conn->raddr.un.sun_path, sizeof(portstr));
@@ -931,7 +865,6 @@ connectDBStart(PGconn *conn)
#endif /* HAVE_UNIX_SOCKETS */
}
-#ifdef HAVE_IPV6
/* Use getaddrinfo2() to resolve the address */
ret = getaddrinfo2(node, portstr, &hint, &addrs);
if (ret || addrs == NULL)
@@ -941,40 +874,27 @@ connectDBStart(PGconn *conn)
gai_strerror(ret));
goto connect_errReturn;
}
-#endif
/*
- * For IPV6 we loop over the possible addresses returned by
- * getaddrinfo2(), and fail only when they all fail (reporting the
- * error returned for the *last* alternative, which may not be what
- * users expect :-(). Otherwise, there is no true loop here.
+ * We loop over the possible addresses returned by getaddrinfo2(),
+ * and fail only when they all fail (reporting the error returned
+ * for the *last* alternative, which may not be what users expect
+ * :-().
*
* In either case, we never actually fall out of the loop; the
* only exits are via "break" or "goto connect_errReturn". Thus,
* there is no exit test in the for().
*/
- for (
-#ifdef HAVE_IPV6
- addr_cur = addrs; ; addr_cur = addr_cur->ai_next
-#else
- ;;
-#endif
- )
+ for (addr_cur = addrs; ; addr_cur = addr_cur->ai_next)
{
/* Open a socket */
-#ifdef HAVE_IPV6
conn->sock = socket(addr_cur->ai_family, SOCK_STREAM,
addr_cur->ai_protocol);
-#else
- conn->sock = socket(family, SOCK_STREAM, 0);
-#endif
if (conn->sock < 0)
{
-#ifdef HAVE_IPV6
/* ignore socket() failure if we have more addrs to try */
if (addr_cur->ai_next != NULL)
continue;
-#endif
printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not create socket: %s\n"),
SOCK_STRERROR(SOCK_ERRNO));
@@ -987,11 +907,7 @@ connectDBStart(PGconn *conn)
* using SSL, then we need the blocking I/O (XXX Can this be fixed?).
*/
-#ifdef HAVE_IPV6
if (isAF_INETx(addr_cur->ai_family))
-#else
- if (isAF_INETx(family))
-#endif
{
if (!connectNoDelay(conn))
goto connect_errReturn;
@@ -1012,11 +928,7 @@ connectDBStart(PGconn *conn)
* ----------
*/
retry1:
-#ifdef HAVE_IPV6
if (connect(conn->sock, addr_cur->ai_addr, addr_cur->ai_addrlen) < 0)
-#else
- if (connect(conn->sock, &conn->raddr.sa, conn->raddr_len) < 0)
-#endif
{
if (SOCK_ERRNO == EINTR)
/* Interrupted system call - we'll just try again */
@@ -1043,7 +955,6 @@ retry1:
* This connection failed. We need to close the socket,
* and either loop to try the next address or report an error.
*/
-#ifdef HAVE_IPV6
/* ignore connect() failure if we have more addrs to try */
if (addr_cur->ai_next != NULL)
{
@@ -1051,19 +962,19 @@ retry1:
conn->sock = -1;
continue;
}
-#endif
+ /* copy failed address for error report */
+ memcpy(&conn->raddr, addr_cur->ai_addr, addr_cur->ai_addrlen);
+ conn->raddr_len = addr_cur->ai_addrlen;
connectFailureMessage(conn, SOCK_ERRNO);
goto connect_errReturn;
} /* loop over addrs */
-#ifdef HAVE_IPV6
/* Remember the successfully opened address alternative */
memcpy(&conn->raddr, addr_cur->ai_addr, addr_cur->ai_addrlen);
conn->raddr_len = addr_cur->ai_addrlen;
/* and release the address list */
- FREEADDRINFO2(hint.ai_family, addrs);
+ freeaddrinfo2(hint.ai_family, addrs);
addrs = NULL;
-#endif
#ifdef USE_SSL
/* Attempt to negotiate SSL usage */
@@ -1153,10 +1064,8 @@ connect_errReturn:
conn->sock = -1;
}
conn->status = CONNECTION_BAD;
-#ifdef HAVE_IPV6
if (addrs != NULL)
- FREEADDRINFO2(hint.ai_family, addrs);
-#endif
+ freeaddrinfo2(hint.ai_family, addrs);
return 0;
}
diff --git a/src/port/getaddrinfo.c b/src/port/getaddrinfo.c
new file mode 100644
index 00000000000..aa3d3ab6b87
--- /dev/null
+++ b/src/port/getaddrinfo.c
@@ -0,0 +1,132 @@
+/* $Header: /cvsroot/pgsql/src/port/getaddrinfo.c,v 1.1 2003/03/29 11:31:52 petere Exp $ */
+
+#include "c.h"
+#include "getaddrinfo.h"
+#include <netdb.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+
+int
+getaddrinfo(const char *node, const char *service,
+ const struct addrinfo *hints,
+ struct addrinfo **res)
+{
+ struct addrinfo *ai;
+ struct sockaddr_in sin, *psin;
+
+ if (!hints || (hints->ai_family != AF_INET && hints->ai_family != AF_UNSPEC))
+ return EAI_FAMILY;
+
+ if (hints->ai_socktype != SOCK_STREAM)
+ return EAI_SOCKTYPE;
+
+ if (!node && !service)
+ return EAI_NONAME;
+
+ if (node)
+ {
+ if (node[0] == '\0')
+ sin.sin_addr.s_addr = htonl(INADDR_ANY);
+ else if (hints->ai_flags & AI_NUMERICHOST)
+ {
+ inet_aton(node, &sin.sin_addr);
+ }
+ else
+ {
+ struct hostent *hp;
+
+ hp = gethostbyname(node);
+ if (hp == NULL)
+ {
+ switch (h_errno)
+ {
+ case HOST_NOT_FOUND:
+ return EAI_NONAME;
+ case NO_DATA:
+ return EAI_NODATA;
+ case TRY_AGAIN:
+ return EAI_AGAIN;
+ case NO_RECOVERY:
+ default:
+ return EAI_FAIL;
+ }
+ }
+ if (hp->h_addrtype != AF_INET)
+ return EAI_ADDRFAMILY;
+
+ memmove(&(sin.sin_addr), hp->h_addr, hp->h_length);
+ }
+ }
+ else
+ {
+ if (hints->ai_flags & AI_PASSIVE)
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ }
+
+ if (service)
+ sin.sin_port = htons((unsigned short)atoi(service));
+
+ ai = malloc(sizeof(*ai));
+ if (!ai)
+ return EAI_MEMORY;
+ psin = malloc(sizeof(*psin));
+ if (!psin)
+ {
+ free(ai);
+ return EAI_MEMORY;
+ }
+
+ memcpy(psin, &sin, sizeof(sin));
+
+ ai->ai_family = hints->ai_family;
+ ai->ai_socktype = hints->ai_socktype;
+ ai->ai_protocol = hints->ai_protocol;
+ ai->ai_addrlen = sizeof(*psin);
+ ai->ai_addr = (struct sockaddr *) psin;
+ ai->ai_canonname = NULL;
+ ai->ai_next = NULL;
+
+ *res = ai;
+
+ return 0;
+}
+
+
+void
+freeaddrinfo(struct addrinfo *res)
+{
+ if (res)
+ {
+ if (res->ai_addr)
+ free(res->ai_addr);
+ free(res);
+ }
+}
+
+
+const char*
+gai_strerror(int errcode)
+{
+ int hcode;
+
+ switch (errcode)
+ {
+ case EAI_NONAME:
+ hcode = HOST_NOT_FOUND;
+ break;
+ case EAI_NODATA:
+ hcode = NO_DATA;
+ break;
+ case EAI_AGAIN:
+ hcode = TRY_AGAIN;
+ break;
+ case EAI_FAIL:
+ default:
+ hcode = NO_RECOVERY;
+ break;
+ }
+
+ return hstrerror(hcode);
+}