Segmentation Violation

106 views
Skip to first unread message

Muhammad Talha

unread,
Aug 1, 2024, 4:26:30 AM8/1/24
to ns-3-users
Hi, 
Im not that familiar with C programming or socket programming and have been stuck on the issue for the last 2 days.
Im on ns3.42 with python cppyy binding. (which is probably causing the issue)

Ive simplified the script to only be the error part. Its simply 2 sockets on a p2p connection sending a packet from 1 to the other. The receiver just prints that it received the packet.

Im getting a segmentation violation error and it fails on line 90 where is sends the packet. Could anyone guide me as to what the issue could be
DummyCode.py

Muhammad Talha

unread,
Aug 1, 2024, 6:42:17 AM8/1/24
to ns-3-users
I've tried to retry from the groud up multiple times but always end up at the same error when using "socket.Send". Do i have to make another cppyy.cppdef function to make sure it sends properly?

ns.cppyy.cppdef("""
namespace ns3
{
    void RecvCallback(Ptr<Socket> socket) {
        if (socket->Recv()) {
            std::cout << "Received one packet!" << std::endl;
        }
    }            
   
    Callback<void, Ptr<Socket>> MakeRecvCallback()
    {
        return MakeCallback(&RecvCallback);
    }
               
}
""")

ns.LogComponentEnable("UdpSocketImpl", ns.LOG_LEVEL_INFO)
ns.LogComponentEnable("Ipv4L3Protocol", ns.LOG_LEVEL_INFO)
ns.LogComponentEnable("ArpL3Protocol", ns.LOG_LEVEL_INFO)

packetsize = 64 #bytes
pktcount = 2
pktinterval = 0.25 #seconds

nodes = ns.NodeContainer()
nodes.Create(2)

p2p = ns.PointToPointHelper()
p2p.SetDeviceAttribute("DataRate", ns.StringValue("5Mbps"))
p2p.SetChannelAttribute("Delay", ns.StringValue("2ms"))
devices = p2p.Install(nodes)

internet = ns.InternetStackHelper()
internet.Install(nodes)

address = ns.Ipv4AddressHelper()
address.SetBase(ns.Ipv4Address("10.1.1.0"), ns.Ipv4Mask("255.255.255.0"))
interfaces = address.Assign(devices)

def RecPkt(socket):
    if socket.Recv():
        print ("Received one packet!")

def SndPkt(socket):
    if pktcount > 0:
        socket.Send(ns.Packet(1024))
        #ns.Simulator.Schedule(pktinterval, SndPkt, socket, packetsize, pktcount-1, pktinterval)
        print ("Sending one packet!")
    else:
        socket.Close()

appSource = nodes.Get(0)
appSink = nodes.Get(1)

remoteAddr = interfaces.GetAddress(1)

sink = ns.Socket.CreateSocket(appSink, ns.TypeId.LookupByName("ns3::UdpSocketFactory"))
sink.Bind(ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 80).ConvertTo())

# Use the C++ MakeRecvCallback function
MakeRecvCallback = ns.MakeRecvCallback
recv_callback = MakeRecvCallback()
sink.SetRecvCallback(recv_callback)

source = ns.Socket.CreateSocket(appSource, ns.TypeId.LookupByName("ns3::UdpSocketFactory"))
source.Connect(ns.InetSocketAddress(remoteAddr, 80).ConvertTo())

ns.Simulator.Schedule(ns.Seconds(30.0), SndPkt(source))

#source.Send(ns.Packet(1))

print ("Run Simulation.")
ns.Simulator.Stop(ns.Seconds(100))
ns.Simulator.Run()
ns.Simulator.Destroy()

Muhammad Talha

unread,
Aug 1, 2024, 6:42:55 AM8/1/24
to ns-3-users
Sorry for the poor english grammar.

Muhammad Talha

unread,
Aug 2, 2024, 3:39:10 AM8/2/24
to ns-3-users
Update:

I've attempted to implement the socket binding, connection, and callback part in C directly. However, I'm not very comfortable with C programming. It seems that the StartApplication function is not working because there's no output from NS_LOG_UNCOND("Sender App Start"). Could someone guide me on the issue with either the new code or the previous code? I'm concerned that I might be making a fundamental mistake or missing a concept, as I'm new to socket programming. Any help would be greatly appreciated.

Output:
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __cxx_global_var_initcling_module_139_, __orc_init_func.cling-module-139, _GLOBAL__sub_I_cling_module_139, _ZN3ns3L16g_timeInitHelperE, $.cling-module-139.__inits.0 }) }
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { __orc_init_func.cling-module-139 }) }
Creating nodes
Creating point-to-point link
Device 0 type: ns3::PointToPointNetDevice
Device 1 type: ns3::PointToPointNetDevice
Installing internet stack
Assigning IP addresses
Creating receiver sockets
0x56df135d8300
Creating sender socket
ns3::Socket
Running simulation
looking for ns3::SocketSetDontFragmentTag
looking for ns3::SocketIpTtlTag
found initial merge before tid
tid not found after first merge
looking for ns3::SocketIpTosTag
found initial merge before tid
tid not found after first merge
Sojourn time +0ms
looking for ns3::SocketPriorityTag
found initial merge before tid
tid not found after first merge
looking for ns3::SocketPriorityTag
found initial merge before tid
tid not found after first merge
 The socket was not bound to any group.
 The socket was not bound to any group.
Simulation finished

DummyCode (1).py
Reply all
Reply to author
Forward
0 new messages