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_array(struct autobuf *abuf, const char* header);
97 static void abuf_json_close_array(struct autobuf *abuf);
98 static void abuf_json_open_array_entry(struct autobuf *abuf);
99 static void abuf_json_close_array_entry(struct autobuf *abuf);
100 static void abuf_json_boolean(struct autobuf *abuf, const char* key, int value);
101 static void abuf_json_string(struct autobuf *abuf, const char* key, const char* value);
102 static void abuf_json_int(struct autobuf *abuf, const char* key, int value);
103 static void abuf_json_float(struct autobuf *abuf, const char* key, float value);
105 static void send_info(unsigned int /*send_what*/, int /*socket*/);
106 static void ipc_action(int, void *, unsigned int);
107 static void ipc_print_neighbors(struct autobuf *);
108 static void ipc_print_links(struct autobuf *);
109 static void ipc_print_routes(struct autobuf *);
110 static void ipc_print_topology(struct autobuf *);
111 static void ipc_print_hna(struct autobuf *);
112 static void ipc_print_mid(struct autobuf *);
113 static void ipc_print_gateways(struct autobuf *);
114 static void ipc_print_config(struct autobuf *);
115 static void ipc_print_interfaces(struct autobuf *);
117 #define TXT_IPC_BUFSIZE 256
119 #define SIW_NEIGHBORS 0x0001
120 #define SIW_LINKS 0x0002
121 #define SIW_ROUTES 0x0004
122 #define SIW_HNA 0x0008
123 #define SIW_MID 0x0010
124 #define SIW_TOPOLOGY 0x0020
125 #define SIW_GATEWAYS 0x0040
126 #define SIW_INTERFACES 0x0080
127 #define SIW_CONFIG 0x0100
129 /* ALL = neighbors links routes hna mid topology gateways interfaces */
130 #define SIW_ALL 0x00FF
132 #define MAX_CLIENTS 3
134 static char *outbuffer[MAX_CLIENTS];
135 static size_t outbuffer_size[MAX_CLIENTS];
136 static size_t outbuffer_written[MAX_CLIENTS];
137 static int outbuffer_socket[MAX_CLIENTS];
138 static int outbuffer_count;
140 static struct timer_entry *writetimer_entry;
143 /* JSON support functions */
146 /* JSON does not tolerate commas dangling at the end of arrays, so we need to
147 * count which entry number we're at in order to make sure we don't tack a
148 * dangling comma on at the end */
149 static int entrynumber = 0;
150 static int arrayentrynumber = 0;
153 abuf_json_open_array(struct autobuf *abuf, const char* header)
155 arrayentrynumber = 0;
156 abuf_appendf(abuf, "{\"%s\": [\n", header);
160 abuf_json_close_array(struct autobuf *abuf)
162 abuf_appendf(abuf, "]}\n");
166 abuf_json_open_array_entry(struct autobuf *abuf)
169 if (arrayentrynumber)
170 abuf_appendf(abuf, ",\n{");
172 abuf_appendf(abuf, "{");
177 abuf_json_close_array_entry(struct autobuf *abuf)
179 abuf_appendf(abuf, "}");
183 abuf_json_boolean(struct autobuf *abuf, const char* key, int value)
186 abuf_appendf(abuf, ",\n");
188 abuf_appendf(abuf, "\n");
189 abuf_appendf(abuf, "\t\"%s\": %s", key, value ? "true" : "false");
194 abuf_json_string(struct autobuf *abuf, const char* key, const char* value)
197 abuf_appendf(abuf, ",\n");
199 abuf_appendf(abuf, "\n");
200 abuf_appendf(abuf, "\t\"%s\": \"%s\"", key, value);
205 abuf_json_int(struct autobuf *abuf, const char* key, int value)
208 abuf_appendf(abuf, ",\n");
210 abuf_appendf(abuf, "\n");
211 abuf_appendf(abuf, "\t\"%s\": %i", key, value);
216 abuf_json_float(struct autobuf *abuf, const char* key, float value)
219 abuf_appendf(abuf, ",\n");
221 abuf_appendf(abuf, "\n");
222 abuf_appendf(abuf, "\t\"%s\": %.03f", key, value);
228 *Do initialization here
230 *This function is called by the my_init
231 *function in uolsrd_plugin.c
234 olsrd_plugin_init(void)
236 /* Initial IPC value */
244 * destructor - called at unload
247 olsr_plugin_exit(void)
249 if (ipc_socket != -1)
254 plugin_ipc_init(void)
256 union olsr_sockaddr sst;
260 /* Init ipc socket */
261 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
263 olsr_printf(1, "(JSONINFO) socket()=%s\n", strerror(errno));
267 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) {
269 olsr_printf(1, "(JSONINFO) setsockopt()=%s\n", strerror(errno));
273 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
274 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0) {
275 perror("SO_REUSEADDR failed");
279 /* Bind the socket */
281 /* complete the socket structure */
282 memset(&sst, 0, sizeof(sst));
283 if (olsr_cnf->ip_version == AF_INET) {
284 sst.in4.sin_family = AF_INET;
285 addrlen = sizeof(struct sockaddr_in);
287 sst.in4.sin_len = addrlen;
289 sst.in4.sin_addr.s_addr = jsoninfo_listen_ip.v4.s_addr;
290 sst.in4.sin_port = htons(ipc_port);
292 sst.in6.sin6_family = AF_INET6;
293 addrlen = sizeof(struct sockaddr_in6);
295 sst.in6.sin6_len = addrlen;
297 sst.in6.sin6_addr = jsoninfo_listen_ip.v6;
298 sst.in6.sin6_port = htons(ipc_port);
301 /* bind the socket to the port number */
302 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
304 olsr_printf(1, "(JSONINFO) bind()=%s\n", strerror(errno));
309 /* show that we are willing to listen */
310 if (listen(ipc_socket, 1) == -1) {
312 olsr_printf(1, "(JSONINFO) listen()=%s\n", strerror(errno));
317 /* Register with olsrd */
318 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
321 olsr_printf(2, "(JSONINFO) listening on port %d\n", ipc_port);
328 ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused)))
330 union olsr_sockaddr pin;
332 char addr[INET6_ADDRSTRLEN];
335 unsigned int send_what = 0;
338 socklen_t addrlen = sizeof(pin);
340 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
342 olsr_printf(1, "(JSONINFO) accept()=%s\n", strerror(errno));
347 tv.tv_sec = tv.tv_usec = 0;
348 if (olsr_cnf->ip_version == AF_INET) {
349 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
351 if (!ip4equal(&pin.in4.sin_addr, &jsoninfo_accept_ip.v4) && jsoninfo_accept_ip.v4.s_addr != INADDR_ANY) {
352 #ifdef JSONINFO_ALLOW_LOCALHOST
353 if (pin.in4.sin_addr.s_addr != INADDR_LOOPBACK) {
355 olsr_printf(1, "(JSONINFO) From host(%s) not allowed!\n", addr);
356 close(ipc_connection);
358 #ifdef JSONINFO_ALLOW_LOCALHOST
363 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
365 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
366 if (!ip6equal(&in6addr_any, &jsoninfo_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &jsoninfo_accept_ip.v6)) {
367 olsr_printf(1, "(JSONINFO) From host(%s) not allowed!\n", addr);
368 close(ipc_connection);
374 olsr_printf(2, "(JSONINFO) Connect from %s\n", addr);
377 /* purge read buffer to prevent blocking on linux */
379 FD_SET((unsigned int)ipc_connection, &rfds); /* Win32 needs the cast here */
380 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
382 ssize_t s = recv(ipc_connection, (void *)&requ, sizeof(requ), 0); /* Win32 needs the cast here */
385 /* print out the requested tables */
386 if (0 != strstr(requ, "/status")) send_what = SIW_ALL;
387 else { /* included in /status */
388 if (0 != strstr(requ, "/neighbors")) send_what |= SIW_NEIGHBORS;
389 if (0 != strstr(requ, "/links")) send_what |= SIW_LINKS;
390 if (0 != strstr(requ, "/routes")) send_what |= SIW_ROUTES;
391 if (0 != strstr(requ, "/hna")) send_what |= SIW_HNA;
392 if (0 != strstr(requ, "/mid")) send_what |= SIW_MID;
393 if (0 != strstr(requ, "/topology")) send_what |= SIW_TOPOLOGY;
394 if (0 != strstr(requ, "/gateways")) send_what |= SIW_GATEWAYS;
395 if (0 != strstr(requ, "/interfaces")) send_what |= SIW_INTERFACES;
397 if (0 != strstr(requ, "/config")) send_what |= SIW_CONFIG;
399 if ( send_what == 0 ) send_what = SIW_ALL;
402 send_info(send_what, ipc_connection);
406 ipc_print_neighbors(struct autobuf *abuf)
408 struct ipaddr_str buf1;
409 struct neighbor_entry *neigh;
410 struct neighbor_2_list_entry *list_2;
413 abuf_json_open_array(abuf, "neighbors");
416 OLSR_FOR_ALL_NBR_ENTRIES(neigh) {
417 abuf_json_open_array_entry(abuf);
418 abuf_json_string(abuf, "ipv4Address",
419 olsr_ip_to_string(&buf1, &neigh->neighbor_main_addr));
420 abuf_json_boolean(abuf, "symmetric", (neigh->status == SYM));
421 abuf_json_boolean(abuf, "multiPointRelay", neigh->is_mpr);
422 abuf_json_boolean(abuf, "multiPointRelaySelector",
423 olsr_lookup_mprs_set(&neigh->neighbor_main_addr) != NULL);
424 abuf_json_int(abuf, "willingness", neigh->willingness);
425 abuf_appendf(abuf, ",\n");
428 if (neigh->neighbor_2_list.next) {
429 abuf_appendf(abuf, "\t\"twoHopNeighbors\": [");
430 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
432 abuf_appendf(abuf, ", ");
435 olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
439 abuf_appendf(abuf, "]");
440 abuf_json_int(abuf, "twoHopNeighborCount", thop_cnt);
441 abuf_json_close_array_entry(abuf);
443 OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
444 abuf_json_close_array(abuf);
448 ipc_print_links(struct autobuf *abuf)
450 struct ipaddr_str buf1, buf2;
451 struct lqtextbuffer lqbuffer1, lqbuffer2;
453 struct link_entry *my_link = NULL;
455 abuf_json_open_array(abuf, "links");
456 OLSR_FOR_ALL_LINK_ENTRIES(my_link) {
458 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
460 abuf_json_open_array_entry(abuf);
461 abuf_json_string(abuf, "localIP",
462 olsr_ip_to_string(&buf1, &my_link->local_iface_addr));
463 abuf_json_string(abuf, "remoteIP",
464 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr));
465 abuf_json_int(abuf, "msValid", diff);
466 lqs = get_link_entry_text(my_link, '\t', &lqbuffer1);
467 abuf_json_float(abuf, "linkQuality", atof(lqs));
468 abuf_json_float(abuf, "neighborLinkQuality", atof(strrchr(lqs, '\t')));
469 abuf_json_float(abuf, "linkCost",
470 atof(get_linkcost_text(my_link->linkcost, false, &lqbuffer2)));
471 abuf_json_close_array_entry(abuf);
473 OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
474 abuf_json_close_array(abuf);
478 ipc_print_routes(struct autobuf *abuf)
480 struct ipaddr_str buf1, buf2;
482 struct lqtextbuffer lqbuffer;
484 //abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
485 abuf_json_open_array(abuf, "routes");
487 /* Walk the route table */
488 OLSR_FOR_ALL_RT_ENTRIES(rt) {
489 abuf_json_open_array_entry(abuf);
490 abuf_json_string(abuf, "destination",
491 olsr_ip_to_string(&buf1, &rt->rt_dst.prefix));
492 abuf_json_int(abuf, "genmask", rt->rt_dst.prefix_len);
493 abuf_json_string(abuf, "gateway",
494 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway));
495 abuf_json_int(abuf, "metric", rt->rt_best->rtp_metric.hops);
496 abuf_json_float(abuf, "expectedTransmissionCount",
497 atof(get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer)));
498 abuf_json_string(abuf, "interface",
499 if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
500 abuf_json_close_array_entry(abuf);
502 OLSR_FOR_ALL_RT_ENTRIES_END(rt);
504 abuf_json_close_array(abuf);
508 ipc_print_topology(struct autobuf *abuf)
512 abuf_json_open_array(abuf, "topology");
513 //abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
516 OLSR_FOR_ALL_TC_ENTRIES(tc) {
517 struct tc_edge_entry *tc_edge;
518 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge) {
519 if (tc_edge->edge_inv) {
520 struct ipaddr_str dstbuf, addrbuf;
521 struct lqtextbuffer lqbuffer1, lqbuffer2;
522 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
523 int diff = (int)(vt);
525 abuf_json_open_array_entry(abuf);
526 abuf_json_string(abuf, "destinationIP",
527 olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr));
528 abuf_json_string(abuf, "lastHopIP",
529 olsr_ip_to_string(&addrbuf, &tc->addr));
530 lqs = get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1);
531 abuf_json_float(abuf, "linkQuality", atof(lqs));
532 abuf_json_float(abuf, "neighborLinkQuality", atof(strrchr(lqs, '\t')));
533 abuf_json_float(abuf, "cost",
534 atof(get_linkcost_text(tc_edge->cost, false, &lqbuffer2)));
535 abuf_json_int(abuf, "msValid", diff);
536 abuf_json_close_array_entry(abuf);
539 OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
541 OLSR_FOR_ALL_TC_ENTRIES_END(tc);
543 abuf_json_close_array(abuf);
547 ipc_print_hna(struct autobuf *abuf)
549 struct ip_prefix_list *hna;
550 struct hna_entry *tmp_hna;
551 struct hna_net *tmp_net;
552 struct ipaddr_str buf, mainaddrbuf;
554 abuf_json_open_array(abuf, "hna");
556 /* Announced HNA entries */
557 if (olsr_cnf->ip_version == AF_INET) {
558 for (hna = olsr_cnf->hna_entries; hna != NULL; hna = hna->next) {
559 abuf_json_open_array_entry(abuf);
560 abuf_json_string(abuf, "destination",
561 olsr_ip_to_string(&buf, &hna->net.prefix));
562 abuf_json_int(abuf, "genmask", hna->net.prefix_len);
563 abuf_json_string(abuf, "gateway",
564 olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
565 abuf_json_close_array_entry(abuf);
568 for (hna = olsr_cnf->hna_entries; hna != NULL; hna = hna->next) {
569 abuf_json_open_array_entry(abuf);
570 abuf_json_string(abuf, "destination",
571 olsr_ip_to_string(&buf, &hna->net.prefix));
572 abuf_json_int(abuf, "genmask", hna->net.prefix_len);
573 abuf_json_string(abuf, "gateway",
574 olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
575 abuf_json_close_array_entry(abuf);
580 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna) {
582 /* Check all networks */
583 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
584 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
585 int diff = (int)(vt);
586 abuf_json_open_array_entry(abuf);
587 abuf_json_string(abuf, "destination",
588 olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix)),
589 abuf_json_int(abuf, "genmask", tmp_net->hna_prefix.prefix_len);
590 abuf_json_string(abuf, "gateway",
591 olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
592 abuf_json_int(abuf, "msValid", diff);
593 abuf_json_close_array_entry(abuf);
596 OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
598 abuf_json_close_array(abuf);
602 ipc_print_mid(struct autobuf *abuf)
605 struct mid_entry *entry;
606 struct mid_address *alias;
608 abuf_json_open_array(abuf, "mid");
611 for (idx = 0; idx < HASHSIZE; idx++) {
612 entry = mid_set[idx].next;
614 while (entry != &mid_set[idx]) {
615 struct ipaddr_str buf, buf2;
616 alias = entry->aliases;
619 uint32_t vt = alias->vtime - now_times;
620 int diff = (int)(vt);
622 abuf_json_open_array_entry(abuf);
623 abuf_json_string(abuf, "ipv4Address",
624 olsr_ip_to_string(&buf, &entry->main_addr));
625 abuf_json_string(abuf, "alias",
626 olsr_ip_to_string(&buf2, &alias->alias));
627 abuf_json_open_array_entry(abuf);
628 abuf_json_int(abuf, "msValid", diff);
629 abuf_json_close_array_entry(abuf);
630 alias = alias->next_alias;
635 abuf_json_close_array(abuf);
639 ipc_print_gateways(struct autobuf *abuf)
642 abuf_json_string(abuf, "error", "Gateway mode is only supported in Linux");
644 static const char IPV4[] = "ipv4";
645 static const char IPV4_NAT[] = "ipv4(n)";
646 static const char IPV6[] = "ipv6";
647 static const char NONE[] = "-";
649 struct ipaddr_str buf;
650 struct gateway_entry *gw;
651 struct lqtextbuffer lqbuf;
653 // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
654 abuf_json_open_array(abuf, "gateways");
655 //abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
656 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw) {
657 char v4 = '-', v6 = '-';
658 bool autoV4 = false, autoV6 = false;
659 const char *v4type = NONE, *v6type = NONE;
662 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
666 if (gw == olsr_get_ipv4_inet_gateway(&autoV4)) {
667 v4 = autoV4 ? 'a' : 's';
668 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit)
669 && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
673 if (gw == olsr_get_ipv6_inet_gateway(&autoV6)) {
674 v6 = autoV6 ? 'a' : 's';
675 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
680 v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
686 abuf_appendf(abuf, "%c%c\t%s\t%s\t%d\t%u\t%u\t%s\t%s\t%s\n",
687 v4, v6, olsr_ip_to_string(&buf, &gw->originator),
688 get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops,
689 gw->uplink, gw->downlink, v4type, v6type,
690 gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
692 OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
693 abuf_json_close_array(abuf);
698 ipc_print_config(struct autobuf *abuf)
700 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
704 ipc_print_interfaces(struct autobuf *abuf)
706 const struct olsr_if *ifs;
707 abuf_json_open_array(abuf, "interfaces");
708 //abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
709 for (ifs = olsr_cnf->interfaces; ifs != NULL; ifs = ifs->next) {
710 const struct interface *const rifs = ifs->interf;
711 abuf_json_open_array_entry(abuf);
712 abuf_json_string(abuf, "name", ifs->name);
714 abuf_json_string(abuf, "state", "down");
716 abuf_json_string(abuf, "state", "up");
717 abuf_json_int(abuf, "mtu", rifs->int_mtu);
718 abuf_json_boolean(abuf, "wireless", rifs->is_wireless);
720 if (olsr_cnf->ip_version == AF_INET) {
721 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
722 abuf_json_string(abuf, "ipv4Address",
723 ip4_to_string(&addrbuf, rifs->int_addr.sin_addr));
724 abuf_json_string(abuf, "netmask",
725 ip4_to_string(&maskbuf, rifs->int_netmask.sin_addr));
726 abuf_json_string(abuf, "broadcast",
727 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
729 struct ipaddr_str addrbuf, maskbuf;
730 abuf_json_string(abuf, "ipv6Address",
731 ip6_to_string(&addrbuf, &rifs->int6_addr.sin6_addr));
732 abuf_json_string(abuf, "multicast",
733 ip6_to_string(&maskbuf, &rifs->int6_multaddr.sin6_addr));
736 abuf_json_close_array_entry(abuf);
738 abuf_json_close_array(abuf);
743 jsoninfo_write_data(void *foo __attribute__ ((unused)))
746 int result, i, j, max;
751 for (i=0; i<outbuffer_count; i++) {
752 /* And we cast here since we get a warning on Win32 */
753 FD_SET((unsigned int)(outbuffer_socket[i]), &set);
755 if (outbuffer_socket[i] > max) {
756 max = outbuffer_socket[i];
763 result = select(max + 1, NULL, &set, NULL, &tv);
768 for (i=0; i<outbuffer_count; i++) {
769 if (FD_ISSET(outbuffer_socket[i], &set)) {
770 result = send(outbuffer_socket[i],
771 outbuffer[i] + outbuffer_written[i],
772 outbuffer_size[i] - outbuffer_written[i],
775 outbuffer_written[i] += result;
778 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
779 /* close this socket and cleanup*/
780 close(outbuffer_socket[i]);
783 for (j=i+1; j<outbuffer_count; j++) {
784 outbuffer[j-1] = outbuffer[j];
785 outbuffer_size[j-1] = outbuffer_size[j];
786 outbuffer_socket[j-1] = outbuffer_socket[j];
787 outbuffer_written[j-1] = outbuffer_written[j];
793 if (outbuffer_count == 0) {
794 olsr_stop_timer(writetimer_entry);
799 send_info(unsigned int send_what, int the_socket)
803 abuf_init(&abuf, 4096);
805 /* if sending all, then wrap in a JSON array */
806 if (send_what == SIW_ALL)
807 abuf_appendf(&abuf, "[\n");
809 /* Print tables to IPC socket */
811 if ((send_what & SIW_LINKS) == SIW_LINKS) {
812 ipc_print_links(&abuf);
813 if (send_what == SIW_ALL) abuf_appendf(&abuf, ",");
815 if ((send_what & SIW_NEIGHBORS) == SIW_NEIGHBORS) {
816 ipc_print_neighbors(&abuf);
817 if (send_what == SIW_ALL) abuf_appendf(&abuf, ",");
819 if ((send_what & SIW_TOPOLOGY) == SIW_TOPOLOGY) {
820 ipc_print_topology(&abuf);
821 if (send_what == SIW_ALL) abuf_appendf(&abuf, ",");
823 if ((send_what & SIW_HNA) == SIW_HNA) {
824 ipc_print_hna(&abuf);
825 if (send_what == SIW_ALL) abuf_appendf(&abuf, ",");
827 if ((send_what & SIW_MID) == SIW_MID) {
828 ipc_print_mid(&abuf);
829 if (send_what == SIW_ALL) abuf_appendf(&abuf, ",");
831 if ((send_what & SIW_ROUTES) == SIW_ROUTES) {
832 ipc_print_routes(&abuf);
833 if (send_what == SIW_ALL) abuf_appendf(&abuf, ",");
835 if ((send_what & SIW_GATEWAYS) == SIW_GATEWAYS) {
836 ipc_print_gateways(&abuf);
837 if (send_what == SIW_ALL) abuf_appendf(&abuf, ",");
839 if ((send_what & SIW_CONFIG) == SIW_CONFIG) {
840 ipc_print_config(&abuf);
841 if (send_what == SIW_ALL) abuf_appendf(&abuf, ",");
843 if ((send_what & SIW_INTERFACES) == SIW_INTERFACES) {
844 ipc_print_interfaces(&abuf);
847 /* end of JSON array for status */
848 if (send_what == SIW_ALL)
849 abuf_appendf(&abuf, "]\n");
851 outbuffer[outbuffer_count] = olsr_malloc(abuf.len, "txt output buffer");
852 outbuffer_size[outbuffer_count] = abuf.len;
853 outbuffer_written[outbuffer_count] = 0;
854 outbuffer_socket[outbuffer_count] = the_socket;
856 memcpy(outbuffer[outbuffer_count], abuf.buf, abuf.len);
859 if (outbuffer_count == 1) {
860 writetimer_entry = olsr_start_timer(100,
863 &jsoninfo_write_data,
876 * indent-tabs-mode: nil