Convert Address to InetSocketAddress

607 views
Skip to first unread message

patriciaruiz

unread,
Aug 26, 2009, 1:40:56 PM8/26/09
to ns-3-users
Hello,

I am trying to get the ipv4 address of a device, but the only command
I found to get the address is GetAddress() from a netDevice. The
problem is that it returns Address. So, I tried to convert it to
InetSocketAddress and after that to ipv4. This is the part of the code
where I am doing that:

Ptr <NetDevice> aux2 = dev->GetDevice(dev->GetId());
Address add = aux2->GetAddress();

InetSocketAddress inetSourceAddr = InetSocketAddress::ConvertFrom
(add);

The problem is that I got an error:

assert failed. file=../src/node/inet-socket-address.cc, line=91,
cond="address.CheckCompatible (GetType (), 6)"


I don't know if there is any easier way to get the own address of a
device, or how to solve this problem. Can anyone help, please?

Tom Henderson

unread,
Aug 27, 2009, 12:30:07 AM8/27/09
to ns-3-...@googlegroups.com
The error is probably because you are trying to convert a device address
(such as a Mac address) into an InetSocketAddress, and the conversion fails.

What you seem to want is an Ipv4InterfaceAddress (or when using Ipv6, an
Ipv6InterfaceAddress). You need to look at class Ipv4 and methods
GetNAddresses() and GetAddress(). Note that each interface may have
more than one address (especially IPv6) but typically will have one
address for IPv4.


>
>
> >
>

patriciaruiz

unread,
Aug 27, 2009, 9:25:16 AM8/27/09
to ns-3-users
Thanks a lot Tom, I didn't know the address got from a netDevice was
the mac address.....
I will just show how I did it, just in case someone wants to do the
same.
I was trying to get the Ipv4 address from a device. The code is the
following:

Ptr<Ipv4> ipv4 = GetNode()->GetObject<Ipv4>();
Ipv4InterfaceAddress iaddr = ipv4->GetAddress (1,0);
Ipv4Address addri = iaddr.GetLocal ();

Variable addri has the Ipv4 value you set when initializing the
system.
Reply all
Reply to author
Forward
0 new messages