How does the kernel set up a connection for the program

Packet capture tcp-nn -i eth0 Port 80 or ARP

Packet capture Two types of request packets: one is baidu port 80 and the other is ARP requestCopy the code

Start ListeningCopy the code

Arp -d 192.168.150.2 && curl www.baidu.com

Delete the specified entry from the ARP table and request BaiduCopy the code

Then look at what tcpdump is listening for

So this is the familiar three-way handshake data transfer and the four-way breakup process. Notice that before the three-way handshake there are two ARP packets requesting who has a MAC address 192.168.150.2 please tell 192.168.150.11 Someone will tell it that the IP address is on the network card 53:2bCopy the code

I've just started the process of three handshakesCopy the code

If -n is not carried, TCP -i eth0 port 80 or ARP

The route without - n

A -n will expose these valuesCopy the code

Prepare two machines

node1

node2

These two hosts can be pinged together on the same LAN without a routerCopy the code

node1 ping node2

Routing table 1

Set a new loopback interface ifconfig LO :22 192.168.168.88/24 on node 2

Can I ping192.168.168.88 on node1?

Take the address and mask (255.255.255.0) and calculate that 192.168.168.0 is different from the destination address 192.168.150.0. Skip the first entry and match the second entry and throw it to the router 192.168.150.2. This is the IPV4 Internet next-hop mechanism However, if you go out from gateway 192.168.150.2, you can't get back. Therefore, node1 can never ping 192.168.168.88 in this way, so you need to add an entry to the routing tableCopy the code

Route add host 192.168.88.88 gw 192.168.150.12

Add a host through the gateway to 192.168.150.12 that you can ping. This entry is only 192.168.88.88 will go through the gateway. The second entry in routing table 1 above shows that all unknown hosts will go through the default gatewayCopy the code

Notice that the mask of this new entry is 255.255.255.255. When the IP 192.168.88.88 and the mask are used to calculate and, the IP is forwarded to 192.168.150.12 as the previous target address, so it will pass through Only the same LAN can go through the gateway. If the LAN does not need to go through the gateway, the switch can go out. This is the process of static routingCopy the code

Let me show you the whole process using the diagram I drew in the last article

1, user space program HTTP protocol (text) 2, transmission control layer three handshake transmission data four break up process 3, how to go out of the host packet? Routing rules and the next mechanism at the network layer 4. After the next hop is found, how does the packet get to the next hop? The process of MAC address switching at the link layer 5 Application layer thread/process multiplexing NIO needs to focus on the IO modelCopy the code

A switch is different from a router

In the routing table, X indicates that the switch has a routing table at layer 3. Route A switch can view its MAC address. Router is a network layer and can view its IP address From A network to B network there must be A forwarding rules Home router is incorporated in the routers and switches home router with 4 interface can connect cable plugged into four machines This is the switch switch with a wire inserted inside is a router has two interfaces on a router One is the switch A switch is LAN IP operators A LAN has at least three network layers, link layer, and physical layerCopy the code