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.1 2007/01/31 12:38:26 bernd67 Exp $
47 * Dynamic linked library for the olsr.org olsr daemon
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <sys/select.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
67 #include "olsr_types.h"
68 #include "neighbor_table.h"
69 #include "two_hop_neighbor_table.h"
70 #include "mpr_selector_set.h"
75 #include "socket_parser.h"
77 #include "olsrd_txtinfo.h"
78 #include "olsrd_plugin.h"
82 #define close(x) closesocket(x)
86 static int ipc_socket;
88 static int ipc_connection;
89 static int ipc_socket_up;
92 /* IPC initialization function */
94 plugin_ipc_init(void);
97 send_info(int neighonly);
103 ipc_print_neigh_link(void);
106 ipc_print_routes(void);
109 ipc_print_topology(void);
114 #define TXT_IPC_BUFSIZE 256
116 ipc_sendf(const char* format, ...);
119 *Do initialization here
121 *This function is called by the my_init
122 *function in uolsrd_plugin.c
127 /* Initial IPC value */
137 * destructor - called at unload
151 struct sockaddr_in sin;
154 /* Init ipc socket */
155 if ((ipc_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
158 olsr_printf(1, "(TXTINFO) socket()=%s\n", strerror(errno));
164 if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0)
167 olsr_printf(1, "(TXTINFO) setsockopt()=%s\n", strerror(errno));
172 #if defined __FreeBSD__ && defined SO_NOSIGPIPE
173 if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0)
175 perror("SO_REUSEADDR failed");
180 /* Bind the socket */
182 /* complete the socket structure */
183 memset(&sin, 0, sizeof(sin));
184 sin.sin_family = AF_INET;
185 sin.sin_addr.s_addr = INADDR_ANY;
186 sin.sin_port = htons(ipc_port);
188 /* bind the socket to the port number */
189 if (bind(ipc_socket, (struct sockaddr *) &sin, sizeof(sin)) == -1)
192 olsr_printf(1, "(TXTINFO) bind()=%s\n", strerror(errno));
197 /* show that we are willing to listen */
198 if (listen(ipc_socket, 1) == -1)
201 olsr_printf(1, "(TXTINFO) listen()=%s\n", strerror(errno));
206 /* Register with olsrd */
207 add_olsr_socket(ipc_socket, &ipc_action);
210 olsr_printf(2, "(TXTINFO) listening on port %d\n",ipc_port);
222 struct sockaddr_in pin;
226 addrlen = sizeof(struct sockaddr_in);
231 if ((ipc_connection = accept(fd, (struct sockaddr *) &pin, &addrlen)) == -1)
234 olsr_printf(1, "(TXTINFO) accept()=%s\n", strerror(errno));
240 addr = inet_ntoa(pin.sin_addr);
241 if(ntohl(pin.sin_addr.s_addr) != ntohl(ipc_accept_ip.s_addr))
243 olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr);
244 close(ipc_connection);
251 olsr_printf(2, "(TXTINFO) Connect from %s\n",addr);
254 /* purge read buffer to prevent blocking on linux*/
257 FD_SET(ipc_connection, &rfds);
258 struct timeval tv = {0,0};
260 if(select(ipc_connection+1, &rfds, NULL, NULL, &tv)) {
262 ssize_t s = recv(ipc_connection, &requ, sizeof(requ), 0);
265 /* To print out neighbours only on the Freifunk Status
266 * page the normal output is somewhat lengthy. The
267 * header parsing is sufficient for standard wget.
269 neighonly = (0 != strstr(requ, "/neighbours"));
273 send_info(neighonly);
275 close(ipc_connection);
282 ipc_print_neigh_link(void)
284 struct neighbor_entry *neigh;
285 struct neighbor_2_list_entry *list_2;
286 struct link_entry *link = NULL;
289 ipc_sendf("Table: Links\nLocal IP\tremote IP\tHysteresis\tLinkQuality\tlost\ttotal\tNLQ\tETX\n");
295 ipc_sendf( "%s\t%s\t%0.2f\t%0.2f\t%d\t%d\t%0.2f\t%0.2f\t\n",
296 olsr_ip_to_string(&link->local_iface_addr),
297 olsr_ip_to_string(&link->neighbor_iface_addr),
298 link->L_link_quality,
299 link->loss_link_quality,
302 link->neigh_link_quality,
303 (link->loss_link_quality * link->neigh_link_quality) ? 1.0 / (link->loss_link_quality * link->neigh_link_quality) : 0.0);
306 ipc_sendf("\nTable: Neighbors\nIP address\tSYM\tMPR\tMPRS\tWillingness\t2 Hop Neighbors\n");
309 for(index=0;index<HASHSIZE;index++)
311 for(neigh = neighbortable[index].next;
312 neigh != &neighbortable[index];
316 "%s\t%s\t%s\t%s\t%d\t",
317 olsr_ip_to_string(&neigh->neighbor_main_addr),
318 (neigh->status == SYM) ? "YES" : "NO",
319 neigh->is_mpr ? "YES" : "NO",
320 olsr_lookup_mprs_set(&neigh->neighbor_main_addr) ? "YES" : "NO",
325 for(list_2 = neigh->neighbor_2_list.next;
326 list_2 != &neigh->neighbor_2_list;
327 list_2 = list_2->next)
329 //size += sprintf(&buf[size], "<option>%s</option>\n", olsr_ip_to_string(&list_2->neighbor_2->neighbor_2_addr));
332 ipc_sendf("%d\n", thop_cnt);
341 ipc_print_routes(void)
344 struct rt_entry *routes;
346 ipc_sendf("Table: Routes\nDestination\tGateway\tMetric\tETX\tInterface\tType\n");
349 for(index = 0;index < HASHSIZE;index++)
351 for(routes = routingtable[index].next;
352 routes != &routingtable[index];
353 routes = routes->next)
356 ipc_sendf( "%s\t%s\t%d\t%.2f\t%s\tHOST\n",
357 olsr_ip_to_string(&routes->rt_dst),
358 olsr_ip_to_string(&routes->rt_router),
361 routes->rt_if->int_name);
366 for(index = 0;index < HASHSIZE;index++)
368 for(routes = hna_routes[index].next;
369 routes != &hna_routes[index];
370 routes = routes->next)
372 ipc_sendf("%s\t%s\t%d\t%s\t\tHNA\n",
373 olsr_ip_to_string(&routes->rt_dst),
374 olsr_ip_to_string(&routes->rt_router),
376 routes->rt_if->int_name);
385 ipc_print_topology(void)
388 struct tc_entry *entry;
389 struct topo_dst *dst_entry;
392 ipc_sendf("Table: Topology\nDestination IP\tLast hop IP\tLQ\tILQ\tETX\n");
395 for(index=0;index<HASHSIZE;index++)
397 /* For all TC entries */
398 entry = tc_table[index].next;
399 while(entry != &tc_table[index])
401 /* For all destination entries of that TC entry */
402 dst_entry = entry->destinations.next;
403 while(dst_entry != &entry->destinations)
405 ipc_sendf( "%s\t%s\t%0.2f\t%0.2f\t%0.2f\n",
406 olsr_ip_to_string(&dst_entry->T_dest_addr),
407 olsr_ip_to_string(&entry->T_last_addr),
408 dst_entry->link_quality,
409 dst_entry->inverse_link_quality,
410 (dst_entry->link_quality * dst_entry->inverse_link_quality) ? 1.0 / (dst_entry->link_quality * dst_entry->inverse_link_quality) : 0.0);
412 dst_entry = dst_entry->next;
426 struct hna_entry *tmp_hna;
427 struct hna_net *tmp_net;
431 ipc_sendf("Table: HNA\nNetwork\tNetmask\tGateway\n");
433 /* Announced HNA entries */
434 struct hna4_entry *hna4;
435 for(hna4 = olsr_cnf->hna4_entries; hna4; hna4 = hna4->next)
437 ipc_sendf("%s\t%s\t%s\n",
438 olsr_ip_to_string(&hna4->net),
439 olsr_ip_to_string(&hna4->netmask),
440 olsr_ip_to_string(&olsr_cnf->main_addr));
442 struct hna6_entry *hna6;
443 for(hna6 = olsr_cnf->hna6_entries; hna6; hna6 = hna6->next)
445 ipc_sendf("%s\t%d\t%s\n",
446 olsr_ip_to_string(&hna6->net),
448 olsr_ip_to_string(&olsr_cnf->main_addr));
452 for(index=0;index<HASHSIZE;index++)
454 tmp_hna = hna_set[index].next;
455 /* Check all entrys */
456 while(tmp_hna != &hna_set[index])
458 /* Check all networks */
459 tmp_net = tmp_hna->networks.next;
461 while(tmp_net != &tmp_hna->networks)
463 if (AF_INET == olsr_cnf->ip_version) {
464 ipc_sendf("%s\t%s\t%s\n",
465 olsr_ip_to_string(&tmp_net->A_network_addr),
466 olsr_ip_to_string((union olsr_ip_addr *)&tmp_net->A_netmask.v4),
467 olsr_ip_to_string(&tmp_hna->A_gateway_addr));
470 ipc_sendf("%s\t%d\t%s\n",
471 olsr_ip_to_string(&tmp_net->A_network_addr),
472 tmp_net->A_netmask.v6,
473 olsr_ip_to_string(&tmp_hna->A_gateway_addr));
475 tmp_net = tmp_net->next;
478 tmp_hna = tmp_hna->next;
488 send_info(int neighonly)
491 /* Print minimal http header */
492 ipc_sendf("HTTP/1.0 200 OK\n");
493 ipc_sendf("Content-type: text/plain\n\n");
495 /* Print tables to IPC socket */
497 /* links + Neighbors */
498 ipc_print_neigh_link();
501 if (!neighonly) ipc_print_topology();
504 if (!neighonly) ipc_print_hna();
507 if (!neighonly) ipc_print_routes();
511 * In a bigger mesh, there are probs with the fixed
512 * bufsize. Because the Content-Length header is
513 * optional, the sprintf() is changed to a more
514 * scalable solution here.
518 ipc_sendf(const char* format, ...)
520 char txtnetbuf[TXT_IPC_BUFSIZE];
524 #if defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __MacOSX__
527 int flags = MSG_NOSIGNAL;
529 va_start(arg, format);
530 rv = vsnprintf(txtnetbuf, sizeof(txtnetbuf), format, arg);
533 if (0 > send(ipc_connection, txtnetbuf, rv, flags)) {
535 olsr_printf(1, "(TXTINFO) Failed sending data to client!\n");
537 close(ipc_connection);