Linux Network Programming - 7. TCPIP Protocol

xiaoxiao2021-03-06  111

You may have heard of the TCP / IP protocol, then you know what is TCP, what is IP? In this chapter, let's learn this most extensive agreement on the current network. 7.1 Network Transmission hierarchy If you After testing computer level exams, then you should already know this concept of network transfer. On the network, people are divided into seven hierarchicals in order to transmit data, and the transfer of networks into 7 levels. Subsequent: Application layer, represent layer , The session layer, the transport layer, the network layer, the data link layer and the physical layer. After the layer is divided, when the data is transferred, if you want to data, you can go directly to the next layer, not the need to manage Details of data transmission. The next layer is only available to its previous layer, not to manage other things. If you don't want to exam, you don't have to remember these things. Just know that it is hierarchical, and each The function of the layer is different. 7.2 IP protocol IP protocol is the protocol of the network layer. It mainly completes the transmission of the packet. The following table is the IP4 packet format 0 4 8 16 32 -------------- ----------------------------------------- | Version | Header | Service Type | Data Total package | --------------------------------------------- --- | Identify | DF | MF | Debris offset | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------- | Survival Time | Agreement | The first test and | ----------------------- ----------------------- | Source IP Address | ---------------------- --------------------------------------------- ------------------------------ | Options | ================= ================================================ --------------- ---------------------------------- Let's take a look at the structure of IP Struct IP {#IF __BYTE_ORDER = = __Little_endian unsigned int ip_hl: 4; / * Header length * / unsigned int ip_v: 4; / * Version * / #ENDIF #IF __BYTE_ORDER == __BIG_ENDIAN UNSIGNED INT IP_V: 4; / * Version * / unsigned int ip_hl: 4; / * header length * / #ENDIF u_int8_t ip_tos; / * type of service * / u_short ip_len; / * Total length * / u_short ip_id; / * identification * / u_short ip_off; / * fragment offset field * / #define IP_RF 0x8000 / * reserved fragment flag * / #define IP_DF 0x4000 / * dont fragment flag * / #define IP_MF 0x2000 / * more Fragments flag * / #define ip_offmask 0x1ff / * mask for fragmenting bits * / u_int8_t ip_ttl; / * time to live * / u_int8_t ip_p;

/ * protocol * / u_short ip_sum; / * checksum * / struct in_addr ip_src, ip_dst; / * source and dest address * /}; IP_vip protocol version number, here is 4, now IPv6 has already come out of the first length of IP_HLIP package, this The value is in 4 bytes. The fixed length of the head is 20 bytes, and if the IP package is not option, this value is 5. IP_tos service type, the priority provided. IP_LEN means the length of IP data. Bytes are in units. IP_ID identifies this IP packet. IP_off fragment offset, this with the above ID used to recombinate the fragment. IP_TTL survival time. If there is no way to lose one, until 0 is abandoned. IP_P protocol Represents a high-level protocol for creating this IP packet. Such as TCP, UDP protocol. IP_SUM header checksum, providing verification of the header data. IP_src, ip_dst sender, and receiver's IP address about IP protocol, please Refer to RFC791 7.3 ICMP Protocol ICMP is a message control protocol. When you pass an IP packet on the network, if an error occurs, you will use the ICMP protocol to report an error. The structure of the ICMP package is as follows: 0 8 16 32 -------------------------------------------------- ------------------- | Type | Code | Check: ---------------------- ---------------------------------------------- | Data | | ----------------------------------------------------------------------------- ------------------- ICMP is defined in the Struct ICMphDr {u_int8_t type; / * message type * / u_int8_t code; / * type sub-code * / u_int16_t checksum ; union {structure {u_int16_t id; u_INT16_T sequence;} echo; / * echo datagram * / u_int32_t gateway; / * Gateway address * / s Truct {u_int16_t __unused; u_int16_t mtu;} frag; / * path mtu discovery * /} UN;}; About the ICMP protocol Details You can view the RFC792 7.4 UDP protocol UDP protocol is based on the IP protocol, used in transmission Layers of protocols. Theudp and IP protocols are unreliable data report services .udp's head format is: 0 16 32 ------------------------ --------------------------- | UDP source port | UDP destination port | -------------- ------------------------------------- | UDP Data Report Length | UDP Data Report | -------------------------------------------------- The UDP structure is defined in: struct udphdr {u_int16_t source; u_INT16_T DEST; U_INT16_T LEN; U_INT16_T CHECK;};

For details on the UDP protocol, please refer to the RFC768 7.5 TCP TCP protocol is also based on the IP protocol, but the TCP protocol is reliable. The .TCP data structure is complicated in the previous structure. 0 4 8 10 16 24 32 ------------------------------------------------------------------------------------------------------------------------------ --------------------- | Source port | destination port | ---------------------- --------------------------------------------- | Serial number | -------------------------------------------------- --------------- | Confirmation Number | ----------------------------------------------------------------------------------------- ----------------------------------- | | | U | A | P | s | f | | | First Length | Reservation | R | C | S | Y | i | Window | | | | | | ------------------- ---------------------------------------------- | Checksum Emergency pointer | ---------------------------------------------- ------------------- | Options | Fill bytes | ------------------------ ---------------------------------------- TCP structure is defined in: Struct tcphdr {u_int16_t source; u_int16_t dest; u_int32_t seq; u_int32_t ack_seq; #if __BYTE_ORDER == __LITTLE_ENDIAN u_int16_t res1: 4; u_int16_t doff: 4; u_int16_t fin: 1; u_int16_t syn: 1; u_int16_t rst: 1; u_int16_t psh: 1; U_INT16_T ACK: 1; U_INT16_T URG: 1; U_INT16_T RES2: 2; #ELIF __BYTE _Order == __big_endian u_int16_t doff: 4; u_int16_t res1; u_int16_t res2: 2; U_INT16_T URG: 1; U_INT16_T ACK: 1; U_INT16_T PSH: 1; U_INT16_T RST: 1; U_INT16_T SYN: 1; U_INT16_T FIN: 1; # ENDIF U_INT16_T WINDOW; U_INT16_T CHECK; U_INT16_T URG_PRT;}; Source Send the source port DEST of the TCP data to accept the destination port SEQ identifying the TCP data The start sequence number ACK_SEQ confirmed by the data byte included in the TCP, indicating the next time Accept data serial number. DOFF data header. Like IP protocol, in 4 bytes. Generally, 5 URG If the emergency data pointer is set, the bit is 1 Ack If the confirmation number is correct, then 1 PSH If set to 1, after the receiver receives the data, it is immediately handed over to the previous layer program RST. When the request is reconnected SYN is 1, it means that the relative is closed when the request is established. The Window window tells the recipient to receive the size Check for TCP data for the TCP data. If URG = 1, it is pointed out that the offset value of the serial number starting from the historical data is detailed about the TCP protocol. For details, please see RFC793 7.6 TCP The Connection TCP protocol is a reliable connection. In order to ensure the reliability of the connection, the TCP connection is divided into several steps. We call this connection process as "three handshake"

转载请注明原文地址:https://www.9cbs.com/read-103032.html

New Post(0)