[systemd-devel] [PATCH] networkd DHCPv4 logging endian fix

Lennart Poettering lennart at poettering.net
Wed Feb 11 03:59:24 PST 2015


On Wed, 11.02.15 11:50, Paul Martin (paul.martin at codethink.co.uk) wrote:

> On Tue, Feb 10, 2015 at 08:10:43PM +0100, Lennart Poettering wrote:
> 
> > Hmm, I think it would be nicer to use be32toh() here instead, since it
> > ensures the macro is (to a limited degree) typesafe.
> > 
> > Any chance you could rework that?
> 
> From: Paul Martin <paul.martin at codethink.co.uk>
> Date: Wed, 11 Feb 2015 11:47:16 +0000
> Subject: [PATCH] networkd dhcpv4 logging endian fix
> 
> On a big-endian host, systemd-networkd prints out IPv4 network
> addresses byte reversed:

Applied. Thansk!

> 
> Feb 10 16:43:32 hostname systemd-networkd[151]: eth0 : DHCPv4 address 158.1.24.10/16 via 1.1.24.10
> 
> The address obtained is 10.24.1.158/16 and the route is
> 
>   10.24.0.0/16 dev eth0  src 10.24.1.187
> 
> The macro ADDRESS_FMT_VAL() unpacks a "struct in_addr" in a
> little-endian specific manner.
> 
> This patch forces the passed address into host order, then unpacks it.
> 
> On an x86 later than i486, compiled with -O2, the only extra overhead
> is a single bswap instruction.
> 
> ---
>  src/network/networkd-link.h | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
> 
> diff --git a/src/network/networkd-link.h b/src/network/networkd-link.h
> index 449dbc8..dbbda02 100644
> --- a/src/network/networkd-link.h
> +++ b/src/network/networkd-link.h
> @@ -22,6 +22,7 @@
>  #pragma once
>  
>  #include "networkd.h"
> +#include <endian.h>
>  
>  typedef enum LinkState {
>          LINK_STATE_PENDING,
> @@ -152,8 +153,8 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_unref);
>  
>  #define log_link_struct(link, level, ...) log_struct(level, "INTERFACE=%s", link->ifname, __VA_ARGS__)
>  
> -#define ADDRESS_FMT_VAL(address)            \
> -        (address).s_addr & 0xFF,            \
> -        ((address).s_addr >> 8) & 0xFF,     \
> -        ((address).s_addr >> 16) & 0xFF,    \
> -        (address).s_addr >> 24
> +#define ADDRESS_FMT_VAL(address)                   \
> +        be32toh((address).s_addr) >> 24,           \
> +        (be32toh((address).s_addr) >> 16) & 0xFFu, \
> +        (be32toh((address).s_addr) >> 8) & 0xFFu,  \
> +        be32toh((address).s_addr) & 0xFFu
> -- 
> 2.1.4
> 
> 
> 
> -- 
> Paul Martin                                  http://www.codethink.co.uk/
> Senior Software Developer
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel


Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list