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