您的位置:首页 > 理论基础 > 计算机网络

linux网络编程之TCP/IP基础(三):IP数据报格式和IP地址路由

2016-10-18 09:10 881 查看
一、IP数据报格式

IP数据报格式如下:



注:需要注意的是网络数据包以大端字节序传输,当然头部也得是大端字节序,也就是说:

The most significant bit is numbered 0 at the left, and the least significant bit of a 32-bit value is numbered 31 on the right.

The 4 bytes in the 32-bit value are transmitted in the order: bits 0-7 first, then bits 8-15, then 16-23, and bits 24-31 last. This is called big endian byte ordering, which is the byte ordering
required for all binary integers in the TCP/IP headers as they traverse a network. This is called the network byte order. Machines that store binary integers in other formats, such as the little endian format,
must convert the header values into the network byte order before transmitting the data.

版本
IP协议版本号,长度为4位,IPv4此字段值为4,IPv6此字段值为6

首部长度
以32位的字为单位,该字段长度为4位,最小值为5,即不带任何选项的IP首部20个字节;最大值为15,所以首部长度最大为60个字节

服务类型(TOS)
长度为8位。此字段包含3位的优先权(现已忽略),4位的服务类型子字段(只能有一位置1)和1位的保留位(必须置0)。4位的服务类型分别为最小延迟(D)、最大吞吐量(T)、最高可靠性(R)、最小费用(F),如下图。



总长度
该字段长度为16位,以字节为单位,该字段长度包含IP的头部和数据部分(payload)。IP数据报最大可达65535个字节。

The total length field is required in the IP header since some data links (e.g., Ethernet) pad small frames to be a minimum length.

when a datagram is fragmented the total length field of each fragment is changed to be the size of that fragment.

标识
16位标识,用来标识一个IP数据报,每发送一个此值会加1,可用于分片和重新组装成数据报。

标志与片偏移

3位标志其中第一位不使用, 每二位DF(Don’t Fragment),该位如果为1,如果传输的数据报超过最大传输单元,该数据报会被丢弃,并发送一个ICMP差错报文。第三位MF(More Fragment)表示是否有更多的片,该位为1,说明后续有分片。最后一片MF为0。

注:在这里稍微讲一下IP层分片的问题。假设一个IP数据报大于最大传输单元MTU,那么如果设置了分片标志位,将会被分片传输。

每一片都有自己的IP 头部,IP头部中的标识是一样的,但是片偏移不同(以8字节为单位)。除了最后一片,分片要求其他片除去IP头部的大小必须是8字节的整数倍。除了第一片有tcp/udp头部,其他片都没有。分片完成后,每一片独自成为一个数据包(跟数据报概念不同,参见这里),可以走不同的路由,最后到达目的地的时候IP层根据它们各自IP头部的信息重新组成一个IP数据报。

分片是有风险的,因为一旦某一片丢失,就需要重传这个IP数据报,因为IP层本身并没有超时重传的机制,可靠性需要TCP层来保证(一些UDP协议的可靠性由应用程序保证),一旦一个TCP段中的某一片丢失,TCP协议层会超时重传。此外,分片可以发生在源主机或者中间的路由,如果发生在中间的路由,源主机根本不知道是怎样分片,所以要尽量避免分片。

应用数据的多个IP数据报由TCP层根据seq number 进行重组成原始数据,存放到TCP接收缓冲区。

Using sequence numbers, a receiving TCP discards duplicate segments and reorders segments that arrive out of order. Recall that any of these anomalies can happen
because TCP uses IP to deliver its segments, and IP does not provide duplicate elimination or guarantee correct ordering. Because it is a byte stream protocol, however, TCP never delivers data to the receiving application out of order. Thus, the receiving
TCP may be forced to hold on to data with larger sequence numbers before giving it to an application until a missing lower-sequence-numbered segment (a “hole”) is filled in.



实战经验:在使用tcpdump 并指定端口时,如果分片传输,除第一片外,其他片可能捕捉不到。

