[systemd-devel] [PATCH 08/11] libsystemd-dhcp: Add functions for sending unicast UDP messages

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Fri Dec 20 07:26:20 PST 2013


On Fri, Dec 20, 2013 at 05:16:17PM +0200, Patrik Flykt wrote:
> 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;
Minor thing, by why not write it as
{
        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;
}

Zbyszek


More information about the systemd-devel mailing list