Internet Protocol (2)-from top to bottom

Continued from the previous article, from: Ruan Yifeng's web log

table of Contents

Seven, a summary

8. User's Internet settings

8.1 Static IP address

8.2 Dynamic IP address

8.3 DHCP protocol

8.4 Internet settings: summary

Nine, an example: visit a webpage

9.1 Local parameters

9.2 DNS protocol

9.3 Subnet mask

9.4 Application layer protocol

9.5 TCP protocol

9.6 IP protocol

9.7 Ethernet protocol

9.8 Server-side response


Seven, a summary

First, make a summary of the previous content.

We already know that network communication is the exchange of data packets. Computer A sends a data packet to computer B, and the latter receives it and replies with a data packet, thus realizing communication between the two computers. The structure of the data packet is basically as follows:

To send this packet, you need to know two addresses:

  * The MAC address of the other party

  * The IP address of the other party

With these two addresses, the data packet can be accurately delivered to the receiver. However, as mentioned earlier, MAC addresses have limitations. If two computers are not in the same subnet, they cannot know each other's MAC address and must be forwarded through a gateway.

In the picture above, computer No. 1 wants to send a data packet to computer No. 4. It first judges whether the No. 4 computer is on the same subnet, and it turns out that it is not (the judgment method will be described later), so it sends this data packet to gateway A. Through the routing protocol, gateway A finds that computer No. 4 is located in subnet B, and sends the data packet to gateway B, and gateway B forwards it to computer No. 4.

Computer No. 1 sends the data packet to gateway A, and it must know the MAC address of gateway A. Therefore, the destination address of the data packet is actually divided into two situations:

Scenes Packet address
Same subnet The other party's MAC address, the other party's IP address
Not the same subnet The MAC address of the gateway, the IP address of the other party

Before sending a data packet, the computer must determine whether the other party is in the same subnet, and then select the corresponding MAC address. Next, let's take a look at how this process is completed in actual use.

8. User's Internet settings

8.1 Static IP address

You bought a new computer, plugged in the internet cable, and turned it on. Will the computer be able to access the internet?

Usually you have to make some settings. Sometimes, the administrator (or ISP) will tell you the following four parameters, you fill them in the operating system, the computer can connect to the Internet:

  * This machine's IP address
  * Subnet mask
  * Gateway IP address
  * DNS IP address

The figure below is the setting window of Windows system.

These four parameters are indispensable, and I will explain why you need to know them to get online. Since they are given, the computer will be assigned the same IP address every time it is turned on, so this situation is called "static IP address Internet access".

However, such a setting is very professional, and ordinary users are intimidated, and if the IP address of one computer remains unchanged, other computers cannot use this address, which is not flexible enough. For these two reasons, most users use "dynamic IP addresses to surf the Internet."

8.2 Dynamic IP address

The so-called "dynamic IP address" means that after the computer is turned on, it will automatically be assigned an IP address without manual setting. The protocol it uses is called the DHCP protocol .

This agreement stipulates that in each sub-network, there is a computer responsible for managing all IP addresses of the network, which is called a "DHCP server". When a new computer joins the network, it must send a "DHCP request" packet to the "DHCP server" to apply for an IP address and related network parameters.

As mentioned earlier, if two computers are on the same subnet, they must know each other's MAC address and IP address before sending data packets. However, the newly added computer does not know these two addresses, how to send data packets?

The DHCP protocol makes some clever provisions.

8.3 DHCP protocol

First of all, it is an application layer protocol, built on top of the UDP protocol, so the entire packet looks like this:

  (1) At the top of the "Ethernet header", set the MAC address of the sender (this machine) and the MAC address of the receiver (DHCP server). The former is the MAC address of the local network card, the latter does not know at this time, just fill in a broadcast address: FF-FF-FF-FF-FF-FF.

  (2) The following "IP header", set the IP address of the sender and the IP address of the receiver. At this time, the machine does not know about both. Therefore, the sender's IP address is set to 0.0.0.0, and the receiver's IP address is set to 255.255.255.255.

  (3) The last "UDP header", set the port of the sender and the port of the receiver. This part is stipulated by the DHCP protocol. The sender is port 68 and the receiver is port 67.

After the data packet is constructed, it can be sent out. Ethernet is broadcast transmission, and every computer on the same subnet has received this packet. Because the receiver's MAC address is FF-FF-FF-FF-FF-FF, it is not clear who it was sent to, so every computer that receives this packet must also analyze the IP address of the packet to determine it. Not for yourself. When you see that the sender's IP address is 0.0.0.0 and the receiver is 255.255.255.255, the DHCP server knows that "this packet is sent to me", and other computers can discard this packet.

