Patch from Sven-Ola Tuecke <sven-ola@gmx.de> from
authorBernd Petrovitsch <bernd@firmix.at>
Thu, 29 Nov 2007 00:26:16 +0000 (00:26 +0000)
committerBernd Petrovitsch <bernd@firmix.at>
Thu, 29 Nov 2007 00:26:16 +0000 (00:26 +0000)
http://download-master.berlin.freifunk.net/sven-ola/nylon/packages/olsrd/files/135-georss-map-for-nameservice.patch: fixes and extensions for the namesaervice plugin

lib/nameservice/src/mapwrite.c [new file with mode: 0644]
lib/nameservice/src/mapwrite.h [new file with mode: 0644]
lib/nameservice/src/nameservice.c
lib/nameservice/src/nameservice.h

diff --git a/lib/nameservice/src/mapwrite.c b/lib/nameservice/src/mapwrite.c
new file mode 100644 (file)
index 0000000..30b6173
--- /dev/null
@@ -0,0 +1,281 @@
+/*
+ * Copyright (c) 2007, Sven-Ola <sven-ola-aet-gmx.de>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions 
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice, 
+ *   this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice, 
+ *   this list of conditions and the following disclaimer in the documentation 
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the UniK olsr daemon nor the names of its contributors 
+ *   may be used to endorse or promote products derived from this software 
+ *   without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+/* $Id: mapwrite.c,v 1.1 2007/11/29 00:26:16 bernd67 Exp $ */
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "defs.h"
+#include "scheduler.h"
+#include "nameservice.h"
+#include "mid_set.h"
+#include "tc_set.h"
+#include "net_olsr.h"
+#include "mapwrite.h"
+
+static char my_latlon_str[48];
+
+/**
+ * lookup a nodes position
+ */
+static char* lookup_position_latlon(union olsr_ip_addr *ip)
+{
+  int hash;
+  struct db_entry *entry;
+  if (ipequal(ip, &olsr_cnf->main_addr))
+  {
+    return my_latlon_str;
+  }
+  for (hash = 0; hash < HASHSIZE; hash++) 
+  {
+    for(entry = latlon_list[hash]; entry != NULL; entry = entry->next)
+    {
+      if (NULL != entry->names && ipequal(&entry->originator, ip))
+      {
+        return entry->names->name;
+      }
+    }
+  }
+  return NULL;
+}
+
+/**
+ * write latlon positions to a file
+ */
+void mapwrite_work(FILE* fmap)
+{
+  int hash;
+  struct olsr_if *ifs;
+  union olsr_ip_addr ip;
+  struct ipaddr_str strbuf1, strbuf2;
+  struct tc_entry *tc;
+  struct tc_edge_entry *tc_edge;
+
+  if (!my_names || !fmap) return;
+
+  for (ifs = olsr_cnf->interfaces; ifs; ifs = ifs->next)
+  {
+    if (0 != ifs->interf)
+    {
+      if (olsr_cnf->ip_version == AF_INET)
+      {
+        if (!(ip4equal((struct in_addr *)&olsr_cnf->main_addr, &ifs->interf->int_addr.sin_addr)))
+        {
+          if (0 > fprintf(fmap, "Mid('%s','%s');\n",
+            olsr_ip_to_string(&strbuf1, &olsr_cnf->main_addr),
+            olsr_ip_to_string(&strbuf2, (union olsr_ip_addr *)&ifs->interf->int_addr.sin_addr)))
+          {
+            return;
+          }
+        }
+      }
+      else if (!(ip6equal((struct in6_addr *)&olsr_cnf->main_addr, &ifs->interf->int6_addr.sin6_addr)))
+      {
+        if (0 > fprintf(fmap, "Mid('%s','%s');\n",
+          olsr_ip_to_string(&strbuf1, &olsr_cnf->main_addr),
+          olsr_ip_to_string(&strbuf2, (union olsr_ip_addr *)&ifs->interf->int6_addr.sin6_addr)))
+        {
+          return;
+        }
+      }
+    }
+  }
+
+  for (hash = 0; hash < HASHSIZE; hash++) 
+  {
+    struct mid_entry *entry = mid_set[hash].next;
+    while(entry != &mid_set[hash])
+    {
+      struct mid_address *alias = entry->aliases;
+      while(alias)
+      {
+        if (0 > fprintf(fmap, "Mid('%s','%s');\n",
+          olsr_ip_to_string(&strbuf1, &entry->main_addr),
+          olsr_ip_to_string(&strbuf2, &alias->alias)))
+        {
+          return;
+        }
+        alias = alias->next_alias;
+      }
+      entry = entry->next;
+    }
+  }
+  lookup_defhna_latlon(&ip);
+  sprintf(my_latlon_str, "%f,%f,%d", my_lat, my_lon, get_isdefhna_latlon());
+  if (0 > fprintf(fmap, "Self('%s',%s,'%s','%s');\n",
+    olsr_ip_to_string(&strbuf1, &olsr_cnf->main_addr), my_latlon_str, 
+    olsr_ip_to_string(&strbuf2, &ip), my_names->name))
+  {
+    return;
+  }
+  for (hash = 0; hash < HASHSIZE; hash++) 
+  {
+    struct db_entry *entry;
+    for(entry = latlon_list[hash]; entry != NULL; entry = entry->next)
+    {
+      if (NULL != entry->names)
+      {
+        if (0 > fprintf(fmap, "Node('%s',%s,'%s','%s');\n",
+          olsr_ip_to_string(&strbuf1, &entry->originator),
+          entry->names->name, olsr_ip_to_string(&strbuf2, &entry->names->ip),
+          lookup_name_latlon(&entry->originator)))
+        {
+          return;
+        }
+      }
+    }
+  }
+
+  OLSR_FOR_ALL_TC_ENTRIES(tc) {
+  OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge) {
+    char* lla = lookup_position_latlon(&tc->addr);
+    char* llb = lookup_position_latlon(&tc_edge->T_dest_addr);
+    if (NULL != lla && NULL != llb)
+    {
+      /*
+       * To speed up processing, Links with both positions are named PLink()
+       */
+      if (0 > fprintf(fmap, "PLink('%s','%s',%f,%f,%f,%s,%s);\n", 
+            olsr_ip_to_string(&strbuf1, &tc_edge->T_dest_addr),
+            olsr_ip_to_string(&strbuf2, &tc->addr), 
+            tc_edge->link_quality,
+            tc_edge->inverse_link_quality,
+            (tc_edge->link_quality * tc_edge->inverse_link_quality) ?
+              1.0 / (tc_edge->link_quality * tc_edge->inverse_link_quality) : 0.0,
+            lla, llb))
+      {
+        return;
+      }
+    }
+    else
+    {
+      /*
+       * If one link end pos is unkown, only send Link()
+       */
+      if (0 > fprintf(fmap, "Link('%s','%s',%f,%f,%f);\n", 
+            olsr_ip_to_string(&strbuf1, &tc_edge->T_dest_addr),
+            olsr_ip_to_string(&strbuf2, &tc->addr), 
+            tc_edge->link_quality,
+            tc_edge->inverse_link_quality,
+            (tc_edge->link_quality * tc_edge->inverse_link_quality) ?
+              1.0 / (tc_edge->link_quality * tc_edge->inverse_link_quality) : 0.0))
+      {
+        return;
+      }
+    }
+  } OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
+  } OLSR_FOR_ALL_TC_ENTRIES_END(tc);
+}
+
+#ifndef WIN32
+
+/*
+ * Windows doesn't know fifo's AFAIK. We better write
+ * to a file (done in nameservice.c, see #ifdef WIN32)
+ */
+static const char* the_fifoname = 0;
+static int fifopolltime = 0;
+
+static void mapwrite_poll(void)
+{
+  fifopolltime++;
+  if (0 == (fifopolltime & 7) && 0 != the_fifoname)
+  {
+    FILE * fout;
+    /* Non-blocking means: fail open if no pipe reader */
+    int fd = open(the_fifoname, O_WRONLY | O_NONBLOCK);
+    if (0 <= fd)
+    {
+      /* 
+       * Change to blocking, otherwhise expect fprintf errors
+       */
+      fcntl(fd, F_SETFL, O_WRONLY);
+      fout = fdopen(fd, "w");
+      if (0 != fout)
+      {
+        mapwrite_work(fout);
+        fclose(fout);
+        /* Give pipe reader cpu slot to detect EOF */
+        usleep(1);
+      }
+      else
+      {
+        close(fd);
+      }
+    }
+  }
+}
+
+int mapwrite_init(const char* fifoname)
+{
+  the_fifoname = fifoname;
+  if (0 != fifoname && 0 != *fifoname)
+  {
+    unlink(fifoname);
+    if (0 > mkfifo(fifoname, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH))
+    {
+      OLSR_PRINTF(1, "mkfifo(%s): %s", fifoname, strerror(errno));
+      return OLSR_FALSE;
+    }
+    else
+    {
+      the_fifoname = fifoname;
+      olsr_register_timeout_function(&mapwrite_poll, OLSR_FALSE);
+    }
+  }
+  return OLSR_TRUE;
+}
+
+void mapwrite_exit(void)
+{
+  if (0 != the_fifoname)
+  {
+    unlink(the_fifoname);
+    /* Ignore any Error */
+    the_fifoname = 0;
+  }
+}
+#endif
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ */
diff --git a/lib/nameservice/src/mapwrite.h b/lib/nameservice/src/mapwrite.h
new file mode 100644 (file)
index 0000000..f54be0d
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2007, Sven-Ola <sven-ola-aet-gmx.de>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions 
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice, 
+ *   this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice, 
+ *   this list of conditions and the following disclaimer in the documentation 
+ *   and/or other materials provided with the distribution.
+ * * Neither the name of the UniK olsr daemon nor the names of its contributors 
+ *   may be used to endorse or promote products derived from this software 
+ *   without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
+ * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+/* $Id: mapwrite.h,v 1.1 2007/11/29 00:26:16 bernd67 Exp $ */
+#ifndef _MAPWRITE_H
+#define _MAPWRITE_H
+
+int mapwrite_init(const char* fifoname);
+void mapwrite_work(FILE* fmap);
+void mapwrite_exit(void);
+
+#endif /* _MAPWRITE_H */
+
+/*
+ * Local Variables:
+ * mode: c
+ * c-indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ */
index fa5ca1d..a56fde0 100644 (file)
@@ -31,7 +31,7 @@
  *
  */
 
