Header And Logo

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

Functions

test_ifaddrs.c File Reference

#include "postgres.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "libpq/ip.h"
Include dependency graph for test_ifaddrs.c:

Go to the source code of this file.

Functions

static void print_addr (struct sockaddr *addr)
static void callback (struct sockaddr *addr, struct sockaddr *mask, void *unused)
int main (int argc, char *argv[])

Function Documentation

static void callback ( struct sockaddr *  addr,
struct sockaddr *  mask,
void *  unused 
) [static]
int main ( int  argc,
char *  argv[] 
)

Definition at line 59 of file test_ifaddrs.c.

References callback(), NULL, pg_foreach_ifaddr(), and strerror().

{
#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;
}

static void print_addr ( struct sockaddr *  addr  )  [static]

Definition at line 19 of file test_ifaddrs.c.

References getnameinfo, NI_NUMERICHOST, and NULL.

Referenced by callback().

{
    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);
}