2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tønnesen(andreto@olsr.org)
4 * includes code by Bruno Randolf
5 * includes code by Andreas Lopatic
6 * includes code by Sven-Ola Tuecke
7 * includes code by Lorenz Schori
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * * Neither the name of olsr.org, olsrd nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
37 * Visit http://www.olsr.org for more information.
39 * If you find this software useful feel free to make a donation
40 * to the project. For more information see the website or contact
41 * the copyright holders.
43 * $Id: olsrd_txtinfo.c,v 1.4 2007/04/22 19:54:31 bernd67 Exp $
47 * Dynamic linked library for the olsr.org olsr daemon
51 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <sys/select.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
69 #include "olsr_types.h"
70 #include "neighbor_table.h"
71 #include "two_hop_neighbor_table.h"
72 #include "mpr_selector_set.h"
77 #include "socket_parser.h"
79 #include "olsrd_txtinfo.h"
80 #include "olsrd_plugin.h"
84 #define close(x) closesocket(x)
88 static int ipc_socket;
90 static int ipc_connection;
91 static int ipc_socket_up;
94 /* IPC initialization function */
96 plugin_ipc_init(void);
99 send_info(int neighonly);
105 ipc_print_neigh_link(void);
108 ipc_print_routes(void);
111 ipc_print_topology(void);
119 #define TXT_IPC_BUFSIZE 256
121 ipc_sendf(const char* format, ...) __attribute__((format(printf, 1, 2)));
124 *Do initialization here
126 *This function is called by the my_init
127 *function in uolsrd_plugin.c
130 olsrd_plugin_init(void)
132 /* Initial IPC value */
142 * destructor - called at unload
145 olsr_plugin_exit(void)
154 plugin_ipc_init(void)
156 struct sockaddr_in sin;
159 /* Init ipc socket */
160 if ((ipc_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
163 olsr_printf(1, "(TXTINFO) socket()=%s\n", strerror(errno));
169 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
172 olsr_printf(1, "(TXTINFO) setsockopt()=%s\n", strerror(errno));
177 #if defined __FreeBSD__ && defined SO_NOSIGPIPE
178 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0)
180 perror("SO_REUSEADDR failed");
185 /* Bind the socket */
187 /* complete the socket structure */
188 memset(&sin, 0, sizeof(sin));
189 sin.sin_family = AF_INET;
190 sin.sin_addr.s_addr = INADDR_ANY;
191 sin.sin_port = htons(ipc_port);
193 /* bind the socket to the port number */
194 if (bind(ipc_socket, (struct sockaddr *) &sin, sizeof(sin)) == -1)
197 olsr_printf(1, "(TXTINFO) bind()=%s\n", strerror(errno));
202 /* show that we are willing to listen */
203 if (listen(ipc_socket, 1) == -1)
206 olsr_printf(1, "(TXTINFO) listen()=%s\n", strerror(errno));
211 /* Register with olsrd */
212 add_olsr_socket(ipc_socket, &ipc_action);
215 olsr_printf(2, "(TXTINFO) listening on port %d\n",ipc_port);
227 struct sockaddr_in pin;
231 addrlen = sizeof(struct sockaddr_in);
236 if ((ipc_connection = accept(fd, (struct sockaddr *) &pin, &addrlen)) == -1)
239 olsr_printf(1, "(TXTINFO) accept()=%s\n", strerror(errno));
245 addr = inet_ntoa(pin.sin_addr);
246 if(ntohl(pin.sin_addr.s_addr) != ntohl(ipc_accept_ip.s_addr))
248 olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr);
249 close(ipc_connection);
256 olsr_printf(2, "(TXTINFO) Connect from %s\n",addr);
259 /* purge read buffer to prevent blocking on linux*/
262 FD_SET(ipc_connection, &rfds);
263 struct timeval tv = {0,0};
265 if(select(ipc_connection+1, &rfds, NULL, NULL, &tv)) {
267 ssize_t s = recv(ipc_connection, &requ, sizeof(requ), 0);
270 /* To print out neighbours only on the Freifunk Status
271 * page the normal output is somewhat lengthy. The
272 * header parsing is sufficient for standard wget.
274 neighonly = (0 != strstr(requ, "/neighbours"));
278 send_info(neighonly);
280 close(ipc_connection);
287 ipc_print_neigh_link(void)
289 struct neighbor_entry *neigh;
290 struct neighbor_2_list_entry *list_2;
291 struct link_entry *link = NULL;
294 ipc_sendf("Table: Links\nLocal IP\tremote IP\tHysteresis\tLinkQuality\tlost\ttotal\tNLQ\tETX\n");
300 ipc_sendf( "%s\t%s\t%0.2f\t%0.2f\t%d\t%d\t%0.2f\t%0.2f\t\n",
301 olsr_ip_to_string(&link->local_iface_addr),
302 olsr_ip_to_string(&link->neighbor_iface_addr),
303 link->L_link_quality,
304 link->loss_link_quality,
307 link->neigh_link_quality,
308 (link->loss_link_quality * link->neigh_link_quality) ? 1.0 / (link->loss_link_quality * link->neigh_link_quality) : 0.0);
311 ipc_sendf("\nTable: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWillingness\t2 Hop Neighbors\n");
314 for(index=0;index<HASHSIZE;index++)
316 for(neigh = neighbortable[index].next;
317 neigh != &neighbortable[index];
321 "%s\t%s\t%s\t%s\t%d\t",
322 olsr_ip_to_string(&neigh->neighbor_main_addr),
323 (neigh->status == SYM) ? "YES" : "NO",
324 neigh->is_mpr ? "YES" : "NO",
325 olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO",
330 for(list_2 = neigh->neighbor_2_list.next;
331 list_2 != &neigh->neighbor_2_list;
332 list_2 = list_2->next)
334 //size += sprintf(&buf[size], "<option>%s</option>\n", olsr_ip_to_string(&list_2->neighbor_2->neighbor_2_addr));
337 ipc_sendf("%d\n", thop_cnt);
346 ipc_print_routes(void)
349 struct rt_entry *routes;
351 ipc_sendf("Table: Routes\nDestination\tGateway\tMetric\tETX\tInterface\tType\n");
354 for(index = 0;index < HASHSIZE;index++)
356 for(routes = routingtable[index].next;
357 routes != &routingtable[index];
358 routes = routes->next)
361 ipc_sendf( "%s\t%s\t%d\t%.2f\t%s\tHOST\n",
362 olsr_ip_to_string(&routes->rt_dst),
363 olsr_ip_to_string(&routes->rt_router),
366 routes->rt_if->int_name);
371 for(index = 0;index < HASHSIZE;index++)
373 for(routes = hna_routes[index].next;
374 routes != &hna_routes[index];
375 routes = routes->next)
377 ipc_sendf("%s\t%s\t%d\t%s\t\tHNA\n",
378 olsr_ip_to_string(&routes->rt_dst),
379 olsr_ip_to_string(&routes->rt_router),
381 routes->rt_if->int_name);
390 ipc_print_topology(void)
393 struct tc_entry *entry;
394 struct topo_dst *dst_entry;
397 ipc_sendf("Table: Topology\nDestination IP\tLast hop IP\tLQ\tILQ\tETX\n");
400 for(index=0;index<HASHSIZE;index++)
402 /* For all TC entries */
403 entry = tc_table[index].next;
404 while(entry != &tc_table[index])
406 /* For all destination entries of that TC entry */
407 dst_entry = entry->destinations.next;
408 while(dst_entry != &entry->destinations)
410 ipc_sendf( "%s\t%s\t%0.2f\t%0.2f\t%0.2f\n",
411 olsr_ip_to_string(&dst_entry->T_dest_addr),
412 olsr_ip_to_string(&entry->T_last_addr),
413 dst_entry->link_quality,
414 dst_entry->inverse_link_quality,
415 (dst_entry->link_quality * dst_entry->inverse_link_quality) ? 1.0 / (dst_entry->link_quality * dst_entry->inverse_link_quality) : 0.0);
417 dst_entry = dst_entry->next;
431 struct hna_entry *tmp_hna;
432 struct hna_net *tmp_net;
436 ipc_sendf("Table: HNA\nNetwork\tNetmask\tGateway\n");
438 /* Announced HNA entries */
439 struct hna4_entry *hna4;
440 for(hna4 = olsr_cnf->hna4_entries; hna4; hna4 = hna4->next)
442 ipc_sendf("%s\t%s\t%s\n",
443 olsr_ip_to_string(&hna4->net),
444 olsr_ip_to_string(&hna4->netmask),
445 olsr_ip_to_string(&olsr_cnf->main_addr));
447 struct hna6_entry *hna6;
448 for(hna6 = olsr_cnf->hna6_entries; hna6; hna6 = hna6->next)
450 ipc_sendf("%s\t%d\t%s\n",
451 olsr_ip_to_string(&hna6->net),
453 olsr_ip_to_string(&olsr_cnf->main_addr));
457 for(index=0;index<HASHSIZE;index++)
459 tmp_hna = hna_set[index].next;
460 /* Check all entrys */
461 while(tmp_hna != &hna_set[index])
463 /* Check all networks */
464 tmp_net = tmp_hna->networks.next;
466 while(tmp_net != &tmp_hna->networks)
468 if (AF_INET == olsr_cnf->ip_version) {
469 ipc_sendf("%s\t%s\t%s\n",
470 olsr_ip_to_string(&tmp_net->A_network_addr),
471 olsr_ip_to_string((union olsr_ip_addr *)&tmp_net->A_netmask.v4),
472 olsr_ip_to_string(&tmp_hna->A_gateway_addr));
475 ipc_sendf("%s\t%d\t%s\n",
476 olsr_ip_to_string(&tmp_net->A_network_addr),
477 tmp_net->A_netmask.v6,
478 olsr_ip_to_string(&tmp_hna->A_gateway_addr));
480 tmp_net = tmp_net->next;
483 tmp_hna = tmp_hna->next;
495 unsigned short is_first;
496 struct mid_entry *entry;
497 struct mid_address *alias;
499 ipc_sendf("Table: MID\nIP\tAliases\n");
502 for( index = 0; index < HASHSIZE; index++ )
504 entry = mid_set[index].next;
506 while( entry != &mid_set[index] )
508 ipc_sendf( olsr_ip_to_string( &entry->main_addr ) );
509 alias = entry->aliases;
515 ( is_first ? "\t" : ";" ),
516 olsr_ip_to_string( &alias->alias )
519 alias = alias->next_alias;
533 send_info(int neighonly)
536 /* Print minimal http header */
537 ipc_sendf("HTTP/1.0 200 OK\n");
538 ipc_sendf("Content-type: text/plain\n\n");
540 /* Print tables to IPC socket */
542 /* links + Neighbors */
543 ipc_print_neigh_link();
546 if (!neighonly) ipc_print_topology();
549 if (!neighonly) ipc_print_hna();
552 if (!neighonly) ipc_print_mid();
555 if (!neighonly) ipc_print_routes();
559 * In a bigger mesh, there are probs with the fixed
560 * bufsize. Because the Content-Length header is
561 * optional, the sprintf() is changed to a more
562 * scalable solution here.
566 ipc_sendf(const char* format, ...)
568 char txtnetbuf[TXT_IPC_BUFSIZE];
572 #if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __MacOSX__
575 int flags = MSG_NOSIGNAL;
577 va_start(arg, format);
578 rv = vsnprintf(txtnetbuf, sizeof(txtnetbuf), format, arg);
581 if (0 > send(ipc_connection, txtnetbuf, rv, flags)) {
583 olsr_printf(1, "(TXTINFO) Failed sending data to client!\n");
585 close(ipc_connection);