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 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in
20 * the documentation and/or other materials provided with the
22 * * Neither the name of olsr.org, olsrd nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
39 * Visit http://www.olsr.org for more information.
41 * If you find this software useful feel free to make a donation
42 * to the project. For more information see the website or contact
43 * the copyright holders.
48 * Dynamic linked library for the olsr.org olsr daemon
52 #include <sys/types.h>
53 #include <sys/socket.h>
55 #include <sys/select.h>
56 #endif /* !defined _WIN32 */
57 #include <netinet/in.h>
58 #include <arpa/inet.h>
71 #include "builddata.h"
72 #include "olsr_types.h"
73 #include "neighbor_table.h"
74 #include "two_hop_neighbor_table.h"
75 #include "mpr_selector_set.h"
81 #include "lq_plugin.h"
82 #include "common/autobuf.h"
85 #include "olsrd_txtinfo.h"
86 #include "olsrd_plugin.h"
89 #define close(x) closesocket(x)
92 static int ipc_socket;
94 /* IPC initialization function */
95 static int plugin_ipc_init(void);
97 static void send_info(unsigned int /*send_what*/, int /*socket*/);
99 static void ipc_action(int, void *, unsigned int);
101 static void ipc_print_neigh(struct autobuf *, bool);
103 static void ipc_print_link(struct autobuf *);
105 static void ipc_print_routes(struct autobuf *);
107 static void ipc_print_topology(struct autobuf *);
109 static void ipc_print_hna(struct autobuf *);
111 static void ipc_print_mid(struct autobuf *);
113 static void ipc_print_gateway(struct autobuf *);
115 static void ipc_print_config(struct autobuf *);
117 static void ipc_print_interface(struct autobuf *);
119 static void ipc_print_sgw(struct autobuf *);
121 #define TXT_IPC_BUFSIZE 256
123 #define SIW_NEIGH 0x0001
124 #define SIW_LINK 0x0002
125 #define SIW_ROUTE 0x0004
126 #define SIW_HNA 0x0008
127 #define SIW_MID 0x0010
128 #define SIW_TOPO 0x0020
129 #define SIW_GATEWAY 0x0040
130 #define SIW_INTERFACE 0x0080
131 #define SIW_CONFIG 0x0100
132 #define SIW_2HOP 0x0200
133 #define SIW_VERSION 0x0400
134 #define SIW_SGW 0x0800
136 /* ALL = neigh link route hna mid topo */
137 #define SIW_ALL 0x003F
139 #define MAX_CLIENTS 3
141 static char *outbuffer[MAX_CLIENTS];
142 static size_t outbuffer_size[MAX_CLIENTS];
143 static size_t outbuffer_written[MAX_CLIENTS];
144 static int outbuffer_socket[MAX_CLIENTS];
145 static int outbuffer_count = 0;
147 static struct timer_entry *writetimer_entry;
150 *Do initialization here
152 *This function is called by the my_init
153 *function in uolsrd_plugin.c
156 olsrd_plugin_init(void)
158 /* Initial IPC value */
166 * destructor - called at unload
169 olsr_plugin_exit(void)
171 if (ipc_socket != -1)
176 plugin_ipc_init(void)
178 union olsr_sockaddr sst;
182 /* Init ipc socket */
183 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
185 olsr_printf(1, "(TXTINFO) socket()=%s\n", strerror(errno));
189 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) {
191 olsr_printf(1, "(TXTINFO) setsockopt()=%s\n", strerror(errno));
195 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
196 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0) {
197 perror("SO_REUSEADDR failed");
200 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
202 if (txtinfo_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
203 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&yes, sizeof(yes)) < 0) {
204 perror("IPV6_V6ONLY failed");
208 #endif /* defined linux */
209 /* Bind the socket */
211 /* complete the socket structure */
212 memset(&sst, 0, sizeof(sst));
213 if (olsr_cnf->ip_version == AF_INET) {
214 sst.in4.sin_family = AF_INET;
215 addrlen = sizeof(struct sockaddr_in);
217 sst.in4.sin_len = addrlen;
218 #endif /* SIN6_LEN */
219 sst.in4.sin_addr.s_addr = txtinfo_listen_ip.v4.s_addr;
220 sst.in4.sin_port = htons(ipc_port);
222 sst.in6.sin6_family = AF_INET6;
223 addrlen = sizeof(struct sockaddr_in6);
225 sst.in6.sin6_len = addrlen;
226 #endif /* SIN6_LEN */
227 sst.in6.sin6_addr = txtinfo_listen_ip.v6;
228 sst.in6.sin6_port = htons(ipc_port);
231 /* bind the socket to the port number */
232 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
234 olsr_printf(1, "(TXTINFO) bind()=%s\n", strerror(errno));
239 /* show that we are willing to listen */
240 if (listen(ipc_socket, 1) == -1) {
242 olsr_printf(1, "(TXTINFO) listen()=%s\n", strerror(errno));
247 /* Register with olsrd */
248 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
251 olsr_printf(2, "(TXTINFO) listening on port %d\n", ipc_port);
258 ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused)))
260 union olsr_sockaddr pin;
262 char addr[INET6_ADDRSTRLEN];
265 unsigned int send_what = 0;
268 socklen_t addrlen = sizeof(pin);
270 if (outbuffer_count >= MAX_CLIENTS) {
274 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
276 olsr_printf(1, "(TXTINFO) accept()=%s\n", strerror(errno));
281 tv.tv_sec = tv.tv_usec = 0;
282 if (olsr_cnf->ip_version == AF_INET) {
283 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
285 if (!ip4equal(&pin.in4.sin_addr, &txtinfo_accept_ip.v4) && txtinfo_accept_ip.v4.s_addr != INADDR_ANY) {
286 #ifdef TXTINFO_ALLOW_LOCALHOST
287 if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
288 #endif /* TXTINFO_ALLOW_LOCALHOST */
289 olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr);
290 close(ipc_connection);
292 #ifdef TXTINFO_ALLOW_LOCALHOST
294 #endif /* TXTINFO_ALLOW_LOCALHOST */
297 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
299 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
300 if (!ip6equal(&in6addr_any, &txtinfo_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &txtinfo_accept_ip.v6)) {
301 olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr);
302 close(ipc_connection);
308 olsr_printf(2, "(TXTINFO) Connect from %s\n", addr);
311 /* purge read buffer to prevent blocking on linux */
313 FD_SET((unsigned int)ipc_connection, &rfds); /* Win32 needs the cast here */
314 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
316 ssize_t s = recv(ipc_connection, (void *)&requ, sizeof(requ)-1, 0); /* Win32 needs the cast here */
318 if (s == sizeof(requ)-1) {
319 /* input was much too long, just skip the rest */
322 while (recv(ipc_connection, (void *)&dummy, sizeof(dummy), 0) == sizeof(dummy));
326 /* To print out neighbours only on the Freifunk Status
327 * page the normal output is somewhat lengthy. The
328 * header parsing is sufficient for standard wget.
330 if (0 != strstr(requ, "/neighbours")) send_what = SIW_NEIGH | SIW_LINK;
332 /* print out every combinations of requested tabled
333 * 3++ letter abbreviations are matched */
334 if (0 != strstr(requ, "/all")) send_what = SIW_ALL;
335 else { /*already included in /all*/
336 if (0 != strstr(requ, "/nei")) send_what |= SIW_NEIGH;
337 if (0 != strstr(requ, "/lin")) send_what |= SIW_LINK;
338 if (0 != strstr(requ, "/rou")) send_what |= SIW_ROUTE;
339 if (0 != strstr(requ, "/hna")) send_what |= SIW_HNA;
340 if (0 != strstr(requ, "/mid")) send_what |= SIW_MID;
341 if (0 != strstr(requ, "/top")) send_what |= SIW_TOPO;
343 if (0 != strstr(requ, "/gat")) send_what |= SIW_GATEWAY;
344 if (0 != strstr(requ, "/con")) send_what |= SIW_CONFIG;
345 if (0 != strstr(requ, "/int")) send_what |= SIW_INTERFACE;
346 if (0 != strstr(requ, "/2ho")) send_what |= SIW_2HOP;
347 if (0 != strstr(requ, "/ver")) send_what |= SIW_VERSION;
348 if (0 != strstr(requ, "/sgw")) send_what |= SIW_SGW;
352 if ( send_what == 0 ) send_what = SIW_ALL;
355 send_info(send_what, ipc_connection);
359 ipc_print_neigh(struct autobuf *abuf, bool list_2hop)
361 struct ipaddr_str buf1;
362 struct neighbor_entry *neigh;
363 struct neighbor_2_list_entry *list_2;
366 abuf_puts(abuf, "Table: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWill.");
367 if (list_2hop) abuf_puts(abuf,"\n\t2hop interface adrress\n");
368 else abuf_puts(abuf, "\t2 Hop Neighbors\n");
371 OLSR_FOR_ALL_NBR_ENTRIES(neigh) {
372 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",
373 neigh->is_mpr ? "YES" : "NO", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO", neigh->willingness);
376 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
377 if (list_2hop) abuf_appendf(abuf, "\t%s\n", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
381 abuf_appendf(abuf, "%d\n", thop_cnt);
384 OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
385 abuf_puts(abuf, "\n");
389 ipc_print_link(struct autobuf *abuf)
391 struct ipaddr_str buf1, buf2;
392 struct lqtextbuffer lqbuffer1, lqbuffer2;
394 struct link_entry *my_link = NULL;
396 #ifdef ACTIVATE_VTIME_TXTINFO
397 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tVTime\tLQ\tNLQ\tCost\n");
398 #else /* ACTIVATE_VTIME_TXTINFO */
399 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tHyst.\tLQ\tNLQ\tCost\n");
400 #endif /* ACTIVATE_VTIME_TXTINFO */
403 OLSR_FOR_ALL_LINK_ENTRIES(my_link) {
404 #ifdef ACTIVATE_VTIME_TXTINFO
405 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
407 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),
408 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
409 diff/1000, abs(diff%1000),
410 get_link_entry_text(my_link, '\t', &lqbuffer1),
411 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
412 #else /* ACTIVATE_VTIME_TXTINFO */
413 abuf_appendf(abuf, "%s\t%s\t0.00\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
414 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
415 get_link_entry_text(my_link, '\t', &lqbuffer1),
416 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
417 #endif /* ACTIVATE_VTIME_TXTINFO */
418 } OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
420 abuf_puts(abuf, "\n");
424 ipc_print_routes(struct autobuf *abuf)
426 struct ipaddr_str buf1, buf2;
428 struct lqtextbuffer lqbuffer;
430 abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
432 /* Walk the route table */
433 OLSR_FOR_ALL_RT_ENTRIES(rt) {
434 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,
435 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway), rt->rt_best->rtp_metric.hops,
436 get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer),
437 if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
438 } OLSR_FOR_ALL_RT_ENTRIES_END(rt);
440 abuf_puts(abuf, "\n");
445 ipc_print_topology(struct autobuf *abuf)
449 #ifdef ACTIVATE_VTIME_TXTINFO
450 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
451 #else /* ACTIVATE_VTIME_TXTINFO */
452 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\n");
453 #endif /* ACTIVATE_VTIME_TXTINFO */
456 OLSR_FOR_ALL_TC_ENTRIES(tc) {
457 struct tc_edge_entry *tc_edge;
458 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge) {
459 if (tc_edge->edge_inv) {
460 struct ipaddr_str dstbuf, addrbuf;
461 struct lqtextbuffer lqbuffer1, lqbuffer2;
462 #ifdef ACTIVATE_VTIME_TXTINFO
463 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
464 int diff = (int)(vt);
465 abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d.%03d\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr),
466 olsr_ip_to_string(&addrbuf, &tc->addr),
467 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1),
468 get_linkcost_text(tc_edge->cost, false, &lqbuffer2),
469 diff/1000, diff%1000);
470 #else /* ACTIVATE_VTIME_TXTINFO */
471 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),
472 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1), get_linkcost_text(tc_edge->cost, false, &lqbuffer2));
473 #endif /* ACTIVATE_VTIME_TXTINFO */
475 } OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
476 } OLSR_FOR_ALL_TC_ENTRIES_END(tc);
478 abuf_puts(abuf, "\n");
482 ipc_print_hna(struct autobuf *abuf)
484 struct ip_prefix_list *hna;
485 struct hna_entry *tmp_hna;
486 struct hna_net *tmp_net;
487 struct ipaddr_str buf, mainaddrbuf;
489 #ifdef ACTIVATE_VTIME_TXTINFO
490 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\tVTime\n");
491 #else /* ACTIVATE_VTIME_TXTINFO */
492 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\n");
493 #endif /* ACTIVATE_VTIME_TXTINFO */
495 /* Announced HNA entries */
496 for (hna = olsr_cnf->hna_entries; hna != NULL; hna = hna->next) {
497 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &hna->net.prefix), hna->net.prefix_len,
498 olsr_ip_to_string(&mainaddrbuf, &olsr_cnf->main_addr));
502 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna) {
504 /* Check all networks */
505 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
506 #ifdef ACTIVATE_VTIME_TXTINFO
507 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
508 int diff = (int)(vt);
509 abuf_appendf(abuf, "%s/%d\t%s\t\%d.%03d\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
510 tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr),
511 diff/1000, abs(diff%1000));
512 #else /* ACTIVATE_VTIME_TXTINFO */
513 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
514 tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
515 #endif /* ACTIVATE_VTIME_TXTINFO */
518 OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
520 abuf_puts(abuf, "\n");
526 * Construct the sgw table for a given ip version
528 * @param abuf the string buffer
529 * @param ipv6 true for IPv6, false for IPv4
530 * @param fmtv the format for printing
532 static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const char * fmtv) {
533 struct gateway_entry * current_gw;
534 struct gw_list * list;
535 struct gw_container_entry * gw;
537 list = ipv6 ? &gw_list_ipv6 : &gw_list_ipv4;
539 char current[2] = { 0, 0 };
540 char originator[INET6_ADDRSTRLEN];
541 char prefix[(INET6_ADDRSTRLEN * 2) + 1];
543 uint32_t downlink = 0;
544 olsr_linkcost pc = 0;
545 char sipv4[2] = { 0, 0 };
546 char sipv4nat[2] = { 0, 0 };
547 char sipv6[2] = { 0, 0 };
548 char if_name[IF_NAMESIZE];
549 char destination[INET6_ADDRSTRLEN];
550 long long int cost = 0;
552 memset(originator, 0, sizeof(originator));
553 memset(prefix, 0, sizeof(prefix));
554 memset(if_name, 0, sizeof(if_name));
555 memset(destination, 0, sizeof(destination));
557 abuf_appendf(abuf, "# Table: Smart Gateway IPv%s\n", ipv6 ? "6" : "4");
558 abuf_appendf(abuf, fmth, "#", "Originator", "Prefix", "Uplink", "Downlink", "PathCost", "IPv4", "IPv4-NAT", "IPv6", "Tunnel-Name", "Destination", "Cost");
560 current_gw = olsr_get_inet_gateway(false);
561 OLSR_FOR_ALL_GWS(&list->head, gw) {
563 current[0] = (current_gw && (gw->gw == current_gw)) ? '*' : ' ';
566 struct tc_entry* tc = olsr_lookup_tc_entry(&gw->gw->originator);
568 inet_ntop(ipv6 ? AF_INET6 : AF_INET, &gw->gw->originator, originator, sizeof(originator));
569 strncpy(prefix, olsr_ip_prefix_to_string(&gw->gw->external_prefix), sizeof(prefix));
570 prefix[sizeof(prefix) - 1] = '\0';
571 uplink = gw->gw->uplink;
572 downlink = gw->gw->downlink;
573 pc = tc ?tc->path_cost : ROUTE_COST_BROKEN;
574 sipv4[0] = gw->gw->ipv4 ? 'Y' : 'N';
575 sipv4nat[0] = gw->gw->ipv4nat ? 'Y' : 'N';
576 sipv6[0] = gw->gw->ipv6 ? 'Y' : 'N';
579 strncpy(if_name, gw->tunnel->if_name, sizeof(if_name));
580 inet_ntop(ipv6 ? AF_INET6 : AF_INET, &gw->tunnel->target, destination, sizeof(destination));
583 cost = (long long int)gw->gw->path_cost;
585 abuf_appendf(abuf, fmtv, current, originator, prefix, uplink, downlink, pc, sipv4, sipv4nat, sipv6, if_name, destination, cost);
587 } OLSR_FOR_ALL_GWS_END(gw);
590 #endif /* __linux__ */
593 ipc_print_sgw(struct autobuf *abuf)
596 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
599 static const char * fmth4 = "%s%-16s %-33s %-8s %-8s %-8s %-4s %-8s %-4s %-16s %-16s %s\n";
600 static const char * fmtv4 = "%s%-16s %-33s %-8u %-8u %-8u %-4s %-8s %-4s %-16s %-16s %lld\n";
601 static const char * fmth6 = "%s%-46s %-93s %-8s %-8s %-8s %-4s %-8s %-4s %-16s %-46s %s\n";
602 static const char * fmtv6 = "%s%-46s %-93s %-8u %-8u %-8u %-4s %-8s %-4s %-16s %-46s %lld\n";
604 sgw_ipvx(abuf, false, fmth4, fmtv4);
605 abuf_puts(abuf, "\n");
606 sgw_ipvx(abuf, true, fmth6, fmtv6);
607 abuf_puts(abuf, "\n");
608 #endif /* __linux__ */
612 ipc_print_mid(struct autobuf *abuf)
615 unsigned short is_first;
616 struct mid_entry *entry;
617 struct mid_address *alias;
618 #ifdef ACTIVATE_VTIME_TXTINFO
619 abuf_puts(abuf, "Table: MID\nIP address\tAlias\tVTime\n");
620 #else /* ACTIVATE_VTIME_TXTINFO */
621 abuf_puts(abuf, "Table: MID\nIP address\tAliases\n");
622 #endif /* ACTIVATE_VTIME_TXTINFO */
625 for (idx = 0; idx < HASHSIZE; idx++) {
626 entry = mid_set[idx].next;
628 while (entry != &mid_set[idx]) {
629 #ifdef ACTIVATE_VTIME_TXTINFO
630 struct ipaddr_str buf, buf2;
631 #else /* ACTIVATE_VTIME_TXTINFO */
632 struct ipaddr_str buf;
633 abuf_puts(abuf, olsr_ip_to_string(&buf, &entry->main_addr));
634 #endif /* ACTIVATE_VTIME_TXTINFO */
635 alias = entry->aliases;
639 #ifdef ACTIVATE_VTIME_TXTINFO
640 uint32_t vt = alias->vtime - now_times;
641 int diff = (int)(vt);
643 abuf_appendf(abuf, "%s\t%s\t%d.%03d\n",
644 olsr_ip_to_string(&buf, &entry->main_addr),
645 olsr_ip_to_string(&buf2, &alias->alias),
646 diff/1000, abs(diff%1000));
647 #else /* ACTIVATE_VTIME_TXTINFO */
648 abuf_appendf(abuf, "%s%s", (is_first ? "\t" : ";"), olsr_ip_to_string(&buf, &alias->alias));
649 #endif /* ACTIVATE_VTIME_TXTINFO */
650 alias = alias->next_alias;
654 #ifndef ACTIVATE_VTIME_TXTINFO
655 abuf_puts(abuf,"\n");
656 #endif /* ACTIVATE_VTIME_TXTINFO */
659 abuf_puts(abuf, "\n");
663 ipc_print_gateway(struct autobuf *abuf)
666 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
667 #else /* __linux__ */
668 static const char IPV4[] = "ipv4";
669 static const char IPV4_NAT[] = "ipv4(n)";
670 static const char IPV6[] = "ipv6";
671 static const char NONE[] = "-";
673 struct ipaddr_str buf;
674 struct gateway_entry *gw;
675 struct lqtextbuffer lqbuf;
677 // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
678 abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
679 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw) {
680 char v4 = '-', v6 = '-';
681 const char *v4type = NONE, *v6type = NONE;
684 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
688 if (gw == olsr_get_inet_gateway(false)) {
691 else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit)
692 && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
696 if (gw == olsr_get_inet_gateway(true)) {
699 else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
704 v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
710 abuf_appendf(abuf, "%c%c\t%s\t%s\t%d\t%u\t%u\t%s\t%s\t%s\n",
711 v4, v6, olsr_ip_to_string(&buf, &gw->originator),
712 get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops,
713 gw->uplink, gw->downlink, v4type, v6type,
714 gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
715 } OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
716 #endif /* __linux__ */
720 ipc_print_config(struct autobuf *abuf)
722 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
726 ipc_print_version(struct autobuf *abuf)
728 abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
731 ipc_print_interface(struct autobuf *abuf)
733 const struct olsr_if *ifs;
734 abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
735 for (ifs = olsr_cnf->interfaces; ifs != NULL; ifs = ifs->next) {
736 const struct interface *const rifs = ifs->interf;
737 abuf_appendf(abuf, "%s\t", ifs->name);
739 abuf_puts(abuf, "DOWN\n");
742 abuf_appendf(abuf, "UP\t%d\t%s\t",
743 rifs->int_mtu, rifs->is_wireless ? "Yes" : "No");
745 if (olsr_cnf->ip_version == AF_INET) {
746 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
747 abuf_appendf(abuf, "%s\t%s\t%s\n",
748 ip4_to_string(&addrbuf, rifs->int_addr.sin_addr), ip4_to_string(&maskbuf, rifs->int_netmask.sin_addr),
749 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
751 struct ipaddr_str addrbuf, maskbuf;
752 abuf_appendf(abuf, "%s\t\t%s\n",
753 ip6_to_string(&addrbuf, &rifs->int6_addr.sin6_addr), ip6_to_string(&maskbuf, &rifs->int6_multaddr.sin6_addr));
756 abuf_puts(abuf, "\n");
761 txtinfo_write_data(void *foo __attribute__ ((unused))) {
763 int result, i, j, max;
768 for (i=0; i<outbuffer_count; i++) {
769 /* And we cast here since we get a warning on Win32 */
770 FD_SET((unsigned int)(outbuffer_socket[i]), &set);
772 if (outbuffer_socket[i] > max) {
773 max = outbuffer_socket[i];
780 result = select(max + 1, NULL, &set, NULL, &tv);
785 for (i=0; i<outbuffer_count; i++) {
786 if (FD_ISSET(outbuffer_socket[i], &set)) {
787 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
789 outbuffer_written[i] += result;
792 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
793 /* close this socket and cleanup*/
794 close(outbuffer_socket[i]);
797 for (j=i+1; j<outbuffer_count; j++) {
798 outbuffer[j-1] = outbuffer[j];
799 outbuffer_size[j-1] = outbuffer_size[j];
800 outbuffer_socket[j-1] = outbuffer_socket[j];
801 outbuffer_written[j-1] = outbuffer_written[j];
807 if (outbuffer_count == 0) {
808 olsr_stop_timer(writetimer_entry);
813 send_info(unsigned int send_what, int the_socket)
817 abuf_init(&abuf, 4096);
819 /* Print minimal http header */
820 abuf_puts(&abuf, "HTTP/1.0 200 OK\n");
821 abuf_puts(&abuf, "Content-type: text/plain\n\n");
823 /* Print tables to IPC socket */
826 if ((send_what & SIW_LINK) == SIW_LINK) ipc_print_link(&abuf);
828 if ((send_what & SIW_NEIGH) == SIW_NEIGH) ipc_print_neigh(&abuf,false);
830 if ((send_what & SIW_TOPO) == SIW_TOPO) ipc_print_topology(&abuf);
832 if ((send_what & SIW_HNA) == SIW_HNA) ipc_print_hna(&abuf);
834 if ((send_what & SIW_SGW) == SIW_SGW) ipc_print_sgw(&abuf);
836 if ((send_what & SIW_MID) == SIW_MID) ipc_print_mid(&abuf);
838 if ((send_what & SIW_ROUTE) == SIW_ROUTE) ipc_print_routes(&abuf);
840 if ((send_what & SIW_GATEWAY) == SIW_GATEWAY) ipc_print_gateway(&abuf);
842 if ((send_what & SIW_CONFIG) == SIW_CONFIG) ipc_print_config(&abuf);
844 if ((send_what & SIW_INTERFACE) == SIW_INTERFACE) ipc_print_interface(&abuf);
845 /* 2hop neighbour list */
846 if ((send_what & SIW_2HOP) == SIW_2HOP) ipc_print_neigh(&abuf,true);
848 if ((send_what & SIW_VERSION) == SIW_VERSION) ipc_print_version(&abuf);
850 assert(outbuffer_count < MAX_CLIENTS);
852 outbuffer[outbuffer_count] = olsr_malloc(abuf.len, "txt output buffer");
853 outbuffer_size[outbuffer_count] = abuf.len;
854 outbuffer_written[outbuffer_count] = 0;
855 outbuffer_socket[outbuffer_count] = the_socket;
857 memcpy(outbuffer[outbuffer_count], abuf.buf, abuf.len);
860 if (outbuffer_count == 1) {
861 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &txtinfo_write_data, NULL, 0);
872 * indent-tabs-mode: nil