Merge branch 'milestone/monitoring' into jennifer/request_id
[trust_router.git] / common / tr_rp.c
index 3897e58..0b7d08e 100644 (file)
 #include <talloc.h>
 
 #include <tr.h>
-#include <trust_router/tr_name.h>
-#include <tr_gss.h>
+#include <tr_name_internal.h>
+#include <tr_gss_names.h>
 #include <tr_config.h>
 #include <tr_rp.h>
 #include <tr_debug.h>
 
-static int tr_rp_client_destructor(void *obj)
+TR_RP_REALM *tr_rp_realm_lookup(TR_RP_REALM *rp_realms, TR_NAME *rp_name)
 {
+  TR_RP_REALM *rp = NULL;
+
+  if (!rp_name) {
+    tr_debug("tr_rp_realm_lookup: Bad parameters.");
+    return NULL;
+  }
+
+  for (rp=rp_realms; NULL!=rp; rp=rp->next) {
+    if (0==tr_name_cmp(tr_rp_realm_get_id(rp), rp_name))
+      return rp;
+  } 
+  return NULL;
+}
+
+static int tr_rp_realm_destructor(void *obj)
+{
+  TR_RP_REALM *rp=talloc_get_type_abort(obj, TR_RP_REALM);
+  if (rp->realm_id!=NULL)
+    tr_free_name(rp->realm_id);
   return 0;
 }
 
