@@ -15,6 +15,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <stdio.h>
#include <string.h>
#include <assert.h>
@@ -82,3 +83,27 @@ const char *hwaddr_preprocess(const char *mac, size_t *length)
return NULL;
}
}
+
+int hwaddr_from_pretty_str(const char *hwaddr_str, size_t hwaddr_len, uint8_t *hwaddr, size_t hwaddr_size)
+{
+ if (hwaddr_size < hwaddr_len)
+ return -1;
+
+ if (hwaddr_len == 6) {
+ sscanf(hwaddr_str,
+ "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
+ hwaddr, hwaddr + 1, hwaddr + 2,
+ hwaddr + 3, hwaddr + 4, hwaddr + 5);
+ } else if (hwaddr_len == 8) {
+ sscanf(hwaddr_str,
+ "%hhX:%hhX:%hhX:%hhX:%hhX:%hhX:%hhX:%hhX",
+ hwaddr, hwaddr + 1, hwaddr + 2,
+ hwaddr + 3, hwaddr + 4, hwaddr + 5,
+ hwaddr + 6, hwaddr + 7);
+ } else {
+ pb_debug("unsupported hwaddr format\n");
+ return -1;
+ }
+
+ return 0;
+}
@@ -56,5 +56,7 @@ int hwaddr_cmp(uint8_t *hwaddr1, size_t hwaddr1_len, uint8_t *hwaddr2, size_t hw
const char *hwaddr_preprocess(const char *mac, size_t *length);
+int hwaddr_from_pretty_str(const char *hwaddr_str, size_t hwaddr_len, uint8_t *hwaddr, size_t hwaddr_size);
+
#endif /* UTIL_H */
This function extracts a method used in several places to convert a pretty hardware address back to raw bytes. It also includes support for an 8-byte hardware address. Signed-off-by: Daniel M. Weeks <weeksd2@rpi.edu> --- lib/util/util.c | 25 +++++++++++++++++++++++++ lib/util/util.h | 2 ++ 2 files changed, 27 insertions(+)