2 * Copyright (c) 2012, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <trust_router/tr_name.h>
42 static int tr_comm_destructor(void *obj)
44 TR_COMM *comm=talloc_get_type_abort(obj, TR_COMM);
46 tr_free_name(comm->id);
50 TR_COMM *tr_comm_new(TALLOC_CTX *mem_ctx)
52 TR_COMM *comm=talloc(mem_ctx, TR_COMM);
56 comm->type=TR_COMM_UNKNOWN;
58 comm->idp_realms=NULL;
60 talloc_set_destructor((void *)comm, tr_comm_destructor);
65 void tr_comm_free(TR_COMM *comm)
70 /* does not take responsibility for freeing IDP realm */
71 void tr_comm_add_idp_realm(TR_COMM *comm, TR_IDP_REALM *realm)
73 TR_IDP_REALM *cur=NULL;
75 if (comm->idp_realms==NULL)
76 comm->idp_realms=realm;
78 for (cur=comm->idp_realms; cur->comm_next!=NULL; cur=cur->comm_next) { }
83 /* does not take responsibility for freeing RP realm */
84 void tr_comm_add_rp_realm(TR_COMM *comm, TR_RP_REALM *realm)
86 TR_RP_REALM *cur=NULL;
88 if (comm->rp_realms==NULL)
89 comm->rp_realms=realm;
91 for (cur=comm->rp_realms; cur->next!=NULL; cur=cur->next) { }
96 static TR_COMM *tr_comm_tail(TR_COMM *comm)
101 while (comm->next!=NULL)
106 /* All list members are in the talloc context of the head.
107 * This will require careful thought if entries are ever removed
108 * Call like comms=tr_comm_add_func(comms, new_comm);
109 * or just use the tr_comm_add(comms, new) macro. */
110 TR_COMM *tr_comm_add_func(TR_COMM *comms, TR_COMM *new)
115 tr_comm_tail(comms)->next=new;
117 talloc_steal(comms, new);
124 TR_IDP_REALM *tr_find_comm_idp (TR_COMM *comm, TR_NAME *idp_realm)
128 if ((!comm) || (!idp_realm)) {
132 for (idp = comm->idp_realms; NULL != idp; idp = idp->comm_next) {
133 if (!tr_name_cmp (idp_realm, idp->realm_id)) {
134 tr_debug("tr_find_comm_idp: Found IdP %s in community %s.", idp_realm->buf, comm->id->buf);
138 /* if we didn't find one, return NULL */
142 TR_RP_REALM *tr_find_comm_rp (TR_COMM *comm, TR_NAME *rp_realm)
146 if ((!comm) || (!rp_realm)) {
150 for (rp = comm->rp_realms; NULL != rp; rp = rp->next) {
151 if (!tr_name_cmp (rp_realm, rp->realm_name)) {
152 tr_debug("tr_find_comm_rp: Found RP %s in community %s.", rp_realm->buf, comm->id->buf);
156 /* if we didn't find one, return NULL */
160 TR_COMM *tr_comm_lookup(TR_COMM *comms, TR_NAME *comm_name)
162 TR_COMM *cfg_comm = NULL;
164 for (cfg_comm = comms; NULL != cfg_comm; cfg_comm = cfg_comm->next) {
165 if ((cfg_comm->id->len == comm_name->len) &&
166 (!strncmp(cfg_comm->id->buf, comm_name->buf, comm_name->len)))