n-dhcp4: maintain the probe's lease list in "n-dhcp4-c-probe.c"

The lease list and the probe's state are strongly related. That is
evidenced by the fact that sometimes we check the state and then
access probe->current_lease without further checking.

The code in "n-dhcp4-c-probe.c" (select_lease, accept, decline) already
changes and maintains the state, it should also maintain the lease list.
Move the code.
This commit is contained in:
Thomas Haller 2022-05-23 11:17:15 +02:00
parent 825bf49430
commit f40bbb819f
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 15 additions and 39 deletions

View file

@ -354,28 +354,12 @@ _c_public_ int n_dhcp4_client_lease_query(NDhcp4ClientLease *lease, uint8_t opti
* Returns -ENOTRECOVERABLE when called in an unexpected state.
*/
_c_public_ int n_dhcp4_client_lease_select(NDhcp4ClientLease *lease) {
NDhcp4ClientLease *l, *t_l;
NDhcp4ClientProbe *probe;
int r;
if (!lease->probe)
return -ENOTRECOVERABLE;
if (lease->probe->current_lease)
return -ENOTRECOVERABLE;
r = n_dhcp4_client_probe_transition_select(lease->probe, lease->message, n_dhcp4_gettime(CLOCK_BOOTTIME));
if (r)
return r;
/*
* Only one of the offered leases can be selected, so flush the list.
* All offered lease, including this one are now dead.
*/
probe = lease->probe;
c_list_for_each_entry_safe(l, t_l, &probe->lease_list, probe_link)
n_dhcp4_client_lease_unlink(l);
return 0;
return n_dhcp4_client_probe_transition_select(lease->probe, lease->message, n_dhcp4_gettime(CLOCK_BOOTTIME));
}
/**
@ -392,20 +376,12 @@ _c_public_ int n_dhcp4_client_lease_select(NDhcp4ClientLease *lease) {
* Returns -ENOTRECOVERABLE when called in an unexpected state.
*/
_c_public_ int n_dhcp4_client_lease_accept(NDhcp4ClientLease *lease) {
int r;
if (!lease->probe)
return -ENOTRECOVERABLE;
if (lease->probe->current_lease != lease)
return -ENOTRECOVERABLE;
r = n_dhcp4_client_probe_transition_accept(lease->probe, lease->message);
if (r)
return r;
n_dhcp4_client_lease_unlink(lease);
return 0;
return n_dhcp4_client_probe_transition_accept(lease->probe, lease->message);
}
/**
@ -422,19 +398,10 @@ _c_public_ int n_dhcp4_client_lease_accept(NDhcp4ClientLease *lease) {
* Returns -ENOTRECOVERABLE when called in an unexpected state.
*/
_c_public_ int n_dhcp4_client_lease_decline(NDhcp4ClientLease *lease, const char *error) {
int r;
if (!lease->probe)
return -ENOTRECOVERABLE;
if (lease->probe->current_lease != lease)
return -ENOTRECOVERABLE;
r = n_dhcp4_client_probe_transition_decline(lease->probe, lease->message, error, n_dhcp4_gettime(CLOCK_BOOTTIME));
if (r)
return r;
lease->probe->current_lease = n_dhcp4_client_lease_unref(lease->probe->current_lease);
n_dhcp4_client_lease_unlink(lease);
return 0;
return n_dhcp4_client_probe_transition_decline(lease->probe, lease->message, error, n_dhcp4_gettime(CLOCK_BOOTTIME));
}

View file

@ -1024,6 +1024,7 @@ static int n_dhcp4_client_probe_transition_nak(NDhcp4ClientProbe *probe) {
int n_dhcp4_client_probe_transition_select(NDhcp4ClientProbe *probe, NDhcp4Incoming *offer, uint64_t ns_now) {
_c_cleanup_(n_dhcp4_outgoing_freep) NDhcp4Outgoing *request = NULL;
NDhcp4ClientLease *l, *t_l;
int r;
switch (probe->state) {
@ -1042,10 +1043,15 @@ int n_dhcp4_client_probe_transition_select(NDhcp4ClientProbe *probe, NDhcp4Incom
else
request = NULL; /* consumed */
/* XXX: ignore other offers */
probe->state = N_DHCP4_CLIENT_PROBE_STATE_REQUESTING;
/*
* Only one of the offered leases can be selected, so flush the list.
* All offered lease, including this one are now dead.
*/
c_list_for_each_entry_safe(l, t_l, &probe->lease_list, probe_link)
n_dhcp4_client_lease_unlink(l);
return 0;
case N_DHCP4_CLIENT_PROBE_STATE_INIT:
case N_DHCP4_CLIENT_PROBE_STATE_INIT_REBOOT:
@ -1083,7 +1089,7 @@ int n_dhcp4_client_probe_transition_accept(NDhcp4ClientProbe *probe, NDhcp4Incom
return r;
probe->state = N_DHCP4_CLIENT_PROBE_STATE_BOUND;
n_dhcp4_client_lease_unlink(probe->current_lease);
n_dhcp4_client_arm_timer(probe->client);
return 0;
@ -1124,6 +1130,9 @@ int n_dhcp4_client_probe_transition_decline(NDhcp4ClientProbe *probe, NDhcp4Inco
/* XXX: what state to transition to? */
n_dhcp4_client_lease_unlink(probe->current_lease);
probe->current_lease = n_dhcp4_client_lease_unref(probe->current_lease);
return 0;
case N_DHCP4_CLIENT_PROBE_STATE_INIT: