当前位置:网站首页>How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
2022-07-01 02:57:00 【wzyAcyy】
The book is based on a simple scenario : The user enters a... Into the browser URL The response returns , The life cycle of a network request .
The book is divided into six parts :
- Application layer client generation HTTP, Protocol stack delegated to the operating system
- Protocol stack (TCP/IP modular ) Call the network card driver to generate an electrical signal
- How does the network card reach the router used to access the Internet through the router
- Internet internal relay transmission
- arrive web The server passes the firewall first
- web How does the server collect data
The second chapter mainly introduces , How does the protocol stack and network card in the operating system send application messages to the server :
- Create socket
- Connect to server
- Sending and receiving data
- Disconnect from the server and delete the socket
- IP Packet sending and receiving operation with Ethernet
- use UDP Operation of sending and receiving data
This paper introduces the fifth chapter ,IP Packet sending and receiving operation with Ethernet .
0. Abstract
IP The address is actually assigned to the network card
MAC The address is written when the network card is produced ROM The value in
As long as there is The receiving party MAC Address , The sender MAC Address , Ethernet type These three features are Ethernet
ARP The main function of the protocol is to IP Address resolves to physical address
Routing tables store paths to specific network addresses
1. What is a bag
Packets are in the seven layer network model The network layer Transmission data unit , Also known as network packet . Package structure : Head + data . The head contains Destination address And so on , The head can be understood as the face order of express , The data is the goods in the package
Packet transmission process :
- The sender Create a package , Send the packet to the nearest network forwarding device .
- The forwarding device will judge the next forwarding device according to the information in the header . The forwarding process requires a table of addresses
- After multiple forwarding, it arrives at the receiver's network device .
The sender and receiver are collectively referred to as terminal nodes , The forwarding device is an intermediate node .
Create a package : Generate information with correct control ( Destination address ) The head of , Attach the data to be sent
What are the tables in the network device ? Record the sending direction of each address , Query in the table according to the destination address in the header .
TCP/IP Package is based on package ,TCP/IP The five layer model is based on the seven layer network model .
TCP/IP The package encapsulates the application layer , Transport layer , The network layer , Data link layer . The header encapsulates the network layer and the data link layer , Data encapsulates the application layer and the transport layer .
give an example :
Data link layer : Ethernet
The network layer :IP( Fix )
Transport layer :TCP( Fix )
application layer :HTTP
TCP/IP The header contains MAC head ( For Ethernet protocol ) and IP head ( be used for IP agreement ).
TCP/IP In the network TCP/IP The whole process of packet transmission :
- The sender will aim at IP write in IP head ,IP The protocol finds the next router based on this address IP,IP The protocol is based on the next route IP Find the Ethernet address , write in MAC head .
- The forwarding device will judge the next forwarding device according to the information in the header , The forwarding process involves routing tables and Ethernet tables .
- Final TCP/IP The bag will arrive at its destination .
IP And Ethernet : Ethernet can be replaced by other networks , Such as wireless LAN 、FTTH etc. , Separate the network layer from the data link layer , It can better use various communication technologies as required .
2. Overview of packet sending and receiving operations
In the protocol stack IP How does the module complete the receiving and sending operation , The first step of corresponding packet transmission .
IP Module work content :
- The sender TCP The module adds TCP head , Pass to IP modular , This part is the content of the network package .
- IP After receiving the delegation, the module adds IP The head and MAC head (IP The header contains the control information required to send to the destination ,MAC The header contains the control information that transmits the packet to the nearest router through the Ethernet LAN ),IP The module will TCP/IP Send the packet to the network card , The network card will convert the binary into an optical signal and continue to transmit .
- The receiving network card receives the photoelectric signal and converts it into a digital signal to IP modular .IP Module analysis IP The head and MAC After the header, the data (TCP head + data ) Pass to TCP modular .
- TCP Module analysis TCP Headers pass data to applications .
In the process of receiving and sending Key points :
TCP The module is divided into several stages when sending and receiving data , And design corresponding function network package for each stage (TCP Three handshakes , Four waves and so on ).
IP The packet sending and receiving operations are the same , about IP In terms of modules TCP The header and data are binary data , I don't care about the content when sending and receiving . All in all IP The responsibility of the agreement is to package data , Sending and receiving data .
3. Generate IP Head
IP Header format
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Three important fields :
Source Address source address , The sender IP.
Destination Address Destination address , The receiving party IP.
Protocol Protocol type 6bit: Indicates which protocol the package comes from ,TCP Agreement No 06( Hexadecimal ),UDP Number 16( Hexadecimal ), The agreement number is detailed in RFC790, ASSIGNED NUMBERS
IP The address is actually assigned to the network card , If a computer has multiple network cards , The sender IP How to fill in ? How to decide which network card to give the package to ?
Judge by routing table .
View routing table instructions route print
IPv4 Routing table
===========================================================================
Active routing :
Network goals Network mask gateway Interface Jump points
0.0.0.0 0.0.0.0 192.168.0.1 192.168.0.10 50
127.0.0.0 255.0.0.0 On the link 127.0.0.1 331
127.0.0.1 255.255.255.255 On the link 127.0.0.1 331
127.255.255.255 255.255.255.255 On the link 127.0.0.1 331
192.168.0.0 255.255.255.0 On the link 192.168.0.10 306
192.168.0.10 255.255.255.255 On the link 192.168.0.10 306
192.168.0.255 255.255.255.255 On the link 192.168.0.10 306
224.0.0.0 240.0.0.0 On the link 127.0.0.1 331
224.0.0.0 240.0.0.0 On the link 192.168.0.10 306
255.255.255.255 255.255.255.255 On the link 127.0.0.1 331
255.255.255.255 255.255.255.255 On the link 192.168.0.10 306
===========================================================================
Network goals : The ultimate goal of the network package
gateway : Forwarding router IP. If the gateway is the same as the interface , Then go directly to the target IP Send package .
Interface : The sender IP
Jump points : Transmission costs , The smaller the number, the lower the cost , Metric per forwarding -1, Metric change 0 after , Transmission failed .
Both the network target and the netmask are 0.0.0.0, Indicates the default gateway . If sender IP Does not match other network targets , Automatically match this line .
4. For generating Ethernet MAC Head
The receiving party IP Indicates the final destination of the package , If there is no matching network target , Go to the default gateway , The goal is MAC Address Must match the network target .
MAC Head
The receiving party MAC Address 48bit
The sender MAC Address 48bit
The type of ether 16bit
0000-05DC:IEEE 802.3
0800:IP agreement
0806:ARP agreement
86DD:IPv6
MAC The address is written when the network card is produced ROM The value in , Set sender MAC As long as the MAC Address read write MAC Just head .
The sender MAC You need to find the next forwarding device through the routing table IP, And then according to IP Address the query MAC Address .
MAC Address :Media Access Control Address, Also known as LAN address (LAN Address), Ethernet address (Ethernet Address) Or physical address (Physical Address), The address used to confirm the location of the network device
5. adopt ARP Query the destination router's MAC Address
ARP:address resolution protocol, Address resolution protocol .
ARP Use Ethernet broadcasts to ask questions about all devices :“ This XXX IP Is it yours ?”, Then someone will answer :“ This is my , my MAC The address is XXX”.
ARP There's also caching , You can use commands ARP -a see :
Internet Address Physical address type
192.168.0.1 70-af-6a-74-7b-c8 dynamic
192.168.0.7 24-05-0f-e6-8b-54 dynamic
192.168.0.255 ff-ff-ff-ff-ff-ff static state
224.0.0.2 01-00-5e-00-00-02 static state
224.0.0.22 01-00-5e-00-00-16 static state
224.0.0.251 01-00-5e-00-00-fb static state
224.0.0.252 01-00-5e-00-00-fc static state
239.255.255.250 01-00-5e-7f-ff-fa static state
255.255.255.255 ff-ff-ff-ff-ff-ff static state
The failure time is usually a few minutes , If changed IP And MAC Correspondence of , It takes a few minutes to take effect .
here ,IP The module gets MAC Address , write in MAC Send the header to the network card . If the data has been packed before the network card is sent , The network card can adapt to various types of packages .
Experienced TCP head IP head Routing table MAC head ARP agreement ARP cache ,IP This concludes the module .
Next, it's the network card's turn .
6. Ethernet Basics
Before introducing the network card, first understand Ethernet .
At present, Ethernet mostly adopts switches ( Switching hub ) The pattern of , The landscape has experienced many changes , As long as there are these three features, it is Ethernet :
- The receiving party MAC Address
- The sender MAC Address
- Ethernet type
Ethernet only cares about sending and receiving packets , Don't care about the actual content of the network package
7. take IP Packets are converted into photoelectric signals and sent out
IP A packet is just a digital signal in memory , It is converted into photoelectric signal by network card .
The general structure of the network card :
- ROM: Deposit MAC The location of the address
- buffer , Temporary storage of network packets
- MAC modular , Where Ethernet transceiver operation is controlled
- PHY(MAU), Where the signal is sent and received
- RJ-45 Interface , Connect the base of the network cable
MAC Address The only global , Write... During production .MAC The address will be driven by the network card from ROM Read and write MAC modular .
8. Add three more control data to the network packet
After the network card driver obtains the package , It will be copied to the buffer of the network card .MAC modular , Remove the package from the network card buffer , Add header and start frame separator at the beginning , Add... At the end FCS( Frame check sequence ).
| Header and start frame delimiter | MAC Head | IP Head | TCP Head | ------ data ------| FCS |
| Network card production | IP Module production | TCP production | Application production | Network card production |
TODO To be continued
边栏推荐
猜你喜欢
随机推荐
Dell服务器重启iDRAC方法
Dell server restart Idrac method
【机器学习】向量化计算 -- 机器学习路上必经路
【EXSI】主机间传输文件
ipmitool下载地址和编译安装时可能出现的问题
Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
Dart training and sphygmomanometer inflation pump power control DPC
咱就是说 随便整几千个表情包为我所用一下
MCU firmware packaging Script Software
Prototype and prototype chain in JS
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)
PTA 1016
PTA 1017
Mouse over effect 8
STM32 - DS18B20 temperature sampling of first-line protocol
Sampling Area Lights
使用ipmitool配置X86服务器的BMC网络和用户信息
If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
Share Creators萌芽人才培養計劃來了!








