diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:46:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 19:46:48 +0000 |
commit | 311bcfc6b3acdd6fd152798c7f287ddf74fa2a98 (patch) | |
tree | 0ec307299b1dada3701e42f4ca6eda57d708261e /src/tools/ifaddrs/test_ifaddrs.c | |
parent | Initial commit. (diff) | |
download | postgresql-15-311bcfc6b3acdd6fd152798c7f287ddf74fa2a98.tar.xz postgresql-15-311bcfc6b3acdd6fd152798c7f287ddf74fa2a98.zip |
Adding upstream version 15.4.upstream/15.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/ifaddrs/test_ifaddrs.c')
-rw-r--r-- | src/tools/ifaddrs/test_ifaddrs.c | 73 |
1 files changed, 73 insertions, 0 deletions
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; +} |