From f3f343c04bc8bfef533da315b19301a2454b8e1a Mon Sep 17 00:00:00 2001 From: Tim Kourt Date: Tue, 25 Jun 2019 15:20:53 -0700 Subject: [PATCH] rtnlutil: Add parser for ifaddrmsg struct Its purpose is to extract interface label, ip, and broadcast addresses out of ifaddrmsg rntl message. --- src/rtnlutil.c | 36 ++++++++++++++++++++++++++++++++++++ src/rtnlutil.h | 3 +++ 2 files changed, 39 insertions(+) diff --git a/src/rtnlutil.c b/src/rtnlutil.c index 57196ff8..963faed3 100644 --- a/src/rtnlutil.c +++ b/src/rtnlutil.c @@ -26,6 +26,7 @@ #include #include +#include #include @@ -74,3 +75,38 @@ uint32_t rtnl_set_linkmode_and_operstate(struct l_netlink *rtnl, int ifindex, return id; } + +void rtnl_ifaddr_extract(const struct ifaddrmsg *ifa, int bytes, + char **label, char **ip, char **broadcast) +{ + struct in_addr in_addr; + struct rtattr *attr; + + for (attr = IFA_RTA(ifa); RTA_OK(attr, bytes); + attr = RTA_NEXT(attr, bytes)) { + switch (attr->rta_type) { + case IFA_LOCAL: + if (!ip) + break; + + in_addr = *((struct in_addr *) RTA_DATA(attr)); + *ip = l_strdup(inet_ntoa(in_addr)); + + break; + case IFA_BROADCAST: + if (!broadcast) + break; + + in_addr = *((struct in_addr *) RTA_DATA(attr)); + *broadcast = l_strdup(inet_ntoa(in_addr)); + + break; + case IFA_LABEL: + if (!label) + break; + + *label = l_strdup(RTA_DATA(attr)); + break; + } + } +} diff --git a/src/rtnlutil.h b/src/rtnlutil.h index 0caa7179..030a6baf 100644 --- a/src/rtnlutil.h +++ b/src/rtnlutil.h @@ -25,3 +25,6 @@ uint32_t rtnl_set_linkmode_and_operstate(struct l_netlink *rtnl, int ifindex, l_netlink_command_func_t cb, void *user_data, l_netlink_destroy_func_t destroy); + +void rtnl_ifaddr_extract(const struct ifaddrmsg *ifa, int bytes, + char **label, char **ip, char **broadcast);