[systemd-devel] [PATCH v2 25/26] dhcp: Add notification callback

Patrik Flykt patrik.flykt at linux.intel.com
Sun Nov 24 23:13:41 PST 2013


Define a notification callback and events for stopping and client
lease expiry. Add functions to fetch IP parameters from a lease.
---
v2: - previous 26/28
    - prefix length is more useful, implement a function for it instead of
      netmask

 src/dhcp/client.c            |  101 +++++++++++++++++++++++++++++++++++++++++-
 src/systemd/sd-dhcp-client.h |   11 +++++
 2 files changed, 111 insertions(+), 1 deletion(-)

diff --git a/src/dhcp/client.c b/src/dhcp/client.c
index a8fb08c..fb28b7b 100644
--- a/src/dhcp/client.c
+++ b/src/dhcp/client.c
@@ -63,6 +63,8 @@ struct DHCPClient {
         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;
 };
 
@@ -75,6 +77,17 @@ static const uint8_t default_req_opts[] = {
         DHCP_OPTION_NTP_SERVER,
 };
 
+int sd_dhcp_client_set_callback(DHCPClient *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(DHCPClient *client, const uint8_t option)
 {
         size_t i;
@@ -142,8 +155,88 @@ int sd_dhcp_client_set_mac(DHCPClient *client, const struct ether_addr *addr)
         return 0;
 }
 
+int sd_dhcp_client_get_address(DHCPClient *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_prefixlen(DHCPClient *client)
+{
+        uint32_t mask, len = 0;
+
+        assert_return(client, -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:
+                mask = be32toh(client->lease->subnet_mask);
+                while (mask) {
+                        len++;
+                        mask = mask << 1;
+                }
+
+                break;
+        }
+
+        return len;
+}
+
+int sd_dhcp_client_get_router(DHCPClient *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(DHCPClient *client, int event)
 {
+        if (client->cb)
+                client->cb(client, event, client->userdata);
+
         return 0;
 }
 
@@ -168,6 +261,8 @@ static int client_stop(DHCPClient *client, int error)
 
         client->attempt = 1;
 
+        client_notify(client, error);
+
         switch (client->state) {
 
         case DHCP_STATE_INIT:
@@ -459,6 +554,10 @@ error:
 static int client_timeout_expire(sd_event_source *s, uint64_t usec,
                                  void *userdata)
 {
+        DHCPClient *client = userdata;
+
+        client_stop(client, DHCP_EVENT_EXPIRED);
+
         return 0;
 }
 
@@ -843,7 +942,7 @@ error:
 
 int sd_dhcp_client_stop(DHCPClient *client)
 {
-        return client_stop(client, 0);
+        return client_stop(client, DHCP_EVENT_STOP);
 }
 
 DHCPClient *sd_dhcp_client_new(sd_event *event)
diff --git a/src/systemd/sd-dhcp-client.h b/src/systemd/sd-dhcp-client.h
index 3185552..c3d2a48 100644
--- a/src/systemd/sd-dhcp-client.h
+++ b/src/systemd/sd-dhcp-client.h
@@ -27,19 +27,30 @@
 #include "sd-event.h"
 
 enum {
+        DHCP_EVENT_STOP                         = 0,
         DHCP_EVENT_NO_LEASE                     = 1,
         DHCP_EVENT_IP_ACQUIRE                   = 2,
         DHCP_EVENT_IP_CHANGE                    = 3,
+        DHCP_EVENT_EXPIRED                      = 4,
 };
 
 typedef struct DHCPClient DHCPClient;
 
+typedef void (*sd_dhcp_client_cb_t)(DHCPClient *client, int event,
+                                    void *userdata);
+int sd_dhcp_client_set_callback(DHCPClient *client, sd_dhcp_client_cb_t cb,
+                                void *userdata);
+
 int sd_dhcp_client_set_request_option(DHCPClient *client, const uint8_t option);
 int sd_dhcp_client_set_request_address(DHCPClient *client,
                                        const struct in_addr *last_address);
 int sd_dhcp_client_set_index(DHCPClient *client, const int interface_index);
 int sd_dhcp_client_set_mac(DHCPClient *client, const struct ether_addr *addr);
 
+int sd_dhcp_client_get_address(DHCPClient *client, struct in_addr *addr);
+int sd_dhcp_client_get_prefixlen(DHCPClient *client);
+int sd_dhcp_client_get_router(DHCPClient *client, struct in_addr *addr);
+
 int sd_dhcp_client_stop(DHCPClient *client);
 int sd_dhcp_client_start(DHCPClient *client);
 DHCPClient *sd_dhcp_client_new(sd_event *event);
-- 
1.7.10.4



More information about the systemd-devel mailing list