X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=common%2Ftr_rp.c;h=6d0880a221d155ba9eaeaa304765bd7da7c55de8;hb=98be752015619fab5d29405bea10158a0e26d044;hp=9a056c08c80ee98b2dad8a06ae2687c6597a5813;hpb=f4fa9a7584189324cff981ccf965802b4c69ddda;p=trust_router.git diff --git a/common/tr_rp.c b/common/tr_rp.c index 9a056c0..6d0880a 100644 --- a/common/tr_rp.c +++ b/common/tr_rp.c @@ -35,8 +35,8 @@ #include #include -#include -#include +#include +#include #include #include #include @@ -54,7 +54,7 @@ TR_RP_CLIENT *tr_rp_client_new(TALLOC_CTX *mem_ctx) client->next=NULL; client->comm_next=NULL; client->gss_names=NULL; - client->filter=NULL; + client->filters=NULL; talloc_set_destructor((void *)client, tr_rp_client_destructor); } return client; @@ -96,29 +96,64 @@ int tr_rp_client_add_gss_name(TR_RP_CLIENT *rp_client, TR_NAME *gss_name) return tr_gss_names_add(rp_client->gss_names, gss_name); } -int tr_rp_client_set_filter(TR_RP_CLIENT *client, TR_FILTER *filt) +int tr_rp_client_set_filters(TR_RP_CLIENT *client, TR_FILTER_SET *filts) { - if (client->filter!=NULL) - tr_filter_free(client->filter); - client->filter=filt; - talloc_steal(client, filt); + if (client->filters!=NULL) + tr_filter_set_free(client->filters); + client->filters=filts; + talloc_steal(client, filts); return 0; /* success */ } -TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name) +TR_RP_CLIENT_ITER *tr_rp_client_iter_new(TALLOC_CTX *memctx) { - TR_RP_CLIENT *rp = NULL; + return talloc(memctx, TR_RP_CLIENT_ITER); +} - if ((!rp_clients) || (!gss_name)) { - tr_debug("tr_rp_client_lookup: Bad parameters."); +void tr_rp_client_iter_free(TR_RP_CLIENT_ITER *iter) +{ + talloc_free(iter); +} + +TR_RP_CLIENT *tr_rp_client_iter_first(TR_RP_CLIENT_ITER *iter, TR_RP_CLIENT *rp_clients) +{ + if (!iter) { + tr_err("tr_rp_client_iter_first: Iterator is null, failing."); return NULL; } + *iter=rp_clients; + return *iter; +} - for (rp = rp_clients; NULL != rp; rp = rp->next) { - if (tr_gss_names_matches(rp->gss_names, gss_name)) - return rp; - } - return NULL; +TR_RP_CLIENT *tr_rp_client_iter_next(TR_RP_CLIENT_ITER *iter) +{ + if (*iter) + *iter=(*iter)->next; + return *iter; +} + +/** + * Find a client associated with a GSS name. It's possible there are other clients that match as well. + * + * @param rp_clients List of RP clients to search + * @param gss_name GSS name to search for + * @return Borrowed reference to an RP client linked to the GSS name + */ +TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name) +{ + TR_RP_CLIENT_ITER *iter=tr_rp_client_iter_new(NULL); + TR_RP_CLIENT *client=NULL; + + if (iter==NULL) { + tr_err("tr_rp_client_lookup: Unable to allocate iterator"); + return NULL; + } + for (client=tr_rp_client_iter_first(iter, rp_clients); client != NULL; client=tr_rp_client_iter_next(iter)) { + if (tr_gss_names_matches(client->gss_names, gss_name)) + break; + } + tr_rp_client_iter_free(iter); + return client; } TR_RP_REALM *tr_rp_realm_lookup(TR_RP_REALM *rp_realms, TR_NAME *rp_name) @@ -193,6 +228,37 @@ TR_RP_REALM *tr_rp_realm_add_func(TR_RP_REALM *head, TR_RP_REALM *new) 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; +} + void tr_rp_realm_incref(TR_RP_REALM *realm) { realm->refcount++; @@ -204,6 +270,37 @@ void tr_rp_realm_decref(TR_RP_REALM *realm) realm->refcount--; } +/* remove any with zero refcount + * Call via macro. */ +TR_RP_REALM *tr_rp_realm_sweep_func(TR_RP_REALM *head) +{ + TR_RP_REALM *rp=NULL; + TR_RP_REALM *old_next=NULL; + + 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); + } + + 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; +} + TR_NAME *tr_rp_realm_get_id(TR_RP_REALM *rp) { if (rp==NULL)