Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the network and host ID portion of an IP address

I need to work out the algorithm regarding how you calculate the network and host portion of an IP address.

  1. Is the host ID the public part? Is the network ID the private part for locating the computer within the local network?

  2. If the subnet mask is a value smaller than 255 the corresponding octet in the IP address must be broken down into binary to determine which part of the number is the host ID and which portion is the network ID. Is the result binary number always split in two?

    (e.g. An IP address of 192.168.33.22 with a subnet mask of 255.255.224.0 means that the octet holding 33 be broken down as follows: 0010|0001 indicating that 0010 is the network ID portion and 0001 is the host ID portion?)

Thank you in advance for any help.

like image 421
fwc Avatar asked Dec 28 '11 02:12

fwc


People also ask

What is the host portion of an IP address?

Host Part. This is the part of the IP address that you assign to each host. It uniquely identifies this machine on your network. Note that for each host on your network, the network part of the address will be the same, but the host part must be different.

Which identifies with parts of the IP address belong to the network ID?

Class B addresses: The first 16 bits of the IP address are used for the network ID. The final 16 bits are for the host ID. Class C addresses: The first 24 bits of the IP address are used for the network ID. The final 8 bits are for the host ID.


2 Answers

You're over-complicating things.

IPv4 addresses (and subnet masks) are merely displayed in dot-decimal notation simply as a means of making them more readable to humans. Within the computer, they are simply 4 bytes of contiguous memory (often stored, for example, within a long int):

Stored in computer:    11000000 10101000 00100001 00010110
Displayed for human:        192.     168.      33.      22

Stored in computer:    11111111 11111111 11100000 00000000
Displayed for human:        255.     255.     224.       0

The 1s in the mask indicate bits that identify the network number, thus one merely need use a bitwise AND operation to extract the network number:

address   11000000 10101000 00100001 00010110    192.168.33.22
mask      11111111 11111111 11100000 00000000    255.255.224.0
(AND)     -----------------------------------    -------------
network   11000000 10101000 00100000 00000000    192.168.32.0

Since the introduction of CIDR (prior to which the address's class indicated the network/host boundary), hosts usually only know the mask of their own network and are therefore unable to divide arbitrary addresses (e.g. that of a datagram's destination) into network and host numbers.

So what's the point? Well, a source host can still take the bitwise AND of the destination's address and its (the source's) own network mask. Whilst the result of that operation will not necessarily produce a meaningful network number, it will match the source's network number if and only if they are on the same network:

  • if they match, the destination should be reachable at the link layer (e.g. by looking up its MAC address, perhaps via broadcasting an ARP request, and then encapsulating the datagram in a frame that is addressed to that MAC);

  • if they differ, the source must send the datagram to a router that is on its own network (using the above process to reach that router); the router will see that the frame is addressed to it, but that the datagram is not, and should then forward the datagram (encapsulated in a different frame) towards the destination. Many hosts only know of one router, their "default gateway", although other configurations are possible.

Those address bits that don't identify the source's network number, evidently indicated by 0s in its network mask, can be considered to form its host number—although it's really neither meaningful nor useful to extract it in the same way as was done above: even when communicating with a host on one's own network, its full address is used for identification, never the host number alone.

That said, as a purely academic exercise it is of course possible to perform a bitwise AND with the complement of the mask:

address   11000000 10101000 00100001 00010110    192.168.33.22
~mask     00000000 00000000 00011111 11111111    0.0.31.255
(AND)     -----------------------------------    -------------
host      00000000 00000000 00000001 00010110    0.0.1.22

So, to address your questions:

  1. Is the host ID the public part? Is the network ID the private part for locating the computer within the local network?

    The entire address is "public"; there are no "private" parts. Lookup protocols like ARP (which uses the full address) are used to locate computers within the local network.

  2. If the subnet mask is a value smaller than 255 the corresponding octet in the IP address must be broken down into binary to determine which part of the number is the host ID and which portion is the network ID. Is the result binary number always split in two?

    Nothing is "split in two". It only appears that way because dot-decimal notation was intended to make IPv4 addresses more readable to humans (albeit that decision was taken prior to the invention of CIDR, when network numbers were always aligned to byte boundaries and thus never caused the apparent "split" of a decimal number).

like image 147
eggyal Avatar answered Oct 23 '22 00:10

eggyal


  • Is the host ID the public part? Is the network ID the private part for locating the computer within the local network?

The host and network portions of an ip address have nothing to do with public and private.

  • If the subnet mask is a value smaller than 255 the corresponding octet in the IP address must be broken down into binary to determine which part of the number is the host ID and which portion is the network ID. Is the result binary number always split in two? ...a subnet mask of 255.255.224.0 means that the octet holding 33 be broken down as follows: 0010|0001...

Your example is wrong. Specifically, you assume that 224 has four consecutive binary bits in it when you spit the 33 octet as 0010|0001 (where | is the division between network and host)...

The octet in the subnet mask containing 224 has three consecutive binary 1s in it: 11100000. Therefore the "network portion" of the whole IP address is: 192.168.32.0. The "host portion" of the ip address is 0.0.1.22. Using your notation, the third octet of ip 192.168.33.22 (mask 255.255.224.0) is: 001|00001.

To get the network portion of an IP address, you must perform a binary AND of the ip address and its netmask. The host portion is a binary AND of the inverted netmask (bits flipped between 0 and 1).

EDIT

Let's make another example to address your comment:

IP Address 192.168.255.22, NetMask 255.255.224.0

The network portion of this address is 192.168.224.0 and the host portion of the address is 0.0.31.22. I intentionally chose the numbers in the example to make the math as obvious as possible. Please convert 224 and 31 to binary, it should make things clear. If not, please reference the wikipedia article on subnetting

like image 2
Mike Pennington Avatar answered Oct 23 '22 02:10

Mike Pennington