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