简体   繁体   English

与C linux中的ipv6范围相比,如何查找ipv4地址的范围名称(链接,主机和全局)

[英]how to find ipv4 address scope name (link, host and global) as compared with ipv6 scope in C linux

Here i am not talking about the scope id its about the scope name (host, link, site or global). 在这里,我不是在谈论作用域ID,它是关于作用域名称(主机,链接,站点或全局)的。 How to compare/get scopes of ipv4 address as well as for ipv6. 如何比较/获取ipv4地址以及ipv6的范围。

直接解决方案是将地址与Wikipedia上此列表中的前缀进行比较。

To match my IP6 answer : however as noted IP4 the classifications are a recommendation that have been enshrined in the IP6 protocol so not completely equivalent. 为了匹配我的IP6答案 :但是,正如IP4所指出的那样,分类是IP6协议中包含的建议,因此并不完全等效。

There are helper macros to assist: 有帮助宏可以帮助您:

#define IN_LINKLOCAL(i) (((uint32_t)(i) & 0xffff0000) == 0xa9fe0000)
#define IN_PRIVATE(i)   ((((uint32_t)(i) & 0xff000000) == 0x0a000000) || \
                         (((uint32_t)(i) & 0xfff00000) == 0xac100000) || \
                         (((uint32_t)(i) & 0xffff0000) == 0xc0a80000))

struct sockaddr_in s4;
if (IN_LOOPBACK(ntohl(s4->sin_addr.s_addr)) {
  puts ("loopback");
} else if (IN_MULTICAST(ntohl(s4->sin_addr.s_addr)) {
  puts ("multicast");
} else if (IN_BADCLASS(ntohl(s4->sin_addr.s_addr)) {
  puts ("reserved");
} else if (IN_LINKLOCAL(ntohl(s4->sin_addr.s_addr)) {
  puts ("link-local");
} else if (IN_PRIVATE(ntohl(s4->sin_addr.s_addr)) {
  puts ("private");
} else {
  puts ("global");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM