2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of olsr.org, olsrd nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * Visit http://www.olsr.org for more information.
36 * If you find this software useful feel free to make a donation
37 * to the project. For more information see the website or contact
38 * the copyright holders.
43 * Dynamic linked library for the olsr.org olsr daemon
46 #include <sys/types.h>
47 #include <sys/socket.h>
49 #include <sys/select.h>
51 #include <netinet/in.h>
52 #include <arpa/inet.h>
65 #include "builddata.h"
66 #include "olsr_types.h"
67 #include "neighbor_table.h"
68 #include "two_hop_neighbor_table.h"
69 #include "mpr_selector_set.h"
75 #include "lq_plugin.h"
76 #include "common/autobuf.h"
79 #include "olsrd_txtinfo.h"
80 #include "olsrd_plugin.h"
83 #define close(x) closesocket(x)
86 /* defines to make txtinfo and jsoninfo look alike */
87 #define PLUGIN_NAME "TXTINFO"
88 #define info_accept_ip txtinfo_accept_ip
89 #define info_listen_ip txtinfo_listen_ip
90 #define info_ipv6_only txtinfo_ipv6_only
91 #ifdef TXTINFO_ALLOW_LOCALHOST
92 #define INFO_ALLOW_LOCALHOST TXTINFO_ALLOW_LOCALHOST
95 static int ipc_socket;
98 #define HTTP_200 "HTTP/1.1 200 OK"
100 /* IPC initialization function */
101 static int plugin_ipc_init(void);
103 static void send_info(unsigned int /*send_what*/, int /*socket*/);
105 static void ipc_action(int, void *, unsigned int);
107 #define TXT_IPC_BUFSIZE 256
109 /* these provide all of the runtime status info */
110 #define SIW_NEIGHBORS 0x0001
111 #define SIW_LINKS 0x0002
112 #define SIW_ROUTES 0x0004
113 #define SIW_HNA 0x0008
114 #define SIW_MID 0x0010
115 #define SIW_TOPOLOGY 0x0020
116 #define SIW_GATEWAYS 0x0040
117 #define SIW_INTERFACES 0x0080
118 #define SIW_2HOP 0x0100
119 #define SIW_SGW 0x0200
120 #define SIW_RUNTIME_ALL (SIW_NEIGHBORS | SIW_LINKS | SIW_ROUTES | SIW_HNA | SIW_MID | SIW_TOPOLOGY | SIW_GATEWAYS | SIW_INTERFACES | SIW_2HOP | SIW_SGW)
122 /* these only change at olsrd startup */
123 #define SIW_VERSION 0x0400
124 #define SIW_CONFIG 0x0800
125 #define SIW_PLUGINS 0x1000
126 #define SIW_STARTUP_ALL (SIW_VERSION | SIW_CONFIG | SIW_PLUGINS)
128 /* this is everything in normal format */
129 #define SIW_ALL (SIW_RUNTIME_ALL | SIW_STARTUP_ALL)
131 /* this data is not normal format but olsrd.conf format */
132 #define SIW_OLSRD_CONF 0x2000
134 #define MAX_CLIENTS 3
136 static char *outbuffer[MAX_CLIENTS];
137 static size_t outbuffer_size[MAX_CLIENTS];
138 static size_t outbuffer_written[MAX_CLIENTS];
139 static int outbuffer_socket[MAX_CLIENTS];
140 static int outbuffer_count = 0;
142 static struct timer_entry *writetimer_entry;
144 static void build_http_header(const char *status, const char *mime, struct autobuf *abuf, int *contentLengthPlaceholderStart) {
146 abuf_appendf(abuf, "%s\r\n", status);
154 if (strftime(buf, sizeof(buf), "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", gmtime(&currtime))) {
155 abuf_puts(abuf, buf);
160 abuf_puts(abuf, "Server: OLSRD "PLUGIN_NAME"\r\n");
162 /* connection-type */
163 abuf_puts(abuf, "Connection: close\r\n");
167 abuf_appendf(abuf, "Content-Type: %s\r\n", mime);
171 /* No needs to be strict here, access control is based on source IP */
172 abuf_puts(abuf, "Access-Control-Allow-Origin: *\r\n");
173 abuf_puts(abuf, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
174 abuf_puts(abuf, "Access-Control-Allow-Headers: Accept, Origin, X-Requested-With\r\n");
175 abuf_puts(abuf, "Access-Control-Max-Age: 1728000\r\n");
178 abuf_puts(abuf, "Content-Length: ");
179 *contentLengthPlaceholderStart = abuf->len;
180 abuf_puts(abuf, " "); /* 12 spaces reserved for the length (max. 1TB-1), to be filled at the end */
181 abuf_puts(abuf, "\r\n");
184 * No caching dynamic pages
186 abuf_puts(abuf, "Cache-Control: no-cache\r\n");
189 abuf_puts(abuf, "\r\n");
192 static void http_header_adjust_content_length(struct autobuf *abuf, int contentLengthPlaceholderStart, int contentLength) {
193 char buf[12 + 1]; /* size must match to number of spaces used (+1 for the terminating byte) */
195 memset(buf, 0, sizeof(buf));
196 snprintf(buf, sizeof(buf), "%d", contentLength);
197 buf[sizeof(buf) - 1] = '\0';
198 memcpy(&abuf->buf[contentLengthPlaceholderStart], buf, strlen(buf));
202 *Do initialization here
204 *This function is called by the my_init
205 *function in uolsrd_plugin.c
207 int olsrd_plugin_init(void) {
208 /* Initial IPC value */
216 * destructor - called at unload
218 void olsr_plugin_exit(void) {
219 if (ipc_socket != -1)
223 static int plugin_ipc_init(void) {
224 union olsr_sockaddr sst;
228 /* Init ipc socket */
229 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
231 olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
235 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
237 olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
241 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
242 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *) &yes, sizeof(yes)) < 0) {
243 perror("SO_REUSEADDR failed");
246 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
247 #if defined linux && defined IPV6_V6ONLY
248 if (info_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
249 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
250 perror("IPV6_V6ONLY failed");
254 #endif /* defined linux && defined IPV6_V6ONLY */
255 /* Bind the socket */
257 /* complete the socket structure */
258 memset(&sst, 0, sizeof(sst));
259 if (olsr_cnf->ip_version == AF_INET) {
260 sst.in4.sin_family = AF_INET;
261 addrlen = sizeof(struct sockaddr_in);
263 sst.in4.sin_len = addrlen;
264 #endif /* SIN6_LEN */
265 sst.in4.sin_addr.s_addr = info_listen_ip.v4.s_addr;
266 sst.in4.sin_port = htons(ipc_port);
268 sst.in6.sin6_family = AF_INET6;
269 addrlen = sizeof(struct sockaddr_in6);
271 sst.in6.sin6_len = addrlen;
272 #endif /* SIN6_LEN */
273 sst.in6.sin6_addr = info_listen_ip.v6;
274 sst.in6.sin6_port = htons(ipc_port);
277 /* bind the socket to the port number */
278 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
280 olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
285 /* show that we are willing to listen */
286 if (listen(ipc_socket, 1) == -1) {
288 olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
293 /* Register with olsrd */
294 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
297 olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
303 static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
304 union olsr_sockaddr pin;
306 char addr[INET6_ADDRSTRLEN];
309 unsigned int send_what = 0;
312 socklen_t addrlen = sizeof(pin);
314 if (outbuffer_count >= MAX_CLIENTS) {
318 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
320 olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
325 tv.tv_sec = tv.tv_usec = 0;
326 if (olsr_cnf->ip_version == AF_INET) {
327 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
329 if (!ip4equal(&pin.in4.sin_addr, &info_accept_ip.v4) && info_accept_ip.v4.s_addr != INADDR_ANY) {
330 #ifdef INFO_ALLOW_LOCALHOST
331 if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
332 #endif /* INFO_ALLOW_LOCALHOST */
333 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
334 close(ipc_connection);
336 #ifdef INFO_ALLOW_LOCALHOST
338 #endif /* INFO_ALLOW_LOCALHOST */
341 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
343 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
344 if (!ip6equal(&in6addr_any, &info_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &info_accept_ip.v6)) {
345 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
346 close(ipc_connection);
352 olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
355 /* purge read buffer to prevent blocking on linux */
357 FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
358 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
360 ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
362 if (s == sizeof(requ) - 1) {
363 /* input was much too long, just skip the rest */
366 while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
372 /* print out the requested tables */
373 if (strstr(requ, "/con"))
374 send_what |= SIW_OLSRD_CONF;
375 else if (strstr(requ, "/all"))
378 /* print out every combinations of requested tabled
379 * 3++ letter abbreviations are matched */
380 if (strstr(requ, "/nei"))
381 send_what |= SIW_NEIGHBORS;
382 if (strstr(requ, "/lin"))
383 send_what |= SIW_LINKS;
384 if (strstr(requ, "/rou"))
385 send_what |= SIW_ROUTES;
386 if (strstr(requ, "/hna"))
387 send_what |= SIW_HNA;
388 if (strstr(requ, "/mid"))
389 send_what |= SIW_MID;
390 if (strstr(requ, "/top"))
391 send_what |= SIW_TOPOLOGY;
392 if (strstr(requ, "/gat"))
393 send_what |= SIW_GATEWAYS;
394 if (strstr(requ, "/int"))
395 send_what |= SIW_INTERFACES;
396 if (strstr(requ, "/2ho"))
397 send_what |= SIW_2HOP;
398 if (strstr(requ, "/sgw"))
399 send_what |= SIW_SGW;
402 if (strstr(requ, "/ver"))
403 send_what |= SIW_VERSION;
404 if (strstr(requ, "/config"))
405 send_what |= SIW_CONFIG;
406 if (strstr(requ, "/plugins"))
407 send_what |= SIW_PLUGINS;
409 /* To print out neighbours only on the Freifunk Status
410 * page the normal output is somewhat lengthy. The
411 * header parsing is sufficient for standard wget.
413 if (strstr(requ, "/neighbours"))
414 send_what = SIW_NEIGHBORS | SIW_LINKS;
422 send_info(send_what, ipc_connection);
425 static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
426 struct ipaddr_str buf1;
427 struct neighbor_entry *neigh;
428 struct neighbor_2_list_entry *list_2;
431 abuf_puts(abuf, "Table: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWill.");
433 abuf_puts(abuf, "\n\t2hop interface adrress\n");
435 abuf_puts(abuf, "\t2 Hop Neighbors\n");
438 OLSR_FOR_ALL_NBR_ENTRIES(neigh)
440 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",
441 neigh->is_mpr ? "YES" : "NO", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO", neigh->willingness);
444 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
446 abuf_appendf(abuf, "\t%s\n", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
451 abuf_appendf(abuf, "%d\n", thop_cnt);
453 }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
454 abuf_puts(abuf, "\n");
457 static void ipc_print_links(struct autobuf *abuf) {
458 struct ipaddr_str buf1, buf2;
459 struct lqtextbuffer lqbuffer1, lqbuffer2;
461 struct link_entry *my_link = NULL;
463 #ifdef ACTIVATE_VTIME_TXTINFO
464 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tVTime\tLQ\tNLQ\tCost\n");
465 #else /* ACTIVATE_VTIME_TXTINFO */
466 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tHyst.\tLQ\tNLQ\tCost\n");
467 #endif /* ACTIVATE_VTIME_TXTINFO */
470 OLSR_FOR_ALL_LINK_ENTRIES(my_link)
472 #ifdef ACTIVATE_VTIME_TXTINFO
473 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
475 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),
476 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
477 diff/1000, abs(diff%1000),
478 get_link_entry_text(my_link, '\t', &lqbuffer1),
479 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
480 #else /* ACTIVATE_VTIME_TXTINFO */
481 abuf_appendf(abuf, "%s\t%s\t0.00\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
482 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr), get_link_entry_text(my_link, '\t', &lqbuffer1),
483 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
484 #endif /* ACTIVATE_VTIME_TXTINFO */
485 }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
487 abuf_puts(abuf, "\n");
490 static void ipc_print_routes(struct autobuf *abuf) {
491 struct ipaddr_str buf1, buf2;
493 struct lqtextbuffer lqbuffer;
495 abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
497 /* Walk the route table */
498 OLSR_FOR_ALL_RT_ENTRIES(rt)
500 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,
501 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway), rt->rt_best->rtp_metric.hops,
502 get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer), if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
503 }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
505 abuf_puts(abuf, "\n");
509 static void ipc_print_topology(struct autobuf *abuf) {
512 #ifdef ACTIVATE_VTIME_TXTINFO
513 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
514 #else /* ACTIVATE_VTIME_TXTINFO */
515 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\n");
516 #endif /* ACTIVATE_VTIME_TXTINFO */
519 OLSR_FOR_ALL_TC_ENTRIES(tc)
521 struct tc_edge_entry *tc_edge;
522 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
524 if (tc_edge->edge_inv) {
525 struct ipaddr_str dstbuf, addrbuf;
526 struct lqtextbuffer lqbuffer1, lqbuffer2;
527 #ifdef ACTIVATE_VTIME_TXTINFO
528 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
529 int diff = (int)(vt);
530 abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d.%03d\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr),
531 olsr_ip_to_string(&addrbuf, &tc->addr),
532 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1),
533 get_linkcost_text(tc_edge->cost, false, &lqbuffer2),
534 diff/1000, diff%1000);
535 #else /* ACTIVATE_VTIME_TXTINFO */
536 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),
537 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1), get_linkcost_text(tc_edge->cost, false, &lqbuffer2));
538 #endif /* ACTIVATE_VTIME_TXTINFO */
540 }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
541 }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
543 abuf_puts(abuf, "\n");
546 static void ipc_print_hna(struct autobuf *abuf) {
547 struct ip_prefix_list *hna;
548 struct hna_entry *tmp_hna;
549 struct hna_net *tmp_net;
550 struct ipaddr_str buf, mainaddrbuf;
552 #ifdef ACTIVATE_VTIME_TXTINFO
553 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\tVTime\n");
554 #else /* ACTIVATE_VTIME_TXTINFO */
555 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\n");
556 #endif /* ACTIVATE_VTIME_TXTINFO */
558 /* Announced HNA entries */
559 for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
560 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));
564 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
567 /* Check all networks */
568 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
569 #ifdef ACTIVATE_VTIME_TXTINFO
570 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
571 int diff = (int)(vt);
572 abuf_appendf(abuf, "%s/%d\t%s\t\%d.%03d\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
573 tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr),
574 diff/1000, abs(diff%1000));
575 #else /* ACTIVATE_VTIME_TXTINFO */
576 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix), tmp_net->hna_prefix.prefix_len,
577 olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
578 #endif /* ACTIVATE_VTIME_TXTINFO */
580 }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
582 abuf_puts(abuf, "\n");
585 static void ipc_print_mid(struct autobuf *abuf) {
587 unsigned short is_first;
588 struct mid_entry *entry;
589 struct mid_address *alias;
590 #ifdef ACTIVATE_VTIME_TXTINFO
591 abuf_puts(abuf, "Table: MID\nIP address\tAlias\tVTime\n");
592 #else /* ACTIVATE_VTIME_TXTINFO */
593 abuf_puts(abuf, "Table: MID\nIP address\tAliases\n");
594 #endif /* ACTIVATE_VTIME_TXTINFO */
597 for (idx = 0; idx < HASHSIZE; idx++) {
598 entry = mid_set[idx].next;
600 while (entry != &mid_set[idx]) {
601 #ifdef ACTIVATE_VTIME_TXTINFO
602 struct ipaddr_str buf, buf2;
603 #else /* ACTIVATE_VTIME_TXTINFO */
604 struct ipaddr_str buf;
605 abuf_puts(abuf, olsr_ip_to_string(&buf, &entry->main_addr));
606 #endif /* ACTIVATE_VTIME_TXTINFO */
607 alias = entry->aliases;
611 #ifdef ACTIVATE_VTIME_TXTINFO
612 uint32_t vt = alias->vtime - now_times;
613 int diff = (int)(vt);
615 abuf_appendf(abuf, "%s\t%s\t%d.%03d\n",
616 olsr_ip_to_string(&buf, &entry->main_addr),
617 olsr_ip_to_string(&buf2, &alias->alias),
618 diff/1000, abs(diff%1000));
619 #else /* ACTIVATE_VTIME_TXTINFO */
620 abuf_appendf(abuf, "%s%s", (is_first ? "\t" : ";"), olsr_ip_to_string(&buf, &alias->alias));
621 #endif /* ACTIVATE_VTIME_TXTINFO */
622 alias = alias->next_alias;
626 #ifndef ACTIVATE_VTIME_TXTINFO
627 abuf_puts(abuf, "\n");
628 #endif /* ACTIVATE_VTIME_TXTINFO */
631 abuf_puts(abuf, "\n");
634 static void ipc_print_gateways(struct autobuf *abuf) {
636 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
637 #else /* __linux__ */
638 static const char IPV4[] = "ipv4";
639 static const char IPV4_NAT[] = "ipv4(n)";
640 static const char IPV6[] = "ipv6";
641 static const char NONE[] = "-";
643 struct ipaddr_str buf;
644 struct gateway_entry *gw;
645 struct lqtextbuffer lqbuf;
647 // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
648 abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
649 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
651 char v4 = '-', v6 = '-';
652 const char *v4type = NONE, *v6type = NONE;
655 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
659 if (gw == olsr_get_inet_gateway(false)) {
661 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
665 if (gw == olsr_get_inet_gateway(true)) {
667 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
672 v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
678 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),
679 get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops, gw->uplink, gw->downlink, v4type, v6type,
680 gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
681 }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
682 #endif /* __linux__ */
687 /** interface names for smart gateway tunnel interfaces, IPv4 */
688 extern struct interfaceName * sgwTunnel4InterfaceNames;
690 /** interface names for smart gateway tunnel interfaces, IPv6 */
691 extern struct interfaceName * sgwTunnel6InterfaceNames;
694 * Construct the sgw table for a given ip version
696 * @param abuf the string buffer
697 * @param ipv6 true for IPv6, false for IPv4
698 * @param fmtv the format for printing
700 static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const char * fmtv) {
701 struct interfaceName * sgwTunnelInterfaceNames;
703 abuf_appendf(abuf, "# Table: Smart Gateway IPv%s\n", ipv6 ? "6" : "4");
704 abuf_appendf(abuf, fmth, "#", "Originator", "Prefix", "Uplink", "Downlink", "PathCost", "IPv4", "IPv4-NAT", "IPv6", "Tunnel-Name", "Destination", "Cost");
706 sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
707 if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
708 struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
710 for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
711 struct interfaceName * node = &sgwTunnelInterfaceNames[i];
712 struct gateway_entry * gw = node->gw;
719 struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
721 struct ipaddr_str originatorStr;
722 const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
723 struct ipaddr_str prefixIpStr;
724 const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
725 union olsr_ip_addr netmask = { { 0 } };
726 struct ipaddr_str prefixMaskStr;
727 const char * prefixMASKStr;
728 char prefixAndMask[strlen(prefix) + 1 + INET_ADDRSTRLEN + 1];
731 prefix_to_netmask((uint8_t *) &netmask, sizeof(netmask.v4), gw->external_prefix.prefix_len);
732 prefixMASKStr = olsr_ip_to_string(&prefixMaskStr, &netmask);
733 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%s", prefix, prefixMASKStr);
735 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%d", prefix, gw->external_prefix.prefix_len);
738 abuf_appendf(abuf, fmtv, //
739 (current_gw && (current_gw == gw)) ? "*" : " ", // selected
740 originator, // Originator
741 prefixAndMask, // 4: Prefix IP / Prefix Mask, 6: Prefix IP / Prefix Length
742 gw->uplink, // Uplink
743 gw->downlink, // Downlink
744 !tc ? ROUTE_COST_BROKEN : tc->path_cost, // PathCost
745 gw->ipv4 ? "Y" : "N", // IPv4
746 gw->ipv4nat ? "Y" : "N", // IPv4-NAT
747 gw->ipv6 ? "Y" : "N", // IPv6
748 node->name, // Tunnel-Name
749 originator, // Destination
750 gw->path_cost // Cost
756 #endif /* __linux__ */
758 static void ipc_print_sgw(struct autobuf *abuf) {
760 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
763 static const char * fmth4 = "%s%-15s %-31s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-15s %s\n";
764 static const char * fmtv4 = "%s%-15s %-31s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-15s %lld\n";
766 static const char * fmth6 = "%s%-45s %-49s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-45s %s\n";
767 static const char * fmtv6 = "%s%-45s %-49s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-45s %lld\n";
770 sgw_ipvx(abuf, false, fmth4, fmtv4);
771 abuf_puts(abuf, "\n");
773 sgw_ipvx(abuf, true, fmth6, fmtv6);
774 abuf_puts(abuf, "\n");
776 #endif /* __linux__ */
779 static void ipc_print_version(struct autobuf *abuf) {
780 abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
783 static void ipc_print_olsrd_conf(struct autobuf *abuf) {
784 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
787 static void ipc_print_interfaces(struct autobuf *abuf) {
788 const struct olsr_if *ifs;
789 abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
790 for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
791 const struct interface_olsr * const rifs = ifs->interf;
792 abuf_appendf(abuf, "%s\t", ifs->name);
794 abuf_puts(abuf, "DOWN\n");
797 abuf_appendf(abuf, "UP\t%d\t%s\t", rifs->int_mtu, rifs->is_wireless ? "Yes" : "No");
799 if (olsr_cnf->ip_version == AF_INET) {
800 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
801 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),
802 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
804 struct ipaddr_str addrbuf, maskbuf;
805 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));
808 abuf_puts(abuf, "\n");
811 static void info_write_data(void *foo __attribute__ ((unused))) {
813 int result, i, j, max;
818 for (i = 0; i < outbuffer_count; i++) {
819 /* And we cast here since we get a warning on Win32 */
820 FD_SET((unsigned int )(outbuffer_socket[i]), &set);
822 if (outbuffer_socket[i] > max) {
823 max = outbuffer_socket[i];
830 result = select(max + 1, NULL, &set, NULL, &tv);
835 for (i = 0; i < outbuffer_count; i++) {
836 if (FD_ISSET(outbuffer_socket[i], &set)) {
837 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
839 outbuffer_written[i] += result;
842 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
843 /* close this socket and cleanup*/
844 close(outbuffer_socket[i]);
847 for (j = i + 1; j < outbuffer_count; j++) {
848 outbuffer[j - 1] = outbuffer[j];
849 outbuffer_size[j - 1] = outbuffer_size[j];
850 outbuffer_socket[j - 1] = outbuffer_socket[j];
851 outbuffer_written[j - 1] = outbuffer_written[j];
857 if (!outbuffer_count) {
858 olsr_stop_timer(writetimer_entry);
862 static void send_info(unsigned int send_what, int the_socket) {
865 const char *content_type = "text/plain; charset=utf-8";
866 int contentLengthPlaceholderStart = 0;
867 int headerLength = 0;
869 abuf_init(&abuf, 2 * 4096);
872 build_http_header(HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
873 headerLength = abuf.len;
876 // only add if normal format
877 if (send_what & SIW_ALL) {
878 if (send_what & SIW_LINKS)
879 ipc_print_links(&abuf);
880 if (send_what & SIW_NEIGHBORS)
881 ipc_print_neighbors(&abuf, false);
882 if (send_what & SIW_TOPOLOGY)
883 ipc_print_topology(&abuf);
884 if (send_what & SIW_HNA)
885 ipc_print_hna(&abuf);
886 if (send_what & SIW_SGW)
887 ipc_print_sgw(&abuf);
888 if (send_what & SIW_MID)
889 ipc_print_mid(&abuf);
890 if (send_what & SIW_ROUTES)
891 ipc_print_routes(&abuf);
892 if (send_what & SIW_GATEWAYS)
893 ipc_print_gateways(&abuf);
894 if (send_what & SIW_CONFIG) {
897 if (send_what & SIW_INTERFACES)
898 ipc_print_interfaces(&abuf);
899 if (send_what & SIW_2HOP)
900 ipc_print_neighbors(&abuf, true);
901 if (send_what & SIW_VERSION)
902 ipc_print_version(&abuf);
903 if (send_what & SIW_PLUGINS) {
906 } else if (send_what & SIW_OLSRD_CONF) {
907 /* this outputs the olsrd.conf text directly, not normal format */
908 ipc_print_olsrd_conf(&abuf);
912 http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
915 /* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
916 outbuffer[outbuffer_count] = abuf.buf;
917 outbuffer_size[outbuffer_count] = abuf.len;
918 outbuffer_written[outbuffer_count] = 0;
919 outbuffer_socket[outbuffer_count] = the_socket;
926 if (outbuffer_count == 1) {
927 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
938 * indent-tabs-mode: nil