Initialize refcount for IDP realms
[trust_router.git] / common / tr_idp.c
index 3019ef6..f6c0836 100644 (file)
@@ -52,6 +52,7 @@ TR_AAA_SERVER *tr_aaa_server_new(TALLOC_CTX *mem_ctx, TR_NAME *hostname)
 {
   TR_AAA_SERVER *aaa=talloc(mem_ctx, TR_AAA_SERVER);
   if (aaa!=NULL) {
+    aaa->next=NULL;
     aaa->hostname=hostname;
     talloc_set_destructor((void *)aaa, tr_aaa_server_destructor);
   }
@@ -63,7 +64,33 @@ void tr_aaa_server_free(TR_AAA_SERVER *aaa)
   talloc_free(aaa);
 }
 
-TR_AAA_SERVER *tr_idp_aaa_server_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_realm_name, TR_NAME *comm)
+TR_AAA_SERVER_ITER *tr_aaa_server_iter_new(TALLOC_CTX *mem_ctx)
+{
+  return talloc(mem_ctx, TR_AAA_SERVER_ITER);
+}
+
+void tr_aaa_server_iter_free(TR_AAA_SERVER_ITER *iter)
+{
+  talloc_free(iter);
+}
+
+TR_AAA_SERVER *tr_aaa_server_iter_first(TR_AAA_SERVER_ITER *iter, TR_AAA_SERVER *aaa)
+{
+  iter->this=aaa;
+  return iter->this;
+}
+
+TR_AAA_SERVER *tr_aaa_server_iter_next(TR_AAA_SERVER_ITER *iter)
+{
+  if (iter->this!=NULL) {
+    iter->this=iter->this->next;
+  }
+  return iter->this;
+}
+
+
+/* fills in shared if pointer not null */
+TR_AAA_SERVER *tr_idp_aaa_server_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_realm_name, TR_NAME *comm, int *shared_out)
 {
   TR_IDP_REALM *idp = NULL;
 
@@ -73,9 +100,11 @@ TR_AAA_SERVER *tr_idp_aaa_server_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_r
       break;
     }
   }
-  if (idp)
+  if (idp) {
+    if (shared_out!=NULL)
+      *shared_out=idp->shared_config;
     return idp->aaa_servers;
-  else 
+  else 
     return NULL;
 }
 
@@ -110,6 +139,7 @@ TR_IDP_REALM *tr_idp_realm_new(TALLOC_CTX *mem_ctx)
     idp->aaa_servers=NULL;
     idp->apcs=NULL;
     idp->origin=TR_REALM_LOCAL;
+    idp->refcount=0;
     talloc_set_destructor((void *)idp, tr_idp_realm_destructor);
   }
   return idp;
@@ -198,6 +228,37 @@ TR_IDP_REALM *tr_idp_realm_add_func(TR_IDP_REALM *head, TR_IDP_REALM *new)
   return head;
 }
 
+/* use the macro */
+TR_IDP_REALM *tr_idp_realm_remove_func(TR_IDP_REALM *head, TR_IDP_REALM *remove)
+{
+  TALLOC_CTX *list_ctx=talloc_parent(head);
+  TR_IDP_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;
+}
+
 static int tr_idp_realm_apc_count(TR_IDP_REALM *idp)
 {
   int ii=0;
@@ -308,3 +369,35 @@ void tr_idp_realm_decref(TR_IDP_REALM *realm)
   if (realm->refcount>0)
     realm->refcount--;
 }
+
+/* remove any with zero refcount 
+ * Call via macro. */
+TR_IDP_REALM *tr_idp_realm_sweep_func(TR_IDP_REALM *head)
+{
+  TR_IDP_REALM *idp=NULL;
+  TR_IDP_REALM *old_next=NULL;
+
+  if (head==NULL)
+    return NULL;
+
+  while ((head!=NULL) && (head->refcount==0)) {
+    idp=head; /* keep a pointer so we can remove it */
+    tr_idp_realm_remove(head, idp); /* use this to get talloc contexts right */
+    tr_idp_realm_free(idp);
+  }
+
+  if (head==NULL)
+    return NULL;
+
+  /* will not remove the head here, that has already been done */
+  for (idp=head; idp->next!=NULL; idp=idp->next) {
+    if (idp->next->refcount==0) {
+      old_next=idp->next;
+      tr_idp_realm_remove(head, idp->next); /* changes idp->next */
+      tr_idp_realm_free(old_next);
+    }
+  }
+
+  return head;
+}
+