summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-08-15 13:18:50 +0200
committerLennart Poettering <lennart@poettering.net>2014-08-15 13:19:03 +0200
commitdb73295accbec0c6513817f0a64a92018592bb26 (patch)
tree3199040de4a794dc8020c56cab5d8bb8e1155801 /src
parentc22bf27beea56516d8f83784759fb30495e058d7 (diff)
util: never use ether_ntoa(), since it formats with %x, not %02x, which makes ethernet addresses look funny
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd-network/sd-dhcp-client.c3
-rw-r--r--src/libsystemd/sd-rtnl/test-rtnl.c3
-rw-r--r--src/network/networkctl.c7
-rw-r--r--src/shared/socket-util.c19
-rw-r--r--src/shared/socket-util.h5
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 f5910d9e0b..8a9887d19f 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 4b6e5339d9..46b5bb20c3 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 620bc46b8f..33fe8bfc61 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 38729a25b8..e3e54e8e08 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 0b4998d044..07d0aff72b 100644
--- a/src/shared/socket-util.h
+++ b/src/shared/socket-util.h
@@ -23,6 +23,7 @@
#include <sys/socket.h>
#include <netinet/in.h>
+#include <netinet/ether.h>
#include <sys/un.h>
#include <asm/types.h>
#include <linux/netlink.h>
@@ -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]);