网络层
2022-03-18 本文已影响0人
freemanIT
网络层
网络层数据包(Packet): 首部, 数据两部分组成
数据: 很多时候是由传输层传递下来的数据段(Segment)

首部 - 版本, 长度, 区分服务
版本(Version)
- 占4 位
- 0b0100: IPv4
- 0b0110: IPv6
长度(Header Length)
- 占4 位, 二进制乘4, 是最终的长度
- 0b0101: 20 最小值
- 0b1111: 60 最大值
区分服务(Differentiated Services Field)
- 8 位
- 可以用于提高网络访问质量(QoS: Quality of Service)
首部 - 总长度
总长度(Total Length)
- 16 位
- 首部 + 数据的长度之和, 最大值65535

帧数据最大不超过1500 字节, 超过数据, 需要分片(Fragment) 传输给数据链路层, 每一篇都有自己的网络层首部(IP首部)
网络层首部 - 标识, 标志
标识(Identification)
- 16 位
- 数据包的ID, 当书包过大进行分片时, 同一个数据包的所有篇的标识都是一样的
- 有个计数器专门管理数据包的ID, 没发出一个数据包, ID增加1
标志(Flags)
- 16位
- 第一位(Reserved Bit), 保留位
- 第二位(Don't Fragment), 1 代表不允许分片, 0 代表允许分片
- 第三位(More Fragment), 1 代表不是最后一片, 0 代表最后一片
ping 的几个用法,
- ping /? 查看ping 的用法
- ping ip地址 -l 数据包大小 发送指定大小的数据包(ping www.baidu.com -l 1400)
- ping ip地址 -f, 不允许网络分层(ping www.baidu.com -l 2000 -f)
ping使用, 发送4 次ICMP 数据包

wireshark 查看数据


网络层首部 - 片偏移
片偏移(Fragment Offset)
- 13 位
- 片偏移*8, 字节偏移
- 每一片的长度是8 的整数倍

网络层首部 - 生存时间
生存时间(Time To Live, TTL)
-
8 位
-
每个路由器在转发之前会将TTL 减1, 为0, 路由器返回错误
-
能过推测对方的操作系统, 中间经过多少个路由器

根据TTL 追踪经过路由器
ping ip地址 -i TTL 设置TTL值
通过tracert, pathping, 追踪路由器


网络层首部 - 协议, 首部校验和
协议(Protocol)
- 8 位
- 使用的协议

首部校验和(Header Checksum)
- 检查首部是否有错误

