]> granicus.if.org Git - postgresql/commitdiff
Move code shared between libpq and backend from backend/libpq/ to common/.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 2 Sep 2016 10:49:59 +0000 (13:49 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 2 Sep 2016 10:49:59 +0000 (13:49 +0300)
When building libpq, ip.c and md5.c were symlinked or copied from
src/backend/libpq into src/interfaces/libpq, but now that we have a
directory specifically for routines that are shared between the server and
client binaries, src/common/, move them there.

Some routines in ip.c were only used in the backend. Keep those in
src/backend/libpq, but rename to ifaddr.c to avoid confusion with the file
that's now in common.

Fix the comment in src/common/Makefile to reflect how libpq actually links
those files.

There are two more files that libpq symlinks directly from src/backend:
encnames.c and wchar.c. I don't feel compelled to move those right now,
though.

Patch by Michael Paquier, with some changes by me.

Discussion: <69938195-9c76-8523-0af8-eb718ea5b36e@iki.fi>

25 files changed:
contrib/passwordcheck/passwordcheck.c
src/backend/commands/user.c
src/backend/libpq/Makefile
src/backend/libpq/auth.c
src/backend/libpq/crypt.c
src/backend/libpq/hba.c
src/backend/libpq/ifaddr.c [moved from src/backend/libpq/ip.c with 72% similarity]
src/backend/libpq/pqcomm.c
src/backend/postmaster/pgstat.c
src/backend/postmaster/postmaster.c
src/backend/utils/adt/network.c
src/backend/utils/adt/pgstatfuncs.c
src/backend/utils/adt/varlena.c
src/common/Makefile
src/common/ip.c [new file with mode: 0644]
src/common/md5.c [moved from src/backend/libpq/md5.c with 98% similarity]
src/include/common/ip.h [moved from src/include/libpq/ip.h with 65% similarity]
src/include/common/md5.h [moved from src/include/libpq/md5.h with 96% similarity]
src/include/libpq/ifaddr.h [new file with mode: 0644]
src/interfaces/libpq/Makefile
src/interfaces/libpq/fe-auth.c
src/interfaces/libpq/fe-connect.c
src/tools/ifaddrs/Makefile
src/tools/ifaddrs/test_ifaddrs.c
src/tools/msvc/Mkvcbuild.pm

index b4c1ce005bf0ab5728c3fd46717d8bdcfeee35a9..a0db89bbbf4e477aa1827a7ea4827555e3919e5d 100644 (file)
@@ -21,8 +21,8 @@
 #endif
 
 #include "commands/user.h"
+#include "common/md5.h"
 #include "fmgr.h"
-#include "libpq/md5.h"
 
 PG_MODULE_MAGIC;
 
index b6ea95061d014a071ca75ce03ccc28aee990c935..821dce3ce7b6b342539626493d66822da79c1b04 100644 (file)
@@ -29,7 +29,7 @@
 #include "commands/dbcommands.h"
 #include "commands/seclabel.h"
 #include "commands/user.h"
-#include "libpq/md5.h"
+#include "common/md5.h"
 #include "miscadmin.h"
 #include "storage/lmgr.h"
 #include "utils/acl.h"
index 09410c4bb19d3e36c79f22d5e836a2ecc32933e3..1bdd8adde24634f2707dd8f39a74edd42a08d6f9 100644 (file)
@@ -14,7 +14,7 @@ include $(top_builddir)/src/Makefile.global
 
 # be-fsstubs is here for historical reasons, probably belongs elsewhere
 
-OBJS = be-fsstubs.o be-secure.o auth.o crypt.o hba.o ip.o md5.o pqcomm.o \
+OBJS = be-fsstubs.o be-secure.o auth.o crypt.o hba.o ifaddr.o pqcomm.o \
        pqformat.o pqmq.o pqsignal.o
 
 ifeq ($(with_openssl),yes)
index fc8b99b444b408edf5aab8b3c7e3b25e6cb76095..d907e6bc91990c4449ccabd796a84f03c4de421d 100644 (file)
 #include <arpa/inet.h>
 #include <unistd.h>
 
+#include "common/ip.h"
+#include "common/md5.h"
 #include "libpq/auth.h"
 #include "libpq/crypt.h"
-#include "libpq/ip.h"
 #include "libpq/libpq.h"
 #include "libpq/pqformat.h"
-#include "libpq/md5.h"
 #include "miscadmin.h"
 #include "replication/walsender.h"
 #include "storage/ipc.h"
index d79f5a2496856073c4b5e5fdea7056f41ca3fb20..d84a18033048698ff288eb46b5f6f19ae64d955f 100644 (file)
@@ -21,8 +21,8 @@
 #endif
 
 #include "catalog/pg_authid.h"
+#include "common/md5.h"
 #include "libpq/crypt.h"
-#include "libpq/md5.h"
 #include "miscadmin.h"
 #include "utils/builtins.h"
 #include "utils/syscache.h"
index d612c11159ee966bcd16f6bd59dba21e589aecb4..f1e9a38c92e6315d2b478884fc11d6b1eef370dd 100644 (file)
@@ -26,7 +26,8 @@
 #include <unistd.h>
 
 #include "catalog/pg_collation.h"
-#include "libpq/ip.h"
+#include "common/ip.h"
+#include "libpq/ifaddr.h"
 #include "libpq/libpq.h"
 #include "postmaster/postmaster.h"
 #include "regex/regex.h"
similarity index 72%
rename from src/backend/libpq/ip.c
rename to src/backend/libpq/ifaddr.c
index 9591ed286242dd0d2155e4d0a6c68d241198a2da..8f7fa2015d41dda185553276b03cf7703e7dc043 100644 (file)
@@ -1,14 +1,14 @@
 /*-------------------------------------------------------------------------
  *
- * ip.c
- *       IPv6-aware network access.
+ * ifaddr.c
+ *       IP netmask calculations, and enumerating network interfaces.
  *
  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       src/backend/libpq/ip.c
+ *       src/backend/libpq/ifaddr.c
  *
  * This file and the IPV6 implementation were initially provided by
  * Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design
@@ -17,8 +17,7 @@
  *-------------------------------------------------------------------------
  */
 
-/* This is intended to be used in both frontend and backend, so use c.h */
-#include "c.h"
+#include "postgres.h"
 
 #include <unistd.h>
 #include <sys/types.h>
@@ -32,8 +31,7 @@
 #include <arpa/inet.h>
 #include <sys/file.h>
 
-#include "libpq/ip.h"
-
+#include "libpq/ifaddr.h"
 
 static int range_sockaddr_AF_INET(const struct sockaddr_in * addr,
                                           const struct sockaddr_in * netaddr,
@@ -45,226 +43,6 @@ static int range_sockaddr_AF_INET6(const struct sockaddr_in6 * addr,
                                                const struct sockaddr_in6 * netmask);
 #endif
 
-#ifdef HAVE_UNIX_SOCKETS
-static int getaddrinfo_unix(const char *path,
-                                const struct addrinfo * hintsp,
-                                struct addrinfo ** result);
-
-static int getnameinfo_unix(const struct sockaddr_un * sa, int salen,
-                                char *node, int nodelen,
-                                char *service, int servicelen,
-                                int flags);
-#endif
-
-
-/*
- *     pg_getaddrinfo_all - get address info for Unix, IPv4 and IPv6 sockets
- */
-int
-pg_getaddrinfo_all(const char *hostname, const char *servname,
-                                  const struct addrinfo * hintp, struct addrinfo ** result)
-{
-       int                     rc;
-
-       /* not all versions of getaddrinfo() zero *result on failure */
-       *result = NULL;
-
-#ifdef HAVE_UNIX_SOCKETS
-       if (hintp->ai_family == AF_UNIX)
-               return getaddrinfo_unix(servname, hintp, result);
-#endif
-
-       /* NULL has special meaning to getaddrinfo(). */
-       rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
-                                        servname, hintp, result);
-
-       return rc;
-}
-
-
-/*
- *     pg_freeaddrinfo_all - free addrinfo structures for IPv4, IPv6, or Unix
- *
- * Note: the ai_family field of the original hint structure must be passed
- * so that we can tell whether the addrinfo struct was built by the system's
- * getaddrinfo() routine or our own getaddrinfo_unix() routine.  Some versions
- * of getaddrinfo() might be willing to return AF_UNIX addresses, so it's
- * not safe to look at ai_family in the addrinfo itself.
- */
-void
-pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo * ai)
-{
-#ifdef HAVE_UNIX_SOCKETS
-       if (hint_ai_family == AF_UNIX)
-       {
-               /* struct was built by getaddrinfo_unix (see pg_getaddrinfo_all) */
-               while (ai != NULL)
-               {
-                       struct addrinfo *p = ai;
-
-                       ai = ai->ai_next;
-                       free(p->ai_addr);
-                       free(p);
-               }
-       }
-       else
-#endif   /* HAVE_UNIX_SOCKETS */
-       {
-               /* struct was built by getaddrinfo() */
-               if (ai != NULL)
-                       freeaddrinfo(ai);
-       }
-}
-
-
-/*
- *     pg_getnameinfo_all - get name info for Unix, IPv4 and IPv6 sockets
- *
- * The API of this routine differs from the standard getnameinfo() definition
- * in two ways: first, the addr parameter is declared as sockaddr_storage
- * rather than struct sockaddr, and second, the node and service fields are
- * guaranteed to be filled with something even on failure return.
- */
-int
-pg_getnameinfo_all(const struct sockaddr_storage * addr, int salen,
-                                  char *node, int nodelen,
-                                  char *service, int servicelen,
-                                  int flags)
-{
-       int                     rc;
-
-#ifdef HAVE_UNIX_SOCKETS
-       if (addr && addr->ss_family == AF_UNIX)
-               rc = getnameinfo_unix((const struct sockaddr_un *) addr, salen,
-                                                         node, nodelen,
-                                                         service, servicelen,
-                                                         flags);
-       else
-#endif
-               rc = getnameinfo((const struct sockaddr *) addr, salen,
-                                                node, nodelen,
-                                                service, servicelen,
-                                                flags);
-
-       if (rc != 0)
-       {
-               if (node)
-                       strlcpy(node, "???", nodelen);
-               if (service)
-                       strlcpy(service, "???", servicelen);
-       }
-
-       return rc;
-}
-
-
-#if defined(HAVE_UNIX_SOCKETS)
-
-/* -------
- *     getaddrinfo_unix - get unix socket info using IPv6-compatible API
- *
- *     Bugs: only one addrinfo is set even though hintsp is NULL or
- *               ai_socktype is 0
- *               AI_CANONNAME is not supported.
- * -------
- */
-static int
-getaddrinfo_unix(const char *path, const struct addrinfo * hintsp,
-                                struct addrinfo ** result)
-{
-       struct addrinfo hints;
-       struct addrinfo *aip;
-       struct sockaddr_un *unp;
-
-       *result = NULL;
-
-       MemSet(&hints, 0, sizeof(hints));
-
-       if (strlen(path) >= sizeof(unp->sun_path))
-               return EAI_FAIL;
-
-       if (hintsp == NULL)
-       {
-               hints.ai_family = AF_UNIX;
-               hints.ai_socktype = SOCK_STREAM;
-       }
-       else
-               memcpy(&hints, hintsp, sizeof(hints));
-
-       if (hints.ai_socktype == 0)
-               hints.ai_socktype = SOCK_STREAM;
-
-       if (hints.ai_family != AF_UNIX)
-       {
-               /* shouldn't have been called */
-               return EAI_FAIL;
-       }
-
-       aip = calloc(1, sizeof(struct addrinfo));
-       if (aip == NULL)
-               return EAI_MEMORY;
-
-       unp = calloc(1, sizeof(struct sockaddr_un));
-       if (unp == NULL)
-       {
-               free(aip);
-               return EAI_MEMORY;
-       }
-
-       aip->ai_family = AF_UNIX;
-       aip->ai_socktype = hints.ai_socktype;
-       aip->ai_protocol = hints.ai_protocol;
-       aip->ai_next = NULL;
-       aip->ai_canonname = NULL;
-       *result = aip;
-
-       unp->sun_family = AF_UNIX;
-       aip->ai_addr = (struct sockaddr *) unp;
-       aip->ai_addrlen = sizeof(struct sockaddr_un);
-
-       strcpy(unp->sun_path, path);
-
-#ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
-       unp->sun_len = sizeof(struct sockaddr_un);
-#endif
-
-       return 0;
-}
-
-/*
- * Convert an address to a hostname.
- */
-static int
-getnameinfo_unix(const struct sockaddr_un * sa, int salen,
-                                char *node, int nodelen,
-                                char *service, int servicelen,
-                                int flags)
-{
-       int                     ret = -1;
-
-       /* Invalid arguments. */
-       if (sa == NULL || sa->sun_family != AF_UNIX ||
-               (node == NULL && service == NULL))
-               return EAI_FAIL;
-
-       if (node)
-       {
-               ret = snprintf(node, nodelen, "%s", "[local]");
-               if (ret == -1 || ret > nodelen)
-                       return EAI_MEMORY;
-       }
-
-       if (service)
-       {
-               ret = snprintf(service, servicelen, "%s", sa->sun_path);
-               if (ret == -1 || ret > servicelen)
-                       return EAI_MEMORY;
-       }
-
-       return 0;
-}
-#endif   /* HAVE_UNIX_SOCKETS */
-
 
 /*
  * pg_range_sockaddr - is addr within the subnet specified by netaddr/netmask ?
index 90b6946b386b1234613d61f8275bdefd6ed49094..bae96bf18fb5a97a69de44f4c5c21ee99ccbca11 100644 (file)
@@ -89,7 +89,7 @@
 #include <mstcpip.h>
 #endif
 
-#include "libpq/ip.h"
+#include "common/ip.h"
 #include "libpq/libpq.h"
 #include "miscadmin.h"
 #include "storage/ipc.h"
index 2f99aea7912a6865ee0fd06f60b4add851ad07d2..8a2ce9134437e4d9e2bf07714a9286de1e194287 100644 (file)
@@ -38,7 +38,7 @@
 #include "access/xact.h"
 #include "catalog/pg_database.h"
 #include "catalog/pg_proc.h"
-#include "libpq/ip.h"
+#include "common/ip.h"
 #include "libpq/libpq.h"
 #include "libpq/pqsignal.h"
 #include "mb/pg_wchar.h"
index a28e215e2d71f0cf65e06c21cf6fe2977c2ab020..eaf3f61ecf43fcede4f27ad2b4cf99adfe2e64e5 100644 (file)
@@ -99,9 +99,9 @@
 #include "access/xlog.h"
 #include "bootstrap/bootstrap.h"
 #include "catalog/pg_control.h"
+#include "common/ip.h"
 #include "lib/ilist.h"
 #include "libpq/auth.h"
-#include "libpq/ip.h"
 #include "libpq/libpq.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
index 3f6987af0489b44bca1396a649ddeb0888772525..dbc557e583852802be5e8ed26cfd170cbeb6f844 100644 (file)
@@ -14,7 +14,7 @@
 
 #include "access/hash.h"
 #include "catalog/pg_type.h"
-#include "libpq/ip.h"
+#include "common/ip.h"
 #include "libpq/libpq-be.h"
 #include "libpq/pqformat.h"
 #include "miscadmin.h"
index 1bba5fa8c816021a8212284b2ba553efee12cb65..5d1ccf51d41d8acac53cd53c14bb30602a88d719 100644 (file)
@@ -16,8 +16,8 @@
 
 #include "access/htup_details.h"
 #include "catalog/pg_type.h"
+#include "common/ip.h"
 #include "funcapi.h"
-#include "libpq/ip.h"
 #include "miscadmin.h"
 #include "pgstat.h"
 #include "storage/proc.h"
index bf7c0cd73566444e092c52e6a7a46499a2d6c17d..582d3e460b29bfbc3488fe80c578c41945248e86 100644 (file)
@@ -21,8 +21,8 @@
 #include "access/tuptoaster.h"
 #include "catalog/pg_collation.h"
 #include "catalog/pg_type.h"
+#include "common/md5.h"
 #include "lib/hyperloglog.h"
-#include "libpq/md5.h"
 #include "libpq/pqformat.h"
 #include "miscadmin.h"
 #include "parser/scansup.h"
index 72b73697a8cbac361673dc1bc6f39711027342a5..a5fa6497662431ff830bbf1963571877db6cbb22 100644 (file)
@@ -6,11 +6,15 @@
 # This makefile generates two outputs:
 #
 #      libpgcommon.a - contains object files with FRONTEND defined,
-#              for use by client application and libraries
+#              for use by client applications
 #
 #      libpgcommon_srv.a - contains object files without FRONTEND defined,
 #              for use only by the backend binaries
 #
+# You can also symlink/copy individual source files from this directory,
+# to compile with different options. (libpq does that, because it needs
+# to use -fPIC on some platforms.)
+#
 # IDENTIFICATION
 #    src/common/Makefile
 #
@@ -36,8 +40,8 @@ override CPPFLAGS += -DVAL_LDFLAGS_EX="\"$(LDFLAGS_EX)\""
 override CPPFLAGS += -DVAL_LDFLAGS_SL="\"$(LDFLAGS_SL)\""
 override CPPFLAGS += -DVAL_LIBS="\"$(LIBS)\""
 
-OBJS_COMMON = config_info.o controldata_utils.o exec.o keywords.o \
-       pg_lzcompress.o pgfnames.o psprintf.o relpath.o rmtree.o \
+OBJS_COMMON = config_info.o controldata_utils.o exec.o ip.o keywords.o \
+       md5.o pg_lzcompress.o pgfnames.o psprintf.o relpath.o rmtree.o \
        string.o username.o wait_error.o
 
 OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o restricted_token.o
diff --git a/src/common/ip.c b/src/common/ip.c
new file mode 100644 (file)
index 0000000..797d910
--- /dev/null
@@ -0,0 +1,260 @@
+/*-------------------------------------------------------------------------
+ *
+ * ip.c
+ *       IPv6-aware network access.
+ *
+ * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ *       src/common/ip.c
+ *
+ * This file and the IPV6 implementation were initially provided by
+ * Nigel Kukard <nkukard@lbsd.net>, Linux Based Systems Design
+ * http://www.lbsd.net.
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef FRONTEND
+#include "postgres.h"
+#else
+#include "postgres_fe.h"
+#endif
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#ifdef HAVE_NETINET_TCP_H
+#include <netinet/tcp.h>
+#endif
+#include <arpa/inet.h>
+#include <sys/file.h>
+
+#include "common/ip.h"
+
+
+
+#ifdef HAVE_UNIX_SOCKETS
+static int getaddrinfo_unix(const char *path,
+                                const struct addrinfo * hintsp,
+                                struct addrinfo ** result);
+
+static int getnameinfo_unix(const struct sockaddr_un * sa, int salen,
+                                char *node, int nodelen,
+                                char *service, int servicelen,
+                                int flags);
+#endif
+
+
+/*
+ *     pg_getaddrinfo_all - get address info for Unix, IPv4 and IPv6 sockets
+ */
+int
+pg_getaddrinfo_all(const char *hostname, const char *servname,
+                                  const struct addrinfo * hintp, struct addrinfo ** result)
+{
+       int                     rc;
+
+       /* not all versions of getaddrinfo() zero *result on failure */
+       *result = NULL;
+
+#ifdef HAVE_UNIX_SOCKETS
+       if (hintp->ai_family == AF_UNIX)
+               return getaddrinfo_unix(servname, hintp, result);
+#endif
+
+       /* NULL has special meaning to getaddrinfo(). */
+       rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
+                                        servname, hintp, result);
+
+       return rc;
+}
+
+
+/*
+ *     pg_freeaddrinfo_all - free addrinfo structures for IPv4, IPv6, or Unix
+ *
+ * Note: the ai_family field of the original hint structure must be passed
+ * so that we can tell whether the addrinfo struct was built by the system's
+ * getaddrinfo() routine or our own getaddrinfo_unix() routine.  Some versions
+ * of getaddrinfo() might be willing to return AF_UNIX addresses, so it's
+ * not safe to look at ai_family in the addrinfo itself.
+ */
+void
+pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo * ai)
+{
+#ifdef HAVE_UNIX_SOCKETS
+       if (hint_ai_family == AF_UNIX)
+       {
+               /* struct was built by getaddrinfo_unix (see pg_getaddrinfo_all) */
+               while (ai != NULL)
+               {
+                       struct addrinfo *p = ai;
+
+                       ai = ai->ai_next;
+                       free(p->ai_addr);
+                       free(p);
+               }
+       }
+       else
+#endif   /* HAVE_UNIX_SOCKETS */
+       {
+               /* struct was built by getaddrinfo() */
+               if (ai != NULL)
+                       freeaddrinfo(ai);
+       }
+}
+
+
+/*
+ *     pg_getnameinfo_all - get name info for Unix, IPv4 and IPv6 sockets
+ *
+ * The API of this routine differs from the standard getnameinfo() definition
+ * in two ways: first, the addr parameter is declared as sockaddr_storage
+ * rather than struct sockaddr, and second, the node and service fields are
+ * guaranteed to be filled with something even on failure return.
+ */
+int
+pg_getnameinfo_all(const struct sockaddr_storage * addr, int salen,
+                                  char *node, int nodelen,
+                                  char *service, int servicelen,
+                                  int flags)
+{
+       int                     rc;
+
+#ifdef HAVE_UNIX_SOCKETS
+       if (addr && addr->ss_family == AF_UNIX)
+               rc = getnameinfo_unix((const struct sockaddr_un *) addr, salen,
+                                                         node, nodelen,
+                                                         service, servicelen,
+                                                         flags);
+       else
+#endif
+               rc = getnameinfo((const struct sockaddr *) addr, salen,
+                                                node, nodelen,
+                                                service, servicelen,
+                                                flags);
+
+       if (rc != 0)
+       {
+               if (node)
+                       strlcpy(node, "???", nodelen);
+               if (service)
+                       strlcpy(service, "???", servicelen);
+       }
+
+       return rc;
+}
+
+
+#if defined(HAVE_UNIX_SOCKETS)
+
+/* -------
+ *     getaddrinfo_unix - get unix socket info using IPv6-compatible API
+ *
+ *     Bugs: only one addrinfo is set even though hintsp is NULL or
+ *               ai_socktype is 0
+ *               AI_CANONNAME is not supported.
+ * -------
+ */
+static int
+getaddrinfo_unix(const char *path, const struct addrinfo * hintsp,
+                                struct addrinfo ** result)
+{
+       struct addrinfo hints;
+       struct addrinfo *aip;
+       struct sockaddr_un *unp;
+
+       *result = NULL;
+
+       MemSet(&hints, 0, sizeof(hints));
+
+       if (strlen(path) >= sizeof(unp->sun_path))
+               return EAI_FAIL;
+
+       if (hintsp == NULL)
+       {
+               hints.ai_family = AF_UNIX;
+               hints.ai_socktype = SOCK_STREAM;
+       }
+       else
+               memcpy(&hints, hintsp, sizeof(hints));
+
+       if (hints.ai_socktype == 0)
+               hints.ai_socktype = SOCK_STREAM;
+
+       if (hints.ai_family != AF_UNIX)
+       {
+               /* shouldn't have been called */
+               return EAI_FAIL;
+       }
+
+       aip = calloc(1, sizeof(struct addrinfo));
+       if (aip == NULL)
+               return EAI_MEMORY;
+
+       unp = calloc(1, sizeof(struct sockaddr_un));
+       if (unp == NULL)
+       {
+               free(aip);
+               return EAI_MEMORY;
+       }
+
+       aip->ai_family = AF_UNIX;
+       aip->ai_socktype = hints.ai_socktype;
+       aip->ai_protocol = hints.ai_protocol;
+       aip->ai_next = NULL;
+       aip->ai_canonname = NULL;
+       *result = aip;
+
+       unp->sun_family = AF_UNIX;
+       aip->ai_addr = (struct sockaddr *) unp;
+       aip->ai_addrlen = sizeof(struct sockaddr_un);
+
+       strcpy(unp->sun_path, path);
+
+#ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
+       unp->sun_len = sizeof(struct sockaddr_un);
+#endif
+
+       return 0;
+}
+
+/*
+ * Convert an address to a hostname.
+ */
+static int
+getnameinfo_unix(const struct sockaddr_un * sa, int salen,
+                                char *node, int nodelen,
+                                char *service, int servicelen,
+                                int flags)
+{
+       int                     ret = -1;
+
+       /* Invalid arguments. */
+       if (sa == NULL || sa->sun_family != AF_UNIX ||
+               (node == NULL && service == NULL))
+               return EAI_FAIL;
+
+       if (node)
+       {
+               ret = snprintf(node, nodelen, "%s", "[local]");
+               if (ret == -1 || ret > nodelen)
+                       return EAI_MEMORY;
+       }
+
+       if (service)
+       {
+               ret = snprintf(service, servicelen, "%s", sa->sun_path);
+               if (ret == -1 || ret > servicelen)
+                       return EAI_MEMORY;
+       }
+
+       return 0;
+}
+#endif   /* HAVE_UNIX_SOCKETS */
similarity index 98%
rename from src/backend/libpq/md5.c
rename to src/common/md5.c
index 5af54e6c5611875a8b3d1e24e808b436e14d9812..6dad16529cedc4716bc859161d3c2596ed840fa2 100644 (file)
  *     Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       src/backend/libpq/md5.c