-/* $Id: nameservice.c,v 1.38 2007/11/29 00:10:17 bernd67 Exp $ */
+/* $Id: nameservice.c,v 1.39 2007/11/29 00:26:16 bernd67 Exp $ */
 
 /*
  * Dynamic linked library for UniK OLSRd
@@ -59,6 +59,7 @@
 
 #include "plugin_util.h"
 #include "nameservice.h"
+#include "mapwrite.h"
 #include "olsrd_copy.h"
 #include "compat.h"
 
@@ -73,7 +74,7 @@ static char my_resolv_file[MAX_FILE +1];
 static char my_services_file[MAX_FILE + 1];
 static char latlon_in_file[MAX_FILE + 1];
 static char my_latlon_file[MAX_FILE + 1];
-static float my_lat = 0.0, my_lon = 0.0;
+float my_lat = 0.0, my_lon = 0.0;
 
 /* the databases (using hashing)
  * for hostnames, service_lines and dns-servers
@@ -82,7 +83,7 @@ static float my_lat = 0.0, my_lon = 0.0;
  * are store in a linked list (without hashing)
  * */
 static struct db_entry* list[HASHSIZE];
-static struct name_entry *my_names = NULL;
+struct name_entry *my_names = NULL;
 static olsr_bool name_table_changed = OLSR_TRUE;
 
 static struct db_entry* service_list[HASHSIZE];
