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);
47 if (comm->owner_realm!=NULL)
48 tr_free_name(comm->owner_realm);
49 if (comm->owner_email!=NULL)
50 tr_free_name(comm->owner_email);
54 TR_COMM *tr_comm_new(TALLOC_CTX *mem_ctx)
56 TR_COMM *comm=talloc(mem_ctx, TR_COMM);
60 comm->type=TR_COMM_UNKNOWN;
62 comm->idp_realms=NULL;
64 comm->owner_realm=NULL;
65 comm->owner_contact=NULL;
66 talloc_set_destructor((void *)comm, tr_comm_destructor);
71 void tr_comm_free(TR_COMM *comm)
76 void tr_comm_set_id(TR_COMM *comm, TR_NAME *id)
79 tr_free_name(comm->id);
83 TR_NAME *tr_comm_get_id(TR_COMM *comm)
88 TR_NAME *tr_comm_dup_id(TR_COMM *comm)
90 return tr_dup_name(comm->id);
93 void tr_comm_set_type(TR_COMM *comm, TR_COMM_TYPE type)
98 TR_COMM_TYPE tr_comm_get_type(TR_COMM *comm)
103 void tr_comm_set_owner_realm(TR_COMM *comm, TR_NAME *realm)
105 if (comm->owner_realm!=NULL)
106 tr_free_name(comm->owner_realm);
107 comm->owner_realm=realm;
110 TR_NAME *tr_comm_get_owner_realm(TR_COMM *comm)
112 return comm->owner_realm;
115 TR_NAME *tr_comm_dup_owner_realm(TR_COMM *comm)
117 return tr_dup_name(tr->owner_realm);
120 void tr_comm_set_owner_contact(TR_COMM *comm, TR_NAME *contact)
122 if (comm->owner_contact != NULL)
123 tr_free_name(comm->owner_contact);
124 comm->owner_contact=contact;
127 TR_NAME *tr_comm_get_owner_contact(TR_COMM *comm)
129 return comm->owner_contact;
132 TR_NAME *tr_comm_dup_owner_contact(TR_COMM *comm)
134 return tr_dup_name(tr->owner_contact);
137 /* does not take responsibility for freeing IDP realm */
138 void tr_comm_add_idp_realm(TR_COMM *comm, TR_IDP_REALM *realm)
140 TR_IDP_REALM *cur=NULL;
142 if (comm->idp_realms==NULL)
143 comm->idp_realms=realm;
145 for (cur=comm->idp_realms; cur->comm_next!=NULL; cur=cur->comm_next) { }
146 cur->comm_next=realm;
150 /* does not take responsibility for freeing RP realm */
151 void tr_comm_add_rp_realm(TR_COMM *comm, TR_RP_REALM *realm)
153 TR_RP_REALM *cur=NULL;
155 if (comm->rp_realms==NULL)
156 comm->rp_realms=realm;
158 for (cur=comm->rp_realms; cur->next!=NULL; cur=cur->next) { }
163 static TR_COMM *tr_comm_tail(TR_COMM *comm)
168 while (comm->next!=NULL)
173 /* All list members are in the talloc context of the head.
174 * This will require careful thought if entries are ever removed
175 * Call like comms=tr_comm_add_func(comms, new_comm);
176 * or just use the tr_comm_add(comms, new) macro. */
177 TR_COMM *tr_comm_add_func(TR_COMM *comms, TR_COMM *new)
182 tr_comm_tail(comms)->next=new;
184 talloc_steal(comms, new);
191 TR_IDP_REALM *tr_find_comm_idp (TR_COMM *comm, TR_NAME *idp_realm)
195 if ((!comm) || (!idp_realm)) {
199 for (idp = comm->idp_realms; NULL != idp; idp = idp->comm_next) {
200 if (!tr_name_cmp (idp_realm, idp->realm_id)) {
201 tr_debug("tr_find_comm_idp: Found IdP %s in community %s.", idp_realm->buf, comm->id->buf);
205 /* if we didn't find one, return NULL */
209 TR_RP_REALM *tr_find_comm_rp (TR_COMM *comm, TR_NAME *rp_realm)
213 if ((!comm) || (!rp_realm)) {
217 for (rp = comm->rp_realms; NULL != rp; rp = rp->next) {
218 if (!tr_name_cmp (rp_realm, rp->realm_name)) {
219 tr_debug("tr_find_comm_rp: Found RP %s in community %s.", rp_realm->buf, comm->id->buf);
223 /* if we didn't find one, return NULL */
227 TR_COMM *tr_comm_lookup(TR_COMM *comms, TR_NAME *comm_name)
229 TR_COMM *cfg_comm = NULL;
231 for (cfg_comm = comms; NULL != cfg_comm; cfg_comm = cfg_comm->next) {
232 if ((cfg_comm->id->len == comm_name->len) &&
233 (!strncmp(cfg_comm->id->buf, comm_name->buf, comm_name->len)))