Have some issue understanding PacketSocket

68 views
Skip to first unread message

宋刚

unread,
Jun 12, 2018, 4:43:17 AM6/12/18
to ns-3-users
Hi, there , I am trying to do a routing protocol simulation using ns3, but find out that I have some difficulty in understanding the usage of PacketSocket. I found this code snippet somewhere
    PacketSocketAddress socket;
    socket
.SetAllDevices ();
    socket
.SetPhysicalAddress (gwdev.Get (0)->GetBroadcast());
    socket
.SetProtocol(0);

    TypeId psfid = TypeId::LookupByName ("ns3::PacketSocketFactory");

    for (uint32_t i = 0; i < m_numNodes*layers; i++)
    {
        Ptr<Socket> nodeSocket = Socket::CreateSocket (node.Get (i), psfid);

        nodeSocket->SetAllowBroadcast(true);
        nodeSocket->Bind ();
        nodeSocket->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
        nodeSocket->Connect (socket);


        Ptr<Socket> gwSocket = Socket::CreateSocket (gw.Get (0), psfid);
        gwSocket->SetAllowBroadcast(true);
        gwSocket->Bind();
        gwSocket->SetRecvCallback (MakeCallback (&Experiment::GWReceivePacket, this));
    }
where the node is a NodeContainer for underwater node and gw is a NodeContainer for sink node.
I can't understand what exactly the PacketSocketAddress does and how underwater nodes connect to each other, also, I can't find any useful document. Can anyone explain this code snippet to me? Thank you in advance.

jared...@gmail.com

unread,
Jun 13, 2018, 12:59:53 PM6/13/18
to ns-3-users
I am not a core dev or network expert, but I have found this Sockets API documentation.  There's also the PacketSocket class docs.  Also the PacketSocketFactory class docs.  From the former:

A PacketSocket can be used to connect an application to a net device. The application provides the buffers of data, the socket converts them to a raw packet and the net device then adds the protocol specific headers and trailers. This socket type is very similar to the linux and BSD "packet" sockets.

If you're more familiar with Linux or BSD sockets then this might make sense to you.  I hope those links can get you started on your investigation.

Jared.

宋刚

unread,
Jun 19, 2018, 7:54:32 AM6/19/18
to ns-3-users
Hello, Jared.
Thanks for you reply, I read the documents you mentioned but found out that I am still confused. It seems that nothing about PacketSocketAddress was mentioned in the documents, and I still don't know what role does it play in the connection, is it something like an IP address? If it is the case, why there isn't any code that assign PacketSocketAddress to every node?
我没有看到有一些PacketSocketAddress分配给每个节点的代码。

jared...@gmail.com

unread,
Jun 20, 2018, 9:09:52 PM6/20/18
to ns-3-users
I would point you to PacketSocketHelper, which adds a PacketSocketFactory to a node.  When you add a application that uses a PacketSocket, e.g. PacketSocketServer, it creates a new socket using the PacketSocketFactory that the helper previously aggregated to the node.  The application has an address associated with it that it uses to configure the PacketSocket.  See PacketSocketServer::StartApplicationPacketSocketFactory::CreateSocket, SocketFactory, and PacketSocket::Bind.  In PacketSocketAppsTest::DoRun they set the address to the device address which I guess is more like a MAC address.  There are other kinds of sockets (see the inheritance diagram for the base Socket) and other applications that likely use those sockets.

Keep investigating, I think I prob. just spent a little more time with the code.  I don't really understand how this maps to how real network stacks work.  I wish someone with domain expertise would chime in with a little background.

Jared.
Reply all
Reply to author
Forward
0 new messages