Add a generic TR_LIST type, use for TR_FILTER's 'lines' member
authorJennifer Richards <jennifer@painless-security.com>
Mon, 23 Apr 2018 15:01:55 +0000 (11:01 -0400)
committerJennifer Richards <jennifer@painless-security.com>
Mon, 23 Apr 2018 15:01:55 +0000 (11:01 -0400)
CMakeLists.txt
Makefile.am
common/tr_filter.c
common/tr_list.c [new file with mode: 0644]
include/tr_filter.h
include/tr_list.h [new file with mode: 0644]

index 4ae8dba..cb948a2 100644 (file)
@@ -96,7 +96,7 @@ set(SOURCE_FILES
     trp/trp_upd.c
     trp/trpc.c
     trp/trps.c include/tr_name_internal.h mon/mon_req.c mon/mon_req_encode.c mon/mon_req_decode.c
-        mon/mon_resp.c mon/mon_common.c mon/mon_resp_encode.c mon/mon_resp_decode.c tr/tr_mon.c mon/mons.c include/tr_socket.h common/tr_gss.c include/tr_gss.h common/tr_config_internal.c mon/mons_handlers.c include/mons_handlers.h tr/tr_tid_mons.c tr/tr_tid_mons.c trp/trp_route.c include/trp_route.h trp/trp_rtable_encoders.c trp/trp_route_encoders.c trp/trp_peer.c include/trp_peer.h trp/trp_peer_encoders.c trp/trp_ptable_encoders.c common/tr_idp_encoders.c common/tr_comm_encoders.c common/tr_rp_client.c include/tr_rp_client.h common/tr_rp_client_encoders.c common/tr_filter_encoders.c common/tr_config_encoders.c common/tr_config_filters.c common/tr_config_realms.c common/tr_config_rp_clients.c common/tr_config_orgs.c common/tr_config_comms.c)
+        mon/mon_resp.c mon/mon_common.c mon/mon_resp_encode.c mon/mon_resp_decode.c tr/tr_mon.c mon/mons.c include/tr_socket.h common/tr_gss.c include/tr_gss.h common/tr_config_internal.c mon/mons_handlers.c include/mons_handlers.h tr/tr_tid_mons.c tr/tr_tid_mons.c trp/trp_route.c include/trp_route.h trp/trp_rtable_encoders.c trp/trp_route_encoders.c trp/trp_peer.c include/trp_peer.h trp/trp_peer_encoders.c trp/trp_ptable_encoders.c common/tr_idp_encoders.c common/tr_comm_encoders.c common/tr_rp_client.c include/tr_rp_client.h common/tr_rp_client_encoders.c common/tr_filter_encoders.c common/tr_config_encoders.c common/tr_config_filters.c common/tr_config_realms.c common/tr_config_rp_clients.c common/tr_config_orgs.c common/tr_config_comms.c common/tr_list.c include/tr_list.h)
 
 # Does not actually build!
 add_executable(trust_router ${SOURCE_FILES})
index 1c933d6..60efb3b 100644 (file)
@@ -27,6 +27,7 @@ common_srcs = common/tr_name.c \
        common/tr_filter_encoders.c \
        common/tr_gss_names.c \
        common/tr_socket.c \
+       common/tr_list.c \
        $(mon_srcs)
 
 tid_srcs = tid/tid_resp.c \
index 828c7c4..224110e 100644 (file)
@@ -595,25 +595,17 @@ TR_FLINE *tr_fline_new(TALLOC_CTX *mem_ctx)
   return fl;
 }
 
-static int tr_filter_destructor(void *object)
-{
-  TR_FILTER *filt = talloc_get_type_abort(object, TR_FILTER);
-  if (filt->lines)
-    g_ptr_array_unref(filt->lines);
-  return 0;
-}
 TR_FILTER *tr_filter_new(TALLOC_CTX *mem_ctx)
 {
   TR_FILTER *f = talloc(mem_ctx, TR_FILTER);
 
   if (f != NULL) {
     f->type = TR_FILTER_TYPE_UNKNOWN;
-    f->lines = g_ptr_array_new();
+    f->lines = tr_list_new(f);
     if (f->lines == NULL) {
       talloc_free(f);
       return NULL;
     }
-    talloc_set_destructor((void *)f, tr_filter_destructor);
   }
   return f;
 }
