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"))
376 /* print out every combinations of requested tabled
377 * 3++ letter abbreviations are matched */
378 if (strstr(requ, "/nei"))
379 send_what |= SIW_NEIGHBORS;
380 if (strstr(requ, "/lin"))
381 send_what |= SIW_LINKS;
382 if (strstr(requ, "/rou"))
383 send_what |= SIW_ROUTES;
384 if (strstr(requ, "/hna"))
385 send_what |= SIW_HNA;
386 if (strstr(requ, "/mid"))
387 send_what |= SIW_MID;
388 if (strstr(requ, "/top"))
389 send_what |= SIW_TOPOLOGY;
390 if (strstr(requ, "/gat"))
391 send_what |= SIW_GATEWAYS;
392 if (strstr(requ, "/int"))
393 send_what |= SIW_INTERFACES;
394 if (strstr(requ, "/2ho"))
395 send_what |= SIW_2HOP;
396 if (strstr(requ, "/sgw"))
397 send_what |= SIW_SGW;
400 if (strstr(requ, "/ver"))
401 send_what |= SIW_VERSION;
403 /* To print out neighbours only on the Freifunk Status
404 * page the normal output is somewhat lengthy. The
405 * header parsing is sufficient for standard wget.
407 if (strstr(requ, "/neighbours"))
408 send_what = SIW_NEIGHBORS | SIW_LINKS;
416 send_info(send_what, ipc_connection);
419 static void ipc_print_neighbors(struct autobuf *abuf, bool list_2hop) {
420 struct ipaddr_str buf1;
421 struct neighbor_entry *neigh;
422 struct neighbor_2_list_entry *list_2;
425 abuf_puts(abuf, "Table: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWill.");
427 abuf_puts(abuf, "\n\t2hop interface adrress\n");
429 abuf_puts(abuf, "\t2 Hop Neighbors\n");
432 OLSR_FOR_ALL_NBR_ENTRIES(neigh)
434 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",
435 neigh->is_mpr ? "YES" : "NO", olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO", neigh->willingness);
438 for (list_2 = neigh->neighbor_2_list.next; list_2 != &neigh->neighbor_2_list; list_2 = list_2->next) {
440 abuf_appendf(abuf, "\t%s\n", olsr_ip_to_string(&buf1, &list_2->neighbor_2->neighbor_2_addr));
445 abuf_appendf(abuf, "%d\n", thop_cnt);
447 }OLSR_FOR_ALL_NBR_ENTRIES_END(neigh);
448 abuf_puts(abuf, "\n");
451 static void ipc_print_links(struct autobuf *abuf) {
452 struct ipaddr_str buf1, buf2;
453 struct lqtextbuffer lqbuffer1, lqbuffer2;
455 struct link_entry *my_link = NULL;
457 #ifdef ACTIVATE_VTIME_TXTINFO
458 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tVTime\tLQ\tNLQ\tCost\n");
459 #else /* ACTIVATE_VTIME_TXTINFO */
460 abuf_puts(abuf, "Table: Links\nLocal IP\tRemote IP\tHyst.\tLQ\tNLQ\tCost\n");
461 #endif /* ACTIVATE_VTIME_TXTINFO */
464 OLSR_FOR_ALL_LINK_ENTRIES(my_link)
466 #ifdef ACTIVATE_VTIME_TXTINFO
467 int diff = (unsigned int)(my_link->link_timer->timer_clock - now_times);
469 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),
470 olsr_ip_to_string(&buf2, &my_link->neighbor_iface_addr),
471 diff/1000, abs(diff%1000),
472 get_link_entry_text(my_link, '\t', &lqbuffer1),
473 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
474 #else /* ACTIVATE_VTIME_TXTINFO */
475 abuf_appendf(abuf, "%s\t%s\t0.00\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), get_link_entry_text(my_link, '\t', &lqbuffer1),
477 get_linkcost_text(my_link->linkcost, false, &lqbuffer2));
478 #endif /* ACTIVATE_VTIME_TXTINFO */
479 }OLSR_FOR_ALL_LINK_ENTRIES_END(my_link);
481 abuf_puts(abuf, "\n");
484 static void ipc_print_routes(struct autobuf *abuf) {
485 struct ipaddr_str buf1, buf2;
487 struct lqtextbuffer lqbuffer;
489 abuf_puts(abuf, "Table: Routes\nDestination\tGateway IP\tMetric\tETX\tInterface\n");
491 /* Walk the route table */
492 OLSR_FOR_ALL_RT_ENTRIES(rt)
494 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,
495 olsr_ip_to_string(&buf2, &rt->rt_best->rtp_nexthop.gateway), rt->rt_best->rtp_metric.hops,
496 get_linkcost_text(rt->rt_best->rtp_metric.cost, true, &lqbuffer), if_ifwithindex_name(rt->rt_best->rtp_nexthop.iif_index));
497 }OLSR_FOR_ALL_RT_ENTRIES_END(rt);
499 abuf_puts(abuf, "\n");
503 static void ipc_print_topology(struct autobuf *abuf) {
506 #ifdef ACTIVATE_VTIME_TXTINFO
507 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\tVTime\n");
508 #else /* ACTIVATE_VTIME_TXTINFO */
509 abuf_puts(abuf, "Table: Topology\nDest. IP\tLast hop IP\tLQ\tNLQ\tCost\n");
510 #endif /* ACTIVATE_VTIME_TXTINFO */
513 OLSR_FOR_ALL_TC_ENTRIES(tc)
515 struct tc_edge_entry *tc_edge;
516 OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge)
518 if (tc_edge->edge_inv) {
519 struct ipaddr_str dstbuf, addrbuf;
520 struct lqtextbuffer lqbuffer1, lqbuffer2;
521 #ifdef ACTIVATE_VTIME_TXTINFO
522 uint32_t vt = tc->validity_timer != NULL ? (tc->validity_timer->timer_clock - now_times) : 0;
523 int diff = (int)(vt);
524 abuf_appendf(abuf, "%s\t%s\t%s\t%s\t%d.%03d\n", olsr_ip_to_string(&dstbuf, &tc_edge->T_dest_addr),
525 olsr_ip_to_string(&addrbuf, &tc->addr),
526 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1),
527 get_linkcost_text(tc_edge->cost, false, &lqbuffer2),
528 diff/1000, diff%1000);
529 #else /* ACTIVATE_VTIME_TXTINFO */
530 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),
531 get_tc_edge_entry_text(tc_edge, '\t', &lqbuffer1), get_linkcost_text(tc_edge->cost, false, &lqbuffer2));
532 #endif /* ACTIVATE_VTIME_TXTINFO */
534 }OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
535 }OLSR_FOR_ALL_TC_ENTRIES_END(tc);
537 abuf_puts(abuf, "\n");
540 static void ipc_print_hna(struct autobuf *abuf) {
541 struct ip_prefix_list *hna;
542 struct hna_entry *tmp_hna;
543 struct hna_net *tmp_net;
544 struct ipaddr_str buf, mainaddrbuf;
546 #ifdef ACTIVATE_VTIME_TXTINFO
547 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\tVTime\n");
548 #else /* ACTIVATE_VTIME_TXTINFO */
549 abuf_puts(abuf, "Table: HNA\nDestination\tGateway\n");
550 #endif /* ACTIVATE_VTIME_TXTINFO */
552 /* Announced HNA entries */
553 for (hna = olsr_cnf->hna_entries; hna != NULL ; hna = hna->next) {
554 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));
558 OLSR_FOR_ALL_HNA_ENTRIES(tmp_hna)
561 /* Check all networks */
562 for (tmp_net = tmp_hna->networks.next; tmp_net != &tmp_hna->networks; tmp_net = tmp_net->next) {
563 #ifdef ACTIVATE_VTIME_TXTINFO
564 uint32_t vt = tmp_net->hna_net_timer != NULL ? (tmp_net->hna_net_timer->timer_clock - now_times) : 0;
565 int diff = (int)(vt);
566 abuf_appendf(abuf, "%s/%d\t%s\t\%d.%03d\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix),
567 tmp_net->hna_prefix.prefix_len, olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr),
568 diff/1000, abs(diff%1000));
569 #else /* ACTIVATE_VTIME_TXTINFO */
570 abuf_appendf(abuf, "%s/%d\t%s\n", olsr_ip_to_string(&buf, &tmp_net->hna_prefix.prefix), tmp_net->hna_prefix.prefix_len,
571 olsr_ip_to_string(&mainaddrbuf, &tmp_hna->A_gateway_addr));
572 #endif /* ACTIVATE_VTIME_TXTINFO */
574 }OLSR_FOR_ALL_HNA_ENTRIES_END(tmp_hna);
576 abuf_puts(abuf, "\n");
579 static void ipc_print_mid(struct autobuf *abuf) {
581 unsigned short is_first;
582 struct mid_entry *entry;
583 struct mid_address *alias;
584 #ifdef ACTIVATE_VTIME_TXTINFO
585 abuf_puts(abuf, "Table: MID\nIP address\tAlias\tVTime\n");
586 #else /* ACTIVATE_VTIME_TXTINFO */
587 abuf_puts(abuf, "Table: MID\nIP address\tAliases\n");
588 #endif /* ACTIVATE_VTIME_TXTINFO */
591 for (idx = 0; idx < HASHSIZE; idx++) {
592 entry = mid_set[idx].next;
594 while (entry != &mid_set[idx]) {
595 #ifdef ACTIVATE_VTIME_TXTINFO
596 struct ipaddr_str buf, buf2;
597 #else /* ACTIVATE_VTIME_TXTINFO */
598 struct ipaddr_str buf;
599 abuf_puts(abuf, olsr_ip_to_string(&buf, &entry->main_addr));
600 #endif /* ACTIVATE_VTIME_TXTINFO */
601 alias = entry->aliases;
605 #ifdef ACTIVATE_VTIME_TXTINFO
606 uint32_t vt = alias->vtime - now_times;
607 int diff = (int)(vt);
609 abuf_appendf(abuf, "%s\t%s\t%d.%03d\n",
610 olsr_ip_to_string(&buf, &entry->main_addr),
611 olsr_ip_to_string(&buf2, &alias->alias),
612 diff/1000, abs(diff%1000));
613 #else /* ACTIVATE_VTIME_TXTINFO */
614 abuf_appendf(abuf, "%s%s", (is_first ? "\t" : ";"), olsr_ip_to_string(&buf, &alias->alias));
615 #endif /* ACTIVATE_VTIME_TXTINFO */
616 alias = alias->next_alias;
620 #ifndef ACTIVATE_VTIME_TXTINFO
621 abuf_puts(abuf, "\n");
622 #endif /* ACTIVATE_VTIME_TXTINFO */
625 abuf_puts(abuf, "\n");
628 static void ipc_print_gateways(struct autobuf *abuf) {
630 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
631 #else /* __linux__ */
632 static const char IPV4[] = "ipv4";
633 static const char IPV4_NAT[] = "ipv4(n)";
634 static const char IPV6[] = "ipv6";
635 static const char NONE[] = "-";
637 struct ipaddr_str buf;
638 struct gateway_entry *gw;
639 struct lqtextbuffer lqbuf;
641 // Status IP ETX Hopcount Uplink-Speed Downlink-Speed ipv4/ipv4-nat/- ipv6/- ipv6-prefix/-
642 abuf_puts(abuf, "Table: Gateways\nStatus\tGateway IP\tETX\tHopcnt\tUplink\tDownlnk\tIPv4\tIPv6\tPrefix\n");
643 OLSR_FOR_ALL_GATEWAY_ENTRIES(gw)
645 char v4 = '-', v6 = '-';
646 const char *v4type = NONE, *v6type = NONE;
649 if ((tc = olsr_lookup_tc_entry(&gw->originator)) == NULL) {
653 if (gw == olsr_get_inet_gateway(false)) {
655 } else if (gw->ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit) && (olsr_cnf->smart_gw_allow_nat || !gw->ipv4nat)) {
659 if (gw == olsr_get_inet_gateway(true)) {
661 } else if (gw->ipv6 && olsr_cnf->ip_version == AF_INET6) {
666 v4type = gw->ipv4nat ? IPV4_NAT : IPV4;
672 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),
673 get_linkcost_text(tc->path_cost, true, &lqbuf), tc->hops, gw->uplink, gw->downlink, v4type, v6type,
674 gw->external_prefix.prefix_len == 0 ? NONE : olsr_ip_prefix_to_string(&gw->external_prefix));
675 }OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
676 #endif /* __linux__ */
681 /** interface names for smart gateway tunnel interfaces, IPv4 */
682 extern struct interfaceName * sgwTunnel4InterfaceNames;
684 /** interface names for smart gateway tunnel interfaces, IPv6 */
685 extern struct interfaceName * sgwTunnel6InterfaceNames;
688 * Construct the sgw table for a given ip version
690 * @param abuf the string buffer
691 * @param ipv6 true for IPv6, false for IPv4
692 * @param fmtv the format for printing
694 static void sgw_ipvx(struct autobuf *abuf, bool ipv6, const char * fmth, const char * fmtv) {
695 struct interfaceName * sgwTunnelInterfaceNames;
697 abuf_appendf(abuf, "# Table: Smart Gateway IPv%s\n", ipv6 ? "6" : "4");
698 abuf_appendf(abuf, fmth, "#", "Originator", "Prefix", "Uplink", "Downlink", "PathCost", "IPv4", "IPv4-NAT", "IPv6", "Tunnel-Name", "Destination", "Cost");
700 sgwTunnelInterfaceNames = !ipv6 ? sgwTunnel4InterfaceNames : sgwTunnel6InterfaceNames;
701 if (olsr_cnf->smart_gw_active && sgwTunnelInterfaceNames) {
702 struct gateway_entry * current_gw = olsr_get_inet_gateway(ipv6);
704 for (i = 0; i < olsr_cnf->smart_gw_use_count; i++) {
705 struct interfaceName * node = &sgwTunnelInterfaceNames[i];
706 struct gateway_entry * gw = node->gw;
713 struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
715 struct ipaddr_str originatorStr;
716 const char * originator = olsr_ip_to_string(&originatorStr, &gw->originator);
717 struct ipaddr_str prefixIpStr;
718 const char * prefix = olsr_ip_to_string(&prefixIpStr, &gw->external_prefix.prefix);
719 union olsr_ip_addr netmask = { { 0 } };
720 struct ipaddr_str prefixMaskStr;
721 const char * prefixMASKStr;
722 char prefixAndMask[strlen(prefix) + 1 + INET_ADDRSTRLEN + 1];
725 prefix_to_netmask((uint8_t *) &netmask, sizeof(netmask.v4), gw->external_prefix.prefix_len);
726 prefixMASKStr = olsr_ip_to_string(&prefixMaskStr, &netmask);
727 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%s", prefix, prefixMASKStr);
729 snprintf(prefixAndMask, sizeof(prefixAndMask), "%s/%d", prefix, gw->external_prefix.prefix_len);
732 abuf_appendf(abuf, fmtv, //
733 (current_gw && (current_gw == gw)) ? "*" : " ", // selected
734 originator, // Originator
735 prefixAndMask, // 4: Prefix IP / Prefix Mask, 6: Prefix IP / Prefix Length
736 gw->uplink, // Uplink
737 gw->downlink, // Downlink
738 !tc ? ROUTE_COST_BROKEN : tc->path_cost, // PathCost
739 gw->ipv4 ? "Y" : "N", // IPv4
740 gw->ipv4nat ? "Y" : "N", // IPv4-NAT
741 gw->ipv6 ? "Y" : "N", // IPv6
742 node->name, // Tunnel-Name
743 originator, // Destination
744 gw->path_cost // Cost
750 #endif /* __linux__ */
752 static void ipc_print_sgw(struct autobuf *abuf) {
754 abuf_puts(abuf, "Gateway mode is only supported in linux\n");
757 static const char * fmth4 = "%s%-15s %-31s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-15s %s\n";
758 static const char * fmtv4 = "%s%-15s %-31s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-15s %lld\n";
760 static const char * fmth6 = "%s%-45s %-49s %-9s %-9s %-10s %-4s %-8s %-4s %-15s %-45s %s\n";
761 static const char * fmtv6 = "%s%-45s %-49s %-9u %-9u %-10u %-4s %-8s %-4s %-15s %-45s %lld\n";
764 sgw_ipvx(abuf, false, fmth4, fmtv4);
765 abuf_puts(abuf, "\n");
767 sgw_ipvx(abuf, true, fmth6, fmtv6);
768 abuf_puts(abuf, "\n");
770 #endif /* __linux__ */
773 static void ipc_print_version(struct autobuf *abuf) {
774 abuf_appendf(abuf, "Version: %s (built on %s on %s)\n", olsrd_version, build_date, build_host);
777 static void ipc_print_olsrd_conf(struct autobuf *abuf) {
778 olsrd_write_cnf_autobuf(abuf, olsr_cnf);
781 static void ipc_print_interfaces(struct autobuf *abuf) {
782 const struct olsr_if *ifs;
783 abuf_puts(abuf, "Table: Interfaces\nName\tState\tMTU\tWLAN\tSrc-Adress\tMask\tDst-Adress\n");
784 for (ifs = olsr_cnf->interfaces; ifs != NULL ; ifs = ifs->next) {
785 const struct interface_olsr * const rifs = ifs->interf;
786 abuf_appendf(abuf, "%s\t", ifs->name);
788 abuf_puts(abuf, "DOWN\n");
791 abuf_appendf(abuf, "UP\t%d\t%s\t", rifs->int_mtu, rifs->is_wireless ? "Yes" : "No");
793 if (olsr_cnf->ip_version == AF_INET) {
794 struct ipaddr_str addrbuf, maskbuf, bcastbuf;
795 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),
796 ip4_to_string(&bcastbuf, rifs->int_broadaddr.sin_addr));
798 struct ipaddr_str addrbuf, maskbuf;
799 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));
802 abuf_puts(abuf, "\n");
805 static void info_write_data(void *foo __attribute__ ((unused))) {
807 int result, i, j, max;
812 for (i = 0; i < outbuffer_count; i++) {
813 /* And we cast here since we get a warning on Win32 */
814 FD_SET((unsigned int )(outbuffer_socket[i]), &set);
816 if (outbuffer_socket[i] > max) {
817 max = outbuffer_socket[i];
824 result = select(max + 1, NULL, &set, NULL, &tv);
829 for (i = 0; i < outbuffer_count; i++) {
830 if (FD_ISSET(outbuffer_socket[i], &set)) {
831 result = send(outbuffer_socket[i], outbuffer[i] + outbuffer_written[i], outbuffer_size[i] - outbuffer_written[i], 0);
833 outbuffer_written[i] += result;
836 if (result <= 0 || outbuffer_written[i] == outbuffer_size[i]) {
837 /* close this socket and cleanup*/
838 close(outbuffer_socket[i]);
841 for (j = i + 1; j < outbuffer_count; j++) {
842 outbuffer[j - 1] = outbuffer[j];
843 outbuffer_size[j - 1] = outbuffer_size[j];
844 outbuffer_socket[j - 1] = outbuffer_socket[j];
845 outbuffer_written[j - 1] = outbuffer_written[j];
851 if (!outbuffer_count) {
852 olsr_stop_timer(writetimer_entry);
856 static void send_info(unsigned int send_what, int the_socket) {
859 const char *content_type = "text/plain; charset=utf-8";
860 int contentLengthPlaceholderStart = 0;
861 int headerLength = 0;
863 abuf_init(&abuf, 2 * 4096);
866 build_http_header(HTTP_200, content_type, &abuf, &contentLengthPlaceholderStart);
867 headerLength = abuf.len;
870 // only add if normal format
871 if (send_what & SIW_ALL) {
872 if (send_what & SIW_LINKS)
873 ipc_print_links(&abuf);
874 if (send_what & SIW_NEIGHBORS)
875 ipc_print_neighbors(&abuf, false);
876 if (send_what & SIW_TOPOLOGY)
877 ipc_print_topology(&abuf);
878 if (send_what & SIW_HNA)
879 ipc_print_hna(&abuf);
880 if (send_what & SIW_SGW)
881 ipc_print_sgw(&abuf);
882 if (send_what & SIW_MID)
883 ipc_print_mid(&abuf);
884 if (send_what & SIW_ROUTES)
885 ipc_print_routes(&abuf);
886 if (send_what & SIW_GATEWAYS)
887 ipc_print_gateways(&abuf);
888 if (send_what & SIW_INTERFACES)
889 ipc_print_interfaces(&abuf);
890 if (send_what & SIW_2HOP)
891 ipc_print_neighbors(&abuf, true);
892 if (send_what & SIW_VERSION)
893 ipc_print_version(&abuf);
894 } else if (send_what & SIW_OLSRD_CONF) {
895 /* this outputs the olsrd.conf text directly, not normal format */
896 ipc_print_olsrd_conf(&abuf);
900 http_header_adjust_content_length(&abuf, contentLengthPlaceholderStart, abuf.len - headerLength);
903 /* avoid a memcpy: just move the abuf.buf pointer and clear abuf */
904 outbuffer[outbuffer_count] = abuf.buf;
905 outbuffer_size[outbuffer_count] = abuf.len;
906 outbuffer_written[outbuffer_count] = 0;
907 outbuffer_socket[outbuffer_count] = the_socket;
914 if (outbuffer_count == 1) {
915 writetimer_entry = olsr_start_timer(100, 0, OLSR_TIMER_PERIODIC, &info_write_data, NULL, 0);
926 * indent-tabs-mode: nil