
On Tue, Jan 28, 2020 at 10:54:09PM -0300, Julio Faracco wrote:
LXC version 3 or higher introduced indexes for network interfaces. Libvirt should be able to parse entries like `lxc.net.2.KEY`. This commit adds functions to parse this type of field. That's why array structures are so important this time.
Signed-off-by: Julio Faracco <jcfaracco@gmail.com> --- src/lxc/lxc_native.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c index b101848c09..35ac05f702 100644 --- a/src/lxc/lxc_native.c +++ b/src/lxc/lxc_native.c @@ -649,6 +649,55 @@ lxcNetworkParseDataSuffix(const char *entry, }
+static lxcNetworkParseDataPtr +lxcNetworkGetParseDataByIndex(lxcNetworkParseDataArray *networks, + unsigned int index) +{ + size_t ndata = networks->ndata; + size_t i; + + for (i = 0; i < ndata; i++) { + if (networks->parseData[i]->index == index) + return networks->parseData[i]; + } + + /* Index was not found. So, it is time to add new * + * interface and return this last position. */ + if (VIR_EXPAND_N(networks->parseData, networks->ndata, 1) < 0) + return NULL; + + networks->parseData[ndata] = g_new0(lxcNetworkParseData, 1); + networks->parseData[ndata]->index = index; + + return networks->parseData[ndata]; +} + + +static int +lxcNetworkParseDataEntry(const char *name, + virConfValuePtr value, + lxcNetworkParseDataArray *networks) +{ + lxcNetworkParseData *parseData; + const char *suffix_tmp = STRSKIP(name, "lxc.net."); + g_autofree char *suffix = NULL;
Actually your v2 PATCH was right. The 'suffix' variable is not a newly allocated string - it is just a pointer to an offset in the original string.
+ unsigned long long index; + + if (virStrToLong_ull(suffix_tmp, &suffix, 10, &index) < 0) + return -1; + + if (suffix[0] != '.') + return -1; + + suffix++; + + if (!(parseData = lxcNetworkGetParseDataByIndex(networks, index))) + return -1; + + return lxcNetworkParseDataSuffix(suffix, value, parseData); +} +
Regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|