IP协议(RFC791)梳理

1. IP数据包格式
IP数据包格式如下图所示,其中每个短横线的长度代表1个比特位:


1.1 Version
取值为常量4.


1.2 IHL
  • 即Internet Header Length,给出头部的长度,即指向数据部分的开始。
  • 单位为32位(4字节),最小取值为5。


1.3 Type of Service
描述该数据包的传输需求,8个比特位可划分为以下几个字段:


1.3.1 Bit 0~2: Precedence
Precedence字段的取值列表如下:
取值 含义
111 Network Control
110 Internetwork Control
101 CRITIC/ECP
100 Flash Override
011 Flash
010 Immediate
001 Priority
000 Routine


1.3.2 Bit 3~5
比特位索引 unset含义 set含义
3 普通时延 低时延
4 普通吞吐量 高吞吐
5 普通可靠性 高可靠

除非极端情况,以上3个状态位最多2个set。

1.3.3 Bit 6、7
暂未使用。

1.4 Total Length
  • 数据包的总长度(包括头部),单位为字节
  • 2字节最大取值为65536字节,推荐单个数据包长度为576字节


1.5 Identification
Identification用于分段标识,使得接收端可以重新组装分段的数据包。分为2部分,3位的Flag和13位的offset。

1.5.1 Flag
共3位:
  • bit 0:必须为0
  • bit 1: 0=可以分段, 1 = 不可分段
  • bit 2: 0 = 最后一个分段, 1 = 还有更多分段


1.5.2 Offset
13位的offset,单位为8字节,用于标识本分段位于整个数据包的第几个8字节。同时意味着IP数据包的分段长度必须是8字节的整数倍。

1.6 Time to Live
数据包的剩余存活时间,TTL为0的数据包必须被丢弃。单位为秒,且每次被处理至少减1.

1.7 Protocol
This field indicates the next level protocol used in the data portion of the internet datagram.

1.8 Header Checksum
数据包头部的checksum。

1.9 Source Address和Destination Address


1.10 Options
Options可以出现或不出现在头部中(由头部长度字段可判别),长度可变,主要包含control、安全、debug方面的option,详略。

2. IP数据包发送过程
如下图的结构中,两个应用程序通过IP协议交换数据包,其中通过1个IP Gateway.


其中Gateway用于在不同的网络之间转发数据包:


具体发送过程如下:
  • The sending application program prepares its data and calls on its local internet module to send that data as a datagram and passes the destination address and other parameters as arguments of the call.
  • The internet module prepares a datagram header and attaches the data to it. The internet module determines a local network address for this internet address, in this case it is the address of a gateway.
  • It sends this datagram and the local network address to the localnetwork interface.
  • The local network interface creates a local network header, and attaches the datagram to it, then sends the result via the local network.
  • The datagram arrives at a gateway host wrapped in the local network header, the local network interface strips off this header, and turns the datagram over to the internet module. The internet module determines from the internet address that the datagram is to be forwarded to another host in a second network. The internet module determines a local net address for the destination host. It calls on the local network interface for that network to send the datagram.
  • This local network interface creates a local network header and attaches the datagram sending the result to the destination host.
  • At this destination host the datagram is stripped of the local net header by the local network interface and handed to the internet module.
  • The internet module determines that the datagram is for an application program in this host. It passes the data to the application program in response to a system call, passing the source address and other parameters as results of the call.

猜你喜欢

转载自chong-zh.iteye.com/blog/2356974
今日推荐