Is IP address on the same subnet as the local machine (with IPv6 support)

前端 未结 4 1345
无人及你
无人及你 2020-12-18 06:37

Does anyone have some code that will determine if an IP address (IPv4 or IPv6) is on the same subnet as the machine running the application? I\'ve seen numerous examples of

4条回答
  •  我在风中等你
    2020-12-18 07:07

    The moral equivalent of the IPv4 network mask in IPv6 is called the prefix length. (Actually, we like to talk about prefix length instead of network mask in IPv4 too, but some people haven't gotten the memo yet.)

    An additional wrinkle in IPv6 that doesn't come up with IPv4 is that default routers advertise their presence on the link by answering router solicitation queries from hosts. Hosts keep a list of all the default routers they find this way along with their valid and preferred lifetimes. Routers may also advertise zero, one or more prefixes for which they serve as a default router, and hosts keep a list of these along with their associated routers and their individual valid and preferred lifetimes.

    Each prefix has two ancillary bits in the advertisement, A and L, which get coalesced by hosts when they're added to the prefix list. The A=1 bit indicates whether hosts are allowed to automatically self-configure an interface address with that prefix, whereas A=0 means that hosts are required to obtain an address with that prefix via DHCPv6 or manually. The L=1 bit indicates that the prefix is "on link" and the host may use neighbor discovery (the IPv6 equivalent of ARP) to send directly across the network, whereas L=0 indicates that the prefix is "off link" and that hosts are required to send all traffic for that prefix to a default router.

    Long story short: if you want to know that an IPv6 address is "on link" then you have to walk the IPv6 prefix list and compare each one with the address and also look at the L-bit to make sure it's an on-link prefix. Alas, I only know the BSD-system way of looking at the prefix list, i.e. sysctl(ICMPV6CTL_ND6_PRLIST, ...). Really not sure what if anything MSFT has made available to C# developers for that.

    Yes, I realize this isn't a complete answer. Alas.

提交回复
热议问题