mirror of
https://github.com/systemd/systemd.git
synced 2025-01-17 14:03:58 +08:00
libsystemd-dhcp: Add functions for sending unicast UDP messages
Create a helper functions setting up an unicast DHCP UDP socket and sending data. Add function stubs for the test program. [tomegun: initialize structs when allocating, and drop unneccesary 'err']
This commit is contained in:
parent
2fba7b03b5
commit
234fc2dfce
@ -29,8 +29,11 @@
|
|||||||
#include "dhcp-protocol.h"
|
#include "dhcp-protocol.h"
|
||||||
|
|
||||||
int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link);
|
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,
|
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
|
||||||
const void *packet, size_t len);
|
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,
|
int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
|
||||||
size_t optlen, const void *optval);
|
size_t optlen, const void *optval);
|
||||||
|
@ -53,6 +53,27 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int dhcp_network_bind_udp_socket(int index, be32_t client_address)
|
||||||
|
{
|
||||||
|
int s;
|
||||||
|
union sockaddr_union src = {
|
||||||
|
.in.sin_family = AF_INET,
|
||||||
|
.in.sin_port = htobe16(DHCP_PORT_CLIENT),
|
||||||
|
.in.sin_addr.s_addr = client_address,
|
||||||
|
};
|
||||||
|
|
||||||
|
s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
|
||||||
|
if (s < 0)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
if (bind(s, &src.sa, sizeof(src.in)) < 0) {
|
||||||
|
close_nointr_nofail(s);
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
|
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
|
||||||
const void *packet, size_t len)
|
const void *packet, size_t len)
|
||||||
{
|
{
|
||||||
@ -61,3 +82,18 @@ int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int dhcp_network_send_udp_socket(int s, be32_t server_address,
|
||||||
|
const void *packet, size_t len)
|
||||||
|
{
|
||||||
|
union sockaddr_union dest = {
|
||||||
|
.in.sin_family = AF_INET,
|
||||||
|
.in.sin_port = htobe16(DHCP_PORT_SERVER),
|
||||||
|
.in.sin_addr.s_addr = server_address,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in)) < 0)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@ -184,6 +184,17 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
|
|||||||
return test_fd[0];
|
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)
|
static void test_discover_message(sd_event *e)
|
||||||
{
|
{
|
||||||
sd_dhcp_client *client;
|
sd_dhcp_client *client;
|
||||||
|
Loading…
Reference in New Issue
Block a user