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. *