"Network Programming of ASCE1885"---IP Datagram Format

51CTO 2022-09-23 08:04:15 阅读数:648

networkprogrammingasce1885asceip


IPDeal with unreliable connectionless datagram transmission services,IPLayer provides services throughIPLayer of datagram encapsulation and opened to realize.IPThe format of the datagram is divided into header and the data of two parts,The header area is in order to correct the high-level data transferred and add a variety of control information,Data area including the high-level protocol need to transfer the data.

IP数据报的格式如下:

《ASCE1885的网络编程》---IP数据报格式_互联网

 

注意,The above said data,最高位在左边,记为0位;最低位在右边,记为31位.在网络中传输数据时,先传输0~7位,其次是8~15位,然后传输16~23位,最后传输24~31位.由于TCP/IPAll the binary digits in a protocol header in the network transmission is required in this order,So it is called the network byte order.在实际编程中,Storage of binary number by any other means must be used before the data transmission network programmingAPIThe corresponding function converts the head into the network byte order.

 

IPData submitted to the function of each field is as follows:

1)版本号:占用4位二进制数,表示该IP数据报使用的IP协议版本.目前Internet中使用的主要是TCP/IP协议族中版本号为4的IP协议.

 

2)头长度:占用4位二进制位,此域指出整个报头的长度(包括选项),该长度是以32位二进制数为一个计数单位的,接收端通过此域可以计算出报头在何处结束及从何处开始读数据.普通IP数据报(没有任何选项)该字段的值是5(即20个字节的长度).

 

3)服务类型(TOS、type of service):占用8位二进制位,用于规定本数据报的处理方式.服务类型字段的8位分成了5个子域:

《ASCE1885的网络编程》---IP数据报格式_网络_02


 

(1)—优先权(0-7)数越大,表示该数据报优先权越高.网络中路由器可以使用优先权进行拥塞控制,如当网络发生拥塞时可以根据数据报的优先权来决定数据报的取舍.

(2)—短延迟位D(Delay):该位置1时,数据报请求以短延迟信道传输,0表示正常延时.

(3)—高吞吐量位T(Throughput):该位置1时,数据报请求以高吞吐量信道传输,0表示普通.

(4)—高可靠位R(Reliability):该位置1时,数据报请求以高可靠性信道传输,0表示普通.

(5)—保留位.

目前在Internet中使用的TCP/IP协议大多数情况下网络并未对TOS进行处理,但在实际编程时,有专门的函数来设置该字段的各域.The application protocol of some important set of recommendedTOS值:

 

《ASCE1885的网络编程》---IP数据报格式_编程_03

从上表可以看出,For the application of direct interaction with users,Generally use the short time delay;With the application of a large number of data need to be,Generally selects high throughput;To a datagram transmission control information of application,Generally selects high reliability.In the datagram survival period does not supportTOS的,TOS字段就设置为0x00.

 

4)总长度:占用16位二进制位,总长度字段是指整个IP数据报的长度(报头区+数据区),以字节为单位.利用头部长度字段和总长度字段就可以计算出IP数据报中数据内容的起始位置和长度.由于该字段长度为16位二进制数,因此理论上IP数据报最长可达65536个字节(事实上受物理网络的限制,要比这个数值小很多).

 

5)生存时间(TTL,time to live):占用8位二进制位,它指定了数据报可以在网络中传输的最长时间.实际应用中把生存时间字段设置成了数据报可以经过的最大路由器数.TTL的初始值由源主机设置(通常为32、64、128或256),一旦经过一个处理它的路由器,它的值就减1.当该字段为0时,数据报就丢弃,并发送ICMP报文通知源主机,因此可以防止进入一个循环回路时,数据报无休止地传输下去.

 

6)上层协议标识:占用8位二进制位,IP协议可以承载各种上层协议,目标端根据协议标识就可以把收到的IP数据报送到TCP或UDP等处理此报文的上层协议了.

常用网际协议编号:

 

