2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
4 * includes code by Bruno Randolf
5 * includes code by Andreas Lopatic
6 * includes code by Sven-Ola Tuecke
7 * includes code by Lorenz Schori
8 * includes bugs by Markus Kittenberger
9 * includes bugs by Hans-Christoph Steiner
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
22 * * Neither the name of olsr.org, olsrd nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
39 * Visit http://www.olsr.org for more information.
41 * If you find this software useful feel free to make a donation
42 * to the project. For more information see the website or contact
43 * the copyright holders.
48 * Dynamic linked library for the olsr.org olsr daemon
51 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <sys/select.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
72 #endif /* __linux__ */
76 #include "builddata.h"
77 #include "olsr_types.h"
78 #include "neighbor_table.h"
79 #include "two_hop_neighbor_table.h"
80 #include "mpr_selector_set.h"
86 #include "lq_plugin.h"
87 #include "common/autobuf.h"
89 #include "egressTypes.h"
91 #include "olsrd_jsoninfo.h"
92 #include "olsrd_plugin.h"
95 #define close(x) closesocket(x)
98 /* defines to make txtinfo and jsoninfo look alike */
99 #define PLUGIN_NAME "JSONINFO"
101 static int ipc_socket;
104 #define HTTP_200 "HTTP/1.1 200 OK"
106 /* IPC initialization function */
107 static int plugin_ipc_init(void);
109 static int read_uuid_from_file(const char *file);
111 static void send_info(unsigned int /*send_what*/, int /*socket*/);
112 static void ipc_action(int, void *, unsigned int);
114 static size_t build_http_header(const char *status, const char *mime, uint32_t msgsize, char *buf, uint32_t bufsize);
117 * this is the size of the buffer used for build_http_header
118 * the amount of data written into the buffer will be less than
119 * 400 bytes approximatively.
120 * The size may vary because the Content-Length header contains
121 * the length of the json data
123 #define MAX_HTTPHEADER_SIZE 512
125 #define TXT_IPC_BUFSIZE 256
127 /* these provide all of the runtime status info */
128 #define SIW_NEIGHBORS 0x0001
129 #define SIW_LINKS 0x0002
130 #define SIW_ROUTES 0x0004
131 #define SIW_HNA 0x0008
132 #define SIW_MID 0x0010
133 #define SIW_TOPOLOGY 0x0020
134 #define SIW_GATEWAYS 0x0040
135 #define SIW_INTERFACES 0x0080
136 #define SIW_2HOP 0x0100
137 #define SIW_SGW 0x0200
138 #define SIW_RUNTIME_ALL (SIW_NEIGHBORS | SIW_LINKS | SIW_ROUTES | SIW_HNA | SIW_MID | SIW_TOPOLOGY | SIW_GATEWAYS | SIW_INTERFACES | SIW_2HOP | SIW_SGW)
140 /* these only change at olsrd startup */
141 #define SIW_VERSION 0x0400
142 #define SIW_CONFIG 0x0800
143 #define SIW_PLUGINS 0x1000
144 #define SIW_STARTUP_ALL (SIW_VERSION | SIW_CONFIG | SIW_PLUGINS)
146 /* this is everything in JSON format */
147 #define SIW_ALL (SIW_RUNTIME_ALL | SIW_STARTUP_ALL)
149 /* this data is not JSON format but olsrd.conf format */
150 #define SIW_OLSRD_CONF 0x2000
152 #define MAX_CLIENTS 3
154 static char *outbuffer[MAX_CLIENTS];
155 static size_t outbuffer_size[MAX_CLIENTS];
156 static size_t outbuffer_written[MAX_CLIENTS];
157 static int outbuffer_socket[MAX_CLIENTS];
158 static int outbuffer_count;
160 char uuid[UUIDLEN + 1];
161 char uuidfile[FILENAME_MAX];
163 static struct timeval start_time;
164 static struct timer_entry *writetimer_entry;
166 /* JSON support functions */
168 /* JSON does not allow commas dangling at the end of arrays, so we need to
169 * count which entry number we're at in order to make sure we don't tack a
170 * dangling comma on at the end */
171 static int entrynumber[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
172 static int currentjsondepth = 0;
174 static void abuf_json_new_indent(struct autobuf *abuf) {
175 int i = currentjsondepth;
181 abuf_puts(abuf, "\n");
183 abuf_puts(abuf, " ");
187 static void abuf_json_mark_output(bool open, struct autobuf *abuf) {
189 assert(!currentjsondepth);
190 abuf_json_new_indent(abuf);
191 abuf_puts(abuf, "{");
193 entrynumber[currentjsondepth] = 0;
195 entrynumber[currentjsondepth] = 0;
197 assert(!currentjsondepth);
198 abuf_json_new_indent(abuf);
199 abuf_puts(abuf, "\n}");
203 static void abuf_json_mark_object(bool open, bool array, struct autobuf *abuf, const char* header) {
205 if (entrynumber[currentjsondepth]) {
206 abuf_appendf(abuf, ",");
208 abuf_json_new_indent(abuf);
210 abuf_appendf(abuf, "\"%s\": %s", header, array ? "[" : "{");
212 abuf_appendf(abuf, "%s", array ? "[" : "{");
214 entrynumber[currentjsondepth]++;
216 entrynumber[currentjsondepth] = 0;
218 entrynumber[currentjsondepth] = 0;
220 abuf_json_new_indent(abuf);
221 abuf_appendf(abuf, "%s", array ? "]" : "}");
225 static void abuf_json_mark_array_entry(bool open, struct autobuf *abuf) {
226 abuf_json_mark_object(open, false, abuf, NULL);
229 static void abuf_json_insert_comma(struct autobuf *abuf) {
230 if (entrynumber[currentjsondepth])
231 abuf_appendf(abuf, ",");
234 static void abuf_json_boolean(struct autobuf *abuf, const char* key, int value) {
235 abuf_json_insert_comma(abuf);
236 abuf_json_new_indent(abuf);
237 abuf_appendf(abuf, "\"%s\": %s", key, value ? "true" : "false");
238 entrynumber[currentjsondepth]++;
241 static void abuf_json_string(struct autobuf *abuf, const char* key, const char* value) {
242 abuf_json_insert_comma(abuf);
243 abuf_json_new_indent(abuf);
244 abuf_appendf(abuf, "\"%s\": \"%s\"", key, value);
245 entrynumber[currentjsondepth]++;
248 static void abuf_json_int(struct autobuf *abuf, const char* key, long value) {
249 abuf_json_insert_comma(abuf);
250 abuf_json_new_indent(abuf);
251 abuf_appendf(abuf, "\"%s\": %li", key, value);
252 entrynumber[currentjsondepth]++;
255 static void abuf_json_float(struct autobuf *abuf, const char* key, float value) {
256 abuf_json_insert_comma(abuf);
257 abuf_json_new_indent(abuf);
258 abuf_appendf(abuf, "\"%s\": %.03f", key, (double) value);
259 entrynumber[currentjsondepth]++;
262 /* Linux specific functions for getting system info */
265 static int get_string_from_file(const char* filename, char* buf, int len) {
267 int fd = open(filename, O_RDONLY);
269 bytes = read(fd, buf, len);
271 buf[bytes - 1] = '\0'; // remove trailing \n
279 static int abuf_json_sysdata(struct autobuf *abuf, const char* key, const char* syspath) {
283 ret = get_string_from_file(syspath, buf, 256);
285 abuf_json_string(abuf, key, buf);
289 static void abuf_json_sys_class_net(struct autobuf *abuf, const char* key, const char* ifname, const char* datapoint) {
291 snprintf(filename, 255, "/sys/class/net/%s/%s", ifname, datapoint);
292 abuf_json_sysdata(abuf, key, filename);
295 #endif /* __linux__ */
298 *Do initialization here
300 *This function is called by the my_init
301 *function in uolsrd_plugin.c
303 int olsrd_plugin_init(void) {
304 /* Initial IPC value */
308 gettimeofday(&start_time, NULL);
310 if (!strlen(uuidfile))
311 strscpy(uuidfile, "uuid.txt", sizeof(uuidfile));
312 read_uuid_from_file(uuidfile);
319 * destructor - called at unload
321 void olsr_plugin_exit(void) {
322 if (ipc_socket != -1)
326 static int plugin_ipc_init(void) {
327 union olsr_sockaddr sst;
331 /* Init ipc socket */
332 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
334 olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
338 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
340 olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
344 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
345 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0) {
346 perror("SO_REUSEADDR failed");
349 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
350 #if defined linux && defined IPV6_V6ONLY
351 if (jsoninfo_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
352 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
353 perror("IPV6_V6ONLY failed");
357 #endif /* defined linux && defined IPV6_V6ONLY */
358 /* Bind the socket */
360 /* complete the socket structure */
361 memset(&sst, 0, sizeof(sst));
362 if (olsr_cnf->ip_version == AF_INET) {
363 sst.in4.sin_family = AF_INET;
364 addrlen = sizeof(struct sockaddr_in);
366 sst.in4.sin_len = addrlen;
367 #endif /* SIN6_LEN */
368 sst.in4.sin_addr.s_addr = jsoninfo_listen_ip.v4.s_addr;
369 sst.in4.sin_port = htons(ipc_port);
371 sst.in6.sin6_family = AF_INET6;
372 addrlen = sizeof(struct sockaddr_in6);
374 sst.in6.sin6_len = addrlen;
375 #endif /* SIN6_LEN */
376 sst.in6.sin6_addr = jsoninfo_listen_ip.v6;
377 sst.in6.sin6_port = htons(ipc_port);
380 /* bind the socket to the port number */
381 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
383 olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
388 /* show that we are willing to listen */
389 if (listen(ipc_socket, 1) == -1) {
391 olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
396 /* Register with olsrd */
397 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
400 olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
406 static int read_uuid_from_file(const char *file) {
412 memset(uuid, 0, sizeof(uuid));
414 f = fopen(file, "r");
415 olsr_printf(1, "("PLUGIN_NAME") Reading UUID from '%s'\n", file);
417 olsr_printf(1, "("PLUGIN_NAME") Could not open '%s': %s\n", file, strerror(errno));
420 chars = fread(uuid, 1, UUIDLEN, f);
422 uuid[chars] = '\0'; /* null-terminate the string */
424 /* we only use the first line of the file */
425 end = strchr(uuid, '\n');
430 olsr_printf(1, "("PLUGIN_NAME") Could not read UUID from '%s': %s\n", file, strerror(errno));
438 static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
439 union olsr_sockaddr pin;
441 char addr[INET6_ADDRSTRLEN];
444 unsigned int send_what = 0;
447 socklen_t addrlen = sizeof(pin);
449 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
451 olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
456 tv.tv_sec = tv.tv_usec = 0;
457 if (olsr_cnf->ip_version == AF_INET) {
458 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
460 if (!ip4equal(&pin.in4.sin_addr, &jsoninfo_accept_ip.v4) && jsoninfo_accept_ip.v4.s_addr != INADDR_ANY) {
461 #ifdef JSONINFO_ALLOW_LOCALHOST
462 if (pin.in4.sin_addr.s_addr != INADDR_LOOPBACK) {
463 #endif /* JSONINFO_ALLOW_LOCALHOST */
464 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
465 close(ipc_connection);
467 #ifdef JSONINFO_ALLOW_LOCALHOST
469 #endif /* JSONINFO_ALLOW_LOCALHOST */
472 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
474 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
475 if (!ip6equal(&in6addr_any, &jsoninfo_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &jsoninfo_accept_ip.v6)) {
476 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
477 close(ipc_connection);
483 olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
486 /* purge read buffer to prevent blocking on linux */
488 FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
489 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
491 ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
493 if (s == sizeof(requ) - 1) {
494 /* input was too much long, just skip the rest */
497 while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
503 /* print out the requested tables */
504 if (strstr(requ, "/olsrd.conf"))
505 send_what |= SIW_OLSRD_CONF;
506 else if (strstr(requ, "/all"))
509 // these are the two overarching categories
510 if (strstr(requ, "/runtime"))
511 send_what |= SIW_RUNTIME_ALL;
512 if (strstr(requ, "/startup"))
513 send_what |= SIW_STARTUP_ALL;
515 // these are the individual sections
516 if (strstr(requ, "/neighbors"))
517 send_what |= SIW_NEIGHBORS;
518 if (strstr(requ, "/links"))
519 send_what |= SIW_LINKS;
520 if (strstr(requ, "/routes"))
521 send_what |= SIW_ROUTES;
522 if (strstr(requ, "/hna"))
523 send_what |= SIW_HNA;
524 if (strstr(requ, "/mid"))
525 send_what |= SIW_MID;
526 if (strstr(requ, "/topology"))
527 send_what |= SIW_TOPOLOGY;
528 if (strstr(requ, "/gateways"))
529 send_what |= SIW_GATEWAYS;
530 if (strstr(requ, "/interfaces"))
531 send_what |= SIW_INTERFACES;
532 if (strstr(requ, "/2hop"))
533 send_what |= SIW_2HOP;
534 if (strstr(requ, "/sgw"))
535 send_what |= SIW_SGW;
538 if (strstr(requ, "/version"))
539 send_what |= SIW_VERSION;
540 if (strstr(requ, "/config"))
541 send_what |= SIW_CONFIG;
542 if (strstr(requ, "/plugins"))
543 send_what |= SIW_PLUGINS;
550 send_info(send_what, ipc_connection);
553 static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
554 struct ipaddr_str buf1;
555 struct neighbor_entry *neigh;
556 struct neighbor_2_list_entry *list_2;
560 abuf_json_mark_object(true, true, abuf, "neighbors");
562 abuf_json_mark_object(true, true, abuf, "2hop");
565 OLSR_FOR_ALL_NBR_ENTRIES(neigh)
567 abuf_json_mark_array_entry(true, abuf);
569 abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf1, &neigh->neighbor_main_addr));
570 abuf_json_boolean(abuf, "symmetric", (neigh->status == SYM));
571 abuf_json_boolean(abuf, "multiPointRelay", neigh->is_mpr);
572 abuf_json_boolean(abuf, "multiPointRelaySelector", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) != NULL);
573 abuf_json_int(abuf, "willingness", neigh->willingness);
577 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
580 abuf_json_int(abuf, "twoHopNeighborCount", thop_cnt);
582 abuf_json_mark_object(true, true, abuf, "twoHopNeighbors");
583 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
584 abuf_json_mark_array_entry(true, abuf);
585 abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
586 abuf_json_mark_array_entry(false, abuf);
588 abuf_json_mark_object(false, true, abuf, false);
591 abuf_json_mark_array_entry(false, abuf);
592 }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
593 abuf_json_mark_object(false, true, abuf, NULL);
596 static void ipc_print_links(struct autobuf *abuf) {
597 struct ipaddr_str buf1, buf2;
598 struct lqtextbuffer lqbuffer1;
600 struct link_entry *my_link = NULL;
602 abuf_json_mark_object(true, true, abuf, "links");
604 OLSR_FOR_ALL_LINK_ENTRIES(my_link)
607 int diff = (unsigned int) (my_link->link_timer->timer_clock - now_times);
609 abuf_json_mark_array_entry(true, abuf);
610 abuf_json_string(abuf, "localIP", olsr_ip_to_string(&buf1, &my_link->local_iface_addr));
611 abuf_json_string(abuf, "remoteIP", olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr));
612 abuf_json_int(abuf, "validityTime", diff);
613 lqs = get_link_entry_text(my_link, '\t', &lqbuffer1);
614 abuf_json_float(abuf, "linkQuality", atof(lqs));
615 abuf_json_float(abuf, "neighborLinkQuality", atof(strrchr(lqs, '\t')));
616 if (my_link->linkcost >= LINK_COST_BROKEN)
617 abuf_json_int(abuf, "linkCost", LINK_COST_BROKEN);
619 abuf_json_int(abuf, "linkCost", my_link->linkcost);
620 abuf_json_mark_array_entry(false, abuf);
621 }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
622 abuf_json_mark_object(false, true, abuf, NULL);
625 static void ipc_print_routes(struct autobuf *abuf) {
626 struct ipaddr_str buf1, buf2;
629 abuf_json_mark_object(true, true, abuf, "routes");
631 /* Walk the route table */
632 OLSR_FOR_ALL_RT_ENTRIES(rt)
634 abuf_json_mark_array_entry(true, abuf);
635 abuf_json_string(abuf, "destination", olsr_ip_to_string(&buf1, &rt->rt_dst.prefix));
636 abuf_json_int(abuf, "genmask", rt->rt_dst.prefix_len);
637 abuf_json_string(abuf, "gateway", olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway));
638 abuf_json_int(abuf, "metric", rt->rt_best->rtp_metric.hops);
639 if (rt->rt_best->rtp_metric.cost >= ROUTE_COST_BROKEN)
640 abuf_json_int(abuf, "rtpMetricCost", ROUTE_COST_BROKEN);
642 abuf_json_int(abuf, "rtpMetricCost", rt->rt_best->rtp_metric.cost);
643 abuf_json_string(abuf, "networkInterface", if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
644 abuf_json_mark_array_entry(false, abuf);
645 }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
647 abuf_json_mark_object(false, true, abuf, NULL);
650 static void ipc_print_topology(struct autobuf *abuf) {
653 abuf_json_mark_object(true, true, abuf, "topology");
656 OLSR_FOR_ALL_TC_ENTRIES(tc)
658 struct tc_edge_entry *tc_edge;
659 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
661 if (tc_edge->edge_inv) {
662 struct ipaddr_str dstbuf, addrbuf;
663 struct lqtextbuffer lqbuffer1;
664 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
665 int diff = (int) (vt);
667 abuf_json_mark_array_entry(true, abuf);
668 abuf_json_string(abuf, "destinationIP", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr));
669 abuf_json_string(abuf, "lastHopIP", olsr_ip_to_string(&addrbuf, &tc->addr));
670 lqs = get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1);
671 abuf_json_float(abuf, "linkQuality", atof(lqs));
672 abuf_json_float(abuf, "neighborLinkQuality", atof(strrchr(lqs, '\t')));
673 if (tc_edge->cost >= LINK_COST_BROKEN)
674 abuf_json_int(abuf, "tcEdgeCost", LINK_COST_BROKEN);
676 abuf_json_int(abuf, "tcEdgeCost", tc_edge->cost);
677 abuf_json_int(abuf, "validityTime", diff);
678 abuf_json_mark_array_entry(false, abuf);
680 }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
681 }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
683 abuf_json_mark_object(false, true, abuf, NULL);
686 static void ipc_print_hna(struct autobuf *abuf) {
687 struct hna_entry *tmp_hna;
688 struct hna_net *tmp_net;
689 struct ipaddr_str buf, mainaddrbuf;
691 abuf_json_mark_object(true, true, abuf, "hna");
693 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
696 /* Check all networks */
697 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
698 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
699 int diff = (int) (vt);
700 abuf_json_mark_array_entry(true, abuf);
701 abuf_json_string(abuf, "destination", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix)), abuf_json_int(abuf, "genmask",
702 tmp_net->hna_prefix.prefix_len);
703 abuf_json_string(abuf, "gateway", olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
704 abuf_json_int(abuf, "validityTime", diff);
705 abuf_json_mark_array_entry(false, abuf);
707 }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
709 abuf_json_mark_object(false, true, abuf, NULL);
712 static void ipc_print_mid(struct autobuf *abuf) {
714 struct mid_entry *entry;
715 struct mid_address *alias;
717 abuf_json_mark_object(true, true, abuf, "mid");
720 for (idx = 0; idx < HASHSIZE; idx++) {
721 entry = mid_set[idx].next;
723 while (entry != &mid_set[idx]) {
724 struct ipaddr_str buf, buf2;
725 abuf_json_mark_array_entry(true, abuf);
726 abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf, &entry->main_addr));
728 abuf_json_mark_object(true, true, abuf, "aliases");
729 alias = entry->aliases;
731 uint32_t vt = alias->vtime - now_times;
732 int diff = (int) (vt);
734 abuf_json_mark_array_entry(true, abuf);
735 abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf2, &alias->alias));
736 abuf_json_int(abuf, "validityTime", diff);
737 abuf_json_mark_array_entry(false, abuf);
739 alias = alias->next_alias;
741 abuf_json_mark_object(false, true, abuf, NULL); // aliases
742 abuf_json_mark_array_entry(false, abuf);
746 abuf_json_mark_object(false, true, abuf, NULL); // mid
749 static void ipc_print_gateways(struct autobuf *abuf) {
751 abuf_json_string(abuf, "error", "Gateway mode is only supported in Linux");
752 #else /* __linux__ */
754 struct ipaddr_str buf;
755 struct gateway_entry *gw;
757 abuf_json_mark_object(true, true, abuf, "gateways");
758 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
760 const char *v4 = "", *v6 = "";
761 bool autoV4 = false, autoV6 = false;
762 const char *ipType = "";
765 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
769 if (gw == olsr_get_inet_gateway(false)) {
771 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
775 if (gw == olsr_get_inet_gateway(true)) {
777 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
781 abuf_json_mark_array_entry(true, abuf);
784 abuf_json_string(abuf, "ipv4Status", v4);
785 } else if (gw->ipv6) {
787 abuf_json_string(abuf, "ipv6Status", v6);
789 abuf_json_string(abuf, "ipType", ipType);
790 abuf_json_boolean(abuf, "ipv4", gw->ipv4);
791 abuf_json_boolean(abuf, "ipv4Nat", gw->ipv4nat);
792 abuf_json_boolean(abuf, "ipv6", gw->ipv6);
793 abuf_json_boolean(abuf, "autoIpv4", autoV4);
794 abuf_json_boolean(abuf, "autoIpv6", autoV6);
795 abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&buf, &gw->originator));
796 if (tc->path_cost >= ROUTE_COST_BROKEN)
797 abuf_json_int(abuf, "tcPathCost", ROUTE_COST_BROKEN);
799 abuf_json_int(abuf, "tcPathCost", tc->path_cost);
800 abuf_json_int(abuf, "hopCount", tc->hops);
801 abuf_json_int(abuf, "uplinkSpeed", gw->uplink);
802 abuf_json_int(abuf, "downlinkSpeed", gw->downlink);
803 if (!gw->external_prefix.prefix_len)
804 abuf_json_string(abuf, "externalPrefix", olsr_ip_prefix_to_string(&gw->external_prefix));
805 abuf_json_mark_array_entry(false, abuf);
806 }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
807 abuf_json_mark_object(false, true, abuf, NULL);
808 #endif /* __linux__ */
811 static void ipc_print_plugins(struct autobuf *abuf) {
812 struct plugin_entry *pentry;
813 struct plugin_param *pparam;
814 abuf_json_mark_object(true, true, abuf, "plugins");
815 if (olsr_cnf->plugins)
816 for (pentry = olsr_cnf->plugins; pentry; pentry = pentry->next) {
817 abuf_json_mark_array_entry(true, abuf);
818 abuf_json_string(abuf, "plugin", pentry->name);
819 for (pparam = pentry->params; pparam; pparam = pparam->next) {
820 int i, keylen = strlen(pparam->key);
821 char key[keylen + 1];
824 strcpy(key, pparam->key);
825 for (i = 0; i < keylen; i++)
826 key[i] = tolower(key[i]);
828 // test if a int/long and set as such in JSON
829 value = atol(pparam->value);
830 snprintf(valueTest, 255, "%li", value);
831 if (!strcmp(valueTest, pparam->value))
832 abuf_json_int(abuf, key, value);
834 abuf_json_string(abuf, key, pparam->value);
836 abuf_json_mark_array_entry(false, abuf);
838 abuf_json_mark_object(false, true, abuf, NULL);
843 /** interface names for smart gateway tunnel interfaces, IPv4 */
844 extern struct interfaceName * sgwTunnel4InterfaceNames;
846 /** interface names for smart gateway tunnel interfaces, IPv6 */
847 extern struct interfaceName * sgwTunnel6InterfaceNames;
850 * Construct the sgw table for a given ip version
852 * @param abuf the string buffer
853 * @param ipv6 true for IPv6, false for IPv4
854 * @param fmtv the format for printing
856 static void sgw_ipvx(struct autobuf *abuf, bool ipv6) {
857 struct interfaceName * sgwTunnelInterfaceNames;
859 abuf_json_mark_object(true, true, abuf, ipv6 ? "ipv6" : "ipv4");
861 sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
862 if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
863 struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
865 for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
866 struct interfaceName * node = &sgwTunnelInterfaceNames[i];
867 struct gateway_entry * gw = node->gw;
873 abuf_json_mark_array_entry(true, abuf);
875 struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
877 struct ipaddr_str originatorStr;
878 const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
879 struct ipaddr_str prefixIpStr;
880 const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
882 abuf_json_boolean(abuf, "selected", current_gw && (current_gw == gw));
883 abuf_json_string(abuf, "originator", originator);
884 abuf_json_string(abuf, "prefix", prefix);
885 abuf_json_int(abuf, "prefixLen", gw->external_prefix.prefix_len);
886 abuf_json_int(abuf, "uplink", gw->uplink);
887 abuf_json_int(abuf, "downlink", gw->downlink);
888 abuf_json_int(abuf, "pathcost", !tc ? ROUTE_COST_BROKEN : tc->path_cost);
889 abuf_json_boolean(abuf, "IPv4", gw->ipv4);
890 abuf_json_boolean(abuf, "IPv4-NAT", gw->ipv4nat);
891 abuf_json_boolean(abuf, "IPv6", gw->ipv6);
892 abuf_json_string(abuf, "tunnel", node->name);
893 abuf_json_string(abuf, "destination", originator);
894 abuf_json_int(abuf, "cost", gw->path_cost);
895 abuf_json_int(abuf, "tableNr", node->tableNr);
896 abuf_json_int(abuf, "ruleNr", node->ruleNr);
897 abuf_json_int(abuf, "bypassRuleNr", node->bypassRuleNr);
899 abuf_json_mark_array_entry(false, abuf);
903 abuf_json_mark_object(false, true, abuf, NULL);
905 #endif /* __linux__ */
907 static void ipc_print_sgw(struct autobuf *abuf) {
909 abuf_json_string(abuf, "error", "Gateway mode is only supported in Linux");
911 abuf_json_mark_object(true, false, abuf, "sgw");
913 sgw_ipvx(abuf, false);
914 sgw_ipvx(abuf, true);
916 abuf_json_mark_object(false, false, abuf, NULL);
917 #endif /* __linux__ */
920 static void ipc_print_version(struct autobuf *abuf) {
921 abuf_json_mark_object(true, false, abuf, "version");
923 abuf_json_string(abuf, "version", olsrd_version);
924 abuf_json_string(abuf, "date", build_date);
925 abuf_json_string(abuf, "host", build_host);
927 abuf_json_mark_object(false, false, abuf, NULL);
930 static void ipc_print_config(struct autobuf *abuf) {
931 struct ip_prefix_list *hna;
932 struct ipaddr_str buf, mainaddrbuf;
933 struct ip_prefix_list *ipcn;
934 struct olsr_lq_mult *mult;
935 char ipv6_buf[INET6_ADDRSTRLEN]; /* buffer for IPv6 inet_htop */
937 abuf_json_mark_object(true, false, abuf, "config");
939 abuf_json_int(abuf, "olsrPort", olsr_cnf->olsrport);
940 abuf_json_int(abuf, "debugLevel", olsr_cnf->debug_level);
941 abuf_json_boolean(abuf, "noFork", olsr_cnf->no_fork);
942 abuf_json_boolean(abuf, "hostEmulation", olsr_cnf->host_emul);
943 abuf_json_int(abuf, "ipVersion", olsr_cnf->ip_version);
944 abuf_json_boolean(abuf, "allowNoInterfaces", olsr_cnf->allow_no_interfaces);
945 abuf_json_int(abuf, "typeOfService", olsr_cnf->tos);
946 abuf_json_int(abuf, "rtProto", olsr_cnf->rt_proto);
947 abuf_json_int(abuf, "rtTable", olsr_cnf->rt_table);
948 abuf_json_int(abuf, "rtTableDefault", olsr_cnf->rt_table_default);
949 abuf_json_int(abuf, "rtTableTunnel", olsr_cnf->rt_table_tunnel);
950 abuf_json_int(abuf, "rtTablePriority", olsr_cnf->rt_table_pri);
951 abuf_json_int(abuf, "rtTableTunnelPriority", olsr_cnf->rt_table_tunnel_pri);
952 abuf_json_int(abuf, "rtTableDefauiltOlsrPriority", olsr_cnf->rt_table_defaultolsr_pri);
953 abuf_json_int(abuf, "rtTableDefaultPriority", olsr_cnf->rt_table_default_pri);
954 abuf_json_int(abuf, "willingness", olsr_cnf->willingness);
955 abuf_json_boolean(abuf, "willingnessAuto", olsr_cnf->willingness_auto);
957 abuf_json_int(abuf, "brokenLinkCost", LINK_COST_BROKEN);
958 abuf_json_int(abuf, "brokenRouteCost", ROUTE_COST_BROKEN);
960 abuf_json_string(abuf, "fibMetrics", FIB_METRIC_TXT[olsr_cnf->fib_metric]);
962 abuf_json_string(abuf, "defaultIpv6Multicast", inet_ntop(AF_INET6, &olsr_cnf->interface_defaults->ipv6_multicast.v6, ipv6_buf, sizeof(ipv6_buf)));
963 if (olsr_cnf->interface_defaults->ipv4_multicast.v4.s_addr)
964 abuf_json_string(abuf, "defaultIpv4Broadcast", inet_ntoa(olsr_cnf->interface_defaults->ipv4_multicast.v4));
966 abuf_json_string(abuf, "defaultIpv4Broadcast", "auto");
968 if (olsr_cnf->interface_defaults->mode == IF_MODE_ETHER)
969 abuf_json_string(abuf, "defaultInterfaceMode", "ether");
971 abuf_json_string(abuf, "defaultInterfaceMode", "mesh");
973 abuf_json_float(abuf, "defaultHelloEmissionInterval", olsr_cnf->interface_defaults->hello_params.emission_interval);
974 abuf_json_float(abuf, "defaultHelloValidityTime", olsr_cnf->interface_defaults->hello_params.validity_time);
975 abuf_json_float(abuf, "defaultTcEmissionInterval", olsr_cnf->interface_defaults->tc_params.emission_interval);
976 abuf_json_float(abuf, "defaultTcValidityTime", olsr_cnf->interface_defaults->tc_params.validity_time);
977 abuf_json_float(abuf, "defaultMidEmissionInterval", olsr_cnf->interface_defaults->mid_params.emission_interval);
978 abuf_json_float(abuf, "defaultMidValidityTime", olsr_cnf->interface_defaults->mid_params.validity_time);
979 abuf_json_float(abuf, "defaultHnaEmissionInterval", olsr_cnf->interface_defaults->hna_params.emission_interval);
980 abuf_json_float(abuf, "defaultHnaValidityTime", olsr_cnf->interface_defaults->hna_params.validity_time);
981 abuf_json_boolean(abuf, "defaultAutoDetectChanges", olsr_cnf->interface_defaults->autodetect_chg);
983 abuf_json_mark_object(true, true, abuf, "defaultLinkQualityMultipliers");
984 for (mult = olsr_cnf->interface_defaults->lq_mult; mult != NULL ; mult = mult->next) {
985 abuf_json_mark_array_entry(true, abuf);
986 abuf_json_string(abuf, "route", inet_ntop(olsr_cnf->ip_version, &mult->addr, ipv6_buf, sizeof(ipv6_buf)));
987 abuf_json_float(abuf, "multiplier", mult->value / 65535.0);
988 abuf_json_mark_array_entry(false, abuf);
990 abuf_json_mark_object(false, true, abuf, NULL);
992 abuf_json_mark_object(true, true, abuf, "hna");
993 for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
994 abuf_json_mark_array_entry(true, abuf);
995 abuf_json_string(abuf, "destination", olsr_ip_to_string(&buf, &hna->net.prefix));
996 abuf_json_int(abuf, "genmask", hna->net.prefix_len);
997 abuf_json_string(abuf, "gateway", olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
998 abuf_json_mark_array_entry(false, abuf);
1000 abuf_json_mark_object(false, true, abuf, NULL);
1002 abuf_json_int(abuf, "totalIpcConnectionsAllowed", olsr_cnf->ipc_connections);
1003 abuf_json_mark_object(true, true, abuf, "ipcAllowedAddresses");
1004 if (olsr_cnf->ipc_connections) {
1005 for (ipcn = olsr_cnf->ipc_nets; ipcn != NULL ; ipcn = ipcn->next) {
1006 abuf_json_mark_array_entry(true, abuf);
1007 abuf_json_string(abuf, "ipAddress", olsr_ip_to_string(&mainaddrbuf, &ipcn->net.prefix));
1008 abuf_json_int(abuf, "netmask", ipcn->net.prefix_len);
1009 abuf_json_mark_array_entry(false, abuf);
1012 abuf_json_mark_object(false, true, abuf, NULL);
1014 // keep all time in ms, so convert these two, which are in seconds
1015 abuf_json_int(abuf, "pollRate", olsr_cnf->pollrate * 1000);
1016 abuf_json_int(abuf, "nicChangePollInterval", olsr_cnf->nic_chgs_pollrate * 1000);
1017 abuf_json_boolean(abuf, "clearScreen", olsr_cnf->clear_screen);
1018 abuf_json_int(abuf, "tcRedundancy", olsr_cnf->tc_redundancy);
1019 abuf_json_int(abuf, "mprCoverage", olsr_cnf->mpr_coverage);
1021 if (!olsr_cnf->lq_level) {
1022 abuf_json_boolean(abuf, "useHysteresis", olsr_cnf->use_hysteresis);
1023 if (olsr_cnf->use_hysteresis) {
1024 abuf_json_float(abuf, "hysteresisScaling", olsr_cnf->hysteresis_param.scaling);
1025 abuf_json_float(abuf, "hysteresisLowThreshold", olsr_cnf->hysteresis_param.thr_low);
1026 abuf_json_float(abuf, "hysteresisHighThreshold", olsr_cnf->hysteresis_param.thr_high);
1029 abuf_json_int(abuf, "linkQualityLevel", olsr_cnf->lq_level);
1030 abuf_json_float(abuf, "linkQualityAging", olsr_cnf->lq_aging);
1031 abuf_json_boolean(abuf, "linkQualityFisheye", olsr_cnf->lq_fish);
1032 abuf_json_string(abuf, "linkQualityAlgorithm", olsr_cnf->lq_algorithm);
1033 // keep all time in ms, so convert this from seconds
1034 abuf_json_int(abuf, "minTcValidTime", olsr_cnf->min_tc_vtime * 1000);
1035 abuf_json_boolean(abuf, "setIpForward", olsr_cnf->set_ip_forward);
1036 abuf_json_string(abuf, "lockFile", olsr_cnf->lock_file);
1037 abuf_json_boolean(abuf, "useNiit", olsr_cnf->use_niit);
1040 abuf_json_boolean(abuf, "smartGateway", olsr_cnf->smart_gw_active);
1041 if (olsr_cnf->smart_gw_active) {
1042 abuf_json_boolean(abuf, "smartGatewayAlwaysRemoveServerTunnel", olsr_cnf->smart_gw_always_remove_server_tunnel);
1043 abuf_json_int(abuf, "smartGatewayUseCount", olsr_cnf->smart_gw_use_count);
1044 abuf_json_string(abuf, "smartGatewayInstanceId", olsr_cnf->smart_gw_instance_id);
1045 abuf_json_string(abuf, "smartGatewayPolicyRoutingScript", olsr_cnf->smart_gw_policyrouting_script);
1047 struct autobuf egressbuf;
1048 struct sgw_egress_if * egressif = olsr_cnf->smart_gw_egress_interfaces;
1050 abuf_init(&egressbuf, (olsr_cnf->smart_gw_egress_interfaces_count * IFNAMSIZ) /* interface names */
1051 + (olsr_cnf->smart_gw_egress_interfaces_count - 1) /* commas */);
1053 if (egressbuf.len) {
1054 abuf_puts(&egressbuf, ",");
1056 abuf_appendf(&egressbuf, "%s", egressif->name);
1057 egressif = egressif->next;
1059 abuf_json_string(abuf, "smartGatewayEgressInterfaces", egressbuf.buf);
1060 abuf_free(&egressbuf);
1062 abuf_json_int(abuf, "smartGatewayTablesOffset", olsr_cnf->smart_gw_offset_tables);
1063 abuf_json_int(abuf, "smartGatewayRulesOffset", olsr_cnf->smart_gw_offset_rules);
1064 abuf_json_boolean(abuf, "smartGatewayAllowNat", olsr_cnf->smart_gw_allow_nat);
1065 abuf_json_boolean(abuf, "smartGatewayUplinkNat", olsr_cnf->smart_gw_uplink_nat);
1066 abuf_json_int(abuf, "smartGatewayPeriod", olsr_cnf->smart_gw_period);
1067 abuf_json_int(abuf, "smartGatewayStableCount", olsr_cnf->smart_gw_stablecount);
1068 abuf_json_int(abuf, "smartGatewayThreshold", olsr_cnf->smart_gw_thresh);
1069 abuf_json_int(abuf, "smartGatewayUplink", olsr_cnf->smart_gw_uplink);
1070 abuf_json_int(abuf, "smartGatewayDownlink", olsr_cnf->smart_gw_downlink);
1071 abuf_json_int(abuf, "smartGatewayType", olsr_cnf->smart_gw_type);
1072 abuf_json_string(abuf, "smartGatewayPrefix", olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->smart_gw_prefix.prefix));
1073 abuf_json_int(abuf, "smartGatewayPrefixLength", olsr_cnf->smart_gw_prefix.prefix_len);
1075 #endif /* __linux__ */
1077 abuf_json_string(abuf, "mainIpAddress", olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
1078 abuf_json_string(abuf, "unicastSourceIpAddress", olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->unicast_src_ip));
1080 abuf_json_boolean(abuf, "useSourceIpRoutes", olsr_cnf->use_src_ip_routes);
1082 abuf_json_int(abuf, "maxPrefixLength", olsr_cnf->maxplen);
1083 abuf_json_int(abuf, "ipSize", olsr_cnf->ipsize);
1084 abuf_json_boolean(abuf, "deleteInternetGatewaysAtStartup", olsr_cnf->del_gws);
1085 // keep all time in ms, so convert this from seconds
1086 abuf_json_int(abuf, "willingnessUpdateInterval", olsr_cnf->will_int * 1000);
1087 abuf_json_float(abuf, "maxSendMessageJitter", olsr_cnf->max_jitter);
1088 abuf_json_int(abuf, "exitValue", olsr_cnf->exit_value);
1089 // keep all time in ms, so convert this from seconds
1090 abuf_json_int(abuf, "maxTcValidTime", olsr_cnf->max_tc_vtime * 1000);
1092 abuf_json_int(abuf, "niit4to6InterfaceIndex", olsr_cnf->niit4to6_if_index);
1093 abuf_json_int(abuf, "niit6to4InterfaceIndex", olsr_cnf->niit6to4_if_index);
1095 abuf_json_boolean(abuf, "hasIpv4Gateway", olsr_cnf->has_ipv4_gateway);
1096 abuf_json_boolean(abuf, "hasIpv6Gateway", olsr_cnf->has_ipv6_gateway);
1098 abuf_json_int(abuf, "ioctlSocket", olsr_cnf->ioctl_s);
1100 abuf_json_int(abuf, "routeNetlinkSocket", olsr_cnf->rtnl_s);
1101 abuf_json_int(abuf, "routeMonitorSocket", olsr_cnf->rt_monitor_socket);
1102 #endif /* __linux__ */
1104 #if defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__
1105 abuf_json_int(abuf, "routeChangeSocket", olsr_cnf->rts);
1106 #endif /* defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__ */
1107 abuf_json_float(abuf, "linkQualityNatThreshold", olsr_cnf->lq_nat_thresh);
1109 abuf_json_string(abuf, "olsrdVersion", olsrd_version);
1110 abuf_json_string(abuf, "olsrdBuildDate", build_date);
1111 abuf_json_string(abuf, "olsrdBuildHost", build_host);
1113 #if defined _WIN32 || defined _WIN64
1114 abuf_json_string(abuf, "os", "Windows");
1115 #elif defined __gnu_linux__
1116 abuf_json_string(abuf, "os", "GNU/Linux");
1117 #elif defined __ANDROID__
1118 abuf_json_string(abuf, "os", "Android");
1119 #elif defined __APPLE__
1120 abuf_json_string(abuf, "os", "Mac OS X");
1121 #elif defined __NetBSD__
1122 abuf_json_string(abuf, "os", "NetBSD");
1123 #elif defined __OpenBSD__
1124 abuf_json_string(abuf, "os", "OpenBSD");
1125 #elif defined __FreeBSD__ || defined __FreeBSD_kernel__
1126 abuf_json_string(abuf, "os", "FreeBSD");
1127 #else /* OS detection */
1128 abuf_json_string(abuf, "os", "Undefined");
1129 #endif /* OS detection */
1131 abuf_json_int(abuf, "startTime", start_time.tv_sec);
1133 abuf_json_mark_object(false, false, abuf, NULL);
1136 static void ipc_print_interfaces(struct autobuf *abuf) {
1139 char path[PATH_MAX], linkpath[PATH_MAX];
1140 #endif /* __linux__ */
1141 char ipv6_buf[INET6_ADDRSTRLEN]; /* buffer for IPv6 inet_htop */
1142 struct olsr_lq_mult *mult;
1143 const struct olsr_if *ifs;
1144 abuf_json_mark_object(true, true, abuf, "interfaces");
1145 for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
1146 const struct interface_olsr * const rifs = ifs->interf;
1147 abuf_json_mark_array_entry(true, abuf);
1148 abuf_json_string(abuf, "name", ifs->name);
1150 abuf_json_mark_object(true, true, abuf, "linkQualityMultipliers");
1151 for (mult = ifs->cnf->lq_mult; mult != NULL ; mult = mult->next) {
1152 abuf_json_mark_array_entry(true, abuf);
1153 abuf_json_string(abuf, "route", inet_ntop(olsr_cnf->ip_version, &mult->addr, ipv6_buf, sizeof(ipv6_buf)));
1154 abuf_json_float(abuf, "multiplier", mult->value / 65535.0);
1155 abuf_json_mark_array_entry(false, abuf);
1157 abuf_json_mark_object(false, true, abuf, NULL);
1160 abuf_json_string(abuf, "state", "down");
1162 abuf_json_string(abuf, "state", "up");
1163 abuf_json_string(abuf, "nameFromKernel", rifs->int_name);
1164 abuf_json_int(abuf, "interfaceMode", rifs->mode);
1165 abuf_json_boolean(abuf, "emulatedHostClientInterface", rifs->is_hcif);
1166 abuf_json_boolean(abuf, "sendTcImmediately", rifs->immediate_send_tc);
1167 abuf_json_int(abuf, "fishEyeTtlIndex", rifs->ttl_index);
1168 abuf_json_int(abuf, "olsrForwardingTimeout", rifs->fwdtimer);
1169 abuf_json_int(abuf, "olsrMessageSequenceNumber", rifs->olsr_seqnum);
1170 abuf_json_int(abuf, "olsrInterfaceMetric", rifs->int_metric);
1171 abuf_json_int(abuf, "olsrMTU", rifs->int_mtu);
1172 abuf_json_int(abuf, "helloEmissionInterval", rifs->hello_etime);
1173 abuf_json_int(abuf, "helloValidityTime", me_to_reltime(rifs->valtimes.hello));
1174 abuf_json_int(abuf, "tcValidityTime", me_to_reltime(rifs->valtimes.tc));
1175 abuf_json_int(abuf, "midValidityTime", me_to_reltime(rifs->valtimes.mid));
1176 abuf_json_int(abuf, "hnaValidityTime", me_to_reltime(rifs->valtimes.hna));
1177 abuf_json_boolean(abuf, "wireless", rifs->is_wireless);
1180 abuf_json_boolean(abuf, "icmpRedirect", rifs->nic_state.redirect);
1181 abuf_json_boolean(abuf, "spoofFilter", rifs->nic_state.spoof);
1182 #endif /* __linux__ */
1184 if (olsr_cnf->ip_version == AF_INET) {
1185 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
1186 abuf_json_string(abuf, "ipv4Address", ip4_to_string(&addrbuf, rifs->int_addr.sin_addr));
1187 abuf_json_string(abuf, "netmask", ip4_to_string(&maskbuf, rifs->int_netmask.sin_addr));
1188 abuf_json_string(abuf, "broadcast", ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
1190 struct ipaddr_str addrbuf, maskbuf;
1191 abuf_json_string(abuf, "ipv6Address", ip6_to_string(&addrbuf, &rifs->int6_addr.sin6_addr));
1192 abuf_json_string(abuf, "multicast", ip6_to_string(&maskbuf, &rifs->int6_multaddr.sin6_addr));
1196 snprintf(path, PATH_MAX, "/sys/class/net/%s/device/driver/module", ifs->name);
1197 linklen = readlink(path, linkpath, PATH_MAX - 1);
1199 linkpath[linklen] = '\0';
1200 abuf_json_string(abuf, "kernelModule", basename(linkpath));
1203 abuf_json_sys_class_net(abuf, "addressLength", ifs->name, "addr_len");
1204 abuf_json_sys_class_net(abuf, "carrier", ifs->name, "carrier");
1205 abuf_json_sys_class_net(abuf, "dormant", ifs->name, "dormant");
1206 abuf_json_sys_class_net(abuf, "features", ifs->name, "features");
1207 abuf_json_sys_class_net(abuf, "flags", ifs->name, "flags");
1208 abuf_json_sys_class_net(abuf, "linkMode", ifs->name, "link_mode");
1209 abuf_json_sys_class_net(abuf, "macAddress", ifs->name, "address");
1210 abuf_json_sys_class_net(abuf, "ethernetMTU", ifs->name, "mtu");
1211 abuf_json_sys_class_net(abuf, "operationalState", ifs->name, "operstate");
1212 abuf_json_sys_class_net(abuf, "txQueueLength", ifs->name, "tx_queue_len");
1213 abuf_json_sys_class_net(abuf, "collisions", ifs->name, "statistics/collisions");
1214 abuf_json_sys_class_net(abuf, "multicastPackets", ifs->name, "statistics/multicast");
1215 abuf_json_sys_class_net(abuf, "rxBytes", ifs->name, "statistics/rx_bytes");
1216 abuf_json_sys_class_net(abuf, "rxCompressed", ifs->name, "statistics/rx_compressed");
1217 abuf_json_sys_class_net(abuf, "rxCrcErrors", ifs->name, "statistics/rx_crc_errors");
1218 abuf_json_sys_class_net(abuf, "rxDropped", ifs->name, "statistics/rx_dropped");
1219 abuf_json_sys_class_net(abuf, "rxErrors", ifs->name, "statistics/rx_errors");
1220 abuf_json_sys_class_net(abuf, "rxFifoErrors", ifs->name, "statistics/rx_fifo_errors");
1221 abuf_json_sys_class_net(abuf, "rxFrameErrors", ifs->name, "statistics/rx_frame_errors");
1222 abuf_json_sys_class_net(abuf, "rxLengthErrors", ifs->name, "statistics/rx_length_errors");
1223 abuf_json_sys_class_net(abuf, "rxMissedErrors", ifs->name, "statistics/rx_missed_errors");
1224 abuf_json_sys_class_net(abuf, "rxOverErrors", ifs->name, "statistics/rx_over_errors");
1225 abuf_json_sys_class_net(abuf, "rxPackets", ifs->name, "statistics/rx_packets");
1226 abuf_json_sys_class_net(abuf, "txAbortedErrors", ifs->name, "statistics/tx_aborted_errors");
1227 abuf_json_sys_class_net(abuf, "txBytes", ifs->name, "statistics/tx_bytes");
1228 abuf_json_sys_class_net(abuf, "txCarrierErrors", ifs->name, "statistics/tx_carrier_errors");
1229 abuf_json_sys_class_net(abuf, "txCompressed", ifs->name, "statistics/tx_compressed");
1230 abuf_json_sys_class_net(abuf, "txDropped", ifs->name, "statistics/tx_dropped");
1231 abuf_json_sys_class_net(abuf, "txErrors", ifs->name, "statistics/tx_errors");
1232 abuf_json_sys_class_net(abuf, "txFifoErrors", ifs->name, "statistics/tx_fifo_errors");
1233 abuf_json_sys_class_net(abuf, "txHeartbeatErrors", ifs->name, "statistics/tx_heartbeat_errors");
1234 abuf_json_sys_class_net(abuf, "txPackets", ifs->name, "statistics/tx_packets");
1235 abuf_json_sys_class_net(abuf, "txWindowErrors", ifs->name, "statistics/tx_window_errors");
1236 abuf_json_sys_class_net(abuf, "beaconing", ifs->name, "wireless/beacon");
1237 abuf_json_sys_class_net(abuf, "encryptionKey", ifs->name, "wireless/crypt");
1238 abuf_json_sys_class_net(abuf, "fragmentationThreshold", ifs->name, "wireless/fragment");
1239 abuf_json_sys_class_net(abuf, "signalLevel", ifs->name, "wireless/level");
1240 abuf_json_sys_class_net(abuf, "linkQuality", ifs->name, "wireless/link");
1241 abuf_json_sys_class_net(abuf, "misc", ifs->name, "wireless/misc");
1242 abuf_json_sys_class_net(abuf, "noiseLevel", ifs->name, "wireless/noise");
1243 abuf_json_sys_class_net(abuf, "nwid", ifs->name, "wireless/nwid");
1244 abuf_json_sys_class_net(abuf, "wirelessRetries", ifs->name, "wireless/retries");
1245 abuf_json_sys_class_net(abuf, "wirelessStatus", ifs->name, "wireless/status");
1246 #endif /* __linux__ */
1247 abuf_json_mark_array_entry(false, abuf);
1249 abuf_json_mark_object(false, true, abuf, NULL);
1252 static void ipc_print_olsrd_conf(struct autobuf *abuf) {
1253 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
1256 static void jsoninfo_write_data(void *foo __attribute__ ((unused))) {
1258 int result, i, j, max;
1263 for (i = 0; i < outbuffer_count; i++) {
1264 /* And we cast here since we get a warning on Win32 */
1265 FD_SET((unsigned int )(outbuffer_socket[i]), &set);
1267 if (outbuffer_socket[i] > max) {
1268 max = outbuffer_socket[i];
1275 result = select(max + 1, NULL, &set, NULL, &tv);
1280 for (i = 0; i < outbuffer_count; i++) {
1281 if (FD_ISSET(outbuffer_socket[i], &set)) {
1282 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
1284 outbuffer_written[i] += result;
1287 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
1288 /* close this socket and cleanup*/
1289 close(outbuffer_socket[i]);
1292 for (j = i + 1; j < outbuffer_count; j++) {
1293 outbuffer[j - 1] = outbuffer[j];
1294 outbuffer_size[j - 1] = outbuffer_size[j];
1295 outbuffer_socket[j - 1] = outbuffer_socket[j];
1296 outbuffer_written[j - 1] = outbuffer_written[j];
1302 if (!outbuffer_count) {
1303 olsr_stop_timer(writetimer_entry);
1307 static void send_info(unsigned int send_what, int the_socket) {
1308 struct autobuf abuf;
1309 size_t header_len = 0;
1310 char header_buf[MAX_HTTPHEADER_SIZE];
1311 const char *content_type = "application/json";
1313 /* global variables for tracking when to put a comma in for JSON */
1315 currentjsondepth = 0;
1317 abuf_init(&abuf, 32768);
1319 // only add if outputing JSON
1320 if (send_what & SIW_ALL) {
1321 abuf_json_mark_output(true, &abuf);
1323 abuf_json_int(&abuf, "systemTime", time(NULL));
1324 abuf_json_int(&abuf, "timeSinceStartup", now_times);
1326 abuf_json_string(&abuf, "uuid", uuid);
1328 if (send_what & SIW_NEIGHBORS)
1329 ipc_print_neighbors(&abuf, false);
1330 if (send_what & SIW_LINKS)
1331 ipc_print_links(&abuf);
1332 if (send_what & SIW_ROUTES)
1333 ipc_print_routes(&abuf);
1334 if (send_what & SIW_HNA)
1335 ipc_print_hna(&abuf);
1336 if (send_what & SIW_MID)
1337 ipc_print_mid(&abuf);
1338 if (send_what & SIW_TOPOLOGY)
1339 ipc_print_topology(&abuf);
1340 if (send_what & SIW_GATEWAYS)
1341 ipc_print_gateways(&abuf);
1342 if (send_what & SIW_INTERFACES)
1343 ipc_print_interfaces(&abuf);
1344 if (send_what & SIW_2HOP)
1345 ipc_print_neighbors(&abuf, true);
1346 if (send_what & SIW_SGW)
1347 ipc_print_sgw(&abuf);
1349 if (send_what & SIW_VERSION)
1350 ipc_print_version(&abuf);
1351 if (send_what & SIW_CONFIG)
1352 ipc_print_config(&abuf);
1353 if (send_what & SIW_PLUGINS)
1354 ipc_print_plugins(&abuf);
1356 abuf_json_mark_output(false, &abuf);
1357 abuf_puts(&abuf, "\n");
1358 } else if (send_what & SIW_OLSRD_CONF) {
1359 /* this outputs the olsrd.conf text directly, not JSON */
1360 ipc_print_olsrd_conf(&abuf);
1364 header_len = build_http_header(HTTP_200, content_type, abuf.len, header_buf, sizeof(header_buf));
1367 outbuffer[outbuffer_count] = olsr_malloc(header_len + abuf.len, "json output buffer");
1368 outbuffer_size[outbuffer_count] = header_len + abuf.len;
1369 outbuffer_written[outbuffer_count] = 0;
1370 outbuffer_socket[outbuffer_count] = the_socket;
1372 memcpy(outbuffer[outbuffer_count], header_buf, header_len);
1373 memcpy((outbuffer[outbuffer_count]) + header_len, abuf.buf, abuf.len);
1376 if (outbuffer_count == 1) {
1377 writetimer_entry = olsr_start_timer(100, 0,
1378 OLSR_TIMER_PERIODIC, &jsoninfo_write_data,
1385 static size_t build_http_header(const char *status, const char *mime, uint32_t msgsize, char *buf, uint32_t bufsize) {
1389 size = snprintf(buf, bufsize, "%s\r\n", status);
1393 size += strftime(&buf[size], bufsize - size, "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", localtime(&currtime));
1395 /* Server version */
1396 size += snprintf(&buf[size], bufsize - size, "Server: OLSRD "PLUGIN_NAME" plugin\r\n");
1398 /* connection-type */
1399 size += snprintf(&buf[size], bufsize - size, "Connection: closed\r\n");
1403 size += snprintf(&buf[size], bufsize - size, "Content-type: %s\r\n", mime);
1407 /**No needs to be strict here, access control is based on source IP*/
1408 size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Origin: *\r\n");
1409 size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
1410 size += snprintf(&buf[size], bufsize - size, "Access-Control-Allow-Headers: Accept, Origin, X-Requested-With\r\n");
1411 size += snprintf(&buf[size], bufsize - size, "Access-Control-Max-Age: 1728000\r\n");
1413 /* Content length */
1415 size += snprintf(&buf[size], bufsize - size, "Content-length: %i\r\n", msgsize);
1419 * No caching dynamic pages
1421 size += snprintf(&buf[size], bufsize - size, "Cache-Control: no-cache\r\n");
1424 size += snprintf(&buf[size], bufsize - size, "\r\n");
1434 * indent-tabs-mode: nil