3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
5 * includes code by Bruno Randolf
6 * includes code by Andreas Lopatic
7 * includes code by Sven-Ola Tuecke
8 * includes code by Lorenz Schori
9 * includes bugs by Markus Kittenberger
10 * includes bugs by Hans-Christoph Steiner
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * * Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
23 * * Neither the name of olsr.org, olsrd nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 * Visit http://www.olsr.org for more information.
42 * If you find this software useful feel free to make a donation
43 * to the project. For more information see the website or contact
44 * the copyright holders.
49 * Dynamic linked library for the olsr.org olsr daemon
53 #include <sys/types.h>
54 #include <sys/socket.h>
56 #include <sys/select.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
71 #include "olsr_types.h"
72 #include "neighbor_table.h"
73 #include "two_hop_neighbor_table.h"
74 #include "mpr_selector_set.h"
80 #include "lq_plugin.h"
81 #include "common/autobuf.h"
84 #include "olsrd_jsoninfo.h"
85 #include "olsrd_plugin.h"
88 #define close(x) closesocket(x)
91 static int ipc_socket;
93 /* IPC initialization function */
94 static int plugin_ipc_init(void);
96 static void abuf_json_open_object(struct autobuf *abuf, const char* header);
97 static void abuf_json_close_object(struct autobuf *abuf);
98 static void abuf_json_open_array(struct autobuf *abuf, const char* header);
99 static void abuf_json_close_array(struct autobuf *abuf);
100 static void abuf_json_open_array_entry(struct autobuf *abuf);
101 static void abuf_json_close_array_entry(struct autobuf *abuf);
102 static void abuf_json_boolean(struct autobuf *abuf, const char* key, int value);
103 static void abuf_json_string(struct autobuf *abuf, const char* key, const char* value);
104 static void abuf_json_int(struct autobuf *abuf, const char* key, long value);
105 static void abuf_json_float(struct autobuf *abuf, const char* key, float value);
107 static void send_info(unsigned int /*send_what*/, int /*socket*/);
108 static void ipc_action(int, void *, unsigned int);
109 static void ipc_print_neighbors(struct autobuf *);
110 static void ipc_print_links(struct autobuf *);
111 static void ipc_print_routes(struct autobuf *);
112 static void ipc_print_topology(struct autobuf *);
113 static void ipc_print_hna(struct autobuf *);
114 static void ipc_print_mid(struct autobuf *);
115 static void ipc_print_gateways(struct autobuf *);
116 static void ipc_print_config(struct autobuf *);
117 static void ipc_print_interfaces(struct autobuf *);
118 static void ipc_print_plugins(struct autobuf *);
119 static void ipc_print_olsrd_conf(struct autobuf *abuf);
121 #define TXT_IPC_BUFSIZE 256
123 #define SIW_NEIGHBORS 0x0001
124 #define SIW_LINKS 0x0002
125 #define SIW_ROUTES 0x0004
126 #define SIW_HNA 0x0008
127 #define SIW_MID 0x0010
128 #define SIW_TOPOLOGY 0x0020
129 #define SIW_GATEWAYS 0x0040
130 #define SIW_INTERFACES 0x0080
131 #define SIW_CONFIG 0x0100
132 #define SIW_ALL 0x00FF
134 /* these don't change at runtime, so they are not part of ALL/status */
135 #define SIW_PLUGINS 0x0200
136 #define SIW_OLSRD_CONF 0x0400
138 #define MAX_CLIENTS 3
140 static char *outbuffer[MAX_CLIENTS];
141 static size_t outbuffer_size[MAX_CLIENTS];
142 static size_t outbuffer_written[MAX_CLIENTS];
143 static int outbuffer_socket[MAX_CLIENTS];
144 static int outbuffer_count;
146 static struct timeval start_time;
147 static struct timer_entry *writetimer_entry;
150 /* JSON support functions */
153 /* JSON does not tolerate commas dangling at the end of arrays, so we need to
154 * count which entry number we're at in order to make sure we don't tack a
155 * dangling comma on at the end */
156 static int entrynumber = 0;
157 static int arrayentrynumber = 0;
160 abuf_json_open_object(struct autobuf *abuf, const char* header)
163 abuf_appendf(abuf, "{\"%s\": {", header);
167 abuf_json_close_object(struct autobuf *abuf)
169 abuf_appendf(abuf, "\t}\n}\n");
173 abuf_json_open_array(struct autobuf *abuf, const char* header)
175 arrayentrynumber = 0;
176 abuf_appendf(abuf, "{\"%s\": [\n", header);
180 abuf_json_close_array(struct autobuf *abuf)
182 abuf_appendf(abuf, "]}\n");
186 abuf_json_open_array_entry(struct autobuf *abuf)
189 if (arrayentrynumber)
190 abuf_appendf(abuf, ",\n{");
192 abuf_appendf(abuf, "{");
197 abuf_json_close_array_entry(struct autobuf *abuf)
199 abuf_appendf(abuf, "}");
203 abuf_json_boolean(struct autobuf *abuf, const char* key, int value)
206 abuf_appendf(abuf, ",\n");
208 abuf_appendf(abuf, "\n");
209 abuf_appendf(abuf, "\t\"%s\": %s", key, value ? "true" : "false");
214 abuf_json_string(struct autobuf *abuf, const char* key, const char* value)
217 abuf_appendf(abuf, ",\n");
219 abuf_appendf(abuf, "\n");
220 abuf_appendf(abuf, "\t\"%s\": \"%s\"", key, value);
225 abuf_json_int(struct autobuf *abuf, const char* key, long value)
228 abuf_appendf(abuf, ",\n");
230 abuf_appendf(abuf, "\n");
231 abuf_appendf(abuf, "\t\"%s\": %li", key, value);
236 abuf_json_float(struct autobuf *abuf, const char* key, float value)
239 abuf_appendf(abuf, ",\n");
241 abuf_appendf(abuf, "\n");
242 abuf_appendf(abuf, "\t\"%s\": %.03f", key, value);
248 *Do initialization here
250 *This function is called by the my_init
251 *function in uolsrd_plugin.c
254 olsrd_plugin_init(void)
256 /* Initial IPC value */
260 gettimeofday(&start_time, NULL);
267 * destructor - called at unload
270 olsr_plugin_exit(void)
272 if (ipc_socket != -1)
277 plugin_ipc_init(void)
279 union olsr_sockaddr sst;
283 /* Init ipc socket */
284 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
286 olsr_printf(1, "(JSONINFO) socket()=%s\n", strerror(errno));
290 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) {
292 olsr_printf(1, "(JSONINFO) setsockopt()=%s\n", strerror(errno));
296 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
297 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0) {
298 perror("SO_REUSEADDR failed");
302 /* Bind the socket */
304 /* complete the socket structure */
305 memset(&sst, 0, sizeof(sst));
306 if (olsr_cnf->ip_version == AF_INET) {
307 sst.in4.sin_family = AF_INET;
308 addrlen = sizeof(struct sockaddr_in);
310 sst.in4.sin_len = addrlen;
312 sst.in4.sin_addr.s_addr = jsoninfo_listen_ip.v4.s_addr;
313 sst.in4.sin_port = htons(ipc_port);
315 sst.in6.sin6_family = AF_INET6;
316 addrlen = sizeof(struct sockaddr_in6);
318 sst.in6.sin6_len = addrlen;
320 sst.in6.sin6_addr = jsoninfo_listen_ip.v6;
321 sst.in6.sin6_port = htons(ipc_port);
324 /* bind the socket to the port number */
325 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
327 olsr_printf(1, "(JSONINFO) bind()=%s\n", strerror(errno));
332 /* show that we are willing to listen */
333 if (listen(ipc_socket, 1) == -1) {
335 olsr_printf(1, "(JSONINFO) listen()=%s\n", strerror(errno));
340 /* Register with olsrd */
341 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
344 olsr_printf(2, "(JSONINFO) listening on port %d\n", ipc_port);
351 ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused)))
353 union olsr_sockaddr pin;
355 char addr[INET6_ADDRSTRLEN];
358 unsigned int send_what = 0;
361 socklen_t addrlen = sizeof(pin);
363 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
365 olsr_printf(1, "(JSONINFO) accept()=%s\n", strerror(errno));
370 tv.tv_sec = tv.tv_usec = 0;
371 if (olsr_cnf->ip_version == AF_INET) {
372 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
374 if (!ip4equal(&pin.in4.sin_addr, &jsoninfo_accept_ip.v4) && jsoninfo_accept_ip.v4.s_addr != INADDR_ANY) {
375 #ifdef JSONINFO_ALLOW_LOCALHOST
376 if (pin.in4.sin_addr.s_addr != INADDR_LOOPBACK) {
378 olsr_printf(1, "(JSONINFO) From host(%s) not allowed!\n", addr);
379 close(ipc_connection);
381 #ifdef JSONINFO_ALLOW_LOCALHOST
386 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
388 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
389 if (!ip6equal(&in6addr_any, &jsoninfo_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &jsoninfo_accept_ip.v6)) {
390 olsr_printf(1, "(JSONINFO) From host(%s) not allowed!\n", addr);
391 close(ipc_connection);
397 olsr_printf(2, "(JSONINFO) Connect from %s\n", addr);
400 /* purge read buffer to prevent blocking on linux */
402 FD_SET((unsigned int)ipc_connection, &rfds); /* Win32 needs the cast here */
403 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
405 ssize_t s = recv(ipc_connection, (void *)&requ, sizeof(requ), 0); /* Win32 needs the cast here */
408 /* print out the requested tables */
409 if (0 != strstr(requ, "/status")) send_what = SIW_ALL;
410 else { /* included in /status */
411 if (0 != strstr(requ, "/neighbors")) send_what |= SIW_NEIGHBORS;
412 if (0 != strstr(requ, "/links")) send_what |= SIW_LINKS;
413 if (0 != strstr(requ, "/routes")) send_what |= SIW_ROUTES;
414 if (0 != strstr(requ, "/hna")) send_what |= SIW_HNA;
415 if (0 != strstr(requ, "/mid")) send_what |= SIW_MID;
416 if (0 != strstr(requ, "/topology")) send_what |= SIW_TOPOLOGY;
417 if (0 != strstr(requ, "/gateways")) send_what |= SIW_GATEWAYS;
418 if (0 != strstr(requ, "/interfaces")) send_what |= SIW_INTERFACES;
420 /* these don't change during runtime, so leave them out of ALL */
421 if (0 != strstr(requ, "/config")) send_what |= SIW_CONFIG;
422 if (0 != strstr(requ, "/plugins")) send_what |= SIW_PLUGINS;
423 if (0 != strstr(requ, "/olsrd.conf")) send_what |= SIW_OLSRD_CONF;
425 if ( send_what == 0 ) send_what = SIW_ALL;
428 send_info(send_what, ipc_connection);
432 ipc_print_neighbors(struct autobuf *abuf)
434 struct ipaddr_str buf1;
435 struct neighbor_entry *neigh;
436 struct neighbor_2_list_entry *list_2;
439 abuf_json_open_array(abuf, "neighbors");
442 OLSR_FOR_ALL_NBR_ENTRIES(neigh) {
443 abuf_json_open_array_entry(abuf);
444 abuf_json_string(abuf, "ipv4Address",
445 olsr_ip_to_string(&buf1, &neigh->neighbor_main_addr));
446 abuf_json_boolean(abuf, "symmetric", (neigh->status == SYM));
447 abuf_json_boolean(abuf, "multiPointRelay", neigh->is_mpr);
448 abuf_json_boolean(abuf, "multiPointRelaySelector",
449 olsr_lookup_mprs_set(&neigh->neighbor_main_addr) != NULL);
450 abuf_json_int(abuf, "willingness", neigh->willingness);
451 abuf_appendf(abuf, ",\n");
454 if (neigh->neighbor_2_list.next) {
455 abuf_appendf(abuf, "\t\"twoHopNeighbors\": [");
456 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
458 abuf_appendf(abuf, ", ");
461 olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
465 abuf_appendf(abuf, "]");
466 abuf_json_int(abuf, "twoHopNeighborCount", thop_cnt);
467 abuf_json_close_array_entry(abuf);
469 OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
470 abuf_json_close_array(abuf);
474 ipc_print_links(struct autobuf *abuf)
476 struct ipaddr_str buf1, buf2;
477 struct lqtextbuffer lqbuffer1, lqbuffer2;
479 struct link_entry *my_link = NULL;
481 abuf_json_open_array(abuf, "links");
482 OLSR_FOR_ALL_LINK_ENTRIES(my_link) {
484 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
486 abuf_json_open_array_entry(abuf);
487 abuf_json_string(abuf, "localIP",
488 olsr_ip_to_string(&buf1, &my_link->local_iface_addr));
489 abuf_json_string(abuf, "remoteIP",
490 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr));
491 abuf_json_int(abuf, "validityTime", diff);
492 lqs = get_link_entry_text(my_link, '\t', &lqbuffer1);
493 abuf_json_float(abuf, "linkQuality", atof(lqs));
494 abuf_json_float(abuf, "neighborLinkQuality", atof(strrchr(lqs, '\t')));
495 abuf_json_float(abuf, "linkCost",
496 atof(get_linkcost_text(my_link->linkcost, false, &lqbuffer2)));
497 abuf_json_close_array_entry(abuf);
499 OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
500 abuf_json_close_array(abuf);
504 ipc_print_routes(struct autobuf *abuf)
506 struct ipaddr_str buf1, buf2;
508 struct lqtextbuffer lqbuffer;
510 //abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
511 abuf_json_open_array(abuf, "routes");
513 /* Walk the route table */
514 OLSR_FOR_ALL_RT_ENTRIES(rt) {
515 abuf_json_open_array_entry(abuf);
516 abuf_json_string(abuf, "destination",
517 olsr_ip_to_string(&buf1, &rt->rt_dst.prefix));
518 abuf_json_int(abuf, "genmask", rt->rt_dst.prefix_len);
519 abuf_json_string(abuf, "gateway",
520 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway));
521 abuf_json_int(abuf, "metric", rt->rt_best->rtp_metric.hops);
522 abuf_json_float(abuf, "expectedTransmissionCount",
523 atof(get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer)));
524 abuf_json_string(abuf, "interface",
525 if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
526 abuf_json_close_array_entry(abuf);
528 OLSR_FOR_ALL_RT_ENTRIES_END(rt);
530 abuf_json_close_array(abuf);
534 ipc_print_topology(struct autobuf *abuf)
538 abuf_json_open_array(abuf, "topology");
539 //abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
542 OLSR_FOR_ALL_TC_ENTRIES(tc) {
543 struct tc_edge_entry *tc_edge;
544 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge) {
545 if (tc_edge->edge_inv) {
546 struct ipaddr_str dstbuf, addrbuf;
547 struct lqtextbuffer lqbuffer1, lqbuffer2;
548 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
549 int diff = (int)(vt);
551 abuf_json_open_array_entry(abuf);
552 abuf_json_string(abuf, "destinationIP",
553 olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr));
554 abuf_json_string(abuf, "lastHopIP",
555 olsr_ip_to_string(&addrbuf, &tc->addr));
556 lqs = get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1);
557 abuf_json_float(abuf, "linkQuality", atof(lqs));
558 abuf_json_float(abuf, "neighborLinkQuality", atof(strrchr(lqs, '\t')));
559 abuf_json_float(abuf, "cost",
560 atof(get_linkcost_text(tc_edge->cost, false, &lqbuffer2)));
561 abuf_json_int(abuf, "validityTime", diff);
562 abuf_json_close_array_entry(abuf);
565 OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
567 OLSR_FOR_ALL_TC_ENTRIES_END(tc);
569 abuf_json_close_array(abuf);
573 ipc_print_hna(struct autobuf *abuf)
575 struct ip_prefix_list *hna;
576 struct hna_entry *tmp_hna;
577 struct hna_net *tmp_net;
578 struct ipaddr_str buf, mainaddrbuf;
580 abuf_json_open_array(abuf, "hna");
582 /* Announced HNA entries */
583 if (olsr_cnf->ip_version == AF_INET) {
584 for (hna = olsr_cnf->hna_entries; hna != NULL; hna = hna->next) {
585 abuf_json_open_array_entry(abuf);
586 abuf_json_string(abuf, "destination",
587 olsr_ip_to_string(&buf, &hna->net.prefix));
588 abuf_json_int(abuf, "genmask", hna->net.prefix_len);
589 abuf_json_string(abuf, "gateway",
590 olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
591 abuf_json_close_array_entry(abuf);
594 for (hna = olsr_cnf->hna_entries; hna != NULL; hna = hna->next) {
595 abuf_json_open_array_entry(abuf);
596 abuf_json_string(abuf, "destination",
597 olsr_ip_to_string(&buf, &hna->net.prefix));
598 abuf_json_int(abuf, "genmask", hna->net.prefix_len);
599 abuf_json_string(abuf, "gateway",
600 olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
601 abuf_json_close_array_entry(abuf);
606 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna) {
608 /* Check all networks */
609 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
610 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
611 int diff = (int)(vt);
612 abuf_json_open_array_entry(abuf);
613 abuf_json_string(abuf, "destination",
614 olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix)),
615 abuf_json_int(abuf, "genmask", tmp_net->hna_prefix.prefix_len);
616 abuf_json_string(abuf, "gateway",
617 olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
618 abuf_json_int(abuf, "validityTime", diff);
619 abuf_json_close_array_entry(abuf);
622 OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
624 abuf_json_close_array(abuf);
628 ipc_print_mid(struct autobuf *abuf)
631 struct mid_entry *entry;
632 struct mid_address *alias;
634 abuf_json_open_array(abuf, "mid");
637 for (idx = 0; idx < HASHSIZE; idx++) {
638 entry = mid_set[idx].next;
640 while (entry != &mid_set[idx]) {
641 struct ipaddr_str buf, buf2;
642 alias = entry->aliases;
645 uint32_t vt = alias->vtime - now_times;
646 int diff = (int)(vt);
648 abuf_json_open_array_entry(abuf);
649 abuf_json_string(abuf, "ipv4Address",
650 olsr_ip_to_string(&buf, &entry->main_addr));
651 abuf_json_string(abuf, "alias",
652 olsr_ip_to_string(&buf2, &alias->alias));
653 abuf_json_open_array_entry(abuf);
654 abuf_json_int(abuf, "validityTime", diff);
655 abuf_json_close_array_entry(abuf);
656 alias = alias->next_alias;
661 abuf_json_close_array(abuf);
665 ipc_print_gateways(struct autobuf *abuf)
668 abuf_json_string(abuf, "error", "Gateway mode is only supported in Linux");
671 struct ipaddr_str buf;
672 struct gateway_entry *gw;
673 struct lqtextbuffer lqbuf;
675 abuf_json_open_array(abuf, "gateways");
676 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw) {
677 const char *v4 = "", *v6 = "";
678 bool autoV4 = false, autoV6 = false;
679 const char *ipType = "";
682 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
686 if (gw == olsr_get_ipv4_inet_gateway(&autoV4)) {
687 v4 = autoV4 ? "auto" : "s";
688 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit)
689 && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
693 if (gw == olsr_get_ipv6_inet_gateway(&autoV6)) {
694 v6 = autoV6 ? "auto" : "s";
695 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
699 abuf_json_open_array_entry(abuf);
702 abuf_json_string(abuf, "ipv4Status", v4);
703 } else if (gw->ipv6) {
705 abuf_json_string(abuf, "ipv6Status", v6);
707 abuf_json_string(abuf, "ipType", ipType);
708 abuf_json_boolean(abuf, "ipv4", gw->ipv4);
709 abuf_json_boolean(abuf, "ipv4Nat", gw->ipv4nat);
710 abuf_json_boolean(abuf, "ipv6", gw->ipv6);
711 abuf_json_boolean(abuf, "autoIpv4", autoV4);
712 abuf_json_boolean(abuf, "autoIpv6", autoV6);
713 abuf_json_string(abuf, "ipAddress",
714 olsr_ip_to_string(&buf, &gw->originator));
715 abuf_json_float(abuf, "expectedTransmissionCount",
716 atof(get_linkcost_text(tc->path_cost, true, &lqbuf)));
717 abuf_json_int(abuf, "hopCount", tc->hops);
718 abuf_json_int(abuf, "uplinkSpeed", gw->uplink);
719 abuf_json_int(abuf, "downlinkSpeed", gw->downlink);
720 if(gw->external_prefix.prefix_len == 0)
721 abuf_json_string(abuf, "externalPrefix",
722 olsr_ip_prefix_to_string(&gw->external_prefix));
723 abuf_json_close_array_entry(abuf);
725 OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
726 abuf_json_close_array(abuf);
732 ipc_print_plugins(struct autobuf *abuf)
734 struct plugin_entry *pentry;
735 struct plugin_param *pparam;
736 abuf_json_open_array(abuf, "plugins");
737 if (olsr_cnf->plugins)
738 for (pentry = olsr_cnf->plugins; pentry; pentry = pentry->next) {
739 abuf_json_open_array_entry(abuf);
740 abuf_json_string(abuf, "plugin", pentry->name);
741 for (pparam = pentry->params; pparam; pparam = pparam->next) {
742 abuf_json_string(abuf, pparam->key, pparam->value);
744 abuf_json_close_array_entry(abuf);
746 abuf_json_close_array(abuf);
751 ipc_print_config(struct autobuf *abuf)
753 struct ipaddr_str mainaddrbuf;
754 struct ip_prefix_list *ipcn;
756 abuf_json_open_object(abuf, "config");
758 abuf_json_int(abuf, "olsrPort", olsr_cnf->olsrport);
759 abuf_json_int(abuf, "debugLevel", olsr_cnf->debug_level);
760 abuf_json_int(abuf, "noFork", olsr_cnf->no_fork);
761 abuf_json_boolean(abuf, "hostEmulation", olsr_cnf->host_emul);
762 abuf_json_int(abuf, "ipVersion", olsr_cnf->ip_version);
763 abuf_json_boolean(abuf, "allowNoInterfaces", olsr_cnf->allow_no_interfaces);
764 abuf_json_int(abuf, "typeOfService", olsr_cnf->tos);
765 abuf_json_int(abuf, "rtProto", olsr_cnf->rt_proto);
766 abuf_json_int(abuf, "rtTable", olsr_cnf->rt_table);
767 abuf_json_int(abuf, "rtTableDefault", olsr_cnf->rt_table_default);
768 abuf_json_int(abuf, "rtTableTunnel", olsr_cnf->rt_table_tunnel);
769 abuf_json_int(abuf, "rtTablePriority", olsr_cnf->rt_table_pri);
770 abuf_json_int(abuf, "rtTableTunnelPriority", olsr_cnf->rt_table_tunnel_pri);
771 abuf_json_int(abuf, "rtTableDefauiltOlsrPriority", olsr_cnf->rt_table_defaultolsr_pri);
772 abuf_json_int(abuf, "rtTableDefaultPriority", olsr_cnf->rt_table_default_pri);
773 abuf_json_int(abuf, "willingness", olsr_cnf->willingness);
774 abuf_json_boolean(abuf, "willingnessAuto", olsr_cnf->willingness_auto);
776 abuf_json_string(abuf, "fibMetrics", FIB_METRIC_TXT[olsr_cnf->fib_metric]);
778 struct if_config_options *interface_defaults;
780 abuf_json_int(abuf, "ipcConnections", olsr_cnf->ipc_connections);
781 if (olsr_cnf->ipc_connections)
782 for (ipcn = olsr_cnf->ipc_nets; ipcn != NULL; ipcn = ipcn->next) {
783 abuf_json_string(abuf, "ipcAllowedAddress",
784 olsr_ip_to_string(&mainaddrbuf, &ipcn->net.prefix));
785 abuf_json_int(abuf, "ipcAllowedAddressMask", ipcn->net.prefix_len);
788 // keep all time in ms, so convert these two, which are in seconds
789 abuf_json_int(abuf, "pollRate", olsr_cnf->pollrate * 1000);
790 abuf_json_int(abuf, "nicChangePollInterval", olsr_cnf->nic_chgs_pollrate * 1000);
791 abuf_json_boolean(abuf, "clearScreen", olsr_cnf->clear_screen);
792 abuf_json_int(abuf, "tcRedundancy", olsr_cnf->tc_redundancy);
793 abuf_json_int(abuf, "mprCoverage", olsr_cnf->mpr_coverage);
795 if (olsr_cnf->lq_level == 0) {
796 abuf_json_boolean(abuf, "useHysteresis", olsr_cnf->use_hysteresis);
797 if (olsr_cnf->use_hysteresis) {
798 abuf_json_float(abuf, "hysteresisScaling", olsr_cnf->hysteresis_param.scaling);
799 abuf_json_float(abuf, "hysteresisLowThreshold", olsr_cnf->hysteresis_param.thr_low);
800 abuf_json_float(abuf, "hysteresisHighThreshold", olsr_cnf->hysteresis_param.thr_high);
803 abuf_json_int(abuf, "linkQualityLevel", olsr_cnf->lq_level);
804 abuf_json_int(abuf, "linkQualityFisheye", olsr_cnf->lq_fish);
805 abuf_json_float(abuf, "linkQualityAging", olsr_cnf->lq_aging);
806 abuf_json_string(abuf, "linkQualityAlgorithm", olsr_cnf->lq_algorithm);
807 // keep all time in ms, so convert this from seconds
808 abuf_json_int(abuf, "minTcValidTime", olsr_cnf->min_tc_vtime * 1000);
809 abuf_json_boolean(abuf, "setIpForward", olsr_cnf->set_ip_forward);
810 abuf_json_string(abuf, "lockFile", olsr_cnf->lock_file);
811 abuf_json_boolean(abuf, "useNiit", olsr_cnf->use_niit);
813 abuf_json_boolean(abuf, "smartGateway", olsr_cnf->smart_gw_active);
814 if (olsr_cnf->smart_gw_active) {
815 abuf_json_boolean(abuf, "smartGatewayAllowNat", olsr_cnf->smart_gw_allow_nat);
816 abuf_json_boolean(abuf, "smartGatewayUplinkNat", olsr_cnf->smart_gw_uplink_nat);
817 abuf_json_int(abuf, "smartGatewayPeriod", olsr_cnf->smart_gw_period);
818 abuf_json_int(abuf, "smartGatewayStableCount", olsr_cnf->smart_gw_stablecount);
819 abuf_json_int(abuf, "smartGatewayThreshold", olsr_cnf->smart_gw_thresh);
820 abuf_json_int(abuf, "smartGatewayUplink", olsr_cnf->smart_gw_uplink);
821 abuf_json_int(abuf, "smartGatewayDownlink", olsr_cnf->smart_gw_downlink);
822 abuf_json_int(abuf, "smartGatewayType", olsr_cnf->smart_gw_type);
823 abuf_json_string(abuf, "smartGatewayPrefix",
824 olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->smart_gw_prefix.prefix));
825 abuf_json_int(abuf, "smartGatewayPrefixLength", olsr_cnf->smart_gw_prefix.prefix_len);
828 abuf_json_string(abuf, "mainIpAddress",
829 olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
830 abuf_json_string(abuf, "unicastSourceIpAddress",
831 olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->unicast_src_ip));
833 abuf_json_boolean(abuf, "useSourceIpRoutes", olsr_cnf->use_src_ip_routes);
835 abuf_json_int(abuf, "maxPrefixLength", olsr_cnf->maxplen);
836 abuf_json_int(abuf, "ipSize", olsr_cnf->ipsize);
837 abuf_json_boolean(abuf, "deleteInternetGatewaysAtStartup", olsr_cnf->del_gws);
838 // keep all time in ms, so convert this from seconds
839 abuf_json_int(abuf, "willingnessUpdateInterval", olsr_cnf->will_int * 1000);
840 abuf_json_float(abuf, "maxSendMessageJitter", olsr_cnf->max_jitter);
841 abuf_json_int(abuf, "exitValue", olsr_cnf->exit_value);
842 // keep all time in ms, so convert this from seconds
843 abuf_json_int(abuf, "maxTcValidTime", olsr_cnf->max_tc_vtime * 1000);
845 abuf_json_int(abuf, "niit4to6InterfaceIndex", olsr_cnf->niit4to6_if_index);
846 abuf_json_int(abuf, "niit6to4InterfaceIndex", olsr_cnf->niit6to4_if_index);
848 abuf_json_boolean(abuf, "hasIpv4Gateway", olsr_cnf->has_ipv4_gateway);
849 abuf_json_boolean(abuf, "hasIpv6Gateway", olsr_cnf->has_ipv6_gateway);
851 abuf_json_int(abuf, "ioctlSocket", olsr_cnf->ioctl_s);
853 abuf_json_int(abuf, "routeNetlinkSocket", olsr_cnf->rtnl_s);
854 abuf_json_int(abuf, "routeMonitorSocket", olsr_cnf->rt_monitor_socket);
857 #if defined __FreeBSD__ || defined __FreeBSD_kernel__ || defined __APPLE__ || defined __NetBSD__ || defined __OpenBSD__
858 abuf_json_int(abuf, "routeChangeSocket", olsr_cnf->rts);
860 abuf_json_float(abuf, "linkQualityNatThreshold", olsr_cnf->lq_nat_thresh);
862 abuf_json_string(abuf, "olsrdVersion", olsrd_version);
863 abuf_json_string(abuf, "olsrdBuildDate", build_date);
864 abuf_json_string(abuf, "olsrdBuildHost", build_host);
866 #if defined _WIN32 || defined _WIN64
867 abuf_json_string(abuf, "os", "Windows");
868 #elif defined __gnu_linux__
869 abuf_json_string(abuf, "os", "GNU/Linux");
870 #elif defined __ANDROID__
871 abuf_json_string(abuf, "os", "Android");
872 #elif defined __APPLE__
873 abuf_json_string(abuf, "os", "Mac OS X");
874 #elif defined __NetBSD__
875 abuf_json_string(abuf, "os", "NetBSD");
876 #elif defined __OpenBSD__
877 abuf_json_string(abuf, "os", "OpenBSD");
878 #elif defined __FreeBSD__ || defined __FreeBSD_kernel__
879 abuf_json_string(abuf, "os", "FreeBSD");
881 abuf_json_string(abuf, "os", "Undefined");
884 abuf_json_int(abuf, "systemTime", time(NULL));
885 abuf_json_int(abuf, "startTime", start_time.tv_sec);
887 abuf_json_close_object(abuf);
891 ipc_print_interfaces(struct autobuf *abuf)
893 const struct olsr_if *ifs;
894 abuf_json_open_array(abuf, "interfaces");
895 //abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
896 for (ifs = olsr_cnf->interfaces; ifs != NULL; ifs = ifs->next) {
897 const struct interface *const rifs = ifs->interf;
898 abuf_json_open_array_entry(abuf);
899 abuf_json_string(abuf, "name", ifs->name);
901 abuf_json_string(abuf, "state", "down");
903 abuf_json_string(abuf, "state", "up");
904 abuf_json_int(abuf, "mtu", rifs->int_mtu);
905 abuf_json_boolean(abuf, "wireless", rifs->is_wireless);
907 if (olsr_cnf->ip_version == AF_INET) {
908 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
909 abuf_json_string(abuf, "ipv4Address",
910 ip4_to_string(&addrbuf, rifs->int_addr.sin_addr));
911 abuf_json_string(abuf, "netmask",
912 ip4_to_string(&maskbuf, rifs->int_netmask.sin_addr));
913 abuf_json_string(abuf, "broadcast",
914 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
916 struct ipaddr_str addrbuf, maskbuf;
917 abuf_json_string(abuf, "ipv6Address",
918 ip6_to_string(&addrbuf, &rifs->int6_addr.sin6_addr));
919 abuf_json_string(abuf, "multicast",
920 ip6_to_string(&maskbuf, &rifs->int6_multaddr.sin6_addr));
923 abuf_json_close_array_entry(abuf);
925 abuf_json_close_array(abuf);
930 ipc_print_olsrd_conf(struct autobuf *abuf)
932 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
937 jsoninfo_write_data(void *foo __attribute__ ((unused)))
940 int result, i, j, max;
945 for (i=0; i<outbuffer_count; i++) {
946 /* And we cast here since we get a warning on Win32 */
947 FD_SET((unsigned int)(outbuffer_socket[i]), &set);
949 if (outbuffer_socket[i] > max) {
950 max = outbuffer_socket[i];
957 result = select(max + 1, NULL, &set, NULL, &tv);
962 for (i=0; i<outbuffer_count; i++) {
963 if (FD_ISSET(outbuffer_socket[i], &set)) {
964 result = send(outbuffer_socket[i],
965 outbuffer[i] + outbuffer_written[i],
966 outbuffer_size[i] - outbuffer_written[i],
969 outbuffer_written[i] += result;
972 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
973 /* close this socket and cleanup*/
974 close(outbuffer_socket[i]);
977 for (j=i+1; j<outbuffer_count; j++) {
978 outbuffer[j-1] = outbuffer[j];
979 outbuffer_size[j-1] = outbuffer_size[j];
980 outbuffer_socket[j-1] = outbuffer_socket[j];
981 outbuffer_written[j-1] = outbuffer_written[j];
987 if (outbuffer_count == 0) {
988 olsr_stop_timer(writetimer_entry);
993 send_info(unsigned int send_what, int the_socket)
997 abuf_init(&abuf, 32768);
999 /* wrap everything in a JSON array to handle multiple elements*/
1000 abuf_puts(&abuf, "\n[\n");
1002 /* Print tables to IPC socket */
1004 if ((send_what & SIW_LINKS) == SIW_LINKS) {
1005 ipc_print_links(&abuf);
1006 // if this is being output with others, then include commas
1007 if (send_what != SIW_LINKS) abuf_puts(&abuf, ",");
1009 if ((send_what & SIW_NEIGHBORS) == SIW_NEIGHBORS) {
1010 ipc_print_neighbors(&abuf);
1011 if (send_what != SIW_NEIGHBORS) abuf_puts(&abuf, ",");
1013 if ((send_what & SIW_TOPOLOGY) == SIW_TOPOLOGY) {
1014 ipc_print_topology(&abuf);
1015 if (send_what != SIW_TOPOLOGY) abuf_puts(&abuf, ",");
1017 if ((send_what & SIW_HNA) == SIW_HNA) {
1018 ipc_print_hna(&abuf);
1019 if (send_what != SIW_HNA) abuf_puts(&abuf, ",");
1021 if ((send_what & SIW_MID) == SIW_MID) {
1022 ipc_print_mid(&abuf);
1023 if (send_what != SIW_MID) abuf_puts(&abuf, ",");
1025 if ((send_what & SIW_ROUTES) == SIW_ROUTES) {
1026 ipc_print_routes(&abuf);
1027 if (send_what != SIW_ROUTES) abuf_puts(&abuf, ",");
1029 if ((send_what & SIW_GATEWAYS) == SIW_GATEWAYS) {
1030 ipc_print_gateways(&abuf);
1031 if (send_what != SIW_GATEWAYS) abuf_puts(&abuf, ",");
1033 if ((send_what & SIW_INTERFACES) == SIW_INTERFACES) {
1034 ipc_print_interfaces(&abuf);
1035 if (send_what != SIW_INTERFACES) abuf_puts(&abuf, ",");
1037 if ((send_what & SIW_CONFIG) == SIW_CONFIG) {
1038 ipc_print_config(&abuf);
1039 if (send_what != SIW_CONFIG) abuf_puts(&abuf, ",");
1041 if ((send_what & SIW_PLUGINS) == SIW_PLUGINS) {
1042 ipc_print_plugins(&abuf);
1045 /* end of JSON array for status */
1046 abuf_puts(&abuf, "]\n");
1048 /* this outputs the olsrd.conf text directly, not JSON */
1049 if ((send_what & SIW_OLSRD_CONF) == SIW_OLSRD_CONF) {
1050 ipc_print_olsrd_conf(&abuf);
1053 outbuffer[outbuffer_count] = olsr_malloc(abuf.len, "txt output buffer");
1054 outbuffer_size[outbuffer_count] = abuf.len;
1055 outbuffer_written[outbuffer_count] = 0;
1056 outbuffer_socket[outbuffer_count] = the_socket;
1058 memcpy(outbuffer[outbuffer_count], abuf.buf, abuf.len);
1061 if (outbuffer_count == 1) {
1062 writetimer_entry = olsr_start_timer(100,
1064 OLSR_TIMER_PERIODIC,
1065 &jsoninfo_write_data,
1078 * indent-tabs-mode: nil