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
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
21 * * Neither the name of olsr.org, olsrd nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
38 * Visit http://www.olsr.org for more information.
40 * If you find this software useful feel free to make a donation
41 * to the project. For more information see the website or contact
42 * the copyright holders.
47 * Dynamic linked library for the olsr.org olsr daemon
50 #include <sys/types.h>
51 #include <sys/socket.h>
53 #include <sys/select.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
69 #include "builddata.h"
70 #include "olsr_types.h"
71 #include "neighbor_table.h"
72 #include "two_hop_neighbor_table.h"
73 #include "mpr_selector_set.h"
79 #include "lq_plugin.h"
80 #include "common/autobuf.h"
83 #include "olsrd_txtinfo.h"
84 #include "olsrd_plugin.h"
87 #define close(x) closesocket(x)
90 /* defines to make txtinfo and jsoninfo look alike */
91 #define PLUGIN_NAME "TXTINFO"
92 #define info_accept_ip txtinfo_accept_ip
93 #define info_listen_ip txtinfo_listen_ip
94 #define info_ipv6_only txtinfo_ipv6_only
95 #ifdef TXTINFO_ALLOW_LOCALHOST
96 #define INFO_ALLOW_LOCALHOST TXTINFO_ALLOW_LOCALHOST
99 static int ipc_socket;
101 /* IPC initialization function */
102 static int plugin_ipc_init(void);
104 static void send_info(unsigned int /*send_what*/, int /*socket*/);
106 static void ipc_action(int, void *, unsigned int);
108 static void ipc_print_neighbors(struct autobuf *, bool);
110 static void ipc_print_links(struct autobuf *);
112 static void ipc_print_routes(struct autobuf *);
114 static void ipc_print_topology(struct autobuf *);
116 static void ipc_print_hna(struct autobuf *);
118 static void ipc_print_mid(struct autobuf *);
120 static void ipc_print_gateways(struct autobuf *);
122 static void ipc_print_olsrd_conf(struct autobuf *);
124 static void ipc_print_interfaces(struct autobuf *);
126 static void ipc_print_sgw(struct autobuf *);
128 #define TXT_IPC_BUFSIZE 256
130 /* these provide all of the runtime status info */
131 #define SIW_NEIGHBORS 0x0001
132 #define SIW_LINKS 0x0002
133 #define SIW_ROUTES 0x0004
134 #define SIW_HNA 0x0008
135 #define SIW_MID 0x0010
136 #define SIW_TOPOLOGY 0x0020
137 #define SIW_GATEWAYS 0x0040
138 #define SIW_INTERFACES 0x0080
139 #define SIW_2HOP 0x0100
140 #define SIW_SGW 0x0200
141 #define SIW_RUNTIME_ALL (SIW_NEIGHBORS | SIW_LINKS | SIW_ROUTES | SIW_HNA | SIW_MID | SIW_TOPOLOGY)
143 /* these only change at olsrd startup */
144 #define SIW_VERSION 0x0400
145 #define SIW_STARTUP_ALL (SIW_VERSION)
147 /* this is everything in normal format */
148 #define SIW_ALL (SIW_RUNTIME_ALL | SIW_STARTUP_ALL)
150 /* this data is not normal format but olsrd.conf format */
151 #define SIW_OLSRD_CONF 0x2000
153 #define MAX_CLIENTS 3
155 static char *outbuffer[MAX_CLIENTS];
156 static size_t outbuffer_size[MAX_CLIENTS];
157 static size_t outbuffer_written[MAX_CLIENTS];
158 static int outbuffer_socket[MAX_CLIENTS];
159 static int outbuffer_count = 0;
161 static struct timer_entry *writetimer_entry;
164 *Do initialization here
166 *This function is called by the my_init
167 *function in uolsrd_plugin.c
169 int olsrd_plugin_init(void) {
170 /* Initial IPC value */
178 * destructor - called at unload
180 void olsr_plugin_exit(void) {
181 if (ipc_socket != -1)
185 static int plugin_ipc_init(void) {
186 union olsr_sockaddr sst;
190 /* Init ipc socket */
191 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
193 olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
197 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
199 olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
203 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
204 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0) {
205 perror("SO_REUSEADDR failed");
208 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
209 #if defined linux && defined IPV6_V6ONLY
210 if (info_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
211 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
212 perror("IPV6_V6ONLY failed");
216 #endif /* defined linux && defined IPV6_V6ONLY */
217 /* Bind the socket */
219 /* complete the socket structure */
220 memset(&sst, 0, sizeof(sst));
221 if (olsr_cnf->ip_version == AF_INET) {
222 sst.in4.sin_family = AF_INET;
223 addrlen = sizeof(struct sockaddr_in);
225 sst.in4.sin_len = addrlen;
226 #endif /* SIN6_LEN */
227 sst.in4.sin_addr.s_addr = info_listen_ip.v4.s_addr;
228 sst.in4.sin_port = htons(ipc_port);
230 sst.in6.sin6_family = AF_INET6;
231 addrlen = sizeof(struct sockaddr_in6);
233 sst.in6.sin6_len = addrlen;
234 #endif /* SIN6_LEN */
235 sst.in6.sin6_addr = info_listen_ip.v6;
236 sst.in6.sin6_port = htons(ipc_port);
239 /* bind the socket to the port number */
240 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
242 olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
247 /* show that we are willing to listen */
248 if (listen(ipc_socket, 1) == -1) {
250 olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
255 /* Register with olsrd */
256 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
259 olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
265 static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
266 union olsr_sockaddr pin;
268 char addr[INET6_ADDRSTRLEN];
271 unsigned int send_what = 0;
274 socklen_t addrlen = sizeof(pin);
276 if (outbuffer_count >= MAX_CLIENTS) {
280 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
282 olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
287 tv.tv_sec = tv.tv_usec = 0;
288 if (olsr_cnf->ip_version == AF_INET) {
289 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
291 if (!ip4equal(&pin.in4.sin_addr, &info_accept_ip.v4) && info_accept_ip.v4.s_addr != INADDR_ANY) {
292 #ifdef INFO_ALLOW_LOCALHOST
293 if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
294 #endif /* INFO_ALLOW_LOCALHOST */
295 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
296 close(ipc_connection);
298 #ifdef INFO_ALLOW_LOCALHOST
300 #endif /* INFO_ALLOW_LOCALHOST */
303 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
305 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
306 if (!ip6equal(&in6addr_any, &info_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &info_accept_ip.v6)) {
307 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
308 close(ipc_connection);
314 olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
317 /* purge read buffer to prevent blocking on linux */
319 FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
320 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
322 ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
324 if (s == sizeof(requ) - 1) {
325 /* input was much too long, just skip the rest */
328 while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
334 /* To print out neighbours only on the Freifunk Status
335 * page the normal output is somewhat lengthy. The
336 * header parsing is sufficient for standard wget.
338 if (strstr(requ, "/neighbours"))
339 send_what = SIW_NEIGHBORS | SIW_LINKS;
341 /* print out every combinations of requested tabled
342 * 3++ letter abbreviations are matched */
343 if (strstr(requ, "/all"))
344 send_what = SIW_RUNTIME_ALL;
345 else { /*already included in /all*/
346 if (strstr(requ, "/nei"))
347 send_what |= SIW_NEIGHBORS;
348 if (strstr(requ, "/lin"))
349 send_what |= SIW_LINKS;
350 if (strstr(requ, "/rou"))
351 send_what |= SIW_ROUTES;
352 if (strstr(requ, "/hna"))
353 send_what |= SIW_HNA;
354 if (strstr(requ, "/mid"))
355 send_what |= SIW_MID;
356 if (strstr(requ, "/top"))
357 send_what |= SIW_TOPOLOGY;
359 if (strstr(requ, "/gat"))
360 send_what |= SIW_GATEWAYS;
361 if (strstr(requ, "/con"))
362 send_what |= SIW_OLSRD_CONF;
363 if (strstr(requ, "/int"))
364 send_what |= SIW_INTERFACES;
365 if (strstr(requ, "/2ho"))
366 send_what |= SIW_2HOP;
367 if (strstr(requ, "/ver"))
368 send_what |= SIW_VERSION;
369 if (strstr(requ, "/sgw"))
370 send_what |= SIW_SGW;
378 send_info(send_what, ipc_connection);
381 static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
382 struct ipaddr_str buf1;
383 struct neighbor_entry *neigh;
384 struct neighbor_2_list_entry *list_2;
387 abuf_puts(abuf, "Table: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWill.");
389 abuf_puts(abuf, "\n\t2hop interface adrress\n");
391 abuf_puts(abuf, "\t2 Hop Neighbors\n");
394 OLSR_FOR_ALL_NBR_ENTRIES(neigh)
396 abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d\t", olsr_ip_to_string(&buf1, &neigh->neighbor_main_addr), (neigh->status == SYM) ? "YES" : "NO",
397 neigh->is_mpr ? "YES" : "NO", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO", neigh->willingness);
400 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
402 abuf_appendf(abuf, "\t%s\n", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
407 abuf_appendf(abuf, "%d\n", thop_cnt);
409 }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
410 abuf_puts(abuf, "\n");
413 static void ipc_print_links(struct autobuf *abuf) {
414 struct ipaddr_str buf1, buf2;
415 struct lqtextbuffer lqbuffer1, lqbuffer2;
417 struct link_entry *my_link = NULL;
419 #ifdef ACTIVATE_VTIME_TXTINFO
420 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tVTime\tLQ\tNLQ\tCost\n");
421 #else /* ACTIVATE_VTIME_TXTINFO */
422 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tHyst.\tLQ\tNLQ\tCost\n");
423 #endif /* ACTIVATE_VTIME_TXTINFO */
426 OLSR_FOR_ALL_LINK_ENTRIES(my_link)
428 #ifdef ACTIVATE_VTIME_TXTINFO
429 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
431 abuf_appendf(abuf, "%s\t%s\t%d.%03d\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
432 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
433 diff/1000, abs(diff%1000),
434 get_link_entry_text(my_link, '\t', &lqbuffer1),
435 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
436 #else /* ACTIVATE_VTIME_TXTINFO */
437 abuf_appendf(abuf, "%s\t%s\t0.00\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
438 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr), get_link_entry_text(my_link, '\t', &lqbuffer1),
439 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
440 #endif /* ACTIVATE_VTIME_TXTINFO */
441 }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
443 abuf_puts(abuf, "\n");
446 static void ipc_print_routes(struct autobuf *abuf) {
447 struct ipaddr_str buf1, buf2;
449 struct lqtextbuffer lqbuffer;
451 abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
453 /* Walk the route table */
454 OLSR_FOR_ALL_RT_ENTRIES(rt)
456 abuf_appendf(abuf, "%s/%d\t%s\t%d\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &rt->rt_dst.prefix), rt->rt_dst.prefix_len,
457 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway), rt->rt_best->rtp_metric.hops,
458 get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer), if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
459 }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
461 abuf_puts(abuf, "\n");
465 static void ipc_print_topology(struct autobuf *abuf) {
468 #ifdef ACTIVATE_VTIME_TXTINFO
469 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
470 #else /* ACTIVATE_VTIME_TXTINFO */
471 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\n");
472 #endif /* ACTIVATE_VTIME_TXTINFO */
475 OLSR_FOR_ALL_TC_ENTRIES(tc)
477 struct tc_edge_entry *tc_edge;
478 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
480 if (tc_edge->edge_inv) {
481 struct ipaddr_str dstbuf, addrbuf;
482 struct lqtextbuffer lqbuffer1, lqbuffer2;
483 #ifdef ACTIVATE_VTIME_TXTINFO
484 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
485 int diff = (int)(vt);
486 abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d.%03d\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr),
487 olsr_ip_to_string(&addrbuf, &tc->addr),
488 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1),
489 get_linkcost_text(tc_edge->cost, false, &lqbuffer2),
490 diff/1000, diff%1000);
491 #else /* ACTIVATE_VTIME_TXTINFO */
492 abuf_appendf(abuf, "%s\t%s\t%s\t%s\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr), olsr_ip_to_string(&addrbuf, &tc->addr),
493 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1), get_linkcost_text(tc_edge->cost, false, &lqbuffer2));
494 #endif /* ACTIVATE_VTIME_TXTINFO */
496 }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
497 }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
499 abuf_puts(abuf, "\n");
502 static void ipc_print_hna(struct autobuf *abuf) {
503 struct ip_prefix_list *hna;
504 struct hna_entry *tmp_hna;
505 struct hna_net *tmp_net;
506 struct ipaddr_str buf, mainaddrbuf;
508 #ifdef ACTIVATE_VTIME_TXTINFO
509 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\tVTime\n");
510 #else /* ACTIVATE_VTIME_TXTINFO */
511 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\n");
512 #endif /* ACTIVATE_VTIME_TXTINFO */
514 /* Announced HNA entries */
515 for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
516 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &hna->net.prefix), hna->net.prefix_len, olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
520 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
523 /* Check all networks */
524 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
525 #ifdef ACTIVATE_VTIME_TXTINFO
526 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
527 int diff = (int)(vt);
528 abuf_appendf(abuf, "%s/%d\t%s\t\%d.%03d\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
529 tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr),
530 diff/1000, abs(diff%1000));
531 #else /* ACTIVATE_VTIME_TXTINFO */
532 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix), tmp_net->hna_prefix.prefix_len,
533 olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
534 #endif /* ACTIVATE_VTIME_TXTINFO */
536 }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
538 abuf_puts(abuf, "\n");
541 static void ipc_print_mid(struct autobuf *abuf) {
543 unsigned short is_first;
544 struct mid_entry *entry;
545 struct mid_address *alias;
546 #ifdef ACTIVATE_VTIME_TXTINFO
547 abuf_puts(abuf, "Table: MID\nIP address\tAlias\tVTime\n");
548 #else /* ACTIVATE_VTIME_TXTINFO */
549 abuf_puts(abuf, "Table: MID\nIP address\tAliases\n");
550 #endif /* ACTIVATE_VTIME_TXTINFO */
553 for (idx = 0; idx < HASHSIZE; idx++) {
554 entry = mid_set[idx].next;
556 while (entry != &mid_set[idx]) {
557 #ifdef ACTIVATE_VTIME_TXTINFO
558 struct ipaddr_str buf, buf2;
559 #else /* ACTIVATE_VTIME_TXTINFO */
560 struct ipaddr_str buf;
561 abuf_puts(abuf, olsr_ip_to_string(&buf, &entry->main_addr));
562 #endif /* ACTIVATE_VTIME_TXTINFO */
563 alias = entry->aliases;
567 #ifdef ACTIVATE_VTIME_TXTINFO
568 uint32_t vt = alias->vtime - now_times;
569 int diff = (int)(vt);
571 abuf_appendf(abuf, "%s\t%s\t%d.%03d\n",
572 olsr_ip_to_string(&buf, &entry->main_addr),
573 olsr_ip_to_string(&buf2, &alias->alias),
574 diff/1000, abs(diff%1000));
575 #else /* ACTIVATE_VTIME_TXTINFO */
576 abuf_appendf(abuf, "%s%s", (is_first ? "\t" : ";"), olsr_ip_to_string(&buf, &alias->alias));
577 #endif /* ACTIVATE_VTIME_TXTINFO */
578 alias = alias->next_alias;
582 #ifndef ACTIVATE_VTIME_TXTINFO
583 abuf_puts(abuf, "\n");
584 #endif /* ACTIVATE_VTIME_TXTINFO */
587 abuf_puts(abuf, "\n");
590 static void ipc_print_gateways(struct autobuf *abuf) {
592 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
593 #else /* __linux__ */
594 static const char IPV4[] = "ipv4";
595 static const char IPV4_NAT[] = "ipv4(n)";
596 static const char IPV6[] = "ipv6";
597 static const char NONE[] = "-";
599 struct ipaddr_str buf;
600 struct gateway_entry *gw;
601 struct lqtextbuffer lqbuf;
603 // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
604 abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
605 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
607 char v4 = '-', v6 = '-';
608 const char *v4type = NONE, *v6type = NONE;
611 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
615 if (gw == olsr_get_inet_gateway(false)) {
617 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
621 if (gw == olsr_get_inet_gateway(true)) {
623 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
628 v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
634 abuf_appendf(abuf, "%c%c\t%s\t%s\t%d\t%u\t%u\t%s\t%s\t%s\n", v4, v6, olsr_ip_to_string(&buf, &gw->originator),
635 get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops, gw->uplink, gw->downlink, v4type, v6type,
636 gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
637 }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
638 #endif /* __linux__ */
643 /** interface names for smart gateway tunnel interfaces, IPv4 */
644 extern struct interfaceName * sgwTunnel4InterfaceNames;
646 /** interface names for smart gateway tunnel interfaces, IPv6 */
647 extern struct interfaceName * sgwTunnel6InterfaceNames;
650 * Construct the sgw table for a given ip version
652 * @param abuf the string buffer
653 * @param ipv6 true for IPv6, false for IPv4
654 * @param fmtv the format for printing
656 static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const char * fmtv) {
657 struct interfaceName * sgwTunnelInterfaceNames;
659 abuf_appendf(abuf, "# Table: Smart Gateway IPv%s\n", ipv6 ? "6" : "4");
660 abuf_appendf(abuf, fmth, "#", "Originator", "Prefix", "Uplink", "Downlink", "PathCost", "IPv4", "IPv4-NAT", "IPv6", "Tunnel-Name", "Destination", "Cost");
662 sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
663 if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
664 struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
666 for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
667 struct interfaceName * node = &sgwTunnelInterfaceNames[i];
668 struct gateway_entry * gw = node->gw;
675 struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
677 struct ipaddr_str originatorStr;
678 const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
679 struct ipaddr_str prefixIpStr;
680 const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
681 union olsr_ip_addr netmask = { { 0 } };
682 struct ipaddr_str prefixMaskStr;
683 const char * prefixMASKStr;
684 char prefixAndMask[strlen(prefix) + 1 + INET_ADDRSTRLEN + 1];
687 prefix_to_netmask((uint8_t *) &netmask, sizeof(netmask.v4), gw->external_prefix.prefix_len);
688 prefixMASKStr = olsr_ip_to_string(&prefixMaskStr, &netmask);
689 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%s", prefix, prefixMASKStr);
691 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%d", prefix, gw->external_prefix.prefix_len);
694 abuf_appendf(abuf, fmtv, //
695 (current_gw && (current_gw == gw)) ? "*" : " ", // selected
696 originator, // Originator
697 prefixAndMask, // 4: Prefix IP / Prefix Mask, 6: Prefix IP / Prefix Length
698 gw->uplink, // Uplink
699 gw->downlink, // Downlink
700 !tc ? ROUTE_COST_BROKEN : tc->path_cost, // PathCost
701 gw->ipv4 ? "Y" : "N", // IPv4
702 gw->ipv4nat ? "Y" : "N", // IPv4-NAT
703 gw->ipv6 ? "Y" : "N", // IPv6
704 node->name, // Tunnel-Name
705 originator, // Destination
706 gw->path_cost // Cost
712 #endif /* __linux__ */
714 static void ipc_print_sgw(struct autobuf *abuf) {
716 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
719 static const char * fmth4 = "%s%-15s %-31s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-15s %s\n";
720 static const char * fmtv4 = "%s%-15s %-31s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-15s %lld\n";
722 static const char * fmth6 = "%s%-45s %-49s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-45s %s\n";
723 static const char * fmtv6 = "%s%-45s %-49s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-45s %lld\n";
726 sgw_ipvx(abuf, false, fmth4, fmtv4);
727 abuf_puts(abuf, "\n");
729 sgw_ipvx(abuf, true, fmth6, fmtv6);
730 abuf_puts(abuf, "\n");
732 #endif /* __linux__ */
735 static void ipc_print_version(struct autobuf *abuf) {
736 abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
739 static void ipc_print_olsrd_conf(struct autobuf *abuf) {
740 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
743 static void ipc_print_interfaces(struct autobuf *abuf) {
744 const struct olsr_if *ifs;
745 abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
746 for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
747 const struct interface_olsr * const rifs = ifs->interf;
748 abuf_appendf(abuf, "%s\t", ifs->name);
750 abuf_puts(abuf, "DOWN\n");
753 abuf_appendf(abuf, "UP\t%d\t%s\t", rifs->int_mtu, rifs->is_wireless ? "Yes" : "No");
755 if (olsr_cnf->ip_version == AF_INET) {
756 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
757 abuf_appendf(abuf, "%s\t%s\t%s\n", ip4_to_string(&addrbuf, rifs->int_addr.sin_addr), ip4_to_string(&maskbuf, rifs->int_netmask.sin_addr),
758 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
760 struct ipaddr_str addrbuf, maskbuf;
761 abuf_appendf(abuf, "%s\t\t%s\n", ip6_to_string(&addrbuf, &rifs->int6_addr.sin6_addr), ip6_to_string(&maskbuf, &rifs->int6_multaddr.sin6_addr));
764 abuf_puts(abuf, "\n");
767 static void info_write_data(void *foo __attribute__ ((unused))) {
769 int result, i, j, max;
774 for (i = 0; i < outbuffer_count; i++) {
775 /* And we cast here since we get a warning on Win32 */
776 FD_SET((unsigned int )(outbuffer_socket[i]), &set);
778 if (outbuffer_socket[i] > max) {
779 max = outbuffer_socket[i];
786 result = select(max + 1, NULL, &set, NULL, &tv);
791 for (i = 0; i < outbuffer_count; i++) {
792 if (FD_ISSET(outbuffer_socket[i], &set)) {
793 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
795 outbuffer_written[i] += result;
798 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
799 /* close this socket and cleanup*/
800 close(outbuffer_socket[i]);
803 for (j = i + 1; j < outbuffer_count; j++) {
804 outbuffer[j - 1] = outbuffer[j];
805 outbuffer_size[j - 1] = outbuffer_size[j];
806 outbuffer_socket[j - 1] = outbuffer_socket[j];
807 outbuffer_written[j - 1] = outbuffer_written[j];
813 if (!outbuffer_count) {
814 olsr_stop_timer(writetimer_entry);
818 static void send_info(unsigned int send_what, int the_socket) {
821 abuf_init(&abuf, 2 * 4096);
823 /* Print minimal http header */
825 abuf_puts(&abuf, "HTTP/1.1 200 OK\r\n");
826 abuf_puts(&abuf, "Content-type: text/plain\r\n\r\n");
829 /* Print tables to IPC socket */
831 if (send_what & SIW_NEIGHBORS)
832 ipc_print_neighbors(&abuf, false);
833 if (send_what & SIW_LINKS)
834 ipc_print_links(&abuf);
835 if (send_what & SIW_ROUTES)
836 ipc_print_routes(&abuf);
837 if (send_what & SIW_HNA)
838 ipc_print_hna(&abuf);
839 if (send_what & SIW_MID)
840 ipc_print_mid(&abuf);
841 if (send_what & SIW_TOPOLOGY)
842 ipc_print_topology(&abuf);
843 if (send_what & SIW_GATEWAYS)
844 ipc_print_gateways(&abuf);
845 if (send_what & SIW_INTERFACES)
846 ipc_print_interfaces(&abuf);
847 if (send_what & SIW_2HOP)
848 ipc_print_neighbors(&abuf, true);
849 if (send_what & SIW_SGW)
850 ipc_print_sgw(&abuf);
852 if (send_what & SIW_VERSION)
853 ipc_print_version(&abuf);
855 if (send_what & SIW_OLSRD_CONF)
856 ipc_print_olsrd_conf(&abuf);
858 assert(outbuffer_count < MAX_CLIENTS);
860 outbuffer[outbuffer_count] = olsr_malloc(abuf.len, PLUGIN_NAME" output buffer");
861 outbuffer_size[outbuffer_count] = abuf.len;
862 outbuffer_written[outbuffer_count] = 0;
863 outbuffer_socket[outbuffer_count] = the_socket;
865 memcpy(outbuffer[outbuffer_count], abuf.buf, abuf.len);
868 if (outbuffer_count == 1) {
869 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
880 * indent-tabs-mode: nil