From 4db05578f2b40f0374477bccf85a492a7e8ee8fc Mon Sep 17 00:00:00 2001 Message-Id: <4db05578f2b40f0374477bccf85a492a7e8ee8fc@dist-git> From: Martin Kletzander Date: Thu, 6 Aug 2015 13:10:23 +0200 Subject: [PATCH] conf: Add getter for network routes https://bugzilla.redhat.com/show_bug.cgi?id=1094205 Add virNetworkDefGetRouteByIndex() similarly to virNetworkDefGetIpByIndex(), but for routes. Signed-off-by: Martin Kletzander (cherry picked from commit 0f10eb6a289502b295b56bc0925cf05cfa32193d) Signed-off-by: Martin Kletzander Signed-off-by: Jiri Denemark --- src/conf/network_conf.c | 26 ++++++++++++++++++++++++++ src/conf/network_conf.h | 3 +++ src/libvirt_private.syms | 1 + 3 files changed, 30 insertions(+) diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index 31d4463..72006e9 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -803,6 +803,32 @@ virNetworkDefGetIpByIndex(const virNetworkDef *def, return NULL; } +/* return routes[index], or NULL if there aren't enough routes */ +virNetworkRouteDefPtr +virNetworkDefGetRouteByIndex(const virNetworkDef *def, + int family, size_t n) +{ + size_t i; + + if (!def->routes || n >= def->nroutes) + return NULL; + + if (family == AF_UNSPEC) + return def->routes[n]; + + /* find the nth route of type "family" */ + for (i = 0; i < def->nroutes; i++) { + virSocketAddrPtr addr = virNetworkRouteDefGetAddress(def->routes[i]); + if (VIR_SOCKET_ADDR_IS_FAMILY(addr, family) + && (n-- <= 0)) { + return def->routes[i]; + } + } + + /* failed to find enough of the right family */ + return NULL; +} + /* return number of 1 bits in netmask for the network's ipAddress, * or -1 on error */ diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h index 9411a02..1cd5100 100644 --- a/src/conf/network_conf.h +++ b/src/conf/network_conf.h @@ -360,6 +360,9 @@ virPortGroupDefPtr virPortGroupFindByName(virNetworkDefPtr net, virNetworkIpDefPtr virNetworkDefGetIpByIndex(const virNetworkDef *def, int family, size_t n); +virNetworkRouteDefPtr +virNetworkDefGetRouteByIndex(const virNetworkDef *def, + int family, size_t n); int virNetworkIpDefPrefix(const virNetworkIpDef *def); int virNetworkIpDefNetmask(const virNetworkIpDef *def, virSocketAddrPtr netmask); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index a6b523a..46e535e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -597,6 +597,7 @@ virNetworkDefFormat; virNetworkDefFormatBuf; virNetworkDefFree; virNetworkDefGetIpByIndex; +virNetworkDefGetRouteByIndex; virNetworkDefParseFile; virNetworkDefParseNode; virNetworkDefParseString; -- 2.5.0