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.
40 #include <trust_router/tr_dh.h>
41 #include <tid_internal.h>
43 static int tid_resp_destructor(void *obj)
45 TID_RESP *resp=talloc_get_type_abort(obj, TID_RESP);
46 if (resp->err_msg!=NULL)
47 tr_free_name(resp->err_msg);
48 if (resp->rp_realm!=NULL)
49 tr_free_name(resp->rp_realm);
50 if (resp->realm!=NULL)
51 tr_free_name(resp->realm);
53 tr_free_name(resp->comm);
54 if (resp->orig_coi!=NULL)
55 tr_free_name(resp->orig_coi);
59 TID_RESP *tid_resp_new(TALLOC_CTX *mem_ctx)
61 TID_RESP *resp=talloc(mem_ctx, TID_RESP);
63 resp->result=TID_ERROR;
71 resp->error_path=NULL;
72 talloc_set_destructor((void *)resp, tid_resp_destructor);
77 void tid_resp_free(TID_RESP *resp)
83 TID_RESP *tid_resp_dup(TALLOC_CTX *mem_ctx, TID_RESP *resp)
85 TALLOC_CTX *tmp_ctx=NULL;
86 TID_RESP *newresp=NULL;
91 tmp_ctx=talloc_new(NULL);
92 newresp=talloc(tmp_ctx, TID_RESP);
95 newresp->result=resp->result;
96 newresp->err_msg=tr_dup_name(resp->err_msg);
97 newresp->rp_realm=tr_dup_name(resp->err_msg);
98 newresp->realm=tr_dup_name(resp->realm);
99 newresp->comm=tr_dup_name(resp->comm);
100 newresp->orig_coi=tr_dup_name(resp->orig_coi);
101 newresp->servers=tid_srvr_blk_dup(newresp, resp->servers);
102 tid_resp_set_cons(newresp, resp->cons);
103 tid_resp_set_error_path(newresp, resp->error_path);
105 talloc_free(tmp_ctx);
109 TR_EXPORT int tid_resp_get_result(TID_RESP *resp)
111 return(resp->result);
114 void tid_resp_set_result(TID_RESP *resp, int result)
116 resp->result = result;
119 TR_EXPORT TR_NAME *tid_resp_get_err_msg(TID_RESP *resp)
121 return(resp->err_msg);
124 void tid_resp_set_err_msg(TID_RESP *resp, TR_NAME *err_msg)
126 if (resp->err_msg!=NULL)
127 tr_free_name(resp->err_msg);
129 resp->err_msg = err_msg;
132 TR_EXPORT TR_NAME *tid_resp_get_rp_realm(TID_RESP *resp)
134 return(resp->rp_realm);
137 void tid_resp_set_rp_realm(TID_RESP *resp, TR_NAME *rp_realm)
139 resp->rp_realm = rp_realm;
142 TR_EXPORT TR_NAME *tid_resp_get_realm(TID_RESP *resp)
147 void tid_resp_set_realm(TID_RESP *resp, TR_NAME *realm)
152 TR_EXPORT TR_NAME *tid_resp_get_comm(TID_RESP *resp)
157 void tid_resp_set_comm(TID_RESP *resp, TR_NAME *comm)
162 TR_EXPORT TR_NAME *tid_resp_get_orig_coi(TID_RESP *resp)
164 return(resp->orig_coi);
167 void tid_resp_set_orig_coi(TID_RESP *resp, TR_NAME *orig_coi)
169 resp->orig_coi = orig_coi;
172 TR_EXPORT TID_SRVR_BLK *tid_resp_get_server(TID_RESP *resp,
175 TID_SRVR_BLK *this=NULL;
178 for (this=resp->servers; index>0; index--, this=this->next) {}
183 size_t tid_resp_get_num_servers(const TID_RESP *resp)
186 TID_SRVR_BLK *this=NULL;
189 for (count=0, this=resp->servers; this!=NULL; count++, this=this->next) {}
193 static int tid_srvr_blk_destructor(void *obj)
195 TID_SRVR_BLK *srvr=talloc_get_type_abort(obj, TID_SRVR_BLK);
197 if (srvr->key_name!=NULL)
198 tr_free_name(srvr->key_name);
199 if (srvr->aaa_server_dh!=NULL)
200 tr_destroy_dh_params(srvr->aaa_server_dh);
201 if (srvr->path!=NULL)
202 json_decref(srvr->path);
206 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_new(TALLOC_CTX *mem_ctx)
208 TID_SRVR_BLK *srvr=talloc(mem_ctx, TID_SRVR_BLK);
212 srvr->aaa_server_addr=NULL;
214 srvr->aaa_server_dh=NULL;
215 srvr->key_expiration=(GTimeVal){0};
217 talloc_set_destructor((void *)srvr, tid_srvr_blk_destructor);
222 TR_EXPORT void tid_srvr_blk_free(TID_SRVR_BLK *srvr)
227 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_dup(TALLOC_CTX *mem_ctx, TID_SRVR_BLK *srvr)
229 TID_SRVR_BLK *new=NULL;
234 new=tid_srvr_blk_new(mem_ctx);
236 if (srvr->aaa_server_addr!=NULL)
237 new->aaa_server_addr=talloc_strdup(new, srvr->aaa_server_addr);
238 new->key_name=tr_dup_name(srvr->key_name);
239 new->aaa_server_dh=tr_dh_dup(srvr->aaa_server_dh);
240 new->key_expiration=srvr->key_expiration;
241 tid_srvr_blk_set_path(new, srvr->path);
243 tid_srvr_blk_add(new->next, tid_srvr_blk_dup(mem_ctx, srvr->next));
249 TR_EXPORT TID_SRVR_BLK *tid_srvr_blk_add_func(TID_SRVR_BLK *head, TID_SRVR_BLK *new)
251 TID_SRVR_BLK *this=head;
256 while (this->next!=NULL)
262 TR_EXPORT void tid_srvr_blk_set_path(TID_SRVR_BLK *block, json_t *path)
264 if (block->path!=NULL)
265 json_decref(block->path);
267 if (block->path!=NULL)
268 json_incref(block->path);
271 TR_EXPORT const json_t *tid_srvr_get_path( const TID_SRVR_BLK *block)
278 TR_EXPORT void tid_resp_set_cons(TID_RESP *resp, TR_CONSTRAINT_SET *cons)
280 json_t *jc=(json_t *)cons;
282 if (resp->cons!=NULL)
283 json_decref((json_t *) (resp->cons));
285 resp->cons=(TR_CONSTRAINT_SET *)jc;
290 TR_EXPORT void tid_resp_set_error_path(TID_RESP *resp, json_t *ep)
292 if (resp->error_path!=NULL)
293 json_decref(resp->error_path);
295 if (resp->error_path!=NULL)
296 json_incref(resp->error_path);
299 TR_EXPORT json_t *tid_resp_get_error_path(const TID_RESP *resp)
303 return resp->error_path;
306 TR_EXPORT json_t *tid_resp_get_a_path(const TID_RESP *const_resp)
309 TID_SRVR_BLK *server;
310 TID_RESP *resp = (TID_RESP *) const_resp;
315 if (resp->error_path)
316 return resp->error_path;
317 tid_resp_servers_foreach( resp, server, index) {