求问: Linux 怎么检测从接口中获取的 ipv6 地址是不是临时地址

2022-08-25 22:48:56 +08:00
 heyenyan
struct ifaddrs *ifaddr, *ifa;
    int family, s;
    char *host = NULL;

    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {

        family = ifa->ifa_addr->sa_family;
       
        if (family == AF_INET6) {
            if ((host = malloc(NI_MAXHOST)) == NULL)
                return NULL;
            s = getnameinfo(ifa->ifa_addr,
                            sizeof(struct sockaddr_in6),
                            host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
            if (s != 0) {
                return NULL;
            }
            printf("%s %d %s \n\n",ifa->ifa_name,ifaddr->ifa_flags,host);
        }
    }
1881 次点击
所在节点    Linux
4 条回复
HarveyLiu
2022-08-26 15:37:11 +08:00
是不是临时地址,不是运营商分配过期时间说了算么
heyenyan
2022-08-27 00:45:19 +08:00
@HarveyLiu `ip a`都能标出来 temporary dynamic,我想筛选出来这一个地址。。。但是就是找不到确认的方法
qakito
2022-08-30 10:18:32 +08:00
可以通过 netlink(ip 命令就是通过 netlink)

RTM_NEWADDR, RTM_DELADDR, RTM_GETADDR
Add, remove or receive information about an IP address associ‐
ated with an interface. In Linux 2.2, an interface can carry
multiple IP addresses, this replaces the alias device concept in
2.0. In Linux 2.2, these messages support IPv4 and IPv6
addresses. They contain an ifaddrmsg structure, optionally fol‐
lowed by rtattr routing attributes.

struct ifaddrmsg {
unsigned char ifa_family; /* Address type */
unsigned char ifa_prefixlen; /* Prefixlength of address */
unsigned char ifa_flags; /* Address flags */
unsigned char ifa_scope; /* Address scope */
int ifa_index; /* Interface index */
};

ifa_family is the address family type (currently AF_INET or
AF_INET6), ifa_prefixlen is the length of the address mask of
the address if defined for the family (like for IPv4), ifa_scope
is the address scope, ifa_index is the interface index of the
interface the address is associated with. ifa_flags is a flag
word of IFA_F_SECONDARY for secondary address (old alias inter‐
face), IFA_F_PERMANENT for a permanent address set by the user
and other undocumented flags.

判断 ifa_flags & IFA_F_SECONDARY

本地测试
ip -6 addr show
2: ens2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 2::cd5b:65df:8ebd:cee8/64 scope global temporary dynamic
valid_lft 604458sec preferred_lft 85521sec
inet6 2::5054:ff:fe09:9265/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 2591821sec preferred_lft 604621sec
inet6 fe80::4d7d:7c1f:9991:fddd/64 scope link stable-privacy
valid_lft forever preferred_lft forever

./getaddr
new addr
ifa flags 1
addr 2::cd5b:65df:8ebd:cee8 <== 临时地址
new addr
ifa flags 0
addr 2::5054:ff:fe09:9265
new addr
ifa flags 80
addr fe80::4d7d:7c1f:9991:fddd
heyenyan
2022-08-30 12:40:27 +08:00
@qakito 感谢感谢ヾ(≧∇≦谢谢≧∇≦)ノ

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/875457

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX