How to get self Interface address in receive packet method from socket?

73 views
Skip to first unread message

Hassam Mughal

unread,
Oct 25, 2019, 6:08:54 AM10/25/19
to ns-3-users
Greetings everyone!
I am trying to print the IPv4 Interface address of the interface that receives the packet. However, there is no method in the socket. My Code snippet is following;

static inline std::string
PrintReceivedPacket (Ptr<Socket> socket, Ptr<Packet> packet, Address senderAddress)
{
  std
::ostringstream oss;


  oss
<< Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId ();
 
 
if (InetSocketAddress::IsMatchingType (senderAddress))
   
{
     
InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress);
      oss
<< socket-> GetNode()->GetDevice(socket->GetNode ()->GetId ()) << " received one packet from " << addr.GetIpv4 ();
   
}
 
else
   
{
      oss
<< " received one packet!";
   
}
 
return oss.str ();
}
Any guidance or help in this regard will be highly appreciated.
Thanks

γιωργος γιωργος

unread,
Oct 25, 2019, 6:38:44 AM10/25/19
to ns-3-...@googlegroups.com
Hi,

Maybe you can try something like this:

Ptr<Node> node = socket->GetNode ();
Ptr<Ipv4> ipv4_ptr = node->GetObject<Ipv4> (); // Get Ipv4 instance of the node
Ipv4Address addr = ipv4_ptr->GetAddress (1, 0).GetLocal (); // Get Ipv4InterfaceAddress of 1st interface.
std::cout << "Node: " << node->GetId() << " Address: " << addr<<std::endl;


Best regards,
George

--
Posting to this group should follow these guidelines https://www.nsnam.org/wiki/Ns-3-users-guidelines-for-posting
---
You received this message because you are subscribed to the Google Groups "ns-3-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/e863f98d-05b3-4477-b9c8-9da1729c4770%40googlegroups.com.

Hassam Mughal

unread,
Oct 25, 2019, 6:50:50 AM10/25/19
to ns-3-users
Dear George,
Thanks for this solution. Let me try it and get back to you.
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-...@googlegroups.com.

Hassam Mughal

unread,
Oct 25, 2019, 6:59:26 AM10/25/19
to ns-3-users
I am encountering an issue, which is that as I have 3 interfaces, so I don't know on which Interface I have received this packet on, and If I do this for every interface, it will give me the result as follows:

output.png

So, what should be my strategy in such situation?


On Friday, October 25, 2019 at 7:38:44 PM UTC+9, George Katsouris wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-...@googlegroups.com.

Hassam Mughal

unread,
Oct 25, 2019, 7:03:01 AM10/25/19
to ns-3-users
Kindly find my code attached hereby. I have tried to have three interfaces with different port numbers for each interface.


On Friday, October 25, 2019 at 7:38:44 PM UTC+9, George Katsouris wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-...@googlegroups.com.
dsdv-manet.cc

γιωργος γιωργος

unread,
Oct 25, 2019, 7:27:26 AM10/25/19
to ns-3-...@googlegroups.com
Well, my first instinct is to say that the interface that receives the packet is that on the same subnet, in that case 10.1.3.1.

Have you tried checking on your trace file to see the traffic? You could try and run: tcpdump -nn -tt -r <your-trace>.pcap

Best regards, George

To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/76fde66d-e147-49e1-84e9-9d1dd66dc03e%40googlegroups.com.

Hassam Mughal

unread,
Oct 25, 2019, 9:05:03 AM10/25/19
to ns-3-users
The traffic for the Dsdv_Manet_2Nodes_10SimTime-0-1.pcap is as follows: Which shows that the packets are received at their respective interfaces such that for 10.1.1.2 --> 10.1.1.1 and 10.1.2.2 -->> 10.1.2.1

output.png

γιωργος γιωργος

unread,
Oct 25, 2019, 9:15:52 AM10/25/19
to ns-3-...@googlegroups.com
That was the desired outcome, right?

To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/0d92fbb0-b1ba-4e4c-8c74-670f0138f84a%40googlegroups.com.

Hassam Mughal

unread,
Oct 25, 2019, 9:20:12 AM10/25/19
to ns-3-users

routingtable.png

Yah. Thanks, So it is working fine. But when I install a routing protocol on it, it doesn't print routing table accordingly. And the table looks like this

γιωργος γιωργος

unread,
Oct 25, 2019, 9:32:36 AM10/25/19
to ns-3-...@googlegroups.com
I don't have any relevant experience with the routing protocol, so I can't be of help on that, sorry.

Best regards,
George

To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/c29377a3-9a34-4f05-bb55-333ff2205eb8%40googlegroups.com.

Hassam Mughal

unread,
Oct 25, 2019, 10:01:10 AM10/25/19
to ns-3-users
Ahan, it's alright. Thanks a ton. Do you have any experience with DCE ?

γιωργος γιωργος

unread,
Oct 25, 2019, 1:30:39 PM10/25/19
to ns-3-...@googlegroups.com
I can't say I do, sorry.

To unsubscribe from this group and stop receiving emails from it, send an email to ns-3-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ns-3-users/633e0924-0821-43d2-b279-3694c17d5a9d%40googlegroups.com.

Harunur Roshid

unread,
Nov 24, 2019, 2:01:28 AM11/24/19
to ns-3-users
please send your full ipv4 code of this works
Reply all
Reply to author
Forward
0 new messages