转自:http://blog.chinaunix.net/uid-26275986-id-4106579.html
计算机网络确实是一门精深的学问,之所以这么说,是自己在学习的过程中才了解到设计一个网络互相实现通信是多么复杂的一件事情;平时轻松随意地使用着网络服务,但是谁又能理解到比特流兄们是如何产生、发出、传输、接收、分析最后呈现的呢?就想爱因斯坦说的,其实从某种意义上来说随着科技的人们越来越不”自由“了,因为以前人们对于自己掌握的工具非常了解,而现在,则并不清楚工具的原理。我说的话,这也是社会高度分工的一种体现吧!好,废话少说,言归正传,网络层是”计算机网络“的核心部分,内容比较多,很难在一天的时间内搞定,因此自己决定这部分内容分为两天来学习,今天这篇是昨天学习的一个梳理。
一、网络层到底应当提供什么服务?
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
网络层到底应当向传输层提供怎样的服务?在网络设计实现的初期是人们最为争议的问题。其争论焦点的实质其实就是:在计算机通信中,可靠交付应当由谁来负责?是网络还是端系统?
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
我们知道,物理层的设计解决了所有物理接口特性不一致的问题;数据链路层提供了并行数据向串行数据转换的过程,即使得计算机终端的数据可以在物理层上传说,同时数据链路层还解决了硬件层面寻址定位的问题;那么网络层呢?我们现在看到的网络层IP最大的作用就是路由寻址了。初期有人主张借鉴电信网络的服务特点,网络层提供虚电路服务,其核心思想是数据的可靠交付由网络负责,比如像电话,通话前先拨号建立连接,通话时持续占用线路资源确保通话质量。但是这种设计思路却并不适合计算机网络。一个原因是通话作为连续信号确实有必要建立可靠的连接保障,但是计算机更多的是突发性数据传输,长时间占用一个连接会导致不可避免的带宽资源浪费;另一个原因则是电信线路两端的电话机智能性很低,不足以处理连接差错,但是计算机终端却具有相当的智能,可以处理传输错误的问题,如果把可靠**由计算机负责,那么无疑会降低线路交换机、路由器的设计实现成本。可靠交付由终端负责的模式我们称之为数据报服务。现在我们已经知道,我们最终采用的正式数据报服务的模式设计的网络层。最终网络层向上层只提供简单灵活的、无连接的、尽最大努力交付的数据报服务。下面是两种模式的对比:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>虚电路服务与数据报服务的模式:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/4fae50aeee651818.webp"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>虚电路服务与数据报服务的对比:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/8be1ccb5166feb93.webp"
> src="https://img.php1.cn/3cd4a/1eebe/cd5/7494af3c1cda418d.webp"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
二、网际协议IP
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>1.
IP协议族
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
网际协议IP是TCP/IP体系中两个最重要的协议之一,也是最重要的因特网标准协议之一,与IP协议配套的还有四个协议:ICMP、IGMP、ARP、RARP,之所以把它们放到一起,是因为IP协议经常要用到ARP和RARP协议,而ICMP和IGMP协议常常作为IP数据报的数据部分,它们的关系如下:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/a1be7872e8d4934f.webp"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
IP地址的结构如下:
Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1e618/bdf/129913486c37ddf6.jpeg"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
IP数据报由首部和数据两部分组成,首部的一部分是固定长度,共20字节,是所有IP数据报都必须具有的;在首部的后面是一些可选字段,其长度是可变的。
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-1.
版本:4bit,主要是IPv4与IPv6;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-2.
首部长度:占4比特,最大表示15,单位为4Byte,因此IP数据报首部最大为60字节;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-3.
区分服务:占8bit,一般不用;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-4.
总长度:16位,单位为字节,因此IP数据报的总长度为65535字节;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-5.
标识:占16位,用于IP分片时标注同一个数据报的分片;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-6.
标志:占3位,分片控制信息,MF=1表示还有分片,DF=1表示不能分片;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-7.
片偏移:占13位,用于指出某个IP数据报的分片在整体数据报中的位置;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-8.
生存时间:占8位,每经过一个路由器减一,为0时分组丢弃;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-9.
协议:占8位,用于标识上层数据的协议,如:ICMP-1、IGMP-2、TCP-6、UDP-17、IPv6-41、OSPF-89等;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-10.
首部检验和:16位,仅检验数据报的首部;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-11.
源IP与目的IP,各32位;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>2.
虚拟互连网络
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
虚拟互连网络是一个重要的概念,要理解这个概念必须首先明白几个事实条件。一个是从物理层上看存在着各种各样的网络,比如以太网、无线网、卫星网络等,它们会具有不同的寻址方案、最大分组长度、网络接入机制、超时控制、用户接入控制以及不同的管理和控制方式等;另一个是现实的需要往往各不相同,因此无法找到一种网络满足所有的需求,因此网络必须存在着多样性。这就造成了一个问题,如果要实现上述各种不同网络的连接通信,就必须克服它们物理层上的异构性。比如网络寻址,如果选择MAC地址作为标准,那么就会在物理层的传输时存在不同网络的物理地址的标准转换,这对于一台简单的计算机终端是无法承担的负荷。因此人们提出了利用IP组建虚拟互连网络,所有的网络具有抽象的一致的地址规范,从而使得整体看上去所有的终端都接入了一个虚拟的网络,”虚拟“指的是物理现实中是不同的网络,如图:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/189d8/b64/5b34b53b79a39fdd.jpeg"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>3.
IP地址
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
人们选择使用IP地址来标识网络中的不同主体,其实也很简单,IP地址就是一个32位,即4个字节的整数,当然采用点分十进制把每个字节表示出来就是类似192.168.1.1的形式。IP地址的发展经过了三个阶段:分类的IP地址、子网的划分和构成超网。这里我们分别进行简单的介绍。
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
首先是IP地址分类,每个IP地址都按照网络号+主机号分为不同的类别,一般是不同的网络号长度作为区分类别的标识,任何一本教科书或者度娘上都可以找到,不再赘述:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/70be2ca197098d98.webp"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
这里有一个约定促成的规定,每个网络中,主机位全0的用作该网络ID,主机位全1则用作该网络的广播地址。很快人们就发现这样的分类方法存在着弊端:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-1.
IP地址空间的利用率有时很低,比如一个A类地址的主机数超过1000万,一个B类地址网络主机数也超过6万,有些企业申请到一个B类地址,但是却根本无法物尽其用,但是又不愿意让出资源,导致巨大的浪费;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-2.
给每个物理网络分配一个网络号会使路由表变得太大因而是网络性能变慢;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-3.
两级IP地址不够灵活,有些单位想灵活地添加删除IP地址都必须申请;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
因此人们又提出了”子网“概念,即将原先的IP两级结构(网络号+主机号)的主机号中继续分隔出(子网号+主机号),这样在网络外部还是使用统一的ABC类网络,内部则是一个分类网络的进一步细分,这种细分只有在网络出口的路由器知道,外界仍然看作一个整体的网络。为了便于快速计算一个IP的子网地址,又引入了子网掩码的概念,子网掩码与IP逻辑按位与得到相应的网络地址。
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/9b0d/ae9/2d998ad7838fbf16.jpeg"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
下面我们来看一个例子,如果一个接收到的数据报的目的IP是141.14.72.24,路由表中的子网掩码为255.255.192.0,那么应该转发到哪个子网呢?
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/ff61bfdd3c0af92e.webp"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
上例中如果修改子网掩码为255.255.224.0,那么网络号会变成什么呢?
Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/72fd2c126203a875.webp"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
可以看到,不同的子网掩码作用到相同的IP是有可能得出相同的网络号的。但是每个网络中可以划分的子网数和每个子网中的主机数是不同的。
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>4.
使用子网时的分组转发
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
此时我们的路由器中应该保存着一张路由表,路由表中的记录保存着【目的网络地址+子网掩码+下一跳地址】。划分子网的情况下,路由器转发分组的算法如下:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-1.
路由器从收到的数据报首部提取目的IP地址D;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-2.
先判断是否目的地址在直连网段(即接口直接连接的网络),对路由器直接相连的网络逐个进行检查,用各个网络的子网掩码和D逐位”与“操作,看结果是否和相应的网络地址匹配。若匹配,则把分组进行直接交付,将D转换成物理地址,把数据报封装成帧发送出去,转发任务结束;否则就是间接支付,执行‘-3’;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-3.
路由表特定目的地址匹配:若D与路由表中的特定地址路由匹配,成功则转发数据报到路由表中所指明的下一跳路由器,否则执行‘-4’;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-4.
对路由表中的每一行进行查找匹配,用其中的子网掩码与D做与操作,所得结果N若与改行的目的网络地址匹配,则把数据报传送给该行指明的下一跳路由器;否则执行‘-5’;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-5.
若路由表中有一个默认路由,则把数据报传送个路由表中所指明的默认路由器;否则执行‘-6’;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-6.
报告转发分组出错;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>5.
无分类编址CIDR(构造超网)
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
所谓无分类编址,其实就是推翻之前的ABC分类IP地址和子网分类,统一按照子网掩码来对网络进行标识,这时会出现多个传统的分类网络位于同一个新网络的情况,我们称之为路由聚合,对于这种一个网络包含多个传统网络的网络,我们称之为超网。CIDR最主要的特点有两个:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-1.
CIDR消除了传统的A类、B类和C类地址以及划分子网的概念,划分为【网络前缀+主机号】的二级结构;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-2.
CIDR把网络前缀都相同的连续的IP地址组成一个CIDR地址块,网络前缀越短,包含的主机就越多;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
其实在自己看来,所谓的CIDR就是严格按照子网掩码来区分路由表的子网了,并且在遇到匹配结果有多个时,按照最长的子网掩码优先匹配,即最长前缀匹配原则:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/a5d7215df572c386.webp"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>6.
二叉树搜索
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
对于CIDR应该在路由器中如何存放路由表呢?如果按照正常按序逐个匹配无疑是差劲的方案,注意到CIDR的网络前缀的重要性,因此只需要按照‘0-1’建立关于网络前缀的唯一前缀的层次结构,然后在查找时通过二叉树搜索匹配到标识路由项的叶子节点,叶子节点上应该保存有该网络的掩码和下一跳地址,看D|Netmask
?= 唯一前缀,若匹配则按下一跳转发,否则丢弃该分组。
Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/e3aa5425383ba10d.png"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
三、IP地址与ARP协议
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
这里涉及一个重要的观念,数据报在同一个网络内是按硬件地址寻址,只有在跨网络时才是IP寻址。在IP层抽象的互联网上只能看到IP数据报,路由器只根据目的IP地址的网络号路由转发,而在局域网的链路层,只能看见MAC帧。ARP协议就是用于建立IP地址与MAC地址之间对应的协议:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/94ce/a6e/865776f6fe3d5f59.jpeg"
>
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>
这里重点来介绍一下ARP的过程,ARP协议用于主机获得目的IP的MAC地址,从而可以在链路上寻址发送。其主要过程为:
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-1.
当主机A要向本局域网上的某个主机B发送IP数据报时,先检查其ARP高速缓存,若有主机B的IP地址,则直接将这个地址写入到MAC帧中,然后通过局域网把该MAC帧广播发送到该硬件地址;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-2.
若检查不到主机B的IP地址的项目,则运行ARP协议,首先在本局域网上发送一个ARP请求;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-3.
本局域网上所有运行主机上运行的ARP进程都收到此ARP分组;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-3.
主机B的ARP进程分析请求帧中的IP地址是自己的,因此向主机A发送ARP响应分组,告知自己的MAC地址;同时将A的IP-MAC地址写到自己的ARP高速缓存;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-4.
主机A收到B的ARP响应分组后,在其ARP告诉缓存中写入主机B的IP地址到硬件地址的映射;
Microsoft YaHei"; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘>-5.
若所找的IP地址在另一个网络需要传递路由器,则ARP会找到本网络上路由器的接口的MAC地址返回;
Microsoft YaHei"; border: 0px currentColor; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; word-wrap: break-word; font-size-adjust: none; font-stretch: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;‘
src="https://img.php1.cn/3cd4a/1eebe/cd5/4fae50aeee651818.webp"
>