Create a function for handling the full IP, UDP and DHCP packet
and tie it to the main loop. Verify IP and UDP headers and checksum.
Creat a new lease structure with using the values supplied in the
DHCP message. Free the lease structure when client is stopped.

Split out socket handling into a creation and a sending part. As a
result modify the test code.
---
v3: use stack for 576 byte receive buffer, unref event source before closing
    fd, set socket to SOCK_NONBLOCK

 src/dhcp/client.c           |  195 ++++++++++++++++++++++++++++++++++++++++++-
 src/dhcp/internal.h         |    7 +-
 src/dhcp/network.c          |   38 +++++----
 src/dhcp/protocol.h         |    6 ++
 src/dhcp/test-dhcp-client.c |   26 +++++-
 5 files changed, 251 insertions(+), 21 deletions(-)

diff --git a/src/dhcp/client.c b/src/dhcp/client.c
index 5ab8015..bb0c9b3 100644
--- a/src/dhcp/client.c
+++ b/src/dhcp/client.c
@@ -32,17 +32,31 @@
 
 #define DHCP_CLIENT_MIN_OPTIONS_SIZE            312
 
+struct DHCPLease {
+        uint32_t lifetime;
+        uint32_t address;
+        uint32_t server_address;
+        uint32_t subnet_mask;
+        uint32_t router;
+};
+
+typedef struct DHCPLease DHCPLease;
+
 struct sd_dhcp_client {
         DHCPState state;
         sd_event *event;
         sd_event_source *timeout_resend;
         int index;
+        int fd;
+        union sockaddr_union link;
+        sd_event_source *receive_message;
         uint8_t *req_opts;
         size_t req_opts_size;
         uint32_t last_addr;
         struct ether_addr mac_addr;
         uint32_t xid;
         usec_t start_time;
+        DHCPLease *lease;
 };
 
 static const uint8_t default_req_opts[] = {
@@ -128,6 +142,13 @@ static int client_stop(sd_dhcp_client *client, int error)
         assert_return(client->state != DHCP_STATE_INIT &&
                       client->state != DHCP_STATE_INIT_REBOOT, -EALREADY);
 
+        client->receive_message =
+                sd_event_source_unref(client->receive_message);
+
+        if (client->fd >= 0)
+                close(client->fd);
+        client->fd = -1;
+
         client->timeout_resend = sd_event_source_unref(client->timeout_resend);
 
         switch (client->state) {
@@ -135,6 +156,7 @@ static int client_stop(sd_dhcp_client *client, int error)
         case DHCP_STATE_INIT:
         case DHCP_STATE_SELECTING:
 
+                client->start_time = 0;
                 client->state = DHCP_STATE_INIT;
                 break;
 
@@ -148,6 +170,11 @@ static int client_stop(sd_dhcp_client *client, int error)
                 break;
         }
 
+        if (client->lease) {
+                free(client->lease);
+                client->lease = NULL;
+        }
+
         return 0;
 }
 
@@ -278,9 +305,10 @@ static int client_send_discover(sd_dhcp_client *client, 
uint16_t secs)
         discover->ip.check = client_checksum(&discover->ip,
                                              sizeof(discover->ip));
 
-        err = dhcp_network_send_raw_packet(client->index, discover, len);
+        err = dhcp_network_send_raw_socket(client->fd, &client->link,
+                                           discover, len);
 
-        return 0;
+        return err;
 }
 
 static int client_timeout_resend(sd_event_source *s, uint64_t usec,
@@ -334,6 +362,154 @@ error:
         return 0;
 }
 