Next, the DHCP server reads the data content of this packet, assigns an IP address, and sends back a "DHCP response" packet. The structure of this response packet is similar. The MAC address of the Ethernet header is the network card addresses of both parties, and the IP address of the IP header is the IP address of the DHCP server (sender) and 255.255.255.255 (receiver), UDP header The ports are 67 (sender) and 68 (receiver). The IP address assigned to the requester and the specific parameters of the network are included in the Data section.

The newly added computer receives this response packet, so it knows its own IP address, subnet mask, gateway address, DNS server and other parameters.

8.4 Internet settings: summary

In this part, there is one point to remember: whether it is a "static IP address" or a "dynamic IP address", the first step for a computer to go online is to determine the four parameters. These four values ​​are very important and worth repeating:

  * This machine's IP address
  * Subnet mask
  * Gateway IP address
  * DNS IP address

With these values, the computer can "surf" on the Internet. Next, let's look at an example of how the Internet protocol works when a user visits a web page.

Nine, an example: visit a webpage

9.1 Local parameters

We assume that after the steps in the previous section, the user has set his own network parameters:

  * The machine's IP address: 192.168.1.100
  * Subnet mask: 255.255.255.0
  * Gateway's IP address: 192.168.1.1
  * DNS's IP address: 8.8.8.8

Then he opened the browser and wanted to visit Google, and entered the URL: www.google.com in the address bar.

This means that the browser has to send a data packet of a web page request to Google.

9.2 DNS protocol

We know that to send data packets, you must know the other party's IP address. However, now, we only know the web address www.google.com, not its IP address.

The DNS protocol can help us to convert this URL into an IP address. The known DNS server is 8.8.8.8, so we send a DNS packet (port 53) to this address.

Then, the DNS server responded, telling us that Google’s IP address is 172.194.72.105. So we know the other party's IP address.

9.3 Subnet mask

Next, we have to determine whether this IP address is in the same subnet, which requires the subnet mask.

The known subnet mask is 255.255.255.0. This machine uses it to perform a binary AND operation on its own IP address 192.168.1.100 (both digits are 1, the result is 1, otherwise it is 0), the calculation result is 192.168 .1.0; Then an AND operation is also performed on Google’s IP address 172.194.72.105, the result is 172.194.72.0. These two results are not equal, so the conclusion is that Google and this machine are not in the same subnet.

Therefore, if we want to send a data packet to Google, it must be forwarded through the gateway 192.168.1.1, that is, the MAC address of the receiver will be the MAC address of the gateway.

9.4 Application layer protocol

The HTTP protocol is used to browse the web, and its entire data packet structure is like this:

The content of the HTTP part is similar to the following:

  GET / HTTP/1.1
  Host: www.google.com
  Connection: keep-alive
  User-Agent: Mozilla/5.0 (Windows NT 6.1) ......
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: zh-CN,zh;q=0.8
  Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
  Cookie: ... ...

We assume that the length of this part is 4960 bytes, and it will be embedded in the TCP packet.

9.5 TCP protocol

TCP data packets need to set the port. The HTTP port of the receiver (Google) is 80 by default, and the port of the sender (the machine) is a randomly generated integer between 1024-65535, which is assumed to be 51775.

The header length of a TCP data packet is 20 bytes, plus the embedded HTTP data packet, the total length becomes 4980 bytes.

9.6 IP protocol

Then, the TCP data packet is embedded in the IP data packet. IP data packets need to set the IP addresses of both parties, which are known, the sender is 192.168.1.100 (local machine), and the receiver is 172.194.72.105 (Google).

The header length of the IP packet is 20 bytes, plus the embedded TCP packet, the total length becomes 5000 bytes.

9.7 Ethernet protocol

Finally, the IP data packet is embedded in the Ethernet data packet. Ethernet data packets need to set the MAC addresses of both parties, the sender is the MAC address of the machine's network card, and the receiver is the MAC address of the gateway 192.168.1.1 (obtained through the ARP protocol).

The maximum length of the data part of an Ethernet data packet is 1500 bytes, while the current IP data packet length is 5000 bytes. Therefore, the IP data packet must be divided into four packets. Because each packet has its own IP header (20 bytes), the length of the IP data packet of the four packets is 1500, 1500, 1500, 560, respectively.

9.8 Server-side response

After being forwarded by multiple gateways, Google's server 172.194.72.105 received these four Ethernet packets.

According to the serial number of the IP header, Google puts together the four packets, takes out the complete TCP data packet, then reads the "HTTP request" inside, then makes the "HTTP response", and then sends it back using the TCP protocol.

After receiving the HTTP response, the machine can display the web page and complete a network communication.

This example ends here. Although simplified, it roughly reflects the entire communication process of the Internet protocol.

Guess you like

Origin blog.csdn.net/weixin_37719279/article/details/82855227