+ *       src/common/md5.c
  */
 
-/* This is intended to be used in both frontend and backend, so use c.h */
-#include "c.h"
+#ifndef FRONTEND
+#include "postgres.h"
+#else
+#include "postgres_fe.h"
+#endif
 
-#include "libpq/md5.h"
+#include "common/md5.h"
 
 
 /*
similarity index 65%
rename from src/include/libpq/ip.h
rename to src/include/common/ip.h
index ce9bc6e225318cbacfdd1e750562c2a2dfe1a30b..36e051132410391122476767b51eca68d138c32b 100644 (file)
@@ -3,12 +3,11 @@
  * ip.h
  *       Definitions for IPv6-aware network access.
  *
- * These definitions are used by both frontend and backend code.  Be careful
- * what you include here!
+ * These definitions are used by both frontend and backend code.
  *
  * Copyright (c) 2003-2016, PostgreSQL Global Development Group
  *
- * src/include/libpq/ip.h
+ * src/include/common/ip.h
  *
  *-------------------------------------------------------------------------
  */
 #define IS_AF_UNIX(fam) (0)
 #endif
 
-typedef void (*PgIfAddrCallback) (struct sockaddr * addr,
-                                                                                         struct sockaddr * netmask,
-                                                                                         void *cb_data);
-
 extern int pg_getaddrinfo_all(const char *hostname, const char *servname,
                                   const struct addrinfo * hintp,
                                   struct addrinfo ** result);
