1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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;
}
|