简体   繁体   English

如何知道给定的IPV6地址是否与本地计算机属于同一子网

[英]How to know if given IPV6 address belongs to the same subnet as local machine

Currenly I have ac function that takes a ip address and subnetmask and checks if given ipv4 is private/public. 目前,我有一个带有IP地址和子网掩码的ac函数,并检查给定的ipv4是否为私有/公共。

It does by following logic to matchsubnet: 它通过以下逻辑来匹配子网:

(local ipv4 address && local subnetmask) == (given ipv4 && given subnetmask)

What logic can be written for ipv6? 可以为ipv6编写什么逻辑? I can write a function to receive an ip and subnet prefix. 我可以编写一个函数来接收ip和子网前缀。 Can i get local subnet prefix? 我可以获取本地子网前缀吗?

I order to determine if two hosts are on the same subnet take both their addresses and prefix lengths (subnet masks). 我要确定两个主机是否在同一子网上,同时使用它们的地址和前缀长度(子网掩码)。

If the prefix lengths are different then they are not on the same network. 如果前缀长度不同,则它们不在同一网络上。

If the prefix lengths are the same then take the length ( very probably 64, so let's assume that) and compare the first 64 bits of both addresses with each other. 如果前缀长度相同,则取该长度(很可能为64,因此我们假设为该长度),并将两个地址的前64位相互比较。 If they are the same then they are both on the same network. 如果它们相同,则它们都在同一网络上。

It is basically the same as you do for IPv4 with making the bits that don't being to the prefix. 基本上,与IPv4相同,只是使比特不成为前缀。 With 128 addresses it might be easier though to implement it as a loop comparing 4 bits at a time (comparing the hexadecimal notation) or 8 bits at a time (comparing the parsed bytes). 使用128个地址,将其实现为一次循环比较一次比较4位(比较十六进制表示法)或一次比较8位(比较解析的字节)可能会更容易。

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

相关问题 如何拆分 IPV6 地址以获取 Javascript 中的 Network ID 子网? - How to split IPV6 address to get the Network ID subnet in Javascript? 如何找到机器可以使用的 ipv6 地址的范围(前缀?),以及如何将 ipv6 的 su.net 转换为 ip 地址的片段? - How to find range (prefix?) of ipv6 addresses that a machine can use, and how to transform a subnet of ipv6 into a slice of ip addresses? 使用netlink API从linux接口删除ipv4地址属于同一子网 - deleting ipv4 address belongs to the same subnet from linux interface using netlink API 如何允许用户输入IPv6地址的子网掩码? 真的有必要吗? - How to allow user to input the subnet mask for IPv6 address? Is it really necessary? IPv6 链路本地地址格式 - IPv6 link-local address format 同一物理接口上多个VLAN网络的IPv6链路本地地址 - IPv6 link-local address for multiple VLAN networks over the same physical interface 如何将MAC地址转换为IPv6链接本地地址,反之亦然 - How to convert MAC address into IPv6 link local address and vice-versa 如何在烧瓶中获取ipv6地址? - How to get ipv6 address in flask? 如何获得正确的IPv6 IP地址? - How to get correct IPv6 Ip address? 如何从NDIS数据包缓冲区知道ipv4或ipv6? - How to know ipv4 or ipv6 from NDIS packet buffer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM