How to resolve IPv4 address from IPv4 mapped IPv6 address?

元气小坏坏 提交于 2021-02-08 10:00:12

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/14272656/how-to-resolve-ipv4-address-from-ipv4-mapped-ipv6-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!