@@ -93,7 +94,7 @@ static struct db_entry* forwarder_list[HASHSIZE];
 static struct name_entry *my_forwarders = NULL;
 static olsr_bool forwarder_table_changed = OLSR_TRUE;
 
-static struct db_entry* latlon_list[HASHSIZE];
+struct db_entry* latlon_list[HASHSIZE];
 static olsr_bool latlon_table_changed = OLSR_TRUE;
 
 /* regular expression to be matched by valid hostnames, compiled in name_init() */
@@ -118,7 +119,7 @@ name_constructor(void)
 
        GetWindowsDirectory(my_hosts_file, MAX_FILE - 12);
        GetWindowsDirectory(my_services_file, MAX_FILE - 12);
-       GetWindowsDirectory(my_latlon_file, MAX_FILE - 12);
+       GetWindowsDirectory(my_resolv_file, MAX_FILE - 12);
 
        len = strlen(my_hosts_file);
        if (my_hosts_file[len - 1] != '\\')
@@ -134,20 +135,15 @@ name_constructor(void)
        if (my_resolv_file[len - 1] != '\\')
                my_resolv_file[len++] = '\\';
        strcpy(my_resolv_file + len, "resolvconf_olsr");
-
-       len = strlen(my_latlon_file);
-       if (my_latlon_file[len - 1] != '\\')
-               my_latlon_file[len++] = '\\';
-       strcpy(my_latlon_file + len, "latlon.js");
 #else
        strcpy(my_hosts_file, "/var/run/hosts_olsr");
        strcpy(my_services_file, "/var/run/services_olsr");
        strcpy(my_resolv_file, "/var/run/resolvconf_olsr");
