Basic knowledge of TCP/IP

Source: Internet
Author: User
Tags ack control characters dsap

Recently work needs themselves in the card embedded TCP/IP protocol stack, because after all, SCM performance is limited, complete TCP/IP stack is very large, so can only be ported simplified, has been successfully ported LWIP protocol stack to the project board, but the boss said the stack is too cumbersome to write, Especially in memory management this piece, not easy to understand, and our board to the accuracy of the data requirements are not so high, the focus is to ensure that the line, the occasional loss of one or two packets are okay, so according to this demand self-write protocol stack, the implementation of UDP and Telnet communication, the current UDP has been implemented, the remaining functions are slowly increasing, Through this period of time research found that the implementation of UDP is not difficult, the most important thing is to understand some basic concepts of network communication, collected a lot of information, now will feel more useful basic knowledge introduced as follows.

General overview of Ethernet

The name "Ethernet" originates from a scientific hypothesis: 19th century scientists generally believe that light travels through a substance called ether. To this day, the existence of ether has long been denied by Einstein's special theory of relativity, and Ethernet has become the mainstream computer network, still for human transmission of light and electromagnetic information.

Ethernet (Ethernet) refers to the baseband LAN specification created by Xerox and jointly developed by Xerox, Intel, and Dec, which is the most common standard of communication protocol used by existing LANs today. The Ethernet network uses CSMA/CD (carrier monitoring multiplexing and collision detection) technology and operates on multiple types of cables at 10m/s rates. Ethernet is similar to the IEEE802.3 series standard.

Includes standard Ethernet (10MBIT/S), Fast Ethernet (100MBIT/S), and 10G (10GBIT/S) Ethernet. They all conform to IEEE802.3.

The following two topologies are mainly available:

General line: The required cable less, inexpensive, high management costs, difficult to isolate the point of failure, the use of shared access mechanism, easy to cause network congestion. The early Ethernet uses the bus-type topology, uses the coaxial cable as the transmission medium, the connection is simple, usually does not need the specialized network equipment in the small-scale network, but because of its inherent flaw, has been gradually replaced by the hub and the switch as the core star network. Such as:

Total Linetype

Star: Easy to manage, easy to expand, need dedicated network equipment as the core node of the network, need more cable, the reliability of the core equipment requirements high. Using a dedicated network device (such as a hub or switch) as the core node, a twisted pair connects each host in the LAN to the core node, which forms a star-shaped structure. Although the star network requires more cables than the total linetype, wiring and connectors are cheaper than bus type. In addition, the star topology can be cascaded in a convenient way to extend the network to a large scale, so it has been widely used by most of the Ethernet.

1. How Ethernet Works

The Ethernet uses a carrier frame listening multi-access (CSMA/CD) mechanism with collision detection. All the information sent in the network can be seen by the nodes in the Ethernet, so we say that Ethernet is a broadcast network.

The working process for Ethernet is as follows:

When a host in an Ethernet network transmits data, it will proceed as follows:

1, listen to the channel on whether there is a signal in the transmission. If any, indicates that the channel is busy and continues listening until the channel is idle.

2. If no signal is heard, the data will be transmitted.

3, the transmission of the time to continue to monitor, such as the discovery of a conflict to perform the backoff algorithm, randomly wait for a period of time, re-execute Step 1 (when the conflict occurs, the computer involved in the conflict will be sent back to the listening channel state.

Note: Each computer is allowed to send only one packet at a time, a congestion sequence to warn all nodes)

4. If no conflict is found, sending succeeds, all computers must wait 9.6 microseconds (running at 10Mbps) after the most recent send before attempting to send data again.

2. Frame format

There are four different frame formats for Ethernet, but there are 64bit (8 byte) leading characters at the beginning of each Ethernet frame, where the first 7 bytes are called the Pre-sync code (preamble), the content is 16 binary 0xAA; The last 1 bytes are the frame start identifier 0xAB It identifies the beginning of the Ethernet frame, the role of the leading character is to synchronize the receiving node and prepare to receive the data frame. Such as:

