[systemd-devel] [PATCH 08/11] libsystemd-dhcp: Add functions for sending unicast UDP messages
Patrik Flykt
patrik.flykt at linux.intel.com
Fri Dec 20 07:16:17 PST 2013
Create a helper functions setting up an unicast DHCP UDP socket and
sending data. Add function stubs for the test program.
---
src/libsystemd-dhcp/dhcp-internal.h | 3 +++
src/libsystemd-dhcp/dhcp-network.c | 38 ++++++++++++++++++++++++++++++++
src/libsystemd-dhcp/test-dhcp-client.c | 11 +++++++++
3 files changed, 52 insertions(+)
diff --git a/src/libsystemd-dhcp/dhcp-internal.h b/src/libsystemd-dhcp/dhcp-internal.h
index 4472d95..43b5b1d 100644
--- a/src/libsystemd-dhcp/dhcp-internal.h
+++ b/src/libsystemd-dhcp/dhcp-internal.h
@@ -29,8 +29,11 @@
#include "dhcp-protocol.h"
int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link);
+int dhcp_network_bind_udp_socket(int index, be32_t client_address);
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
const void *packet, size_t len);
+int dhcp_network_send_udp_socket(int s, be32_t server_address,
+ 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/libsystemd-dhcp/dhcp-network.c b/src/libsystemd-dhcp/dhcp-network.c
index 3ff2d0b..89350ca 100644
--- a/src/libsystemd-dhcp/dhcp-network.c
+++ b/src/libsystemd-dhcp/dhcp-network.c
@@ -54,6 +54,28 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
return s;
}
+int dhcp_network_bind_udp_socket(int index, be32_t client_address)
+{
+ int s, err;
+ union sockaddr_union src = {};
+
+ s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ if (s < 0)
+ return -errno;
+
+ src.in.sin_family = AF_INET;
+ src.in.sin_port = htobe16(DHCP_PORT_CLIENT);
+ src.in.sin_addr.s_addr = client_address;
+
+ if (bind(s, &src.sa, sizeof(src.in)) < 0) {
+ err = -errno;
+ close(s);
+ return err;
+ }
+
+ return s;
+}
+
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
const void *packet, size_t len)
{
@@ -64,3 +86,19 @@ int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
return err;
}
+
+int dhcp_network_send_udp_socket(int s, be32_t server_address,
+ const void *packet, size_t len)
+{
+ int err = 0;
+ union sockaddr_union dest = {};
+
+ dest.in.sin_family = AF_INET;
+ dest.in.sin_port = htobe16(DHCP_PORT_SERVER);
+ dest.in.sin_addr.s_addr = server_address;
+
+ if (sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in)) < 0)
+ err = -errno;
+
+ return err;
+}
diff --git a/src/libsystemd-dhcp/test-dhcp-client.c b/src/libsystemd-dhcp/test-dhcp-client.c
index 1ff78c1..617236b 100644
--- a/src/libsystemd-dhcp/test-dhcp-client.c
+++ b/src/libsystemd-dhcp/test-dhcp-client.c
@@ -184,6 +184,17 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
return test_fd[0];
}
+int dhcp_network_bind_udp_socket(int index, be32_t client_address)
+{
+ return 0;
+}
+
+int dhcp_network_send_udp_socket(int s, be32_t server_address,
+ const void *packet, size_t len)
+{
+ return 0;
+}
+
static void test_discover_message(sd_event *e)
{
sd_dhcp_client *client;
--
1.7.10.4
More information about the systemd-devel
mailing list