TCP/IP basics of Linux network Programming (i): TCP/IP protocol stack and datagram package __ block chain

Source: Internet
Author: User

First, Iso/osi Reference Model

The OSI (open System Interconnection) Open Systems Interconnection model is a network-layered model defined by ISO (International Organization for Standardization), a total of seven layers, The following figure.


Physical layer (physical Layer): The physical layer defines the specification of all electronic and physical devices, providing a physical medium for upper-level transmission, with bits (bit) in the data in this layer. The specifications belonging to the definition of this layer are Eia/tia RS-232, Eia/tia RS-449, v.35, RJ-45, etc., the actual use of equipment such as network cards belong to this layer.


Data link Layer (Layer): A data frame for the bit stream received by the physical layer. Provide reliable data transmission services to achieve error-free data transmission. In the data

The unit of data in the link layer is a frame. The specifications belonging to the definition of this layer are SDLC, HDLC, PPP, STP, Frame relay, etc., the actual use of devices such as switch switches belong to this layer.

Network layer (Network Layer): The network layer is responsible for routing, grouping and reorganizing the data between the subnets. The unit of data transfer in this layer is the packet (packet) (this packet can is either an IP datagram or a fragment of the IP datagram). The specifications that belong to this layer are IP, IPX, RIP, OSPF, ICMP, IGMP, and so on. Devices that are actually in use, such as routers, belong to this layer.


Note: Perhaps everyone is often confused by the terms of the packet or datagram, and the following gives a more accurate definition:

An IP datagram are the unit of End-to-end transmission at the IP layer (before fragmentation and after reassembly), and a P Acket is the unit of data passed between the IP layer and the link layer. A packet can be a complete IP datagram or a fragment of IP datagram.

Transport Layer (Transport Layer): Provides a reliable data transfer service that detects packets discarded by routers, and then produces a retransmission request that can reorder packets that are received in a disorderly order. The transmission unit in the Transport Layer data is a segment (segment).

Session Layer: Manages the session process between hosts, including management during session creation, termination, and sessions.

The presentation layer (presentation Layer): The presentation layer transforms the data transmitted by the network, so that the information transmitted between multiple hosts can understand each other, including data compression, encryption, format conversion and so on.

Application layer (Application Layer): The application layer communicates with the application interface to achieve the purpose that is presented to the user. In this common agreement are: HTTP,HTTPS,FTP,TELNET,SSH,SMTP,POP3, etc.


Two, TCP/IP protocol four-layer model

TCP/IP network protocol stack is divided into application layer (application), Transport layer (transport), Network layer (network) and link layer (link) four layers. As shown in the following figure, if there is no special description, the general reference of the graph is derived from the "TCP/IP detailed volume one."


The process of communicating between two computers through the TCP/IP protocol is as follows:


The transport layer and the following mechanisms are provided by the kernel, the application layer is provided by the user process, the application interprets the meaning of the communication data, and the transport layer and the details of the following processing traffic send the data from one computer to the other computer through a certain path. When application-layer data is sent over the network via a protocol stack, each protocol is prefixed with a data header (header), called Encapsulation (encapsulation), as shown in the following illustration:


The different protocol layer has different appellation to the packet, in the transport layer is called the segment (segment), at the network layer is called the Data Packet (packet), at the link layer is called the frame. The data is encapsulated into a frame and sent to the transmission medium, and then the corresponding header is stripped after each layer protocol, and finally the application layer data is submitted to the application for processing.
The above figure corresponds to two computers in the same network segment, if the two computers in different network segments, then the data from one computer to another computer transmission process to pass through one or more routers, as shown in the following figure:


In fact, under the link layer there is a physical layer, refers to the transmission of electrical signals, such as the current Ethernet network cable (twisted pair), early Ethernet used in the coaxial cable (now mainly for cable TV), optical fiber, etc. are the concept of the physical layer. The ability of the physical layer determines the maximum transmission rate, transmission distance, anti-jamming and so on. Hubs are network devices that work on the physical layer for twisted-pair connections and signal relaying (amplifying the decaying signal again to make it farther).

Link Layer has Ethernet, Token Ring network and other standards, the link layer is responsible for the driver of the network card device, frame synchronization (that is, from the network cable to detect what signals counted as the beginning of the new frame), conflict detection (if the conflict is detected automatically repeat), data error checking and so on. A switch is a network device that works at the link layer and can forward data frames between different link layer networks, such as between 10 Gigabit Ethernet and Gigabit Ethernet, between Ethernet and Token Ring network, because the frame format of different link layers is different, the switch will remove the link layer header and then forward it after the packet is removed.

The IP protocol of the network layer is the foundation of the Internet. Hosts on the Internet are identified by IP addresses, and a large number of routers on the Internet are responsible for choosing the right path to forward packets based on IP addresses, which often go through more than 10 routers from the source host on the Internet to the destination host. Routers are network devices that work on the third tier, with the function of a switch that forwards packets between different link layer interfaces, so routers need to remove the two-tier header of the network layer and the link layer and encapsulate it in the packet. The IP protocol does not guarantee the reliability of the transmission, the packet may be lost during transmission, and the reliability can be supported in the upper layer protocol or application.


The network layer is responsible for point-to-point (point-to-point) transmission (where "point" refers to a host or router), while the transport layer is responsible for end-to-end (end-to-end) transmission (where the "end" refers to the source and destination hosts). The transport layer can select either a TCP or UDP protocol. TCP is a connection-oriented, reliable protocol, a bit like a phone call, the two sides picked up the phone after the identity of the establishment of a connection, and then speak on the line, this side of the word there to ensure that hear, and is in the order to hear the words, said the hanging machine disconnected. In other words, TCP transmission of the two sides need to establish a connection, then by the TCP protocol to ensure the reliability of data transceiver, lost packets automatically, the top application received always reliable data flow, after the communication closed. UDP protocols are not connection-oriented, nor are they guaranteed to be reliable, a bit like sending letters, writing a letter in a mailbox, neither guaranteeing that the letter will not be lost in the mail, nor guaranteeing that the letter is sent to the destination in order. Applications that use UDP protocols need to do their own job of throwing BAOZHONGFA, sorting messages, and so on.


After the destination host receives the packet, how to reach the application at the end of each layer protocol stack. The entire process is shown in the following illustration:


The Ethernet driver first determines the payload of the data frame based on the "Upper protocol" field in the Ethernet header (payload, which means removing the actual data transmitted outside the protocol header), is the datagram of IP, ARP or RARP protocol, and then handing over the corresponding protocol. In the case of an IP datagram, the IP protocol then determines whether the payload of the datagram is TCP, UDP, ICMP, or IGMP, based on the "Upper layer protocol" field in the IP header, and then the corresponding protocol is processed. In the case of a TCP segment or UDP segment, the TCP or UDP protocol then determines which user process should be given the application-tier data according to the port number field of the TCP header or UDP header. The IP address is the address that identifies different hosts on the network, and the port number is the address on the same host that identifies the different processes, and the IP address and port number together identify the only process in the network.


Note that although IP, ARP, and RARP datagrams require an Ethernet driver to encapsulate frames, they are functionally divided, and ARP and rarp belong to the link layer, and IP belongs to the network layer. Although ICMP, IGMP, TCP, UDP data all need IP protocol to encapsulate the datagram, but from the functional partition, ICMP, IGMP and IP belong to the network layer, TCP and UDP belong to the transport layer.


Reference:

"Linux C Programming One-stop learning"

"TCP/IP details Volume One"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.