@@ -39,13 +34,4 @@ extern int pg_getnameinfo_all(const struct sockaddr_storage * addr, int salen,
                                   char *service, int servicelen,
                                   int flags);
 
-extern int pg_range_sockaddr(const struct sockaddr_storage * addr,
-                                 const struct sockaddr_storage * netaddr,
-                                 const struct sockaddr_storage * netmask);
-
-extern int pg_sockaddr_cidr_mask(struct sockaddr_storage * mask,
-                                         char *numbits, int family);
-
-extern int     pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data);
-
 #endif   /* IP_H */
similarity index 96%
rename from src/include/libpq/md5.h
rename to src/include/common/md5.h
index f3eec8b4f4ffe252d6ee7c6296c5866cbb0a701d..4a0432076a03375302d8bc0c6d5ac61e81f11159 100644 (file)
@@ -9,7 +9,7 @@
  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * src/include/libpq/md5.h
+ * src/include/common/md5.h
  *
  *-------------------------------------------------------------------------
  */
diff --git a/src/include/libpq/ifaddr.h b/src/include/libpq/ifaddr.h
new file mode 100644 (file)
index 0000000..40094a6
--- /dev/null
@@ -0,0 +1,30 @@
+/*-------------------------------------------------------------------------
+ *
+ * ifaddr.h
+ *       IP netmask calculations, and enumerating network interfaces.
+ *
+ * Copyright (c) 2003-2016, PostgreSQL Global Development Group
+ *
+ * src/include/libpq/ifaddr.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef IFADDR_H
+#define IFADDR_H
+
+#include "libpq/pqcomm.h"              /* pgrminclude ignore */
+
+typedef void (*PgIfAddrCallback) (struct sockaddr * addr,
+                                                                                         struct sockaddr * netmask,
+                                                                                         void *cb_data);
+
+extern int pg_range_sockaddr(const struct sockaddr_storage * addr,
+                                 const struct sockaddr_storage * netaddr,
+                                 const struct sockaddr_storage * netmask);
+
+extern int pg_sockaddr_cidr_mask(struct sockaddr_storage * mask,
+                                         char *numbits, int family);
+
+extern int     pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data);
+
+#endif   /* IFADDR_H */
index 0b4065ed8f0143c0abc3d553568291d0aa2d0fb0..b1789eb35e3cac2a8a55a8b35d58d446bd935145 100644 (file)
@@ -39,10 +39,10 @@ OBJS += chklocale.o inet_net_ntop.o noblock.o pgstrcasecmp.o pqsignal.o \
        thread.o
 # libpgport C files that are needed if identified by configure
 OBJS += $(filter crypt.o getaddrinfo.o getpeereid.o inet_aton.o open.o system.o snprintf.o strerror.o strlcpy.o win32error.o win32setlocale.o, $(LIBOBJS))
