PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 <arpa/inet.h>
12 #include <netinet/in.h>
13 #include <sys/socket.h>
14 
15 #include "libpq/ifaddr.h"
16 
17 
18 static void
19 print_addr(struct sockaddr * addr)
20 {
21  char buffer[256];
22  int ret,
23  len;
24 
25  switch (addr->sa_family)
26  {
27  case AF_INET:
28  len = sizeof(struct sockaddr_in);
29  break;
30 #ifdef HAVE_IPV6
31  case AF_INET6:
32  len = sizeof(struct sockaddr_in6);
33  break;
34 #endif
35  default:
36  len = sizeof(struct sockaddr_storage);
37  break;
38  }
39 
40  ret = getnameinfo(addr, len, buffer, sizeof(buffer), NULL, 0,
42  if (ret != 0)
43  printf("[unknown: family %d]", addr->sa_family);
44  else
45  printf("%s", buffer);
46 }
47 
48 static void
49 callback(struct sockaddr * addr, struct sockaddr * mask, void *unused)
50 {
51  printf("addr: ");
52  print_addr(addr);
53  printf(" mask: ");
54  print_addr(mask);
55  printf("\n");
56 }
57 
58 int
59 main(int argc, char *argv[])
60 {
61 #ifdef WIN32
62  WSADATA wsaData;
63 
64  if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
65  {
66  fprintf(stderr, "WSAStartup failed\n");
67  return 1;
68  }
69 #endif
70 
72  fprintf(stderr, "pg_foreach_ifaddr failed: %s\n", strerror(errno));
73  return 0;
74 }
#define NI_NUMERICHOST
Definition: getaddrinfo.h:80
static void print_addr(struct sockaddr *addr)
Definition: test_ifaddrs.c:19
int main(int argc, char *argv[])
Definition: test_ifaddrs.c:59
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:49
#define NULL
Definition: c.h:226
const char * strerror(int errnum)
Definition: strerror.c:19
#define getnameinfo
Definition: getaddrinfo.h:153