《ASCE1885的网络编程》---IP数据报格式_internet_04

7)校验和:占用16位二进制数,用于协议头数据有效性的校验,可以保证IP报头区在传输时的正确性和完整性.头部检验和字段是根据IP协议头计算出的检验和,它不对头部后面的数据进行计算.

原理:发送端首先将检验和字段置0,然后对头部中每16位二进制数进行反码求和的运算,并将结果存在校验和字段中.         由于接收方在计算过程中包含了发送方放在头部的校验和,因此,如果头部在传输过程中没有发生任何差错,The calculation result of the receiver should be all1.

 

8)源地址:占用32位二进制数,表示发送端IP地址.

 

9)目的地址:占用32位二进制数,表述目的端IP地址.

 

 

======================IPThe datagram fragmentation and restructuring======================

最大传输单元:

IPWhen a datagram transmission on the Internet,May be after multiple physical network can be transmitted from the source side to the destination end.Different network due to the link layer and the physical properties of different media,Therefore when carries on the data transfer,To the maximum length of a data frame has a limit,This limit the maximum transmission unitMTU(Maximum Transmission Unit).

Communication between two hosts on the same network,该网络的MTU值是确定的,There is no fragmentation problem.Fragmentation problem only commonly exists in differentMTUThe value in the Internet.Due to the Internet now mainly using the router for Internet connection,Therefore shard work shall be the responsibility of the router usually.

When the communication between the two hosts through multiple differentMTUValue of network,MTUThe smallest is the bottleneck of communication pathMTU值,It's called the pathMTU.Due to the routing is not necessarily symmetrical(从A到B的路由可能与从B到A的路由不同),因此,路径MTU在两个方向上不一定是一致的,The table below are several common networkMTU值:

《ASCE1885的网络编程》---IP数据报格式_路由器_05

 

分片:

A datagram to suitable for network transmission into multiple datagram fragmentation is the process by which,Be shard after allIPThe datagram may take different paths to reach the target host.

一个IPDatagram during transmission can be divided,Also may not be divided.如果被分片,分片后的IPThe datagram and had no shardIPThe datagram structure is the same,即也是由IP头部和IPData area two parts:

 

《ASCE1885的网络编程》---IP数据报格式_互联网_06

分片后的IP数据报,Data area is originalIPThe datagram data area a continuous part,The head is the originalIPA datagram header copy,But not with the original of fragmentationIPA datagram header there are two main different:标志和片偏移:

(1)—标志:在IPA datagram header has a call“标志”的字段,用3位二进制数表示:

 

《ASCE1885的网络编程》---IP数据报格式_编程_07

不分片DF(Do not Fragment)Mark if is set1,The data reported in transmission process could not be shard,Such as network connectivity test commandping就可以用-FParameter is set to the data transmission without fragmentation,But when the data can'tMTUThe smaller network,Will generate inaccessible data error.

Piece of unfinishedMF(More Fragment)Mark if is set1,Show that the datagram last datagram fragmentation later,Finally a datagram the bit is set0.

 

(2)—片偏移:IP数据报被分片后,Each piece of data area in the originalIPThe position of data area with13Slice offset to say.In the figure above shard1的偏移为0;分片2的偏移为600;分片3的偏移为1200实际在IP地址中,Due to the deviation in8Bytes for the unit to calculate,因而在IPThe datagram fragmentation1的偏移是0;分片2的偏移是75;分片3的偏移是150.

 

重组:

When a piece ofIPWhen a datagram to reach the final target host,The target host assembly for each shard to,Back to the source host sends theIP数据报,这个过程叫做IPThe restructuring of the datagram.

在IPA datagram header,标识用16位二进制数表示,It uniquely identifies the host sends each datagram.When a datagram is shard,Each shard the datagram only“标识”A copy of the value of the field the same,So all a datagram fragmentation with the same id.

The principle of target side host restructuring the datagram is:

(1)—根据“标识”Fields can determine which belong to the original received dividedIP数据报;