TTL
TTL(Time To Live)表示数据报最多可经过的路由器的数量。数据报每经过一个路由器,TTL减1,减为0时丢弃,并发送ICMP报文通知源主机。TTL可以避免数据报在路由器之间不断循环。

协议类型
表示IP层上承载的是哪个高级协议。在封装与分用的过程中,协议栈知道该交给哪个层的协议处理。1 ICMP 2 IGMP 6 TCP 17UDP

头部校验和
保证数据报头部的数据完整性,但校验不包括数据部分。这样做的目的有二:一是所有将数据封装在IP数据包中的高层协议均含有覆盖整个数据的校验和,因此IP数据报没有必要再对其所承载的数据部分进行校验。二是每经过一个路由器,IP数据报的头部要发生改变(如TTL),而数据部分不变,这样只对发生改变的头部进行校验,显然不会浪费太多的时间。为了减少计算时间,一般不用CRC校验码,而是采用更简单的网际校验和(Internet
Checksum)。

Since a router often changes only the TTL field (decrementing it by 1), a router can incrementally update the checksum when it forwards a received datagram, instead of calculating the checksum over the entire IP header again.

The standard BSD implementation, however, does not use this incremental update feature when forwarding a datagram.

源IP地址
发送数据的主机IP地址

目的IP地址
接收数据的主机IP地址

选项与填充(选项为4字节整数倍,否则用0填充)
安全和处理限制
路径记录:记录所经历路由器的IP地址
时间戳:记录所经历路由器的IP地址和时间
宽松源站路由:指定数据报文必须经历的IP地址,可以经过没有指定的IP地址。
严格的源站路由:指定数据报文必须经历的IP地址,不能经过没有指定的IP地址。

二、IP地址与路由

IPv4的IP地址长度为4字节,通常采用点分十进制表示法(dotted decimal representation)例如0xc0a80002表示为192.168.0.2。Internet被各种路由器和网关设备分隔成很多网段,为了标识不同的网段,需要把32位的IP地址划分成网络号和主机号两部分,网络号相同的各主机位于同一网段,相互间可以直接通信,网络号不同的主机之间通信则需要通过路由器转发。

In our general scheme, IP can receive a datagram from TCP, UDP, ICMP, or IGMP (that is, a locally generated datagram) to send, or one that has been received from a network interface (a datagram to forward). The IP layer has a routing
table in memory that it searches each time it receives a datagram to send. When a datagram is received from a network interface, IP first checks if the destination IP address is one of its own IP addresses or an IP broadcast address. If so, the datagram is
delivered to the protocol module specified by the protocol field in the IP header. If the datagram is not destined for this IP layer, then (1) if the IP layer was configured to act as a router the packet is forwarded (that is, handled as an outgoing datagram
as described below), else (2) the datagram is silently discarded.

假设某主机上的网络接口配置和路由表如下:





这台主机只有一个网络接口连到192.168.232.0/24网络。路由表的Destination是目的网络地址,Genmask是子网掩码,Gateway是下一跳地址,Iface是发送接口,Flags中的U标志表示此条目有效(可以禁用某些条目),G标志表示此条目的下一跳地址是某个路由器的地址,没有G标志的条目表示目的网络地址是与本机接口直接相连的网络,不必经路由器转发,因此下一跳地址处记为*号。

如果要发送的数据包的目的地址是192.168.232.1,跟第三行的子网掩码做与运算得到192.168.232.0,正是第三行的目的网络地址,因此从eth0接口发送出去,由于192.168.232.0/24正是与eth0接口直接相连的网络,因此可以直接发到目的主机,不需要经路由器转发。

如果要发送的数据包的目的地址是202.10.1.2,跟后两行路由表条目都不匹配,那么就要按缺省路由条目,从eth0接口发出去,首先发往192.168.232.2 路由器,再让路由器根据它的路由表决定下一跳地址。

A complete matching host address is searched for before a matching network ID. Only if both of these fail is a default route used.

路由的处理过程如下,ARP部分可以参考这里



参考:

Linux C 编程一站式学习》

《TCP/IP详解 卷一》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