summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-05-25 00:29:13 +0200
committerTom Gundersen <teg@jklm.no>2014-06-13 17:07:19 +0200
commit20af7091de0cdf92bf299addfc3f96c3ef805bd8 (patch)
treeec3d865eee312619cd8a3f00336474489cd966d9
parent8de4a226c71ef43e652274b33b5d19211a44ac7b (diff)
sd-dhcp-server: add support for setting the server address
-rw-r--r--src/libsystemd-network/dhcp-server-internal.h1
-rw-r--r--src/libsystemd-network/sd-dhcp-server.c13
-rw-r--r--src/libsystemd-network/test-dhcp-server.c17
-rw-r--r--src/systemd/sd-dhcp-server.h1
4 files changed, 32 insertions, 0 deletions
diff --git a/src/libsystemd-network/dhcp-server-internal.h b/src/libsystemd-network/dhcp-server-internal.h
index 6c2f2b4070..58a98772ba 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 ecdc15dff7..37f81588c3 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), -EUNATCH);
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 dd0f29a04c..f0f9cfe582 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) == -EUNATCH);
+ 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 ab63294ac5..5edeffc31c 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