From c8eb33c2c25521a8c915b234fec3706369063da3 Mon Sep 17 00:00:00 2001 From: Tim Kourt Date: Fri, 27 Sep 2019 12:52:22 -0700 Subject: [PATCH] rtnlutil: Add parser for IPv6 RTNL packet At this time, we are only looking for IFA_ADDRESS attribute that represents the IPv6 IP address. --- src/rtnlutil.c | 30 ++++++++++++++++++++++++++++++ src/rtnlutil.h | 3 +++ 2 files changed, 33 insertions(+) diff --git a/src/rtnlutil.c b/src/rtnlutil.c index d20b3284..b1d6450e 100644 --- a/src/rtnlutil.c +++ b/src/rtnlutil.c @@ -418,3 +418,33 @@ uint32_t rtnl_route_ipv4_add_gateway(struct l_netlink *rtnl, int ifindex, gateway, src, priority_offset, proto, cb, user_data, destroy); } + +void rtnl_ifaddr_ipv6_extract(const struct ifaddrmsg *ifa, int len, char **ip) +{ + struct in6_addr in6_addr; + struct rtattr *attr; + char address[128]; + + for (attr = IFA_RTA(ifa); RTA_OK(attr, len); + attr = RTA_NEXT(attr, len)) { + switch (attr->rta_type) { + case IFA_ADDRESS: + if (!ip) + break; + + memcpy(&in6_addr.s6_addr, RTA_DATA(attr), + sizeof(in6_addr.s6_addr)); + + if (!inet_ntop(AF_INET6, &in6_addr, address, + INET6_ADDRSTRLEN)) { + + l_error("rtnl: Failed to extract IPv6 address"); + break; + } + + *ip = l_strdup(address); + + break; + } + } +} diff --git a/src/rtnlutil.h b/src/rtnlutil.h index b159d959..8c0a923d 100644 --- a/src/rtnlutil.h +++ b/src/rtnlutil.h @@ -67,3 +67,6 @@ uint32_t rtnl_route_ipv4_add_gateway(struct l_netlink *rtnl, int ifindex, l_netlink_command_func_t cb, void *user_data, l_netlink_destroy_func_t destroy); + +void rtnl_ifaddr_ipv6_extract(const struct ifaddrmsg *ifa, int bytes, + char **ip);