Use json_is_true() in place of json_boolean_value() for compatibility
[trust_router.git] / trp / trp_req.c
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <jansson.h>
36 #include <talloc.h>
37
38 #include <tr_name_internal.h>
39 #include <trp_internal.h>
40 #include <tr_debug.h>
41
42 static int trp_req_destructor(void *object)
43 {
44   TRP_REQ *req=talloc_get_type_abort(object, TRP_REQ);
45   
46   /* clean up TR_NAME data, which are not managed by talloc */
47   if (req->comm != NULL)
48     tr_free_name(req->comm);
49
50   if (req->realm != NULL)
51     tr_free_name(req->realm);
52
53   if (req->peer != NULL)
54     tr_free_name(req->peer);
55
56   return 0;
57 }
58
59 TRP_REQ *trp_req_new(TALLOC_CTX *mem_ctx)
60 {
61   TRP_REQ *new_req=talloc(mem_ctx, TRP_REQ);
62
63   if (new_req != NULL) {
64     new_req->comm=NULL;
65     new_req->realm=NULL;
66     new_req->peer=NULL;
67   }
68
69   talloc_set_destructor((void *)new_req, trp_req_destructor);
70   return new_req;
71 }
72
73 void trp_req_free(TRP_REQ *req)
74 {
75   if (req!=NULL)
76     talloc_free(req);
77 }
78
79 TR_NAME *trp_req_get_comm(TRP_REQ *req)
80 {
81   if (req!=NULL)
82     return req->comm;
83   else
84     return NULL;
85 }
86
87 void trp_req_set_comm(TRP_REQ *req, TR_NAME *comm)
88 {
89   if (req)
90     req->comm=comm;
91 }
92
93 TR_NAME *trp_req_get_realm(TRP_REQ *req)
94 {
95   if (req!=NULL)
96     return req->realm;
97   else
98     return NULL;
99 }
100
101
102 void trp_req_set_realm(TRP_REQ *req, TR_NAME *realm)
103 {
104   if (req)
105     req->realm=realm;
106 }
107
108 TR_NAME *trp_req_get_peer(TRP_REQ *req)
109 {
110   if (req!=NULL)
111     return req->peer;
112   else
113     return NULL;
114 }
115
116
117 void trp_req_set_peer(TRP_REQ *req, TR_NAME *peer)
118 {
119   if (req)
120     req->peer=peer;
121 }
122
123 /* Defines what we use as a wildcard for realm or community name.
124  * Must not be a valid name for either of those. Currently, we
125  * use the empty string. */
126 static int trp_req_name_is_wildcard(TR_NAME *name)
127 {
128   return (name!=NULL) && (name->len==0);
129 }
130
131 int trp_req_is_wildcard(TRP_REQ *req)
132 {
133   return (req!=NULL) && trp_req_name_is_wildcard(req->comm) && trp_req_name_is_wildcard(req->realm);
134 }
135
136 TRP_RC trp_req_make_wildcard(TRP_REQ *req)
137 {
138   if (req==NULL)
139     return TRP_BADARG;
140
141   req->comm=tr_new_name("");
142   if (req->comm==NULL)
143     return TRP_NOMEM;
144
145   req->realm=tr_new_name("");
146   if (req->realm==NULL) {
147     tr_free_name(req->comm);
148     req->comm=NULL;
149     return TRP_NOMEM;
150   }
151
152   return TRP_SUCCESS;
153 }