Completion of constraints code, not fully tested.
[trust_router.git] / common / tr_config.c
index d101d9b..4f90e16 100644 (file)
@@ -40,7 +40,7 @@
 #include <tr_config.h>
 #include <tr.h>
 #include <tr_filter.h>
-
+#include <tr_constraint.h>
 void tr_print_config (FILE *stream, TR_CFG *cfg) {
   fprintf(stream, "tr_print_config: Not yet implemented.\n");
   return;
@@ -109,17 +109,40 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
   }
 }
 
-static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_INSTANCE *tr, const char *ctype, json_t *jdc, TR_CFG_RC *rc)
+static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_INSTANCE *tr, char *ctype, json_t *jc, TR_CFG_RC *rc)
 {
+  TR_CONSTRAINT *cons;
+  int i;
+
+  if ((!tr) || (!ctype) || (!jc) || (!rc) ||
+      (!json_is_array(jc)) ||
+      (0 >= json_array_size(jc)) ||
+      (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
+      (!json_is_string(json_array_get(jc, 0)))) {
+    fprintf(stderr, "tr_cfg_parse_one_constraint: config error.\n");
+    *rc = TR_CFG_NOPARSE;
+    return NULL;
+  }
+
   if (NULL == (cons = malloc(sizeof(TR_CONSTRAINT)))) {
-    fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory.\n");
+    fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (cons).\n");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
 
-  memset(filt, 0, sizeof(TR_FILTER));
+  memset(cons, 0, sizeof(TR_CONSTRAINT));
 
-  
+  if (NULL == (cons->type = tr_new_name(ctype))) {
+    fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (type).\n");
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
+
+  for (i = 0; i < json_array_size(jc); i++) {
+    cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i))));
+  }
+
+  return cons;
 }
 
 static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
@@ -172,7 +195,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CF
     tr_filter_free(filt);
     return NULL;
   }
-  
+
   /* For each filter line... */
   for (i = 0; i < json_array_size(jfls); i++) {
 
@@ -184,26 +207,6 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CF
       return NULL;
     }
  
-    if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
-       (json_is_array(jrc)) &&
-       (0 != json_array_size(jrc)) &&
-       (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
-      if (NULL == (filt->realm_cons = tr_cfg_parse_one_constraint(tr, "realm", jrc, rc)))
-       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
-      tr_filter_free(filt);
-      return NULL;
-    }
-
-    if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
-       (json_is_array(jdc)) &&
-       (0 != json_array_size(jdc)) &&
-       (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
-      if (NULL == (filt->realm_cons = tr_cfg_parse_one_constraint(tr, "domain", jdc, rc)))
-       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
-      tr_filter_free(filt);
-      return NULL;
-    }
-
     if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
        (!json_is_array(jfspecs)) ||
        (0 == json_array_size(jfspecs))) {
@@ -221,7 +224,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CF
     }
 
     if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
-      fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
+      fprintf(stderr, "tr_config_parse_one_filter: Out of memory (fline).\n");
       *rc = TR_CFG_NOMEM;
       tr_filter_free(filt);
       return NULL;
@@ -241,7 +244,31 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CF
       tr_filter_free(filt);
       return NULL;
     }
-      
+
+    if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) &&
+       (json_is_array(jrc)) &&
+       (0 != json_array_size(jrc)) &&
+       (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) {
+
+      if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(tr, "realm", jrc, rc))) {
+       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint");
+      tr_filter_free(filt);
+      return NULL;
+      }
+    }
+
+    if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) &&
+       (json_is_array(jdc)) &&
+       (0 != json_array_size(jdc)) &&
+       (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) {
+
+      if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(tr, "domain", jdc, rc))) {
+       fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint");
+      tr_filter_free(filt);
+      return NULL;
+      }
+    }
+
     /*For each filter spec within the filter line... */
     for (j = 0; j <json_array_size(jfspecs); j++) {
       
@@ -273,6 +300,7 @@ static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CF
       }
     }
   }
+
   return filt;
 }
 
@@ -826,7 +854,7 @@ json_t *tr_read_config (int n, struct dirent **cfg_files) {
     }
   }
 
-  //  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
+  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
 
   return jcfg;
 }