计算机网络基础(七)---网络层-ICMP协议

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文章内容概览"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/99/99c390afa76faa67c10d669b115844fc.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"ICMP协议详解"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"网际控制报文协议(Internet Control Message Protocol)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"该协议主要是用于辅助IP协议进行数据传输的"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ICMP协议"},{"type":"text","marks":[{"type":"strong"}],"text":"可以报告错误信息或者异常情况"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"ICMP的报文数据是通过封装在IP数据报中进行数据传输的"},{"type":"text","text":"。ICMP的报文分为两个部分:ICMP报文首部和ICMP报文数据"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0c/0c3fa34b0dc935d512bc2099c196a50c.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"ICMP报文首部"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/07/07ebb633f98907dd933bc3813a76c92e.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"类型"},{"type":"text","text":":主要指的是ICMP报文的种类(主要有两大类,后边会进行说明)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"代码"},{"type":"text","text":":主要是指,不同的ICMP报文种类具体有哪些错误"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"校验和"},{"type":"text","text":":主要是校验报文在整个传输中,是否存在错误"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在前边介绍IP协议首部中,有一个8位协议,它表明的是IP数据所携带的具体数据是什么协议的。ICMP的字段值为1"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/0b/0b2618dc8c86b98de76118c1def17c15.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果IP协议传输的数据是ICMP数据的话,那么,将会在协议中写入1"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b8/b82ce19fd0e8d9d21c5c464b4ac9a8d3.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"ICMP协议报文的两个种类"}]},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"差错报告报文"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f5/f5756b405f4cfab9d09118d41aa10159.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"网络不可达"},{"type":"text","text":":IP地址可以表示一个网络,当主机号全为0时就表示的是某一个网络,如果整个网络不可达,就会报告一个类型为3,具体代码为0的ICMP协议报文"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"主机不可达"},{"type":"text","text":":如果计算机A要和计算机B进行通信,而计算机B是关机的状态,就会出现主机不可达的情况"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"网络重定向"},{"type":"text","text":":传输给某一个网络的数据,可能不能走该网络了,需要进行重定向"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"主机重定向"},{"type":"text","text":":如果发送的报文,主机告知不能处理,请发送到另外一个主机"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":5},"content":[{"type":"text","text":"询问报文"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c4/c49deb877d596d27fc4b876f40e3c3ae.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"回送请求或应答"},{"type":"text","text":":主要是验证网络是否通。假设计算机A要和计算机B进行通信,A会发送一个空的数据给B,如果B收到,就给一个回应"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"时间戳请求或应答"},{"type":"text","text":":当需要进行时间同步时,会用到这个"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"ICMP协议的应用"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Ping应用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在前边的文章中有使用到ping命令来查看网络的响应时间和TTL。这部分主要是介绍ping的原理是什么"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ping应用主要是使用ICMP的"},{"type":"text","marks":[{"type":"strong"}],"text":"询问报文"},{"type":"text","text":",它会发送"},{"type":"text","marks":[{"type":"strong"}],"text":"回送请求或应答"},{"type":"text","text":"类型的报文。当使用ping命令的时候,它首先会组装一个IP协议的报文,然后在IP的数据报中组装ICMP的询问报文,然后通过数据链路层发送出去,这个就是ping应用的原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通常我们会使用ping命令来查看本地的网络是否通,或者网络质量是怎么样的。平时在家中,如何出现网络故障,我们也会使用ping命令来进行排查"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"第一步会先ping本地回环地址127.0.0.1(如果没有返回,说明协议栈有问题,这个时候可能需要重装系统,或重新安装协议栈)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果上一步正常返回,则ping网关地址(也就是路由器地址)。如果计算机是处于内网的话,一般就是ping 192.168.0.1或者ping 192.168.1.1。如果上一步ping网关也是通的,说明本机到路由器是通的,如果不通,说明网线或者wifi是有问题的"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果ping网关是通的,则ping远端地址(比如百度)。如果不通,则说明在你家到ISP之间网络是故障的,这个时候就需要联系电信或移动或联通"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Traceroute应用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Traceroute可以探测IP数据报在网络中走的路径"},{"type":"text","text":",在"},{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/1305a459158b19e5c7bbecaaa","title":""},"content":[{"type":"text","text":"IP协议详解"}]},{"type":"text","text":"这篇文章中,有介绍到计算机是连接在虚拟互联网络中的,我们并不关心数据报在网络中经过了哪些路径。但是,如果需要对网络故障进行排查的话,Traceroute应用可以提供更高级的功能,使我们更了解网络是怎么样的"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Traceroute应用原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在前边介绍IP首部时,首部里边有一个TTL,表明IP数据报文在网络中的寿命,每经过一个设备,TTL减1,当TTL为0时,"},{"type":"text","marks":[{"type":"strong"}],"text":"网络设备必须丢弃该报文"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9f/9f8ef7916f79013b4387b00a72c38ecc.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当TTL为0时,网络设备必须丢弃该报文。但是,并没有告诉丢弃的时候应该怎么处理。了解了ICMP报文之后可以知道,如果网络设备要丢弃报文,将会发送一个"},{"type":"text","marks":[{"type":"strong"}],"text":"ICMP终点不可达差错报文"},{"type":"text","text":"。也就是说,它会报告一个错误,告诉源端主机,报文传输发生了错误,这样源主机才会察觉到问题。下边是一个例子"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"借助前边用到的一个图,该图指示了从计算机A到计算机B的网络走向,前边的文章中提到,数据报文是一跳一跳的传输的,因此它会先后跳到中型网络=》大型网络=》中型网络=》小型网络=》计算机B。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f9/f9eb2e163354e19237dcfcc4d0ced284.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Traceroute应用巧妙的应用了"},{"type":"text","marks":[{"type":"strong"}],"text":"ICMP差错不可达报文"},{"type":"text","text":"的作用,为了探测数据报走过的路径,它"},{"type":"text","marks":[{"type":"strong"}],"text":"首先会封装一个TTL为1的IP报文"},{"type":"text","text":"。在上图中,计算机A发送了一个TTL为1的报文之后,到达中型网络之后就会发现TTL为0了,此时就会往源机器(也就是A)发出一个ICMP差错不可达报文。此时,计算机A会记录该中型网络的IP。此时Traceroute会再封装一个TTL为2的请求报文,当数据到达大型网络之后,TTL为0了。大型网络会丢弃该报文,并且向源机器发出一个ICMP差错不可达报文。此时计算机A又会记录大型网络的IP。"},{"type":"text","marks":[{"type":"strong"}],"text":"TTL会依次的增加,直到收到了正常的回应,表示说计算机A发送的报文,已经被B收到了"},{"type":"text","text":"。此时,计算机A已经收到了所有的路径机器的信息,此时就可以输出数据报在网络中走过的路径了"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,Traceroute应用巧妙的应用了ICMP差错不可达报文的作用,每次将TTL加1,直到到达目标机器,记录经过的每一个机器的IP,因此来获取路径。这就是Traceroute应用的原理"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"看一波实际操作,在windows中可以使用"},{"type":"text","marks":[{"type":"strong"}],"text":"tracert+域名/IP"},{"type":"text","text":"的命令来查看输出。Mac中有自带的工具可以使用"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c1/c1d9380e922e3c8f7a3d81de30ce36d8.png","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先是可以看到百度的地址为:182.61.200.7。然后packets下边第一跳就是我的内网路由器地址192.168.0.1。第二跳来到的是113.45.64.1,这个应该就是小区的内网IP。后边的每一跳所经过的IP,可以直接在百度上搜一下是哪里的IP"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果想看到更加详细的跨省路径,可以追踪访问github.com,因为这个服务器在美国,所以会比较慢"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在快速变化的技术中寻找不变,才是一个技术人的核心竞争力。知行合一,理论结合实践"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5e/5eb4b2fdb444b8c7257756dc6b23fada.png","alt":null,"title":"","style":[{"key":"width","value":"25%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章