[PATCH 03/11] rtnl: Add l_rtnl_route_new_static

Andrew Zaborowski <andrew.zaborowski at intel.com>
Newsgroups dev.linux.lists.ell
Message-ID <[email protected]>
Add a constructor for prefix routes through a specific gateway/router.
---
Name follows suggestion in
https://lists.01.org/hyperkitty/list/ell(a)lists.01.org/message/ZPJRBKRROCGZFVLLFUCL6GYHHG2JQZLW/
---
 ell/ell.sym |  1 +
 ell/rtnl.c  | 31 +++++++++++++++++++++++++++++++
 ell/rtnl.h  |  2 ++
 3 files changed, 34 insertions(+)

diff --git a/ell/ell.sym b/ell/ell.sym
index ec995fc..3816e3f 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -639,6 +639,7 @@ global:
 	l_rtnl_address_set_scope;
 	l_rtnl_route_new_gateway;
 	l_rtnl_route_new_prefix;
+	l_rtnl_route_new_static;
 	l_rtnl_route_free;
 	l_rtnl_route_get_family;
 	l_rtnl_route_get_gateway;
diff --git a/ell/rtnl.c b/ell/rtnl.c
index afbff9d..dbaf97b 100644
--- a/ell/rtnl.c
+++ b/ell/rtnl.c
@@ -419,6 +419,37 @@ LIB_EXPORT struct l_rtnl_route *l_rtnl_route_new_prefix(const char *ip,
 	return rt;
 }
 
+LIB_EXPORT struct l_rtnl_route *l_rtnl_route_new_static(const char *gw,
+							const char *ip,
+							uint8_t prefix_len)
+{
+	struct in_addr gw_addr4;
+	struct in6_addr gw_addr6;
+	struct in_addr dst_addr4;
+	struct in6_addr dst_addr6;
+	int family;
+	struct l_rtnl_route *rt;
+
+	if ((family = address_get(gw, &gw_addr4, &gw_addr6)) < 0)
+		return NULL;
+
+	if (address_get(ip, &dst_addr4, &dst_addr6) != family)
+		return NULL;
+
+	if (prefix_len == 0 || prefix_len > (family == AF_INET ? 32 : 128))
+		return NULL;
+
+	rt = l_rtnl_route_new_gateway(gw);
+	rt->dst_prefix_len = prefix_len;
+
+	if (family == AF_INET6)
+		memcpy(&rt->dst.in6_addr, &dst_addr6, sizeof(dst_addr6));
+	else
+		memcpy(&rt->dst.in_addr, &dst_addr4, sizeof(dst_addr4));
+
+	return rt;
+}
+
 LIB_EXPORT void l_rtnl_route_free(struct l_rtnl_route *rt)
 {
 	l_free(rt);
diff --git a/ell/rtnl.h b/ell/rtnl.h
index bbd410e..55c4997 100644
--- a/ell/rtnl.h
+++ b/ell/rtnl.h
@@ -71,6 +71,8 @@ bool l_rtnl_address_set_scope(struct l_rtnl_address *addr, uint8_t scope);
 struct l_rtnl_route *l_rtnl_route_new_gateway(const char *gw);
 struct l_rtnl_route *l_rtnl_route_new_prefix(const char *ip,
 							uint8_t prefix_len);
+struct l_rtnl_route *l_rtnl_route_new_static(const char *gw, const char *ip,
+							uint8_t prefix_len);
 void l_rtnl_route_free(struct l_rtnl_route *rt);
 DEFINE_CLEANUP_FUNC(l_rtnl_route_free);
 uint8_t l_rtnl_route_get_family(const struct l_rtnl_route *rt);
-- 
2.32.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.