PostgreSQL Source Code  git master
test_ifaddrs.c
Go to the documentation of this file.
1 /*
2  * src/tools/ifaddrs/test_ifaddrs.c
3  *
4  *
5  * test_ifaddrs.c
6  * test pg_foreach_ifaddr()
7  */
8 
9 #include "postgres.h"
10 
11 #include <netinet/in.h>
12 #include <arpa/inet.h>
13 
14 #include "libpq/ifaddr.h"
15 
16 
17 static void
18 print_addr(struct sockaddr *addr)
19 {
20  char buffer[256];
21  int ret,
22  len;
23 
24  switch (addr->sa_family)
25  {
26  case AF_INET:
27  len = sizeof(struct sockaddr_in);
28  break;
29 #ifdef HAVE_IPV6
30  case AF_INET6:
31  len = sizeof(struct sockaddr_in6);
32  break;
33 #endif
34  default:
35  len = sizeof(struct sockaddr_storage);
36  break;
37  }
38 
39  ret = getnameinfo(addr, len, buffer, sizeof(buffer), NULL, 0,
41  if (ret != 0)
42  printf("[unknown: family %d]", addr->sa_family);
43  else
44  printf("%s", buffer);
45 }
46 
47 static void
48 callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
49 {
50  printf("addr: ");
51  print_addr(addr);
52  printf(" mask: ");
53  print_addr(mask);
54  printf("\n");
55 }
56 
57 int
58 main(int argc, char *argv[])
59 {
60 #ifdef WIN32
61  WSADATA wsaData;
62 
63  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
64  {
65  fprintf(stderr, "WSAStartup failed\n");
66  return 1;
67  }
68 #endif
69 
70  if (pg_foreach_ifaddr(callback, NULL) < 0)
71  fprintf(stderr, "pg_foreach_ifaddr failed: %s\n", strerror(errno));
72  return 0;
73 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:78
static void print_addr(struct sockaddr *addr)
Definition: test_ifaddrs.c:18
int main(int argc, char *argv[])
Definition: test_ifaddrs.c:58
int pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
Definition: ifaddr.c:559
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
const char * strerror(int errnum)
Definition: strerror.c:19
#define getnameinfo
Definition: getaddrinfo.h:151