Ipv4AddressHelper向网卡分配地址的方法

来源:互联网 发布:linux怎么拷贝文件夹 编辑:程序博客网 时间:2024/05/22 03:00

物理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的函数如下,

Ipv4InterfaceContainerIpv4AddressHelper::Assign (const NetDeviceContainer &c){  NS_LOG_FUNCTION_NOARGS ();  Ipv4InterfaceContainer retval;  for (uint32_t i = 0; i < c.GetN (); ++i) {      Ptr<NetDevice> device = c.Get (i);      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");      Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (NewAddress (), m_mask);      <span style="color:#FF6666;">ipv4->AddAddress (interface, ipv4Addr);</span>      ipv4->SetMetric (interface, 1);      ipv4->SetUp (interface);      retval.Add (ipv4, interface);    }  return retval;}

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

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




0 0
原创粉丝点击