libusbgx: print leading zero for MAC address bytes
authorStefan Agner <stefan@agner.ch>
Fri, 24 Apr 2015 19:27:53 +0000 (21:27 +0200)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 22 Dec 2015 20:45:36 +0000 (21:45 +0100)
The ethernet gadget driver requires the hex formatted MAC address
bytes with leading zero, in other words each byte needs to be two
characters in length (see get_ether_addr in u_ether.c). The libc
implementation ether_ntoa does not print leading zeros. Hence use
our own implementation which provides the format expected by the
kernel.

Signed-off-by: Stefan Agner <stefan@agner.ch>
[Rebased onto current master and update description]
Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
include/usbg/usbg_internal.h
src/usbg.c
src/usbg_schemes_libconfig.c

index 30098c9..30d3fcf 100644 (file)
@@ -168,5 +168,7 @@ static inline int file_select(const struct dirent *dent)
 
 int usbg_translate_error(int error);
 
+char *usbg_ether_ntoa_r(const struct ether_addr *addr, char *buf);
+
 #endif /* USBG_INTERNAL_H */
 
index d8b00c9..6239c60 100644 (file)
@@ -852,6 +852,15 @@ static int usbg_rm_all_dirs(const char *path)
        return ret;
 }
 
+char *usbg_ether_ntoa_r(const struct ether_addr *addr, char *buf)
+{
+       sprintf(buf, "%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 buf;
+}
+
 static int usbg_parse_function_net_attrs(usbg_function *f,
                usbg_f_net_attrs *f_net_attrs)
 {
@@ -2928,12 +2937,12 @@ int usbg_set_function_net_attrs(usbg_function *f, const usbg_f_net_attrs *attrs)
                goto out;
        }
 
-       addr = ether_ntoa_r(&attrs->dev_addr, addr_buf);
+       addr = usbg_ether_ntoa_r(&attrs->dev_addr, addr_buf);
        ret = usbg_write_string(f->path, f->name, "dev_addr", addr);
        if (ret != USBG_SUCCESS)
                goto out;
 
-       addr = ether_ntoa_r(&attrs->host_addr, addr_buf);
+       addr = usbg_ether_ntoa_r(&attrs->host_addr, addr_buf);
        ret = usbg_write_string(f->path, f->name, "host_addr", addr);
        if (ret != USBG_SUCCESS)
                goto out;
@@ -3171,7 +3180,7 @@ int usbg_set_net_dev_addr(usbg_function *f, struct ether_addr *dev_addr)
 
        if (f && dev_addr) {
                char str_buf[USBG_MAX_STR_LENGTH];
-               char *str_addr = ether_ntoa_r(dev_addr, str_buf);
+               char *str_addr = usbg_ether_ntoa_r(dev_addr, str_buf);
                ret = usbg_write_string(f->path, f->name, "dev_addr", str_addr);
        } else {
                ret = USBG_ERROR_INVALID_PARAM;
@@ -3186,7 +3195,7 @@ int usbg_set_net_host_addr(usbg_function *f, struct ether_addr *host_addr)
 
        if (f && host_addr) {
                char str_buf[USBG_MAX_STR_LENGTH];
-               char *str_addr = ether_ntoa_r(host_addr, str_buf);
+               char *str_addr = usbg_ether_ntoa_r(host_addr, str_buf);
                ret = usbg_write_string(f->path, f->name, "host_addr", str_addr);
        } else {
                ret = USBG_ERROR_INVALID_PARAM;
index 212fc2d..1cbf3c4 100644 (file)
@@ -326,7 +326,7 @@ static int usbg_export_f_net_attrs(usbg_f_net_attrs *attrs,
        if (!node)
                goto out;
 
-       addr = ether_ntoa_r(&attrs->dev_addr, addr_buf);
+       addr = usbg_ether_ntoa_r(&attrs->dev_addr, addr_buf);
        cfg_ret = config_setting_set_string(node, addr);
        if (cfg_ret != CONFIG_TRUE) {
                ret = USBG_ERROR_OTHER_ERROR;
@@ -337,7 +337,7 @@ static int usbg_export_f_net_attrs(usbg_f_net_attrs *attrs,
        if (!node)
                goto out;
 
-       addr = ether_ntoa_r(&attrs->host_addr, addr_buf);
+       addr = usbg_ether_ntoa_r(&attrs->host_addr, addr_buf);
        cfg_ret = config_setting_set_string(node, addr);
        if (cfg_ret != CONFIG_TRUE) {
                ret = USBG_ERROR_OTHER_ERROR;