2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
43 * Dynamic linked library for the olsr.org olsr daemon
46 #include <arpa/inet.h>
53 #include "scheduler.h"
54 #include "../../info/http_headers.h"
55 #include "../../info/info_types.h"
56 #include "jsoninfo_printers.h"
57 #include "olsrd_jsoninfo_helpers.h"
58 #include "olsrd_jsoninfo.h"
61 #define close(x) closesocket(x)
64 /* defines to make txtinfo and jsoninfo look alike */
65 #define PLUGIN_NAME "JSONINFO"
66 #define info_accept_ip jsoninfo_accept_ip
67 #define info_listen_ip jsoninfo_listen_ip
68 #define info_ipv6_only jsoninfo_ipv6_only
69 #ifdef JSONINFO_ALLOW_LOCALHOST
70 #define INFO_ALLOW_LOCALHOST JSONINFO_ALLOW_LOCALHOST
73 static int ipc_socket;
75 /* IPC initialization function */
76 static int plugin_ipc_init(void);
78 static void send_info(unsigned int /*send_what*/, int /*socket*/);
80 static void ipc_action(int, void *, unsigned int);
82 #define TXT_IPC_BUFSIZE 256
84 /* these provide all of the runtime status info */
85 #define SIW_NEIGHBORS 0x0001
86 #define SIW_LINKS 0x0002
87 #define SIW_ROUTES 0x0004
88 #define SIW_HNA 0x0008
89 #define SIW_MID 0x0010
90 #define SIW_TOPOLOGY 0x0020
91 #define SIW_GATEWAYS 0x0040
92 #define SIW_INTERFACES 0x0080
93 #define SIW_2HOP 0x0100
94 #define SIW_SGW 0x0200
95 #define SIW_RUNTIME_ALL (SIW_NEIGHBORS | SIW_LINKS | SIW_ROUTES | SIW_HNA | SIW_MID | SIW_TOPOLOGY | SIW_GATEWAYS | SIW_INTERFACES | SIW_2HOP | SIW_SGW)
97 /* these only change at olsrd startup */
98 #define SIW_VERSION 0x0400
99 #define SIW_CONFIG 0x0800
100 #define SIW_PLUGINS 0x1000
101 #define SIW_STARTUP_ALL (SIW_VERSION | SIW_CONFIG | SIW_PLUGINS)
103 /* this is everything in normal format */
104 #define SIW_ALL (SIW_RUNTIME_ALL | SIW_STARTUP_ALL)
106 /* this data is not normal format but olsrd.conf format */
107 #define SIW_OLSRD_CONF 0x2000
109 #define MAX_CLIENTS 3
111 static char *outbuffer[MAX_CLIENTS];
112 static size_t outbuffer_size[MAX_CLIENTS];
113 static size_t outbuffer_written[MAX_CLIENTS];
114 static int outbuffer_socket[MAX_CLIENTS];
115 static int outbuffer_count = 0;
117 static struct timer_entry *writetimer_entry;
119 static printer_functions_t printer_functions = { //
121 .neighbors = &ipc_print_neighbors, //
122 .links = &ipc_print_links, //
123 .routes = &ipc_print_routes, //
124 .topology = &ipc_print_topology, //
125 .hna = &ipc_print_hna, //
126 .mid = &ipc_print_mid, //
127 .gateways = &ipc_print_gateways, //
128 .sgw = &ipc_print_sgw, //
129 .version = &ipc_print_version, //
130 .olsrd_conf = &ipc_print_olsrd_conf, //
131 .interfaces = &ipc_print_interfaces, //
132 .config = &ipc_print_config, //
133 .plugins = &ipc_print_plugins //
136 static void determine_action(unsigned int *send_what, char *requ) {
137 if (strstr(requ, "/olsrd.conf"))
138 *send_what |= SIW_OLSRD_CONF;
139 else if (strstr(requ, "/all"))
140 *send_what = SIW_ALL;
142 // these are the two overarching categories
143 if (strstr(requ, "/runtime"))
144 *send_what |= SIW_RUNTIME_ALL;
145 if (strstr(requ, "/startup"))
146 *send_what |= SIW_STARTUP_ALL;
148 // these are the individual sections
149 if (strstr(requ, "/neighbors"))
150 *send_what |= SIW_NEIGHBORS;
151 if (strstr(requ, "/links"))
152 *send_what |= SIW_LINKS;
153 if (strstr(requ, "/routes"))
154 *send_what |= SIW_ROUTES;
155 if (strstr(requ, "/hna"))
156 *send_what |= SIW_HNA;
157 if (strstr(requ, "/mid"))
158 *send_what |= SIW_MID;
159 if (strstr(requ, "/topology"))
160 *send_what |= SIW_TOPOLOGY;
161 if (strstr(requ, "/gateways"))
162 *send_what |= SIW_GATEWAYS;
163 if (strstr(requ, "/interfaces"))
164 *send_what |= SIW_INTERFACES;
165 if (strstr(requ, "/2hop"))
166 *send_what |= SIW_2HOP;
167 if (strstr(requ, "/sgw"))
168 *send_what |= SIW_SGW;
171 if (strstr(requ, "/version"))
172 *send_what |= SIW_VERSION;
173 if (strstr(requ, "/config"))
174 *send_what |= SIW_CONFIG;
175 if (strstr(requ, "/plugins"))
176 *send_what |= SIW_PLUGINS;
178 /* To print out neighbours only on the Freifunk Status
179 * page the normal output is somewhat lengthy. The
180 * header parsing is sufficient for standard wget.
182 if (strstr(requ, "/neighbours"))
183 *send_what = SIW_NEIGHBORS | SIW_LINKS;
188 *Do initialization here
190 *This function is called by the my_init
191 *function in uolsrd_plugin.c
193 int olsrd_plugin_init(void) {
194 /* Initial IPC value */
197 plugin_init(PLUGIN_NAME);
204 * destructor - called at unload
206 void olsr_plugin_exit(void) {
207 if (ipc_socket != -1)
211 static int plugin_ipc_init(void) {
212 union olsr_sockaddr sst;
216 /* Init ipc socket */
217 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
219 olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
223 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
225 olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
229 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
230 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *) &yes, sizeof(yes)) < 0) {
231 perror("SO_REUSEADDR failed");
234 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
235 #if defined linux && defined IPV6_V6ONLY
236 if (info_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
237 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
238 perror("IPV6_V6ONLY failed");
242 #endif /* defined linux && defined IPV6_V6ONLY */
243 /* Bind the socket */
245 /* complete the socket structure */
246 memset(&sst, 0, sizeof(sst));
247 if (olsr_cnf->ip_version == AF_INET) {
248 sst.in4.sin_family = AF_INET;
249 addrlen = sizeof(struct sockaddr_in);
251 sst.in4.sin_len = addrlen;
252 #endif /* SIN6_LEN */
253 sst.in4.sin_addr.s_addr = info_listen_ip.v4.s_addr;
254 sst.in4.sin_port = htons(ipc_port);
256 sst.in6.sin6_family = AF_INET6;
257 addrlen = sizeof(struct sockaddr_in6);
259 sst.in6.sin6_len = addrlen;
260 #endif /* SIN6_LEN */
261 sst.in6.sin6_addr = info_listen_ip.v6;
262 sst.in6.sin6_port = htons(ipc_port);
265 /* bind the socket to the port number */
266 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
268 olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
273 /* show that we are willing to listen */
274 if (listen(ipc_socket, 1) == -1) {
276 olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
281 /* Register with olsrd */
282 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
285 olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
291 static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
292 union olsr_sockaddr pin;
294 char addr[INET6_ADDRSTRLEN];
297 unsigned int send_what = 0;
300 socklen_t addrlen = sizeof(pin);
302 if (outbuffer_count >= MAX_CLIENTS) {
306 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
308 olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
313 tv.tv_sec = tv.tv_usec = 0;
314 if (olsr_cnf->ip_version == AF_INET) {
315 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
317 if (!ip4equal(&pin.in4.sin_addr, &info_accept_ip.v4) && info_accept_ip.v4.s_addr != INADDR_ANY) {
318 #ifdef INFO_ALLOW_LOCALHOST
319 if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
320 #endif /* INFO_ALLOW_LOCALHOST */
321 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
322 close(ipc_connection);
324 #ifdef INFO_ALLOW_LOCALHOST
326 #endif /* INFO_ALLOW_LOCALHOST */
329 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
331 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
332 if (!ip6equal(&in6addr_any, &info_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &info_accept_ip.v6)) {
333 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
334 close(ipc_connection);
340 olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
343 /* purge read buffer to prevent blocking on linux */
345 FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
346 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
348 ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
350 if (s == sizeof(requ) - 1) {
351 /* input was much too long, just skip the rest */
354 while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
360 determine_action(&send_what, requ);
367 send_info(send_what, ipc_connection);
370 static void info_write_data(void *foo __attribute__ ((unused))) {
372 int result, i, j, max;
377 for (i = 0; i < outbuffer_count; i++) {
378 /* And we cast here since we get a warning on Win32 */
379 FD_SET((unsigned int )(outbuffer_socket[i]), &set);
381 if (outbuffer_socket[i] > max) {
382 max = outbuffer_socket[i];
389 result = select(max + 1, NULL, &set, NULL, &tv);
394 for (i = 0; i < outbuffer_count; i++) {
395 if (FD_ISSET(outbuffer_socket[i], &set)) {
396 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
398 outbuffer_written[i] += result;
401 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
402 /* close this socket and cleanup*/
403 close(outbuffer_socket[i]);
406 for (j = i + 1; j < outbuffer_count; j++) {
407 outbuffer[j - 1] = outbuffer[j];
408 outbuffer_size[j - 1] = outbuffer_size[j];
409 outbuffer_socket[j - 1] = outbuffer_socket[j];
410 outbuffer_written[j - 1] = outbuffer_written[j];
416 if (!outbuffer_count) {
417 olsr_stop_timer(writetimer_entry);
421 static void send_info(unsigned int send_what, int the_socket) {
424 const char *content_type = (send_what & SIW_ALL) ? "application/json; charset=utf-8" : "text/plain; charset=utf-8";
425 int contentLengthPlaceholderStart = 0;
426 int headerLength = 0;
428 /* global variables for tracking when to put a comma in for JSON */
429 abuf_json_reset_entry_number_and_depth();
431 abuf_init(&abuf, 2 * 4096);
434 build_http_header(PLUGIN_NAME, HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
435 headerLength = abuf.len;
438 // only add if normal format
439 if (send_what & SIW_ALL) {
440 abuf_json_mark_output(true, &abuf);
442 abuf_json_int(&abuf, "systemTime", time(NULL));
443 abuf_json_int(&abuf, "timeSinceStartup", now_times);
445 abuf_json_string(&abuf, "uuid", uuid);
447 if ((send_what & SIW_LINKS) && printer_functions.links)
448 (*printer_functions.links)(&abuf);
449 if ((send_what & SIW_NEIGHBORS) && printer_functions.neighbors)
450 (*printer_functions.neighbors)(&abuf, false);
451 if ((send_what & SIW_TOPOLOGY) && printer_functions.topology)
452 (*printer_functions.topology)(&abuf);
453 if ((send_what & SIW_HNA) && printer_functions.hna)
454 (*printer_functions.hna)(&abuf);
455 if ((send_what & SIW_SGW) && printer_functions.sgw)
456 (*printer_functions.sgw)(&abuf);
457 if ((send_what & SIW_MID) && printer_functions.mid)
458 (*printer_functions.mid)(&abuf);
459 if ((send_what & SIW_ROUTES) && printer_functions.routes)
460 (*printer_functions.routes)(&abuf);
461 if ((send_what & SIW_GATEWAYS) && printer_functions.gateways)
462 (*printer_functions.gateways)(&abuf);
463 if ((send_what & SIW_CONFIG) && printer_functions.config)
464 (*printer_functions.config)(&abuf);
465 if ((send_what & SIW_INTERFACES) && printer_functions.interfaces)
466 (*printer_functions.interfaces)(&abuf);
467 if ((send_what & SIW_2HOP) && printer_functions.neighbors)
468 (*printer_functions.neighbors)(&abuf, true);
469 if ((send_what & SIW_VERSION) && printer_functions.version)
470 (*printer_functions.version)(&abuf);
471 if ((send_what & SIW_PLUGINS) && printer_functions.plugins)
472 (*printer_functions.plugins)(&abuf);
474 abuf_json_mark_output(false, &abuf);
475 abuf_puts(&abuf, "\n");
476 } else if ((send_what & SIW_OLSRD_CONF) && printer_functions.olsrd_conf) {
477 /* this outputs the olsrd.conf text directly, not normal format */
478 (*printer_functions.olsrd_conf)(&abuf);
482 http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
485 /* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
486 outbuffer[outbuffer_count] = abuf.buf;
487 outbuffer_size[outbuffer_count] = abuf.len;
488 outbuffer_written[outbuffer_count] = 0;
489 outbuffer_socket[outbuffer_count] = the_socket;
496 if (outbuffer_count == 1) {
497 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
508 * indent-tabs-mode: nil