+static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
+                              void *user_data)
+{
+        DHCPLease *lease = user_data;
+        be32_t val;
+
+        switch(code) {
+
+        case DHCP_OPTION_IP_ADDRESS_LEASE_TIME:
+                if (len == 4) {
+                        memcpy(&val, option, 4);
+                        lease->lifetime = be32toh(val);
+                }
+
+                break;
+
+        case DHCP_OPTION_SERVER_IDENTIFIER:
+                if (len >= 4)
+                        memcpy(&lease->server_address, option, 4);
+
+                break;
+
+        case DHCP_OPTION_SUBNET_MASK:
+                if (len >= 4)
+                        memcpy(&lease->subnet_mask, option, 4);
+
+                break;
+
+        case DHCP_OPTION_ROUTER:
+                if (len >= 4)
+                        memcpy(&lease->router, option, 4);
+
+                break;
+        }
+
+        return 0;
+}
+
+static int client_receive_offer(sd_dhcp_client *client, DHCPPacket *offer,
+                                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))
+                return -EINVAL;
+
+        offer->ip.check = offer->udp.len;
+        offer->ip.ttl = 0;
+
+        if (hdrlen + be16toh(offer->udp.len) > len ||
+            client_checksum(&offer->ip.ttl, be16toh(offer->udp.len) + 12))
+                return -EINVAL;
+
+        if (be16toh(offer->udp.source) != DHCP_PORT_SERVER ||
+            be16toh(offer->udp.dest) != DHCP_PORT_CLIENT)
+                return -EINVAL;
+
+        if (offer->dhcp.op != BOOTREPLY)
+                return -EINVAL;
+
+        if (be32toh(offer->dhcp.xid) != client->xid)
+                return -EINVAL;
+
+        if (memcmp(&offer->dhcp.chaddr[0], &client->mac_addr.ether_addr_octet,
+                    ETHER_ADDR_LEN))
+                return -EINVAL;
+
+        lease = new0(DHCPLease, 1);
+        if (!lease)
+                return -ENOMEM;
+
+        len = len - DHCP_IP_UDP_SIZE;
+        if (dhcp_option_parse(&offer->dhcp, len, client_parse_offer,
+                              lease) != DHCP_OFFER)
+                goto error;
+
+        lease->address = offer->dhcp.yiaddr;
+
+        if (lease->address == INADDR_ANY ||
+            lease->server_address == INADDR_ANY ||
+            lease->subnet_mask == INADDR_ANY ||
+            lease->lifetime == 0)
+                goto error;
+
+        client->lease = lease;
+
+        return 0;
+
+error:
+        free(lease);
+
+        return -ENOMSG;
+}
+
+static int client_receive_raw_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;
+        DHCPPacket *message;
+
+        len = read(fd, &buf, buflen);
+        if (len < 0)
+                goto error;
+
+        message = (DHCPPacket *)&buf;
+
+        switch (client->state) {
+        case DHCP_STATE_SELECTING:
+
+                if (client_receive_offer(client, message, len) >= 0) {
+
+                        client->receive_message =
+                                sd_event_source_unref(client->receive_message);
+                        close(client->fd);
+                        client->fd = -1;
+
+                        client->timeout_resend =
+                                sd_event_source_unref(client->timeout_resend);
+
+                        client->state = DHCP_STATE_REQUESTING;
+                }
+
+                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:
+        return 0;
+}
+
 int sd_dhcp_client_start(sd_dhcp_client *client)
 {
         int err;
@@ -345,6 +521,20 @@ int sd_dhcp_client_start(sd_dhcp_client *client)
 
         client->xid = random_u();
 
+        client->fd = dhcp_network_bind_raw_socket(client->index,
+                                                  &client->link);
+
+        if (client->fd < 0) {
+                err = client->fd;
+                goto error;
+        }
+
+        err = sd_event_add_io(client->event, client->fd, EPOLLIN,
+                              client_receive_raw_message, client,
+                              &client->receive_message);
+        if (err < 0)
+                goto error;
+
         err = sd_event_add_monotonic(client->event, now(CLOCK_MONOTONIC), 0,
                                      client_timeout_resend, client,
                                      &client->timeout_resend);
@@ -377,6 +567,7 @@ sd_dhcp_client *sd_dhcp_client_new(sd_event *event)
         client->event = sd_event_ref(event);
         client->state = DHCP_STATE_INIT;
         client->index = -1;
+        client->fd = -1;
 
         client->req_opts_size = ELEMENTSOF(default_req_opts);
 
diff --git a/src/dhcp/internal.h b/src/dhcp/internal.h
index f3cde61..85bb908 100644
--- a/src/dhcp/internal.h
+++ b/src/dhcp/internal.h
@@ -22,10 +22,15 @@
 ***/
 
 #include <stdint.h>
+#include <linux/if_packet.h>
+
+#include "socket-util.h"
 
 #include "protocol.h"
 
-int dhcp_network_send_raw_packet(int index, const void *packet, size_t len);
+int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link);
+int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
+                                 const void *packet, size_t len);
 
 int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
                        size_t optlen, const void *optval);
diff --git a/src/dhcp/network.c b/src/dhcp/network.c
index ce21031..3a16947 100644
--- a/src/dhcp/network.c
+++ b/src/dhcp/network.c
@@ -30,26 +30,36 @@
 
 #include "internal.h"
 
-int dhcp_network_send_raw_packet(int index, const void *packet, size_t len)
-{
-        _cleanup_close_ int s;
-        union sockaddr_union link = {};
+int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
+ {
+        int s;
 
-        s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC, htons(ETH_P_IP));
+        s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
+                   htons(ETH_P_IP));
         if (s < 0)
                 return -errno;
 
-        link.ll.sll_family = AF_PACKET;
-        link.ll.sll_protocol = htons(ETH_P_IP);
-        link.ll.sll_ifindex =  index;
-        link.ll.sll_halen = ETH_ALEN;
-        memset(&link.ll.sll_addr, 0xff, ETH_ALEN);
+        link->ll.sll_family = AF_PACKET;
+        link->ll.sll_protocol = htons(ETH_P_IP);
+        link->ll.sll_ifindex =  index;
+        link->ll.sll_halen = ETH_ALEN;
+        memset(link->ll.sll_addr, 0xff, ETH_ALEN);
 