-       strcpy(my_latlon_file, "/var/run/latlon.js");
 #endif
 
        my_suffix[0] = '\0';
        my_add_hosts[0] = '\0';
+       my_latlon_file[0] = '\0';
        latlon_in_file[0] = '\0';
        
        /* init lists */
@@ -356,15 +352,15 @@ name_init(void)
        //for service
 
        for (name = my_names; name != NULL; name = name->next) {
-               if (memcmp(&name->ip, &ipz, olsr_cnf->ipsize) == 0) {
+               if (ipequal(&name->ip, &ipz)) {
                        OLSR_PRINTF(2, "NAME PLUGIN: insert main addr for name %s \n", name->name);
-                       memcpy(&name->ip, &olsr_cnf->main_addr, olsr_cnf->ipsize);
+                       name->ip = olsr_cnf->main_addr;
                }
        }
        for (name = my_forwarders; name != NULL; name = name->next) {
                if (name->ip.v4.s_addr == 0) {
                        OLSR_PRINTF(2, "NAME PLUGIN: insert main addr for name %s \n", name->name);
-                       memcpy(&name->ip, &olsr_cnf->main_addr, olsr_cnf->ipsize);
+                       name->ip = olsr_cnf->main_addr;
                }
        }
 
@@ -378,6 +374,7 @@ name_init(void)
        olsr_parser_add_function(&olsr_parser, PARSER_TYPE, 1);
        olsr_register_timeout_function(&olsr_timeout, OLSR_TRUE);
        olsr_register_scheduler_event(&olsr_event, NULL, my_interval, 0, NULL);
+       mapwrite_init(my_latlon_file);
 
        return 1;
 }
@@ -451,7 +448,7 @@ name_destructor(void)
 
        regfree(&regex_t_name);
        regfree(&regex_t_service);
-       
+       mapwrite_exit();
 }
 
 /* free all list entries */
@@ -507,9 +504,11 @@ olsr_timeout(void)
                case 3:
                        write_services_file(); // if service_table_changed
                        break;
+#ifdef WIN32
                case 4:
                        write_latlon_file(); // latlon_table_changed
                        break;
+#endif
                default:
                        timeout_roundrobin = 0;
        } // switch
