chiark / gitweb /
libsystemd-dhcp: add some asserts
[elogind.git] / src / libsystemd-dhcp / dhcp-client.c
index b1bdd385930d90d617b4222ff0f4937eb65324dd..68a7328348b700a0c29dbe04de40588006d9e8e5 100644 (file)
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <net/ethernet.h>
+#include <sys/param.h>
 
 #include "util.h"
 #include "list.h"
 #define DHCP_CLIENT_MIN_OPTIONS_SIZE            312
 
 struct DHCPLease {
+        uint32_t t1;
+        uint32_t t2;
         uint32_t lifetime;
-        uint32_t address;
-        uint32_t server_address;
-        uint32_t subnet_mask;
-        uint32_t router;
+        be32_t address;
+        be32_t server_address;
+        be32_t subnet_mask;
+        be32_t router;
 };
 
 typedef struct DHCPLease DHCPLease;
@@ -52,11 +55,17 @@ struct sd_dhcp_client {
         sd_event_source *receive_message;
         uint8_t *req_opts;
         size_t req_opts_size;
-        uint32_t last_addr;
+        be32_t last_addr;
         struct ether_addr mac_addr;
         uint32_t xid;
         usec_t start_time;
         unsigned int attempt;
+        usec_t request_sent;
+        sd_event_source *timeout_t1;
+        sd_event_source *timeout_t2;
+        sd_event_source *timeout_expire;
+        sd_dhcp_client_cb_t cb;
+        void *userdata;
         DHCPLease *lease;
 };
 
@@ -69,6 +78,20 @@ static const uint8_t default_req_opts[] = {
         DHCP_OPTION_NTP_SERVER,
 };
 
+static int client_receive_message(sd_event_source *s, int fd,
+                                  uint32_t revents, void *userdata);
+
+int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
+                                void *userdata)
+{
+        assert_return(client, -EINVAL);
+
+        client->cb = cb;
+        client->userdata = userdata;
+
+        return 0;
+}
+
 int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option)
 {
         size_t i;
@@ -137,6 +160,102 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client,
         return 0;
 }
 