The leading characters are the real beginning of the various types of frames, and can be broadly divided into the following four categories:

2.1. Ethernet II

That is, Dix 2.0:xerox and Dec, Intel in 1982, the development of the Ethernet standard frame format.

Ethernet Type II Ethernet frames have a minimum length of 64 bytes (6+6+2+46+4) and a maximum length of 1518 bytes (6+6+2+1500+4). The first 12 bytes respectively identify the source node MAC address of the sending data frame and the destination node MAC address of the receiving data frame. (Note: ISL can be packaged up to 1548 bytes, up to 1522 bytes in 802.1Q package).

The next 2 bytes identify the upper-layer data types that the Ethernet frames carry, such as:

0X0800:IP Agreement;

0x0806:arp Agreement;

0x809b:appletalk protocol data;

0x8138:novell type protocol data.

After an indefinite Long data field is a 4-byte frame check sequence (frame check Sequence,fcs), the data from the target MAC address field to the data field is verified with a 32-bit CRC cyclic redundancy check.

2.2. Ethernet 802.3 Raw

Novell published the private Ethernet standard frame format in 1983, as shown in.

In the Ethernet802.3 raw type Ethernet frame, the Type field in the original Ethernet II Ethernet frame is replaced by the "total length" field, which indicates the length of the subsequent data field, with a value range of: 46~1500.

The next 2 bytes are a fixed 16 binary number 0xFFFF, which identifies this frame as Novell Ethernet type data frame.

2.3. Ethernet 802.3 SAP

IEEE published the Ethernet802.3 SAP version of the Ethernet frame format in 1985, as shown in:

As can be seen in the Ethernet802.3 sap frame, the original Ethernet 802.3 raw frame 2 bytes of 0xFFFF into each 1 bytes of DSAP and SSAP, while increasing the 1-byte "Control" field, constitutes the header of the 802.2 Logical Link Control (LLC). LLC provides network services with no Connection (LLC Type 1) and connection-oriented (LLC type 2). LLC1 is applied to Ethernet, while LLC2 is used in Ibmsna network environments.

The new 802.2LLC header includes two service access points: The source service access point (SSAP) and the target service access point (DSAP). They are used to identify the upper-layer data types that are carried by Ethernet frames, such as 16-digit 0x06 for IP protocol data, 16-digit 0XE0 for Novell type Protocol data, and 16 binary 0xf0 for IBM NetBIOS type protocol data.

As for the 1-byte "control" field, it is largely unused (typically set to 0x03, indicating a 802.2 unnumbered data format with no connection service).

2.4. Ethernet 802.3 SNAP

IEEE in 1985 released the Ethernet802.3 snap version of the Ethernet frame format:

Ethernet 802. 3SNAP type Ethernet frame format and Ethernet 802. The main differences between 3SAP type Ethernet frame formats are:

The contents of the 2-byte Dsap and SSAP fields are fixed, with a value of 16 binary 0xAA.

The contents of the 1-byte control field are fixed, with a value of 16 decimal 0x03.

The snap field is added, consisting of the following two items:

Added a 3-byte organization unique identifier (organizationally unique Identifier,oui ID) field whose value is usually equal to the first 3 bytes of the MAC address, which is the network adapter vendor code.

The 2-byte Type field identifies the upper-layer data type that is carried by the Ethernet frame.

3. TCP/IP protocol 3.1. Brief introduction

TCP/IP: Transmission Control Protocol/Internet connection, also known as network Communication protocol, is the most basic protocol of Internet, the foundation of Internet, the network layer of IP Protocol and Transport layer of TCP protocol, TCP/IP defines how electronic devices connected to the Internet, And the standard of how data is transferred between them. In layman's terms: TCP is responsible for discovering transmission problems, signaling it when there is a problem, requesting retransmission until all data is safely and correctly transmitted to the destination, and IP is an address for each networked device on the Internet.

It is not the TCP and IP two protocol collectively, but refers to the entire IP protocol family, OSI (open System Interconnect Reference Model) TCP/IP is divided into seven layers, the actual use we can simplify it to four layers, such as:

