IPv6 Address as text (C)

Andrey V. Elsukov bu7cher at yandex.ru
Wed Dec 9 16:52:15 UTC 2015


On 09.12.15 18:25, Ken Moore wrote:

Simple example:

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include <err.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>

int
main(int argc, char **argv)
{
        char buf[INET6_ADDRSTRLEN];
        struct ifaddrs *ifap, *ifa;
        int error;

        if (getifaddrs(&ifap) != 0)
                err(1, "getifaddrs");
        for (ifa = ifap; ifa; ifa = ifa->ifa_next)
                if (ifa->ifa_addr->sa_family == AF_INET6) {
                        error = getnameinfo(ifa->ifa_addr,
                            ifa->ifa_addr->sa_len, buf, sizeof(buf),
                            NULL, 0, NI_NUMERICHOST);
                        if (error != 0)
                                err(1, "%s", gai_strerror(error));
                        printf("%s\n", buf);
                }
        freeifaddrs(ifap);
        return (0);
}

-- 
WBR, Andrey V. Elsukov

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 538 bytes
Desc: OpenPGP digital signature
URL: <http://lists.freebsd.org/pipermail/freebsd-net/attachments/20151209/362af00a/attachment.sig>


More information about the freebsd-net mailing list