mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
nss: Move address lookup code into a separate function
The part of the code that iterates over an array of JSON values is going to be re-used. Instead of copying it over, move it to a separate function. Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
6078765666
commit
3225e5b26e
@ -141,6 +141,65 @@ appendAddr(leaseAddress **tmpAddress,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
findLeaseInJSON(leaseAddress **tmpAddress,
|
||||||
|
size_t *ntmpAddress,
|
||||||
|
virJSONValuePtr leases_array,
|
||||||
|
size_t nleases,
|
||||||
|
const char *name,
|
||||||
|
int af,
|
||||||
|
bool *found)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
long long expirytime;
|
||||||
|
time_t currtime;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if ((currtime = time(NULL)) == (time_t) - 1) {
|
||||||
|
ERROR("Failed to get current system time");
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < nleases; i++) {
|
||||||
|
virJSONValuePtr lease = virJSONValueArrayGet(leases_array, i);
|
||||||
|
const char *lease_name;
|
||||||
|
|
||||||
|
if (!lease) {
|
||||||
|
/* This should never happen (TM) */
|
||||||
|
ERROR("Unable to get element %zu of %zu", i, nleases);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
lease_name = virJSONValueObjectGetString(lease, "hostname");
|
||||||
|
|
||||||
|
if (STRNEQ_NULLABLE(name, lease_name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (virJSONValueObjectGetNumberLong(lease, "expiry-time", &expirytime) < 0) {
|
||||||
|
/* A lease cannot be present without expiry-time */
|
||||||
|
ERROR("expiry-time field missing for %s", name);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Do not report expired lease */
|
||||||
|
if (expirytime < (long long) currtime) {
|
||||||
|
DEBUG("Skipping expired lease for %s", name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG("Found record for %s", name);
|
||||||
|
*found = true;
|
||||||
|
|
||||||
|
if (appendAddr(tmpAddress, ntmpAddress, lease, af) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = 0;
|
||||||
|
cleanup:
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* findLease:
|
* findLease:
|
||||||
* @name: domain name to lookup
|
* @name: domain name to lookup
|
||||||
@ -174,11 +233,9 @@ findLease(const char *name,
|
|||||||
const char *leaseDir = LEASEDIR;
|
const char *leaseDir = LEASEDIR;
|
||||||
struct dirent *entry;
|
struct dirent *entry;
|
||||||
virJSONValuePtr leases_array = NULL;
|
virJSONValuePtr leases_array = NULL;
|
||||||
ssize_t i, nleases;
|
ssize_t nleases;
|
||||||
leaseAddress *tmpAddress = NULL;
|
leaseAddress *tmpAddress = NULL;
|
||||||
size_t ntmpAddress = 0;
|
size_t ntmpAddress = 0;
|
||||||
time_t currtime;
|
|
||||||
long long expirytime;
|
|
||||||
|
|
||||||
*address = NULL;
|
*address = NULL;
|
||||||
*naddress = 0;
|
*naddress = 0;
|
||||||
@ -226,46 +283,10 @@ findLease(const char *name,
|
|||||||
nleases = virJSONValueArraySize(leases_array);
|
nleases = virJSONValueArraySize(leases_array);
|
||||||
DEBUG("Read %zd leases", nleases);
|
DEBUG("Read %zd leases", nleases);
|
||||||
|
|
||||||
if ((currtime = time(NULL)) == (time_t) - 1) {
|
if (findLeaseInJSON(&tmpAddress, &ntmpAddress,
|
||||||
ERROR("Failed to get current system time");
|
leases_array, nleases,
|
||||||
|
name, af, found) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < nleases; i++) {
|
|
||||||
virJSONValuePtr lease;
|
|
||||||
const char *lease_name;
|
|
||||||
|
|
||||||
lease = virJSONValueArrayGet(leases_array, i);
|
|
||||||
|
|
||||||
if (!lease) {
|
|
||||||
/* This should never happen (TM) */
|
|
||||||
ERROR("Unable to get element %zd of %zd", i, nleases);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
lease_name = virJSONValueObjectGetString(lease, "hostname");
|
|
||||||
|
|
||||||
if (STRNEQ_NULLABLE(name, lease_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (virJSONValueObjectGetNumberLong(lease, "expiry-time", &expirytime) < 0) {
|
|
||||||
/* A lease cannot be present without expiry-time */
|
|
||||||
ERROR("expiry-time field missing for %s", name);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Do not report expired lease */
|
|
||||||
if (expirytime < (long long) currtime) {
|
|
||||||
DEBUG("Skipping expired lease for %s", name);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG("Found record for %s", lease_name);
|
|
||||||
*found = true;
|
|
||||||
|
|
||||||
if (appendAddr(&tmpAddress, &ntmpAddress, lease, af) < 0)
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
*address = tmpAddress;
|
*address = tmpAddress;
|
||||||
*naddress = ntmpAddress;
|
*naddress = ntmpAddress;
|
||||||
|
Loading…
Reference in New Issue
Block a user