0
votes

How to get the IPv4 address from IPv4-mapped IPv6 addresses?

For e.g I have an IP address ::FFFF:129.144.52.38. from this, I need to extract 129.144.52.38. Is there any API for this purpose?

I can identify IPv6 or IPv4 address family by using the following function

int getaddrfamily(const char *addr)
{
    struct addrinfo hint, *info =0;
    memset(&hint, 0, sizeof(hint));
    hint.ai_family = AF_UNSPEC;
    // Uncomment this to disable DNS lookup
    //hint.ai_flags = AI_NUMERICHOST;
    int ret = getaddrinfo(addr, 0, &hint, &info);
    if (ret)
        return -1;
    int result = info->ai_family;
    freeaddrinfo(info);
    return result;
}

If I give a IPv4 mapped IPv6 address, then how it's possible to identify if it's a mapped adress? Is there any socket API to extract IPv4 from a mapped IPv6 address?

2
This question may be of interest: stackoverflow.com/q/13949750/175849Steve-o

2 Answers

3
votes

Try something like this:

#ifndef IN6_IS_ADDR_V4MAPPED
#define IN6_IS_ADDR_V4MAPPED(a) \
       ((((a)->s6_words[0]) == 0) && \
        (((a)->s6_words[1]) == 0) && \
        (((a)->s6_word[2]) == 0) && \
        (((a)->s6_word[3]) == 0) && \
        (((a)->s6_word[4]) == 0) && \
        (((a)->s6_word[5]) == 0xFFFF))
#endif

unsigned long getIPv4addr(const char *addr)
{
    struct addrinfo hint, *info = 0;
    unsigned long result = INADDR_NONE;
    memset(&hint, 0, sizeof(hint));
    hint.ai_family = AF_UNSPEC;
    // Uncomment this to disable DNS lookup
    //hint.ai_flags = AI_NUMERICHOST;
    if (getaddrinfo(addr, 0, &hint, &info) == 0)
    {
        switch (info->ai_family)
        {
            case AF_INET:
            {
                struct sockaddr_in *addr = (struct sockaddr_in*)(info->ai_addr);
                result = addr->sin_addr.s_addr;
                break;
            }

            case AF_INET6:
            {
                struct sockaddr_in6 *addr = (struct sockaddr_in6*)(info->ai_addr);
                if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr))
                    result = ((in_addr*)(addr->sin6_addr.s6_addr+12))->s_addr;
                break;
            }
        }
        freeaddrinfo(info);
    }
    return result;
}
1
votes

You simply extract the four last bytes from the IPv6 address, combine them into a single 32-bit number, and you have your IPv4 address.

Of course you need to check that it really is a IPv4-mapped address first.