Log incoming IP address when accepting a connection
[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_fspec_to_json(TR_FSPEC *fspec)
85 {
86   json_t *fspec_json = NULL;
87   json_t *retval = NULL;
88
89   fspec_json = json_object();
90   if (fspec_json == NULL)
91     goto cleanup;
92
93   OBJECT_SET_OR_FAIL(fspec_json, "field",
94                      tr_name_to_json_string(fspec->field));
95   OBJECT_SET_OR_FAIL(fspec_json, "matches",
96                      items_to_json_array((void **)fspec->match,
97                                          (ITEM_ENCODER_FUNC *) tr_name_to_json_string,
98                                          TR_MAX_FILTER_SPEC_MATCHES));
99
100   /* succeeded - set the return value and increment the reference count */
101   retval = fspec_json;
102   json_incref(retval);
103
104 cleanup:
105   if (fspec_json)
106     json_decref(fspec_json);
107   return retval;
108 }
109
110 static json_t *tr_fline_to_json(TR_FLINE *fline)
111 {
112   json_t *fline_json = NULL;
113   json_t *retval = NULL;
114
115   fline_json = json_object();
116   if (fline_json == NULL)
117     goto cleanup;
118
119   OBJECT_SET_OR_FAIL(fline_json, "action",
120                      json_string( (fline->action == TR_FILTER_ACTION_ACCEPT) ? "accept" : "reject"));
121   OBJECT_SET_OR_FAIL(fline_json, "specs",
122                      items_to_json_array((void **)fline->specs,
123                                          (ITEM_ENCODER_FUNC *) tr_fspec_to_json,
124                                          TR_MAX_FILTER_SPECS));
125   if (fline->realm_cons) {
126     OBJECT_SET_OR_FAIL(fline_json, "realm_constraints",
127                        items_to_json_array((void **) fline->realm_cons->matches,
128                                            (ITEM_ENCODER_FUNC *) tr_name_to_json_string,
129                                            TR_MAX_CONST_MATCHES));
130   }
131   if (fline->domain_cons) {
132     OBJECT_SET_OR_FAIL(fline_json, "domain_constraints",
133                        items_to_json_array((void **) fline->domain_cons->matches,
134                                            (ITEM_ENCODER_FUNC *) tr_name_to_json_string,
135                                            TR_MAX_CONST_MATCHES));
136   }
137
138   /* succeeded - set the return value and increment the reference count */
139   retval = fline_json;
140   json_incref(retval);
141
142 cleanup:
143   if (fline_json)
144     json_decref(fline_json);
145   return retval;
146 }
147
148 json_t *tr_filter_set_to_json(TR_FILTER_SET *filter_set)
149 {
150   json_t *fset_json = NULL;
151   json_t *retval = NULL;
152   TR_FILTER *filt = NULL;
153   TR_FILTER_TYPE *filt_type = NULL;
154   TR_FILTER_TYPE types[] = {
155       TR_FILTER_TYPE_TID_INBOUND,
156       TR_FILTER_TYPE_TRP_INBOUND,
157       TR_FILTER_TYPE_TRP_OUTBOUND,
158       TR_FILTER_TYPE_UNKNOWN /* list terminator */
159   };
160
161   fset_json = json_object();
162   if (fset_json == NULL)
163     goto cleanup;
164
165   for (filt_type = types; *filt_type != TR_FILTER_TYPE_UNKNOWN; filt_type++) {
166     filt = tr_filter_set_get(filter_set, *filt_type);
167     if (filt) {
168       OBJECT_SET_OR_FAIL(fset_json, tr_filter_type_to_string(*filt_type),
169                          items_to_json_array((void **)filt->lines,
170                                              (ITEM_ENCODER_FUNC *) tr_fline_to_json,
171                                              TR_MAX_FILTER_LINES));
172     }
173   }
174
175   /* succeeded - set the return value and increment the reference count */
176   retval = fset_json;
177   json_incref(retval);
178
179 cleanup:
180   if (fset_json)
181     json_decref(fset_json);
182   return retval;
183 }
184