Use TR_LIST for domain/realm constraint matches
[trust_router.git] / common / tr_filter_encoders.c
1 /*
2  * Copyright (c) 2018 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 <talloc.h>
36 #include <jansson.h>
37
38 #include <tr_filter.h>
39 #include <tr_constraint_internal.h>
40
41 /* helper for below */
42 #define OBJECT_SET_OR_FAIL(jobj, key, val)     \
43 do {                                           \
44   if (val)                                     \
45     json_object_set_new((jobj),(key),(val));   \
46   else                                         \
47     goto cleanup;                              \
48 } while (0)
49
50 #define ARRAY_APPEND_OR_FAIL(jary, val)        \
51 do {                                           \
52   if (val)                                     \
53     json_array_append_new((jary),(val));       \
54   else                                         \
55     goto cleanup;                              \
56 } while (0)
57
58
59 typedef json_t *(ITEM_ENCODER_FUNC)(void *);
60
61 enum type_to_array {
62   TYPE_TO_ARRAY_FSPEC,
63   TYPE_TO_ARRAY_CONSTRAINT
64 };
65 static json_t *tr_names_to_json_array(void *obj, enum type_to_array type)
66 {
67   json_t *jarray = json_array();
68   json_t *retval = NULL;
69   TR_FSPEC_ITER fspec_iter = {0};
70   TR_CONSTRAINT_ITER cons_iter = {0};
71   TR_NAME *this_match = NULL;
72
73   if (jarray == NULL)
74     goto cleanup;
75
76   switch(type) {
77     case TYPE_TO_ARRAY_FSPEC:
78       this_match = tr_fspec_iter_first(&fspec_iter, (TR_FSPEC *)obj);
79       break;
80
81     case TYPE_TO_ARRAY_CONSTRAINT:
82       this_match = tr_constraint_iter_first(&cons_iter, (TR_CONSTRAINT *)obj);
83       break;
84   }
85   while(this_match) {
86     ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(this_match));
87     switch(type) {
88       case TYPE_TO_ARRAY_FSPEC:
89         this_match = tr_fspec_iter_next(&fspec_iter);
90         break;
91
92       case TYPE_TO_ARRAY_CONSTRAINT:
93         this_match = tr_constraint_iter_next(&cons_iter);
94         break;
95     }
96   }
97   /* success */
98   retval = jarray;
99   json_incref(retval);
100
101 cleanup:
102   if (jarray)
103     json_decref(jarray);
104
105   return retval;
106 }
107
108 static json_t *tr_fspec_to_json(TR_FSPEC *fspec)
109 {
110   json_t *fspec_json = NULL;
111   json_t *retval = NULL;
112
113   fspec_json = json_object();
114   if (fspec_json == NULL)
115     goto cleanup;
116
117   OBJECT_SET_OR_FAIL(fspec_json, "field",
118                      tr_name_to_json_string(fspec->field));
119   OBJECT_SET_OR_FAIL(fspec_json, "matches",
120                      tr_names_to_json_array(fspec, TYPE_TO_ARRAY_FSPEC));
121
122   /* succeeded - set the return value and increment the reference count */
123   retval = fspec_json;
124   json_incref(retval);
125
126 cleanup:
127   if (fspec_json)
128     json_decref(fspec_json);
129   return retval;
130 }
131
132 static json_t *tr_fspecs_to_json_array(TR_FLINE *fline)
133 {
134   json_t *jarray = json_array();
135   json_t *retval = NULL;
136   TR_FLINE_ITER *iter = tr_fline_iter_new(NULL);
137   TR_FSPEC *this_fspec = NULL;
138
139   if ((jarray == NULL) || (iter == NULL))
140     goto cleanup;
141
142   this_fspec = tr_fline_iter_first(iter, fline);
143   while(this_fspec) {
144     ARRAY_APPEND_OR_FAIL(jarray, tr_fspec_to_json(this_fspec));
145     this_fspec = tr_fline_iter_next(iter);
146   }
147   /* success */
148   retval = jarray;
149   json_incref(retval);
150
151 cleanup:
152   if (jarray)
153     json_decref(jarray);
154   if (iter)
155     tr_fline_iter_free(iter);
156
157   return retval;
158 }
159
160 static json_t *tr_fline_to_json(TR_FLINE *fline)
161 {
162   json_t *fline_json = NULL;
163   json_t *retval = NULL;
164
165   fline_json = json_object();
166   if (fline_json == NULL)
167     goto cleanup;
168
169   OBJECT_SET_OR_FAIL(fline_json, "action",
170                      json_string( (fline->action == TR_FILTER_ACTION_ACCEPT) ? "accept" : "reject"));
171   OBJECT_SET_OR_FAIL(fline_json, "specs",
172                      tr_fspecs_to_json_array(fline));
173   if (fline->realm_cons) {
174     OBJECT_SET_OR_FAIL(fline_json, "realm_constraints",
175                        tr_names_to_json_array(fline->realm_cons, TYPE_TO_ARRAY_CONSTRAINT));
176   }
177   if (fline->domain_cons) {
178     OBJECT_SET_OR_FAIL(fline_json, "domain_constraints",
179                        tr_names_to_json_array(fline->domain_cons, TYPE_TO_ARRAY_CONSTRAINT));
180   }
181
182   /* succeeded - set the return value and increment the reference count */
183   retval = fline_json;
184   json_incref(retval);
185
186 cleanup:
187   if (fline_json)
188     json_decref(fline_json);
189   return retval;
190 }
191
192 static json_t *tr_flines_to_json_array(TR_FILTER *filt)
193 {
194   json_t *jarray = json_array();
195   json_t *retval = NULL;
196   TR_FILTER_ITER *iter = tr_filter_iter_new(NULL);
197   TR_FLINE *this_fline = NULL;
198
199   if ((jarray == NULL) || (iter == NULL))
200     goto cleanup;
201
202   this_fline = tr_filter_iter_first(iter, filt);
203   while(this_fline) {
204     ARRAY_APPEND_OR_FAIL(jarray, tr_fline_to_json(this_fline));
205     this_fline = tr_filter_iter_next(iter);
206   }
207   /* success */
208   retval = jarray;
209   json_incref(retval);
210
211 cleanup:
212   if (jarray)
213     json_decref(jarray);
214   if (iter)
215     tr_filter_iter_free(iter);
216
217   return retval;
218 }
219 json_t *tr_filter_set_to_json(TR_FILTER_SET *filter_set)
220 {
221   json_t *fset_json = NULL;
222   json_t *retval = NULL;
223   TR_FILTER *filt = NULL;
224   TR_FILTER_TYPE *filt_type = NULL;
225   TR_FILTER_TYPE types[] = {
226       TR_FILTER_TYPE_TID_INBOUND,
227       TR_FILTER_TYPE_TRP_INBOUND,
228       TR_FILTER_TYPE_TRP_OUTBOUND,
229       TR_FILTER_TYPE_UNKNOWN /* list terminator */
230   };
231
232   fset_json = json_object();
233   if (fset_json == NULL)
234     goto cleanup;
235
236   for (filt_type = types; *filt_type != TR_FILTER_TYPE_UNKNOWN; filt_type++) {
237     filt = tr_filter_set_get(filter_set, *filt_type);
238     if (filt) {
239       OBJECT_SET_OR_FAIL(fset_json, tr_filter_type_to_string(*filt_type),
240                          tr_flines_to_json_array(filt));
241     }
242   }
243
244   /* succeeded - set the return value and increment the reference count */
245   retval = fset_json;
246   json_incref(retval);
247
248 cleanup:
249   if (fset_json)
250     json_decref(fset_json);
251   return retval;
252 }
253