求问: Linux 怎么检测从接口中获取的 ipv6 地址是不是临时地址 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
heyenyan
V2EX    Linux

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

  •  
  •   heyenyan 2022-08-25 22:48:56 +08:00 2654 次点击
    这是一个创建于 1192 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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     Solana     867 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 23ms UTC 22:39 PVG 06:39 LAX 14:39 JFK 17:39
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86