ns3不使用Ipv4AddressHelper的替代方法

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



int32_t AssignAddress (Ptr device, uint32_t u32Addr, uint32_t u32Mask )
{
  NS_LOG_FUNCTION_NOARGS ();
  //Ipv4InterfaceContainer retval;


  Ptr node = device->GetNode ();
  NS_ASSERT_MSG (node, "Ipv4AddressHelper::Assign(): NetDevice is not not associated "
                 "with any node -> fail");

  Ptr ipv4 = node->GetObject ();
  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);
      }


你可能感兴趣的:(ns3)