From db73295accbec0c6513817f0a64a92018592bb26 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 15 Aug 2014 13:18:50 +0200 Subject: [PATCH] util: never use ether_ntoa(), since it formats with %x, not %02x, which makes ethernet addresses look funny --- src/libsystemd-network/sd-dhcp-client.c | 3 ++- src/libsystemd/sd-rtnl/test-rtnl.c | 3 ++- src/network/networkctl.c | 7 +++++-- src/shared/socket-util.c | 19 +++++++++++++++++++ src/shared/socket-util.h | 5 +++++ 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index f5910d9..8a9887d 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -1420,6 +1420,7 @@ static int client_receive_message_raw(sd_event_source *s, int fd, } int sd_dhcp_client_start(sd_dhcp_client *client) { + char buffer[ETHER_ADDR_TO_STRING_MAX]; int r; assert_return(client, -EINVAL); @@ -1435,7 +1436,7 @@ int sd_dhcp_client_start(sd_dhcp_client *client) { if (r >= 0) log_dhcp_client(client, "STARTED on ifindex %u with address %s", client->index, - ether_ntoa(&client->client_id.mac_addr)); + ether_addr_to_string(&client->client_id.mac_addr, buffer)); return r; } diff --git a/src/libsystemd/sd-rtnl/test-rtnl.c b/src/libsystemd/sd-rtnl/test-rtnl.c index 4b6e533..46b5bb2 100644 --- a/src/libsystemd/sd-rtnl/test-rtnl.c +++ b/src/libsystemd/sd-rtnl/test-rtnl.c @@ -34,6 +34,7 @@ static void test_link_configure(sd_rtnl *rtnl, int ifindex) { _cleanup_rtnl_message_unref_ sd_rtnl_message *message; const char *mac = "98:fe:94:3f:c6:18", *name = "test"; + char buffer[ETHER_ADDR_TO_STRING_MAX]; unsigned int mtu = 1450, mtu_out; const char *name_out; struct ether_addr mac_out; @@ -51,7 +52,7 @@ static void test_link_configure(sd_rtnl *rtnl, int ifindex) { assert_se(streq(name, name_out)); assert_se(sd_rtnl_message_read_ether_addr(message, IFLA_ADDRESS, &mac_out) >= 0); - assert_se(streq(mac, ether_ntoa(&mac_out))); + assert_se(streq(mac, ether_addr_to_string(&mac_out, buffer))); assert_se(sd_rtnl_message_read_u32(message, IFLA_MTU, &mtu_out) >= 0); assert_se(mtu == mtu_out); diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 620bc46..33fe8bf 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -33,6 +33,7 @@ #include "udev-util.h" #include "arphrd-list.h" #include "local-addresses.h" +#include "socket-util.h" static bool arg_no_pager = false; static bool arg_legend = true; @@ -402,8 +403,10 @@ static int link_status_one(sd_rtnl *rtnl, struct udev *udev, const char *name) { if (model) printf(" Model: %s\n", model); - if (have_mac) - printf(" HW Address: %s\n", ether_ntoa(&e)); + if (have_mac) { + char ea[ETHER_ADDR_TO_STRING_MAX]; + printf(" HW Address: %s\n", ether_addr_to_string(&e, ea)); + } if (mtu > 0) printf(" MTU: %u\n", mtu); diff --git a/src/shared/socket-util.c b/src/shared/socket-util.c index 38729a2..e3e54e8 100644 --- a/src/shared/socket-util.c +++ b/src/shared/socket-util.c @@ -727,3 +727,22 @@ bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b return false; } + +char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]) { + assert(addr); + assert(buffer); + + /* Like ether_ntoa() but uses %02x instead of %x to print + * ethernet addresses, which makes them look less funny. Also, + * doesn't use a static buffer. */ + + sprintf(buffer, "%02x:%02x:%02x:%02x:%02x:%02x", + addr->ether_addr_octet[0], + addr->ether_addr_octet[1], + addr->ether_addr_octet[2], + addr->ether_addr_octet[3], + addr->ether_addr_octet[4], + addr->ether_addr_octet[5]); + + return buffer; +} diff --git a/src/shared/socket-util.h b/src/shared/socket-util.h index 0b4998d..07d0aff 100644 --- a/src/shared/socket-util.h +++ b/src/shared/socket-util.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -111,3 +112,7 @@ int netlink_family_to_string_alloc(int b, char **s); int netlink_family_from_string(const char *s) _pure_; bool sockaddr_equal(const union sockaddr_union *a, const union sockaddr_union *b); + +#define ETHER_ADDR_TO_STRING_MAX (3*6) + +char* ether_addr_to_string(const struct ether_addr *addr, char buffer[ETHER_ADDR_TO_STRING_MAX]); -- 2.7.4