Ipv4AddressHelper向网卡分配地址的方法

物理IP的分配和使用方法


  NetDeviceContainer devices;
  devices = csma.Install (nodes);
 
  //
  //
  // Address Configuration
  //
  //

      Ipv4AddressHelper ipv4AddrHelper;

      ipv4AddrHelper.SetBase ("10.0.0.0", "255.255.255.0");
      Ipv4InterfaceContainer interfaces = ipv4AddrHelper.Assign (devices);

研究Assign的函数如下,

Ipv4InterfaceContainer
Ipv4AddressHelper::Assign (const NetDeviceContainer &c)
{
  NS_LOG_FUNCTION_NOARGS ();
  Ipv4InterfaceContainer retval;
  for (uint32_t i = 0; i < c.GetN (); ++i) {
      Ptr device = c.Get (i);

      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");

      Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (NewAddress (), m_mask);
      ipv4->AddAddress (interface, ipv4Addr);
      ipv4->SetMetric (interface, 1);
      ipv4->SetUp (interface);
      retval.Add (ipv4, interface);
    }
  return retval;
}

每次都是NewAddress(), 然后分配到网卡上。所以这个Helper是很自动的。

如果不用Helper,就要老实的每个去分配IP。




你可能感兴趣的:(ns3)