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_STARTUP_ALL (SIW_VERSION)
126 /* this is everything in normal format */
127 #define SIW_ALL (SIW_RUNTIME_ALL | SIW_STARTUP_ALL)
129 /* this data is not normal format but olsrd.conf format */
130 #define SIW_OLSRD_CONF 0x2000
132 #define MAX_CLIENTS 3
134 static char *outbuffer[MAX_CLIENTS];
135 static size_t outbuffer_size[MAX_CLIENTS];
136 static size_t outbuffer_written[MAX_CLIENTS];
137 static int outbuffer_socket[MAX_CLIENTS];
138 static int outbuffer_count = 0;
140 static struct timer_entry *writetimer_entry;
142 static void build_http_header(const char *status, const char *mime, struct autobuf *abuf, int *contentLengthPlaceholderStart) {
144 abuf_appendf(abuf, "%s\r\n", status);
152 if (strftime(buf, sizeof(buf), "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", gmtime(&currtime))) {
153 abuf_puts(abuf, buf);
158 abuf_puts(abuf, "Server: OLSRD "PLUGIN_NAME"\r\n");
160 /* connection-type */
161 abuf_puts(abuf, "Connection: close\r\n");
165 abuf_appendf(abuf, "Content-Type: %s\r\n", mime);
169 /* No needs to be strict here, access control is based on source IP */
170 abuf_puts(abuf, "Access-Control-Allow-Origin: *\r\n");
171 abuf_puts(abuf, "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
172 abuf_puts(abuf, "Access-Control-Allow-Headers: Accept, Origin, X-Requested-With\r\n");
173 abuf_puts(abuf, "Access-Control-Max-Age: 1728000\r\n");
176 abuf_puts(abuf, "Content-Length: ");
177 *contentLengthPlaceholderStart = abuf->len;
178 abuf_puts(abuf, " "); /* 12 spaces reserved for the length (max. 1TB-1), to be filled at the end */
179 abuf_puts(abuf, "\r\n");
182 * No caching dynamic pages
184 abuf_puts(abuf, "Cache-Control: no-cache\r\n");
187 abuf_puts(abuf, "\r\n");
190 static void http_header_adjust_content_length(struct autobuf *abuf, int contentLengthPlaceholderStart, int contentLength) {
191 char buf[12 + 1]; /* size must match to number of spaces used (+1 for the terminating byte) */
193 memset(buf, 0, sizeof(buf));
194 snprintf(buf, sizeof(buf), "%d", contentLength);
195 buf[sizeof(buf) - 1] = '\0';
196 memcpy(&abuf->buf[contentLengthPlaceholderStart], buf, strlen(buf));
200 *Do initialization here
202 *This function is called by the my_init
203 *function in uolsrd_plugin.c
205 int olsrd_plugin_init(void) {
206 /* Initial IPC value */
214 * destructor - called at unload
216 void olsr_plugin_exit(void) {
217 if (ipc_socket != -1)
221 static int plugin_ipc_init(void) {
222 union olsr_sockaddr sst;
226 /* Init ipc socket */
227 if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {
229 olsr_printf(1, "("PLUGIN_NAME") socket()=%s\n", strerror(errno));
233 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)) < 0) {
235 olsr_printf(1, "("PLUGIN_NAME") setsockopt()=%s\n", strerror(errno));
239 #if (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE
240 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *) &yes, sizeof(yes)) < 0) {
241 perror("SO_REUSEADDR failed");
244 #endif /* (defined __FreeBSD__ || defined __FreeBSD_kernel__) && defined SO_NOSIGPIPE */
245 #if defined linux && defined IPV6_V6ONLY
246 if (info_ipv6_only && olsr_cnf->ip_version == AF_INET6) {
247 if (setsockopt(ipc_socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &yes, sizeof(yes)) < 0) {
248 perror("IPV6_V6ONLY failed");
252 #endif /* defined linux && defined IPV6_V6ONLY */
253 /* Bind the socket */
255 /* complete the socket structure */
256 memset(&sst, 0, sizeof(sst));
257 if (olsr_cnf->ip_version == AF_INET) {
258 sst.in4.sin_family = AF_INET;
259 addrlen = sizeof(struct sockaddr_in);
261 sst.in4.sin_len = addrlen;
262 #endif /* SIN6_LEN */
263 sst.in4.sin_addr.s_addr = info_listen_ip.v4.s_addr;
264 sst.in4.sin_port = htons(ipc_port);
266 sst.in6.sin6_family = AF_INET6;
267 addrlen = sizeof(struct sockaddr_in6);
269 sst.in6.sin6_len = addrlen;
270 #endif /* SIN6_LEN */
271 sst.in6.sin6_addr = info_listen_ip.v6;
272 sst.in6.sin6_port = htons(ipc_port);
275 /* bind the socket to the port number */
276 if (bind(ipc_socket, &sst.in, addrlen) == -1) {
278 olsr_printf(1, "("PLUGIN_NAME") bind()=%s\n", strerror(errno));
283 /* show that we are willing to listen */
284 if (listen(ipc_socket, 1) == -1) {
286 olsr_printf(1, "("PLUGIN_NAME") listen()=%s\n", strerror(errno));
291 /* Register with olsrd */
292 add_olsr_socket(ipc_socket, &ipc_action, NULL, NULL, SP_PR_READ);
295 olsr_printf(2, "("PLUGIN_NAME") listening on port %d\n", ipc_port);
301 static void ipc_action(int fd, void *data __attribute__ ((unused)), unsigned int flags __attribute__ ((unused))) {
302 union olsr_sockaddr pin;
304 char addr[INET6_ADDRSTRLEN];
307 unsigned int send_what = 0;
310 socklen_t addrlen = sizeof(pin);
312 if (outbuffer_count >= MAX_CLIENTS) {
316 if ((ipc_connection = accept(fd, &pin.in, &addrlen)) == -1) {
318 olsr_printf(1, "("PLUGIN_NAME") accept()=%s\n", strerror(errno));
323 tv.tv_sec = tv.tv_usec = 0;
324 if (olsr_cnf->ip_version == AF_INET) {
325 if (inet_ntop(olsr_cnf->ip_version, &pin.in4.sin_addr, addr, INET6_ADDRSTRLEN) == NULL)
327 if (!ip4equal(&pin.in4.sin_addr, &info_accept_ip.v4) && info_accept_ip.v4.s_addr != INADDR_ANY) {
328 #ifdef INFO_ALLOW_LOCALHOST
329 if (ntohl(pin.in4.sin_addr.s_addr) != INADDR_LOOPBACK) {
330 #endif /* INFO_ALLOW_LOCALHOST */
331 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
332 close(ipc_connection);
334 #ifdef INFO_ALLOW_LOCALHOST
336 #endif /* INFO_ALLOW_LOCALHOST */
339 if (inet_ntop(olsr_cnf->ip_version, &pin.in6.sin6_addr, addr, INET6_ADDRSTRLEN) == NULL)
341 /* Use in6addr_any (::) in olsr.conf to allow anybody. */
342 if (!ip6equal(&in6addr_any, &info_accept_ip.v6) && !ip6equal(&pin.in6.sin6_addr, &info_accept_ip.v6)) {
343 olsr_printf(1, "("PLUGIN_NAME") From host(%s) not allowed!\n", addr);
344 close(ipc_connection);
350 olsr_printf(2, "("PLUGIN_NAME") Connect from %s\n", addr);
353 /* purge read buffer to prevent blocking on linux */
355 FD_SET((unsigned int )ipc_connection, &rfds); /* Win32 needs the cast here */
356 if (0 <= select(ipc_connection + 1, &rfds, NULL, NULL, &tv)) {
358 ssize_t s = recv(ipc_connection, (void *) &requ, sizeof(requ) - 1, 0); /* Win32 needs the cast here */
360 if (s == sizeof(requ) - 1) {
361 /* input was much too long, just skip the rest */
364 while (recv(ipc_connection, (void *) &dummy, sizeof(dummy), 0) == sizeof(dummy))
370 /* print out the requested tables */
371 if (strstr(requ, "/con"))
372 send_what |= SIW_OLSRD_CONF;
373 else if (strstr(requ, "/all"))
374 send_what = SIW_RUNTIME_ALL;
375 /* To print out neighbours only on the Freifunk Status
376 * page the normal output is somewhat lengthy. The
377 * header parsing is sufficient for standard wget.
379 else if (strstr(requ, "/neighbours"))
380 send_what = SIW_NEIGHBORS | SIW_LINKS;
382 /* print out every combinations of requested tabled
383 * 3++ letter abbreviations are matched */
384 if (strstr(requ, "/nei"))
385 send_what |= SIW_NEIGHBORS;
386 if (strstr(requ, "/lin"))
387 send_what |= SIW_LINKS;
388 if (strstr(requ, "/rou"))
389 send_what |= SIW_ROUTES;
390 if (strstr(requ, "/hna"))
391 send_what |= SIW_HNA;
392 if (strstr(requ, "/mid"))
393 send_what |= SIW_MID;
394 if (strstr(requ, "/top"))
395 send_what |= SIW_TOPOLOGY;
396 if (strstr(requ, "/gat"))
397 send_what |= SIW_GATEWAYS;
398 if (strstr(requ, "/int"))
399 send_what |= SIW_INTERFACES;
400 if (strstr(requ, "/2ho"))
401 send_what |= SIW_2HOP;
402 if (strstr(requ, "/ver"))
403 send_what |= SIW_VERSION;
404 if (strstr(requ, "/sgw"))
405 send_what |= SIW_SGW;
413 send_info(send_what, ipc_connection);
416 static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
417 struct ipaddr_str buf1;
418 struct neighbor_entry *neigh;
419 struct neighbor_2_list_entry *list_2;
422 abuf_puts(abuf, "Table: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWill.");
424 abuf_puts(abuf, "\n\t2hop interface adrress\n");
426 abuf_puts(abuf, "\t2 Hop Neighbors\n");
429 OLSR_FOR_ALL_NBR_ENTRIES(neigh)
431 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",
432 neigh->is_mpr ? "YES" : "NO", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO", neigh->willingness);
435 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
437 abuf_appendf(abuf, "\t%s\n", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
442 abuf_appendf(abuf, "%d\n", thop_cnt);
444 }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
445 abuf_puts(abuf, "\n");
448 static void ipc_print_links(struct autobuf *abuf) {
449 struct ipaddr_str buf1, buf2;
450 struct lqtextbuffer lqbuffer1, lqbuffer2;
452 struct link_entry *my_link = NULL;
454 #ifdef ACTIVATE_VTIME_TXTINFO
455 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tVTime\tLQ\tNLQ\tCost\n");
456 #else /* ACTIVATE_VTIME_TXTINFO */
457 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tHyst.\tLQ\tNLQ\tCost\n");
458 #endif /* ACTIVATE_VTIME_TXTINFO */
461 OLSR_FOR_ALL_LINK_ENTRIES(my_link)
463 #ifdef ACTIVATE_VTIME_TXTINFO
464 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
466 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),
467 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
468 diff/1000, abs(diff%1000),
469 get_link_entry_text(my_link, '\t', &lqbuffer1),
470 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
471 #else /* ACTIVATE_VTIME_TXTINFO */
472 abuf_appendf(abuf, "%s\t%s\t0.00\t%s\t%s\t\n", olsr_ip_to_string(&buf1, &my_link->local_iface_addr),
473 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr), get_link_entry_text(my_link, '\t', &lqbuffer1),
474 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
475 #endif /* ACTIVATE_VTIME_TXTINFO */
476 }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
478 abuf_puts(abuf, "\n");
481 static void ipc_print_routes(struct autobuf *abuf) {
482 struct ipaddr_str buf1, buf2;
484 struct lqtextbuffer lqbuffer;
486 abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
488 /* Walk the route table */
489 OLSR_FOR_ALL_RT_ENTRIES(rt)
491 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,
492 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway), rt->rt_best->rtp_metric.hops,
493 get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer), if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
494 }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
496 abuf_puts(abuf, "\n");
500 static void ipc_print_topology(struct autobuf *abuf) {
503 #ifdef ACTIVATE_VTIME_TXTINFO
504 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
505 #else /* ACTIVATE_VTIME_TXTINFO */
506 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\n");
507 #endif /* ACTIVATE_VTIME_TXTINFO */
510 OLSR_FOR_ALL_TC_ENTRIES(tc)
512 struct tc_edge_entry *tc_edge;
513 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
515 if (tc_edge->edge_inv) {
516 struct ipaddr_str dstbuf, addrbuf;
517 struct lqtextbuffer lqbuffer1, lqbuffer2;
518 #ifdef ACTIVATE_VTIME_TXTINFO
519 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
520 int diff = (int)(vt);
521 abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d.%03d\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr),
522 olsr_ip_to_string(&addrbuf, &tc->addr),
523 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1),
524 get_linkcost_text(tc_edge->cost, false, &lqbuffer2),
525 diff/1000, diff%1000);
526 #else /* ACTIVATE_VTIME_TXTINFO */
527 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),
528 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1), get_linkcost_text(tc_edge->cost, false, &lqbuffer2));
529 #endif /* ACTIVATE_VTIME_TXTINFO */
531 }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
532 }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
534 abuf_puts(abuf, "\n");
537 static void ipc_print_hna(struct autobuf *abuf) {
538 struct ip_prefix_list *hna;
539 struct hna_entry *tmp_hna;
540 struct hna_net *tmp_net;
541 struct ipaddr_str buf, mainaddrbuf;
543 #ifdef ACTIVATE_VTIME_TXTINFO
544 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\tVTime\n");
545 #else /* ACTIVATE_VTIME_TXTINFO */
546 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\n");
547 #endif /* ACTIVATE_VTIME_TXTINFO */
549 /* Announced HNA entries */
550 for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
551 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));
555 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
558 /* Check all networks */
559 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
560 #ifdef ACTIVATE_VTIME_TXTINFO
561 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
562 int diff = (int)(vt);
563 abuf_appendf(abuf, "%s/%d\t%s\t\%d.%03d\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
564 tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr),
565 diff/1000, abs(diff%1000));
566 #else /* ACTIVATE_VTIME_TXTINFO */
567 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix), tmp_net->hna_prefix.prefix_len,
568 olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
569 #endif /* ACTIVATE_VTIME_TXTINFO */
571 }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
573 abuf_puts(abuf, "\n");
576 static void ipc_print_mid(struct autobuf *abuf) {
578 unsigned short is_first;
579 struct mid_entry *entry;
580 struct mid_address *alias;
581 #ifdef ACTIVATE_VTIME_TXTINFO
582 abuf_puts(abuf, "Table: MID\nIP address\tAlias\tVTime\n");
583 #else /* ACTIVATE_VTIME_TXTINFO */
584 abuf_puts(abuf, "Table: MID\nIP address\tAliases\n");
585 #endif /* ACTIVATE_VTIME_TXTINFO */
588 for (idx = 0; idx < HASHSIZE; idx++) {
589 entry = mid_set[idx].next;
591 while (entry != &mid_set[idx]) {
592 #ifdef ACTIVATE_VTIME_TXTINFO
593 struct ipaddr_str buf, buf2;
594 #else /* ACTIVATE_VTIME_TXTINFO */
595 struct ipaddr_str buf;
596 abuf_puts(abuf, olsr_ip_to_string(&buf, &entry->main_addr));
597 #endif /* ACTIVATE_VTIME_TXTINFO */
598 alias = entry->aliases;
602 #ifdef ACTIVATE_VTIME_TXTINFO
603 uint32_t vt = alias->vtime - now_times;
604 int diff = (int)(vt);
606 abuf_appendf(abuf, "%s\t%s\t%d.%03d\n",
607 olsr_ip_to_string(&buf, &entry->main_addr),
608 olsr_ip_to_string(&buf2, &alias->alias),
609 diff/1000, abs(diff%1000));
610 #else /* ACTIVATE_VTIME_TXTINFO */
611 abuf_appendf(abuf, "%s%s", (is_first ? "\t" : ";"), olsr_ip_to_string(&buf, &alias->alias));
612 #endif /* ACTIVATE_VTIME_TXTINFO */
613 alias = alias->next_alias;
617 #ifndef ACTIVATE_VTIME_TXTINFO
618 abuf_puts(abuf, "\n");
619 #endif /* ACTIVATE_VTIME_TXTINFO */
622 abuf_puts(abuf, "\n");
625 static void ipc_print_gateways(struct autobuf *abuf) {
627 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
628 #else /* __linux__ */
629 static const char IPV4[] = "ipv4";
630 static const char IPV4_NAT[] = "ipv4(n)";
631 static const char IPV6[] = "ipv6";
632 static const char NONE[] = "-";
634 struct ipaddr_str buf;
635 struct gateway_entry *gw;
636 struct lqtextbuffer lqbuf;
638 // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
639 abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
640 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
642 char v4 = '-', v6 = '-';
643 const char *v4type = NONE, *v6type = NONE;
646 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
650 if (gw == olsr_get_inet_gateway(false)) {
652 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
656 if (gw == olsr_get_inet_gateway(true)) {
658 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
663 v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
669 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),
670 get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops, gw->uplink, gw->downlink, v4type, v6type,
671 gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
672 }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
673 #endif /* __linux__ */
678 /** interface names for smart gateway tunnel interfaces, IPv4 */
679 extern struct interfaceName * sgwTunnel4InterfaceNames;
681 /** interface names for smart gateway tunnel interfaces, IPv6 */
682 extern struct interfaceName * sgwTunnel6InterfaceNames;
685 * Construct the sgw table for a given ip version
687 * @param abuf the string buffer
688 * @param ipv6 true for IPv6, false for IPv4
689 * @param fmtv the format for printing
691 static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const char * fmtv) {
692 struct interfaceName * sgwTunnelInterfaceNames;
694 abuf_appendf(abuf, "# Table: Smart Gateway IPv%s\n", ipv6 ? "6" : "4");
695 abuf_appendf(abuf, fmth, "#", "Originator", "Prefix", "Uplink", "Downlink", "PathCost", "IPv4", "IPv4-NAT", "IPv6", "Tunnel-Name", "Destination", "Cost");
697 sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
698 if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
699 struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
701 for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
702 struct interfaceName * node = &sgwTunnelInterfaceNames[i];
703 struct gateway_entry * gw = node->gw;
710 struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
712 struct ipaddr_str originatorStr;
713 const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
714 struct ipaddr_str prefixIpStr;
715 const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
716 union olsr_ip_addr netmask = { { 0 } };
717 struct ipaddr_str prefixMaskStr;
718 const char * prefixMASKStr;
719 char prefixAndMask[strlen(prefix) + 1 + INET_ADDRSTRLEN + 1];
722 prefix_to_netmask((uint8_t *) &netmask, sizeof(netmask.v4), gw->external_prefix.prefix_len);
723 prefixMASKStr = olsr_ip_to_string(&prefixMaskStr, &netmask);
724 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%s", prefix, prefixMASKStr);
726 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%d", prefix, gw->external_prefix.prefix_len);
729 abuf_appendf(abuf, fmtv, //
730 (current_gw && (current_gw == gw)) ? "*" : " ", // selected
731 originator, // Originator
732 prefixAndMask, // 4: Prefix IP / Prefix Mask, 6: Prefix IP / Prefix Length
733 gw->uplink, // Uplink
734 gw->downlink, // Downlink
735 !tc ? ROUTE_COST_BROKEN : tc->path_cost, // PathCost
736 gw->ipv4 ? "Y" : "N", // IPv4
737 gw->ipv4nat ? "Y" : "N", // IPv4-NAT
738 gw->ipv6 ? "Y" : "N", // IPv6
739 node->name, // Tunnel-Name
740 originator, // Destination
741 gw->path_cost // Cost
747 #endif /* __linux__ */
749 static void ipc_print_sgw(struct autobuf *abuf) {
751 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
754 static const char * fmth4 = "%s%-15s %-31s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-15s %s\n";
755 static const char * fmtv4 = "%s%-15s %-31s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-15s %lld\n";
757 static const char * fmth6 = "%s%-45s %-49s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-45s %s\n";
758 static const char * fmtv6 = "%s%-45s %-49s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-45s %lld\n";
761 sgw_ipvx(abuf, false, fmth4, fmtv4);
762 abuf_puts(abuf, "\n");
764 sgw_ipvx(abuf, true, fmth6, fmtv6);
765 abuf_puts(abuf, "\n");
767 #endif /* __linux__ */
770 static void ipc_print_version(struct autobuf *abuf) {
771 abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
774 static void ipc_print_olsrd_conf(struct autobuf *abuf) {
775 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
778 static void ipc_print_interfaces(struct autobuf *abuf) {
779 const struct olsr_if *ifs;
780 abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
781 for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
782 const struct interface_olsr * const rifs = ifs->interf;
783 abuf_appendf(abuf, "%s\t", ifs->name);
785 abuf_puts(abuf, "DOWN\n");
788 abuf_appendf(abuf, "UP\t%d\t%s\t", rifs->int_mtu, rifs->is_wireless ? "Yes" : "No");
790 if (olsr_cnf->ip_version == AF_INET) {
791 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
792 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),
793 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
795 struct ipaddr_str addrbuf, maskbuf;
796 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));
799 abuf_puts(abuf, "\n");
802 static void info_write_data(void *foo __attribute__ ((unused))) {
804 int result, i, j, max;
809 for (i = 0; i < outbuffer_count; i++) {
810 /* And we cast here since we get a warning on Win32 */
811 FD_SET((unsigned int )(outbuffer_socket[i]), &set);
813 if (outbuffer_socket[i] > max) {
814 max = outbuffer_socket[i];
821 result = select(max + 1, NULL, &set, NULL, &tv);
826 for (i = 0; i < outbuffer_count; i++) {
827 if (FD_ISSET(outbuffer_socket[i], &set)) {
828 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
830 outbuffer_written[i] += result;
833 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
834 /* close this socket and cleanup*/
835 close(outbuffer_socket[i]);
838 for (j = i + 1; j < outbuffer_count; j++) {
839 outbuffer[j - 1] = outbuffer[j];
840 outbuffer_size[j - 1] = outbuffer_size[j];
841 outbuffer_socket[j - 1] = outbuffer_socket[j];
842 outbuffer_written[j - 1] = outbuffer_written[j];
848 if (!outbuffer_count) {
849 olsr_stop_timer(writetimer_entry);
853 static void send_info(unsigned int send_what, int the_socket) {
856 const char *content_type = "text/plain; charset=utf-8";
857 int contentLengthPlaceholderStart = 0;
858 int headerLength = 0;
860 abuf_init(&abuf, 2 * 4096);
863 build_http_header(HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
864 headerLength = abuf.len;
867 // only add if normal format
868 if (send_what & SIW_ALL) {
869 if (send_what & SIW_LINKS)
870 ipc_print_links(&abuf);
871 if (send_what & SIW_NEIGHBORS)
872 ipc_print_neighbors(&abuf, false);
873 if (send_what & SIW_TOPOLOGY)
874 ipc_print_topology(&abuf);
875 if (send_what & SIW_HNA)
876 ipc_print_hna(&abuf);
877 if (send_what & SIW_SGW)
878 ipc_print_sgw(&abuf);
879 if (send_what & SIW_MID)
880 ipc_print_mid(&abuf);
881 if (send_what & SIW_ROUTES)
882 ipc_print_routes(&abuf);
883 if (send_what & SIW_GATEWAYS)
884 ipc_print_gateways(&abuf);
885 if (send_what & SIW_INTERFACES)
886 ipc_print_interfaces(&abuf);
887 if (send_what & SIW_2HOP)
888 ipc_print_neighbors(&abuf, true);
889 if (send_what & SIW_VERSION)
890 ipc_print_version(&abuf);
891 } else if (send_what & SIW_OLSRD_CONF) {
892 /* this outputs the olsrd.conf text directly, not normal format */
893 ipc_print_olsrd_conf(&abuf);
897 http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
900 /* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
901 outbuffer[outbuffer_count] = abuf.buf;
902 outbuffer_size[outbuffer_count] = abuf.len;
903 outbuffer_written[outbuffer_count] = 0;
904 outbuffer_socket[outbuffer_count] = the_socket;
911 if (outbuffer_count == 1) {
912 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
923 * indent-tabs-mode: nil