ns3不使用Ipv4AddressHelper的替代方法

来源:互联网 发布:vs php 扩展 编辑:程序博客网 时间:2024/06/11 23:51

不使用IPv4AddressHelper,那么就要自己手工分配,如下是我写的分配代码


int32_t AssignAddress (Ptr<NetDevice> device, uint32_t u32Addr, uint32_t u32Mask ){  NS_LOG_FUNCTION_NOARGS ();  //Ipv4InterfaceContainer retval;  Ptr<Node> node = device->GetNode ();  NS_ASSERT_MSG (node, "Ipv4AddressHelper::Assign(): NetDevice is not not associated "                 "with any node -> fail");  Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();  NS_ASSERT_MSG (ipv4, "Ipv4AddressHelper::Assign(): NetDevice is associated"                 " with a node without IPv4 stack installed -> fail "                 "(maybe need to use InternetStackHelper?)");  int32_t interface = ipv4->GetInterfaceForDevice (device);  if (interface == -1)    {      interface = ipv4->AddInterface (device);    }  NS_ASSERT_MSG (interface >= 0, "Ipv4AddressHelper::Assign(): "                 "Interface index not found");  Ipv4Address stAddr ( u32Addr);  Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (stAddr, u32Mask);  ipv4->AddAddress (interface, ipv4Addr);  ipv4->SetMetric (interface, 1);  ipv4->SetUp (interface);  //retval.Add (ipv4, interface);  return interface;}

使用方法如下,

      Ipv4Address stAddr = Ipv4Address("10.0.0.1");      uint32_t u32Addr = stAddr.Get();        Ipv4Mask stMask = Ipv4Mask("255.255.255.0");      uint32_t u32Mask = stMask.Get();      for(int j = 0; j< nNodes; j++)      {          AssignAddress(devices.Get(j), u32Addr+j, u32Mask);      }


0 0
原创粉丝点击