Commit ccb27399 authored by Adam Wujek's avatar Adam Wujek 💬

userspace/libwr: add mac_verify to mac.c

Add a function to verify read MAC address.
Signed-off-by: Adam Wujek's avatarAdam Wujek <adam.wujek@cern.ch>
parent db93166f
......@@ -64,5 +64,6 @@ static inline uint8_t *mac_clean(uint8_t mac[ETH_ALEN])
char *mac_to_string(uint8_t mac[ETH_ALEN]);
char *mac_to_buffer(uint8_t mac[ETH_ALEN], char buffer[ETH_ALEN_STR]);
int mac_from_str(uint8_t *tomac, const char *fromstr);
int mac_verify(char *mac_str);
#endif /* __WHITERABBIT_RTU_MAC_H */
......@@ -61,3 +61,27 @@ int mac_from_str(uint8_t *tomac, const char *fromstr)
return sscanf(fromstr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", tomac + 0,
tomac + 1, tomac + 2, tomac + 3, tomac + 4, tomac + 5);
}
/**
* \brief Function to verify correctness of MAC address
*/
int mac_verify(char *mac_str)
{
uint8_t mac[ETH_ALEN];
char buffer[ETH_ALEN_STR];
/* Check the length of a string containing MAC */
if (ETH_ALEN_STR != strnlen(mac_str, ETH_ALEN_STR + 1)) {
/* Convert string to mac */
mac_from_str(mac, mac_str);
/* Convert mac to string */
mac_to_buffer(mac, buffer);
/* Compare original string with the converted back and forth
* they should be the same */
if (!memcmp(mac_str, buffer, ETH_ALEN_STR)) {
/* MAC is ok */
return 0;
}
}
return -1;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment