Secular wanderer: a programmer who focuses on technical research

Say what I said before

In the last section, I talked about the basic knowledge of computer network. After thinking about it carefully, I felt that it was not common. Therefore, I reviewed the relevant knowledge during this period of time, so today we only talk about two things

  • Next hop mechanism
  • ARP protocol

These two things I can talk with you understand, I think I am also very good, O(∩_∩)O ha ha ~

Let’s review TCP:

  • The application layer is in the user state of the operating system, while the transmission control layer, network layer, data link layer and physical layer are all in the kernel state of the operating system.

  • For example, in the example above, a small example of communicating over TCP: When you want to send a message, the application layer calls the transport control layer to establish a connection.

  • When the transmission control layer receives the signal, it will establish the communication connection through the three-way handshake. After the connection is established, it will notify the application layer to establish threads, create objects, open up memory space and so on

  • The packets are then sent out in other layers of calls

That is, the transport control layer sends the handshake packet first, so: how does the transport control layer know where to send the handshake packet?

In fact, when the transmission control layer calls the network layer, the transmission control layer enters the blocking state and waits for the network layer to clear up the network for it. The link layer knows the location of the next node and sends the handshake packet through the binary coding of the physical layer (receiving back and forth).

How does the network layer dredge the network is the focus that we want to talk about below

Next hop mechanism

The basic solution

There is a network, there must be an Internet, there are N devices on the Internet, how can we narrow that down becomes the network layer’s job.

Just like the express delivery in our life, we only have to fill in the specific province and city, detailed address, name, telephone and other information clearly, so that the express can arrive correctly

Therefore, there are three factors in our current equipment:

  • IP

There are two types: ipv4 and ipv6

  • mask

To obtain the network number of the current IP address, perform bitwise and operation on the IP address and mask.

  • The gateway

Network actual egress address

At that time, there were two solutions to the packet transmission problem:

  1. Each device stores every device in the entire Internet except itself, network topology, connection mode, and so on, and then if the device wants to find one of those points, it just calculates the nearest route in the graph

That’s not going to work. There are so many Internet devices. So we came up with a solution, and then we conquered it with an actual experiment, which is the next jump mechanism

  1. Each device stores only nodes on its own network. In this way, the nearest node can be obtained each time through various ways of comparison. It goes something like this:

That is, device 1 only needs to store the address of switch 1, and router only needs to store the address of switch 1, switch 2, and switch 3

The routing table

Then, how to contrast becomes the key of the problem. Therefore, on each device, there is a table called the routing table:

Then, the IP address and the mask of the entry in the routing table are calculated by bit respectively, and then compared with the previous network number. If the comparison can be made, the gateway address corresponding to the current entry is the next hop address

In addition, the gateway of an entry that does not match any other entry in the routing table but can only match 0.0.0.0 is the default gateway

For example: ping www.baidu.com

Through comparison, the address of the next hop is 192.168.87.2, which means that the current machine will encapsulate a packet, mainly including:

  • Source IP address – Destination IP address
  • Source port number – Destination port number

The network layer just finds the next hop, and the next step is the work of the data link layer

ARP protocol

How do I obtain the MAC address of the next hop

At this layer, the packets transmitted from the network layer are covered with another layer, including the MAC address of the next hop device. In this case, the ARP table is used

Unlike DNS, which is used to determine the relationship between domain names and IP addresses, ARP is used to determine the relationship between IP and MAC addresses and is limited to local area networks

Let’s take a look at what an ARP table looks like

The figure shows all mapping relationships. Based on the routing table, you can determine that the MAC address corresponding to 192.168.87.2 is not written

In fact, the device is brand new or just started, and the ARP table is empty. There is no corresponding MAC address. The computer then sends an ARP packet, including:

  • Source MAC Address: address of device 1
  • Destination MAC address :FF:FF:FF:FF:FF:FF :FF
  • Destination IP address: 192.168.87.2

When the destination MAC address is all F, will be broadcast message, sent by the switch to the current network of all the equipment, if the current in the network equipment can’t match the IP address, then the message will be discarded, if there is a match that would give equipment returns a response, this is the response of packets is like this:

  • Source MAC Address: Indicates the MAC address of the router
  • Destination MAC address: MAC address of device 1
  • Destination IP address: 192.168.87.139

Then device 1 gets the MAC address of the next hop.

Insert a dot here, and after the switch sends the message, how does it return it

The switch has the port-> MAC learning function. After device 1 sends a broadcast message, it saves a message in the switch:

  • 1: – > 192.168.87.139 @ MAC

When the router returns, it compares it here, finds device 1, and records a new message

  • 2: – > 192.168.87.2 @ MAC

The above example is performed in a virtual machine. The following example is simulated using the real IP of the local machine

Packet transmission process

Combined with now known knowledge, we try to restore through a picture how to ping www.baidu.com

Assume that ARP contains the MAC address corresponding to each IP address.

If no, follow the preceding procedure to send a broadcast message, wait for the corresponding device to receive it, and then return

The source MAC address and destination MAC address in the packets change according to the next hop

Arp cannot cross networks

NAT Protocol

The Internet at home looks something like this:

  • The router, which connects us externally to the carrier network, allows us to access the Internet, there’s an external address, there’s an internal network that gives us access to the Internet, there’s a LAN address

As shown in figure

Now, device 1 and Device 2 want to access Baidu through browser respectively, at this time:

  • Device 1 applies for port 192.168.1.3:11211 randomly
  • Device 2 applies for port 192.168.1.4:11211 randomly

There are 65535 available ports. It is very possible for two different devices to apply for the same port

The two devices send all requests to the router level, and the router will do the following:

  • Translate the address of device 1:6.6.6.6:11211
  • Translate device 2’s address: 6.6.6.6:11211

After the router switches the address, it sends the request to Baidu’s server over the Internet. Baidu then returns the request result to the router, and the final result looks like this:

In this case, the routing layer does not know how to return, so we call the method of recognizing this request based on some means: Source Address Translation Protocol (S-NAT), which looks like this:

After the request arrives at the router, the router randomly applies for a port, records the requested address, and then replaces the requested port, so that when the server returns, the request can be distinguished and the result of the request can be returned to the corresponding device by comparing the table in the router

There is also target Address translation protocol (D-NAT), which is the process of replacing target addresses with load balancing services

The last word

Now that we’re done with the basic concepts of networking, let’s move on to the actual coding process and look at Socket usage in Java