util: never use ether_ntoa(), since it formats with %x, not %02x, which makes ethernet addresses look funny

This commit is contained in:
Lennart Poettering
2014-08-15 13:18:50 +02:00
parent c22bf27bee
commit db73295acc
5 changed files with 33 additions and 4 deletions

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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]);