NS3 的 ipv4-static-routing-test-suite 源码分析

2023-10-10 15:08

本文主要是介绍NS3 的 ipv4-static-routing-test-suite 源码分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下面进行源码注释:


// End-to-end tests for Ipv4 static routing#include "ns3/boolean.h"
#include "ns3/config.h"
#include "ns3/inet-socket-address.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/node.h"
#include "ns3/node-container.h"
#include "ns3/packet.h"
#include "ns3/pointer.h"
#include "ns3/simulator.h"
#include "ns3/string.h"
#include "ns3/test.h"
#include "ns3/uinteger.h"
#include "ns3/simple-net-device.h"
#include "ns3/simple-channel.h"
#include "ns3/simple-net-device-helper.h"
#include "ns3/socket-factory.h"
#include "ns3/udp-socket-factory.h"using namespace ns3;/*** \ingroup internet-test* \ingroup tests** \brief IPv4 StaticRouting /32 Test*/
class Ipv4StaticRoutingSlash32TestCase : public TestCase
{
public:Ipv4StaticRoutingSlash32TestCase ();virtual ~Ipv4StaticRoutingSlash32TestCase ();Ptr<Packet> m_receivedPacket; //!< Received packet/*** \brief Send data.* \param socket The sending socket.* \param to Destination address.*/void DoSendData (Ptr<Socket> socket, std::string to);/*** \brief Send data.* \param socket The sending socket.* \param to Destination address.*/void SendData (Ptr<Socket> socket, std::string to);/*** \brief Receive data.* \param socket The receiving socket.*/void ReceivePkt (Ptr<Socket> socket);private:virtual void DoRun (void);
};// Add some help text to this case to describe what it is intended to test
Ipv4StaticRoutingSlash32TestCase::Ipv4StaticRoutingSlash32TestCase (): TestCase ("Slash 32 static routing example")
{
}Ipv4StaticRoutingSlash32TestCase::~Ipv4StaticRoutingSlash32TestCase ()
{
}void
Ipv4StaticRoutingSlash32TestCase::ReceivePkt (Ptr<Socket> socket)
{uint32_t availableData;availableData = socket->GetRxAvailable ();m_receivedPacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);NS_ASSERT (availableData == m_receivedPacket->GetSize ());//cast availableData to void, to suppress 'availableData' set but not used//compiler warning(void) availableData;
}void
Ipv4StaticRoutingSlash32TestCase::DoSendData (Ptr<Socket> socket, std::string to)
{Address realTo = InetSocketAddress (Ipv4Address (to.c_str ()), 1234);NS_TEST_EXPECT_MSG_EQ (socket->SendTo (Create<Packet> (123), 0, realTo),123, "100");
}void
Ipv4StaticRoutingSlash32TestCase::SendData (Ptr<Socket> socket, std::string to)
{m_receivedPacket = Create<Packet> ();Simulator::ScheduleWithContext (socket->GetNode ()->GetId (), Seconds (60),&Ipv4StaticRoutingSlash32TestCase::DoSendData, this, socket, to);Simulator::Stop (Seconds (66));Simulator::Run ();
}// Test program for this 3-router scenario, using static routing
//
// (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
//
void
Ipv4StaticRoutingSlash32TestCase::DoRun (void)
{//这里创建三个节点Ptr<Node> nA = CreateObject<Node> ();Ptr<Node> nB = CreateObject<Node> ();Ptr<Node> nC = CreateObject<Node> ();// 安装协议栈NodeContainer c = NodeContainer (nA, nB, nC);InternetStackHelper internet;internet.Install (c);// 创建了2个链接// a-------------b----------------c// |1.1     1.2  | 1.5        1.6 |// 172.116.1.1/24                 192.168.1.1/24NodeContainer nAnB = NodeContainer (nA, nB);NodeContainer nBnC = NodeContainer (nB, nC);// 路由器A添加一个接入的网卡Ptr<SimpleNetDevice> deviceA = CreateObject<SimpleNetDevice> ();deviceA->SetAddress (Mac48Address::Allocate ());nA->AddDevice (deviceA);// 路由器C添加一个接入网卡Ptr<SimpleNetDevice> deviceC = CreateObject<SimpleNetDevice> ();deviceC->SetAddress (Mac48Address::Allocate ());nC->AddDevice (deviceC);// 对AB和BC链路分别添加一个网间网的互联网卡SimpleNetDeviceHelper devHelper;NetDeviceContainer dAdB = devHelper.Install (nAnB);NetDeviceContainer dBdC = devHelper.Install (nBnC);// AB链路网间网地址Ipv4AddressHelper ipv4;ipv4.SetBase ("10.1.1.0", "255.255.255.252");Ipv4InterfaceContainer iAiB = ipv4.Assign (dAdB);// BC链路的网间网地址ipv4.SetBase ("10.1.1.4", "255.255.255.252");Ipv4InterfaceContainer iBiC = ipv4.Assign (dBdC);// 这里为3个路由器分别建立1个路由表,所有后面没有安装的函数,Ptr<Ipv4> ipv4A = nA->GetObject<Ipv4> ();Ptr<Ipv4> ipv4B = nB->GetObject<Ipv4> ();Ptr<Ipv4> ipv4C = nC->GetObject<Ipv4> ();// 路由表A中添加了一个接口,返回了接口的索引int32_t ifIndexA = ipv4A->AddInterface (deviceA);Ipv4InterfaceAddress ifInAddrA = Ipv4InterfaceAddress (Ipv4Address ("172.16.1.1"), Ipv4Mask ("/32"));ipv4A->AddAddress (ifIndexA, ifInAddrA);   // 路由表内,将接口和地址绑为一项ipv4A->SetMetric (ifIndexA, 1);            // 到该端口是几跳ipv4A->SetUp (ifIndexA);                   // 将端口设置为UP状态// 路由表C中添加了一个接口,返回了接口的索引int32_t ifIndexC = ipv4C->AddInterface (deviceC);Ipv4InterfaceAddress ifInAddrC = Ipv4InterfaceAddress (Ipv4Address ("192.168.1.1"), Ipv4Mask ("/32"));ipv4C->AddAddress (ifIndexC, ifInAddrC);ipv4C->SetMetric (ifIndexC, 1);ipv4C->SetUp (ifIndexC);// 静态路由Ipv4StaticRoutingHelper ipv4RoutingHelper;// 从A到CPtr<Ipv4StaticRouting> staticRoutingA = ipv4RoutingHelper.GetStaticRouting (ipv4A);// The ifIndex 是 1; the first p2p link added// 到C的接入网段地址,需要发送到P2P的AB网间网的B口!!staticRoutingA->AddHostRouteTo (Ipv4Address ("192.168.1.1"), Ipv4Address ("10.1.1.2"), 1);// 同样需要在路由器B上设置相关路由Ptr<Ipv4StaticRouting> staticRoutingB = ipv4RoutingHelper.GetStaticRouting (ipv4B);// The ifIndex on 路由器 B 是 2; 0 是 loopback地址,  1 是AB链路的接口// 到C的接入网段地址,需要发送到P2P的BC网间网的C口!!staticRoutingB->AddHostRouteTo (Ipv4Address ("192.168.1.1"), Ipv4Address ("10.1.1.6"), 2);// 创建发送UDP数据包的测试Ptr<SocketFactory> rxSocketFactory = nC->GetObject<UdpSocketFactory> ();Ptr<Socket> rxSocket = rxSocketFactory->CreateSocket ();NS_TEST_EXPECT_MSG_EQ (rxSocket->Bind (InetSocketAddress (Ipv4Address ("192.168.1.1"), 1234)), 0, "trivial");rxSocket->SetRecvCallback (MakeCallback (&Ipv4StaticRoutingSlash32TestCase::ReceivePkt, this));Ptr<SocketFactory> txSocketFactory = nA->GetObject<UdpSocketFactory> ();Ptr<Socket> txSocket = txSocketFactory->CreateSocket ();txSocket->SetAllowBroadcast (true);// ------ Now the tests ------------// Unicast testSendData (txSocket, "192.168.1.1");NS_TEST_EXPECT_MSG_EQ (m_receivedPacket->GetSize (), 123, "Static routing with /32 did not deliver all packets.");Simulator::Destroy ();
}/*** \ingroup internet-test* \ingroup tests** \brief IPv4 StaticRouting /32 TestSuite*/
class Ipv4StaticRoutingTestSuite : public TestSuite
{
public:Ipv4StaticRoutingTestSuite ();
};Ipv4StaticRoutingTestSuite::Ipv4StaticRoutingTestSuite (): TestSuite ("ipv4-static-routing", UNIT)
{AddTestCase (new Ipv4StaticRoutingSlash32TestCase, TestCase::QUICK);
}static Ipv4StaticRoutingTestSuite ipv4StaticRoutingTestSuite; //!< Static variable for test initialization

 

这篇关于NS3 的 ipv4-static-routing-test-suite 源码分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/181232

相关文章

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Redis主从复制的原理分析

《Redis主从复制的原理分析》Redis主从复制通过将数据镜像到多个从节点,实现高可用性和扩展性,主从复制包括初次全量同步和增量同步两个阶段,为优化复制性能,可以采用AOF持久化、调整复制超时时间、... 目录Redis主从复制的原理主从复制概述配置主从复制数据同步过程复制一致性与延迟故障转移机制监控与维

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专

Spring中Bean有关NullPointerException异常的原因分析

《Spring中Bean有关NullPointerException异常的原因分析》在Spring中使用@Autowired注解注入的bean不能在静态上下文中访问,否则会导致NullPointerE... 目录Spring中Bean有关NullPointerException异常的原因问题描述解决方案总结

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit

python-nmap实现python利用nmap进行扫描分析

《python-nmap实现python利用nmap进行扫描分析》Nmap是一个非常用的网络/端口扫描工具,如果想将nmap集成进你的工具里,可以使用python-nmap这个python库,它提供了... 目录前言python-nmap的基本使用PortScanner扫描PortScannerAsync异