0

Currenly I have a c function that takes a ip address and subnetmask and checks if given ipv4 is private/public.

It does by following logic to matchsubnet:

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

What logic can be written for ipv6? I can write a function to receive an ip and subnet prefix. Can i get local subnet prefix?

SoothingMusic
  • 107
  • 1
  • 13
  • Same logic, larger numbers – Sander Steffann Jun 03 '16 at 12:51
  • Whether it makes sense depends on what you are trying to determine. An IPv6 user usually gets a /48 (or at least a /56) from the ISP, while all the subnets are /64. Do you want to determine whether addresses are on the same subnet, or if addresses belong to the same user? The second one is more difficult. – Sander Steffann Jun 03 '16 at 12:53
  • @Sander Steffann I am looking for the 1st one. I am getting an IP configured somewhere and I am trying to know if it belongs to my subnet. If yes I treat it as private IP, else I treat it as public IP. – SoothingMusic Jun 03 '16 at 13:12

1 Answers1

0

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. 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. 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).

Sander Steffann
  • 9,509
  • 35
  • 40