Determine the network and host ID portion of an IP address
Asked Answered
S

4

7

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.

Sixpence answered 28/12, 2011 at 2:26 Comment(0)
E
12

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

Enos answered 20/5, 2014 at 9:14 Comment(0)
F
2
  • 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

Ferrari answered 28/12, 2011 at 8:54 Comment(5)
Hi Mike, I am not sure I follow the last portion about the network and host ID portion. Are you saying that because the binary version of 224 is 11100000, the network portion with be the part corresponding with the three 1's? and the host ID portion is the section associated with the 0's? Thanks again for your explanation.Sixpence
yes, that answers my question. it seems the algorithm uses 1s part of the binary from the subnet mask. this would mean that the same IP address from my example with the third octet as 31 would have a host ID of 0.0.31.22. Correct me if I am wrong here.Sixpence
@fwc, you're wrong. Read what I said in my answer about 192.168.33.22 and mask 255.255.224.0Ferrari
@MikePennington- I would calculate it as follows: subnet mask for the third octet is still 11100000, so the binary for 33 is 00011111; therefore the it would split as follows 000|11111 where the line is the divider between the network and host ID, or am i still missing something?Sixpence
@fwc, please check your binary math in the python interpreter. >>> int('00011111', 2). That comes out to 31, not 33Ferrari
L
2
Host address portion and network address portion can be easily identified.
Use this trick.
 Class A: N.H.H.H
 Class B: N.N.H.H
 Class C: N.N.N.H
(N= network  H=Host)
Class A network range: 1-127
Class B network range: 128-191
Class C network range: 192-223

Reference: https://www.youtube.com/watch?v=ddodZeXUS0w

Liebman answered 18/4, 2017 at 4:25 Comment(1)
Classful addressing was replaced by CIDR in 1993.Enos
S
0

You can use the following script:

#!/bin/sh
GetNumericIP()
{
    ipbin=0
    for part in `echo $1 | awk -F'.' '{print $1 " " $2 " " $3 " " $4}'`
    do
        ipbin=`expr $ipbin \* 256`
        ipbin=`expr $ipbin + $part`
    done
    echo "$ipbin"
}
GetSrtingIP()
{
    ipbin=$1
    count=0
    while [ $count -le 3 ]
    do
        rem=`expr $ipbin % 256`
        ipbin=`expr $ipbin / 256`
        if [ -z "$ipstr" ]
        then
            ipstr=$rem
        else
            ipstr=`echo ${rem}.${ipstr}`
        fi
        count=`expr $count + 1`
    done
    echo $ipstr
}
mask=$2
maskbin=`GetNumericIP $mask`
ip=$1
ipbin=`GetNumericIP $ip`
networkid=$(( $maskbin & $ipbin ))
networkid=`GetSrtingIP $networkid`
echo "networkid = $networkid"
Swivel answered 30/8, 2013 at 18:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.