3 * The olsr.org Optimized Link-State Routing daemon(olsrd)
4 * Copyright (c) 2004-2009, the olsr.org team - see HISTORY file
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 #include "olsr_types.h"
45 #include "scheduler.h"
47 #include "olsr_cookie.h"
48 #include "olsr_ip_prefix_list.h"
49 #include "olsr_logging.h"
54 #include <net/route.h>
58 #include "neighbor_table.h"
61 #include "interfaces.h"
62 #include "olsr_protocol.h"
66 #include "lq_plugin.h"
67 #include "olsr_cfg_gen.h"
68 #include "common/string.h"
69 #include "olsr_ip_prefix_list.h"
70 #include "olsr_logging.h"
71 #include <netinet/in.h>
72 #include <sys/socket.h>
73 #include <arpa/inet.h>
75 #define PLUGIN_INTERFACE_VERSION 5
77 static int has_inet_gateway;
78 static struct olsr_cookie_info *event_timer_cookie;
79 static union olsr_ip_addr gw_netmask;
82 * Plugin interface version
83 * Used by main olsrd to check plugin interface version
86 olsrd_plugin_interface_version(void)
88 return PLUGIN_INTERFACE_VERSION;
91 static const struct olsrd_plugin_parameters plugin_parameters[] = {
99 struct in_addr from_node;
107 guest_client *client;
108 struct client_list *list;
115 struct interface *ifn;
119 void ping(guest_client *);
122 olsrd_get_plugin_parameters(const struct olsrd_plugin_parameters **params, int *size)
124 *params = plugin_parameters;
125 *size = ARRAYSIZE(plugin_parameters);
130 void *PrintHello(void *threadid)
133 tid = (long)threadid;
134 printf("Hello World! It's me, thread #%ld!\n", tid);
144 * Called after all parameters are passed
147 olsrd_plugin_init(void)
152 list=(client_list*)malloc( sizeof(client_list) );
156 OLSR_INFO(LOG_PLUGINS, "OLSRD automated Client Roaming Plugin\n");
159 gw_netmask.v4.s_addr = inet_addr("255.255.255.255");
161 has_inet_gateway = 0;
164 /* create the cookie */
165 event_timer_cookie = olsr_alloc_cookie("cl roam: Event", OLSR_COOKIE_TYPE_TIMER);
167 /* Register the GW check */
168 olsr_start_timer(3 * MSEC_PER_SEC, 0, OLSR_TIMER_PERIODIC, &olsr_event, NULL, event_timer_cookie);
169 //system("echo \"1\" ");
170 //pthread_create(&ping_thread, NULL, &do_ping, NULL);
171 //system("echo \"2\" ");
172 //CreateThread(NULL, 0, do_ping, NULL, 0, &ThreadId);
173 //if (pthread_create(&ping_thread, NULL, do_ping, NULL) != 0) {
174 // OLSR_WARN(LOG_PLUGINS, "pthread_create() error");
189 int ping_thread(guest_client * target)
192 char ping_command[50];
195 snprintf(ping_command, sizeof(ping_command), "arping -I ath0 -w 1 -c 1 -q %s", inet_ntoa(target->ip));
196 return system(ping_command);
202 void ping_thread_infinite(guest_client * target)
204 char ping_command[50];
207 snprintf(ping_command, sizeof(ping_command), "arping -I ath0 -q %s", inet_ntoa(target->ip));
208 system(ping_command);
214 void ping(guest_client * target)
218 rc = pthread_create(&thread, NULL, ping_thread, (void *) target );
220 printf("ERROR; return code from pthread_create() is %d\n", rc);
225 void ping_infinite(guest_client * target)
229 rc = pthread_create(&thread, NULL, ping_thread_infinite, (void *) target );
231 printf("ERROR; return code from pthread_create() is %d\n", rc);
243 struct olsr_message msg;
247 void add_route(void* guest) {
249 guest_client * host = (guest_client *) guest;
251 if (ping_thread(host)==0) {
253 OLSR_DEBUG(LOG_PLUGINS, "Adding Route\n");
254 printf("Added Route\n");
255 ip_prefix_list_add(&olsr_cnf->hna_entries, &(host->ip), olsr_netmask_to_prefix(&gw_netmask));
256 host->is_announced=1;
258 printf("Decided not to\n");
265 void check_for_route(guest_client * host)
268 if (host->last_seen < 5.0 && ! host->is_announced) {
270 printf("maybe add something\n");
271 int rc = pthread_create(&add, NULL, add_route, (void *)host);
273 printf("ERROR; return code from pthread_create() is %d\n", rc);
276 } else if ((host->last_seen > 5.0) && host->is_announced) {
277 OLSR_DEBUG(LOG_PLUGINS, "Removing Route\n");
278 system("echo \"Entferne route\" ");
279 ip_prefix_list_remove(&olsr_cnf->hna_entries, &(host->ip), olsr_netmask_to_prefix(&gw_netmask), olsr_cnf->ip_version);
280 host->is_announced=0;
284 void check_client_list(client_list * clist) {
285 if (clist!=NULL && clist->client!=NULL) {
287 //ping(clist->client);
288 if( ! check_if_associcated(clist->client))
289 clist->client->last_seen+=2;
290 check_for_route(clist->client);
293 check_client_list(clist->list);
299 guest_client * get_client_by_mac(client_list * clist, u_int64_t mac) {
300 if (clist!=NULL && clist->client!=NULL) {
302 if (clist->client->mac == mac)
303 return clist->client;
305 return get_client_by_mac(clist->list, mac);
313 int ip_is_in_guest_list(client_list * list, guest_client * host) {
316 if (list->client==NULL)
318 else if (inet_lnaof(list->client->ip) == inet_lnaof(host->ip))
321 return ip_is_in_guest_list(list->list, host);
325 void check_local_leases(client_list * clist){
326 check_leases(clist,"/var/dhcp.leases" , 1.0);
330 void check_leases(client_list * clist, char file[], float def_last_seen) {
331 FILE * fp = fopen(file, "r");
336 long long int one, two, three, four, five, six;
339 int parse = fscanf (fp, "%s %llx:%llx:%llx:%llx:%llx:%llx %s %s", s1, &one, &two, &three, &four, &five, &six, s3, s4);
344 //printf ("String 3 = %s\n", s3);
345 user = (guest_client*)malloc( sizeof(guest_client) );
346 inet_aton(s3, &(user->ip));
347 user->mac= six | five<<8 | four<<16 | three<<24 | two<<32 | one<<40;
348 user->last_seen=def_last_seen;
349 user->is_announced=0;
350 //printf("last seen on Add %f\n",user->last_seen);
351 add_client_to_list(clist, user);
363 void check_remote_leases(client_list * clist){
364 check_leases(clist,"/tmp/otherclient" , 20.0);
372 int check_if_associcated(guest_client *client)
374 FILE * fp = fopen("/proc/net/madwifi/ath0/associated_sta", "r");
375 //FILE * fp = fopen("/home/raphael/tmp/leases", "r");
383 long long int one, two, three, four, five, six;
386 parse = fscanf (fp, "macaddr: <%llx:%llx:%llx:%llx:%llx:%llx>\n", &one, &two, &three, &four, &five, &six);
387 if (parse==EOF || parse!=6)
389 if ((six | five<<8 | four<<16 | three<<24 | two<<32 | one<<40)==client->mac) {
395 //printf ("rssi = %s\n", s2);
396 parse = fscanf (fp, " last_rx %f\n", &last_rx);
413 // Will be handy to identify when a client roamed to us. Not used yet.
414 void check_associations(client_list * clist){
415 FILE * fp = fopen("/proc/net/madwifi/ath0/associated_sta", "r");
416 //FILE * fp = fopen("/home/raphael/tmp/leases", "r");
424 long long int one, two, three, four, five, six;
427 parse = fscanf (fp, "macaddr: <%llx:%llx:%llx:%llx:%llx:%llx>\n", &one, &two, &three, &four, &five, &six);
428 if (parse==EOF || parse!=6)
430 uint64_t mac = six | five<<8 | four<<16 | three<<24 | two<<32 | one<<40;
431 //printf ("macaddr: %llx\n", mac);
432 parse = fscanf (fp, " rssi %s\n", s2);
435 //printf ("rssi = %s\n", s2);
436 parse = fscanf (fp, " last_rx %f\n", &last_rx);
439 //printf ("last_rx = %f\n", last_rx);
440 guest_client* node = get_client_by_mac(clist, mac);
442 //printf("Sichtung!\n");
443 node->last_seen=last_rx;
444 //printf("last_seen= %f\n",node->last_seen);
457 void add_client_to_list(client_list * clist, guest_client * host) {
458 if (ip_is_in_guest_list(clist, host)) {
462 if (clist->client!=NULL) {
463 client_list * this_one;
464 this_one = (client_list *)malloc( sizeof(client_list) );
465 this_one->client = host;
466 this_one->list=clist->list;
467 clist->list=this_one;
468 printf("added something\n");
471 printf("added something\n");
481 void check_for_new_clients(client_list * clist) {
482 check_local_leases(clist);
483 check_associations(clist);
491 olsr_event(void *foo __attribute__ ((unused)))
493 struct nbr_entry *nbr;
494 check_for_new_clients(list);
495 check_client_list(list);
501 OLSR_FOR_ALL_NBR_ENTRIES(nbr) {
502 union olsr_ip_addr foobar = nbr->nbr_addr;
504 char wget_command[70];
507 snprintf(wget_command, sizeof(wget_command), "wget -q -O /tmp/otherclient http://%s/dhcp.leases", inet_ntoa(foobar.v4));
509 system(wget_command);
511 check_remote_leases(list);
513 }OLSR_FOR_ALL_NBR_ENTRIES_END();