3 * Copyright (c) 2006, Sven-Ola Tuecke <sven-ola-aet-gmx.de>
4 * Copyright (c) 2004, Andreas Tonnesen(andreto@olsr.org)
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.
41 #include "olsrd_dyn_gw_plain.h"
42 #include "olsr_types.h"
44 #include "scheduler.h"
50 #include <net/route.h>
56 #define PLUGIN_INTERFACE_VERSION 5
58 static int has_inet_gateway;
61 * Plugin interface version
62 * Used by main olsrd to check plugin interface version
65 olsrd_plugin_interface_version(void)
67 return PLUGIN_INTERFACE_VERSION;
70 static const struct olsrd_plugin_parameters plugin_parameters[] = {
74 olsrd_get_plugin_parameters(const struct olsrd_plugin_parameters **params, int *size)
76 *params = plugin_parameters;
77 *size = sizeof(plugin_parameters) / sizeof(*plugin_parameters);
82 * Called after all parameters are passed
85 olsrd_plugin_init(void)
87 printf("OLSRD dyn_gw_plain plugin by Sven-Ola\n");
89 gw_net.v4.s_addr = INET_NET;
90 gw_netmask.v4.s_addr = INET_PREFIX;
94 /* Remove all local Inet HNA entries */
95 while (ip_prefix_list_remove(&olsr_cnf->hna_entries, &gw_net, olsr_netmask_to_prefix(&gw_netmask))) {
96 olsr_printf(DEBUGLEV, "HNA Internet gateway deleted\n");
99 /* Register the GW check */
100 olsr_start_timer(3 * MSEC_PER_SEC, 0, OLSR_TIMER_PERIODIC, &olsr_event, NULL, 0);
106 check_gw(union olsr_ip_addr *net, union olsr_ip_addr *mask)
108 char buff[1024], iface[17];
109 uint32_t gate_addr, dest_addr, netmask;
111 int num, metric, refcnt, use;
114 FILE *fp = fopen(PROCENTRY_ROUTE, "r");
117 perror(PROCENTRY_ROUTE);
118 olsr_printf(DEBUGLEV, "INET (IPv4) not configured in this system.\n");
125 olsr_printf(DEBUGLEV, "Genmask Destination Gateway "
126 "Flags Metric Ref Use Iface\n");
128 while (fgets(buff, 1023, fp)) {
130 sscanf(buff, "%16s %128X %128X %X %d %d %d %128X \n", iface, &dest_addr, &gate_addr, &iflags, &refcnt, &use, &metric,
138 olsr_printf(DEBUGLEV, "%-15s ", olsr_ip_to_string((union olsr_ip_addr *)&netmask));
140 olsr_printf(DEBUGLEV, "%-15s ", olsr_ip_to_string((union olsr_ip_addr *)&dest_addr));
142 olsr_printf(DEBUGLEV, "%-15s %-6d %-2d %7d %s\n",
143 olsr_ip_to_string((union olsr_ip_addr *)&gate_addr),
144 metric, refcnt, use, iface);
147 if ( //(iflags & RTF_GATEWAY) &&
148 (iflags & RTF_UP) && (metric == 0) && (netmask == mask->v4.s_addr) && (dest_addr == net->v4.s_addr)) {
149 olsr_printf(DEBUGLEV, "INTERNET GATEWAY VIA %s detected in routing table.\n", iface);
158 olsr_printf(DEBUGLEV, "No Internet GWs detected...\n");
165 * Scheduled event to update the hna table,
166 * called from olsrd main thread to keep the hna table thread-safe
169 olsr_event(void *foo __attribute__ ((unused)))
171 int res = check_gw(&gw_net, &gw_netmask);
172 if (1 == res && 0 == has_inet_gateway) {
173 olsr_printf(DEBUGLEV, "Adding OLSR local HNA entry for Internet\n");
174 ip_prefix_list_add(&olsr_cnf->hna_entries, &gw_net, olsr_netmask_to_prefix(&gw_netmask));
175 has_inet_gateway = 1;
176 } else if (0 == res && 1 == has_inet_gateway) {
177 /* Remove all local Inet HNA entries */
178 while (ip_prefix_list_remove(&olsr_cnf->hna_entries, &gw_net, olsr_netmask_to_prefix(&gw_netmask))) {
179 olsr_printf(DEBUGLEV, "Removing OLSR local HNA entry for Internet\n");
181 has_inet_gateway = 0;
188 * indent-tabs-mode: nil