mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-03-07 17:28:15 +00:00
libxlGetDHCPInterfaces: Switch to GLib
If we use glib alloc functions, we can drop the 'cleanup' label and @rv variable and also simplify the code a bit. Signed-off-by: Michal Privoznik <mprivozn@redhat.com> Reviewed-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
parent
66eafbc26f
commit
f1625edc16
@ -6234,9 +6234,6 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
|
|||||||
g_autoptr(virConnect) conn = NULL;
|
g_autoptr(virConnect) conn = NULL;
|
||||||
virDomainInterfacePtr *ifaces_ret = NULL;
|
virDomainInterfacePtr *ifaces_ret = NULL;
|
||||||
size_t ifaces_count = 0;
|
size_t ifaces_count = 0;
|
||||||
int rv = -1;
|
|
||||||
int n_leases = 0;
|
|
||||||
virNetworkDHCPLeasePtr *leases = NULL;
|
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
if (!(conn = virGetConnectNetwork()))
|
if (!(conn = virGetConnectNetwork()))
|
||||||
@ -6245,6 +6242,8 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
|
|||||||
for (i = 0; i < vm->def->nnets; i++) {
|
for (i = 0; i < vm->def->nnets; i++) {
|
||||||
g_autoptr(virNetwork) network = NULL;
|
g_autoptr(virNetwork) network = NULL;
|
||||||
char macaddr[VIR_MAC_STRING_BUFLEN];
|
char macaddr[VIR_MAC_STRING_BUFLEN];
|
||||||
|
virNetworkDHCPLeasePtr *leases = NULL;
|
||||||
|
int n_leases = 0;
|
||||||
virDomainInterfacePtr iface = NULL;
|
virDomainInterfacePtr iface = NULL;
|
||||||
size_t j;
|
size_t j;
|
||||||
|
|
||||||
@ -6263,21 +6262,16 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
|
|||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (n_leases) {
|
if (n_leases) {
|
||||||
if (VIR_EXPAND_N(ifaces_ret, ifaces_count, 1) < 0)
|
ifaces_ret = g_renew(typeof(*ifaces_ret), ifaces_ret, ifaces_count + 1);
|
||||||
goto error;
|
ifaces_ret[ifaces_count] = g_new0(typeof(**ifaces_ret), 1);
|
||||||
|
iface = ifaces_ret[ifaces_count];
|
||||||
|
ifaces_count++;
|
||||||
|
|
||||||
if (VIR_ALLOC(ifaces_ret[ifaces_count - 1]) < 0)
|
|
||||||
goto error;
|
|
||||||
|
|
||||||
iface = ifaces_ret[ifaces_count - 1];
|
|
||||||
/* Assuming each lease corresponds to a separate IP */
|
/* Assuming each lease corresponds to a separate IP */
|
||||||
iface->naddrs = n_leases;
|
iface->naddrs = n_leases;
|
||||||
|
|
||||||
if (VIR_ALLOC_N(iface->addrs, iface->naddrs) < 0)
|
iface->addrs = g_new0(typeof(*iface->addrs), iface->naddrs);
|
||||||
goto error;
|
|
||||||
|
|
||||||
iface->name = g_strdup(vm->def->nets[i]->ifname);
|
iface->name = g_strdup(vm->def->nets[i]->ifname);
|
||||||
|
|
||||||
iface->hwaddr = g_strdup(macaddr);
|
iface->hwaddr = g_strdup(macaddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -6286,29 +6280,17 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
|
|||||||
virDomainIPAddressPtr ip_addr = &iface->addrs[j];
|
virDomainIPAddressPtr ip_addr = &iface->addrs[j];
|
||||||
|
|
||||||
ip_addr->addr = g_strdup(lease->ipaddr);
|
ip_addr->addr = g_strdup(lease->ipaddr);
|
||||||
|
|
||||||
ip_addr->type = lease->type;
|
ip_addr->type = lease->type;
|
||||||
ip_addr->prefix = lease->prefix;
|
ip_addr->prefix = lease->prefix;
|
||||||
}
|
|
||||||
|
|
||||||
for (j = 0; j < n_leases; j++)
|
|
||||||
virNetworkDHCPLeaseFree(leases[j]);
|
virNetworkDHCPLeaseFree(leases[j]);
|
||||||
|
}
|
||||||
|
|
||||||
VIR_FREE(leases);
|
VIR_FREE(leases);
|
||||||
}
|
}
|
||||||
|
|
||||||
*ifaces = ifaces_ret;
|
*ifaces = g_steal_pointer(&ifaces_ret);
|
||||||
ifaces_ret = NULL;
|
return ifaces_count;
|
||||||
rv = ifaces_count;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
if (leases) {
|
|
||||||
for (i = 0; i < n_leases; i++)
|
|
||||||
virNetworkDHCPLeaseFree(leases[i]);
|
|
||||||
}
|
|
||||||
VIR_FREE(leases);
|
|
||||||
|
|
||||||
return rv;
|
|
||||||
|
|
||||||
error:
|
error:
|
||||||
if (ifaces_ret) {
|
if (ifaces_ret) {
|
||||||
@ -6317,7 +6299,7 @@ libxlGetDHCPInterfaces(virDomainObjPtr vm,
|
|||||||
}
|
}
|
||||||
VIR_FREE(ifaces_ret);
|
VIR_FREE(ifaces_ret);
|
||||||
|
|
||||||
goto cleanup;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user