util: add a helper function to scan a MAC address.
Signed-off-by: Richard Cochran <richardcochran@gmail.com>master
parent
c05dcce724
commit
5fe77584b4
13
util.c
13
util.c
|
@ -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
10
util.h
|
@ -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.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue