Socket programming practices in Linux (I) basic network knowledge and TCP/IP Overview

Source: Internet
Author: User
Tags ftp protocol

Socket programming practices in Linux (I) basic network knowledge and TCP/IP Overview

ISO/OSI Layer-7 Reference Model

 

1. Physical Layer: It mainly defines physical device standards, such as the interface type of the network cable, the interface type of the optical fiber, and the transmission rate of various transmission media. Its main function is to transmit the bit stream (that is, it is converted from 1, 0 to the current strength for transmission, and then converted to 1, 0 after reaching the destination, that is, the digital-to-analog conversion and digital-to-analog conversion ). The data at this layer is called bits. (FLAG: RJ-45) 2. data link layer: defines how to make formatted data for transmission and how to control access to physical media. This layer usually provides error detection and correction to ensure reliable data transmission. The switch belongs to this layer. 3. Network Layer: Provides the connection and path selection between two host systems in a network located in different geographic locations. The development of Internet greatly increases the number of users accessing information from various sites in the world, and the network layer is the layer for managing such connections. (FLAG: Route Selection, IP/OSPF, ICMP, IGMP) 4. transmission Layer: defines some data transmission protocols and port numbers (WWW port 80, etc.), such as TCP (transmission control protocol, low transmission efficiency, high reliability, high transmission reliability requirements, large data volume), UDP (User Datagram Protocol, which is the opposite of TCP, is used to transmit data with low reliability requirements and small data volume, for example, QQ chat data is transmitted in this way ). Data received from the lower layer is segmented and transmitted, and then reorganized after the destination address is reached. This layer of data is often called segments. 5. Session Layer: establishes a data transmission path through the transport layer (port number: Transmission port and receiving port. It is mainly used to initiate a session or accept a session request between your systems (devices need to know each other, either an IP address, a MAC address, or a host name ). 6. Presentation Layer: ensure that the information sent by the application layer of one system can be read by the application layer of another system. For example, a PC program communicates with another computer. Does one computer use an extended Gbit/s exchange (EBCDIC), and the other uses the American Standard ASCII code (ASCII) to indicate the same characters. If necessary, the presentation layer uses a common format to convert multiple data formats. 7. Application Layer: the OSI Layer closest to the user. This layer provides network services for your applications (such as email, file transfer, and terminal simulation. (FLAG: HTTP, HTTPS, FTP, TELNET, SSH, SMTP, POP3)

TCP/IP layer-4 Model

 

The OSI reference model developed by ISO is too large and complex, causing many criticism. In contrast, the TCP/IP protocol stack developed by the technical staff has been widely used.

 


Application Layer: FTP, TELNET to meet certain user needs, custom protocol, QQ Protocol

Transport Layer: TCP UDP

Network Layer: ICMP, IP, IGMP

Data link layer: ARP and RARP Hardware Interfaces

Two hosts running the FTP protocol:

 

Each layer has its own protocol and communicates with each other. Their language is consistent. We can think of it as A peering communication between process A and process B, that is, the virtual circuit (not a real connection line) transmits the logical flow of peer-to-peer data. For example, two people now communicate via mobile phones, as if they were in their ears, but they actually communicate through electromagnetic waves.

Encapsulation:

Sub-Use:

Port:

 

The ports are allocated and controlled by IANA. They are closely bound to some services: 0 ~ 1023
Registered port: 1024 ~ 49151. The ports are loosely bound to some services.
Dynamic or private (temporary) ports: 49152 ~ 65535 (actually starting from 1024)
Link Layer:

Ethernet frame format

The data packets at the link layer are called Ethernet frames. The Link Layer does not recognize IP addresses [because IP addresses are logical addresses]. The link layer recognizes MAC addresses of physical NICs [hardware addresses) find the target MAC address (ARP Address Resolution Protocol) [MAC-> IP address direction address resolution]; MAC-> IP Reverse Address Resolution (RARP Protocol); Type (2 bytes) used to distinguish IP, ARP, and RARP.

 

// Ethernet header code struct ethernet_hdr {char dest_mac [6]; char src_mac [6]; short protocol ;};

 

MTU/path MTU

The link layer has the maximum transmission unit MTU, which limits the maximum length of data frames. Different network types have an upper limit. The MTU of Ethernet is 1500. You can use the netstat-I command to view the value. If there are packets to be transmitted at the IP layer and the packet length exceeds the MTU, the IP layer performs fragmentation on the packet so that the length of each piece is smaller than or equal to the MTU. In addition, the link layers of multiple networks may have different MTUS. The smaller MTU in the communication path is called the path MTU. However, fragment will reduce the communication efficiency of the network and avoid it as much as possible.

ARP Address Resolution Protocol

Local ARP cache

 

// ARP header code struct arp_hdr {unsigned short hwtype; // fixed 1 unsigned short protype; // fixed 0x0800 (representing requests for IP protocol) unsigned char hwaddrlen; // fixed 6 (MAC address length) unsigned char proaddrlen; // fixed 4 (IP address length) unsigned short opcode; // Request-1, reply-0x0002 unsigned char sender_mac [6]; // the sender's MAC unsigned char sender_ip [4]; // the sender's IP unsigned char dest_mac [6]; // receiver MAC unsigned char dest_ip [4]; // receiver IP };

 

Both the source and destination are added to the ARP buffer.

 

RARP Reverse Address Resolution Protocol, applicable to diskless workstation, where IP is generally stored in the configuration file

To obtain the IP address, send a request to the RARP server through the RARP protocol.

Network Layer:

IP Datagram

 

The maximum length of the header is 15*4 = 60 bytes, the unit is 15, and the option can contain up to 40 bytes.

Determine whether the top layer is TCP or UDP based on the eight-bit protocol type. The header verifies the integrity of the IP header and does not verify the data integrity.

Service type:

 

Up to 65535 IP datagram; one ID is shared by multiple parts.


Internet checksum algorithm: http://blog.csdn.net/zhq651/article/details/8515575

 

// IP header code struct ip_hdr {char ver_hl; char tos; unsigned short len; unsigned short id; unsigned short fragment; char ttl; char protocol; unsigned short hdr_chksum; char src_ip [4]; char dest_ip [4];};
Routing process:

 

Sequence: Search for matched host addresses

Search for network addresses

Search for default table items

ICMP protocol

The ICMP protocol is used to transmit control data such as error information, time, ECHO, and network information. It works at the network layer (IP), and The Ping program uses the ICMP protocol.

 

// ICMP header code struct icmp_hdr {char type; // ICMP message type char code; // "subtype" unsigned short icmpchksum; // checksum };
UDP Message format

 

 

// UDP header code struct udp_hdr {unsigned short src_port; unsigned short dest_port; unsigned short len; unsigned short chksum ;};

 

TCP packet header:

Establish a three-way handshake

Connection terminated Four Waves

Note: In a slow startup, the base is relatively small.

 

 

 

Step a: ping the application to determine whether the host name or IP address is sent. Call the gethostbyname () function to Resolve Host B and convert the host name to a 32-bit IP address. This process is called DNS domain name resolution.

Step B: The ping program sends an ICMP ECHO packet to the destination IP address

Step c: Convert the IP address of the target host to a 48-bit hardware address, send an ARP request broadcast in the LAN, and find the hardware address of host B.

Step d: After the ARP protocol layer of host B receives the ARP request from host A, it fills in the hardware address of the local machine in the response packet and sends the ARP response to host.

Step e: Send ICMP data packets to host B

Step f: Host B receives the ICMP packet from host A and sends A response packet.

Step g: host A receives the ICMP packet response packet from host B.

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.