(2)—根据“标志”字段的“Piece of unfinishedMF”Sub-fields fragmentation can be determined whether the last shard;

(3)—根据“偏移量”Fields can determine fragmentation in the original data to the position of.

 

 

========================IPThe datagram options========================

IP数据报“选项”主要有两大功能:

1)Used to implement the control of the datagram transmission in the process of,Route datagrams should pass such as;

2)进行网络测试,Like a datagram transmission process through which router.

 

IP“选项“The domain is divided into four categories,Each category is divided into a number of options,The serial number of each option is determined:

 

《ASCE1885的网络编程》---IP数据报格式_internet_08

 

 

IP数据报“选项”由三个部分组成:选项码、选项长度和选项数据.One byte of each option and option length,中,The length of the length option is used to determine the option parts;Option to copy code is divided into、Option class, and option #:

《ASCE1885的网络编程》---IP数据报格式_路由器_09

复制:占一位,Used to control a with optionsIPAfter the datagram is shard of options for treatment of.该位置1When all copies option to shard in;置0When the option only copied to the first subdivision in.

Option class and option number is used to determine if the option is what kind of option which of the options,Actually is to determine the function of this option.

 

1)源路由选择:是指IPWhen a datagram transmission in the Internet,By routing is aIPData submitted to the source host specified,To distinguish it from the datagram transmission in the Internet by the routerIPLayer of homing diameter of routing.

By setting the source routing options,Can specify the routing test network connectivity,In order to make the datagram to bypass network error,Can also be used to test a specific network throughput.Source routing can be divided into two classes:Strict source routing and loose source route selection.

 

(1)—Strict regulations source routing has the senderIPThe datagram must go through the path of the each router,相邻路由器之间不得有中间路由器,And the order of the transiting router cannot be changed.If a router sends the source routing under specified by a router is not directly connected network,那么它就返回一个“源路由失败”的ICMP差错报文.Strict source routing options format is as follows:

《ASCE1885的网络编程》---IP数据报格式_编程_10

 

Option code field is100 01001(0x89),即为0类9号选项.选项长度最大为39,可存放9个IP地址.因为IPThe head length field is4位二进制数,所以整个IPHead may include15(<24)个32A long word(即60个字节).由于IPThe head fixed length as20字节,选项码、Option length and pointer share to3个字节,因此剩下60-20-3=37个字节来存放IP地址清单,Thus only9个IP地址.

(2)—Loose source route selection:By the sender to indicate a datagram throughIP地址清单,But in the datagram transmission path,Specified in the option of twoIPBetween address can have otherIP地址的路由器.Format is the same as strict,Only option code field values as0x83.

 

2)记录路由:By setting the record routing options,IPDatagrams can record the datagram from the source host to the target host,By the path of each routerIP地址.Record the routing option data format and strict source routing format is the same,But the option code field values as0x87,Pointer to initial value as4,指向存放第一个IP地址的位置.每个路由器的IPAddress in data section of the options,The value of the pointer field also along with the increase(从4开始到8,12,16,最大到36),It always point to the next storeIP地址的位置.当记录了9个IP地址后,The value of the pointer field as40,According to the data area is full.

 

3)记录时间戳:就是IPThe datagram every through a router. Make a note of itIP地址和时间.The timestamp in the time toms为单位,Timestamp values generally for Greenwich mean time(UT,Universal Time)Time the number of milliseconds since midnight timestamp format options are as follows:

《ASCE1885的网络编程》---IP数据报格式_internet_11

 

Timestamp option option code is0x44.The total length option length said option(一般为36或40),Pointer to a pointer to the next available space(值为5、9、13等).

“溢出OF”Field represents for timestamp option data area space is not enough and failed to record the timestamp of the number of;

“标志FL”Field is used to control the format of the timestamp option,取值如下:

 

 

《ASCE1885的网络编程》---IP数据报格式_互联网_12

 

 

copyright:author[51CTO],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209230729352805.html