[systemd-devel] [PATCH 08/17] sd-dhcp-server: add support for setting the server address

Tom Gundersen teg at jklm.no
Mon May 26 12:39:37 PDT 2014


---
 src/libsystemd-network/dhcp-server-internal.h |  1 +
 src/libsystemd-network/sd-dhcp-server.c       | 13 +++++++++++++
 src/libsystemd-network/test-dhcp-server.c     | 17 +++++++++++++++++
 src/systemd/sd-dhcp-server.h                  |  1 +
 4 files changed, 32 insertions(+)

diff --git a/src/libsystemd-network/dhcp-server-internal.h b/src/libsystemd-network/dhcp-server-internal.h
index 6c2f2b4..58a9877 100644
--- a/src/libsystemd-network/dhcp-server-internal.h
+++ b/src/libsystemd-network/dhcp-server-internal.h
@@ -39,6 +39,7 @@ struct sd_dhcp_server {
         int fd_raw;
 
         int index;
+        be32_t address;
 };
 
 typedef struct DHCPClientId {
diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c
index ecdc15d..7d6170c 100644
--- a/src/libsystemd-network/sd-dhcp-server.c
+++ b/src/libsystemd-network/sd-dhcp-server.c
@@ -27,6 +27,17 @@
 #include "dhcp-server-internal.h"
 #include "dhcp-internal.h"
 
+int sd_dhcp_server_set_address(sd_dhcp_server *server, struct in_addr *address) {
+        assert_return(server, -EINVAL);
+        assert_return(address, -EINVAL);
+        assert_return(address->s_addr, -EINVAL);
+        assert_return(server->address == htobe32(INADDR_ANY), -EBUSY);
+
+        server->address = address->s_addr;
+
+        return 0;
+}
+
 sd_dhcp_server *sd_dhcp_server_ref(sd_dhcp_server *server) {
         if (server)
                 assert_se(REFCNT_INC(server->n_ref) >= 2);
@@ -60,6 +71,7 @@ int sd_dhcp_server_new(sd_dhcp_server **ret, int ifindex) {
         server->n_ref = REFCNT_INIT;
         server->fd_raw = -1;
         server->fd = -1;
+        server->address = htobe32(INADDR_ANY);
         server->index = ifindex;
 
         *ret = server;
@@ -281,6 +293,7 @@ int sd_dhcp_server_start(sd_dhcp_server *server) {
         assert_return(!server->receive_message, -EBUSY);
         assert_return(server->fd_raw == -1, -EBUSY);
         assert_return(server->fd == -1, -EBUSY);
+        assert_return(server->address != htobe32(INADDR_ANY), -EBUSY);
 
         r = socket(AF_PACKET, SOCK_DGRAM | SOCK_NONBLOCK, 0);
         if (r < 0) {
diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c
index dd0f29a..0cbb4df 100644
--- a/src/libsystemd-network/test-dhcp-server.c
+++ b/src/libsystemd-network/test-dhcp-server.c
@@ -32,6 +32,12 @@
 
 static void test_basic(sd_event *event) {
         _cleanup_dhcp_server_unref_ sd_dhcp_server *server = NULL;
+        struct in_addr address_lo = {
+                .s_addr = htonl(INADDR_LOOPBACK),
+        };
+        struct in_addr address_any = {
+                .s_addr = htonl(INADDR_ANY),
+        };
 
         /* attach to loopback interface */
         assert_se(sd_dhcp_server_new(&server, 1) >= 0);
@@ -48,6 +54,11 @@ static void test_basic(sd_event *event) {
         assert_se(sd_dhcp_server_ref(server) == server);
         assert_se(!sd_dhcp_server_unref(server));
 
+        assert_se(sd_dhcp_server_start(server) == -EBUSY);
+        assert_se(sd_dhcp_server_set_address(server, &address_any) == -EINVAL);
+        assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
+        assert_se(sd_dhcp_server_set_address(server, &address_lo) == -EBUSY);
+
         assert_se(sd_dhcp_server_start(server) >= 0);
         assert_se(sd_dhcp_server_start(server) == -EBUSY);
         assert_se(sd_dhcp_server_stop(server) >= 0);
@@ -74,8 +85,14 @@ static void test_message_handler(void) {
                 .option_type.type = DHCP_DISCOVER,
                 .end = DHCP_OPTION_END,
         };
+        struct in_addr address_lo = {
+                .s_addr = htonl(INADDR_LOOPBACK),
+        };
 
         assert_se(sd_dhcp_server_new(&server, 1) >= 0);
+        assert_se(sd_dhcp_server_set_address(server, &address_lo) >= 0);
+        assert_se(sd_dhcp_server_attach_event(server, NULL, 0) >= 0);
+        assert_se(sd_dhcp_server_start(server) >= 0);
 
         assert_se(dhcp_server_handle_message(server, (DHCPMessage*)&test, sizeof(test)) == 1);
 
diff --git a/src/systemd/sd-dhcp-server.h b/src/systemd/sd-dhcp-server.h
index ab63294..5edeffc 100644
--- a/src/systemd/sd-dhcp-server.h
+++ b/src/systemd/sd-dhcp-server.h
@@ -41,4 +41,5 @@ sd_event *sd_dhcp_server_get_event(sd_dhcp_server *client);
 int sd_dhcp_server_start(sd_dhcp_server *server);
 int sd_dhcp_server_stop(sd_dhcp_server *server);
 
+int sd_dhcp_server_set_address(sd_dhcp_server *server, struct in_addr *address);
 #endif
-- 
1.9.0



More information about the systemd-devel mailing list