1 /*
2  * The olsr.org Optimized Link-State Routing daemon (olsrd)
3  *
4  * (c) by the OLSR project
5  *
6  * See our Git repository to find out who worked on this file
7  * and thus is a copyright holder on it.
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  *   notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above copyright
18  *   notice, this list of conditions and the following disclaimer in
19  *   the documentation and/or other materials provided with the
20  *   distribution.
21  * * Neither the name of olsr.org, olsrd nor the names of its
22  *   contributors may be used to endorse or promote products derived
23  *   from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  * Visit http://www.olsr.org for more information.
39  *
40  * If you find this software useful feel free to make a donation
41  * to the project. For more information see the website or contact
42  * the copyright holders.
43  *
44  */
45 
46 #ifndef _OLSRD_LIB_INFO_INFO_TYPES_H_
47 #define _OLSRD_LIB_INFO_INFO_TYPES_H_
48 
49 #include <stdbool.h>
50 #include <assert.h>
51 #include <netinet/in.h>
52 
53 #include "common/autobuf.h"
54 
55 #define CACHE_TIMEOUT_DEFAULT 1000
56 #define REQUEST_TIMEOUT_DEFAULT 20
57 
58 typedef struct {
59     union olsr_ip_addr accept_ip;
60     union olsr_ip_addr listen_ip;
61     int ipc_port;
62     bool http_headers;
63     bool allow_localhost;
64     bool ipv6_only;
65     long cache_timeout;
66     long request_timeout;
67     long request_timeout_sec; /* derived */
68     long request_timeout_usec; /* derived */
69 } info_plugin_config_t;
70 
71 #define INFO_PLUGIN_CONFIG_PLUGIN_PARAMETERS(config) \
72   { .name = "port", .set_plugin_parameter = &set_plugin_port, .data = &config.ipc_port }, \
73   { .name = "accept", .set_plugin_parameter = &set_plugin_ipaddress, .data = &config.accept_ip }, \
74   { .name = "listen", .set_plugin_parameter = &set_plugin_ipaddress, .data = &config.listen_ip }, \
75   { .name = "httpheaders", .set_plugin_parameter = &set_plugin_boolean, .data = &config.http_headers }, \
76   { .name = "allowlocalhost", .set_plugin_parameter = &set_plugin_boolean, .data = &config.allow_localhost }, \
77   { .name = "ipv6only", .set_plugin_parameter = &set_plugin_boolean, .data = &config.ipv6_only },\
78   { .name = "cachetimeout", .set_plugin_parameter = &set_plugin_long, .data = &config.cache_timeout },\
79   { .name = "requesttimeout", .set_plugin_parameter = &set_plugin_long, .data = &config.request_timeout }
80 
81 /* these provide all of the runtime status info */
82 #define SIW_NEIGHBORS                    (1ULL <<  0)
83 #define SIW_LINKS                        (1ULL <<  1)
84 #define SIW_ROUTES                       (1ULL <<  2)
85 #define SIW_HNA                          (1ULL <<  3)
86 #define SIW_MID                          (1ULL <<  4)
87 #define SIW_TOPOLOGY                     (1ULL <<  5)
88 #define SIW_GATEWAYS                     (1ULL <<  6)
89 #define SIW_INTERFACES                   (1ULL <<  7)
90 #define SIW_2HOP                         (1ULL <<  8)
91 #define SIW_SGW                          (1ULL <<  9)
92 #define SIW_PUD_POSITION                 (1ULL << 10)
93 #define SIW_RUNTIME_ALL                  (SIW_NEIGHBORS | SIW_LINKS | SIW_ROUTES | SIW_HNA | SIW_MID | SIW_TOPOLOGY | SIW_GATEWAYS | SIW_INTERFACES | SIW_2HOP | SIW_SGW | SIW_PUD_POSITION)
94 #define SIW_NEIGHBORS_FREIFUNK           (SIW_NEIGHBORS | SIW_LINKS) /* special */
95 
96 /* these only change at olsrd startup */
97 #define SIW_VERSION                      (1ULL << 11)
98 #define SIW_CONFIG                       (1ULL << 12)
99 #define SIW_PLUGINS                      (1ULL << 13)
100 #define SIW_STARTUP_ALL                  (SIW_VERSION | SIW_CONFIG | SIW_PLUGINS)
101 
102 /* this is everything in normal format */
103 #define SIW_ALL                          (SIW_RUNTIME_ALL | SIW_STARTUP_ALL)
104 
105 /* this data is not normal format but olsrd.conf format */
106 #define SIW_OLSRD_CONF                   (1ULL << 14)
107 
108 /* netjson */
109 #define SIW_NETJSON_NETWORK_ROUTES       (1ULL << 15)
110 #define SIW_NETJSON_NETWORK_GRAPH        (1ULL << 16)
111 #define SIW_NETJSON_DEVICE_CONFIGURATION (1ULL << 17)
112 #define SIW_NETJSON_DEVICE_MONITORING    (1ULL << 18)
113 #define SIW_NETJSON_NETWORK_COLLECTION   (1ULL << 19)
114 #define SIW_NETJSON                      (SIW_NETJSON_NETWORK_ROUTES | SIW_NETJSON_NETWORK_GRAPH | SIW_NETJSON_DEVICE_CONFIGURATION | SIW_NETJSON_DEVICE_MONITORING | SIW_NETJSON_NETWORK_COLLECTION)
115 
116 /* poprouting */
117 #define SIW_POPROUTING_HELLO             (1ULL << 20)
118 #define SIW_POPROUTING_TC                (1ULL << 21)
119 #define SIW_POPROUTING_HELLO_MULT        (1ULL << 22)
120 #define SIW_POPROUTING_TC_MULT           (1ULL << 23)
121 #define SIW_POPROUTING                   (SIW_POPROUTING_HELLO | SIW_POPROUTING_TC | SIW_POPROUTING_HELLO_MULT | SIW_POPROUTING_TC_MULT)
122 
123 /* everything */
124 #define SIW_EVERYTHING                   ((SIW_POPROUTING_TC_MULT << 1) - 1)
125 
126 /* command prefixes */
127 #define SIW_PREFIX_HTTP                  "/http"
128 #define SIW_PREFIX_HTTP_LEN              (sizeof(SIW_PREFIX_HTTP) - 1)
129 #define SIW_PREFIX_PLAIN                 "/plain"
130 #define SIW_PREFIX_PLAIN_LEN             (sizeof(SIW_PREFIX_PLAIN) - 1)
131 
132 typedef void (*init_plugin)(const char *plugin_name);
133 typedef unsigned long long (*supported_commands_mask_func)(void);
134 typedef bool (*command_matcher)(const char *str, unsigned long long siw);
135 typedef long (*cache_timeout_func)(info_plugin_config_t *plugin_config, unsigned long long siw);
136 typedef const char * (*mime_type)(unsigned int send_what);
137 typedef void (*output_start_end)(struct autobuf *abuf);
138 typedef void (*printer_error)(struct autobuf *abuf, unsigned int status, const char * req, bool http_headers);
139 typedef void (*printer_generic)(struct autobuf *abuf);
140 
141 typedef struct {
142     bool supportsCompositeCommands;
143     init_plugin init;
144     supported_commands_mask_func supported_commands_mask;
145     command_matcher is_command;
146     cache_timeout_func cache_timeout;
147     mime_type determine_mime_type;
148     output_start_end output_start;
149     output_start_end output_end;
150     printer_error output_error;
151     printer_generic neighbors;
152     printer_generic links;
153     printer_generic routes;
154     printer_generic topology;
155     printer_generic hna;
156     printer_generic mid;
157     printer_generic gateways;
158     printer_generic sgw;
159     printer_generic pudPosition;
160 
161     printer_generic version;
162     printer_generic olsrd_conf;
163     printer_generic interfaces;
164     printer_generic twohop;
165     printer_generic config;
166     printer_generic plugins;
167 
168     printer_generic networkRoutes;
169     printer_generic networkGraph;
170     printer_generic deviceConfiguration;
171     printer_generic deviceMonitoring;
172     printer_generic networkCollection;
173 
174     printer_generic tcTimer;
175     printer_generic helloTimer;
176     printer_generic tcTimerMult;
177     printer_generic helloTimerMult;
178 } info_plugin_functions_t;
179 
180 struct info_cache_entry_t {
181     long long timestamp;
182     struct autobuf buf;
183 };
184 
185 struct info_cache_t {
186     struct info_cache_entry_t neighbors;
187     struct info_cache_entry_t links;
188     struct info_cache_entry_t routes;
189     struct info_cache_entry_t hna;
190     struct info_cache_entry_t mid;
191     struct info_cache_entry_t topology;
192     struct info_cache_entry_t gateways;
193     struct info_cache_entry_t interfaces;
194     struct info_cache_entry_t twohop;
195     struct info_cache_entry_t sgw;
196     struct info_cache_entry_t pudPosition;
197 
198     struct info_cache_entry_t version;
199     struct info_cache_entry_t config;
200     struct info_cache_entry_t plugins;
201 
202     struct info_cache_entry_t networkRoutes;
203     struct info_cache_entry_t networkGraph;
204     struct info_cache_entry_t deviceConfiguration;
205     struct info_cache_entry_t deviceMonitoring;
206     struct info_cache_entry_t networkCollection;
207 };
208 
info_cache_get_entry(struct info_cache_t * cache,unsigned long long siw)209 static INLINE struct info_cache_entry_t * info_cache_get_entry(struct info_cache_t * cache, unsigned long long siw) {
210   struct info_cache_entry_t * r = NULL;
211 
212   if (!cache) {
213     return r;
214   }
215 
216   switch (siw) {
217     case SIW_NEIGHBORS:
218       r = &cache->neighbors;
219       break;
220 
221     case SIW_LINKS:
222       r = &cache->links;
223       break;
224 
225     case SIW_ROUTES:
226       r = &cache->routes;
227       break;
228 
229     case SIW_HNA:
230       r = &cache->hna;
231       break;
232 
233     case SIW_MID:
234       r = &cache->mid;
235       break;
236 
237     case SIW_TOPOLOGY:
238       r = &cache->topology;
239       break;
240 
241     case SIW_GATEWAYS:
242       r = &cache->gateways;
243       break;
244 
245     case SIW_INTERFACES:
246       r = &cache->interfaces;
247       break;
248 
249     case SIW_2HOP:
250       r = &cache->twohop;
251       break;
252 
253     case SIW_SGW:
254       r = &cache->sgw;
255       break;
256 
257     case SIW_PUD_POSITION:
258       r = &cache->pudPosition;
259       break;
260 
261     case SIW_VERSION:
262       r = &cache->version;
263       break;
264 
265     case SIW_CONFIG:
266       r = &cache->config;
267       break;
268 
269     case SIW_PLUGINS:
270       r = &cache->plugins;
271       break;
272 
273     case SIW_NETJSON_NETWORK_ROUTES:
274       r = &cache->networkRoutes;
275       break;
276 
277     case SIW_NETJSON_NETWORK_GRAPH:
278       r = &cache->networkGraph;
279       break;
280 
281     case SIW_NETJSON_DEVICE_CONFIGURATION:
282       r = &cache->deviceConfiguration;
283       break;
284 
285     case SIW_NETJSON_DEVICE_MONITORING:
286       r = &cache->deviceMonitoring;
287       break;
288 
289     case SIW_NETJSON_NETWORK_COLLECTION:
290       r = &cache->networkCollection;
291       break;
292 
293     default:
294       /* not cached */
295       break;
296   }
297 
298   return r;
299 }
300 
info_plugin_config_init(info_plugin_config_t * config,unsigned short port)301 static INLINE void info_plugin_config_init(info_plugin_config_t *config, unsigned short port) {
302   assert(config);
303 
304   if (olsr_cnf->ip_version == AF_INET) {
305     config->accept_ip.v4.s_addr = htonl(INADDR_LOOPBACK);
306     config->listen_ip.v4.s_addr = htonl(INADDR_ANY);
307   } else {
308     config->accept_ip.v6 = in6addr_loopback;
309     config->listen_ip.v6 = in6addr_any;
310   }
311 
312   config->ipc_port = port;
313   config->http_headers = true;
314   config->allow_localhost = false;
315   config->ipv6_only = false;
316   config->cache_timeout = CACHE_TIMEOUT_DEFAULT;
317   config->request_timeout = REQUEST_TIMEOUT_DEFAULT;
318 }
319 
320 #endif /* _OLSRD_LIB_INFO_INFO_TYPES_H_ */
321