Liam Close
Liam Close

Reputation: 21

How to set the IP address and data of Nodes in NS3?

I have two main questions about ns-3.

  1. I have my topology fully built but I want to be able to set(change) the IP addresses of nodes that exists within a NodeContainer. Is this possible and how if so?

  2. I need to set the data a node is holding as I am simulating a peer to peer streaming service. I do not understand how to change the data a node holds or even see the data.

Sorry for the delay. Here is the snippet of code I use to try to set the IP address of a node. Where csmaTopLeftParentToTopLeft is a NodeContainer

node = csmaTopLeftParentToTopLeft.Get(2);

ipv4 = node->GetObject<Ipv4> (); // Get Ipv4 instance of the node

addr = ipv4->GetAddress (0, 0).GetLocal (); // Get Ipv4InterfaceAddress of xth interface.
IPAddress = Ip.c_str();
addr.Set(IPAddress);

This runs but if I check the value after with print statements then it shows the value was never actually changed.

Both these things do not seem like they should be this difficult to do but I have tried many ways and search the internet and could not find anything on this topic.

Upvotes: 0

Views: 2644

Answers (1)

Liam Close
Liam Close

Reputation: 21

I figured out how to set an IP Address of a node.

 Ptr<Node> node;
 Ptr<Ipv4> ipv4;
 Ipv4InterfaceAddress addr;
 Ipv4Address addressIp;
 const char * IPAddress;

 IPAddress = Ip.c_str();

 Ptr<NetDevice> device =  devices.Get(counter);

 node = device->GetNode();

 ipv4 = node->GetObject<Ipv4>(); // Get Ipv4 instance of the node

 int32_t interface = ipv4->GetInterfaceForDevice (device);
 if (interface == -1) {
   interface = ipv4->AddInterface (device);
 }

 Ipv4InterfaceAddress ipv4Addr = Ipv4InterfaceAddress (Ipv4Address(IPAddress), Ipv4Mask ("/16"));

 ipv4->AddAddress (interface, ipv4Addr);
 ipv4->SetMetric (interface, 1);
 ipv4->SetUp (interface);

Upvotes: 1

Related Questions