@@ -633,67 +625,6 @@ TR_FILTER_TYPE tr_filter_get_type(TR_FILTER *filt)
   return filt->type;
 }
 
-/**
- * Add a TR_FLINE to a filter
- *
- * Steals the line into its context on success
- *
- * @param filt
- * @param line
- * @return line, or null on failure
- */
-TR_FLINE *tr_filter_add_line(TR_FILTER *filt, TR_FLINE *line)
-{
-  guint old_len = filt->lines->len;
-  g_ptr_array_add(filt->lines, line);
-
-  if (old_len == filt->lines->len)
-    return NULL; /* failed to add */
-
-  talloc_steal(filt, line);
-  return line;
-}
-
-/**
- * Iterator for TR_FLINES in a TR_FILTER
- *
- * @param mem_ctx
- * @return
- */
-TR_FILTER_ITER *tr_filter_iter_new(TALLOC_CTX *mem_ctx)
-{
-  TR_FILTER_ITER *iter = talloc(mem_ctx, TR_FILTER_ITER);
-  if (iter) {
-    iter->filter = NULL;
-  }
-  return iter;
-}
-
-void tr_filter_iter_free(TR_FILTER_ITER *iter)
-{
-  talloc_free(iter);
-}
-
-TR_FLINE *tr_filter_iter_first(TR_FILTER_ITER *iter, TR_FILTER *filter)
-{
-  if (!iter || !filter)
-    return NULL;
-
-  iter->filter = filter;
-  iter->ii = 0;
-  return tr_filter_iter_next(iter);
-}
-
-TR_FLINE *tr_filter_iter_next(TR_FILTER_ITER *iter)
-{
-  if (!iter)
-    return NULL;
-
-  if (iter->ii < iter->filter->lines->len)
-    return g_ptr_array_index(iter->filter->lines, iter->ii++);
-  return NULL;
-}
-
 TR_FLINE_ITER *tr_fline_iter_new(TALLOC_CTX *mem_ctx)
 {
   TR_FLINE_ITER *iter = talloc(mem_ctx, TR_FLINE_ITER);
diff --git a/common/tr_list.c b/common/tr_list.c
new file mode 100644 (file)
index 0000000..7d6c75c
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright (c) 2018 JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#include <talloc.h>
+#include <tr_list.h>
+
+static int tr_list_destructor(void *object)
+{
+  TR_LIST *list = talloc_get_type_abort(object, TR_LIST);
+  if (*list)
+    g_ptr_array_unref(*list);
+  return 0;
+}
+
+/* Note that the TR_LIST type is a pointer-to-pointer to
+ * a GPtrArray. This is done so that we can hook a talloc destructor
+ * to the list to ensure that the GLib reference count is handled correctly
+ * when used with talloc. */
+TR_LIST *tr_list_new(TALLOC_CTX *mem_ctx)
+{
+  TR_LIST *list = talloc(mem_ctx, TR_LIST);
+  if (list) {
+    *list = g_ptr_array_new();
+    if (*list == NULL) {
+      talloc_free(list);
+      return NULL;
+    }
+    talloc_set_destructor((void *)list, tr_list_destructor);
+  }
+  return list;
+}
+
+void tr_list_free(TR_LIST *list)
+{
+  talloc_free(list);
+}
+
+void *tr_list_add(TR_LIST *list, void *item)
+{
+  guint old_len = (*list)->len;
+  g_ptr_array_add((*list), item);
+
+  if ((*list)->len == old_len)
+    return NULL; /* failed to add */
+
+  return item;
+}
+
+TR_LIST_ITER *tr_list_iter_new(TALLOC_CTX *mem_ctx)
+{
+  TR_LIST_ITER *iter = talloc(mem_ctx, TR_LIST_ITER);
+  if (iter)
+    iter->list = NULL;
+  return iter;
+}
+
+void tr_list_iter_free(TR_LIST_ITER *iter)
+{
+  talloc_free(iter);
+}
+
+void *tr_list_iter_first(TR_LIST_ITER *iter, TR_LIST *list)
+{
+  if (!iter || !list || (!(*list)))
+    return NULL;
+
+  iter->list = list;
+  iter->index = 0;
+  return tr_list_iter_next(iter);
+}
+
+void *tr_list_iter_next(TR_LIST_ITER *iter)
+{
+  if (!iter)
+    return NULL;
+
+  if (iter->index < (*(iter->list))->len)
+    return g_ptr_array_index(*(iter->list), iter->index++);
+  return NULL;
+}
+
+void tr_list_foreach(TR_LIST *list, void (*function)(void *, void *), void *cookie)
+{
+  g_ptr_array_foreach((*list), function, cookie);
+}
\ No newline at end of file
index d5e2b86..0ca2f37 100644 (file)
@@ -39,6 +39,7 @@
 #include <jansson.h>
 #include <glib.h>
 
+#include <tr_list.h>
 #include <tr_name_internal.h>
 #include <trust_router/tr_constraint.h>
 #include <trust_router/tid.h>
@@ -87,14 +88,9 @@ typedef struct tr_fline_iter {
 
 typedef struct tr_filter {
   TR_FILTER_TYPE type;
-  GPtrArray *lines;
+  TR_LIST *lines;
 } TR_FILTER;
 
-typedef struct tr_filter_iter {
-  TR_FILTER *filter;
-  guint ii;
-} TR_FILTER_ITER;
-
 typedef struct tr_filter_set TR_FILTER_SET;
 struct tr_filter_set {
   TR_FILTER *this;
@@ -135,10 +131,13 @@ TR_NAME *tr_fspec_add_match(TR_FSPEC *fspec, TR_NAME *match);
 
 int tr_fspec_matches(TR_FSPEC *fspec, TR_FILTER_TYPE ftype, TR_FILTER_TARGET *target);
 
-TR_FILTER_ITER *tr_filter_iter_new(TALLOC_CTX *mem_ctx);
-void tr_filter_iter_free(TR_FILTER_ITER *iter);
-TR_FLINE *tr_filter_iter_first(TR_FILTER_ITER *iter, TR_FILTER *filter);
-TR_FLINE *tr_filter_iter_next(TR_FILTER_ITER *iter);
+/* Iterator for TR_FILTER.lines */
+typedef TR_LIST_ITER TR_FILTER_ITER;
+#define tr_filter_iter_new(CTX) (tr_list_iter_new(CTX))
+#define tr_filter_iter_free(ITER) (tr_list_iter_free(ITER))
+#define tr_filter_iter_first(ITER, FILT) (tr_list_iter_first((ITER), (FILT)->lines))
+#define tr_filter_iter_next(ITER) (tr_list_iter_next(ITER))
+#define tr_filter_add_line(FILT, LINE) ((TR_FLINE *) tr_list_add((FILT)->lines, (LINE)))
 
 TR_FLINE_ITER *tr_fline_iter_new(TALLOC_CTX *mem_ctx);
 void tr_fline_iter_free(TR_FLINE_ITER *iter);
diff --git a/include/tr_list.h b/include/tr_list.h
new file mode 100644 (file)
index 0000000..12ede45
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2018 JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef TRUST_ROUTER_TR_LIST_H
+#define TRUST_ROUTER_TR_LIST_H
+
+#include <talloc.h>
+#include <glib.h>
+
+typedef GPtrArray *TR_LIST;
+
+typedef struct tr_list_iter{
+  TR_LIST *list;
+  guint index;
+} TR_LIST_ITER;
+
+TR_LIST *tr_list_new(TALLOC_CTX *mem_ctx);
+void *tr_list_add(TR_LIST *list, void *item);
+void tr_list_free(TR_LIST *list);
+
+TR_LIST_ITER *tr_list_iter_new(TALLOC_CTX *mem_ctx);
+void tr_list_iter_free(TR_LIST_ITER *iter);
+void *tr_list_iter_first(TR_LIST_ITER *iter, TR_LIST *list);
+void *tr_list_iter_next(TR_LIST_ITER *iter);
+void tr_list_foreach(TR_LIST *list, void (*function)(void *item, void *cookie), void *cookie);
+
+#endif //TRUST_ROUTER_TR_LIST_H