Linux 扩展 IPv6 地址,以便我可以将其打印到标准输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3727421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:10:51  来源:igfitidea点击:

expand an IPv6 address so I can print it to stdout

clinuxipv6

提问by Jessica

I am using getifaddrs() and inet_ntop() to get the ip addresses on the system. When the system is set to IPv6 the address returned is in the shortened version (using :: for zeros). Is there any way to expand that address to a full one?

我正在使用 getifaddrs() 和 inet_ntop() 来获取系统上的 IP 地址。当系统设置为 IPv6 时,返回的地址是缩短版本(使用 :: 表示零)。有没有办法将该地址扩展为完整地址?

This is the code I am using:

这是我正在使用的代码:

struct ifaddrs *myaddrs, *ifa;
void *in_addr;
char buf[64];

if(getifaddrs(&myaddrs) != 0)
{
    perror("getifaddrs");
    exit(1);
}

for (ifa = myaddrs; ifa != NULL; ifa = ifa->ifa_next)
{
    if (ifa->ifa_addr == NULL)
        continue;
    if (!(ifa->ifa_flags & IFF_UP))
        continue;

    switch (ifa->ifa_addr->sa_family)
    {
        case AF_INET:
        {
            struct sockaddr_in *s4 = (struct sockaddr_in *)ifa->ifa_addr;
            in_addr = &s4->sin_addr;
            break;
        }

        case AF_INET6:
        {
            struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)ifa->ifa_addr;
            in_addr = &s6->sin6_addr;
            break;
        }

        default:
            continue;
    }

    if (!inet_ntop(ifa->ifa_addr->sa_family, in_addr, buf, sizeof(buf)))
    {
        printf("%s: inet_ntop failed!\n", ifa->ifa_name);
    }
    else
    {
        printf("IP address: %s\n", buf);
    }
}

freeifaddrs(myaddrs);

Code is greatly appreciated.

非常感谢代码。

EDIT:
Since this is apparently very hard to comprehend I will give you an example:

编辑:
由于这显然很难理解,我会给你一个例子:

If I get abcd:12::3 I need to expand it to abcd:0012:0000:0000:0000:0000:0000:0003
The reason? because it's part of the requirements. Simple as that.

如果我得到 abcd:12::3 我需要将它扩展为 abcd:0012:0000:0000:0000:0000:0000:0003
原因?因为这是要求的一部分。就那么简单。

采纳答案by nategoose

void ipv6_to_str_unexpanded(char * str, const struct in6_addr * addr) {
   sprintf(str, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
                 (int)addr->s6_addr[0], (int)addr->s6_addr[1],
                 (int)addr->s6_addr[2], (int)addr->s6_addr[3],
                 (int)addr->s6_addr[4], (int)addr->s6_addr[5],
                 (int)addr->s6_addr[6], (int)addr->s6_addr[7],
                 (int)addr->s6_addr[8], (int)addr->s6_addr[9],
                 (int)addr->s6_addr[10], (int)addr->s6_addr[11],
                 (int)addr->s6_addr[12], (int)addr->s6_addr[13],
                 (int)addr->s6_addr[14], (int)addr->s6_addr[15]);
}

回答by Arun Raj

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

            struct in6_addrr
            {
                unsigned char addr[16];
            };

            void ipv6_expander(const struct in6_addr * addr)
            {
                char str[40];
                sprintf(str,"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
                (int)addr->s6_addr[0], (int)addr->s6_addr[1],
                (int)addr->s6_addr[2], (int)addr->s6_addr[3],
                (int)addr->s6_addr[4], (int)addr->s6_addr[5],
                (int)addr->s6_addr[6], (int)addr->s6_addr[7],
                (int)addr->s6_addr[8], (int)addr->s6_addr[9],
                (int)addr->s6_addr[10], (int)addr->s6_addr[11],
                (int)addr->s6_addr[12], (int)addr->s6_addr[13],
                (int)addr->s6_addr[14], (int)addr->s6_addr[15]);
                 printf("\nExpanded ipv6 Addr %s\n",str);
            }

            int main(int argc,char *argv[])
            {
                struct in6_addrr ipv6;
                printf("\nGiven IPv6 Addr %s\n",argv[1]);
                if(inet_pton(AF_INET6,argv[1],&ipv6.addr))
                {
                    ipv6_expander(&ipv6.addr);
                }
                else
                {
                    printf("\n error\n");
                }
                return;
            }