In-depth understanding of IP addresses

IPWe can visit a certain mysterious website by directly entering the address in the browser , so IPhow is this address divided?

IP, written in English as Internet Protocol, refers to the Internet protocol in the TCP/IP network system, and works at the network layer of the OSI model (simple understanding is enough).

More content welcome to visit the blog

1. Standard IPv4 address division rules

IPDivided into IPv4and IPv6, this article mainly talks about IPv4the division of addresses.

It is IPv4stipulated to use 4 bytes to represent, that is, a total of 32 binary numbers, but the binary method is not convenient for people to remember, so we convert it to decimal, that is, the dotted decimal method , and use an English sentence for every 8 digits The words were separated into four groups.

IPv4It is divided into five types of addresses: A, B, C, D, and E , as shown in the figure.

image-20230411110314994

The identification bit in a class A address is 0 , the network address occupies 7 bits, and the host bit occupies 24 bits.

The identification bit in a class B address is 10 , the network address occupies 14 bits, and the host bit occupies 16 bits.

The identification bit in a class C address is 110 , the network address occupies 21 bits, and the host bit occupies 8 bits.

However, Class D addresses and Class E addresses do not distinguish between network addresses and host addresses .

A类地址
00000000 00000000 00000000 00000000 = 0.0.0.0
01111111 11111111 11111111 11111111 = 127.255.255.255
A类地址范围:0.0.0.0 ~ 127.255.255.255
实际能分配的范围:1.0.0.1 ~ 127.255.255.254

B类地址
10000000 00000000 00000000 00000000 = 128.0.0.0
10111111 11111111 11111111 11111111 = 191.255.255.255
B类地址范围:128.0.0.0 ~ 191.255.255.255
实际能分配的范围:128.0.0.1 ~ 191.255.255.254

C类地址
11000000 00000000 00000000 00000000 = 192.0.0.0
11011111 11111111 11111111 11111111 = 223.255.255.255
C类地址范围:192.0.0.0 ~ 223.255.255.255
实际能分配的范围:192.0.0.1 ~ 223.255.255.254

D类地址
11100000 00000000 00000000 00000000 = 224.0.0.0
11101111 11111111 11111111 11111111 = 239.255.255.255
D类地址范围:224.0.0.0 ~ 239.255.255.255
实际能分配的范围:224.0.0.1 ~ 239.255.255.254

E类地址
11110000 00000000 00000000 00000000 = 240.0.0.0
11110111 11111111 11111111 11111111 = 247.255.255.255
E类地址范围:240.0.0.0 ~ 247.255.255.255
实际能分配的范围:240.0.0.1 ~ 247.255.255.254

Notice:

  1. The network bits and host bits are all 0, so it means the local machine in this network, and the whole network in the router (its role is to help the router send packets that cannot be queried in the routing table. If the routing of the all-zero network is set, the routing The packets that cannot be queried in the table will be sent to the route of the all-zero network).
  2. The network bit and the host bit are all 1, indicating the broadcast address of the network.
  3. Network bit specific value, host bits all 0 means a network.
  4. Network bit-specific value, host bits all 1 means broadcast to all hosts on a specific network.
  5. 127.XXX is a reserved address for loop testing.

So the actual host address calculation formula that can be used is
2 n − 2 2^n-22n2
where n represents the number of hosts. Here, for example, the host bits of a class C address are 8 bits, so the maximum IPnumber
2 8 − 2 = 254 2^8-2=254282=2 5 4 The maximum number
that can be used by class B addresses2 16 − 2 = 65 , 534 2^{16}-2=65,534IP
2162=65,5 3 4 The maximum number
that can be used by a class A address2 24 − 2 = 16 , 777 , 214 2^{24}-2=16,777,214IP
2242=16,777,2 1 4
Here, explain why 2 is subtracted, because the case of all 0s and all 1s cannot be used, it is special, so 2 must be subtracted.

Two, IPv4 subnetting

What is subnetting? Why do we need subnets?

The most common type of address is Class C. A total of 254 hosts can be identified in a Class C address. What if we don’t need so many hosts now? Because too many host bits are wasted, can we let the network bits move a certain number of bits to the host bits? When it is possible, that is, a subnet appears.

Subnet: Borrow a certain number of bits from the host bit.

IP = network segment + subnet segment + host segment

3. Subnet mask

Due to the emergence of subnetting, we cannot quickly know the network bits and host bits, so the concept of subnet mask is proposed. The bitwise AND operation is performed through the IP and the subnet mask, and the final result is the network segment address.

We know that only the same network segment can communicate with each other, otherwise a gateway (router) is needed for data forwarding.

For example: if you want to communicate with your girlfriend in a different place first, then you need to forward the data through Tencent's server gateway and forward the data to your girlfriend's WeChat.

The network number in the IP address is represented by "1" in the subnet mask, and the host number in the IP address is represented by "0" in the subnet mask. The function of the subnet mask is to distinguish whether the hosts on the network are in the same network segment, or in other words, the subnet mask is used to distinguish the network number and the host number of the IP address.

image-20230411215849909

Without subnetting, the default subnet mask is:

  • Class A address subnet mask: 255.0.0.0
  • Class B address subnet mask: 255.255.0.0
  • Class C address subnet mask: 255.255.255.0

The network address can be calculated by performing a bitwise AND operation on the IP address and the subnet mask. After knowing the network address, I can then calculate the maximum number of hosts that can be allocated. as follows:

IP地址:192.168.0.1 

子网掩码:255.255.255.0

192.168.0.1 = 11000000 10101000 00000000 00000001

255.255.255.255 = 11111111 11111111 11111111 00000000

进行按位与运算的结果是:11000000 10101000 00000000 00000000 = 192.168.0.0

所以网络段IP地址是192.168.0.0

为了方便表示,我们还可以使用另外一种方式表示,例如:192.168.0.1/24,这个是和上面的表示是等价的。

这里的"/24"表示从左到右有24位1。

能分配的主机数量是:2的8次方减2。

4. Private IP address

Some of the addresses in A, B, and C levels are allocated as private addresses, and the private addresses can only be used in the LAN.

A级私有地址:10.0.0.1 ~ 10.255.255.255
B级私有地址:172.16.0.1 ~ 172.32.255.255
C级私有地址:192.168.0.1 ~ 192.168.255.254

5. Special address

127.XXX is a special address for loopback testing.

168.254.XX is a reserved address, if your IP address is automatically obtained, and you have not found an available DHCP server on the network. One of the IP addresses will be obtained.

references

[1] IP address and subnet division

[2] In-depth understanding of the concepts of IP address, subnet mask, and gateway

[3] Several types of special ip addresses

[4] Understand the IP address in one article

Guess you like

Origin blog.csdn.net/qq_43907505/article/details/130094838