[syslog-ng] [PATCH] db-parser(): implement a MAC address parser

Balint Kovacs balint.kovacs at balabit.com
Wed Aug 15 19:28:03 CEST 2012


From: Balint Kovacs <blint at blint.hu>

The MACADDR parser matches the standard format for printing MAC-48
addresses in human-friendly form, whis consists of is six groups of
two hexadecimal digits, separated colons (:)

Signed-off-by: Balint Kovacs <blint at balabit.hu>
---
 modules/dbparser/radix.c            |   33 +++++++++++++++++++++++++++++++++
 modules/dbparser/radix.h            |    5 ++++-
 modules/dbparser/tests/test_radix.c |    4 ++++
 3 files changed, 41 insertions(+), 1 deletions(-)

diff --git a/modules/dbparser/radix.c b/modules/dbparser/radix.c
index 2f2034b..fb74c20 100644
--- a/modules/dbparser/radix.c
+++ b/modules/dbparser/radix.c
@@ -254,6 +254,34 @@ r_parser_ip(guint8 *str, gint *len, const gchar *param, gpointer state, RParserM
 }
 
 gboolean
+r_parser_macaddr(guint8 *str, gint *len, const gchar *param, gpointer state, RParserMatch *match)
+{
+  gint i;
+  *len = 0;
+
+  for (i = 1; i <= 6; i++)
+    {
+      if (!g_ascii_isxdigit(str[*len]) && !g_ascii_isxdigit(str[*len+1]))
+        {
+          return FALSE;
+        }
+      if (i<6)
+        {
+          if (str[*len+2] != ':')
+            return FALSE;
+          (*len)+=3;
+        }
+      else
+        (*len)+=2;
+    }
+
+  if (G_UNLIKELY(*len == 16))
+    return FALSE;
+
+  return TRUE;
+}
+
+gboolean
 r_parser_float(guint8 *str, gint *len, const gchar *param, gpointer state, RParserMatch *match)
 {
   gboolean dot = FALSE;
@@ -349,6 +377,11 @@ r_new_pnode(guint8 *key)
       parser_node->parse = r_parser_ip;
       parser_node->type = RPT_IP;
     }
+  else if (strcmp(params[0], "MACADDR") == 0)
+    {
+      parser_node->parse = r_parser_macaddr;
+      parser_node->type = RPT_MACADDR;
+    }
   else if (strcmp(params[0], "NUMBER") == 0)
     {
       parser_node->parse = r_parser_number;
diff --git a/modules/dbparser/radix.h b/modules/dbparser/radix.h
index 9756dfe..c36fb63 100644
--- a/modules/dbparser/radix.h
+++ b/modules/dbparser/radix.h
@@ -41,7 +41,8 @@ enum
   RPT_IPV6,
   RPT_IP,
   RPT_FLOAT,
-  RPT_SET
+  RPT_SET,
+  RPT_MACADDR
 };
 
 typedef struct _RParserMatch
@@ -127,6 +128,8 @@ r_parser_type_name(guint8 type)
         return "FLOAT";
       case RPT_SET:
         return "SET";
+      case RPT_MACADDR:
+        return "MACADDR";
       default:
         return "UNKNOWN";
     }
diff --git a/modules/dbparser/tests/test_radix.c b/modules/dbparser/tests/test_radix.c
index b6c9736..aafa698 100644
--- a/modules/dbparser/tests/test_radix.c
+++ b/modules/dbparser/tests/test_radix.c
@@ -302,6 +302,7 @@ test_parsers(void)
   insert_node(root, "AAA at SET@AAA");
   printf("We excpect an error message\n");
   insert_node(root, "AAA at SET:set at AAA");
+  insert_node(root, "AAA at MACADDR@AAA");
 
   test_search_value(root, "a@", NULL);
   test_search_value(root, "a at NUMBER@aa@@", "a@@NUMBER@@aa@@@@");
@@ -343,6 +344,7 @@ test_matches(void)
   insert_node(root, "fff @FLOAT:float@");
   insert_node(root, "zzz @ESTRING:test:gép@");
   insert_node(root, "ggg @SET:set: 	@");
+  insert_node(root, "iii @MACADDR:macaddr@");
 
   test_search_matches(root, "aaa 12345 hihihi",
                       "number", "12345",
@@ -613,6 +615,8 @@ test_matches(void)
   test_search_matches(root, "ggg  aaa", "set", " ", NULL);
   test_search_matches(root, "ggg   aaa", "set", "  ", NULL);
   test_search_matches(root, "ggg 	aaa", "set", "	", NULL);
+  test_search_matches(root, "iii 82:63:25:93:eb:51.iii", "macaddr", "82:63:25:93:eb:51", NULL);
+  test_search_matches(root, "iii 82:63:25:93:EB:51.iii", "macaddr", "82:63:25:93:EB:51", NULL);
   test_search_matches(root, "zzz árvíztűrőtükörfúrógép", "test", "árvíztűrőtükörfúró", NULL);
 
   r_free_node(root, NULL);
-- 
1.7.0.4




More information about the syslog-ng mailing list