diff options
author | André Fabian Silva Delgado <emulatorman@parabola.nu> | 2015-08-05 17:04:01 -0300 |
---|---|---|
committer | André Fabian Silva Delgado <emulatorman@parabola.nu> | 2015-08-05 17:04:01 -0300 |
commit | 57f0f512b273f60d52568b8c6b77e17f5636edc0 (patch) | |
tree | 5e910f0e82173f4ef4f51111366a3f1299037a7b /lib/net_utils.c |
Initial import
Diffstat (limited to 'lib/net_utils.c')
-rw-r--r-- | lib/net_utils.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/net_utils.c b/lib/net_utils.c new file mode 100644 index 000000000..148fc6e99 --- /dev/null +++ b/lib/net_utils.c @@ -0,0 +1,26 @@ +#include <linux/string.h> +#include <linux/if_ether.h> +#include <linux/ctype.h> +#include <linux/kernel.h> + +bool mac_pton(const char *s, u8 *mac) +{ + int i; + + /* XX:XX:XX:XX:XX:XX */ + if (strlen(s) < 3 * ETH_ALEN - 1) + return false; + + /* Don't dirty result unless string is valid MAC. */ + for (i = 0; i < ETH_ALEN; i++) { + if (!isxdigit(s[i * 3]) || !isxdigit(s[i * 3 + 1])) + return false; + if (i != ETH_ALEN - 1 && s[i * 3 + 2] != ':') + return false; + } + for (i = 0; i < ETH_ALEN; i++) { + mac[i] = (hex_to_bin(s[i * 3]) << 4) | hex_to_bin(s[i * 3 + 1]); + } + return true; +} +EXPORT_SYMBOL(mac_pton); |