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