Header And Logo

PostgreSQL
| The world's most advanced open source database.

test_ifaddrs.c

Go to the documentation of this file.
00001 /*
00002  * src/tools/ifaddrs/test_ifaddrs.c
00003  *
00004  *
00005  *  test_ifaddrs.c
00006  *      test pg_foreach_ifaddr()
00007  */
00008 
00009 #include "postgres.h"
00010 
00011 #include <arpa/inet.h>
00012 #include <netinet/in.h>
00013 #include <sys/socket.h>
00014 
00015 #include "libpq/ip.h"
00016 
00017 
00018 static void
00019 print_addr(struct sockaddr * addr)
00020 {
00021     char        buffer[256];
00022     int         ret,
00023                 len;
00024 
00025     switch (addr->sa_family)
00026     {
00027         case AF_INET:
00028             len = sizeof(struct sockaddr_in);
00029             break;
00030 #ifdef HAVE_IPV6
00031         case AF_INET6:
00032             len = sizeof(struct sockaddr_in6);
00033             break;
00034 #endif
00035         default:
00036             len = sizeof(struct sockaddr_storage);
00037             break;
00038     }
00039 
00040     ret = getnameinfo(addr, len, buffer, sizeof(buffer), NULL, 0,
00041                       NI_NUMERICHOST);
00042     if (ret != 0)
00043         printf("[unknown: family %d]", addr->sa_family);
00044     else
00045         printf("%s", buffer);
00046 }
00047 
00048 static void
00049 callback(struct sockaddr * addr, struct sockaddr * mask, void *unused)
00050 {
00051     printf("addr: ");
00052     print_addr(addr);
00053     printf("  mask: ");
00054     print_addr(mask);
00055     printf("\n");
00056 }
00057 
00058 int
00059 main(int argc, char *argv[])
00060 {
00061 #ifdef WIN32
00062     WSADATA     wsaData;
00063 
00064     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
00065     {
00066         fprintf(stderr, "WSAStartup failed\n");
00067         return 1;
00068     }
00069 #endif
00070 
00071     if (pg_foreach_ifaddr(callback, NULL) < 0)
00072         fprintf(stderr, "pg_foreach_ifaddr failed: %s\n", strerror(errno));
00073     return 0;
00074 }