-# backend/libpq
-OBJS += ip.o md5.o
-# utils/mb
+# src/backend/utils/mb
 OBJS += encnames.o wchar.o
+# src/common
+OBJS += ip.o md5.o
 
 ifeq ($(with_openssl),yes)
 OBJS += fe-secure-openssl.o
@@ -96,7 +96,7 @@ backend_src = $(top_srcdir)/src/backend
 chklocale.c crypt.c getaddrinfo.c getpeereid.c inet_aton.c inet_net_ntop.c noblock.c open.c system.c pgsleep.c pgstrcasecmp.c pqsignal.c snprintf.c strerror.c strlcpy.c thread.c win32error.c win32setlocale.c: % : $(top_srcdir)/src/port/%
        rm -f $@ && $(LN_S) $< .
 
-ip.c md5.c: % : $(backend_src)/libpq/%
+ip.c md5.c: % : $(top_srcdir)/src/common/%
        rm -f $@ && $(LN_S) $< .
 
 encnames.c wchar.c: % : $(backend_src)/utils/mb/%
index d23726215b7fcce5ab5b0412dbf81d5b8f5148b7..404bc93306d00ff2b8c219ce929b7e0051852278 100644 (file)
@@ -38,9 +38,9 @@
 #include <pwd.h>
 #endif
 
