bash 如何获取 IPv6 主机的范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11632284/
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
How to get scope of an IPv6 host?
提问by mng
I don't have much knowledge about the IPv6 protocol, so sorry if the question sounds stupid. When I retrieve the list of all IPv6 addresses in my network, I get a field named scope, as shown below :
我对 IPv6 协议了解不多,如果这个问题听起来很愚蠢,那么抱歉。当我检索网络中所有 IPv6 地址的列表时,我得到一个名为 scope 的字段,如下所示:
      inet6 addr: 2001:470:1:82::11/64 Scope:Global
      inet6 addr: 2001:470:1:82::10/64 Scope:Global
      inet6 addr: 2001:470:1:82::13/64 Scope:Global
      inet6 addr: fe80::21d:9ff:fe69:2c50/64 Scope:Link
      inet6 addr: 2001:470:1:82::12/64 Scope:Global
      inet6 addr: 2001:470:1:82::15/64 Scope:Global
      inet6 addr: 2001:470:1:82::14/64 Scope:Global
      inet6 addr: 2001:470:1:82::5/64 Scope:Global
      inet6 addr: 2001:470:1:82::17/64 Scope:Global
      inet6 addr: 2001:470:1:82::6/64 Scope:Global
      inet6 addr: 2001:470:1:82::16/64 Scope:Global
      inet6 addr: 2001:470:1:82::7/64 Scope:Global
      inet6 addr: 2001:470:1:82::19/64 Scope:Global
      inet6 addr: 2001:470:1:82::8/64 Scope:Global
      inet6 addr: 2001:470:1:82::18/64 Scope:Global
      inet6 addr: 2001:470:1:82::9/64 Scope:Global
      inet6 addr: 2001:470:1:82::1b/64 Scope:Global
      inet6 addr: 2001:470:1:82::a/64 Scope:Global
      inet6 addr: 2001:470:1:82::1a/64 Scope:Global
      inet6 addr: 2001:470:1:82::b/64 Scope:Global
      inet6 addr: 2001:470:1:82::1d/64 Scope:Global
      inet6 addr: 2001:470:1:82::c/64 Scope:Global
      inet6 addr: 2001:470:1:82::1c/64 Scope:Global
      inet6 addr: 2001:470:1:82::d/64 Scope:Global
      inet6 addr: 2001:470:1:82::1f/64 Scope:Global
      inet6 addr: 2001:470:1:82::e/64 Scope:Global
      inet6 addr: 2001:470:1:82::1e/64 Scope:Global
      inet6 addr: 2001:470:1:82::f/64 Scope:Global
      inet6 addr: ::1/128 Scope:Host
In my application, I need to get those addresses for which the scope is 'Link'. I could have used a system call to ifconfig and then parsed the output to extract corresponding addresses. But the problem is, I'm using the call to getifaddrs(), which returns a linked list of structure ifaddr, given as :
在我的应用程序中,我需要获取范围为“链接”的那些地址。我可以使用对 ifconfig 的系统调用,然后解析输出以提取相应的地址。但问题是,我正在使用对 getifaddrs() 的调用,它返回结构 ifaddr 的链表,如下所示:
       struct ifaddrs {
           struct ifaddrs  *ifa_next;    /* Next item in list */
           char            *ifa_name;    /* Name of interface */
           unsigned int     ifa_flags;   /* Flags from SIOCGIFFLAGS */
           struct sockaddr *ifa_addr;    /* Address of interface */
           struct sockaddr *ifa_netmask; /* Netmask of interface */
           union {
               struct sockaddr *ifu_broadaddr;
                                /* Broadcast address of interface */
               struct sockaddr *ifu_dstaddr;
                                /* Point-to-point destination address */
           } ifa_ifu;
       #define              ifa_broadaddr ifa_ifu.ifu_broadaddr
       #define              ifa_dstaddr   ifa_ifu.ifu_dstaddr
           void            *ifa_data;    /* Address-specific data */
       };
