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 static outbuffer_t outbuffer;
86 static struct timer_entry *writetimer_entry;
88 static printer_functions_t printer_functions = { //
90 .init = &plugin_init, //
91 .determine_mime_type = &determine_mime_type, //
92 .neighbors = &ipc_print_neighbors, //
93 .links = &ipc_print_links, //
94 .routes = &ipc_print_routes, //
95 .topology = &ipc_print_topology, //
96 .hna = &ipc_print_hna, //
97 .mid = &ipc_print_mid, //
98 .gateways = &ipc_print_gateways, //
99 .sgw = &ipc_print_sgw, //
100 .version = &ipc_print_version, //
101 .olsrd_conf = &ipc_print_olsrd_conf, //
102 .interfaces = &ipc_print_interfaces, //
103 .config = &ipc_print_config, //
104 .plugins = &ipc_print_plugins //
107 static void determine_action(unsigned int *send_what, char *requ) {
108 if (strstr(requ, "/olsrd.conf"))
109 *send_what |= SIW_OLSRD_CONF;
110 else if (strstr(requ, "/all"))
111 *send_what = SIW_ALL;
113 // these are the two overarching categories
114 if (strstr(requ, "/runtime"))
115 *send_what |= SIW_RUNTIME_ALL;
116 if (strstr(requ, "/startup"))
117 *send_what |= SIW_STARTUP_ALL;
119 // these are the individual sections
120 if (strstr(requ, "/neighbors"))
121 *send_what |= SIW_NEIGHBORS;
122 if (strstr(requ, "/links"))
123 *send_what |= SIW_LINKS;
124 if (strstr(requ, "/routes"))
125 *send_what |= SIW_ROUTES;
126 if (strstr(requ, "/hna"))
127 *send_what |= SIW_HNA;
128 if (strstr(requ, "/mid"))
129 *send_what |= SIW_MID;
130 if (strstr(requ, "/topology"))
131 *send_what |= SIW_TOPOLOGY;
132 if (strstr(requ, "/gateways"))
133 *send_what |= SIW_GATEWAYS;
134 if (strstr(requ, "/interfaces"))
135 *send_what |= SIW_INTERFACES;
136 if (strstr(requ, "/2hop"))
137 *send_what |= SIW_2HOP;
138 if (strstr(requ, "/sgw"))
139 *send_what |= SIW_SGW;
142 if (strstr(requ, "/version"))
143 *send_what |= SIW_VERSION;
144 if (strstr(requ, "/config"))
145 *send_what |= SIW_CONFIG;
146 if (strstr(requ, "/plugins"))
147 *send_what |= SIW_PLUGINS;
149 /* To print out neighbours only on the Freifunk Status
150 * page the normal output is somewhat lengthy. The
151 * header parsing is sufficient for standard wget.
153 if (strstr(requ, "/neighbours"))
154 *send_what = SIW_NEIGHBORS | SIW_LINKS;
159 *Do initialization here
161 *This function is called by the my_init
162 *function in uolsrd_plugin.c
164 int olsrd_plugin_init(void) {
165 /* Initial IPC value */
167 memset(&outbuffer, 0, sizeof(outbuffer));
169 if (printer_functions.init) {
170 (*printer_functions.init)(PLUGIN_NAME);
178 * destructor - called at unload
180 void olsr_plugin_exit(void) {
181 if (ipc_socket != -1)
185 static int plugin_ipc_init(void) {
186 union olsr_sockaddr sst;
190 /* Init ipc socket */
191 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
193 olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
197 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
199 olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
203 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
204 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *) &yes, sizeof(yes)) < 0) {
205 perror("SO_REUSEADDR failed");
208 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
209 #if defined linux && defined IPV6_V6ONLY
210 if (info_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
211 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
212 perror("IPV6_V6ONLY failed");
216 #endif /* defined linux && defined IPV6_V6ONLY */
217 /* Bind the socket */
219 /* complete the socket structure */
220 memset(&sst, 0, sizeof(sst));
221 if (olsr_cnf->ip_version == AF_INET) {
222 sst.in4.sin_family = AF_INET;
223 addrlen = sizeof(struct sockaddr_in);
225 sst.in4.sin_len = addrlen;
226 #endif /* SIN6_LEN */
227 sst.in4.sin_addr.s_addr = info_listen_ip.v4.s_addr;
228 sst.in4.sin_port = htons(ipc_port);
230 sst.in6.sin6_family = AF_INET6;
231 addrlen = sizeof(struct sockaddr_in6);
233 sst.in6.sin6_len = addrlen;
234 #endif /* SIN6_LEN */
235 sst.in6.sin6_addr = info_listen_ip.v6;
236 sst.in6.sin6_port = htons(ipc_port);
239 /* bind the socket to the port number */
240 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
242 olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
247 /* show that we are willing to listen */
248 if (listen(ipc_socket, 1) == -1) {
250 olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
255 /* Register with olsrd */
256 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
259 olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
265 static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
266 union olsr_sockaddr pin;
268 char addr[INET6_ADDRSTRLEN];
271 unsigned int send_what = 0;
274 socklen_t addrlen = sizeof(pin);
276 if (outbuffer.count >= MAX_CLIENTS) {
280 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
282 olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
287 tv.tv_sec = tv.tv_usec = 0;
288 if (olsr_cnf->ip_version == AF_INET) {
289 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
291 if (!ip4equal(&pin.in4.sin_addr, &info_accept_ip.v4) && info_accept_ip.v4.s_addr != INADDR_ANY) {
292 #ifdef INFO_ALLOW_LOCALHOST
293 if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
294 #endif /* INFO_ALLOW_LOCALHOST */
295 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
296 close(ipc_connection);
298 #ifdef INFO_ALLOW_LOCALHOST
300 #endif /* INFO_ALLOW_LOCALHOST */
303 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
305 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
306 if (!ip6equal(&in6addr_any, &info_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &info_accept_ip.v6)) {
307 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
308 close(ipc_connection);
314 olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
317 /* purge read buffer to prevent blocking on linux */
319 FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
320 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
322 ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
324 if (s == sizeof(requ) - 1) {
325 /* input was much too long, just skip the rest */
328 while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
334 determine_action(&send_what, requ);
341 send_info(send_what, ipc_connection);
344 static void info_write_data(void *foo __attribute__ ((unused))) {
346 int result, i, j, max;
351 for (i = 0; i < outbuffer.count; i++) {
352 /* And we cast here since we get a warning on Win32 */
353 FD_SET((unsigned int )(outbuffer.socket[i]), &set);
355 if (outbuffer.socket[i] > max) {
356 max = outbuffer.socket[i];
363 result = select(max + 1, NULL, &set, NULL, &tv);
368 for (i = 0; i < outbuffer.count; i++) {
369 if (FD_ISSET(outbuffer.socket[i], &set)) {
370 result = send(outbuffer.socket[i], outbuffer.buffer[i] + outbuffer.written[i], outbuffer.size[i] - outbuffer.written[i], 0);
372 outbuffer.written[i] += result;
375 if (result <= 0 || outbuffer.written[i] == outbuffer.size[i]) {
376 /* close this socket and cleanup*/
377 close(outbuffer.socket[i]);
378 free(outbuffer.buffer[i]);
379 outbuffer.buffer[i] = NULL;
381 for (j = i + 1; j < outbuffer.count; j++) {
382 outbuffer.buffer[j - 1] = outbuffer.buffer[j];
383 outbuffer.size[j - 1] = outbuffer.size[j];
384 outbuffer.socket[j - 1] = outbuffer.socket[j];
385 outbuffer.written[j - 1] = outbuffer.written[j];
391 if (!outbuffer.count) {
392 olsr_stop_timer(writetimer_entry);
396 static void send_info(unsigned int send_what, int the_socket) {
399 const char *content_type = (printer_functions.determine_mime_type) ? (*printer_functions.determine_mime_type)(send_what) : "text/plain; charset=utf-8";
400 int contentLengthPlaceholderStart = 0;
401 int headerLength = 0;
403 abuf_init(&abuf, 2 * 4096);
406 build_http_header(PLUGIN_NAME, HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
407 headerLength = abuf.len;
410 // only add if normal format
411 if (send_what & SIW_ALL) {
412 /* global variables for tracking when to put a comma in for JSON */
413 abuf_json_reset_entry_number_and_depth();
414 abuf_json_mark_output(true, &abuf);
416 abuf_json_int(&abuf, "systemTime", time(NULL));
417 abuf_json_int(&abuf, "timeSinceStartup", now_times);
419 abuf_json_string(&abuf, "uuid", uuid);
421 if ((send_what & SIW_LINKS) && printer_functions.links)
422 (*printer_functions.links)(&abuf);
423 if ((send_what & SIW_NEIGHBORS) && printer_functions.neighbors)
424 (*printer_functions.neighbors)(&abuf, false);
425 if ((send_what & SIW_TOPOLOGY) && printer_functions.topology)
426 (*printer_functions.topology)(&abuf);
427 if ((send_what & SIW_HNA) && printer_functions.hna)
428 (*printer_functions.hna)(&abuf);
429 if ((send_what & SIW_SGW) && printer_functions.sgw)
430 (*printer_functions.sgw)(&abuf);
431 if ((send_what & SIW_MID) && printer_functions.mid)
432 (*printer_functions.mid)(&abuf);
433 if ((send_what & SIW_ROUTES) && printer_functions.routes)
434 (*printer_functions.routes)(&abuf);
435 if ((send_what & SIW_GATEWAYS) && printer_functions.gateways)
436 (*printer_functions.gateways)(&abuf);
437 if ((send_what & SIW_CONFIG) && printer_functions.config)
438 (*printer_functions.config)(&abuf);
439 if ((send_what & SIW_INTERFACES) && printer_functions.interfaces)
440 (*printer_functions.interfaces)(&abuf);
441 if ((send_what & SIW_2HOP) && printer_functions.neighbors)
442 (*printer_functions.neighbors)(&abuf, true);
443 if ((send_what & SIW_VERSION) && printer_functions.version)
444 (*printer_functions.version)(&abuf);
445 if ((send_what & SIW_PLUGINS) && printer_functions.plugins)
446 (*printer_functions.plugins)(&abuf);
448 abuf_json_mark_output(false, &abuf);
449 abuf_puts(&abuf, "\n");
450 } else if ((send_what & SIW_OLSRD_CONF) && printer_functions.olsrd_conf) {
451 /* this outputs the olsrd.conf text directly, not normal format */
452 (*printer_functions.olsrd_conf)(&abuf);
456 http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
459 /* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
460 outbuffer.buffer[outbuffer.count] = abuf.buf;
461 outbuffer.size[outbuffer.count] = abuf.len;
462 outbuffer.written[outbuffer.count] = 0;
463 outbuffer.socket[outbuffer.count] = the_socket;
470 if (outbuffer.count == 1) {
471 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
482 * indent-tabs-mode: nil