From 4985c30a97c5dd871bac0ad7a0b90ee18b6951d8 Mon Sep 17 00:00:00 2001 From: Richard Aas Date: Mon, 22 Jun 2015 08:18:55 +0000 Subject: [PATCH] net: added net_default_gateway_get() --- include/re_net.h | 1 + src/net/net.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/re_net.h b/include/re_net.h index dce2c2c..81f511a 100644 --- a/include/re_net.h +++ b/include/re_net.h @@ -51,6 +51,7 @@ struct sa; /* Net generic */ int net_hostaddr(int af, struct sa *ip); int net_default_source_addr_get(int af, struct sa *ip); +int net_default_gateway_get(int af, struct sa *gw); /* Net sockets */ diff --git a/src/net/net.c b/src/net/net.c index 1fc2474..ce21821 100644 --- a/src/net/net.c +++ b/src/net/net.c @@ -104,3 +104,52 @@ int net_if_apply(net_ifaddr_h *ifh, void *arg) return net_if_list(ifh, arg); #endif } + + +static bool net_rt_handler(const char *ifname, const struct sa *dst, + int dstlen, const struct sa *gw, void *arg) +{ + void **argv = arg; + struct sa *ip = argv[1]; + (void)dst; + (void)dstlen; + + if (0 == str_cmp(ifname, argv[0])) { + *ip = *gw; + return true; + } + + return false; +} + + +/** + * Get the IP-address of the default gateway + * + * @param af Address Family + * @param gw Returned Gateway address + * + * @return 0 if success, otherwise errorcode + */ +int net_default_gateway_get(int af, struct sa *gw) +{ + char ifname[64]; + void *argv[2]; + int err; + + if (!af || !gw) + return EINVAL; + + err = net_rt_default_get(af, ifname, sizeof(ifname)); + if (err) + return err; + + argv[0] = ifname; + argv[1] = gw; + + err = net_rt_list(net_rt_handler, argv); + if (err) + return err; + + return 0; +}