Ymodem is also a kind of xmodem realization. It includes all the features of the xmodem-1k, plus the batch file transfer mode is added for sending a set of files during a single session.

3.2. The contents of the agreement are 3.2.1. Protocol-related control characters

SOH 0x01

STX 0x02

EOT 0x04

ACK 0x06

NAK 0x15

CAN 0x18

Ctrlz 0x1A

3.2.2. Standard frame Format (128 bytes)

SOH

Message packet number

The complement of the package number

Data segment

Checksum

Start byte

Package number

Take back plus 1

128

Data segments and

The 1k-xmodem frame format is as follows:

STX

Message packet number

The complement of the package number

Data segment

Checksum

Start byte

Package number

Take back plus 1

1024

Data segments and

Description

The opening byte of the SOH frame, which represents the first byte in the packet

Packet Sequence Number: to the 256 modulus to the current package number, the first packet is 1, and the packet sequence range 0~255;

The complement of the information packet ordinal: the complement of the current information packet number;

Data segment: The length of the data segment is fixed to 128 bytes (the last segment data byte count is certainly less than 128, populated by Ctrlz), its content is not any restriction, can be text data or binary data;

Arithmetic checksum: 1 bytes of arithmetic checksum, only after summing the data section to 256 modulo;

3.2.3. Transport logic

1> send and receive both sides dial-up connectivity, the sender waits for a NAK signal from the receiving party. When the first nak arrives, the sender interprets the first packet as starting to send;

2> when the sender receives the first NAK and initiates the transmission, the sender transmits the data in a frame format of 128 bytes each time, and waits for the receiver's acknowledgement signal;

3> the sender receives an ACK signal from the receiving party, explaining that the packet is properly received and has the meaning of sending the next packet;

4> the sender receives a NAK signal from the receiving party and interprets the request to resend the same packet;

The 5> sender receives the CAN signal from the receiving party and interprets it as a request to stop the transmission unconditionally;

6> the sender to complete the normal transmission of all data, need to end normally, send EOT signal to notify the receiver. The receiving party confirms with ACK;

7> receiver sends can unconditionally stop the transmission process, the sender receives can, does not send the EOT confirmation;

8> Although the packet is an SOH to mark the beginning of a packet, but the EOT in the SOH position indicates the end of the data transmission, there is no information sent over;

9> receiver should first confirm the integrity of the packet ordinal, by filling the packet ordinal, and then the complement of the message packet ordinal or, the result is 0 is correct, the result is not 0 send Nak request retransmission;

10> the receiver confirms that the packet number is correct, and then checks to see if the expected sequence number. If it is not expected to get the packet sequence number, indicating that a serious error occurred, should send a can to abort the transmission;

11> the only exception to the 10> situation is that the packets received are the same as the previous packet ordinal, in which case the receiver simply ignores the duplicate packet, sends an ACK to the sender, and prepares to receive the next packet.

12> the receiver confirms the integrity of the packet sequence and is expected to be correct, only the 128-byte data section is arithmetic and verified, and the result is compared with the last byte in the frame (arithmetic checksum), the same sends the ACK, different sends the NAK;

3.2.4. Timeout processing

The 1> receiver waits for a packet to arrive with a time-out time limit of 10 seconds, each timeout after sending a NAK;

2> when a packet is received, the time-out interval for each character in the receive process is 1 seconds;

3> to maintain "receiver driver", the sender should not use timeout processing while waiting for a boot byte

4> Once the transmission begins, the sender uses a separate 1-minute timeout period to give the receiver sufficient time to send ACK, NAK, can before the must be processed;

5> all timeouts and error events are retried at least 10 times;

3.2.5. Description of the Checksum

The XMODEM protocol supports 2 checksums, which are cumulative and with CRC checksum. When the receiver starts the transfer at the beginning, it sends a nak, indicating that it wants to be verified in addition and in a way. When the receiver starts the transmission at first, it sends the character "C", indicating that it wants to check it in CRC mode.

