lib: simplify str2mac and use struct ethaddr structure

This commit simplified the string to mac conversion, since it uses
sscanf, instead of depicting each incoming character one by one, and
doing self analysis. Also,this commit changes the internal usage of the
mac address representation in mac handling function.

Signed-off-by: Lou Berger <lberger@labn.net>
Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
This commit is contained in:
Philippe Guibert 2017-02-09 08:42:32 +01:00
parent 4e8e1df143
commit 3d5fc15be2
2 changed files with 19 additions and 89 deletions

View File

@ -1018,104 +1018,34 @@ inet6_ntoa (struct in6_addr addr)
return buf; return buf;
} }
static uint8_t convertchartohexa(uint8_t * hexa, int *error)
{
if ((*hexa == '0') || (*hexa == '1') || (*hexa == '2') ||
(*hexa == '3') || (*hexa == '4') || (*hexa == '5') ||
(*hexa == '6') || (*hexa == '7') || (*hexa == '8')
|| (*hexa == '9'))
return (uint8_t) (*hexa) - '0';
if ((*hexa == 'a') || (*hexa == 'A'))
return 0xa;
if ((*hexa == 'b') || (*hexa == 'B'))
return 0xb;
if ((*hexa == 'c') || (*hexa == 'C'))
return 0xc;
if ((*hexa == 'd') || (*hexa == 'D'))
return 0xd;
if ((*hexa == 'e') || (*hexa == 'E'))
return 0xe;
if ((*hexa == 'f') || (*hexa == 'F'))
return 0xf;
*error = -1;
return 0;
}
/* converts to internal representation of mac address /* converts to internal representation of mac address
* returns 1 on success, 0 otherwise * returns 1 on success, 0 otherwise
* format accepted: AA:BB:CC:DD:EE:FF * format accepted: AA:BB:CC:DD:EE:FF
* if mac parameter is null, then check only * if mac parameter is null, then check only
*/ */
int prefix_str2mac(const char *str, char *mac) int prefix_str2mac(const char *str, struct ethaddr *mac)
{ {
unsigned int k = 0, i, j; unsigned int a[6];
uint8_t *ptr, *ptr2; int i;
size_t len;
uint8_t car;
if (!str) if (!str)
return 0; return 0;
if (str[0] == ':' && str[1] == '\0') if (sscanf (str, "%2x:%2x:%2x:%2x:%2x:%2x",
return 1; a + 0, a + 1, a + 2, a + 3, a + 4, a + 5) != 6)
{
i = 0; /* error in incoming str length */
ptr = (uint8_t *) str;
while (i < 6) {
uint8_t temp[5];
int error = 0;
ptr2 = (uint8_t *) strchr((const char *)ptr, ':');
if (ptr2 == NULL) {
/* if last occurence return ok */
if (i != 5) {
zlog_err("[%s]: format non recognized", mac);
return 0;
}
len = strlen((char *)ptr);
} else {
len = ptr2 - ptr;
}
if (len > 5) {
zlog_err("[%s]: format non recognized", mac);
return 0; return 0;
} }
memcpy(temp, ptr, len); /* valid mac address */
for (j = 0; j < len; j++) { if (!mac)
if (k >= ETHER_ADDR_LEN) return 1;
return 0; for (i = 0; i < 6; ++i)
if (mac) mac->octet[i] = a[i] & 0xff;
mac[k] = 0;
car = convertchartohexa(&temp[j], &error);
if (error)
return 0;
if (mac)
mac[k] = car << 4;
j++;
if (j == len)
return 0;
car = convertchartohexa(&temp[j], &error) & 0xf;
if (error)
return 0;
if (mac)
mac[k] |= car & 0xf;
k++;
i++;
}
ptr = ptr2;
if (ptr == NULL)
break;
ptr++;
}
if (mac && 0) {
zlog_err("leave correct : %02x:%02x:%02x:%02x:%02x:%02x",
mac[0] & 0xff, mac[1] & 0xff, mac[2] & 0xff,
mac[3] & 0xff, mac[4] & 0xff, mac[5] & 0xff);
}
return 1; return 1;
} }
char *prefix_mac2str(const char *mac, char *buf, int size) char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size)
{ {
char *ptr; char *ptr;
@ -1129,8 +1059,8 @@ char *prefix_mac2str(const char *mac, char *buf, int size)
ptr = buf; ptr = buf;
} }
snprintf(ptr, (ETHER_ADDR_STRLEN), snprintf(ptr, (ETHER_ADDR_STRLEN),
"%02x:%02x:%02x:%02x:%02x:%02x", (uint8_t) mac[0], "%02x:%02x:%02x:%02x:%02x:%02x", (uint8_t) mac->octet[0],
(uint8_t) mac[1], (uint8_t) mac[2], (uint8_t) mac[3], (uint8_t) mac->octet[1], (uint8_t) mac->octet[2], (uint8_t) mac->octet[3],
(uint8_t) mac[4], (uint8_t) mac[5]); (uint8_t) mac->octet[4], (uint8_t) mac->octet[5]);
return ptr; return ptr;
} }

View File

@ -289,8 +289,8 @@ extern void masklen2ip6 (const int, struct in6_addr *);
extern void str2in6_addr (const char *, struct in6_addr *); extern void str2in6_addr (const char *, struct in6_addr *);
extern const char *inet6_ntoa (struct in6_addr); extern const char *inet6_ntoa (struct in6_addr);
extern int prefix_str2mac(const char *str, char *mac); extern int prefix_str2mac(const char *str, struct ethaddr *mac);
extern char *prefix_mac2str(const char *mac, char *buf, int size); extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
static inline int ipv6_martian (struct in6_addr *addr) static inline int ipv6_martian (struct in6_addr *addr)
{ {