linux获取其他主机ipv6,linux – 如何获取IPv6主机的范围?
我對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
在我的應用程序中,我需要獲取范圍為“鏈接”的那些地址.我可以使用系統調用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 */
};
問題是:如何從此列表中獲取具有“鏈接”范圍的地址?
解決方法:
一種方法是檢查地址是否在fe80 :: / 10范圍內. IPv6 address space可從IANA獲取,詳細說明了可用的范圍.
我將源代碼下載到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"));
}
那么讓我們來看看上面解析的內容:
$cat /proc/net/if_inet6
20010db8000008000000000000000001 03 40 00 00 eth0
fe800000000000000000000000004321 03 40 20 80 eth0
00000000000000000000000000000001 01 80 10 80 lo
因此,您可以看到左側的第三列(0x00 Global,0x20 Link-Local和0x10 Loopback)是范圍.使用網絡工具代碼中的上述常量,您可以找出它們的含義.需要進一步調查以確定這些常量的更權威的來源,以及解析/ proc / net / if_inet6是否是您的最佳選擇.
標簽:bash,linux,ip-address,ipv6
來源: https://codeday.me/bug/20190714/1461406.html
總結
以上是生活随笔為你收集整理的linux获取其他主机ipv6,linux – 如何获取IPv6主机的范围?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kali linux 截图位置,Kali
- 下一篇: 乌班图linux配置yum仓库,Linu