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"
93 static int ipc_socket;
95 /* IPC initialization function */
96 static int plugin_ipc_init(void);
98 static void send_info(unsigned int /*send_what*/, int /*socket*/);
100 static void ipc_action(int, void *, unsigned int);
102 static void ipc_print_neighbors(struct autobuf *, bool);
104 static void ipc_print_links(struct autobuf *);
106 static void ipc_print_routes(struct autobuf *);
108 static void ipc_print_topology(struct autobuf *);
110 static void ipc_print_hna(struct autobuf *);
112 static void ipc_print_mid(struct autobuf *);
114 static void ipc_print_gateways(struct autobuf *);
116 static void ipc_print_olsrd_conf(struct autobuf *);
118 static void ipc_print_interfaces(struct autobuf *);
120 static void ipc_print_sgw(struct autobuf *);
122 #define TXT_IPC_BUFSIZE 256
124 #define SIW_NEIGH 0x0001
125 #define SIW_LINK 0x0002
126 #define SIW_ROUTE 0x0004
127 #define SIW_HNA 0x0008
128 #define SIW_MID 0x0010
129 #define SIW_TOPO 0x0020
130 #define SIW_GATEWAY 0x0040
131 #define SIW_INTERFACE 0x0080
132 #define SIW_CONFIG 0x0100
133 #define SIW_2HOP 0x0200
134 #define SIW_VERSION 0x0400
135 #define SIW_SGW 0x0800
137 /* ALL = neigh link route hna mid topo */
138 #define SIW_ALL 0x003F
140 #define MAX_CLIENTS 3
142 static char *outbuffer[MAX_CLIENTS];
143 static size_t outbuffer_size[MAX_CLIENTS];
144 static size_t outbuffer_written[MAX_CLIENTS];
145 static int outbuffer_socket[MAX_CLIENTS];
146 static int outbuffer_count = 0;
148 static struct timer_entry *writetimer_entry;
151 *Do initialization here
153 *This function is called by the my_init
154 *function in uolsrd_plugin.c
156 int olsrd_plugin_init(void) {
157 /* Initial IPC value */
165 * destructor - called at unload
167 void olsr_plugin_exit(void) {
168 if (ipc_socket != -1)
172 static int plugin_ipc_init(void) {
173 union olsr_sockaddr sst;
177 /* Init ipc socket */
178 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
180 olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
184 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
186 olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
190 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
191 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0) {
192 perror("SO_REUSEADDR failed");
195 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
196 #if defined linux && defined IPV6_V6ONLY
197 if (txtinfo_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
198 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
199 perror("IPV6_V6ONLY failed");
203 #endif /* defined linux && defined IPV6_V6ONLY */
204 /* Bind the socket */
206 /* complete the socket structure */
207 memset(&sst, 0, sizeof(sst));
208 if (olsr_cnf->ip_version == AF_INET) {
209 sst.in4.sin_family = AF_INET;
210 addrlen = sizeof(struct sockaddr_in);
212 sst.in4.sin_len = addrlen;
213 #endif /* SIN6_LEN */
214 sst.in4.sin_addr.s_addr = txtinfo_listen_ip.v4.s_addr;
215 sst.in4.sin_port = htons(ipc_port);
217 sst.in6.sin6_family = AF_INET6;
218 addrlen = sizeof(struct sockaddr_in6);
220 sst.in6.sin6_len = addrlen;
221 #endif /* SIN6_LEN */
222 sst.in6.sin6_addr = txtinfo_listen_ip.v6;
223 sst.in6.sin6_port = htons(ipc_port);
226 /* bind the socket to the port number */
227 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
229 olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
234 /* show that we are willing to listen */
235 if (listen(ipc_socket, 1) == -1) {
237 olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
242 /* Register with olsrd */
243 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
246 olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
252 static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
253 union olsr_sockaddr pin;
255 char addr[INET6_ADDRSTRLEN];
258 unsigned int send_what = 0;
261 socklen_t addrlen = sizeof(pin);
263 if (outbuffer_count >= MAX_CLIENTS) {
267 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
269 olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
274 tv.tv_sec = tv.tv_usec = 0;
275 if (olsr_cnf->ip_version == AF_INET) {
276 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
278 if (!ip4equal(&pin.in4.sin_addr, &txtinfo_accept_ip.v4) && txtinfo_accept_ip.v4.s_addr != INADDR_ANY) {
279 #ifdef TXTINFO_ALLOW_LOCALHOST
280 if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
281 #endif /* TXTINFO_ALLOW_LOCALHOST */
282 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
283 close(ipc_connection);
285 #ifdef TXTINFO_ALLOW_LOCALHOST
287 #endif /* TXTINFO_ALLOW_LOCALHOST */
290 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
292 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
293 if (!ip6equal(&in6addr_any, &txtinfo_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &txtinfo_accept_ip.v6)) {
294 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
295 close(ipc_connection);
301 olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
304 /* purge read buffer to prevent blocking on linux */
306 FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
307 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
309 ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
311 if (s == sizeof(requ) - 1) {
312 /* input was much too long, just skip the rest */
315 while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
320 /* To print out neighbours only on the Freifunk Status
321 * page the normal output is somewhat lengthy. The
322 * header parsing is sufficient for standard wget.
324 if (strstr(requ, "/neighbours"))
325 send_what = SIW_NEIGH | SIW_LINK;
327 /* print out every combinations of requested tabled
328 * 3++ letter abbreviations are matched */
329 if (strstr(requ, "/all"))
331 else { /*already included in /all*/
332 if (strstr(requ, "/nei"))
333 send_what |= SIW_NEIGH;
334 if (strstr(requ, "/lin"))
335 send_what |= SIW_LINK;
336 if (strstr(requ, "/rou"))
337 send_what |= SIW_ROUTE;
338 if (strstr(requ, "/hna"))
339 send_what |= SIW_HNA;
340 if (strstr(requ, "/mid"))
341 send_what |= SIW_MID;
342 if (strstr(requ, "/top"))
343 send_what |= SIW_TOPO;
345 if (strstr(requ, "/gat"))
346 send_what |= SIW_GATEWAY;
347 if (strstr(requ, "/con"))
348 send_what |= SIW_CONFIG;
349 if (strstr(requ, "/int"))
350 send_what |= SIW_INTERFACE;
351 if (strstr(requ, "/2ho"))
352 send_what |= SIW_2HOP;
353 if (strstr(requ, "/ver"))
354 send_what |= SIW_VERSION;
355 if (strstr(requ, "/sgw"))
356 send_what |= SIW_SGW;
364 send_info(send_what, ipc_connection);
367 static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
368 struct ipaddr_str buf1;
369 struct neighbor_entry *neigh;
370 struct neighbor_2_list_entry *list_2;
373 abuf_puts(abuf, "Table: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWill.");
375 abuf_puts(abuf, "\n\t2hop interface adrress\n");
377 abuf_puts(abuf, "\t2 Hop Neighbors\n");
380 OLSR_FOR_ALL_NBR_ENTRIES(neigh)
382 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",
383 neigh->is_mpr ? "YES" : "NO", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO", neigh->willingness);
386 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
388 abuf_appendf(abuf, "\t%s\n", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
393 abuf_appendf(abuf, "%d\n", thop_cnt);
395 }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
396 abuf_puts(abuf, "\n");
399 static void ipc_print_links(struct autobuf *abuf) {
400 struct ipaddr_str buf1, buf2;
401 struct lqtextbuffer lqbuffer1, lqbuffer2;
403 struct link_entry *my_link = NULL;
405 #ifdef ACTIVATE_VTIME_TXTINFO
406 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tVTime\tLQ\tNLQ\tCost\n");
407 #else /* ACTIVATE_VTIME_TXTINFO */
408 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tHyst.\tLQ\tNLQ\tCost\n");
409 #endif /* ACTIVATE_VTIME_TXTINFO */
412 OLSR_FOR_ALL_LINK_ENTRIES(my_link)
414 #ifdef ACTIVATE_VTIME_TXTINFO
415 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
417 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),
418 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
419 diff/1000, abs(diff%1000),
420 get_link_entry_text(my_link, '\t', &lqbuffer1),
421 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
422 #else /* ACTIVATE_VTIME_TXTINFO */
423 abuf_appendf(abuf, "%s\t%s\t0.00\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
424 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr), get_link_entry_text(my_link, '\t', &lqbuffer1),
425 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
426 #endif /* ACTIVATE_VTIME_TXTINFO */
427 }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
429 abuf_puts(abuf, "\n");
432 static void ipc_print_routes(struct autobuf *abuf) {
433 struct ipaddr_str buf1, buf2;
435 struct lqtextbuffer lqbuffer;
437 abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
439 /* Walk the route table */
440 OLSR_FOR_ALL_RT_ENTRIES(rt)
442 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,
443 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway), rt->rt_best->rtp_metric.hops,
444 get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer), if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
445 }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
447 abuf_puts(abuf, "\n");
451 static void ipc_print_topology(struct autobuf *abuf) {
454 #ifdef ACTIVATE_VTIME_TXTINFO
455 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
456 #else /* ACTIVATE_VTIME_TXTINFO */
457 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\n");
458 #endif /* ACTIVATE_VTIME_TXTINFO */
461 OLSR_FOR_ALL_TC_ENTRIES(tc)
463 struct tc_edge_entry *tc_edge;
464 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
466 if (tc_edge->edge_inv) {
467 struct ipaddr_str dstbuf, addrbuf;
468 struct lqtextbuffer lqbuffer1, lqbuffer2;
469 #ifdef ACTIVATE_VTIME_TXTINFO
470 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
471 int diff = (int)(vt);
472 abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d.%03d\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr),
473 olsr_ip_to_string(&addrbuf, &tc->addr),
474 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1),
475 get_linkcost_text(tc_edge->cost, false, &lqbuffer2),
476 diff/1000, diff%1000);
477 #else /* ACTIVATE_VTIME_TXTINFO */
478 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),
479 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1), get_linkcost_text(tc_edge->cost, false, &lqbuffer2));
480 #endif /* ACTIVATE_VTIME_TXTINFO */
482 }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
483 }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
485 abuf_puts(abuf, "\n");
488 static void ipc_print_hna(struct autobuf *abuf) {
489 struct ip_prefix_list *hna;
490 struct hna_entry *tmp_hna;
491 struct hna_net *tmp_net;
492 struct ipaddr_str buf, mainaddrbuf;
494 #ifdef ACTIVATE_VTIME_TXTINFO
495 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\tVTime\n");
496 #else /* ACTIVATE_VTIME_TXTINFO */
497 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\n");
498 #endif /* ACTIVATE_VTIME_TXTINFO */
500 /* Announced HNA entries */
501 for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
502 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));
506 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
509 /* Check all networks */
510 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
511 #ifdef ACTIVATE_VTIME_TXTINFO
512 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
513 int diff = (int)(vt);
514 abuf_appendf(abuf, "%s/%d\t%s\t\%d.%03d\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
515 tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr),
516 diff/1000, abs(diff%1000));
517 #else /* ACTIVATE_VTIME_TXTINFO */
518 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix), tmp_net->hna_prefix.prefix_len,
519 olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
520 #endif /* ACTIVATE_VTIME_TXTINFO */
522 }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
524 abuf_puts(abuf, "\n");
529 /** interface names for smart gateway tunnel interfaces, IPv4 */
530 extern struct interfaceName * sgwTunnel4InterfaceNames;
532 /** interface names for smart gateway tunnel interfaces, IPv6 */
533 extern struct interfaceName * sgwTunnel6InterfaceNames;
536 * Construct the sgw table for a given ip version
538 * @param abuf the string buffer
539 * @param ipv6 true for IPv6, false for IPv4
540 * @param fmtv the format for printing
542 static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const char * fmtv) {
543 struct interfaceName * sgwTunnelInterfaceNames;
545 abuf_appendf(abuf, "# Table: Smart Gateway IPv%s\n", ipv6 ? "6" : "4");
546 abuf_appendf(abuf, fmth, "#", "Originator", "Prefix", "Uplink", "Downlink", "PathCost", "IPv4", "IPv4-NAT", "IPv6", "Tunnel-Name", "Destination", "Cost");
548 sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
549 if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
550 struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
552 for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
553 struct interfaceName * node = &sgwTunnelInterfaceNames[i];
554 struct gateway_entry * gw = node->gw;
561 struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
563 struct ipaddr_str originatorStr;
564 const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
565 struct ipaddr_str prefixIpStr;
566 const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
567 union olsr_ip_addr netmask = { { 0 } };
568 struct ipaddr_str prefixMaskStr;
569 const char * prefixMASKStr;
570 char prefixAndMask[strlen(prefix) + 1 + INET_ADDRSTRLEN + 1];
573 prefix_to_netmask((uint8_t *) &netmask, sizeof(netmask.v4), gw->external_prefix.prefix_len);
574 prefixMASKStr = olsr_ip_to_string(&prefixMaskStr, &netmask);
575 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%s", prefix, prefixMASKStr);
577 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%d", prefix, gw->external_prefix.prefix_len);
580 abuf_appendf(abuf, fmtv, //
581 (current_gw && (current_gw == gw)) ? "*" : " ", // selected
582 originator, // Originator
583 prefixAndMask, // 4: Prefix IP / Prefix Mask, 6: Prefix IP / Prefix Length
584 gw->uplink, // Uplink
585 gw->downlink, // Downlink
586 !tc ? ROUTE_COST_BROKEN : tc->path_cost, // PathCost
587 gw->ipv4 ? "Y" : "N", // IPv4
588 gw->ipv4nat ? "Y" : "N", // IPv4-NAT
589 gw->ipv6 ? "Y" : "N", // IPv6
590 node->name, // Tunnel-Name
591 originator, // Destination
592 gw->path_cost // Cost
598 #endif /* __linux__ */
600 static void ipc_print_sgw(struct autobuf *abuf) {
602 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
605 static const char * fmth4 = "%s%-15s %-31s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-15s %s\n";
606 static const char * fmtv4 = "%s%-15s %-31s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-15s %lld\n";
608 static const char * fmth6 = "%s%-45s %-49s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-45s %s\n";
609 static const char * fmtv6 = "%s%-45s %-49s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-45s %lld\n";
612 sgw_ipvx(abuf, false, fmth4, fmtv4);
613 abuf_puts(abuf, "\n");
615 sgw_ipvx(abuf, true, fmth6, fmtv6);
616 abuf_puts(abuf, "\n");
618 #endif /* __linux__ */
621 static void ipc_print_mid(struct autobuf *abuf) {
623 unsigned short is_first;
624 struct mid_entry *entry;
625 struct mid_address *alias;
626 #ifdef ACTIVATE_VTIME_TXTINFO
627 abuf_puts(abuf, "Table: MID\nIP address\tAlias\tVTime\n");
628 #else /* ACTIVATE_VTIME_TXTINFO */
629 abuf_puts(abuf, "Table: MID\nIP address\tAliases\n");
630 #endif /* ACTIVATE_VTIME_TXTINFO */
633 for (idx = 0; idx < HASHSIZE; idx++) {
634 entry = mid_set[idx].next;
636 while (entry != &mid_set[idx]) {
637 #ifdef ACTIVATE_VTIME_TXTINFO
638 struct ipaddr_str buf, buf2;
639 #else /* ACTIVATE_VTIME_TXTINFO */
640 struct ipaddr_str buf;
641 abuf_puts(abuf, olsr_ip_to_string(&buf, &entry->main_addr));
642 #endif /* ACTIVATE_VTIME_TXTINFO */
643 alias = entry->aliases;
647 #ifdef ACTIVATE_VTIME_TXTINFO
648 uint32_t vt = alias->vtime - now_times;
649 int diff = (int)(vt);
651 abuf_appendf(abuf, "%s\t%s\t%d.%03d\n",
652 olsr_ip_to_string(&buf, &entry->main_addr),
653 olsr_ip_to_string(&buf2, &alias->alias),
654 diff/1000, abs(diff%1000));
655 #else /* ACTIVATE_VTIME_TXTINFO */
656 abuf_appendf(abuf, "%s%s", (is_first ? "\t" : ";"), olsr_ip_to_string(&buf, &alias->alias));
657 #endif /* ACTIVATE_VTIME_TXTINFO */
658 alias = alias->next_alias;
662 #ifndef ACTIVATE_VTIME_TXTINFO
663 abuf_puts(abuf, "\n");
664 #endif /* ACTIVATE_VTIME_TXTINFO */
667 abuf_puts(abuf, "\n");
670 static void ipc_print_gateways(struct autobuf *abuf) {
672 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
673 #else /* __linux__ */
674 static const char IPV4[] = "ipv4";
675 static const char IPV4_NAT[] = "ipv4(n)";
676 static const char IPV6[] = "ipv6";
677 static const char NONE[] = "-";
679 struct ipaddr_str buf;
680 struct gateway_entry *gw;
681 struct lqtextbuffer lqbuf;
683 // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
684 abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
685 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
687 char v4 = '-', v6 = '-';
688 const char *v4type = NONE, *v6type = NONE;
691 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
695 if (gw == olsr_get_inet_gateway(false)) {
697 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
701 if (gw == olsr_get_inet_gateway(true)) {
703 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
708 v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
714 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),
715 get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops, gw->uplink, gw->downlink, v4type, v6type,
716 gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
717 }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
718 #endif /* __linux__ */
721 static void ipc_print_olsrd_conf(struct autobuf *abuf) {
722 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
725 static void ipc_print_version(struct autobuf *abuf) {
726 abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
728 static void ipc_print_interfaces(struct autobuf *abuf) {
729 const struct olsr_if *ifs;
730 abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
731 for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
732 const struct interface_olsr * const rifs = ifs->interf;
733 abuf_appendf(abuf, "%s\t", ifs->name);
735 abuf_puts(abuf, "DOWN\n");
738 abuf_appendf(abuf, "UP\t%d\t%s\t", rifs->int_mtu, rifs->is_wireless ? "Yes" : "No");
740 if (olsr_cnf->ip_version == AF_INET) {
741 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
742 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),
743 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
745 struct ipaddr_str addrbuf, maskbuf;
746 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));
749 abuf_puts(abuf, "\n");
752 static void txtinfo_write_data(void *foo __attribute__ ((unused))) {
754 int result, i, j, max;
759 for (i = 0; i < outbuffer_count; i++) {
760 /* And we cast here since we get a warning on Win32 */
761 FD_SET((unsigned int )(outbuffer_socket[i]), &set);
763 if (outbuffer_socket[i] > max) {
764 max = outbuffer_socket[i];
771 result = select(max + 1, NULL, &set, NULL, &tv);
776 for (i = 0; i < outbuffer_count; i++) {
777 if (FD_ISSET(outbuffer_socket[i], &set)) {
778 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
780 outbuffer_written[i] += result;
783 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
784 /* close this socket and cleanup*/
785 close(outbuffer_socket[i]);
788 for (j = i + 1; j < outbuffer_count; j++) {
789 outbuffer[j - 1] = outbuffer[j];
790 outbuffer_size[j - 1] = outbuffer_size[j];
791 outbuffer_socket[j - 1] = outbuffer_socket[j];
792 outbuffer_written[j - 1] = outbuffer_written[j];
798 if (outbuffer_count == 0) {
799 olsr_stop_timer(writetimer_entry);
803 static void send_info(unsigned int send_what, int the_socket) {
806 abuf_init(&abuf, 4096);
808 /* Print minimal http header */
809 abuf_puts(&abuf, "HTTP/1.0 200 OK\n");
810 abuf_puts(&abuf, "Content-type: text/plain\n\n");
812 /* Print tables to IPC socket */
815 if (send_what & SIW_LINK)
816 ipc_print_links(&abuf);
818 if (send_what & SIW_NEIGH)
819 ipc_print_neighbors(&abuf, false);
821 if (send_what & SIW_TOPO)
822 ipc_print_topology(&abuf);
824 if (send_what & SIW_HNA)
825 ipc_print_hna(&abuf);
827 if (send_what & SIW_SGW)
828 ipc_print_sgw(&abuf);
830 if (send_what & SIW_MID)
831 ipc_print_mid(&abuf);
833 if (send_what & SIW_ROUTE)
834 ipc_print_routes(&abuf);
836 if (send_what & SIW_GATEWAY)
837 ipc_print_gateways(&abuf);
839 if (send_what & SIW_CONFIG)
840 ipc_print_olsrd_conf(&abuf);
842 if (send_what & SIW_INTERFACE)
843 ipc_print_interfaces(&abuf);
844 /* 2hop neighbour list */
845 if (send_what & SIW_2HOP)
846 ipc_print_neighbors(&abuf, true);
848 if (send_what & SIW_VERSION)
849 ipc_print_version(&abuf);
851 assert(outbuffer_count < MAX_CLIENTS);
853 outbuffer[outbuffer_count] = olsr_malloc(abuf.len, "txt output buffer");
854 outbuffer_size[outbuffer_count] = abuf.len;
855 outbuffer_written[outbuffer_count] = 0;
856 outbuffer_socket[outbuffer_count] = the_socket;
858 memcpy(outbuffer[outbuffer_count], abuf.buf, abuf.len);
861 if (outbuffer_count == 1) {
862 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &txtinfo_write_data, NULL, 0);
873 * indent-tabs-mode: nil