码迷,mamicode.com
首页 > 其他好文 > 详细

CIDR详解和ip最长地址前缀匹配

时间:2015-04-15 10:51:55      阅读:738      评论:0      收藏:0      [点我收藏+]

标签:

  1.CIDR是什么

  无类域间路由(CIDR)编址方案

  摒弃传统的基于类的地址分配方式,允许使用任意长度的地址前缀,有效提高地址空间的利用率。

  就是一个ip加一个网络掩码,不过这个掩码不是之前只有3个值(A类:8,B类:16,C类:24),而是0-32随意的一个值。

  例如:

  208.12.128.0/17

  2.如何理解CIDR格式

  上图

  

技术分享

 

  可以理解为一个从0到(2^32-1)长的线段.

  掩码32表示n个ip的点,数量n是2^32.

  掩码31表示n个小线段1,每个线段1包含2(2^(32-31))个ip点,数量n是2^31.

  掩码30表示n个小线段2,每个线段2包含4(2^(32-30))个ip点,2个小线段1(2^(31-30)),数量n是2^30.

  以此类推,可以形象的理解成"刻度尺".

  3.每个CIDR之间的关系

  1.相离

  见上图

  208.12.16/24

  208.12.21/24

  2.相近

  见上图

  208.12.16/24

  208.12.17/24

  3.包含

  如图

  

技术分享

 

  208.12.21/24

  208.12.16/20

  为什么2个CIDR不能相交?

  2个CIDR假设为ip1/mask1,ip2/mask2.

  mask3 = (mask1 > mask2) ? mask2:mask1;

  ip1/(mask3) 和ip2/(mask3)只有相等和不相等2种情况。

  不相等:相离或相近

  相等:包含

  形象的可以想象下“刻度尺”,其中就没有相交的情况。

  4.ip最长地址前缀匹配

  1.穷举法

  已知一个ip假设为208.12.16.188

  查找hash表,key为(ip & mask)

  其中mask从32递减到0

  最坏时间复杂度=O(32)*hash查找

  2.最长前缀匹配

  ip地址就是一个32bit的数,构建一个bit前缀树进行最长前缀匹配。

  最坏时间复杂度=O(32)

  3.线性表的2分查找

  算出每一个CIDR的(ip & mask)值,按此值排序(从小到大)

  当要查询一个ip地址时,假设为ip1,找出>ip1的第一个值,此值的左面的值假设为(ip2 & mask2)

  判断一下 ip1 & mask2 是否=ip2(根据需要要可以判断一下此ip段的超集,构建线性表时,超集关系就可以得出)。

  最坏时间复杂度=O(lg(n)),n为CIDR的数量。

  http://www.lwinfo.com/uzt/list1/205798.html

  http://www.lwinfo.com/uzt/list1/205799.html

  http://www.lwinfo.com/uzt/list1/205800.html

  http://www.lwinfo.com/uzt/list1/205801.html

  http://www.lwinfo.com/uzt/list1/205802.html

  http://www.lwinfo.com/uzt/list1/205803.html

  http://www.lwinfo.com/uzt/list1/205804.html

  http://www.lwinfo.com/uzt/list1/205805.html

  http://www.lwinfo.com/uzt/list1/205806.html

  http://www.lwinfo.com/uzt/list1/205807.html

  http://www.lwinfo.com/uzt/list1/205808.html

  http://www.lwinfo.com/uzt/list1/205809.html

  http://www.lwinfo.com/uzt/list1/205810.html

  http://www.lwinfo.com/uzt/list1/205811.html

  http://www.lwinfo.com/uzt/list1/205812.html

  http://www.lwinfo.com/uzt/list1/205813.html

  http://www.lwinfo.com/uzt/list1/205814.html

  http://www.lwinfo.com/uzt/list1/205815.html

  http://www.lwinfo.com/uzt/list1/205816.html

  http://www.lwinfo.com/uzt/list1/205817.html

  http://www.lwinfo.com/uzt/list1/205818.html

  http://www.lwinfo.com/uzt/list1/205819.html

  http://www.lwinfo.com/uzt/list1/205820.html

  http://www.lwinfo.com/uzt/list1/205821.html

  http://health.people.com.cn/xywy/wfyxb/tp/8560576074.html

  http://health.people.com.cn/xywy/wfyxb/tp/8560568338.html

  http://health.people.com.cn/xywy/wfyxb/tp/8560555217.html

  http://health.people.com.cn/xywy/wfyxb/tp/8560544077.html

  http://health.people.com.cn/xywy/wfyxb/tp/8560536447.html

  http://health.people.com.cn/xywy/wfyxb/tp/8560521036.html

  http://health.people.com.cn/xywy/wfyxb/tp/8560506993.html

  http://health.people.com.cn/xywy/wfyxb/npxpf/8560491297.html

  http://health.people.com.cn/xywy/wfyxb/npxpf/8560468296.html

  http://health.people.com.cn/xywy/wfyxb/npxpf/8560449808.html

  http://health.people.com.cn/xywy/wfyxb/npxpf/8560432844.html

  http://health.people.com.cn/xywy/wfyxb/npxpf/8560399463.html

  http://health.people.com.cn/xywy/wfyxb/npxpf/8560378393.html

  http://health.people.com.cn/xywy/wfyxb/npxpf/8560360944.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560336583.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560327736.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560309612.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560298508.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560278852.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560258049.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560238514.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560223593.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560209303.html

  http://health.people.com.cn/xywy/wfyxb/wfyxb/8560199136.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560177261.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560165353.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560154522.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560139793.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560127019.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560086063.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560076124.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560061982.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560045696.html

  http://health.people.com.cn/xywy/wfyxb/zl/8560032266.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8560018414.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8560008488.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8559989274.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8559965081.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8559948908.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8559940242.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8559931126.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8559917222.html

  http://health.people.com.cn/xywy/wfyxb/kfal/8559874838.html

  http://health.people.com.cn/xywy/wfyxb/yyxw/8559861683.html

  http://health.people.com.cn/xywy/wfyxb/yyxw/8559843847.html

  http://health.people.com.cn/xywy/wfyxb/yyxw/8559827148.html

  http://health.people.com.cn/xywy/wfyxb/yyxw/8559807673.html

  http://health.people.com.cn/xywy/wfyxb/yyxw/8559793273.html

  http://health.people.com.cn/xywy/wfyxb/yyxw/8559777787.html

  http://health.people.com.cn/xywy/wfyxb/yyxw/8559613538.html

CIDR详解和ip最长地址前缀匹配

标签:

原文地址:http://www.cnblogs.com/xggb/p/4427661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!