+#include "common/md5.h"
 #include "libpq-fe.h"
 #include "fe-auth.h"
-#include "libpq/md5.h"
 
 
 #ifdef ENABLE_GSS
index 76b61bdc251904f204b759130ad893741cb97510..9668b52103f76f37aa6e45eed0429315aa6ae0d5 100644 (file)
@@ -72,7 +72,7 @@ static int ldapServiceLookup(const char *purl, PQconninfoOption *options,
                                  PQExpBuffer errorMessage);
 #endif
 
-#include "libpq/ip.h"
+#include "common/ip.h"
 #include "mb/pg_wchar.h"
 
 #ifndef FD_CLOEXEC
index 231f3889015df915b06d6cdd0bb03bbdf2fc940a..eed6af41e67c7e2891b9b4bb9aa786bf270e3e8e 100644 (file)
@@ -20,8 +20,8 @@ OBJS = test_ifaddrs.o
 
 all: test_ifaddrs
 
-test_ifaddrs: test_ifaddrs.o $(libpq_backend_dir)/ip.o
-       $(CC) $(CFLAGS) test_ifaddrs.o $(libpq_backend_dir)/ip.o $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
+test_ifaddrs: test_ifaddrs.o $(libpq_backend_dir)/ifaddr.o
+       $(CC) $(CFLAGS) test_ifaddrs.o $(libpq_backend_dir)/ifaddr.o $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
 
 clean distclean maintainer-clean:
        rm -f test_ifaddrs$(X) $(OBJS)
index 48d184c84a2b5fd6afdbcbf7ea065b63424a7295..80b9bb026604d06d3564e827dd935837d7ee4558 100644 (file)
@@ -12,7 +12,7 @@
 #include <netinet/in.h>
 #include <sys/socket.h>
 
-#include "libpq/ip.h"
+#include "libpq/ifaddr.h"
 
 
 static void
index 67467286161982c18b7d1681c1c7c620df6acca4..b3ed1f56e21b80b1207d62faa210fa99889b100f 100644 (file)
@@ -110,8 +110,8 @@ sub mkvcbuild
        }
 
        our @pgcommonallfiles = qw(
-         config_info.c controldata_utils.c exec.c keywords.c
-         pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
+         config_info.c controldata_utils.c exec.c ip.c keywords.c
+         md5.c pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
          string.c username.c wait_error.c);
 
        our @pgcommonfrontendfiles = (