V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
heyenyan
V2EX  ›  Linux

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

  •  
  •   heyenyan · 2022-08-25 22:48:56 +08:00 · 2072 次点击
    这是一个创建于 820 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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);
            }
        }
    
    4 条回复    2022-08-30 12:40:27 +08:00
    HarveyLiu
        1
    HarveyLiu  
       2022-08-26 15:37:11 +08:00
    是不是临时地址,不是运营商分配过期时间说了算么
    heyenyan
        2
    heyenyan  
    OP
       2022-08-27 00:45:19 +08:00
    @HarveyLiu `ip a`都能标出来 temporary dynamic,我想筛选出来这一个地址。。。但是就是找不到确认的方法
    qakito
        3
    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
        4
    heyenyan  
    OP
       2022-08-30 12:40:27 +08:00 via iPhone
    @qakito 感谢感谢ヾ(≧∇≦谢谢≧∇≦)ノ
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2692 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 12:35 · PVG 20:35 · LAX 04:35 · JFK 07:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.