From 0f941add365638ad487ba94017cc2cf1f8d45195 Mon Sep 17 00:00:00 2001 From: Patrik Flykt Date: Wed, 12 Mar 2014 11:46:40 +0200 Subject: [PATCH 1/1] libsystemd-network: Restart DHCP acquisition if the lease expires This causes the DHCP client struct initialization and DHCP client starting to be factored out into functions of their own. --- src/libsystemd-network/sd-dhcp-client.c | 72 +++++++++++++++++-------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 2d3af2f18..792e4af9c 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -178,7 +178,7 @@ static int client_notify(sd_dhcp_client *client, int event) { return 0; } -static int client_stop(sd_dhcp_client *client, int error) { +static int client_initialize(sd_dhcp_client *client) { assert_return(client, -EINVAL); client->receive_message = @@ -194,15 +194,24 @@ static int client_stop(sd_dhcp_client *client, int error) { client->attempt = 1; - client_notify(client, error); - client->start_time = 0; client->secs = 0; client->state = DHCP_STATE_INIT; + client->xid = 0; if (client->lease) client->lease = sd_dhcp_lease_unref(client->lease); + return 0; +} + +static int client_stop(sd_dhcp_client *client, int error) { + assert_return(client, -EINVAL); + + client_notify(client, error); + + client_initialize(client); + log_dhcp_client(client, "STOPPED"); return 0; @@ -529,13 +538,46 @@ error: } +static int client_start(sd_dhcp_client *client) { + int r; + + assert_return(client, -EINVAL); + assert_return(client->event, -EINVAL); + assert_return(client->index > 0, -EINVAL); + assert_return(client->fd < 0, -EBUSY); + assert_return(client->xid == 0, -EINVAL); + assert_return(client->state == DHCP_STATE_INIT || + client->state == DHCP_STATE_INIT_REBOOT, -EBUSY); + + client->xid = random_u32(); + + r = dhcp_network_bind_raw_socket(client->index, &client->link); + + if (r < 0) { + client_stop(client, r); + return r; + } + + client->fd = r; + client->start_time = now(CLOCK_MONOTONIC); + client->secs = 0; + + log_dhcp_client(client, "STARTED"); + + return client_initialize_events(client, client_receive_message_raw); +} + static int client_timeout_expire(sd_event_source *s, uint64_t usec, void *userdata) { sd_dhcp_client *client = userdata; log_dhcp_client(client, "EXPIRED"); - client_stop(client, DHCP_EVENT_EXPIRED); + client_notify(client, DHCP_EVENT_EXPIRED); + + /* start over as the lease was lost */ + client_initialize(client); + client_start(client); return 0; } @@ -961,27 +1003,15 @@ int sd_dhcp_client_start(sd_dhcp_client *client) { int r; assert_return(client, -EINVAL); - assert_return(client->event, -EINVAL); - assert_return(client->index > 0, -EINVAL); - assert_return(client->state == DHCP_STATE_INIT || - client->state == DHCP_STATE_INIT_REBOOT, -EBUSY); - client->xid = random_u32(); - - r = dhcp_network_bind_raw_socket(client->index, &client->link); - - if (r < 0) { - client_stop(client, r); + r = client_initialize(client); + if (r < 0) return r; - } - client->fd = r; - client->start_time = now(CLOCK_MONOTONIC); - client->secs = 0; + if (client->last_addr) + client->state = DHCP_STATE_INIT_REBOOT; - log_dhcp_client(client, "STARTED"); - - return client_initialize_events(client, client_receive_message_raw); + return client_start(client); } int sd_dhcp_client_stop(sd_dhcp_client *client) { -- 2.30.2