計算機網絡基礎(七)---網絡層-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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章