* [PATCH 1/3] rtnl: Add l_rtnl_route6_dump() and l_rtnl_route6_extract()
2020-02-14 18:52 [PATCH 0/3] rtnl: add missing APIs Daniel Wagner
@ 2020-02-14 18:52 ` Daniel Wagner
2020-02-14 18:52 ` [PATCH 2/3] unit: Add unit test for l_rtnl_route6_{dump|extract} Daniel Wagner
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Daniel Wagner @ 2020-02-14 18:52 UTC (permalink / raw)
To: ell
[-- 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
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 2/3] unit: Add unit test for l_rtnl_route6_{dump|extract}
2020-02-14 18:52 [PATCH 0/3] rtnl: add missing APIs Daniel Wagner
2020-02-14 18:52 ` [PATCH 1/3] rtnl: Add l_rtnl_route6_dump() and l_rtnl_route6_extract() Daniel Wagner
@ 2020-02-14 18:52 ` 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
3 siblings, 0 replies; 5+ messages in thread
From: Daniel Wagner @ 2020-02-14 18:52 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 1580 bytes --]
---
unit/test-rtnl.c | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/unit/test-rtnl.c b/unit/test-rtnl.c
index c0659c4378da..37db73dc6f2f 100644
--- a/unit/test-rtnl.c
+++ b/unit/test-rtnl.c
@@ -139,6 +139,37 @@ static void test_route4_dump(struct l_netlink *rtnl, void *user_data)
NULL, route4_dump_destroy_cb));
}
+static void route6_dump_cb(int error,
+ uint16_t type, const void *data,
+ uint32_t len, void *user_data)
+{
+ const struct rtmsg *rtmsg = data;
+ char *dst = NULL, *gateway = NULL, *src = NULL;
+ uint32_t idx;
+
+ test_assert(!error);
+ test_assert(type == RTM_NEWROUTE);
+
+ l_rtnl_route6_extract(rtmsg, len, &idx, &dst, &gateway, &src);
+
+ l_info("idx %d dst %s gateway %s src %s", idx, dst, gateway, src);
+
+ l_free(dst);
+ l_free(gateway);
+ l_free(src);
+}
+
+static void route6_dump_destroy_cb(void *user_data)
+{
+ test_next();
+}
+
+static void test_route6_dump(struct l_netlink *rtnl, void *user_data)
+{
+ test_assert(l_rtnl_route6_dump(rtnl, route6_dump_cb,
+ NULL, route6_dump_destroy_cb));
+}
+
static void ifaddr4_dump_cb(int error,
uint16_t type, const void *data,
uint32_t len, void *user_data)
@@ -211,6 +242,7 @@ int main(int argc, char *argv[])
return -1;
test_add("Dump IPv4 routing table", test_route4_dump, NULL);
+ test_add("Dump IPv6 routing table", test_route6_dump, NULL);
test_add("Dump IPv4 addresses", test_ifaddr4_dump, NULL);
test_add("Dump IPv6 addresses", test_ifaddr6_dump, NULL);
--
2.25.0
^ permalink raw reply related [flat|nested] 5+ messages in thread* [PATCH 3/3] rtnl: Extract table, priority and pref
2020-02-14 18:52 [PATCH 0/3] rtnl: add missing APIs Daniel Wagner
2020-02-14 18:52 ` [PATCH 1/3] rtnl: Add l_rtnl_route6_dump() and l_rtnl_route6_extract() Daniel Wagner
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 ` Daniel Wagner
2020-02-14 19:05 ` [PATCH 0/3] rtnl: add missing APIs Denis Kenzior
3 siblings, 0 replies; 5+ messages in thread
From: Daniel Wagner @ 2020-02-14 18:52 UTC (permalink / raw)
To: ell
[-- Attachment #1: Type: text/plain, Size: 5216 bytes --]
---
ell/rtnl.c | 38 ++++++++++++++++++++++++++++++--------
ell/rtnl.h | 8 ++++----
unit/test-rtnl.c | 16 ++++++++++------
3 files changed, 44 insertions(+), 18 deletions(-)
diff --git a/ell/rtnl.c b/ell/rtnl.c
index c94531144eb4..4600a7e42001 100644
--- a/ell/rtnl.c
+++ b/ell/rtnl.c
@@ -69,12 +69,14 @@ static size_t rta_add_data(void *rta_buf, unsigned short type, void *data,
}
static void l_rtnl_route_extract(const struct rtmsg *rtmsg, uint32_t len,
- int family, uint32_t *ifindex, char **dst,
- char **gateway, char **src)
+ int family, uint32_t *table, uint32_t *ifindex,
+ uint32_t *priority, uint8_t *pref,
+ char **dst, char **gateway, char **src)
{
struct rtattr *attr;
char buf[INET6_ADDRSTRLEN];
+ /* Not extracted at the moment: RTA_CACHEINFO for IPv6 */
for (attr = RTM_RTA(rtmsg); RTA_OK(attr, len);
attr = RTA_NEXT(attr, len)) {
switch (attr->rta_type) {
@@ -101,6 +103,24 @@ static void l_rtnl_route_extract(const struct rtmsg *rtmsg, uint32_t len,
inet_ntop(family, RTA_DATA(attr), buf, sizeof(buf));
*src = l_strdup(buf);
+ break;
+ case RTA_TABLE:
+ if (!table)
+ break;
+
+ *table = *((uint32_t *) RTA_DATA(attr));
+ break;
+ case RTA_PRIORITY:
+ if (!priority)
+ break;
+
+ *priority = *((uint32_t *) RTA_DATA(attr));
+ break;
+ case RTA_PREF:
+ if (!pref)
+ break;
+
+ *pref = *((uint8_t *) RTA_DATA(attr));
break;
case RTA_OIF:
if (!ifindex)
@@ -335,10 +355,11 @@ uint32_t l_rtnl_ifaddr4_delete(struct l_netlink *rtnl, int ifindex,
}
void l_rtnl_route4_extract(const struct rtmsg *rtmsg, uint32_t len,
- uint32_t *ifindex, char **dst, char **gateway,
- char **src)
+ uint32_t *table, uint32_t *ifindex,
+ char **dst, char **gateway, char **src)
{
- l_rtnl_route_extract(rtmsg, len, AF_INET, ifindex, dst, gateway, src);
+ l_rtnl_route_extract(rtmsg, len, AF_INET, table, ifindex,
+ NULL, NULL, dst, gateway, src);
}
uint32_t l_rtnl_route4_dump(struct l_netlink *rtnl,
@@ -568,10 +589,11 @@ uint32_t l_rtnl_ifaddr6_delete(struct l_netlink *rtnl, int ifindex,
}
void l_rtnl_route6_extract(const struct rtmsg *rtmsg, uint32_t len,
- uint32_t *ifindex, char **dst, char **gateway,
- char **src)
+ uint32_t *table, uint32_t *ifindex,
+ char **dst, char **gateway, char **src)
{
- l_rtnl_route_extract(rtmsg, len, AF_INET6, ifindex, dst, gateway, src);
+ l_rtnl_route_extract(rtmsg, len, AF_INET6, table, ifindex,
+ NULL, NULL, dst, gateway, src);
}
uint32_t l_rtnl_route6_dump(struct l_netlink *rtnl,
diff --git a/ell/rtnl.h b/ell/rtnl.h
index 2be2eaee99e2..ea283c1c5a6e 100644
--- a/ell/rtnl.h
+++ b/ell/rtnl.h
@@ -63,8 +63,8 @@ uint32_t l_rtnl_ifaddr4_delete(struct l_netlink *rtnl, int ifindex,
l_netlink_destroy_func_t destroy);
void l_rtnl_route4_extract(const struct rtmsg *rtmsg, uint32_t len,
- uint32_t *ifindex, char **dst, char **gateway,
- char **src);
+ uint32_t *table, uint32_t *ifindex,
+ char **dst, char **gateway, char **src);
uint32_t l_rtnl_route4_dump(struct l_netlink *rtnl,
l_netlink_command_func_t cb, void *user_data,
l_netlink_destroy_func_t destroy);
@@ -98,8 +98,8 @@ uint32_t l_rtnl_ifaddr6_delete(struct l_netlink *rtnl, int ifindex,
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 *table, 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);
diff --git a/unit/test-rtnl.c b/unit/test-rtnl.c
index 37db73dc6f2f..107f69a76da1 100644
--- a/unit/test-rtnl.c
+++ b/unit/test-rtnl.c
@@ -114,14 +114,16 @@ static void route4_dump_cb(int error,
{
const struct rtmsg *rtmsg = data;
char *dst = NULL, *gateway = NULL, *src = NULL;
- uint32_t idx;
+ uint32_t table, ifindex;
test_assert(!error);
test_assert(type == RTM_NEWROUTE);
- l_rtnl_route4_extract(rtmsg, len, &idx, &dst, &gateway, &src);
+ l_rtnl_route4_extract(rtmsg, len, &table, &ifindex,
+ &dst, &gateway, &src);
- l_info("idx %d dst %s gateway %s src %s", idx, dst, gateway, src);
+ l_info("table %d ifindex %d dst %s gateway %s src %s",
+ table, ifindex, dst, gateway, src);
l_free(dst);
l_free(gateway);
@@ -145,14 +147,16 @@ static void route6_dump_cb(int error,
{
const struct rtmsg *rtmsg = data;
char *dst = NULL, *gateway = NULL, *src = NULL;
- uint32_t idx;
+ uint32_t table, ifindex;
test_assert(!error);
test_assert(type == RTM_NEWROUTE);
- l_rtnl_route6_extract(rtmsg, len, &idx, &dst, &gateway, &src);
+ l_rtnl_route6_extract(rtmsg, len, &table, &ifindex,
+ &dst, &gateway, &src);
- l_info("idx %d dst %s gateway %s src %s", idx, dst, gateway, src);
+ l_info("table %d ifindex %d dst %s gateway %s src %s",
+ table, ifindex, dst, gateway, src);
l_free(dst);
l_free(gateway);
--
2.25.0
^ permalink raw reply related [flat|nested] 5+ messages in thread