I am trying to run a simulation in ns3(C++) in which a node sends a packet to another node with the method "Send"(included in the ns3 library). The program builds properly but I get a segmentation fault when I run it. I checked the parameters, I am not doing anything fancy with pointers.
It seems the error comes from the method "Send" but I can't figure out what exactly is the problem, I tried displaying the IP addresses and they seem to be alright, I tried to create the packet with the default constructor, with parameters. I also tried to use IPv6 addresses but it did not work neither.
Please help, I'm out of ideas
Here is my code :
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/wifi-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/mobility-module.h"
#include <vector>
#include <string.h>
using namespace ns3;
void velocityHandler(Ptr<ConstantVelocityMobilityModel> vel, Vector3D vec) {
vel->SetVelocity(vec);
}
void sendHandler(UdpL4Protocol* udp,NodeContainer nodes, Ptr<Packet> paquet) {
Ptr<Ipv4> ipv4 = nodes.Get(1)->GetObject<Ipv4>();
Ptr<Ipv4> ipv42 = nodes.Get(0)->GetObject<Ipv4>();
Ipv4Address adr = ipv4->GetAddress(1,0).GetLocal();
Ipv4Address adr2 = ipv42->GetAddress(1,0).GetLocal();
udp->Send(paquet, adr,adr2,(uint16_t)50 ,(uint16_t)60/*,Ptr<Ipv4Route>(route)*/);
}
int main() {
Time::SetResolution (Time::MS);
//Creation des Noeuds
NodeContainer nodes1, nodes2;
nodes1.Create(1);
nodes2.Create(2);
//Positionnement des noeuds
MobilityHelper mobility1, mobility2;
mobility1.SetPositionAllocator("ns3::GridPositionAllocator","MinX",
DoubleValue(0.),"MinY",DoubleValue(6.));
mobility2.SetPositionAllocator("ns3::GridPositionAllocator","MinX",
DoubleValue(2.),"MinY",DoubleValue(0.),
"DeltaX",DoubleValue(2.),"DeltaY",
DoubleValue(4.),"GridWidth",UintegerValue(1),
"LayoutType",StringValue("RowFirst"));
//Deplacements des noeuds
mobility1.SetMobilityModel("ns3::ConstantVelocityMobilityModel");
mobility1.Install(nodes1);
Ptr<ConstantVelocityMobilityModel> velmodel = nodes1.Get(0)->GetObject<ConstantVelocityMobilityModel>();
(*velmodel).SetVelocity(Vector3D(0.,0.,0.));
mobility2.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility2.Install(nodes2);
//liens
PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate", StringValue ("2Mbps"));
p2p.SetChannelAttribute("Delay", StringValue ("2ms"));
//Interfaces
NetDeviceContainer devices;
devices = p2p.Install(nodes2);
//Pile protocolaire
InternetStackHelper stack;
stack.Install(nodes2);
//Adressage
Ipv4AddressHelper address;
address.SetBase("30.0.0.0","255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
//UDP
UdpL4Protocol* udp = new UdpL4Protocol();
udp->SetNode(nodes2.Get(0));
udp->SetNode(nodes2.Get(1));
//lancement de la simulation
Packet a = Packet();
AnimationInterface anim ("animudp.xml");
Simulator::Schedule(Seconds(1.),&velocityHandler, velmodel, Vector3D(5.,0.,0.));
Simulator::Schedule(Seconds(0.2),&sendHandler,udp, nodes2, Ptr<Packet>(&a));
Simulator::Run();
Simulator::Stop(Seconds(10.));
}
and the message i get from the debugger :
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff5919900 in ns3::Callback<void, ns3::Ptr<ns3::Packet>, ns3::Ipv4Address, ns3::Ipv4Address, unsigned char, ns3::Ptr<ns3::Ipv4Route>, ns3::empty, ns3::empty, ns3::empty, ns3::empty>::operator() (this=0x66aa40, a1=..., a2=..., a3=..., a4=17 '\021', a5=...)
at ./ns3/callback.h:1077
1077 return (*(DoPeekImpl ()))(a1,a2,a3,a4,a5);