Chapter 2 transport layer ,TCP,UDP, and SCTP
1. Brief introduction to the agreement
UDP It's a simple one , Unreliable transport protocol , and TCP It's a complex , Reliable byte stream protocol .
Introduction to protocol family : Internet protocol family ( English :Internet Protocol
Suite, The abbreviation is IPS), It's a network communication model , And a whole network transmission protocol family , Infrastructure for the Internet . It is often referred to as TCP/IP protocol family ( English :TCP/IP
Protocol Suite, or TCP/IP
Protocols), abbreviation TCP/IP. Because of the two core agreements of this protocol family , include TCP( Transmission control protocol ) and IP( Internet Protocol ), The first standard adopted in this family . Because the network communication protocol generally adopts the layered structure , When multiple layers of protocols work together , Similar to the stack in Computer Science , Therefore, it is also called TCP/IP Protocol stack ( English :TCP/IP
Protocol Stack) . These agreements originated in the United States Department of defense ( The abbreviation is DoD) Of ARPA Net project , So it's also called DoD Model (DoD
Model). This protocol suite is maintained by the Internet Engineering Task Force . [1]
ICMP meaning :
ICMP yes (Internet Control Message
Protocol)Internet control message protocol . It is TCP/IP A sub protocol of protocol family , Used in IP host , Transmission of control messages between routers . Control message means that the network is not working , Is the host accessible , Whether the route is available or not . These control messages do not transmit user data , But it plays an important role in the transmission of user data .
unix Introduction to the framework of the agreement ( It can be memorized by full English name ):
IPV4: Internet Protocol version 4. Since the early 1980s, it has been the main protocol of the internet protocol family . use 32 Bit address .IPV4 to TCP,UDP,SCTP,ICMP,IGMP Provide packet delivery service
IPV6 agreement : Internet Protocol version 6,20 century 90 Mid-s as IPV4 Substitute for , The main change is to use 128 A larger address , answer 20 century 90 The explosive growth of the Internet since the S
TCP: Transmission control protocol .TCP Is a connection oriented protocol , Provide full duplex byte stream for users .TCP Is a stream socket .TCP Concern confirmation , Details like timeout and retransmission .TCP Both can be used IPV4, It can also be used IPV6
UDP: user datagram protocol .UDP Is a connectionless protocol ,UDP Is a packet socket .UDP There is no guarantee that the data will reach its destination , And TCP Both can be used IPV4 also have access to IPV6
SCTP: Flow control transport protocol .SCTP Provides a reliable full duplex connection oriented protocol , We use association once to refer to SCTP Connection in , because SCTP It's multihomed , Thus, each end of the association involves a set of addresses and a port number .SCTP Provide message service , It also maintains the record boundaries from the application layer . And TCP and UDP
equally ,SCTP Both can be used IPV4, It can also be used IPV6
ICMP: Internet control message protocol .ICMP Handle error and control messages that flow between routers and hosts . These messages are usually provided by TCP\IP Network support software itself ( Not the user process ) Generated and processed . use ping and traceroute The program can be used the same way ICMP. Sometimes we call this Agreement ICMPV4, In order to communicate with ICMPV6 difference
IGMP: Internet Group Management Protocol , More for broadcasting
ARP: Address resolution protocol .ARP Put one IPV4 The address is mapped to a hardware address .ARP It is commonly used for example, Ethernet , Token ring and FDDI Etc. broadcast network , No need for point-to-point cyber soldiers
RARP: Reverse Address Resolution Protocol .RARP Resolve a hardware address into a IPV4 address . It is sometimes used to boot a diskless node .
ICMPv6: Internet control message protocol version 6, Integrated ICMPv4,TGMP and ARP Function of
BPF:BSD Grouping filter . The interface provides access to the data link layer , Usually in the yard Berkeley Found in the kernel of
DLPI: Data link provider interface . The interface also provides access to the data link layer , Usually with SVR4 Kernel provided
All Internet protocols follow RFC Formal specification for
2.TCP connect ( Transmission control protocol )
TCP Establish a connection before a given server , Data exchange is established with the server across connections , Finally terminate the connection
TCP After data transmission failure , It can last 4~10 Retransmission in minutes
TCP After the server receives the data sent by the client , It will be arranged according to the order sent by the client , If duplicate data is sent due to network congestion ,TCP The server will discard the duplicate data
TCP Reliable flow control is provided ,TCP It always tells the peer how much data it can receive at a time And the remaining buffer size , When the buffer is full , When the server must read data from the buffer , Before receiving data from the opposite end
TCP Is a full duplex protocol
3.SCTP( Streaming control protocol )
SCTP And tcp The difference is multihomed , Moreover, sending messages is a module by module message oriented and connection oriented protocol
4.TCP Establishment and termination of
Three way handshake
1)TCP Be ready to receive incoming connections , adopt socket listen bind Equifunction
2) Client through connect Establishing a connection , Send one SYN serial number
3) The server must confirm (ACK) Customer's SYN, And then send a SYN Section of
4) The client confirms the server's SYN
every last SYN Of ACK That's the confirmation number SYN plus 1
every last FIN The confirmation number is FIN plus 1
TCP option
TCP option :
MSS option (maximum segment size) Namely MSS, Notifies the peer of its maximum segment size , parameter TCP_MAXSEG You can set this TCP option
Window size options : What is the maximum window size of the notification peer 65535, It can be done through SO_RCVBUF Socket options to affect this option
timestamp option .
TCP Connection termination ( four-way handshake )
1) An application first calls close, We call it active shutdown . The TCP So send one FIN Segmentation , Indicates that the data transmission is completed
2) Received this FIN Perform passive shutdown on the opposite end of ,FIN Receiving means that no data is received on the corresponding connection
3)TCP One will also be sent FIN
4) Receive this finally FIN The original sender confirms this FIN
similar SYN, One FIN It also takes up one byte of space , each FIN Of ACK That's the number FIN The serial number of 1
TCP Connection status
TCP Defined for a connection 11 States
Sent by one party SYN Then his state is SYNSENT, Receive a paragraph and send it ACK+1 The state of the SYNRCVD, If it is too late to send ACK When confirmed, it becomes ESTABLISHED
If an application receives a FIN Previous call close, Will be converted to FINWAIT
1 state . But if an application is in ESTABLISHED One was received during the state FIN, So convert it to one CLOSE WAIT state
Actively turn off sending FIN, The status changes to FINWAIT1
Received by receiver FIN Will become CLOSEWAIT Compared with FINWAIT_2
Then send it ACK Confirmation code and FIN Will become LASTACK The opposite end becomes LASTACK
The sender sends again ACK code , At this point, the opposite end will become CLOSED state
TIME_WAIT state
TIME_WAIT When a real routing exception is lost , Spend some time looking for a state manifestation of another path , The duration of the breakpoint is generally 2MSL(maximum segment
lifetime)
TIME_WAIT Reasons for existence :
1) Reliable implementation TCP Full duplex
close The active end of is likely to appear TIME_WAIT state , Because he had to repeat it ACK To the other end , To ensure the reliability of the data
2) Allow duplicate segments to disappear in the network
The same ip And the port is repeatedly bound , Ensure that the previous duplicate packets disappear in the network
4.SCTP Establishment and termination of association of
Establishing a connection ( four-way handshake )
1) Establish connection through socket,bind and listen These three functions
2) Client call connect The associated message is opened implicitly and actively . This allows users to sctp Send one INIT News of , This message tells the server client ip And initial serial number , Initial tag used to identify all groups in the association , The number of outbound flows requested by customers and the number of outbound flows that customers can support .
3) Server with a INIT ACK Code to confirm the customer's INIT news , Which contains the server's ip Address list , Initial serial number , Starting mark , The number of outgoing streams requested by the server
The number of external streams that the server can support , And a state cookie. state cookie Contains all the states the server needs to be sure that this association is valid , He is the digital signature to ensure his validity
4) Customers use one COOKIE ECHO The status of the echo server cookie, handle cookie echo External , The message may also be bundled with user data in the same packet
5) The server uses a COOKIE ACK The message confirms the customer's echo cookie It's right
Termination of Association
SCTP Semi closing is not allowed , When one end closes an association , The other end must stop sending data
SCTP No, TIME_WAIT state
( because SCTP stay unix It's a relatively new agreement , And it didn't push it out on a large scale , So I stopped watching it )
5. Port number
TCP,UDP and SCTP Using 16 The port number of a bit integer (1 byte 2 individual 16 System )
The server specifies a port , A client is a temporary port
Division of port number :
0~1023 These ports are supported by IANA control , Is the system reserved port
Registered port is :1024~49151
49152~65535 It is a dynamic or private port ,IANA It doesn't care about these ports . They are what we call temporary ports
6.TCP The port number is concurrent with the server
stay unix The common practice in network programming is that the master server processes every new connection through a loop , What happens if a child process continues to use the server's well-known port to service a long request ? All destination ports on the server are 21 Other TCP The segments are passed to the original server that has the monitor set ( This is the initial parent process ), A child process is then derived to handle client requests .
7. Buffer size and limits
IPV4 The maximum size of the datagram is 65535 byte , include IPV4 The first
IPV6 The maximum packet size is 65575 byte
Be a IP When a datagram will be sent from an interface , If the size exceeds the corresponding link MTU,IPV4 and IPV6 Fragmentation will be performed
8.TCP output
every last TCP The socket has a send buffer , We can use it SO_SNDBUF To change the size of the buffer . When an application calls write When , The kernel copies all data from the buffer of the application process to the send buffer of the socket being written . If the send buffer of the socket cannot hold all the data of the application process ( Or the application process buffer is larger than the sending process buffer , Or the socket buffer already has other data ), The application process will hibernate ,write It will not be returned until all data in the application process is written to the socket buffer . However, it does not mean that the opposite end has received the data
tcp The data sending end of extracting buffer , Opposite end tcp
The data received must be confirmed , Opposite end ack After arrival TCP Then the confirmed data in the buffer will be dropped .TCP You must keep a copy of one of the data that has been sent , Until we receive confirmation from the other party
9.UDP output
whatever UDP All sockets have the size of the send buffer , We can use it SO_SNDBUF, He just changed the maximum size limit written to socket datagrams , If a process writes a datagram larger than the size of the send buffer , The kernel returns a EMSGSIZE Error of ,UDP There's no need for a real send buffer because it's unreliable , It is usually copied into a kernel buffer of some format , After the data is sent , This copy is discarded by the link layer
If udp write A successful return indicates that all data has been added to the link layer output queue , If there is no space to store a piece of data , The kernel usually returns a ENOBUFS Error to application process
Technology
Daily Recommendation