Skip to content

Instantly share code, notes, and snippets.

@SzymonSzott
Created February 21, 2019 11:39
Show Gist options
  • Select an option

  • Save SzymonSzott/de5c431d687f7b3a0b10743af6ac7ce2 to your computer and use it in GitHub Desktop.

Select an option

Save SzymonSzott/de5c431d687f7b3a0b10743af6ac7ce2 to your computer and use it in GitHub Desktop.
Populate ARP cache (for ns-3 Wi-Fi simulations)
// Function to populate ARP cache of all nodes in ns-3, prevents ARP blocking in Wi-Fi networks
// Execute after assigning IP addresses
// Author: Pavel Boyko?
// https://groups.google.com/d/msg/ns-3-users/L_ZfZ9L1_b0/dCuzc859ENcJ
void PopulateARPcache () {
Ptr<ArpCache> arp = CreateObject<ArpCache> ();
arp->SetAliveTimeout (Seconds (3600 * 24 * 365) );
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
{
Ptr<Ipv4L3Protocol> ip = (*i)->GetObject<Ipv4L3Protocol> ();
NS_ASSERT (ip !=0);
ObjectVectorValue interfaces;
ip->GetAttribute ("InterfaceList", interfaces);
for (ObjectVectorValue::Iterator j = interfaces.Begin (); j != interfaces.End (); j++)
{
Ptr<Ipv4Interface> ipIface = (*j).second->GetObject<Ipv4Interface> ();
NS_ASSERT (ipIface != 0);
Ptr<NetDevice> device = ipIface->GetDevice ();
NS_ASSERT (device != 0);
Mac48Address addr = Mac48Address::ConvertFrom (device->GetAddress () );
for (uint32_t k = 0; k < ipIface->GetNAddresses (); k++)
{
Ipv4Address ipAddr = ipIface->GetAddress (k).GetLocal();
if (ipAddr == Ipv4Address::GetLoopback ())
continue;
ArpCache::Entry *entry = arp->Add (ipAddr);
Ipv4Header ipv4Hdr;
ipv4Hdr.SetDestination (ipAddr);
Ptr<Packet> p = Create<Packet> (100);
entry->MarkWaitReply (ArpCache::Ipv4PayloadHeaderPair (p, ipv4Hdr));
entry->MarkAlive (addr);
}
}
}
for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
{
Ptr<Ipv4L3Protocol> ip = (*i)->GetObject<Ipv4L3Protocol> ();
NS_ASSERT (ip !=0);
ObjectVectorValue interfaces;
ip->GetAttribute ("InterfaceList", interfaces);
for (ObjectVectorValue::Iterator j = interfaces.Begin (); j != interfaces.End (); j ++)
{
Ptr<Ipv4Interface> ipIface = (*j).second->GetObject<Ipv4Interface> ();
ipIface->SetAttribute ("ArpCache", PointerValue (arp) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment