getifaddrs and freeifaddrs

5,925 views
Skip to first unread message

Akshay Gattani

unread,
Jul 20, 2010, 9:06:14 PM7/20/10
to android-ndk
I was surprised to find that "ifaddr.h" is missing from the standard
NDK headers (STABLE APIS) when it is a BSD staple. These APIs are
vital for any application that has network interface management
functionality

The closest I came to finding a solution was to include the "ifaddrs-
android.h" header which implements "get/freeifaddrs" in the header
itself.
This is part of the android open source project.
http://android.git.kernel.org/?p=platform/libcore.git;a=blob;f=luni/src/main/native/ifaddrs-android.h;h=eb4b69a16fc0a36583e653e1c59ca06318316aac;hb=HEAD
The "struct ifaddrs" is missing the ifa_data field and hence this
header is not entirely useful to me either.

Has anyone encountered this problem during development and is there a
solution to replicate the get/freeifaddrs functionality in native code
on Android. All I am interested in is reading a list of current
network interfaces (addresses) and any address-family specific data
(MTU, etc), which is ideally part of the "ifa_data" field within
ifaddr.h. (There is no network configuration involved).

Thanks and looking forward to any useful suggestions.

Cheers,
Akshay

Angus Lees

unread,
Jul 20, 2010, 9:57:37 PM7/20/10
to andro...@googlegroups.com
The two usual ways of doing this on any Linux kernel is via
SIOCGIFCONF ioctl calls or netlink. Netlink is a little harder to
find documentation on, but is preferred for new code since SIOCGIFCONF
can't return IPv6 addresses (and Android is IPv6-capable).
I believe both should work on Android without any special permissions
- getifaddrs() looks like it is a tiny wrapper around
ioctl(SIOCGIFCONF) so should be trivial to reimplement if you want to
go that route.

See your nearest netdevice(7) and rtnetlink(3) Linux manpages.

 - Gus

> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.
>
>

Akshay Gattani

unread,
Jul 21, 2010, 2:15:18 PM7/21/10
to android-ndk
Thanks Angus for the pointers. I will probably go the Netlink way as
you have suggested.

On Jul 20, 6:57 pm, Angus Lees <al...@google.com> wrote:
> The two usual ways of doing this on any Linux kernel is via
> SIOCGIFCONF ioctl calls or netlink.  Netlink is a little harder to
> find documentation on, but is preferred for new code since SIOCGIFCONF
> can't return IPv6 addresses (and Android is IPv6-capable).
> I believe both should work on Android without any special permissions
> - getifaddrs() looks like it is a tiny wrapper around
> ioctl(SIOCGIFCONF) so should be trivial to reimplement if you want to
> go that route.
>
> See your nearest netdevice(7) and rtnetlink(3) Linux manpages.
>
>  - Gus
>
> On Wed, Jul 21, 2010 at 11:06, Akshay Gattani
>
>
>
> <akshay.gattani...@gmail.com> wrote:
> > I was surprised to find that "ifaddr.h" is missing from the standard
> > NDK headers (STABLE APIS) when it is a BSD staple. These APIs are
> > vital for any application that has network interface management
> > functionality
>
> > The closest I came to finding a solution was to include the "ifaddrs-
> > android.h" header which implements "get/freeifaddrs" in the header
> > itself.
> > This is part of the android open source project.
> >http://android.git.kernel.org/?p=platform/libcore.git;a=blob;f=luni/s...

Sri

unread,
Aug 17, 2010, 5:40:57 AM8/17/10
to android-ndk
Faced the same issue with IPv6. ioctl didnt work for retrieving the
information. retrieved the IPv6 related information from /proc/net/
if_inet6

has anyone tried setting an IPv6 to an interface using ioctls?


On Jul 21, 11:15 pm, Akshay Gattani <akshay.gattani...@gmail.com>
wrote:
> Thanks Angus for the pointers. I will probably go the Netlink way as
> you have suggested.
>
> On Jul 20, 6:57 pm, Angus Lees <al...@google.com> wrote:
>
> > The two usual ways of doing this on any Linux kernel is via
> > SIOCGIFCONFioctlcalls or netlink.  Netlink is a little harder to

Angus Lees

unread,
Aug 24, 2010, 11:43:47 PM8/24/10
to andro...@googlegroups.com
On Tue, Aug 17, 2010 at 19:40, Sri <pat.s...@gmail.com> wrote:
> Faced the same issue with IPv6. ioctl didnt work for retrieving the
> information. retrieved the IPv6 related information from /proc/net/
> if_inet6
>
> has anyone tried setting an IPv6 to an interface using ioctls?

"Netlink is a little harder to find documentation on, but is preferred


for new code since SIOCGIFCONF can't return IPv6 addresses (and
Android is IPv6-capable)."

Likewise, SIOCSIFCONF is IPv4-only - you should use netlink to get and
set IPv6 config. I suspect Android will require you to have elevated
privileges to modify interface configuration (usual Linux requires
root or CAP_NET_ADMIN).

- Gus

> On Jul 21, 11:15 pm, Akshay Gattani <akshay.gattani...@gmail.com>
> wrote:
>> Thanks Angus for the pointers. I will probably go the Netlink way as
>> you have suggested.
>>
>> On Jul 20, 6:57 pm, Angus Lees <al...@google.com> wrote:
>>
>> > The two usual ways of doing this on any Linux kernel is via
>> > SIOCGIFCONFioctlcalls or netlink.  Netlink is a little harder to
>> > find documentation on, but is preferred for new code since SIOCGIFCONF
>> > can't returnIPv6addresses (and Android isIPv6-capable).
>> > I believe both should work on Android without any special permissions
>> > - getifaddrs() looks like it is a tiny wrapper around
>> >ioctl(SIOCGIFCONF) so should be trivial to reimplement if you want to
>> > go that route.
>>
>> > See your nearest netdevice(7) and rtnetlink(3) Linux manpages.
>>
>> >
>>

Reply all
Reply to author
Forward
0 new messages