@Hassan's code is correct, but it works only for packets sent by a node, not for packets received by another node.
Moreover, mind the following pitfalls:
- ipv4->GetAddress (1,0); will fetch the 1st address for the 2nd interface (the 1st one is the loopback, whose address is 127.0.0.1). Mind that a node can have multiple interfaces and each interface can have multiple addresses.
- There's also IPv6 (but the code is similar).
A more robust solution would iterate through all the addresses on all the interfaces:
for (auto ifIndex = 0; ifIndex < ipv4->GetNInterfaces(); ifIndex++)
{
for (auto addrIndex = 0; addrIndex < ipv4->GetNAddresses(ifIndex); addrIndex++)
{
Ipv4InterfaceAddress iaddr = ipv4->GetAddress (ifIndex, addrIndex);
Ipv4Address source = iaddr.GetAddress ();
}
}
BTW, please use "GetAddress" instead of "GetLocal". Sooner or later I'll deprecate GetLocal.