[systemd-devel] [PATCH 20/28] dhcp: Add timeout and main loop support
Patrik Flykt
patrik.flykt at linux.intel.com
Wed Nov 13 13:22:48 PST 2013
Require a main loop to be set when creating a DHCP client. Set up
a timer to resend DHCP Discover messages and add a 0-2 second
delay to the timeout value. Move to state Selecting after successful
sending of a Discover message.
---
src/dhcp/client.c | 77 ++++++++++++++++++++++++++++++++++++++--
src/dhcp/client.h | 4 ++-
src/dhcp/dhcp-example-client.c | 7 +++-
src/dhcp/test-dhcp-client.c | 19 ++++++----
4 files changed, 96 insertions(+), 11 deletions(-)
diff --git a/src/dhcp/client.c b/src/dhcp/client.c
index 79ac93b..cb3d41b 100644
--- a/src/dhcp/client.c
+++ b/src/dhcp/client.c
@@ -34,12 +34,15 @@
struct DHCPClient {
DHCPState state;
+ sd_event *event;
+ sd_event_source *timeout_resend;
int index;
uint8_t *req_opts;
int req_opts_size;
struct in_addr *last_addr;
struct ether_addr mac_addr;
uint32_t xid;
+ uint64_t start_time;
};
static uint8_t default_req_opts[] = {
@@ -142,6 +145,10 @@ static int client_stop(DHCPClient *client, int error)
client->state == DHCP_STATE_INIT_REBOOT)
return -EALREADY;
+ if (client->timeout_resend)
+ client->timeout_resend =
+ sd_event_source_unref(client->timeout_resend);
+
switch (client->state) {
case DHCP_STATE_INIT:
@@ -294,8 +301,59 @@ error:
return err;
}
+static int client_timeout_resend(sd_event_source *s, uint64_t usec,
+ void *userdata)
+{
+ DHCPClient *client = userdata;
+ uint64_t next_timeout;
+ uint16_t secs;
+ int err = 0;
+
+ switch (client->state) {
+ case DHCP_STATE_INIT:
+ case DHCP_STATE_SELECTING:
+
+ if (!client->start_time)
+ client->start_time = usec;
+
+ secs = (usec - client->start_time) / USEC_PER_SEC;
+
+ next_timeout = usec + 2 * USEC_PER_SEC + (random() & 0x1fffff);
+
+ err = sd_event_add_monotonic(sd_event_get(s), next_timeout,
+ 10 * USEC_PER_MSEC,
+ client_timeout_resend, client,
+ &client->timeout_resend);
+ if (err < 0)
+ goto error;
+
+ if (client_send_discover(client, secs) >= 0)
+ client->state = DHCP_STATE_SELECTING;
+
+ break;
+
+ case DHCP_STATE_INIT_REBOOT:
+ case DHCP_STATE_REBOOTING:
+ case DHCP_STATE_REQUESTING:
+ case DHCP_STATE_BOUND:
+ case DHCP_STATE_RENEWING:
+ case DHCP_STATE_REBINDING:
+
+ break;
+ }
+
+ return 0;
+
+error:
+ client_stop(client, err);
+
+ return 0;
+}
+
int dhcp_client_start(DHCPClient *client)
{
+ int err;
+
if (!client || client->index == -1)
return -EINVAL;
@@ -305,7 +363,18 @@ int dhcp_client_start(DHCPClient *client)
client->xid = random_u();
- return client_send_discover(client, 0);
+ err = sd_event_add_monotonic(client->event, now(CLOCK_MONOTONIC), 0,
+ client_timeout_resend, client,
+ &client->timeout_resend);
+ if (err < 0)
+ goto error;
+
+ return 0;
+
+error:
+ client_stop(client, err);
+
+ return err;
}
int dhcp_client_stop(DHCPClient *client)
@@ -313,15 +382,19 @@ int dhcp_client_stop(DHCPClient *client)
return client_stop(client, 0);
}
-DHCPClient *dhcp_client_new(void)
+DHCPClient *dhcp_client_new(sd_event *event)
{
DHCPClient *client;
int i;
+ if (!event)
+ return NULL;
+
client = new0(DHCPClient, 1);
if (!client)
return NULL;
+ client->event = event;
client->state = DHCP_STATE_INIT;
client->index = -1;
diff --git a/src/dhcp/client.h b/src/dhcp/client.h
index 71fdbe3..568e70a 100644
--- a/src/dhcp/client.h
+++ b/src/dhcp/client.h
@@ -24,6 +24,8 @@
#include <netinet/in.h>
#include <net/ethernet.h>
+#include "sd-event.h"
+
struct DHCPClient;
typedef struct DHCPClient DHCPClient;
@@ -35,4 +37,4 @@ int dhcp_client_set_mac(DHCPClient *client, struct ether_addr *addr);
int dhcp_client_start(DHCPClient *client);
int dhcp_client_stop(DHCPClient *client);
-DHCPClient *dhcp_client_new(void);
+DHCPClient *dhcp_client_new(sd_event *event);
diff --git a/src/dhcp/dhcp-example-client.c b/src/dhcp/dhcp-example-client.c
index b7f1a5f..afb7102 100644
--- a/src/dhcp/dhcp-example-client.c
+++ b/src/dhcp/dhcp-example-client.c
@@ -84,6 +84,7 @@ int main(int argc, char **argv)
DHCPClient *client;
int err, index;
struct ether_addr mac;
+ sd_event *event;
if (argc != 2) {
printf("Usage: %s <interface>\n", argv[0]);
@@ -98,7 +99,10 @@ int main(int argc, char **argv)
if (err < 0)
return 2;
- client = dhcp_client_new();
+ if (sd_event_new(&event) < 0)
+ return 2;
+
+ client = dhcp_client_new(event);
if (!client)
return 3;
@@ -107,6 +111,7 @@ int main(int argc, char **argv)
dhcp_client_set_mac(client, &mac);
dhcp_client_start(client);
+ sd_event_loop(event);
return 0;
}
diff --git a/src/dhcp/test-dhcp-client.c b/src/dhcp/test-dhcp-client.c
index b5dde05..9cad04b 100644
--- a/src/dhcp/test-dhcp-client.c
+++ b/src/dhcp/test-dhcp-client.c
@@ -33,11 +33,11 @@ static struct ether_addr mac_addr = {
.ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
};
-static void test_request_basic(void)
+static void test_request_basic(sd_event *e)
{
DHCPClient *client;
- client = dhcp_client_new();
+ client = dhcp_client_new(e);
assert(client);
@@ -171,12 +171,12 @@ int __dhcp_network_send_raw_packet(int index, void *packet, int len)
return 575;
}
-static void test_discover_message(void)
+static void test_discover_message(sd_event *e)
{
DHCPClient *client;
int res;
- client = dhcp_client_new();
+ client = dhcp_client_new(e);
assert(client);
assert(dhcp_client_set_index(client, 42) >= 0);
@@ -186,15 +186,20 @@ static void test_discover_message(void)
res = dhcp_client_start(client);
- assert(res == 575 || res == -EINPROGRESS);
+ assert(res == 575 || res == 0);
}
int main(int argc, char *argv[])
{
- test_request_basic();
+ sd_event *e;
+
+ assert(sd_event_new(&e) >= 0);
+
+ test_request_basic(e);
test_checksum();
- test_discover_message();
+ test_discover_message(e);
+ sd_event_run(e, (uint64_t) -1);
return 0;
}
--
1.7.10.4
More information about the systemd-devel
mailing list