-        if (bind(s, &link.sa, sizeof(link.ll)) < 0)
+        if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
+                close(s);
                 return -errno;
+        }
 
-        if (sendto(s, packet, len, 0, &link.sa, sizeof(link.ll)) < 0)
-                return -errno;
+        return s;
+}
+
+int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
+                                 const void *packet, size_t len)
+{
+        int err = 0;
+
+        if (sendto(s, packet, len, 0, &link->sa, sizeof(link->ll)) < 0)
+                err = -errno;
 
-        return 0;
+        return err;
 }
diff --git a/src/dhcp/protocol.h b/src/dhcp/protocol.h
index fa077fb..95c4f4e 100644
--- a/src/dhcp/protocol.h
+++ b/src/dhcp/protocol.h
@@ -55,6 +55,10 @@ struct DHCPPacket {
 
 typedef struct DHCPPacket DHCPPacket;
 
+#define DHCP_IP_SIZE            (int32_t)(sizeof(struct iphdr))
+#define DHCP_IP_UDP_SIZE        (int32_t)(sizeof(struct udphdr) + DHCP_IP_SIZE)
+#define DHCP_MESSAGE_SIZE       (int32_t)(sizeof(DHCPMessage))
+
 enum {
         DHCP_PORT_SERVER                        = 67,
         DHCP_PORT_CLIENT                        = 68,
@@ -102,8 +106,10 @@ enum {
         DHCP_OPTION_DOMAIN_NAME                 = 15,
         DHCP_OPTION_NTP_SERVER                  = 42,
         DHCP_OPTION_REQUESTED_IP_ADDRESS        = 50,
+        DHCP_OPTION_IP_ADDRESS_LEASE_TIME       = 51,
         DHCP_OPTION_OVERLOAD                    = 52,
         DHCP_OPTION_MESSAGE_TYPE                = 53,
+        DHCP_OPTION_SERVER_IDENTIFIER           = 54,
         DHCP_OPTION_PARAMETER_REQUEST_LIST      = 55,
         DHCP_OPTION_CLIENT_IDENTIFIER           = 61,
         DHCP_OPTION_END                         = 255,
diff --git a/src/dhcp/test-dhcp-client.c b/src/dhcp/test-dhcp-client.c
index 28762e6..ee0848d 100644
--- a/src/dhcp/test-dhcp-client.c
+++ b/src/dhcp/test-dhcp-client.c
@@ -23,8 +23,12 @@
 #include <assert.h>
 #include <errno.h>
 #include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <unistd.h>
 
 #include "util.h"
+#include "socket-util.h"
 
 #include "protocol.h"
 #include "internal.h"
@@ -34,6 +38,8 @@ static struct ether_addr mac_addr = {
         .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}
 };
 
+static int test_fd[2];
+
 static void test_request_basic(sd_event *e)
 {
         sd_dhcp_client *client;
@@ -125,14 +131,15 @@ static int check_options(uint8_t code, uint8_t len, const 
uint8_t *option,
         return 0;
 }
 
-int dhcp_network_send_raw_packet(int index, const void *packet, size_t len)
+int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
+                                 const void *packet, size_t len)
 {
         size_t size;
         _cleanup_free_ DHCPPacket *discover;
         uint16_t ip_check, udp_check;
         int res;
 
-        assert(index == 42);
+        assert(s >= 0);
         assert(packet);
 
         size = sizeof(DHCPPacket) + 4;
@@ -146,8 +153,8 @@ int dhcp_network_send_raw_packet(int index, const void 
*packet, size_t len)
         assert(discover->ip.protocol == IPPROTO_UDP);
         assert(discover->ip.saddr == INADDR_ANY);
         assert(discover->ip.daddr == INADDR_BROADCAST);
-        assert(discover->udp.source == ntohs(DHCP_PORT_CLIENT));
-        assert(discover->udp.dest == ntohs(DHCP_PORT_SERVER));
+        assert(discover->udp.source == be16toh(DHCP_PORT_CLIENT));
+        assert(discover->udp.dest == be16toh(DHCP_PORT_SERVER));
 
         ip_check = discover->ip.check;
 
@@ -172,6 +179,14 @@ int dhcp_network_send_raw_packet(int index, const void 
*packet, size_t len)
         return 575;
 }
 
+int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
+{
+        if (socketpair(AF_UNIX, SOCK_STREAM, 0, test_fd) < 0)
+                return -errno;
+
+        return test_fd[0];
+}
+
 static void test_discover_message(sd_event *e)
 {
         sd_dhcp_client *client;
@@ -188,6 +203,9 @@ static void test_discover_message(sd_event *e)
         res = sd_dhcp_client_start(client);
 
         assert(res == 0 || res == -EINPROGRESS);
+
+        close(test_fd[0]);
+        close(test_fd[1]);
 }
 
 int main(int argc, char *argv[])
-- 
1.7.10.4

_______________________________________________
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel

Reply via email to