All of lore.kernel.org
 help / color / mirror / Atom feed
From: Daniel Wagner <wagi@monom.org>
To: ell@lists.01.org
Subject: [PATCH 1/3] rtnl: Add l_rtnl_route6_dump() and l_rtnl_route6_extract()
Date: Fri, 14 Feb 2020 19:52:51 +0100	[thread overview]
Message-ID: <20200214185253.32658-2-wagi@monom.org> (raw)
In-Reply-To: <20200214185253.32658-1-wagi@monom.org>

[-- Attachment #1: Type: text/plain, Size: 4421 bytes --]

Add missing function for handling IPv6. Now we for both protocol
versions the same functionality.

While at it, factor out the l_rtnl_route4_extract to use inet_ntop
which can handle both address families.
---
 ell/rtnl.c | 104 +++++++++++++++++++++++++++++++++--------------------
 ell/rtnl.h |   6 ++++
 2 files changed, 72 insertions(+), 38 deletions(-)

diff --git a/ell/rtnl.c b/ell/rtnl.c
index ced9baab66b9..c94531144eb4 100644
--- a/ell/rtnl.c
+++ b/ell/rtnl.c
@@ -68,6 +68,50 @@ static size_t rta_add_data(void *rta_buf, unsigned short type, void *data,
 	return RTA_SPACE(data_len);
 }
 
+static void l_rtnl_route_extract(const struct rtmsg *rtmsg, uint32_t len,
+				int family, uint32_t *ifindex, char **dst,
+				char **gateway,	char **src)
+{
+	struct rtattr *attr;
+	char buf[INET6_ADDRSTRLEN];
+
+	for (attr = RTM_RTA(rtmsg); RTA_OK(attr, len);
+						attr = RTA_NEXT(attr, len)) {
+		switch (attr->rta_type) {
+		case RTA_DST:
+			if (!dst)
+				break;
+
+			inet_ntop(family, RTA_DATA(attr), buf, sizeof(buf));
+			*dst = l_strdup(buf);
+
+			break;
+		case RTA_GATEWAY:
+			if (!gateway)
+				break;
+
+			inet_ntop(family, RTA_DATA(attr), buf, sizeof(buf));
+			*gateway = l_strdup(buf);
+
+			break;
+		case RTA_PREFSRC:
+			if (!src)
+				break;
+
+			inet_ntop(family, RTA_DATA(attr), buf, sizeof(buf));
+			*src = l_strdup(buf);
+
+			break;
+		case RTA_OIF:
+			if (!ifindex)
+				break;
+
+			*ifindex = *((uint32_t *) RTA_DATA(attr));
+			break;
+		}
+	}
+}
+
 uint32_t l_rtnl_set_linkmode_and_operstate(struct l_netlink *rtnl, int ifindex,
 					uint8_t linkmode, uint8_t operstate,
 					l_netlink_command_func_t cb,
@@ -294,44 +338,7 @@ void l_rtnl_route4_extract(const struct rtmsg *rtmsg, uint32_t len,
 				uint32_t *ifindex, char **dst, char **gateway,
 				char **src)
 {
-	struct in_addr in_addr;
-	struct rtattr *attr;
-
-	for (attr = RTM_RTA(rtmsg); RTA_OK(attr, len);
-						attr = RTA_NEXT(attr, len)) {
-		switch (attr->rta_type) {
-		case RTA_DST:
-			if (!dst)
-				break;
-
-			in_addr = *((struct in_addr *) RTA_DATA(attr));
-			*dst = l_strdup(inet_ntoa(in_addr));
-
-			break;
-		case RTA_GATEWAY:
-			if (!gateway)
-				break;
-
-			in_addr = *((struct in_addr *) RTA_DATA(attr));
-			*gateway = l_strdup(inet_ntoa(in_addr));
-
-			break;
-		case RTA_PREFSRC:
-			if (!src)
-				break;
-
-			in_addr = *((struct in_addr *) RTA_DATA(attr));
-			*src = l_strdup(inet_ntoa(in_addr));
-
-			break;
-		case RTA_OIF:
-			if (!ifindex)
-				break;
-
-			*ifindex = *((uint32_t *) RTA_DATA(attr));
-			break;
-		}
-	}
+	l_rtnl_route_extract(rtmsg, len, AF_INET, ifindex, dst, gateway, src);
 }
 
 uint32_t l_rtnl_route4_dump(struct l_netlink *rtnl,
@@ -560,6 +567,27 @@ uint32_t l_rtnl_ifaddr6_delete(struct l_netlink *rtnl, int ifindex,
 						ip, cb, user_data, destroy);
 }
 
+void l_rtnl_route6_extract(const struct rtmsg *rtmsg, uint32_t len,
+				uint32_t *ifindex, char **dst, char **gateway,
+				char **src)
+{
+	l_rtnl_route_extract(rtmsg, len, AF_INET6, ifindex, dst, gateway, src);
+}
+
+uint32_t l_rtnl_route6_dump(struct l_netlink *rtnl,
+				l_netlink_command_func_t cb, void *user_data,
+				l_netlink_destroy_func_t destroy)
+{
+	struct rtmsg rtmsg;
+
+	memset(&rtmsg, 0, sizeof(struct rtmsg));
+	rtmsg.rtm_family = AF_INET6;
+
+	return l_netlink_send(rtnl, RTM_GETROUTE, NLM_F_DUMP, &rtmsg,
+					sizeof(struct rtmsg), cb, user_data,
+					destroy);
+}
+
 static uint32_t l_rtnl_route6_change(struct l_netlink *rtnl,
 					uint16_t nlmsg_type, int ifindex,
 					const char *gateway,
diff --git a/ell/rtnl.h b/ell/rtnl.h
index a6e6d1323015..2be2eaee99e2 100644
--- a/ell/rtnl.h
+++ b/ell/rtnl.h
@@ -97,6 +97,12 @@ uint32_t l_rtnl_ifaddr6_delete(struct l_netlink *rtnl, int ifindex,
 					l_netlink_command_func_t cb,
 					void *user_data,
 					l_netlink_destroy_func_t destroy);
+void l_rtnl_route6_extract(const struct rtmsg *rtmsg, uint32_t len,
+				uint32_t *ifindex, char **dst, char **gateway,
+				char **src);
+uint32_t l_rtnl_route6_dump(struct l_netlink *rtnl,
+				l_netlink_command_func_t cb, void *user_data,
+				l_netlink_destroy_func_t destroy);
 uint32_t l_rtnl_route6_add_gateway(struct l_netlink *rtnl, int ifindex,
 					const char *gateway,
 					uint32_t priority_offset,
-- 
2.25.0

  reply	other threads:[~2020-02-14 18:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-14 18:52 [PATCH 0/3] rtnl: add missing APIs Daniel Wagner
2020-02-14 18:52 ` Daniel Wagner [this message]
2020-02-14 18:52 ` [PATCH 2/3] unit: Add unit test for l_rtnl_route6_{dump|extract} Daniel Wagner
2020-02-14 18:52 ` [PATCH 3/3] rtnl: Extract table, priority and pref Daniel Wagner
2020-02-14 19:05 ` [PATCH 0/3] rtnl: add missing APIs 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=20200214185253.32658-2-wagi@monom.org \
    --to=wagi@monom.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.