+int sd_dhcp_client_get_address(sd_dhcp_client *client, struct in_addr *addr)
+{
+        assert_return(client, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        switch (client->state) {
+        case DHCP_STATE_INIT:
+        case DHCP_STATE_SELECTING:
+        case DHCP_STATE_INIT_REBOOT:
+        case DHCP_STATE_REBOOTING:
+        case DHCP_STATE_REQUESTING:
+                return -EADDRNOTAVAIL;
+
+        case DHCP_STATE_BOUND:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
+                addr->s_addr = client->lease->address;
+
+                break;
+        }
+
+        return 0;
+}
+
+int sd_dhcp_client_get_netmask(sd_dhcp_client *client, struct in_addr *addr)
+{
+        assert_return(client, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        switch (client->state) {
+        case DHCP_STATE_INIT:
+        case DHCP_STATE_SELECTING:
+        case DHCP_STATE_INIT_REBOOT:
+        case DHCP_STATE_REBOOTING:
+        case DHCP_STATE_REQUESTING:
+                return -EADDRNOTAVAIL;
+
+        case DHCP_STATE_BOUND:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
+                addr->s_addr = client->lease->subnet_mask;
+
+                break;
+        }
+
+        return 0;
+}
+
+int sd_dhcp_client_prefixlen(const struct in_addr *addr)
+{
+        int len = 0;
+        uint32_t mask;
+
+        assert_return(addr, -EADDRNOTAVAIL);
+
+        mask = be32toh(addr->s_addr);
+        while (mask) {
+                len++;
+                mask = mask << 1;
+        }
+
+        return len;
+}
+
+int sd_dhcp_client_get_router(sd_dhcp_client *client, struct in_addr *addr)
+{
+        assert_return(client, -EINVAL);
+        assert_return(addr, -EINVAL);
+
+        switch (client->state) {
+        case DHCP_STATE_INIT:
+        case DHCP_STATE_SELECTING:
+        case DHCP_STATE_INIT_REBOOT:
+        case DHCP_STATE_REBOOTING:
+        case DHCP_STATE_REQUESTING:
+                return -EADDRNOTAVAIL;
+
+        case DHCP_STATE_BOUND:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
+                addr->s_addr = client->lease->router;
+
+                break;
+        }
+
+        return 0;
+}
+
+static int client_notify(sd_dhcp_client *client, int event)
+{
+        if (client->cb)
+                client->cb(client, event, client->userdata);
+
+        return 0;
+}
+
 static int client_stop(sd_dhcp_client *client, int error)
 {
         assert_return(client, -EINVAL);
@@ -152,12 +271,20 @@ static int client_stop(sd_dhcp_client *client, int error)
 
         client->timeout_resend = sd_event_source_unref(client->timeout_resend);
 
+        client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
+        client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
+        client->timeout_expire = sd_event_source_unref(client->timeout_expire);
+
         client->attempt = 1;
 
+        client_notify(client, error);
+
         switch (client->state) {
 
         case DHCP_STATE_INIT:
         case DHCP_STATE_SELECTING:
+        case DHCP_STATE_REQUESTING:
+        case DHCP_STATE_BOUND:
 
                 client->start_time = 0;
                 client->state = DHCP_STATE_INIT;
@@ -165,8 +292,6 @@ static int client_stop(sd_dhcp_client *client, int error)
 
         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:
 
@@ -203,6 +328,10 @@ static int client_packet_init(sd_dhcp_client *client, uint8_t type,
            refuse to issue an DHCP lease if 'secs' is set to zero */
         message->secs = htobe16(secs);
 
+        if (client->state == DHCP_STATE_RENEWING ||
+            client->state == DHCP_STATE_REBINDING)
+                message->ciaddr = client->lease->address;
+
         memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
         (*opt)[0] = 0x63;
         (*opt)[1] = 0x82;
@@ -261,10 +390,13 @@ static uint16_t client_checksum(void *buf, int len)
 
         if (len & 0x01) {
                 odd = buf;
-                sum += odd[len];
+                sum += odd[len - 1];
         }
 
-        return ~((sum & 0xffff) + (sum >> 16));
+        while (sum >> 16)
+                sum = (sum & 0xffff) + (sum >> 16);
+
+        return ~sum;
 }
 
 static void client_append_ip_headers(DHCPPacket *packet, uint16_t len)
@@ -366,10 +498,17 @@ static int client_send_request(sd_dhcp_client *client, uint16_t secs)
         if (err < 0)
                 return err;
 
-        client_append_ip_headers(request, len);
+        if (client->state == DHCP_STATE_RENEWING) {
+                err = dhcp_network_send_udp_socket(client->fd,
+                                                   client->lease->server_address,
+                                                   &request->dhcp,
+                                                   len - DHCP_IP_UDP_SIZE);
+        } else {
+                client_append_ip_headers(request, len);
 
-        err = dhcp_network_send_raw_socket(client->fd, &client->link,
-                                           request, len);
+                err = dhcp_network_send_raw_socket(client->fd, &client->link,
+                                                   request, len);
+        }
 
         return err;
 }
@@ -378,17 +517,48 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                                  void *userdata)
 {
         sd_dhcp_client *client = userdata;
-        usec_t next_timeout;
+        usec_t next_timeout = 0;
+        uint32_t time_left;
         uint16_t secs;
         int err = 0;
 
-        secs = (usec - client->start_time) / USEC_PER_SEC;
+        switch (client->state) {
+        case DHCP_STATE_RENEWING:
+
+                time_left = (client->lease->t2 - client->lease->t1)/2;
+                if (time_left < 60)
+                        time_left = 60;
+
+                next_timeout = usec + time_left * USEC_PER_SEC;
+
+                break;
+
+        case DHCP_STATE_REBINDING:
+
+                time_left = (client->lease->lifetime - client->lease->t2)/2;
+                if (time_left < 60)
+                        time_left = 60;
 
-        if (client->attempt < 64)
-                client->attempt *= 2;
+                next_timeout = usec + time_left * USEC_PER_SEC;
 
-        next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC +
-                (random_u() & 0x1fffff);
+                break;
+
+        case DHCP_STATE_INIT:
+        case DHCP_STATE_INIT_REBOOT:
+        case DHCP_STATE_REBOOTING:
+        case DHCP_STATE_SELECTING:
+        case DHCP_STATE_REQUESTING:
+        case DHCP_STATE_BOUND:
+
+                if (client->attempt < 64)
+                        client->attempt *= 2;
+
+                next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
+
+                break;
+        }
+
+        next_timeout += (random_u() & 0x1fffff);
 
         err = sd_event_add_monotonic(client->event, next_timeout,
                                      10 * USEC_PER_MSEC,
@@ -397,6 +567,8 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
         if (err < 0)
                 goto error;
 
+        secs = (usec - client->start_time) / USEC_PER_SEC;
+
         switch (client->state) {
         case DHCP_STATE_INIT:
                 err = client_send_discover(client, secs);
@@ -418,17 +590,19 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                 break;
 
         case DHCP_STATE_REQUESTING:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
                 err = client_send_request(client, secs);
                 if (err < 0 && client->attempt >= 64)
                          goto error;
 
+                client->request_sent = usec;
+
                 break;
 
         case DHCP_STATE_INIT_REBOOT:
         case DHCP_STATE_REBOOTING:
         case DHCP_STATE_BOUND:
-        case DHCP_STATE_RENEWING:
-        case DHCP_STATE_REBINDING:
 
                 break;
         }
@@ -443,6 +617,84 @@ error:
         return 0;
 }
 
+static int client_initialize_events(sd_dhcp_client *client, usec_t usec)
+{
+        int r;
+
+        r = sd_event_add_io(client->event, client->fd, EPOLLIN,
+                            client_receive_message, client,
+                            &client->receive_message);
+        if (r < 0)
+                goto error;
+
+        r = sd_event_add_monotonic(client->event, usec, 0,
+                                   client_timeout_resend, client,
+                                   &client->timeout_resend);
+
+error:
+        if (r < 0)
+                client_stop(client, r);
+
+        return 0;
+
+}
+
+static int client_timeout_expire(sd_event_source *s, uint64_t usec,
+                                 void *userdata)
+{
+        sd_dhcp_client *client = userdata;
+
+        client_stop(client, DHCP_EVENT_EXPIRED);
+
+        return 0;
+}
+
+static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
+{
+        sd_dhcp_client *client = userdata;
+        int r;
+
+        if (client->fd >= 0) {
+                client->receive_message =
+                        sd_event_source_unref(client->receive_message);
+                close(client->fd);
+                client->fd = -1;
+        }
+
+        client->state = DHCP_STATE_REBINDING;
+        client->attempt = 1;
+
+        r = dhcp_network_bind_raw_socket(client->index, &client->link);
+        if (r < 0) {
+                client_stop(client, r);
+                return 0;
+        }
+
+        client->fd = r;
+
+        return client_initialize_events(client, usec);
+}
+
+static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
+{
+        sd_dhcp_client *client = userdata;
+        int r;
+
+        client->state = DHCP_STATE_RENEWING;
+        client->attempt = 1;
+
+        r = dhcp_network_bind_udp_socket(client->index,
+                                         client->lease->address);
+        if (r < 0) {
+                client_stop(client, r);
+                return 0;
+        }
+
+        client->fd = r;
+
+        return client_initialize_events(client, usec);
+}
+
 static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
                               void *user_data)
 {
@@ -476,46 +728,74 @@ static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
                         memcpy(&lease->router, option, 4);
 
                 break;
+
+        case DHCP_OPTION_RENEWAL_T1_TIME:
+                if (len == 4) {
+                        memcpy(&val, option, 4);
+                        lease->t1 = be32toh(val);
+                }
+
+                break;
+
+        case DHCP_OPTION_REBINDING_T2_TIME:
+                if (len == 4) {
+                        memcpy(&val, option, 4);
+                        lease->t2 = be32toh(val);
+                }
+
+                break;
         }
 
         return 0;
 }
 
-static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
-                                size_t len)
+static int client_verify_headers(sd_dhcp_client *client, DHCPPacket *message,
+                                 size_t len)
 {
         size_t hdrlen;
-        DHCPLease *lease;
 
         if (len < (DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE))
                 return -EINVAL;
 
-        hdrlen = offer->ip.ihl * 4;
-        if (hdrlen < 20 || hdrlen > len || client_checksum(&offer->ip,
+        hdrlen = message->ip.ihl * 4;
+        if (hdrlen < 20 || hdrlen > len || client_checksum(&message->ip,
                                                            hdrlen))
                 return -EINVAL;
 
-        offer->ip.check = offer->udp.len;
-        offer->ip.ttl = 0;
+        message->ip.check = message->udp.len;
+        message->ip.ttl = 0;
 
-        if (hdrlen + be16toh(offer->udp.len) > len ||
-            client_checksum(&offer->ip.ttl, be16toh(offer->udp.len) + 12))
+        if (hdrlen + be16toh(message->udp.len) > len ||
+            client_checksum(&message->ip.ttl, be16toh(message->udp.len) + 12))
                 return -EINVAL;
 
-        if (be16toh(offer->udp.source) != DHCP_PORT_SERVER ||
-            be16toh(offer->udp.dest) != DHCP_PORT_CLIENT)
+        if (be16toh(message->udp.source) != DHCP_PORT_SERVER ||
+            be16toh(message->udp.dest) != DHCP_PORT_CLIENT)
                 return -EINVAL;
 
-        if (offer->dhcp.op != BOOTREPLY)
+        if (message->dhcp.op != BOOTREPLY)
                 return -EINVAL;
 
-        if (be32toh(offer->dhcp.xid) != client->xid)
+        if (be32toh(message->dhcp.xid) != client->xid)
                 return -EINVAL;
 
-        if (memcmp(&offer->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
+        if (memcmp(&message->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
                     ETHER_ADDR_LEN))
                 return -EINVAL;
 
+        return 0;
+}
+
+static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
+                                size_t len)
+{
+        int err;
+        DHCPLease *lease;
+
+        err = client_verify_headers(client, offer, len);
+        if (err < 0)
+                return err;
+
         lease = new0(DHCPLease, 1);
         if (!lease)
                 return -ENOMEM;
@@ -543,13 +823,150 @@ error:
         return -ENOMSG;
 }
 
-static int client_receive_raw_message(sd_event_source *s, int fd,
-                                      uint32_t revents, void *userdata)
+static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
+                              size_t len)
+{
+        int r;
+        DHCPPacket *ack;
+        DHCPMessage *dhcp;
+        DHCPLease *lease;
+
+        if (client->state == DHCP_STATE_RENEWING) {
+                dhcp = (DHCPMessage *)buf;
+        } else {
+                ack = (DHCPPacket *)buf;
+
+                r = client_verify_headers(client, ack, len);
+                if (r < 0)
+                        return r;
+
+                dhcp = &ack->dhcp;
+                len -= DHCP_IP_UDP_SIZE;
+        }
+
+        lease = new0(DHCPLease, 1);
+        if (!lease)
+                return -ENOMEM;
+
+        r = dhcp_option_parse(dhcp, len, client_parse_offer, lease);
+
+        if (r == DHCP_NAK) {
+                r = DHCP_EVENT_NO_LEASE;
+                goto error;
+        }
+
+        if (r != DHCP_ACK) {
+                r = -ENOMSG;
+                goto error;
+        }
+
+        lease->address = dhcp->yiaddr;
+
+        if (lease->address == INADDR_ANY ||
+            lease->server_address == INADDR_ANY ||
+            lease->subnet_mask == INADDR_ANY || lease->lifetime == 0) {
+                r = -ENOMSG;
+                goto error;
+        }
+
+        r = DHCP_EVENT_IP_ACQUIRE;
+        if (client->lease) {
+                if (client->lease->address != lease->address ||
+                    client->lease->subnet_mask != lease->subnet_mask ||
+                    client->lease->router != lease->router) {
+                        r = DHCP_EVENT_IP_CHANGE;
+                }
+
+                free(client->lease);
+        }
+
+        client->lease = lease;
+
+        return r;
+
+error:
+        free(lease);
+
+        return r;
+}
+
+static uint64_t client_compute_timeout(uint64_t request_sent,
+                                       uint32_t lifetime)
+{
+        return request_sent + (lifetime - 3) * USEC_PER_SEC +
+                + (random_u() & 0x1fffff);
+}
+
+static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
+{
+        int err;
+        uint64_t next_timeout;
+
+        if (client->lease->lifetime < 10)
+                return -EINVAL;
+
+        client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
+        client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
+        client->timeout_expire = sd_event_source_unref(client->timeout_expire);
+
+        if (!client->lease->t1)
+                client->lease->t1 = client->lease->lifetime / 2;
+
+        next_timeout = client_compute_timeout(client->request_sent,
+                                              client->lease->t1);
+        if (next_timeout < usec)
+                return -EINVAL;
+
+        err = sd_event_add_monotonic(client->event, next_timeout,
+                                     10 * USEC_PER_MSEC,
+                                     client_timeout_t1, client,
+                                     &client->timeout_t1);
+        if (err < 0)
+                return err;
+
+        if (!client->lease->t2)
+                client->lease->t2 = client->lease->lifetime * 7 / 8;
+
+        if (client->lease->t2 < client->lease->t1)
+                return -EINVAL;
+
+        if (client->lease->lifetime < client->lease->t2)
+                return -EINVAL;
+
+        next_timeout = client_compute_timeout(client->request_sent,
+                                              client->lease->t2);
+        if (next_timeout < usec)
+                return -EINVAL;
+
+        err = sd_event_add_monotonic(client->event, next_timeout,
+                                     10 * USEC_PER_MSEC,
+                                     client_timeout_t2, client,
+                                     &client->timeout_t2);
+        if (err < 0)
+                return err;
+
+        next_timeout = client_compute_timeout(client->request_sent,
+                                              client->lease->lifetime);
+        if (next_timeout < usec)
+                return -EINVAL;
+
+        err = sd_event_add_monotonic(client->event, next_timeout,
+                                     10 * USEC_PER_MSEC,
+                                     client_timeout_expire, client,
+                                     &client->timeout_expire);
+        if (err < 0)
+                return err;
+
+        return 0;
+}
+
+static int client_receive_message(sd_event_source *s, int fd,
+                                  uint32_t revents, void *userdata)
 {
         sd_dhcp_client *client = userdata;
         uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
         int buflen = sizeof(buf);
-        int len, err = 0;
+        int len, r = 0, notify_event = 0;
         DHCPPacket *message;
         usec_t time_now;
 
@@ -557,15 +974,15 @@ static int client_receive_raw_message(sd_event_source *s, int fd,
         if (len < 0)
                 return 0;
 
-        err = sd_event_get_now_monotonic(client->event, &time_now);
-        if (err < 0)
+        r = sd_event_get_now_monotonic(client->event, &time_now);
+        if (r < 0)
                 goto error;
 
-        message = (DHCPPacket *)&buf;
-
         switch (client->state) {
         case DHCP_STATE_SELECTING:
 
+                message = (DHCPPacket *)&buf;
+
                 if (client_receive_offer(client, message, len) >= 0) {
 
                         client->timeout_resend =
@@ -574,77 +991,111 @@ static int client_receive_raw_message(sd_event_source *s, int fd,
                         client->state = DHCP_STATE_REQUESTING;
                         client->attempt = 1;
 
-                        err = sd_event_add_monotonic(client->event, time_now, 0,
-                                                     client_timeout_resend,
-                                                     client,
-                                                     &client->timeout_resend);
-                        if (err < 0)
+                        r = sd_event_add_monotonic(client->event, time_now, 0,
+                                                   client_timeout_resend,
+                                                   client,
+                                                   &client->timeout_resend);
+                        if (r < 0)
                                 goto error;
                 }
 
                 break;
 
+        case DHCP_STATE_REQUESTING:
+        case DHCP_STATE_RENEWING:
+        case DHCP_STATE_REBINDING:
+
+                r = client_receive_ack(client, buf, len);
+
+                if (r == DHCP_EVENT_NO_LEASE)
+                        goto error;
+
+                if (r >= 0) {
+                        client->timeout_resend =
+                                sd_event_source_unref(client->timeout_resend);
+
+                        if (client->state == DHCP_STATE_REQUESTING)
+                                notify_event = DHCP_EVENT_IP_ACQUIRE;
+                        else if (r != DHCP_EVENT_IP_ACQUIRE)
+                                notify_event = r;
+
+                        client->state = DHCP_STATE_BOUND;
+                        client->attempt = 1;
+
+                        client->last_addr = client->lease->address;
+
+                        r = client_set_lease_timeouts(client, time_now);
+                        if (r < 0)
+                                goto error;
+
+                        if (notify_event)
+                                client_notify(client, notify_event);
+
+                        client->receive_message =
+                                sd_event_source_unref(client->receive_message);
+                        close(client->fd);
+                        client->fd = -1;
+                }
+
+                r = 0;
+
+                break;
+
         case DHCP_STATE_INIT:
         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;
         }
 
 error:
-        if (err < 0)
-                return client_stop(client, err);
+        if (r < 0 || r == DHCP_EVENT_NO_LEASE)
+                return client_stop(client, r);
 
         return 0;
 }
 
 int sd_dhcp_client_start(sd_dhcp_client *client)
 {
-        int err;
+        int r;
 
         assert_return(client, -EINVAL);
-        assert_return(client->index >= 0, -EINVAL);
+        assert_return(client->index > 0, -EINVAL);
         assert_return(client->state == DHCP_STATE_INIT ||
                       client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
 
         client->xid = random_u();
 
-        client->fd = dhcp_network_bind_raw_socket(client->index,
-                                                  &client->link);
+        r = dhcp_network_bind_raw_socket(client->index, &client->link);
 
-        if (client->fd < 0) {
-                err = client->fd;
-                goto error;
+        if (r < 0) {
+                client_stop(client, r);
+                return r;
         }
 
-        err = sd_event_add_io(client->event, client->fd, EPOLLIN,
-                              client_receive_raw_message, client,
-                              &client->receive_message);
-        if (err < 0)
-                goto error;
-
+        client->fd = r;
         client->start_time = now(CLOCK_MONOTONIC);
-        err = sd_event_add_monotonic(client->event, client->start_time, 0,
-                                     client_timeout_resend, client,
-                                     &client->timeout_resend);
-        if (err < 0)
-                goto error;
 
-        return 0;
-
-error:
-        client_stop(client, err);
-
-        return err;
+        return client_initialize_events(client, client->start_time);
 }
 
 int sd_dhcp_client_stop(sd_dhcp_client *client)
 {
-        return client_stop(client, 0);
+        return client_stop(client, DHCP_EVENT_STOP);
+}
+
+sd_dhcp_client *sd_dhcp_client_free(sd_dhcp_client *client)
+{
+        assert_return(client, NULL);
+
+        sd_dhcp_client_stop(client);
+
+        sd_event_unref(client->event);
+        free(client->req_opts);
+        free(client);
+
+        return NULL;
 }
 
 sd_dhcp_client *sd_dhcp_client_new(sd_event *event)