summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilippe Guibert <philippe.guibert@6wind.com>2017-02-09 08:42:32 +0100
committerPhilippe Guibert <philippe.guibert@6wind.com>2017-02-09 09:02:41 +0100
commit3d5fc15be24e026472e259d02564dae1d73d3af5 (patch)
tree8a8ed43221bbdf5f1e8caacab7d621aae45d7005
parent4e8e1df14390b20c18397fc5643e11cc35942921 (diff)
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>
-rw-r--r--lib/prefix.c104
-rw-r--r--lib/prefix.h4
2 files changed, 19 insertions, 89 deletions
diff --git a/lib/prefix.c b/lib/prefix.c
index b2d3b67586..881db442bc 100644
--- a/lib/prefix.c
+++ b/lib/prefix.c
@@ -1018,104 +1018,34 @@ inet6_ntoa (struct in6_addr addr)
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
* returns 1 on success, 0 otherwise
* format accepted: AA:BB:CC:DD:EE:FF
* 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;
- uint8_t *ptr, *ptr2;
- size_t len;
- uint8_t car;
-
+ unsigned int a[6];
+ int i;
+
if (!str)
return 0;
- if (str[0] == ':' && str[1] == '\0')
- return 1;
-
- i = 0;
- 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);
+ if (sscanf (str, "%2x:%2x:%2x:%2x:%2x:%2x",
+ a + 0, a + 1, a + 2, a + 3, a + 4, a + 5) != 6)
+ {
+ /* error in incoming str length */
return 0;
}
- memcpy(temp, ptr, len);
- for (j = 0; j < len; j++) {
- if (k >= ETHER_ADDR_LEN)
- return 0;
- if (mac)
- 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);
- }
+ /* valid mac address */
+ if (!mac)
+ return 1;
+ for (i = 0; i < 6; ++i)
+ mac->octet[i] = a[i] & 0xff;
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;
@@ -1129,8 +1059,8 @@ char *prefix_mac2str(const char *mac, char *buf, int size)
ptr = buf;
}
snprintf(ptr, (ETHER_ADDR_STRLEN),
- "%02x:%02x:%02x:%02x:%02x:%02x", (uint8_t) mac[0],
- (uint8_t) mac[1], (uint8_t) mac[2], (uint8_t) mac[3],
- (uint8_t) mac[4], (uint8_t) mac[5]);
+ "%02x:%02x:%02x:%02x:%02x:%02x", (uint8_t) mac->octet[0],
+ (uint8_t) mac->octet[1], (uint8_t) mac->octet[2], (uint8_t) mac->octet[3],
+ (uint8_t) mac->octet[4], (uint8_t) mac->octet[5]);
return ptr;
}
diff --git a/lib/prefix.h b/lib/prefix.h
index 2c2b37dabb..cb0cfd2d45 100644
--- a/lib/prefix.h
+++ b/lib/prefix.h
@@ -289,8 +289,8 @@ extern void masklen2ip6 (const int, struct in6_addr *);
extern void str2in6_addr (const char *, struct in6_addr *);
extern const char *inet6_ntoa (struct in6_addr);
-extern int prefix_str2mac(const char *str, char *mac);
-extern char *prefix_mac2str(const char *mac, char *buf, int size);
+extern int prefix_str2mac(const char *str, struct ethaddr *mac);
+extern char *prefix_mac2str(const struct ethaddr *mac, char *buf, int size);
static inline int ipv6_martian (struct in6_addr *addr)
{