util: add a helper function to scan a MAC address.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2015-08-21 22:34:45 +02:00
parent c05dcce724
commit 5fe77584b4
2 changed files with 23 additions and 0 deletions

13
util.c
View File

@ -88,6 +88,19 @@ char *pid2str(struct PortIdentity *id)
return buf; 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) int str2pid(const char *s, struct PortIdentity *result)
{ {
struct PortIdentity pid; struct PortIdentity pid;

10
util.h
View File

@ -21,6 +21,7 @@
#define HAVE_UTIL_H #define HAVE_UTIL_H
#include "ddt.h" #include "ddt.h"
#include "ether.h"
/** /**
* Table of human readable strings, one for each port state. * Table of human readable strings, one for each port state.
@ -54,6 +55,15 @@ char *cid2str(struct ClockIdentity *id);
*/ */
char *pid2str(struct PortIdentity *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. * Scan a string containing a port identity and convert it into binary form.
* *