-TR_RP_CLIENT *tr_rp_client_new(TALLOC_CTX *mem_ctx)
+TR_RP_REALM *tr_rp_realm_new(TALLOC_CTX *mem_ctx)
 {
-  TR_RP_CLIENT *client=talloc(mem_ctx, TR_RP_CLIENT);
-
-  if (client!=NULL) {
-    client->next=NULL;
-    client->comm_next=NULL;
-    client->gss_names=NULL;
-    client->filter=NULL;
-    talloc_set_destructor((void *)client, tr_rp_client_destructor);
+  TR_RP_REALM *rp=talloc(mem_ctx, TR_RP_REALM);
+  if (rp!=NULL) {
+    rp->next=NULL;
+    rp->realm_id=NULL;
+    rp->refcount=0;
+    talloc_set_destructor((void *)rp, tr_rp_realm_destructor);
   }
-  return client;
+  return rp;
 }
 
-void tr_rp_client_free(TR_RP_CLIENT *client)
+void tr_rp_realm_free(TR_RP_REALM *rp)
 {
-  talloc_free(client);
+  talloc_free(rp);
 }
 
-static TR_RP_CLIENT *tr_rp_client_tail(TR_RP_CLIENT *client)
+/* talloc note: lists of idp realms should be assembled using
+ * tr_idp_realm_add(). This will put all of the elements in the
+ * list, other than the head, as children of the head context.
+ * The head can then be placed in whatever context is desired. */
+
+static TR_RP_REALM *tr_rp_realm_tail(TR_RP_REALM *realm)
 {
-  if (client==NULL)
+  if (realm==NULL)
     return NULL;
 
-  while (client->next!=NULL)
-    client=client->next;
-  return client;
+  while (realm->next!=NULL)
+    realm=realm->next;
+  return realm;
 }
 
-/* do not call directly, use the tr_rp_client_add() macro */
-TR_RP_CLIENT *tr_rp_client_add_func(TR_RP_CLIENT *clients, TR_RP_CLIENT *new)
+/* for correct behavior, call like: rp_realms=tr_rp_realm_add_func(rp_realms, new_realm);
+ * or better yet, use the macro */
+TR_RP_REALM *tr_rp_realm_add_func(TR_RP_REALM *head, TR_RP_REALM *new)
 {
-  if (clients==NULL)
-    clients=new;
+  if (head==NULL)
+    head=new;
   else {
-    tr_rp_client_tail(clients)->next=new;
+    tr_rp_realm_tail(head)->next=new;
     while (new!=NULL) {
-      talloc_steal(clients, new); /* put it in the right context */
+      talloc_steal(head, new); /* put it in the right context */
       new=new->next;
     }
   }
-  return clients;
+  return head;
 }
 
+/* use the macro */
+TR_RP_REALM *tr_rp_realm_remove_func(TR_RP_REALM *head, TR_RP_REALM *remove)
+{
+  TALLOC_CTX *list_ctx=talloc_parent(head);
+  TR_RP_REALM *this=NULL;
+
+  if (head==NULL)
+    return NULL;
+
+  if (head==remove) {
+    /* if we're removing the head, put the next element (if present) into the context
+     * the list head was in. */
+    head=head->next;
+    if (head!=NULL) {
+      talloc_steal(list_ctx, head);
+      /* now put all the other elements in the context of the list head */
+      for (this=head->next; this!=NULL; this=this->next)
+        talloc_steal(head, this);
+    }
+  } else {
+    /* not removing the head; no need to play with contexts */
+    for (this=head; this->next!=NULL; this=this->next) {
+      if (this->next==remove) {
+        this->next=remove->next;
+        break;
+      }
+    }
+  }
+  return head;
+}
 
-int tr_rp_client_add_gss_name(TR_RP_CLIENT *rp_client, TR_NAME *gss_name)
+void tr_rp_realm_incref(TR_RP_REALM *realm)
 {
-  return tr_gss_names_add(rp_client->gss_names, gss_name);
+  realm->refcount++;
 }
 
-int tr_rp_client_set_filter(TR_RP_CLIENT *client, TR_FILTER *filt)
+void tr_rp_realm_decref(TR_RP_REALM *realm)
 {
-  if (client->filter!=NULL)
-    tr_filter_free(client->filter);
-  client->filter=filt;
-  talloc_steal(client, filt);
-  return 0; /* success */
+  if (realm->refcount>0)
+    realm->refcount--;
 }
 
-TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name)
+/* remove any with zero refcount 
+ * Call via macro. */
+TR_RP_REALM *tr_rp_realm_sweep_func(TR_RP_REALM *head)
 {
-  TR_RP_CLIENT *rp = NULL;
+  TR_RP_REALM *rp=NULL;
+  TR_RP_REALM *old_next=NULL;
 
-  if ((!rp_clients) || (!gss_name)) {
-    tr_debug("tr_rp_client_lookup: Bad parameters.");
+  if (head==NULL)
     return NULL;
+
+  while ((head!=NULL) && (head->refcount==0)) {
+    rp=head; /* keep a pointer so we can remove it */
+    tr_rp_realm_remove(head, rp); /* use this to get talloc contexts right */
+    tr_rp_realm_free(rp);
   }
 
-  for (rp = rp_clients; NULL != rp; rp = rp->next) {
-    if (tr_gss_names_matches(rp->gss_names, gss_name))
-      return rp;
-  } 
-  return NULL;
+  if (head==NULL)
+    return NULL;
+
+  /* will not remove the head here, that has already been done */
+  for (rp=head; (rp!=NULL) && (rp->next!=NULL); rp=rp->next) {
+    if (rp->next->refcount==0) {
+      old_next=rp->next;
+      tr_rp_realm_remove(head, rp->next); /* changes rp->next, may make it null */
+      tr_rp_realm_free(old_next);
+    }
+  }
+
+  return head;
 }
 
-/* talloc note: lists of idp realms should be assembled using
- * tr_idp_realm_add(). This will put all of the elements in the
- * list, other than the head, as children of the head context.
- * The head can then be placed in whatever context is desired. */
+TR_NAME *tr_rp_realm_get_id(TR_RP_REALM *rp)
+{
+  if (rp==NULL)
+    return NULL;
 
-static TR_RP_REALM *tr_rp_realm_tail(TR_RP_REALM *realm)
+  return rp->realm_id;
+}
+
+TR_NAME *tr_rp_realm_dup_id(TR_RP_REALM *rp)
 {
-  while (realm!=NULL)
-    realm=realm->next;
-  return realm;
+  if (rp==NULL)
+    return NULL;
+
+  return tr_dup_name(tr_rp_realm_get_id(rp));
 }
 
-/* for correct behavior, call like: rp_realms=tr_rp_realm_add(rp_realms, new_realm); */
-TR_RP_REALM *tr_rp_realm_add_func(TR_RP_REALM *head, TR_RP_REALM *new)
+void tr_rp_realm_set_id(TR_RP_REALM *rp, TR_NAME *id)
 {
-  if (head==NULL)
-    head=new;
-  else {
-    tr_rp_realm_tail(head)->next=new;
-    while (new!=NULL) {
-      talloc_steal(head, new); /* put it in the right context */
-      new=new->next;
-    }
-  }
-  return head;
+  if (rp->realm_id!=NULL)
+    tr_free_name(rp->realm_id);
+  rp->realm_id=id;
 }
+
+char *tr_rp_realm_to_str(TALLOC_CTX *mem_ctx, TR_RP_REALM *rp)
+{
+  return talloc_asprintf(mem_ctx,
+                         "RP realm: \"%.*s\"\n",
+                         rp->realm_id->len, rp->realm_id->buf);
+}
+