使用ioctl在Linux中获取IPv6地址 [英] Get IPv6 addresses in linux using ioctl
本文介绍了使用ioctl在Linux中获取IPv6地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在Linux操作系统中获取IPv6地址,如下所示:
I trying to get IPv6 addresses in my linux OS like following:
sd = Socket_m(AF_INET6_m, SOCK_DGRAM_m, 0);
ifc.ifc_buf = buffer_p;
ifc.ifc_len = buffSize;
Ioctl_m(sd, SIOCGIFCONF, &ifc);
如果为接口配置了任何IPv4地址,该方法都可以成功运行,但是如果接口只有一个IPv6地址,则ioctl不会返回该地址.
It works succesfully if any IPv4 address are configured for interface, but if interface has only one IPv6 address it is not returned by ioctl.
例如,由于仅配置了IPv6地址,因此无法获取followith接口的IPv6地址:
For example, I unable to get IPv6 address of the followith interface because only IPv6 address is configured:
br1 Link encap:Ethernet HWaddr 00:10:18:2D:BB:34
inet6 addr: fe80::210:18ff:fe2d:be54/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:194244850 errors:0 dropped:0 overruns:0 frame:0
TX packets:72005 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:12331900995 (11760.6 Mb) TX bytes:6192406 (5.9 Mb)
推荐答案
是的,ioctl是旧版,不会返回IPv6.每个平台都有不同的获取IPv6的方式:
Yes, that ioctl is legacy and won't return IPv6. Each platform has a different way of getting the IPv6 ones:
- Linux,如果您发疯了,请使用NETLINK;如果您最近使用的glibc不太明确,请使用
getifaddrs
;否则请阅读/proc/net/if_inet6
(例如,在Android上). - Darwin或FreeBSD:使用
getifaddrs
. - Solaris,使用
SIOCGLIFCONF
. - 在AIX中,使用
SIOCGIFCONF
实际上返回IPv4和IPv6地址(因为它们在struct sockaddr
中具有一个sa_len
字段,因此它们实际上可以支持该地址).
- Linux, use NETLINK if you're crazy, use
getifaddrs
if you have a vaguely recent glibc, otherwise read/proc/net/if_inet6
(eg on Android). - Darwin or FreeBSD: use
getifaddrs
. - Solaris, use
SIOCGLIFCONF
. - AIX, use
SIOCGIFCONF
which actually returns IPv4 and IPv6 addresses (because they have ansa_len
field instruct sockaddr
they can actually support that).
这篇关于使用ioctl在Linux中获取IPv6地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文