Add net_utils.c module with str2mac & str2addr helper functions.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 net_utils.c | 41 +++++++++++++++++++++++++++++++++++++++++
 net_utils.h |  7 +++++++
 2 files changed, 48 insertions(+)
 create mode 100644 net_utils.c
 create mode 100644 net_utils.h

diff --git a/net_utils.c b/net_utils.c
new file mode 100644
index 0000000..749199b
--- /dev/null
+++ b/net_utils.c
@@ -0,0 +1,41 @@
+/*
+ * netsniff-ng - the packet sniffing beast
+ * Subject to the GPL, version 2.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <arpa/inet.h>
+
+int str2mac(char *str, uint8_t *mac)
+{
+       int i, count;
+       unsigned int tmp[6];
+
+       if (!str)
+              return -1;
+
+       count = sscanf(str, "%02X:%02X:%02X:%02X:%02X:%02X",
+                       &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
+
+       if (count != 6)
+               count = sscanf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
+                       &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
+
+       if (count != 6)
+               return -1;
+
+       for (i = 0; i < 6; i++)
+               mac[i] = (uint8_t)tmp[i];
+
+       return 0;
+}
+
+int str2addr(int af, const char *str, uint8_t *addr)
+{
+       if (inet_pton(af, str, addr))
+               return 0;
+
+       return -1;
+}
diff --git a/net_utils.h b/net_utils.h
new file mode 100644
index 0000000..5bd7b7e
--- /dev/null
+++ b/net_utils.h
@@ -0,0 +1,7 @@
+#ifndef NET_UTILS_I_H
+#define NET_UTILS_I_H
+
+int str2mac(char *str, uint8_t *mac);
+int str2addr(int af, const char *str, uint8_t *addr);
+
+#endif
-- 
2.4.2

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to