From f89087272b5561c9a3fc9d6a4e2a09f75f688fa7 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 22 Sep 2015 14:17:32 +0200 Subject: sd-dhcp6: ensure canceling lease timers and refactor setting lease Factor our common code to set/clear the client lease. Resetting the lease involves canceling old timers. Also, most importantly, when resetting the lease in client_receive_advertise(), ensure to cancel the timers too. It is not immediately clear, why at that point no timers would be pending. The lease-unref from sd_dhcp6_client_unref() can be omited, because of the preceeding client_reset() call. --- src/libsystemd-network/sd-dhcp6-client.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/libsystemd-network/sd-dhcp6-client.c b/src/libsystemd-network/sd-dhcp6-client.c index 5489c77864..59d80bc38a 100644 --- a/src/libsystemd-network/sd-dhcp6-client.c +++ b/src/libsystemd-network/sd-dhcp6-client.c @@ -266,13 +266,18 @@ static void client_notify(sd_dhcp6_client *client, int event) { client->cb(client, event, client->userdata); } -static int client_reset(sd_dhcp6_client *client) { - assert_return(client, -EINVAL); - +static void client_set_lease(sd_dhcp6_client *client, sd_dhcp6_lease *lease) { if (client->lease) { dhcp6_lease_clear_timers(&client->lease->ia); - client->lease = sd_dhcp6_lease_unref(client->lease); + sd_dhcp6_lease_unref(client->lease); } + client->lease = lease; +} + +static int client_reset(sd_dhcp6_client *client) { + assert_return(client, -EINVAL); + + client_set_lease(client, NULL); client->receive_message = sd_event_source_unref(client->receive_message); @@ -826,12 +831,7 @@ static int client_receive_reply(sd_dhcp6_client *client, DHCP6Message *reply, si return 0; } - if (client->lease) { - dhcp6_lease_clear_timers(&client->lease->ia); - client->lease = sd_dhcp6_lease_unref(client->lease); - } - - client->lease = lease; + client_set_lease(client, lease); lease = NULL; return DHCP6_STATE_BOUND; @@ -860,8 +860,7 @@ static int client_receive_advertise(sd_dhcp6_client *client, DHCP6Message *adver r = dhcp6_lease_get_preference(client->lease, &pref_lease); if (r < 0 || pref_advertise > pref_lease) { - sd_dhcp6_lease_unref(client->lease); - client->lease = lease; + client_set_lease(client, lease); lease = NULL; r = 0; } @@ -1233,7 +1232,6 @@ sd_dhcp6_client *sd_dhcp6_client_unref(sd_dhcp6_client *client) { client_reset(client); sd_dhcp6_client_detach_event(client); - sd_dhcp6_lease_unref(client->lease); free(client->req_opts); free(client); -- cgit v1.2.3-54-g00ecf