DEV Community

kyorohiro (kiyohiro kawamura)
kyorohiro (kiyohiro kawamura)

Posted on

Memo : Get hostname from IPv4 and IPv6 At C++

This is just just a note for me...
for my bittorrent client tools

For IP v4

// 
// copy from https://stackoverflow.com/questions/28566424/linux-networking-gethostbyaddr 
//
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
    struct sockaddr_in sa;    /* input */
    socklen_t len;         /* input */
    char hbuf[NI_MAXHOST];

    memset(&sa, 0, sizeof(struct sockaddr_in));

    /* For IPv4*/
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = inet_addr("8.8.8.8");
    len = sizeof(struct sockaddr_in);

    if (getnameinfo((struct sockaddr *) &sa, len, hbuf, sizeof(hbuf), 
        NULL, 0, NI_NAMEREQD)) {
        printf("could not resolve hostname\n");
    }
    else {
        printf("host=%s\n", hbuf);
    }

    return 0;                                                  
}

Enter fullscreen mode Exit fullscreen mode

For IP v6

#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main()
{
    struct sockaddr_in6 sa6;    
    char hbuf[NI_MAXHOST];
    memset(&sa6, 0, sizeof(struct sockaddr_in6));

    sa6.sin6_family = AF_INET6;
    in6_addr addr6;
    int s = inet_pton(AF_INET6, "::1",&addr6);
    sa6.sin6_addr = addr6;

    int len = sizeof(struct sockaddr_in6);
    if (getnameinfo((struct sockaddr *) &sa6, len, hbuf, sizeof(hbuf), 
        NULL, 0, NI_NAMEREQD)) {
        printf("could not resolve hostname\n");
    }
    else {
        printf("host=%s\n", hbuf);
    }

    return 0;                                                  
}
Enter fullscreen mode Exit fullscreen mode

CODE

https://github.com/kyorohiro/hello_libtorren
app/main_get_host_from_ipv6.cpp

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay