Original address:Thorough understanding of IP addresses, subnet masks, and subnetting
What is the IP protocol?
Before answering the question of what the IP protocol is, we need to answer another question: what is a network? From an ordinary person's point of view, the network is nothing more than a WIFI logo on a cell phone or a network cable on a computer. But from an academic point of view, the network is a collection of hardware and software, the crystallization of the sweat and wisdom of countless sages, and a bridge spanning four oceans and seven continents, connecting the joys and sorrows of countless people.
The IP protocol is one of the basic network protocols and is located in the network layer of the OSI model. there are several versions of the IP protocol, the more important ones are IPV4 and IPV6, the current one is widely used in the Internet, and it will be replaced by IPV6 in the future, this article focuses on the IPV4 protocol.
Since the IP protocol is a protocol, its role is to set a standard. So what standards does the IP protocol set, and what do they entail? It would be too long to expand on this, so we're going to look at the IP protocol from two broad perspectives: IP addresses and subnetting. If you're interested in looking at the entire IPV4 protocol standard, check out the IETF's September 1981 publicationRFC 791
The IP protocol was set up with the assumption that the network hardware infrastructure is unreliable, such as fiber optic outages due to weather or equipment outages due to power outages, and that it uses dynamic connections and nodes, which means that there is no central monitoring to track and maintain the status of the network. As a result of this design, the IP protocol can only doDo your best to deliver the message.This raises several obvious questions:
- Data corruption or loss, for example, if ten messages are sent from Nanjing to Shanghai, there may be a fiber outage in Suzhou, resulting in only nine messages being received in Shanghai;
- Data is disorganized or duplicated, or in the example above, Shanghai may receive more than ten messages, and the order in which they are received is most likely to be disorganized.
The IP protocol only ensures that the message is sent to the best of its ability; whether the data is accurate or not is a job for the next level up in the OSI model, which is thetransport layerThe TCP protocol, for example, is a transmission control protocol, but that's an afterthought. The famous TCP protocol, for example, is a transmission control protocol, but that's an afterthought.
IP address
An IP address is specified as a 32-bit binary number. For example11000000 10101000 00000001 00000011
It is an IP address, which can be expressed in dotted decimal notation for ease of human perception and memorization:198.168.1.89
The IP address identifies a total of2^32
The second power of a device, that is4,294,967,296
Individuals. Based onRFC 791 In the description of the IP address, it serves two very important purposes: to indicate the location of a device and to find it, or, to put it bluntly, to find out where a device is. Imagine that these addresses are evenly distributed in every corner of the globe, how do you find one of them quickly? Intuitively, it is logical to think of categorization, so IP addresses are divided into five categories: A, B, C, D, and E. Category D is used as a multicast address, and category E is reserved for use, so we will only discuss the three categories of IP addresses, A, B, and C, here.
How can IP addresses be categorized properly? First of all, you need to break down the IP address into three parts, the network identification bit, the network number, and the host number. This concept is very important and is the core foundation for understanding IP addresses, as shown in the figure:
First of all, let's understand this diagram, take the class B address as an example, according to the IP protocol, the high bit of class B address is10,This is the network identification bit, followed by the network number in the next fourteen bits, and the host number in the last sixteen bits. In other words, an IP address with a high bit of 10 is categorized as a Class B IP address. If there is an IP address like this.10111101 00001010 01011010 00010100(189.10.90.20)
, then its network number and host number are shown below:
The network number is used to identify where the network is located and the host number is used to identify where the host is located. hosts with the same network number can communicate with each other. while hosts that do not use the network number must go through a gateway (router). the reason why IP addresses are designed in this way is for the purpose of IP address management and addressing. Based on the above, we can summarize the detailed classification table of IP address:
descriptive | Category A | Category B | Category C |
network identifier | 0 | 10 | 110 |
IP address range | 0.0.0.0 - 127.255.255.255 | 128.0.0.0 - 191.255.255.255 | 192.0.0.0 - 223.255.255.255 |
Available IP address range | 1.0.0.1 - 127.255.255.254 | 128.0.0.1 - 191.255.255.254 | 192.0.0.1 - 223.255.255.254 |
Number of networks | 126(2^7 - 2) | 16384 (2^14) | 2097152 (2 ^ 21) |
Available hosts per network | 16777214 (2^24 - 2) | 65534 (2 ^ 16 - 2) | 254 (2^8 - 2) |
The above IP address ranges can be calculated from a binary IP address, such as a class A address, which necessarily begins with00000000 00000000 00000000 00000000
The end is inevitable.01111111 11111111 11111111 11111111
The dot-decimal expression is0.0.0.0 - 127.255.255.255
. Since the network number of a class A address is 7 digits and the host number is 24 digits, the number of networks and the number of hosts can be easily calculated. So why do the hosts in each network need to be subtracted by 2, and the number of networks with class A addresses also needs to be subtracted by 2?
Let's start by answering the first question. The host number of each network is all 0's for the network itself, and all 1's are used for the broadcast address, so for the192.168.1
This network number.192.168.1.0
On behalf of the Network.192.168.1.255
Used for broadcasting. Broadcasting is used to send packets between all hosts in the network, i.e., one sender, multiple receivers, with the receivers deciding whether to use or discard the received message.
To answer the second question again, the 128 networks of class A addresses are reserved for two network numbers, the 7-bit full0 (0.0.0.0)
peacebuilding1 (127.0.0.0)
。0.0.0.0
It is used to broadcast to all addresses on the local machine, so in program development, it can be used to listen to all addresses on the local machine. 127.0.0.0 is used for the loopback test, that is, in addition to the127.0.0.1
cap (a poem)127.0.0.255
Outside of these two special host addresses, all other addresses point to themselves, and we commonly use the127.0.0.1
Represent yourself, in fact127.0.0.1 - 127.0.0.254
All represent themselves, can ping through, do not believe you can try.
subnetting
In a Class B network address, there are still 65,534 hosts that can be accommodated. However, in practice, few organizations can use such a large network, in addition, due to the large number of hosts, resulting in the same network propagation path will become more distant, thus affecting the efficiency of the same network propagation. So in order to disseminate information more efficiently, we need to split a network into many subnets, just like a city in reality needs to be split into several districts. After subnetting, the same Class B network is divided into several subnets internally, while externally it is still a network.
How to accomplish subnetting? We need to introduce another new concept:subnet maskThe subnet mask is a 32-bit binary number like an IP address. A subnet mask, like an IP address, consists of a 32-bit binary number. Generally speaking, a subnet mask consists of consecutive 1's in the high part and consecutive 0's in the low part. Let's look at a diagram:
insofar as189.10.90.20
For this IP address, it was originally created by theNetwork identification bit + network number + host numberThe three-part composition, after subnetting, becomes theNetwork identification bit + network number + subnet number + host numberThe four-part composition, expressed as189.10.90.20/20
The 20 after the IP address means that the high digit of the subnet mask consists of 20 consecutive 1's and is expressed in dotted decimal as follows255.255.240.0
Because the host number is occupied by 4 bits of the subnet mask, the host bit becomes 12 bits, and the number of hosts that can be accommodated becomes4092(2^12 - 2)
This IP address has a subnet number of 0101(5).
Judgment of the same network segment
If there is no subnetting, the189.10.90.20
cap (a poem)189.10.200.20
These two IPs are under the same Class B network and can communicate directly without a gateway, but if we use the subnet mask 20 for subnetting, can they still communicate directly? The answer is no, because they are not under the same subnet. We can use and operation with subnet mask and IP address, if the result is the same, then they are under the same network and can communicate directly. Or do we use189.10
This network number.20
for the mask as an example:
IP address | computational process | in the end |
189.10.90.20 | 189.10.90.20 & 255.255.240.0 | 10111101000010100101000000000000 |
189.10.200.20 | 189.10.200.20 & 255.255.240.0 | 10111101000010101100000000000000 |
189.10.202.20 | 189.10.202.20 & 255.255.240.0 | 10111101000010101100000000000000 |
Regarding bitwise arithmetic, if you're not quite sure, you canclick hereUnderstand.
It can be seen that90.20
respond in singing200.20,202.20
are not under the same network, and the200.20,202.20
Under the same network, you can communicate directly. One final question:192.168.1.1/26
cap (a poem)192.168.1.89/26
Is direct communication possible?
Default Subnet Mask
For class A, B, and C addresses, they also have a default subnet mask:
typology | subnet mask |
Class A addresses | 255.0.0.0, decimal representation: 8 |
Class B addresses | 255.255.0.0, decimal representation: 16 |
Class C address | 255.255.255.0, decimal representation: 24. This is the origin of the mask we often assign in LANs, which means that class C addresses are not subnetted. |
private address
existRFC 1918 Some addresses are reserved for private addresses, which do not have direct access to the public network, and the public network does not assign private addresses to the public network. Private addresses are used only for internal use within a family or organization, and are listed below:
typology | realm | subnet mask | Number of IPs |
Class A addresses | 10.0.0.0 - 10.255.255.255 | 10.0.0.0/8 | 16,777,216 |
Class B addresses | 172.16.0.0 - 172.31.255.255 | 172.16.0.0/12 | 1,048,576 |
Class C address | 192.168.0.0 - 192.168.255.255 | 192.168.0.0/16 | 65,536 |