@@ -646,7 +645,7 @@ olsr_parser(union olsr_message *m, struct interface *in_if, union olsr_ip_addr *
 
        /* Check if message originated from this node. 
        If so - back off */
-       if(memcmp(&originator, &olsr_cnf->main_addr, olsr_cnf->ipsize) == 0)
+       if(ipequal(&originator, &olsr_cnf->main_addr))
                return;
 
        /* Check that the neighbor this message was received from is symmetric. 
@@ -761,7 +760,7 @@ create_packet(struct name* to, struct name_entry *from)
                from->name, olsr_ip_to_string(&strbuf, &from->ip), from->len);
        to->type = htons(from->type);
        to->len = htons(from->len);
-       memcpy(&to->ip, &from->ip, olsr_cnf->ipsize);
+       to->ip = from->ip;
        pos += sizeof(struct name);
        strncpy(pos, from->name, from->len);
        pos += from->len;
@@ -787,7 +786,7 @@ decap_namemsg(struct name *from_packet, struct name_entry **to, olsr_bool *this_
        OLSR_PRINTF(4, "NAME PLUGIN: decap type=%d, len=%d, name=%s\n",
                type_of_from_packet, len_of_name, name);
 
-       //XXX: should I check the from_packet->ip here? If so, why not also check the ip fro HOST and SERVICE?
+       //XXX: should I check the from_packet->ip here? If so, why not also check the ip from HOST and SERVICE?
        if( (type_of_from_packet==NAME_HOST && !is_name_wellformed(name)) ||
                (type_of_from_packet==NAME_SERVICE && !is_service_wellformed(name)) ||
                (type_of_from_packet==NAME_LATLON && !is_latlon_wellformed(name)))
@@ -798,7 +797,7 @@ decap_namemsg(struct name *from_packet, struct name_entry **to, olsr_bool *this_
 
        //ignore all packets with a too long name
        //or a spoofed len of its included name string
-       if (len_of_name > MAX_NAME || strlen(name) != len_of_name) {
+       if (len_of_name > MAX_NAME || strlen(name) != len_of_name || NULL != strchr(name, '\\') || NULL != strchr(name, '\'')) {
                OLSR_PRINTF(4, "NAME PLUGIN: from_packet->len %d > MAX_NAME %d or from_packet->len %d !0 strlen(name [%s] in packet)\n",
                        len_of_name, MAX_NAME, len_of_name, name );
                return;
@@ -836,7 +835,7 @@ decap_namemsg(struct name *from_packet, struct name_entry **to, olsr_bool *this_
                                        olsr_ip_to_string(&strbuf, &already_saved_name_entries->ip),
                                        olsr_ip_to_string(&strbuf2, &from_packet->ip),
                                        olsr_ip_to_string(&strbuf3, &already_saved_name_entries->ip));
-                               memcpy(&already_saved_name_entries->ip, &from_packet->ip, olsr_cnf->ipsize);
+                               already_saved_name_entries->ip = from_packet->ip;
                                *this_table_changed = OLSR_TRUE;
                        }
                        if (!*this_table_changed)
@@ -853,7 +852,7 @@ decap_namemsg(struct name *from_packet, struct name_entry **to, olsr_bool *this_
        tmp->type = ntohs(from_packet->type);
        tmp->len = len_of_name > MAX_NAME ? MAX_NAME : ntohs(from_packet->len);
        tmp->name = olsr_malloc(tmp->len+1, "new name_entry name");
-       memcpy(&tmp->ip, &from_packet->ip, olsr_cnf->ipsize);
+       tmp->ip = from_packet->ip;
        strncpy(tmp->name, name, tmp->len);
        tmp->name[tmp->len] = '\0';
 
@@ -941,7 +940,7 @@ insert_new_name_in_list(union olsr_ip_addr *originator, struct db_entry **this_l
        /* find the entry for originator, if there is already one */
        for (entry = this_list[hash]; entry != NULL; entry = entry->next)
        {
-               if (memcmp(originator, &entry->originator, olsr_cnf->ipsize) == 0) {
+               if (ipequal(originator, &entry->originator)) {
 #ifndef NODEBUG
                        struct ipaddr_str strbuf;
 #endif
@@ -966,7 +965,7 @@ insert_new_name_in_list(union olsr_ip_addr *originator, struct db_entry **this_l
                /* insert a new entry */
                entry = olsr_malloc(sizeof(struct db_entry), "new db_entry");
 
-               memcpy(&entry->originator, originator, olsr_cnf->ipsize);
+               entry->originator = *originator;
                olsr_get_timestamp(vtime * 1000, &entry->timer);
                entry->names = NULL;
 
@@ -1445,14 +1444,16 @@ olsr_bool get_isdefhna_latlon(void)
  */
 void lookup_defhna_latlon(union olsr_ip_addr *ip)
 {
-       union olsr_ip_addr dest;
-       struct rt_entry* rt_hna;
-       memset(ip, 0, sizeof(ip));
-       memset(&dest, 0, sizeof(dest));
-       if (NULL != (rt_hna = olsr_lookup_routing_table(&dest))) {
-               //COPY_IP(ip, &rt_hna->rt_best->rtp_nexthop.gateway);
-               *ip = rt_hna->rt_best->rtp_nexthop.gateway;
-       }
+  struct avl_node *rt_tree_node;
+  struct olsr_ip_prefix prefix;
+
+  memset(ip, 0, sizeof(ip));
+  memset(&prefix, 0, sizeof(prefix));
+
+  if (NULL != (rt_tree_node = avl_find(&routingtree, &prefix)))
+  {
+    *ip = ((struct rt_entry *)rt_tree_node->data)->rt_best->rtp_nexthop.gateway;
+  }
 }
 
 /**
@@ -1477,107 +1478,29 @@ lookup_name_latlon(union olsr_ip_addr *ip)
        return "";
 }
 
+#ifdef WIN32
 /**
  * write latlon positions to a javascript file
  */
 void
 write_latlon_file(void)
 {
-       int hash;
-       FILE* js;
-       struct olsr_if *ifs;
-       union olsr_ip_addr ip;
-       struct ipaddr_str strbuf1, strbuf2;
-       struct tc_entry *tc;
-       struct tc_edge_entry *tc_edge;
-
-       if (!my_names || !latlon_table_changed) {
-               return;
-       }
-       OLSR_PRINTF(2, "NAME PLUGIN: writing latlon file\n");
-
-       js = fopen( my_latlon_file, "w" );
-       if (js == NULL) {
-               OLSR_PRINTF(0, "NAME PLUGIN: cant write latlon file\n");
-               return;
-       }
-       fprintf(js, "/* This file is overwritten regularly by olsrd */\n");
-
-       for (ifs = olsr_cnf->interfaces; ifs; ifs = ifs->next)
-       {
-               if (0 != ifs->interf)
-               {
-                       if (olsr_cnf->ip_version == AF_INET)
-                       {
-                               /*
-                                * Didn't find a good sample to grab a simple
-                                * olsr_ip_addr from a given interface. Sven-Ola
-                                */
-                               const char* p = olsr_ip_to_string(&strbuf1, &olsr_cnf->main_addr);
-                               const char* q = ip4_to_string(&strbuf1, ifs->interf->int_addr.sin_addr);
-                               if (0 != strcmp(p, q))
-                               {
-                                       fprintf(js, "Mid('%s','%s');\n", p, q);
-                               }
-                       }
-                       else if (!(ipequal(&olsr_cnf->main_addr, (union olsr_ip_addr *)&ifs->interf->int6_addr.sin6_addr)))
-                       {
-                               fprintf(js, "Mid('%s','%s');\n",
-                                       olsr_ip_to_string(&strbuf1, &olsr_cnf->main_addr),
-                                       olsr_ip_to_string(&strbuf2, (union olsr_ip_addr *)&ifs->interf->int6_addr.sin6_addr));
-                       }
-               }
-       }
-
-       for (hash = 0; hash < HASHSIZE; hash++) 
-       {
-               struct mid_entry *entry = mid_set[hash].next;
-               while(entry != &mid_set[hash])
-               {
-                       struct mid_address *alias = entry->aliases;
-                       while(alias)
-                       {
-                               fprintf(js, "Mid('%s','%s');\n",
-                                               olsr_ip_to_string(&strbuf1, &entry->main_addr),
-                                               olsr_ip_to_string(&strbuf2, &alias->alias));
-                               alias = alias->next_alias;
-                       }
-                       entry = entry->next;
-               }
-       }
-       lookup_defhna_latlon(&ip);
-       fprintf(js, "Self('%s',%f,%f,%d,'%s','%s');\n", olsr_ip_to_string(&strbuf1, &olsr_cnf->main_addr),
-                       my_lat, my_lon, get_isdefhna_latlon(), olsr_ip_to_string(&strbuf2, &ip), my_names->name);
-       for (hash = 0; hash < HASHSIZE; hash++) 
-       {
-               struct db_entry *entry;
-               for(entry = latlon_list[hash]; entry != NULL; entry = entry->next)
-               {
-                       struct name_entry *name;
-                       for (name = entry->names; name != NULL; name = name->next) 
-                       {
-                               fprintf(js, "Node('%s',%s,'%s','%s');\n",
-                                       olsr_ip_to_string(&strbuf1, &entry->originator),
-                                       name->name, olsr_ip_to_string(&strbuf2, &name->ip),
-                                       lookup_name_latlon(&entry->originator));
-                       }
-               }
-       }
-
-       OLSR_FOR_ALL_TC_ENTRIES(tc) {
-               OLSR_FOR_ALL_TC_EDGE_ENTRIES(tc, tc_edge) {
-                       fprintf(js, "Link('%s','%s',%f,%f,%f);\n", 
-                                       olsr_ip_to_string(&strbuf1, &tc_edge->T_dest_addr),
-                                       olsr_ip_to_string(&strbuf2, &tc->addr), 
-                                       tc_edge->link_quality,
-                                       tc_edge->inverse_link_quality,
-                                       olsr_calc_tc_etx(tc_edge));
-               } OLSR_FOR_ALL_TC_EDGE_ENTRIES_END(tc, tc_edge);
-       } OLSR_FOR_ALL_TC_ENTRIES_END(tc);
-
-       fclose(js);
-       latlon_table_changed = OLSR_FALSE;
+  FILE* fmap;
+  
+  if (!my_names || !latlon_table_changed) return;
+  
+  OLSR_PRINTF(2, "NAME PLUGIN: writing latlon file\n");
+
+  if (NULL == (fmap = fopen(my_latlon_file, "w"))) {
+    OLSR_PRINTF(0, "NAME PLUGIN: cant write latlon file\n");
+    return;
+  }
+  fprintf(fmap, "/* This file is overwritten regularly by olsrd */\n");
+  mapwrite_work(fmap);  
+  fclose(fmap);
+  latlon_table_changed = OLSR_FALSE;
 }
+#endif
 
 /*
  * Local Variables:
index 9936da0..5a739b5 100644 (file)
@@ -29,7 +29,7 @@
  *
  */
 
-/* $Id: nameservice.h,v 1.16 2007/11/16 19:12:55 bernd67 Exp $ */
+/* $Id: nameservice.h,v 1.17 2007/11/29 00:26:16 bernd67 Exp $ */
  
 /*
  * Dynamic linked library for UniK OLSRd
@@ -47,6 +47,8 @@
 
 #include "olsrd_plugin.h"
 #include "nameservice_msg.h"
+#include "hashing.h"
+#include "mapwrite.h"
 
 #define PLUGIN_NAME    "OLSRD nameservice plugin"
 #define PLUGIN_VERSION "0.3"
@@ -103,6 +105,9 @@ struct db_entry
        struct db_entry         *next;          /* linked list */
 };
 
+extern struct name_entry *my_names;
+extern struct db_entry* latlon_list[HASHSIZE];
+extern float my_lat, my_lon;
 
 /* Timeout function to register with the sceduler */
 void