Socket Programming Practice (--TCP/IP) beginning (1)

Source: Internet
Author: User
Tags echo message

Ethernet Frame Format

Illustration 1: A link-layer packet, called an Ethernet frame.

Illustration 2: The link layer does not recognize the IP address [because the IP address is a logical address], and the link layer recognizes the physical NIC MAC address [hardware address].

Note 3: Need to find the other side's MAC address (ARP Address Resolution Protocol ) according to the IP address [mac-> IP address Direction Address resolution: RARP Reverse Address Resolution Protocol].

Illustration 4: The application layer communicates according to the IP address of the peer, where does the link layer need the MAC address of the destination address in the data encapsulation process? The IP address needs to be converted to a MAC address, which is address resolution.

Ethernet Header Code:

struct ethernet_hdr{    char dest_mac[6];    Char src_mac[6];    short Protocol;};


ARP Address Resolution Protocol


ARP Header code:

struct arp_hdr{    unsigned short hwtype;//fixed 1    unsigned short protype;//fixed 0x0800 (on behalf of IP protocol requests)    unsigned char  hwaddrlen;//Fixed 6 (that is, MAC address length)    unsigned char proaddrlen;//fixed 4 (i.e. IP address length)    unsigned short opcode;//Request-1, Reply- 0x0002    unsigned char sender_mac[6];//sender mac    unsigned char sender_ip[4];//sender IP    unsigned char dest_mac[6] ;//Recipient Mac    unsigned char dest_ip[4];//receiver IP};
 MTU and Path MTU for Ethernet

Ethernet and IEEE 802.3 have a limit on the length of the data frame, the maximum value of which is 1500 and 1492 bytes, the limit is called the Maximum transmission unit (MTU, Maximum transmission Unit)

If the IP layer has a datagram to pass, and the length of the data than the link layer MTU is also large, then the IP layer will be fragmented (fragmentation), the data is divided into several slices, so that each piece is less than the MTU.

When two hosts on the network communicate with each other, the two hosts go through multiple networks, and each network's link layer may have a different MTU, where the minimum MTU in two communication host paths is called the Path MTU.

IP Datagram

version number, currently taking value 4

First ministerial degree, 4 bytes, value range 5~15

Service type, specifying transport priority, transfer speed, reliability, throughput, etc.

Total message length, maximum length is 65535 bytes

A message ID that uniquely identifies a datagram, and if the datagram is fragmented, the identity of each segment is the same

Flag, highest bit unused, defined as 0, remaining two bits df (not segmented) and MF (more segments)

The segment offset, in 8 bytes, that indicates the offset of the first data word of the segment in the original datagram

time-to-live (TTL), value 0~255, minus 1 per routed node, dropped by 0 o'clock (Implementation principle of Tranceroute program)

Protocol that specifies the type of protocol for the datagram, such as 1 for icmp,4 to ip,6 for tcp,17 to UDP, etc.

The first checksum, which is recalculated every time a gateway is passed, is used to ensure the integrity of the IP header

Options, variable length, provide the control function required in some situations, the IP header must be 4 bytes in length, if the option length is not an integer multiple of 4, the data must be populated

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];};
 ICMP protocol

ICMP protocol is used to transmit error information, time, ECHO, network information and other control data

When we ping a host to see if it is running, an ICMP message is generated. The remote host responds to the ping request with its own ICMP information.

ICMP protocol Description:

itself is part of the IP.

Purpose: The gateway or target machine uses ICMP and source communication to provide feedback when problems occur

Must be implemented in the IP protocol stack

ICMP is included in the payload data of the IP packet, the protocol type in the IP header is 1

The first byte of the ICMP data represents the type of ICMP message, which determines the format of the subsequent data

Characteristics:

Its control capability is not used to guarantee the reliability of the transmission

It's not a reliable transmission itself.

ICMP packets

ICMP is included in the payload data of the IP packet, the protocol type in the IP header is 1

The first byte of the ICMP data represents the type of ICMP message, which determines the format of the subsequent data

ICMP Header code:

struct icmp_hdr{    chartype;//ICMP message type    char code;//"subtype"    unsigned short  icmpchksum;//checksum};

ICMP message type

Message type description

2 0 Echo Reply

2 3 Destination Unreachable

2 4 Source Quench

2 5 Redirect

2 8 Echo

2 Time Exceeded

2 Parameter problem

2 Timestamp

2 Timestamp Reply

2 Information Request

2 Information Reply

2 Address Mask Request

2 Address Mask Reply


Message classification

2 ICMP error message

2 Purpose Unreachable Message (Type 3)

2 Super-times (Type 11)

2 parameter error message (Type 12)

2 ICMP Control messages

2 Report source suppression message (Type 4)

2 Redirection (Type 5)

2 ICMP request/reply message

2 Send-back request and response messages (types 0 and 8)

2 Timestamp Request and response messages (types 13 and 14)

2 Address Mask Request and response messages (types 17 and 18)

2 other

ICMP echo Message

Type: 0 means echo reply,8 represents echo

Code: 0

Identifier: Identifies a session, for example, with the process ID

Serial number: For example, 1 increase per request

Option data: Echo

ICMP Destination Unreachable Message

Type: 3

Code: 0 indicates that the network is unreachable, 1 indicates that the host is unreachable, 2 means the protocol is unreachable, and 3 indicates that the port is unreachable;

IP header of the IP packet in error + first 8 bytes in original IP packets

Socket Programming Practice (--TCP/IP) beginning (1)

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.