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 // these are the two overarching categories
379 if (strstr(requ, "/runtime"))
380 send_what |= SIW_RUNTIME_ALL;
381 if (strstr(requ, "/startup"))
382 send_what |= SIW_STARTUP_ALL;
384 // these are the individual sections
385 if (strstr(requ, "/nei"))
386 send_what |= SIW_NEIGHBORS;
387 if (strstr(requ, "/lin"))
388 send_what |= SIW_LINKS;
389 if (strstr(requ, "/rou"))
390 send_what |= SIW_ROUTES;
391 if (strstr(requ, "/hna"))
392 send_what |= SIW_HNA;
393 if (strstr(requ, "/mid"))
394 send_what |= SIW_MID;
395 if (strstr(requ, "/top"))
396 send_what |= SIW_TOPOLOGY;
397 if (strstr(requ, "/gat"))
398 send_what |= SIW_GATEWAYS;
399 if (strstr(requ, "/int"))
400 send_what |= SIW_INTERFACES;
401 if (strstr(requ, "/2ho"))
402 send_what |= SIW_2HOP;
403 if (strstr(requ, "/sgw"))
404 send_what |= SIW_SGW;
407 if (strstr(requ, "/ver"))
408 send_what |= SIW_VERSION;
409 if (strstr(requ, "/config"))
410 send_what |= SIW_CONFIG;
411 if (strstr(requ, "/plugins"))
412 send_what |= SIW_PLUGINS;
414 /* To print out neighbours only on the Freifunk Status
415 * page the normal output is somewhat lengthy. The
416 * header parsing is sufficient for standard wget.
418 if (strstr(requ, "/neighbours"))
419 send_what = SIW_NEIGHBORS | SIW_LINKS;
427 send_info(send_what, ipc_connection);
430 static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
431 struct ipaddr_str buf1;
432 struct neighbor_entry *neigh;
433 struct neighbor_2_list_entry *list_2;
436 abuf_puts(abuf, "Table: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWill.");
438 abuf_puts(abuf, "\n\t2hop interface adrress\n");
440 abuf_puts(abuf, "\t2 Hop Neighbors\n");
443 OLSR_FOR_ALL_NBR_ENTRIES(neigh)
445 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",
446 neigh->is_mpr ? "YES" : "NO", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO", neigh->willingness);
449 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
451 abuf_appendf(abuf, "\t%s\n", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
456 abuf_appendf(abuf, "%d\n", thop_cnt);
458 }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
459 abuf_puts(abuf, "\n");
462 static void ipc_print_links(struct autobuf *abuf) {
463 struct ipaddr_str buf1, buf2;
464 struct lqtextbuffer lqbuffer1, lqbuffer2;
466 struct link_entry *my_link = NULL;
468 #ifdef ACTIVATE_VTIME_TXTINFO
469 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tVTime\tLQ\tNLQ\tCost\n");
470 #else /* ACTIVATE_VTIME_TXTINFO */
471 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tHyst.\tLQ\tNLQ\tCost\n");
472 #endif /* ACTIVATE_VTIME_TXTINFO */
475 OLSR_FOR_ALL_LINK_ENTRIES(my_link)
477 #ifdef ACTIVATE_VTIME_TXTINFO
478 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
480 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),
481 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
482 diff/1000, abs(diff%1000),
483 get_link_entry_text(my_link, '\t', &lqbuffer1),
484 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
485 #else /* ACTIVATE_VTIME_TXTINFO */
486 abuf_appendf(abuf, "%s\t%s\t0.00\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
487 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr), get_link_entry_text(my_link, '\t', &lqbuffer1),
488 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
489 #endif /* ACTIVATE_VTIME_TXTINFO */
490 }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
492 abuf_puts(abuf, "\n");
495 static void ipc_print_routes(struct autobuf *abuf) {
496 struct ipaddr_str buf1, buf2;
498 struct lqtextbuffer lqbuffer;
500 abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
502 /* Walk the route table */
503 OLSR_FOR_ALL_RT_ENTRIES(rt)
505 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,
506 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway), rt->rt_best->rtp_metric.hops,
507 get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer), if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
508 }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
510 abuf_puts(abuf, "\n");
514 static void ipc_print_topology(struct autobuf *abuf) {
517 #ifdef ACTIVATE_VTIME_TXTINFO
518 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
519 #else /* ACTIVATE_VTIME_TXTINFO */
520 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\n");
521 #endif /* ACTIVATE_VTIME_TXTINFO */
524 OLSR_FOR_ALL_TC_ENTRIES(tc)
526 struct tc_edge_entry *tc_edge;
527 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
529 if (tc_edge->edge_inv) {
530 struct ipaddr_str dstbuf, addrbuf;
531 struct lqtextbuffer lqbuffer1, lqbuffer2;
532 #ifdef ACTIVATE_VTIME_TXTINFO
533 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
534 int diff = (int)(vt);
535 abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d.%03d\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr),
536 olsr_ip_to_string(&addrbuf, &tc->addr),
537 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1),
538 get_linkcost_text(tc_edge->cost, false, &lqbuffer2),
539 diff/1000, diff%1000);
540 #else /* ACTIVATE_VTIME_TXTINFO */
541 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),
542 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1), get_linkcost_text(tc_edge->cost, false, &lqbuffer2));
543 #endif /* ACTIVATE_VTIME_TXTINFO */
545 }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
546 }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
548 abuf_puts(abuf, "\n");
551 static void ipc_print_hna(struct autobuf *abuf) {
552 struct ip_prefix_list *hna;
553 struct hna_entry *tmp_hna;
554 struct hna_net *tmp_net;
555 struct ipaddr_str buf, mainaddrbuf;
557 #ifdef ACTIVATE_VTIME_TXTINFO
558 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\tVTime\n");
559 #else /* ACTIVATE_VTIME_TXTINFO */
560 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\n");
561 #endif /* ACTIVATE_VTIME_TXTINFO */
563 /* Announced HNA entries */
564 for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
565 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));
569 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
572 /* Check all networks */
573 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
574 #ifdef ACTIVATE_VTIME_TXTINFO
575 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
576 int diff = (int)(vt);
577 abuf_appendf(abuf, "%s/%d\t%s\t\%d.%03d\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
578 tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr),
579 diff/1000, abs(diff%1000));
580 #else /* ACTIVATE_VTIME_TXTINFO */
581 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix), tmp_net->hna_prefix.prefix_len,
582 olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
583 #endif /* ACTIVATE_VTIME_TXTINFO */
585 }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
587 abuf_puts(abuf, "\n");
590 static void ipc_print_mid(struct autobuf *abuf) {
592 unsigned short is_first;
593 struct mid_entry *entry;
594 struct mid_address *alias;
595 #ifdef ACTIVATE_VTIME_TXTINFO
596 abuf_puts(abuf, "Table: MID\nIP address\tAlias\tVTime\n");
597 #else /* ACTIVATE_VTIME_TXTINFO */
598 abuf_puts(abuf, "Table: MID\nIP address\tAliases\n");
599 #endif /* ACTIVATE_VTIME_TXTINFO */
602 for (idx = 0; idx < HASHSIZE; idx++) {
603 entry = mid_set[idx].next;
605 while (entry != &mid_set[idx]) {
606 #ifdef ACTIVATE_VTIME_TXTINFO
607 struct ipaddr_str buf, buf2;
608 #else /* ACTIVATE_VTIME_TXTINFO */
609 struct ipaddr_str buf;
610 abuf_puts(abuf, olsr_ip_to_string(&buf, &entry->main_addr));
611 #endif /* ACTIVATE_VTIME_TXTINFO */
612 alias = entry->aliases;
616 #ifdef ACTIVATE_VTIME_TXTINFO
617 uint32_t vt = alias->vtime - now_times;
618 int diff = (int)(vt);
620 abuf_appendf(abuf, "%s\t%s\t%d.%03d\n",
621 olsr_ip_to_string(&buf, &entry->main_addr),
622 olsr_ip_to_string(&buf2, &alias->alias),
623 diff/1000, abs(diff%1000));
624 #else /* ACTIVATE_VTIME_TXTINFO */
625 abuf_appendf(abuf, "%s%s", (is_first ? "\t" : ";"), olsr_ip_to_string(&buf, &alias->alias));
626 #endif /* ACTIVATE_VTIME_TXTINFO */
627 alias = alias->next_alias;
631 #ifndef ACTIVATE_VTIME_TXTINFO
632 abuf_puts(abuf, "\n");
633 #endif /* ACTIVATE_VTIME_TXTINFO */
636 abuf_puts(abuf, "\n");
639 static void ipc_print_gateways(struct autobuf *abuf) {
641 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
642 #else /* __linux__ */
643 static const char IPV4[] = "ipv4";
644 static const char IPV4_NAT[] = "ipv4(n)";
645 static const char IPV6[] = "ipv6";
646 static const char NONE[] = "-";
648 struct ipaddr_str buf;
649 struct gateway_entry *gw;
650 struct lqtextbuffer lqbuf;
652 // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
653 abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
654 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
656 char v4 = '-', v6 = '-';
657 const char *v4type = NONE, *v6type = NONE;
660 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
664 if (gw == olsr_get_inet_gateway(false)) {
666 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
670 if (gw == olsr_get_inet_gateway(true)) {
672 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
677 v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
683 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),
684 get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops, gw->uplink, gw->downlink, v4type, v6type,
685 gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
686 }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
687 #endif /* __linux__ */
692 /** interface names for smart gateway tunnel interfaces, IPv4 */
693 extern struct interfaceName * sgwTunnel4InterfaceNames;
695 /** interface names for smart gateway tunnel interfaces, IPv6 */
696 extern struct interfaceName * sgwTunnel6InterfaceNames;
699 * Construct the sgw table for a given ip version
701 * @param abuf the string buffer
702 * @param ipv6 true for IPv6, false for IPv4
703 * @param fmtv the format for printing
705 static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const char * fmtv) {
706 struct interfaceName * sgwTunnelInterfaceNames;
708 abuf_appendf(abuf, "# Table: Smart Gateway IPv%s\n", ipv6 ? "6" : "4");
709 abuf_appendf(abuf, fmth, "#", "Originator", "Prefix", "Uplink", "Downlink", "PathCost", "IPv4", "IPv4-NAT", "IPv6", "Tunnel-Name", "Destination", "Cost");
711 sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
712 if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
713 struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
715 for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
716 struct interfaceName * node = &sgwTunnelInterfaceNames[i];
717 struct gateway_entry * gw = node->gw;
724 struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
726 struct ipaddr_str originatorStr;
727 const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
728 struct ipaddr_str prefixIpStr;
729 const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
730 union olsr_ip_addr netmask = { { 0 } };
731 struct ipaddr_str prefixMaskStr;
732 const char * prefixMASKStr;
733 char prefixAndMask[strlen(prefix) + 1 + INET_ADDRSTRLEN + 1];
736 prefix_to_netmask((uint8_t *) &netmask, sizeof(netmask.v4), gw->external_prefix.prefix_len);
737 prefixMASKStr = olsr_ip_to_string(&prefixMaskStr, &netmask);
738 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%s", prefix, prefixMASKStr);
740 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%d", prefix, gw->external_prefix.prefix_len);
743 abuf_appendf(abuf, fmtv, //
744 (current_gw && (current_gw == gw)) ? "*" : " ", // selected
745 originator, // Originator
746 prefixAndMask, // 4: Prefix IP / Prefix Mask, 6: Prefix IP / Prefix Length
747 gw->uplink, // Uplink
748 gw->downlink, // Downlink
749 !tc ? ROUTE_COST_BROKEN : tc->path_cost, // PathCost
750 gw->ipv4 ? "Y" : "N", // IPv4
751 gw->ipv4nat ? "Y" : "N", // IPv4-NAT
752 gw->ipv6 ? "Y" : "N", // IPv6
753 node->name, // Tunnel-Name
754 originator, // Destination
755 gw->path_cost // Cost
761 #endif /* __linux__ */
763 static void ipc_print_sgw(struct autobuf *abuf) {
765 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
768 static const char * fmth4 = "%s%-15s %-31s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-15s %s\n";
769 static const char * fmtv4 = "%s%-15s %-31s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-15s %lld\n";
771 static const char * fmth6 = "%s%-45s %-49s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-45s %s\n";
772 static const char * fmtv6 = "%s%-45s %-49s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-45s %lld\n";
775 sgw_ipvx(abuf, false, fmth4, fmtv4);
776 abuf_puts(abuf, "\n");
778 sgw_ipvx(abuf, true, fmth6, fmtv6);
779 abuf_puts(abuf, "\n");
781 #endif /* __linux__ */
784 static void ipc_print_version(struct autobuf *abuf) {
785 abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
788 static void ipc_print_olsrd_conf(struct autobuf *abuf) {
789 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
792 static void ipc_print_interfaces(struct autobuf *abuf) {
793 const struct olsr_if *ifs;
794 abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
795 for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
796 const struct interface_olsr * const rifs = ifs->interf;
797 abuf_appendf(abuf, "%s\t", ifs->name);
799 abuf_puts(abuf, "DOWN\n");
802 abuf_appendf(abuf, "UP\t%d\t%s\t", rifs->int_mtu, rifs->is_wireless ? "Yes" : "No");
804 if (olsr_cnf->ip_version == AF_INET) {
805 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
806 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),
807 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
809 struct ipaddr_str addrbuf, maskbuf;
810 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));
813 abuf_puts(abuf, "\n");
816 static void info_write_data(void *foo __attribute__ ((unused))) {
818 int result, i, j, max;
823 for (i = 0; i < outbuffer_count; i++) {
824 /* And we cast here since we get a warning on Win32 */
825 FD_SET((unsigned int )(outbuffer_socket[i]), &set);
827 if (outbuffer_socket[i] > max) {
828 max = outbuffer_socket[i];
835 result = select(max + 1, NULL, &set, NULL, &tv);
840 for (i = 0; i < outbuffer_count; i++) {
841 if (FD_ISSET(outbuffer_socket[i], &set)) {
842 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
844 outbuffer_written[i] += result;
847 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
848 /* close this socket and cleanup*/
849 close(outbuffer_socket[i]);
852 for (j = i + 1; j < outbuffer_count; j++) {
853 outbuffer[j - 1] = outbuffer[j];
854 outbuffer_size[j - 1] = outbuffer_size[j];
855 outbuffer_socket[j - 1] = outbuffer_socket[j];
856 outbuffer_written[j - 1] = outbuffer_written[j];
862 if (!outbuffer_count) {
863 olsr_stop_timer(writetimer_entry);
867 static void send_info(unsigned int send_what, int the_socket) {
870 const char *content_type = "text/plain; charset=utf-8";
871 int contentLengthPlaceholderStart = 0;
872 int headerLength = 0;
874 abuf_init(&abuf, 2 * 4096);
877 build_http_header(HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
878 headerLength = abuf.len;
881 // only add if normal format
882 if (send_what & SIW_ALL) {
883 if (send_what & SIW_LINKS)
884 ipc_print_links(&abuf);
885 if (send_what & SIW_NEIGHBORS)
886 ipc_print_neighbors(&abuf, false);
887 if (send_what & SIW_TOPOLOGY)
888 ipc_print_topology(&abuf);
889 if (send_what & SIW_HNA)
890 ipc_print_hna(&abuf);
891 if (send_what & SIW_SGW)
892 ipc_print_sgw(&abuf);
893 if (send_what & SIW_MID)
894 ipc_print_mid(&abuf);
895 if (send_what & SIW_ROUTES)
896 ipc_print_routes(&abuf);
897 if (send_what & SIW_GATEWAYS)
898 ipc_print_gateways(&abuf);
899 if (send_what & SIW_CONFIG) {
902 if (send_what & SIW_INTERFACES)
903 ipc_print_interfaces(&abuf);
904 if (send_what & SIW_2HOP)
905 ipc_print_neighbors(&abuf, true);
906 if (send_what & SIW_VERSION)
907 ipc_print_version(&abuf);
908 if (send_what & SIW_PLUGINS) {
911 } else if (send_what & SIW_OLSRD_CONF) {
912 /* this outputs the olsrd.conf text directly, not normal format */
913 ipc_print_olsrd_conf(&abuf);
917 http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
920 /* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
921 outbuffer[outbuffer_count] = abuf.buf;
922 outbuffer_size[outbuffer_count] = abuf.len;
923 outbuffer_written[outbuffer_count] = 0;
924 outbuffer_socket[outbuffer_count] = the_socket;
931 if (outbuffer_count == 1) {
932 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
943 * indent-tabs-mode: nil