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 .is_command = &isCommand, //
92 .determine_mime_type = &determine_mime_type, //
93 .neighbors = &ipc_print_neighbors, //
94 .links = &ipc_print_links, //
95 .routes = &ipc_print_routes, //
96 .topology = &ipc_print_topology, //
97 .hna = &ipc_print_hna, //
98 .mid = &ipc_print_mid, //
99 .gateways = &ipc_print_gateways, //
100 .sgw = &ipc_print_sgw, //
101 .version = &ipc_print_version, //
102 .olsrd_conf = &ipc_print_olsrd_conf, //
103 .interfaces = &ipc_print_interfaces, //
104 .config = &ipc_print_config, //
105 .plugins = &ipc_print_plugins //
108 static void determine_action(unsigned int *send_what, char *requ) {
109 if (!printer_functions.is_command)
111 else if ((*printer_functions.is_command)(requ, SIW_OLSRD_CONF))
112 *send_what |= SIW_OLSRD_CONF;
113 else if ((*printer_functions.is_command)(requ, SIW_ALL))
114 *send_what = SIW_ALL;
116 // these are the two overarching categories
117 if ((*printer_functions.is_command)(requ, SIW_RUNTIME_ALL))
118 *send_what |= SIW_RUNTIME_ALL;
119 if ((*printer_functions.is_command)(requ, SIW_STARTUP_ALL))
120 *send_what |= SIW_STARTUP_ALL;
122 // these are the individual sections
123 if ((*printer_functions.is_command)(requ, SIW_NEIGHBORS))
124 *send_what |= SIW_NEIGHBORS;
125 if ((*printer_functions.is_command)(requ, SIW_LINKS))
126 *send_what |= SIW_LINKS;
127 if ((*printer_functions.is_command)(requ, SIW_ROUTES))
128 *send_what |= SIW_ROUTES;
129 if ((*printer_functions.is_command)(requ, SIW_HNA))
130 *send_what |= SIW_HNA;
131 if ((*printer_functions.is_command)(requ, SIW_MID))
132 *send_what |= SIW_MID;
133 if ((*printer_functions.is_command)(requ, SIW_TOPOLOGY))
134 *send_what |= SIW_TOPOLOGY;
135 if ((*printer_functions.is_command)(requ, SIW_GATEWAYS))
136 *send_what |= SIW_GATEWAYS;
137 if ((*printer_functions.is_command)(requ, SIW_INTERFACES))
138 *send_what |= SIW_INTERFACES;
139 if ((*printer_functions.is_command)(requ, SIW_2HOP))
140 *send_what |= SIW_2HOP;
141 if ((*printer_functions.is_command)(requ, SIW_SGW))
142 *send_what |= SIW_SGW;
145 if ((*printer_functions.is_command)(requ, SIW_VERSION))
146 *send_what |= SIW_VERSION;
147 if ((*printer_functions.is_command)(requ, SIW_CONFIG))
148 *send_what |= SIW_CONFIG;
149 if ((*printer_functions.is_command)(requ, SIW_PLUGINS))
150 *send_what |= SIW_PLUGINS;
152 /* To print out neighbours only on the Freifunk Status
153 * page the normal output is somewhat lengthy. The
154 * header parsing is sufficient for standard wget.
156 if ((*printer_functions.is_command)(requ, SIW_NEIGHBORS_FREIFUNK))
157 *send_what = SIW_NEIGHBORS_FREIFUNK;
162 *Do initialization here
164 *This function is called by the my_init
165 *function in uolsrd_plugin.c
167 int olsrd_plugin_init(void) {
168 /* Initial IPC value */
170 memset(&outbuffer, 0, sizeof(outbuffer));
172 if (printer_functions.init) {
173 (*printer_functions.init)(PLUGIN_NAME);
181 * destructor - called at unload
183 void olsr_plugin_exit(void) {
184 if (ipc_socket != -1)
188 static int plugin_ipc_init(void) {
189 union olsr_sockaddr sst;
193 /* Init ipc socket */
194 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
196 olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
200 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
202 olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
206 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
207 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *) &yes, sizeof(yes)) < 0) {
208 perror("SO_REUSEADDR failed");
211 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
212 #if defined linux && defined IPV6_V6ONLY
213 if (info_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
214 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
215 perror("IPV6_V6ONLY failed");
219 #endif /* defined linux && defined IPV6_V6ONLY */
220 /* Bind the socket */
222 /* complete the socket structure */
223 memset(&sst, 0, sizeof(sst));
224 if (olsr_cnf->ip_version == AF_INET) {
225 sst.in4.sin_family = AF_INET;
226 addrlen = sizeof(struct sockaddr_in);
228 sst.in4.sin_len = addrlen;
229 #endif /* SIN6_LEN */
230 sst.in4.sin_addr.s_addr = info_listen_ip.v4.s_addr;
231 sst.in4.sin_port = htons(ipc_port);
233 sst.in6.sin6_family = AF_INET6;
234 addrlen = sizeof(struct sockaddr_in6);
236 sst.in6.sin6_len = addrlen;
237 #endif /* SIN6_LEN */
238 sst.in6.sin6_addr = info_listen_ip.v6;
239 sst.in6.sin6_port = htons(ipc_port);
242 /* bind the socket to the port number */
243 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
245 olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
250 /* show that we are willing to listen */
251 if (listen(ipc_socket, 1) == -1) {
253 olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
258 /* Register with olsrd */
259 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
262 olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
268 static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
269 union olsr_sockaddr pin;
271 char addr[INET6_ADDRSTRLEN];
274 unsigned int send_what = 0;
277 socklen_t addrlen = sizeof(pin);
279 if (outbuffer.count >= MAX_CLIENTS) {
283 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
285 olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
290 tv.tv_sec = tv.tv_usec = 0;
291 if (olsr_cnf->ip_version == AF_INET) {
292 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
294 if (!ip4equal(&pin.in4.sin_addr, &info_accept_ip.v4) && info_accept_ip.v4.s_addr != INADDR_ANY) {
295 #ifdef INFO_ALLOW_LOCALHOST
296 if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
297 #endif /* INFO_ALLOW_LOCALHOST */
298 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
299 close(ipc_connection);
301 #ifdef INFO_ALLOW_LOCALHOST
303 #endif /* INFO_ALLOW_LOCALHOST */
306 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
308 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
309 if (!ip6equal(&in6addr_any, &info_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &info_accept_ip.v6)) {
310 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
311 close(ipc_connection);
317 olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
320 /* purge read buffer to prevent blocking on linux */
322 FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
323 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
325 ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
327 if (s == sizeof(requ) - 1) {
328 /* input was much too long, just skip the rest */
331 while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
337 determine_action(&send_what, requ);
344 send_info(send_what, ipc_connection);
347 static void info_write_data(void *foo __attribute__ ((unused))) {
349 int result, i, j, max;
354 for (i = 0; i < outbuffer.count; i++) {
355 /* And we cast here since we get a warning on Win32 */
356 FD_SET((unsigned int )(outbuffer.socket[i]), &set);
358 if (outbuffer.socket[i] > max) {
359 max = outbuffer.socket[i];
366 result = select(max + 1, NULL, &set, NULL, &tv);
371 for (i = 0; i < outbuffer.count; i++) {
372 if (FD_ISSET(outbuffer.socket[i], &set)) {
373 result = send(outbuffer.socket[i], outbuffer.buffer[i] + outbuffer.written[i], outbuffer.size[i] - outbuffer.written[i], 0);
375 outbuffer.written[i] += result;
378 if (result <= 0 || outbuffer.written[i] == outbuffer.size[i]) {
379 /* close this socket and cleanup*/
380 close(outbuffer.socket[i]);
381 free(outbuffer.buffer[i]);
382 outbuffer.buffer[i] = NULL;
384 for (j = i + 1; j < outbuffer.count; j++) {
385 outbuffer.buffer[j - 1] = outbuffer.buffer[j];
386 outbuffer.size[j - 1] = outbuffer.size[j];
387 outbuffer.socket[j - 1] = outbuffer.socket[j];
388 outbuffer.written[j - 1] = outbuffer.written[j];
394 if (!outbuffer.count) {
395 olsr_stop_timer(writetimer_entry);
399 static void send_info(unsigned int send_what, int the_socket) {
402 const char *content_type = (printer_functions.determine_mime_type) ? (*printer_functions.determine_mime_type)(send_what) : "text/plain; charset=utf-8";
403 int contentLengthPlaceholderStart = 0;
404 int headerLength = 0;
406 abuf_init(&abuf, 2 * 4096);
409 build_http_header(PLUGIN_NAME, HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
410 headerLength = abuf.len;
413 // only add if normal format
414 if (send_what & SIW_ALL) {
415 /* global variables for tracking when to put a comma in for JSON */
416 abuf_json_reset_entry_number_and_depth();
417 abuf_json_mark_output(true, &abuf);
419 abuf_json_int(&abuf, "systemTime", time(NULL));
420 abuf_json_int(&abuf, "timeSinceStartup", now_times);
422 abuf_json_string(&abuf, "uuid", uuid);
424 if ((send_what & SIW_LINKS) && printer_functions.links)
425 (*printer_functions.links)(&abuf);
426 if ((send_what & SIW_NEIGHBORS) && printer_functions.neighbors)
427 (*printer_functions.neighbors)(&abuf, false);
428 if ((send_what & SIW_TOPOLOGY) && printer_functions.topology)
429 (*printer_functions.topology)(&abuf);
430 if ((send_what & SIW_HNA) && printer_functions.hna)
431 (*printer_functions.hna)(&abuf);
432 if ((send_what & SIW_SGW) && printer_functions.sgw)
433 (*printer_functions.sgw)(&abuf);
434 if ((send_what & SIW_MID) && printer_functions.mid)
435 (*printer_functions.mid)(&abuf);
436 if ((send_what & SIW_ROUTES) && printer_functions.routes)
437 (*printer_functions.routes)(&abuf);
438 if ((send_what & SIW_GATEWAYS) && printer_functions.gateways)
439 (*printer_functions.gateways)(&abuf);
440 if ((send_what & SIW_CONFIG) && printer_functions.config)
441 (*printer_functions.config)(&abuf);
442 if ((send_what & SIW_INTERFACES) && printer_functions.interfaces)
443 (*printer_functions.interfaces)(&abuf);
444 if ((send_what & SIW_2HOP) && printer_functions.neighbors)
445 (*printer_functions.neighbors)(&abuf, true);
446 if ((send_what & SIW_VERSION) && printer_functions.version)
447 (*printer_functions.version)(&abuf);
448 if ((send_what & SIW_PLUGINS) && printer_functions.plugins)
449 (*printer_functions.plugins)(&abuf);
451 abuf_json_mark_output(false, &abuf);
452 abuf_puts(&abuf, "\n");
453 } else if ((send_what & SIW_OLSRD_CONF) && printer_functions.olsrd_conf) {
454 /* this outputs the olsrd.conf text directly, not normal format */
455 (*printer_functions.olsrd_conf)(&abuf);
459 http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
462 /* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
463 outbuffer.buffer[outbuffer.count] = abuf.buf;
464 outbuffer.size[outbuffer.count] = abuf.len;
465 outbuffer.written[outbuffer.count] = 0;
466 outbuffer.socket[outbuffer.count] = the_socket;
473 if (outbuffer.count == 1) {
474 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
485 * indent-tabs-mode: nil