From 63b5c60743791d6e92682fe708515a35004a18b8 Mon Sep 17 00:00:00 2001 From: Denis Kenzior Date: Wed, 24 Aug 2016 21:31:54 -0500 Subject: [PATCH] util: Add util_string_to_address --- src/util.c | 34 ++++++++++++++++++++++++++++++++++ src/util.h | 1 + 2 files changed, 35 insertions(+) diff --git a/src/util.c b/src/util.c index e8c7c376..3ab4c4f2 100644 --- a/src/util.c +++ b/src/util.c @@ -97,6 +97,40 @@ const char *util_address_to_string(const uint8_t *addr) return str; } +bool util_string_to_address(const char *str, uint8_t *addr) +{ + unsigned int i; + + if (!str) + return false; + + if (strlen(str) != 17) + return false; + + for (i = 0; i < 15; i += 3) { + if (!l_ascii_isxdigit(str[i])) + return false; + + if (!l_ascii_isxdigit(str[i + 1])) + return false; + + if (str[i + 2] != ':') + return false; + } + + if (!l_ascii_isxdigit(str[i])) + return false; + + if (!l_ascii_isxdigit(str[i + 1])) + return false; + + sscanf(str, "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx", + &addr[0], &addr[1], &addr[2], + &addr[3], &addr[4], &addr[5]); + + return true; +} + bool _msg_append_attr(struct l_genl_msg *msg, uint16_t type, const char *type_str, uint16_t len, const void *value) diff --git a/src/util.h b/src/util.h index 780e228e..c8d418b0 100644 --- a/src/util.h +++ b/src/util.h @@ -29,6 +29,7 @@ const char *util_ssid_to_utf8(size_t len, const uint8_t *ssid); bool util_ssid_is_utf8(size_t len, const uint8_t *ssid); const char *util_address_to_string(const uint8_t *addr); +bool util_string_to_address(const char *str, uint8_t *addr); static inline uint8_t util_bit_field(const uint8_t oct, int start, int num) {