From 5fe77584b414904620806fdb0343a91f38a16d6c Mon Sep 17 00:00:00 2001 From: Richard Cochran Date: Fri, 21 Aug 2015 22:34:45 +0200 Subject: [PATCH] util: add a helper function to scan a MAC address. Signed-off-by: Richard Cochran --- util.c | 13 +++++++++++++ util.h | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/util.c b/util.c index 06c3296..1fa8336 100644 --- a/util.c +++ b/util.c @@ -88,6 +88,19 @@ char *pid2str(struct PortIdentity *id) return buf; } +int str2mac(const char *s, unsigned char mac[MAC_LEN]) +{ + unsigned char buf[MAC_LEN]; + int c; + c = sscanf(s, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", + &buf[0], &buf[1], &buf[2], &buf[3], &buf[4], &buf[5]); + if (c != MAC_LEN) { + return -1; + } + memcpy(mac, buf, MAC_LEN); + return 0; +} + int str2pid(const char *s, struct PortIdentity *result) { struct PortIdentity pid; diff --git a/util.h b/util.h index 98b395b..734ea05 100644 --- a/util.h +++ b/util.h @@ -21,6 +21,7 @@ #define HAVE_UTIL_H #include "ddt.h" +#include "ether.h" /** * Table of human readable strings, one for each port state. @@ -54,6 +55,15 @@ char *cid2str(struct ClockIdentity *id); */ char *pid2str(struct PortIdentity *id); +/** + * Scan a string containing a MAC address and convert it into binary form. + * + * @param s String in human readable form. + * @param mac Pointer to a buffer to hold the result. + * @return Zero on success, or -1 if the string is incorrectly formatted. + */ +int str2mac(const char *s, unsigned char mac[MAC_LEN]); + /** * Scan a string containing a port identity and convert it into binary form. *