The question is : how to get the addresses with 'Link' scope from this list ?
问题是:如何从此列表中获取具有“链接”范围的地址?
回答by Jeremy Visser
One way to do this would simply be to check whether the address falls within fe80::/10. The IPv6 address spaceis available from IANA, which details the possible scopes available.
一种方法是简单地检查地址是否在fe80::/10. 在IPv6的地址空间可从IANA,其中详细介绍了可以在可能范围。
I downloaded the source code to net-tools(the source package for ifconfig), and it looks like they parse /proc/net/if_inet6. (Comments are my own additions in the following code — also the below is extremely abridged and will most certainly not compile.)
我将源代码下载到net-tools(ifconfig 的源包),看起来它们解析/proc/net/if_inet6. (评论是我自己在下面的代码中添加的——下面的代码也非常删节,肯定不会编译。)
/* some defines collected around the place: */
#define _PATH_PROCNET_IFINET6   "/proc/net/if_inet6"
#define IPV6_ADDR_LOOPBACK      0x0010U
#define IPV6_ADDR_LINKLOCAL     0x0020U
#define IPV6_ADDR_SITELOCAL     0x0040U
#define IPV6_ADDR_COMPATv4      0x0080U
int scope; /* among other stuff */
/* looks like here we parse the contents of /proc/net/if_inet6: */
if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
while (fscanf(f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
          addr6p[0], addr6p[1], addr6p[2], addr6p[3],
          addr6p[4], addr6p[5], addr6p[6], addr6p[7],
      &if_idx, &plen, &scope, &dad_status, devname) != EOF) {
/* slightly later: */
printf(_(" Scope:"));
switch (scope) {
case 0:
    printf(_("Global"));
    break;
case IPV6_ADDR_LINKLOCAL:
    printf(_("Link"));
    break;
case IPV6_ADDR_SITELOCAL:
    printf(_("Site"));
    break;
case IPV6_ADDR_COMPATv4:
    printf(_("Compat"));
    break;
case IPV6_ADDR_LOOPBACK:
    printf(_("Host"));
    break;
default:
    printf(_("Unknown"));
}
So let's have a look at what the above is parsing:
那么让我们来看看上面解析的是什么:
$ cat /proc/net/if_inet6
20010db8000008000000000000000001 03 40 00 00     eth0
fe800000000000000000000000004321 03 40 20 80     eth0
00000000000000000000000000000001 01 80 10 80       lo
So you can see the third column from the left (0x00Global, 0x20Link-Local, and 0x10Loopback) is the scope. Using the above constants from the net-tools code you can work out what they mean. Further investigation would be required to determine a more authoritative source for such constants, and also whether parsing /proc/net/if_inet6is your best option.
所以你可以看到左起第三列(0x00Global、0x20Link-Local 和0x10Loopback)是范围。使用来自网络工具代码的上述常量,您可以计算出它们的含义。需要进一步调查以确定此类常量的更权威来源,以及解析是否/proc/net/if_inet6是您的最佳选择。
回答by Steve-o
There are helper macros to assist:
有帮助宏可以提供帮助:
struct sockaddr_in6 s6;
if (IN6_IS_ADDR_LINKLOCAL(&s6.sin6_addr)) {
  puts ("link-local");
} else if (IN6_IS_ADDR_SITELOCAL(&s6.sin6_addr)) {
  puts ("site-local");
} else if (IN6_IS_ADDR_V4MAPPED(&s6.sin6_addr)) {
  puts ("v4mapped");
} else if (IN6_IS_ADDR_V4COMPAT(&s6.sin6_addr)) {
  puts ("v4compat");
} else if (IN6_IS_ADDR_LOOPBACK(&s6.sin6_addr)) {
  puts ("host");
} else if (IN6_IS_ADDR_UNSPECIFIED(&s6.sin6_addr)) {
  puts ("unspecified");
} else {
  puts ("global");
}
回答by chepner
I don't think the scope is stored explicitly in that data structure. However, you can infer the scope from the IP address itself. See http://en.wikipedia.org/wiki/IPv6_address#Address_scopes
我不认为范围明确存储在该数据结构中。但是,您可以从 IP 地址本身推断范围。请参阅http://en.wikipedia.org/wiki/IPv6_address#Address_scopes

