diff --git a/userspace/libwr/include/libwr/mac.h b/userspace/libwr/include/libwr/mac.h index df8b5c9795a04fd8ef85e55801ab95ac863b8bce..a61d1c0dd2caecb4239b705bbc5ffc0ccb86a5fd 100644 --- a/userspace/libwr/include/libwr/mac.h +++ b/userspace/libwr/include/libwr/mac.h @@ -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 */ diff --git a/userspace/libwr/mac.c b/userspace/libwr/mac.c index dc600d909ae55a8d534fbae5c3d5dbc67fdb01cd..b6207ac8b6c0e1b60da84ca5dbe4d1009401d9cf 100644 --- a/userspace/libwr/mac.c +++ b/userspace/libwr/mac.c @@ -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; +}