3

I have an IP address

12.23.34.22

I need to find the network prefix for this IP Address. For that I need to find the subnet mask and then count the number of contiguous 1s to get the network prefix.

How can we calculate the subnet mask (255.x.x.x) for this given IP ?

Ron Maupin
  • 99,565
  • 26
  • 120
  • 195
user544079
  • 133
  • 1
  • 1
  • 4
  • 2
    The network mask is not calculated. It is assigned. Whoever gave you the IP address can tell you what it is. – Michael Hampton Oct 21 '16 at 05:17
  • Did any answer help you? If so, you should accept the answer so that the question doesn't keep popping up forever, looking for an answer. Alternatively, you could provide and accept your own answer. – Ron Maupin Aug 15 '17 at 04:31

3 Answers3

10

How can we calculate the subnet mask (255.x.x.x) for this given IP ?

You can't. To find the prefix, you need the address and mask, or the address and prefix length.

See the answers to this question on how to calculate everything.

Ron Maupin
  • 99,565
  • 26
  • 120
  • 195
  • So basically only the subnet's router and the nodes that comprise the subnet know the netmask? Or more precisely, a router doesn't exactly know the netmask. It just has rules with the subnet's netmask, that tell it to send packets into the subnet, if an IP address matches a rule (that has a mask and a subnet number). And also nodes know their netmask (ip a). To determine if they're to accept or ignore a packet. That is, a netmask is not needed to send a packet. Correct me if I'm wrong. – x-yuri Aug 01 '19 at 17:47
  • ...And say if I want to know if a site is behind Cloudflare. I've got to do ipcalc IP/PREFIX_LENGTH for each IP range, and check if the resulting subnet number matches the one from the IP range. Or find an IP range that looks similar and check the IP with the range's prefix length. – x-yuri Aug 01 '19 at 17:58
  • A routing table in a router has prefixes (network and mask). IP packets only have addresses. The destination address on a packet coming into a router is compared to the entries in a routing table (masking the address) to see if it matches. If there is a match, the packet is is sent out the interface for the matching prefix, otherwise the packet is dropped. You really have no way to know the path a packet takes to its destination, and that is how IP was designed. You do not care how it gets there, and the path could change at any time; you only care that it does get to the destination. – Ron Maupin Aug 01 '19 at 18:18
4

A subnet mask is a 32 bit value that allows the device that's receiving ip packets to distinguish the network ID portion of the IP address from the host ID portion of the IP address, so without a subnet mask that can be represented in form of a 32 value like (255.0.0.0) or slash-notation (/8) it is impossible to identify the network prefix of an IP address.

Ron Maupin
  • 99,565
  • 26
  • 120
  • 195
kenphor
  • 84
  • 2
0

I think you need to know if it's classful or not. if it's classful then you can use the default subnet mask of class A which is 255.0.0.0 and so the network address will be 12.0.0.0.

Ron Maupin
  • 99,565
  • 26
  • 120
  • 195
sammiie
  • 1
  • 1