动态路由协议——————————RIP(原理+实验)

第一章  什么是RIP    

  • 距离矢量路由协议,属于IGP协议
  • 适合中小企业网络,有RIPv1和RIPv2、RIPv3版本
  • 基于UDP,端口号520
  • 周期性(25.5~30.0/次)更新
  • 支持水平分割、毒性反转和触发更新等防环机制

  1.2  RIP两个版本的区别

        RIPv1:
                (1)不支持认证
                (2)有类路由协议
                (3)不支持VLSM,CIDR
                (4)以广播的形式发送报文

        RIPv2:
                (1)支持明文认证和MD5认证
                (2)无类别路由协议
                (3)支持VLSM,CIDR
                (4)支持以广播或者组播(224.0.0.9)方式发送报文

    1.3  RIP的度量值

        (1)RIP把跳数作为度量值来衡量到达目的网络的距离
        (2)缺省(默认)情况下,直连网络的路由跳数为0,当路由器发送路由更新时,会将度量值加1
        (3)RIP规定超过15跳为网络不可达

第二章  RIP实验

        实验任务:(1)实现全网互通
                          (2)当互通以后,切换到另一条路径,也使其互通

        此实验拓扑,举R1的配置示例,其余配置均在拓扑上展现:

        [R1]rip 1                                        //进入rip路由,名字为1
        [R1]verson 2                                //rip的版本为2(建议为版本2)
        [R1]network 10.0.0.0                  //宣告R1连接的两个网段的路由信息
        [R1]network 30.0.0.0

        在R4上面配置一个环回口0,作为测试用:
                [R4]int LoopBack 0                    //进入测试口0中
                [R4]ip address 192.168.1.1 24                     //给loopback0配置IP地址

                这样就可Ping通R4上面的测试接口 

        我们发现R1走的是上行链路,那么我们如何让其走下行链路呢?

                [R1-GigabitEthernet0/0/0]undo rip input         //禁止接口接收RIP的数据包

开启之后,我们再来测试一下:

        最后附上实验拓扑,以及RIP的笔记

拓扑实验:        链接:https://pan.baidu.com/s/15GvS5JKSgqdJ8-48dpid9A 
                          提取码:6678 
笔记:               链接:https://pan.baidu.com/s/1CKKdQx4okxeZqRJfcYdmNw 
                          提取码:6678

  • 27
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很高兴能够回答你的问题。RIP(Routing Information Protocol)是一个内部网关协议,它用于在同一组织的网络中传递路由信息。RIP使用距离向量算法来确定最佳路径,并且具有自适应性,可以自动调整网络拓扑变化。 以下是RIP的C++代码实现: ``` #include <iostream> #include <vector> #include <string> #include <limits.h> #include <algorithm> #include <unordered_map> using namespace std; // 定义路由表项 struct RoutingTableEntry { string destination; // 目标网络IP地址 int hopCount; // 经过的跳数 string nextHop; // 下一跳路由器IP地址 }; // 实现路由器节点 class Router { public: Router(string routerId) { id = routerId; } // 添加相邻路由器 void addNeighbor(Router* neighbor, int cost) { neighbors[neighbor] = cost; } // 更新路由表 void updateRoutingTable() { routingTable.clear(); unordered_map<string, int> distanceVector; distanceVector[id] = 0; // 初始化距离向量 for (auto& neighbor : neighbors) { distanceVector[neighbor.first->id] = neighbor.second; } // 迭代更新距离向量 bool updated = true; while (updated) { updated = false; for (auto& neighbor : neighbors) { unordered_map<string, int> neighborVector = neighbor.first->getDistanceVector(); for (auto& item : neighborVector) { if (item.first == id) { continue; } int distance = neighbor.second + item.second; if (distance < distanceVector[item.first]) { distanceVector[item.first] = distance; updated = true; } } } } // 生成路由表 for (auto& item : distanceVector) { if (item.first == id) { continue; } int minCost = INT_MAX; Router* minNeighbor = nullptr; for (auto& neighbor : neighbors) { if (neighbor.first->id == item.first) { minCost = neighbor.second; minNeighbor = neighbor.first; break; } unordered_map<string, int> neighborVector = neighbor.first->getDistanceVector(); auto iter = neighborVector.find(item.first); if (iter != neighborVector.end() && neighbor.second + iter->second < minCost) { minCost = neighbor.second + iter->second; minNeighbor = neighbor.first; } } RoutingTableEntry entry; entry.destination = item.first; entry.hopCount = minCost; entry.nextHop = minNeighbor->id; routingTable.push_back(entry); } } // 获取距离向量 unordered_map<string, int> getDistanceVector() { unordered_map<string, int> distanceVector; for (auto& neighbor : neighbors) { distanceVector[neighbor.first->id] = neighbor.second; } return distanceVector; } // 打印路由表 void printRoutingTable() { cout << "Routing table of router " << id << ":" << endl; cout << "Destination\tHop count\tNext hop" << endl; for (auto& entry : routingTable) { cout << entry.destination << "\t\t" << entry.hopCount << "\t\t" << entry.nextHop << endl; } cout << endl; } private: string id; // 路由器ID unordered_map<Router*, int> neighbors; // 相邻路由器和其距离 vector<RoutingTableEntry> routingTable; // 路由表 }; int main() { // 创建四个路由器节点 Router* r1 = new Router("R1"); Router* r2 = new Router("R2"); Router* r3 = new Router("R3"); Router* r4 = new Router("R4"); // 添加相邻路由器和距离 r1->addNeighbor(r2, 2); r1->addNeighbor(r3, 1); r2->addNeighbor(r1, 2); r2->addNeighbor(r3, 3); r2->addNeighbor(r4, 1); r3->addNeighbor(r1, 1); r3->addNeighbor(r2, 3); r3->addNeighbor(r4, 2); r4->addNeighbor(r2, 1); r4->addNeighbor(r3, 2); // 更新路由表 r1->updateRoutingTable(); r2->updateRoutingTable(); r3->updateRoutingTable(); r4->updateRoutingTable(); // 打印路由表 r1->printRoutingTable(); r2->printRoutingTable(); r3->printRoutingTable(); r4->printRoutingTable(); return 0; } ``` 上述代码实现了一个简单的RIP路由器,它可以自动计算最佳路径并生成路由表。需要注意的是,该实现并不完整,仅仅是提供了一个基本的框架,如果需要实际使用,还需要进行更多的改进和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值