我一直在编写一个程序,列出了我的计算机的所有IP地址,网络掩码和接口名称,我遇到了打印网络掩码的问题。这个问题似乎只发生在IPv6上。这是代码:
int i;
struct ifaddrs *ifap, *ifa;
getifaddrs(&ifap);
char *net_mask = NULL;
int buffer_size = 200;
char *ifa_addr = NULL;
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
{
struct sockaddr_in *ifan = (struct sockaddr_in *) ifa->ifa_netmask; //interface netmask
struct sockaddr_in *ifaa_in = (struct sockaddr_in*)ifa->ifa_addr; //interface address
//same as above with the exception that it is for ipv6
struct sockaddr_in6 *ifan6 = (struct sockaddr_in6 *) ifa->ifa_netmask;
struct sockaddr_in6 *ifaa_in6 = (struct sockaddr_in6*)ifa->ifa_addr;
char *prevNet_mask = net_mask;
net_mask = (char*)calloc(buffer_size, sizeof(char*));
ifa_addr = (char*)calloc(buffer_size, sizeof(char*));
inet_ntop(ifa->ifa_addr->sa_family,ifa->ifa_addr->sa_family == AF_INET?(void*)&ifan->sin_addr : (void*)&ifan6->sin6_addr, net_mask, buffer_size);
inet_ntop(ifa->ifa_addr->sa_family, ifa->ifa_addr->sa_family == AF_INET?(void*)&ifaa_in->sin_addr: (void*)&ifaa_in6->sin6_addr ,ifa_addr , buffer_size);
printf("Interface: %s\tNetMask: %s", ifa->ifa_name, net_mask);
//space evenly
int num_spaces = strlen(net_mask) - 40;
if(num_spaces < 0) num_spaces *= -1;
for(i = 0; i <num_spaces;i++) printf(" ");
printf("Address: %s\n",ifa_addr);
}
这是我的输出:
Interface: {BE3A9EA6-72AB-40FD-9036-9AC6BA4D75CF} NetMask: ffff:ffff:ffff:ffff:: Address: fe80::9cfb:3ebd:d866:e214
Interface: {BE3A9EA6-72AB-40FD-9036-9AC6BA4D75CF} NetMask: 255.255.0.0 Address: 169.254.226.20
Interface: {AA4D555F-C490-40DE-9F61-8E5AB3252389} NetMask: ffff:ffff:ffff:ffff:: Address: fe80::e994:389a:9ab4:1e45
Interface: {AA4D555F-C490-40DE-9F61-8E5AB3252389} NetMask: 255.255.0.0 Address: 169.254.30.69
Interface: {E1AE93D7-66CC-4651-A470-1445CC777F36} NetMask: ffff:ffff:ffff:ffff:: Address: fe80::5837:88eb:5801:273e
Interface: {E1AE93D7-66CC-4651-A470-1445CC777F36} NetMask: 255.255.0.0 Address: 169.254.39.62
Interface: {A251C284-7DC3-4F56-AB6A-8A4E4E218B6F} NetMask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff Address: 2602:306:8390:10f0::43
Interface: {A251C284-7DC3-4F56-AB6A-8A4E4E218B6F} NetMask: ffff:ffff:ffff:ffff:: Address: 2602:306:8390:10f0:6810:b0eb:1287:3138
Interface: {A251C284-7DC3-4F56-AB6A-8A4E4E218B6F} NetMask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff Address: 2602:306:8390:10f0:116d:50d0:4291:c8ab
Interface: {A251C284-7DC3-4F56-AB6A-8A4E4E218B6F} NetMask: ffff:ffff:ffff:ffff:: Address: fe80::6810:b0eb:1287:3138
Interface: {A251C284-7DC3-4F56-AB6A-8A4E4E218B6F} NetMask: 255.255.255.0 Address: 192.168.1.70
Interface: {D301A0B1-F567-11E5-A797-806E6F6E6963} NetMask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff Address: ::1
Interface: {D301A0B1-F567-11E5-A797-806E6F6E6963} NetMask: 255.0.0.0 Address: 127.0.0.1
Interface: {01CEB8B7-97FE-40AC-A5D4-47A61834A467} NetMask: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff Address: fe80::5efe:c0a8:146
Interface: {4ED4A6C1-0496-4F0B-AD77-395C9F275D41} NetMask: ffff:ffff:ffff:ffff:: Address: 2001:0:9d38:90d7:2c9a:771:97c6:fef0
Interface: {4ED4A6C1-0496-4F0B-AD77-395C9F275D41} NetMask: ffff:ffff:ffff:ffff:: Address: fe80::2c9a:771:97c6:fef0
Interface: {A8CF5839-BA78-4C69-865E-3BE15F9F9A2C} NetMask: 0.0.0.0 Address: 0.0.0.0
似乎有些接口有一个不完整的ipv6网络掩码。任何人都可以解释为什么会这样吗?
答案 0 :(得分:1)
网络掩码不是不完整的,双冒号意味着一个或多个连续的零块。