Some people may ask, how does the receiver want to verify that the sender has to cooperate, the sender must all support the cumulative and checksum CRC checksum? In fact, Xmodem requires support for CRC must also support the cumulative sum, if the sender only support the cumulative sum, and the receiver with the character "C" to start, then as long as the sender regardless of it, when the receiver continues to send "C", after three times did not receive the answer, will automatically change to send Nak, Since it has been understood that the sender may not support CRC verification, the receiver now changes to accumulate and verify the sender communication. The sender receives the NAK and sends a packet response.

4. TCP protocol 4.1. Introduction to the Agreement

In order to facilitate the query and use of board functions, modeled on the Linux terminal tools to develop a board terminal platform, through the serial port or the network port Telnet can operate the board functions, the underlying code will be all the functions of the Board into a unified format interface, placed in a registry, so that both the host computer Operation board Card, It also facilitates the addition and modification of the underlying functions, and the terminal interface is as follows:

TCP is a connection-oriented, reliable, IP-based Transport layer protocol, and connection-oriented means that two applications using TCP must establish a TCP connection before exchanging data with each other. When the application layer sends a 8-byte data stream for inter-network transmission to the TCP layer, TCP divides the data into segments of appropriate length, and the maximum transmission segment size is usually limited by the maximum Transmission Unit (MTU) of the data link layer of the network to which the computer is connected. TCP then passes the packet to the IP layer, which transmits the packet over the network to the TCP layer on the receiving side.

In order to ensure the reliable transmission of the message, TCP gives each packet a serial number, and the serial number also guarantees that the packets sent to the receiving end can be received sequentially. The receiving end then sends a corresponding acknowledgment (ACK) to the bytes that were successfully received, and if the sender does not receive a confirmation within a reasonable round trip delay (RTT), the corresponding data will be re-transmitted.

In terms of data correctness and legality, TCP uses a checksum function to verify that the data has errors, and to calculate the checksum when it is sent and received. In the guarantee of reliability, the use of time-out retransmission and the delivery confirmation mechanism, in the flow control, the use of Sliding window protocol, the Agreement, for the window of the unconfirmed packet needs retransmission, congestion control, the use of TCP congestion control algorithm;

4.2. TCP Header

The TCP data is encapsulated in an IP datagram, as follows:

As a TCP packet data format, it is typically 20 bytes in the absence of an option:

The source port number and destination port number are used to find the sending and receiving end of the application process, which is the same as the UDP message, the two means plus the IP header source IP address and destination IP address uniquely determine a TCP connection.

The Serial Number field is used to identify the data byte stream sent from the TCP send side to the TCP receiver, which represents the first data bytes in this segment. When a new connection is established, the SYN flag in the handshake message is set to 1, and the ordinal field in the handshake message is the initial ordinal number of the random selection, and the first byte ordinal to be sent by the sender when the connection is established is isn+1.

The confirmation Number field is only useful when the ACK is 1, and the confirmation number contains the next sequence number that is expected to be received by the party sending the acknowledgment, and the confirmation number is added 1 on the last successfully received data byte sequence number, for example, when the data ordinal named X was successfully received by the other party, then the confirmation number returned should be x+1.

The head length is also called the first Minister, the length of the header is given the initial length, in 32bit is 4 bytes, this field has 4bit, so TCP has a maximum of 60 bytes of the header, if there is no option field, the normal header length is 20 bytes.

There are 6 flag bits in the TCP header, and the 6 flags are described below:

Window size is the window size, which fills in the corresponding value to inform the other side of the number of bytes expected to receive, the window size is a key field of TCP traffic control, the window is a 16bit field, so the maximum window size is 65535 bytes.

The 16-bit checksum is the same as the process and principle of the UDP checksum, which is a mandatory field that covers the entire TCP packet, TCP header and TCP data;

The emergency pointer is only valid at Urg 1 o'clock, the emergency pointer is a positive offset, and the value in the ordinal field is added to indicate the ordinal of the last byte of the emergency data.

Basic knowledge of TCP/IP

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.