diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:15:05 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:15:05 +0000 |
commit | 46651ce6fe013220ed397add242004d764fc0153 (patch) | |
tree | 6e5299f990f88e60174a1d3ae6e48eedd2688b2b /src/tools/ifaddrs | |
parent | Initial commit. (diff) | |
download | postgresql-14-46651ce6fe013220ed397add242004d764fc0153.tar.xz postgresql-14-46651ce6fe013220ed397add242004d764fc0153.zip |
Adding upstream version 14.5.upstream/14.5upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/ifaddrs')
-rw-r--r-- | src/tools/ifaddrs/.gitignore | 1 | ||||
-rw-r--r-- | src/tools/ifaddrs/Makefile | 28 | ||||
-rw-r--r-- | src/tools/ifaddrs/README | 12 | ||||
-rw-r--r-- | src/tools/ifaddrs/test_ifaddrs.c | 73 |
4 files changed, 114 insertions, 0 deletions
diff --git a/src/tools/ifaddrs/.gitignore b/src/tools/ifaddrs/.gitignore new file mode 100644 index 0000000..2e90032 --- /dev/null +++ b/src/tools/ifaddrs/.gitignore @@ -0,0 +1 @@ +/test_ifaddrs diff --git a/src/tools/ifaddrs/Makefile b/src/tools/ifaddrs/Makefile new file mode 100644 index 0000000..7cce9a5 --- /dev/null +++ b/src/tools/ifaddrs/Makefile @@ -0,0 +1,28 @@ +#------------------------------------------------------------------------- +# +# Makefile for src/tools/ifaddrs +# +# Copyright (c) 2003-2021, PostgreSQL Global Development Group +# +# src/tools/ifaddrs/Makefile +# +#------------------------------------------------------------------------- + +subdir = src/tools/ifaddrs +top_builddir = ../../.. +include $(top_builddir)/src/Makefile.global + +libpq_backend_dir = $(top_builddir)/src/backend/libpq + +override CPPFLAGS := -I$(libpq_backend_dir) $(CPPFLAGS) + +OBJS = \ + test_ifaddrs.o + +all: test_ifaddrs + +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) diff --git a/src/tools/ifaddrs/README b/src/tools/ifaddrs/README new file mode 100644 index 0000000..9f92803 --- /dev/null +++ b/src/tools/ifaddrs/README @@ -0,0 +1,12 @@ +src/tools/ifaddrs/README + +test_ifaddrs +============ + +This program prints the addresses and netmasks of all the IPv4 and IPv6 +interfaces on the local machine. It is useful for testing that this +functionality works on various platforms. If "samehost" and "samenet" +in pg_hba.conf don't seem to work right, run this program to see what +is happening. + +Usage: test_ifaddrs diff --git a/src/tools/ifaddrs/test_ifaddrs.c b/src/tools/ifaddrs/test_ifaddrs.c new file mode 100644 index 0000000..b8dbb84 --- /dev/null +++ b/src/tools/ifaddrs/test_ifaddrs.c @@ -0,0 +1,73 @@ +/* + * src/tools/ifaddrs/test_ifaddrs.c + * + * + * test_ifaddrs.c + * test pg_foreach_ifaddr() + */ + +#include "postgres.h" + +#include <netinet/in.h> +#include <arpa/inet.h> + +#include "libpq/ifaddr.h" + + +static void +print_addr(struct sockaddr *addr) +{ + char buffer[256]; + int ret, + len; + + switch (addr->sa_family) + { + case AF_INET: + len = sizeof(struct sockaddr_in); + break; +#ifdef HAVE_IPV6 + case AF_INET6: + len = sizeof(struct sockaddr_in6); + break; +#endif + default: + len = sizeof(struct sockaddr_storage); + break; + } + + ret = getnameinfo(addr, len, buffer, sizeof(buffer), NULL, 0, + NI_NUMERICHOST); + if (ret != 0) + printf("[unknown: family %d]", addr->sa_family); + else + printf("%s", buffer); +} + +static void +callback(struct sockaddr *addr, struct sockaddr *mask, void *unused) +{ + printf("addr: "); + print_addr(addr); + printf(" mask: "); + print_addr(mask); + printf("\n"); +} + +int +main(int argc, char *argv[]) +{ +#ifdef WIN32 + WSADATA wsaData; + + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) + { + fprintf(stderr, "WSAStartup failed\n"); + return 1; + } +#endif + + if (pg_foreach_ifaddr(callback, NULL) < 0) + fprintf(stderr, "pg_foreach_ifaddr failed: %s\n", strerror(errno)); + return 0; +} |