From: Andrew Zaborowski <andrew.zaborowski@intel.com>
To: ell@lists.01.org
Subject: [PATCH] rtnl: Add l_rtnl_ifaddr_extract
Date: Wed, 19 May 2021 00:20:35 +0200 [thread overview]
Message-ID: <20210518222035.836352-1-andrew.zaborowski@intel.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 2698 bytes --]
Extracts information from struct ifaddrmsg directly into an
l_rtnl_address object.
---
ell/ell.sym | 1 +
ell/rtnl.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
ell/rtnl.h | 2 ++
3 files changed, 56 insertions(+)
diff --git a/ell/ell.sym b/ell/ell.sym
index 5568ac2..ec44d3a 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -641,6 +641,7 @@ global:
l_rtnl_route6_delete_gateway;
l_rtnl_route_add;
l_rtnl_route_delete;
+ l_rtnl_ifaddr_extract;
l_rtnl_ifaddr_add;
l_rtnl_ifaddr_delete;
/* icmp6 */
diff --git a/ell/rtnl.c b/ell/rtnl.c
index c5b479b..e2996a6 100644
--- a/ell/rtnl.c
+++ b/ell/rtnl.c
@@ -1126,6 +1126,59 @@ done:
return id;
}
+LIB_EXPORT struct l_rtnl_address *l_rtnl_ifaddr_extract(
+ const struct ifaddrmsg *ifa,
+ int bytes)
+{
+ struct rtattr *attr;
+ struct ifa_cacheinfo *cinfo;
+ struct l_rtnl_address *addr;
+
+ if (unlikely(!ifa))
+ return NULL;
+
+ if (!L_IN_SET(ifa->ifa_family, AF_INET, AF_INET6))
+ return NULL;
+
+ addr = l_new(struct l_rtnl_address, 1);
+ addr->prefix_len = ifa->ifa_prefixlen;
+ addr->family = ifa->ifa_family;
+ addr->flags = ifa->ifa_flags;
+ addr->scope = ifa->ifa_scope;
+
+ for (attr = IFA_RTA(ifa); RTA_OK(attr, bytes);
+ attr = RTA_NEXT(attr, bytes)) {
+ switch (attr->rta_type) {
+ case IFA_LOCAL:
+ if (ifa->ifa_family == AF_INET)
+ addr->in_addr =
+ *((struct in_addr *) RTA_DATA(attr));
+
+ break;
+ case IFA_ADDRESS:
+ if (ifa->ifa_family == AF_INET6)
+ addr->in6_addr =
+ *((struct in6_addr *) RTA_DATA(attr));
+
+ break;
+ case IFA_BROADCAST:
+ addr->broadcast = *((struct in_addr *) RTA_DATA(attr));
+ break;
+ case IFA_LABEL:
+ l_strlcpy(addr->label, RTA_DATA(attr),
+ sizeof(addr->label));
+ break;
+ case IFA_CACHEINFO:
+ cinfo = RTA_DATA(attr);
+ addr->preferred_lifetime = cinfo->ifa_prefered;
+ addr->valid_lifetime = cinfo->ifa_valid;
+ break;
+ }
+ }
+
+ return addr;
+}
+
LIB_EXPORT uint32_t l_rtnl_ifaddr_add(struct l_netlink *rtnl, int ifindex,
const struct l_rtnl_address *addr,
l_netlink_command_func_t cb,
diff --git a/ell/rtnl.h b/ell/rtnl.h
index c54a287..adabcfe 100644
--- a/ell/rtnl.h
+++ b/ell/rtnl.h
@@ -164,6 +164,8 @@ uint32_t l_rtnl_route6_delete_gateway(struct l_netlink *rtnl, int ifindex,
void *user_data,
l_netlink_destroy_func_t destroy);
+struct l_rtnl_address *l_rtnl_ifaddr_extract(const struct ifaddrmsg *ifa, int bytes);
+
uint32_t l_rtnl_ifaddr_add(struct l_netlink *rtnl, int ifindex,
const struct l_rtnl_address *addr,
l_netlink_command_func_t cb,
--
2.27.0
next reply other threads:[~2021-05-18 22:20 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-05-18 22:20 Andrew Zaborowski [this message]
2021-05-18 22:43 ` [PATCH] rtnl: Add l_rtnl_ifaddr_extract Denis Kenzior
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210518222035.836352-1-andrew.zaborowski@intel.com \
--to=andrew.zaborowski@intel.com \
--cc=ell@lists.01.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).