使用NS3通过UdpL4Protocol发送数据包

时间:2014-05-15 12:55:20

标签: c++ segmentation-fault simulation ns-3

我正在尝试在ns3(C ++)中运行模拟,其中节点使用方法"发送"(包含在ns3库中)将数据包发送到另一个节点。 程序构建正常,但运行时出现分段错误。 我检查了参数,我没有用指针做任何花哨的东西。

似乎错误来自方法"发送"但是我无法弄清楚到底是什么问题,我尝试显示IP地址并且它们似乎没问题,我尝试使用默认构造函数创建带有参数的数据包。我也尝试使用IPv6地址,但它也没有用。

请帮助,我没有想法

这是我的代码:

#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.));



}

以及我从调试器获得的消息:

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

1 个答案:

答案 0 :(得分:0)