• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 183,  comments - 10,  trackbacks - 0

            前綴匹配

            網(wǎng)絡(luò)層的數(shù)據(jù)報(bào)網(wǎng)絡(luò),在路由器轉(zhuǎn)發(fā)功能實(shí)現(xiàn)中會(huì)用到前綴匹配,即是對(duì) IP 地址與路由表中的目的地址范圍的公共部分進(jìn)行前綴匹配。
            由于有的前綴存在包含的問題,對(duì)有些 IP 地址會(huì)造成多重匹配,短的匹配會(huì)造成 IP 的轉(zhuǎn)發(fā)錯(cuò)誤。
            所以可以遵循 最長前綴匹配原則 進(jìn)行匹配。

            首先做一個(gè) ip 轉(zhuǎn)換的實(shí)現(xiàn)
            從 unsigned int 32 位整型數(shù)到 ip 字符串的轉(zhuǎn)換
            從 ip 字符串到 unsigned int 32 位整型數(shù)的轉(zhuǎn)換

             1 #include <cstdio>
             2 #include <iostream>
             3 #include <string>
             4 using namespace std;
             5 
             6 string uintToIp(unsigned nip)
             7 {
             8     int t[4];
             9     for (int i = 3; i >= 0--i)
            10     {
            11         t[i] = nip % 256;
            12         nip /= 256;
            13     }
            14     string ret;
            15     char s[4];
            16     for (int i = 0; i < 3++i)
            17     {
            18         ret += itoa(t[i], s, 10);
            19         ret += '.';
            20     }
            21     return ret += itoa(t[3], s, 10);
            22 }
            23 
            24 unsigned ipToUint(const string& ip)
            25 {
            26     unsigned ret = 0;
            27     string::size_type st1 = 0, st2 = 0;
            28     string t;
            29     while ((st2 = ip.find('.', st1)) != string::npos)
            30     {
            31         t = ip.substr(st1, st2 - st1);
            32         ret = ret * 256 + atoi(t.c_str());
            33         st1 = st2 + 1;
            34     }
            35     t = ip.substr(st1, ip.size() - st2);
            36     return ret = ret * 256 + atoi(t.c_str());
            37 }
            38 
            39 int main()
            40 {
            41     unsigned nip;
            42     string   ip;
            43     while (cin >> nip)
            44     {
            45         cout << uintToIp(nip) << endl;
            46 
            47         cin >> ip;
            48         cout << ipToUint(ip)  << endl;
            49     }
            50     return 0;
            51 }

             

            ip 的形式有三種
            ·點(diǎn)式十進(jìn)制
            ·二進(jìn)制
            ·unsigned
            它們之間的轉(zhuǎn)換以 unsigned 為橋梁

            前綴匹配的實(shí)現(xiàn)
            利用異或,與前綴進(jìn)行異或,如果得到的結(jié)果的相應(yīng)位都為 0 則說明匹配成功,否則沒有匹配成功。
            ------------------------------
            前綴匹配   鏈路接口
            xxxxxxxxxxx   0
            xxxxxxxxxxxxxx  1
            xxxxxxxxxxx   2
            其他    3
            ------------------------------
            前綴匹配也可以直接尋找在轉(zhuǎn)發(fā)表里的排序前綴中,第一個(gè)大于待匹配的 ip 的那個(gè)前綴即是前綴匹配成功的。

             

              1 #include <cstdio>
              2 #include <iostream>
              3 #include <string>
              4 #include <map>
              5 using namespace std;
              6 
              7 string uintToIp(unsigned nip)
              8 {
              9     int t[4];
             10     for (int i = 3; i >= 0--i)
             11     {
             12         t[i] = nip % 256;
             13         nip /= 256;
             14     }
             15     string ret;
             16     char s[4];
             17     for (int i = 0; i < 3++i)
             18     {
             19         ret += itoa(t[i], s, 10);
             20         ret += '.';
             21     }
             22     return ret += itoa(t[3], s, 10);
             23 }
             24 
             25 unsigned ipToUint(const string& ip)
             26 {
             27     unsigned ret = 0;
             28     string::size_type st1 = 0, st2 = 0;
             29     string t;
             30     while ((st2 = ip.find('.', st1)) != string::npos)
             31     {
             32         t = ip.substr(st1, st2 - st1);
             33         ret = ret * 256 + atoi(t.c_str());
             34         st1 = st2 + 1;
             35     }
             36     t = ip.substr(st1, ip.size() - st2);
             37     return ret = ret * 256 + atoi(t.c_str());
             38 }
             39 
             40 unsigned binaryIpToUint(const string& bip)
             41 {
             42     unsigned ret = 0;
             43     for (string::size_type i = 0; i != bip.size(); ++i)
             44     {
             45         ret = ret * 2 + (bip[i] - '0');
             46     }
             47     return ret;
             48 }
             49 
             50 string uintToBinaryIp(unsigned uip)
             51 {
             52     string ret;
             53     for (int i = 0; i != 32++i)
             54     {
             55         if (uip >= (1 << 31))
             56         {
             57             ret += '1';
             58         }
             59         else
             60         {
             61             ret += '0';
             62         }
             63         uip <<= 1;
             64     }
             65     return ret;
             66 }
             67 
             68 string binaryIpToIp(const string& bip)
             69 {
             70     return uintToIp(binaryIpToUint(bip));
             71 }
             72 
             73 string ipToBinaryIp(const string& ip)
             74 {
             75     return uintToBinaryIp(ipToUint(ip));
             76 }
             77 
             78 //unsigned findNonZeroBitNo(unsigned prefix)
             79 //{
             80 //    unsigned ret = 0;
             81 //    unsigned mask = 1 << 31;
             82 //    while ((prefix & mask) == 0)
             83 //    {
             84 //        ++ret;
             85 //        prefix <<= 1;
             86 //    }
             87 //    return ret;
             88 //}
             89 
             90 bool marchPrefix(unsigned nip, const string& prefix)
             91 {
             92     string ip = uintToBinaryIp(nip);
             93     return ip.substr(0, prefix.size()) == prefix;
             94 }
             95 
             96 struct Prefix
             97 {
             98     string bip;
             99     Prefix(const string& s) : bip(s) {}
            100 };
            101 
            102 bool operator < (const Prefix& lhs, const Prefix& rhs)
            103 {
            104     if (lhs.bip < rhs.bip)
            105     {
            106         if (rhs.bip.find(lhs.bip) != string::npos)
            107         {
            108             return false;
            109         }
            110         else
            111         {
            112             return true;
            113         }
            114     }
            115     else if (lhs.bip > rhs.bip)
            116     {
            117         if (lhs.bip.find(rhs.bip) != string::npos)
            118         {
            119             return true;
            120         }
            121         else
            122         {
            123             return false;
            124         }
            125     }
            126     else
            127     {
            128         return false;
            129     }
            130 }
            131 
            132 bool operator == (const Prefix& lhs, const Prefix& rhs)
            133 {
            134     return lhs.bip == rhs.bip;
            135 }
            136 
            137 int main()
            138 {
            139     map<Prefix, unsigned> forwardingtable;
            140     forwardingtable[Prefix("110010000001011100010")] = 0;
            141     forwardingtable[Prefix("110010000001011100011000")] = 1;
            142     forwardingtable[Prefix("110010000001011100011")] = 2;
            143 
            144     for (map<Prefix, unsigned>::const_iterator cit = forwardingtable.begin(); cit != forwardingtable.end(); ++cit)
            145     {
            146         cout << cit->first.bip << '\t' << cit->second << endl;
            147     }
            148 
            149     unsigned nip;
            150     string   ip;
            151     while (cin >> ip)
            152     {
            153         nip = ipToUint(ip);
            154         //cout << nip << endl;
            155         unsigned linkinterface = 3;
            156         for (map<Prefix, unsigned>::const_iterator cit = forwardingtable.begin(); cit != forwardingtable.end(); ++cit)
            157         {
            158             if (marchPrefix(nip, cit->first.bip) == true)
            159             {
            160                 linkinterface = cit->second;
            161                 break;
            162             }
            163         }
            164         cout << linkinterface << endl;
            165     }
            166     return 0;
            167 }



            posted on 2011-06-17 14:36 unixfy 閱讀(806) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            人人妻久久人人澡人人爽人人精品 | 九九99精品久久久久久| 久久久久久久精品成人热色戒| 久久久久亚洲av毛片大| 色播久久人人爽人人爽人人片aV| 久久婷婷五月综合97色| A级毛片无码久久精品免费| 久久人人爽人人人人片av| 九九久久自然熟的香蕉图片| 久久久久亚洲精品中文字幕| 青青草原精品99久久精品66| 久久精品国产99久久久香蕉| 亚洲综合熟女久久久30p| 久久伊人精品青青草原高清| 久久久一本精品99久久精品66| 久久精品亚洲福利| 97久久精品无码一区二区天美| 久久精品国产亚洲av麻豆蜜芽| 久久无码国产| 久久国产精品久久| 久久精品国产半推半就| 久久伊人色| 欧美国产成人久久精品| 久久婷婷综合中文字幕| 久久久久成人精品无码中文字幕 | 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 久久精品国产色蜜蜜麻豆| 久久99精品国产99久久| 99久久精品国产一区二区 | 久久A级毛片免费观看| 亚洲精品成人网久久久久久| 国内精品久久久久久久coent | 欧美久久天天综合香蕉伊| 久久精品九九亚洲精品| 亚洲欧美日韩中文久久| 久久久久亚洲AV成人网人人网站 | 99精品久久久久中文字幕| 亚洲中文字幕无码久久综合网| 婷婷伊人久久大香线蕉AV | 久久高潮一级毛片免费| 一本大道久久a久久精品综合|