Fixed signed comparison warnings
[olsrd.git] / src / build_msg.c
index 15fc36b..ac57b4d 100644 (file)
 /*
- * OLSR ad-hoc routing table management protocol
- * Copyright (C) 2004 Andreas T√łnnesen (andreto@ifi.uio.no)
+ * The olsr.org Optimized Link-State Routing daemon(olsrd)
+ * Copyright (c) 2004, Andreas T√łnnesen(andreto@olsr.org)
+ * All rights reserved.
  *
- * This file is part of the olsr.org OLSR daemon.
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions 
+ * are met:
  *
- * olsr.org is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * * 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 olsr.org, olsrd nor the names of its 
+ *   contributors may be used to endorse or promote products derived 
+ *   from this software without specific prior written permission.
  *
- * olsr.org is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * 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.
  *
- * You should have received a copy of the GNU General Public License
- * along with olsr.org; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- * 
- * 
- * $Id: build_msg.c,v 1.13 2004/10/12 06:33:55 kattemat Exp $
+ * Visit http://www.olsr.org for more information.
  *
+ * If you find this software useful feel free to make a donation
+ * to the project. For more information see the website or contact
+ * the copyright holders.
+ *
+ * $Id: build_msg.c,v 1.29 2005/03/10 20:43:13 kattemat Exp $
  */
 
 
 #include "defs.h"
+#include "olsr.h"
+#include "log.h"
 #include "build_msg.h"
 #include "local_hna_set.h"
-#include "olsr.h"
+
+#define BMSG_DBGLVL 5
+
+#define OLSR_IPV4_HDRSIZE          12
+#define OLSR_IPV6_HDRSIZE          24
+
+#define OLSR_HELLO_IPV4_HDRSIZE    (OLSR_IPV4_HDRSIZE + 4)   
+#define OLSR_HELLO_IPV6_HDRSIZE    (OLSR_IPV6_HDRSIZE + 4)
+#define OLSR_TC_IPV4_HDRSIZE       (OLSR_IPV4_HDRSIZE + 4)
+#define OLSR_TC_IPV6_HDRSIZE       (OLSR_IPV6_HDRSIZE + 4)
+#define OLSR_MID_IPV4_HDRSIZE      OLSR_IPV4_HDRSIZE
+#define OLSR_MID_IPV6_HDRSIZE      OLSR_IPV6_HDRSIZE
+#define OLSR_HNA_IPV4_HDRSIZE      OLSR_IPV4_HDRSIZE
+#define OLSR_HNA_IPV6_HDRSIZE      OLSR_IPV6_HDRSIZE
 
 
 /* All these functions share this buffer */
 
 static char msg_buffer[MAXMESSAGESIZE - OLSR_HEADERSIZE];
 
-/* Begin:
- * Prototypes for internal functions 
- */
+/* Prototypes for internal functions */
 
 /* IPv4 */
 
-static void
-hello_build4(struct hello_message *, struct interface *);
+static olsr_bool
+serialize_hello4(struct hello_message *, struct interface *);
 
-static void
-tc_build4(struct tc_message *, struct interface *);
+static olsr_bool
+serialize_tc4(struct tc_message *, struct interface *);
 
-static void
-mid_build4(struct interface *);
+static olsr_bool
+serialize_mid4(struct interface *);
 
-static void
-hna_build4(struct interface *);
+static olsr_bool
+serialize_hna4(struct interface *);
 
 /* IPv6 */
 
-static void
-hello_build6(struct hello_message *, struct interface *);
+static olsr_bool
+serialize_hello6(struct hello_message *, struct interface *);
 
-static void
-tc_build6(struct tc_message *, struct interface *);
-
-static void
-mid_build6(struct interface *);
-
-static void
-hna_build6(struct interface *);
-
-/* End:
- * Prototypes for internal functions 
- */
+static olsr_bool
+serialize_tc6(struct tc_message *, struct interface *);
 
+static olsr_bool
+serialize_mid6(struct interface *);
 
+static olsr_bool
+serialize_hna6(struct interface *);
 
-/*
- * Generic calls
- * These are the functions to call from outside
- */
 
 
 /**
@@ -83,7 +102,7 @@ hna_build6(struct interface *);
  * If this won't fit in one packet, chop it up into several.
  * Send the packet if the size of the data contained in the output buffer
  * reach maxmessagesize. Can generate an empty HELLO packet if the 
- * neighbor table is empty. The parameter messag is not used.
+ * neighbor table is empty. 
  *
  *
  *@param message the hello_message struct containing the info
@@ -93,21 +112,21 @@ hna_build6(struct interface *);
  *@return nada
  */
 
-void
-hello_build(struct hello_message *message, struct interface *ifp)
+olsr_bool
+queue_hello(struct hello_message *message, struct interface *ifp)
 {
-  switch(ipversion)
+#ifdef DEBUG
+  OLSR_PRINTF(BMSG_DBGLVL, "Building HELLO on %s\n-------------------\n", ifp->int_name)
+#endif
+
+  switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
-      hello_build4(message, ifp);
-      break;
+      return serialize_hello4(message, ifp);
     case(AF_INET6):
-      hello_build6(message, ifp);
-      break;
-    default:
-      return;
+      return serialize_hello6(message, ifp);
     }
-  return;
+  return OLSR_FALSE;
 }
 
 
@@ -115,14 +134,7 @@ hello_build(struct hello_message *message, struct interface *ifp)
  * Generate TC packet with the contents of the parameter "message".
  * If this won't fit in one packet, chop it up into several.
  * Send the packet if the size of the data contained in the output buffer
- * reach maxmessagesize. Don't generate an empty TC packet.
- * The parameter messag is not used.
- *
- *If parameter ifp = NULL then this is a TC packet who is to be
- *forwarded. In that case each chump of the packet(if bigger then
- *maxmessagesize the packet has to be split) must be forwarded
- *on each interface(exept the one it came on - if it was a wired interface)
- *and seqnumbers must be taken from the recieved packet.
+ * reach maxmessagesize. 
  *
  *@param message the tc_message struct containing the info
  *to send
@@ -131,21 +143,21 @@ hello_build(struct hello_message *message, struct interface *ifp)
  *@return nada
  */
 
-void
-tc_build(struct tc_message *message, struct interface *ifp)           
+olsr_bool
+queue_tc(struct tc_message *message, struct interface *ifp)           
 {
-  switch(ipversion)
+#ifdef DEBUG
+  OLSR_PRINTF(BMSG_DBGLVL, "Building TC on %s\n-------------------\n", ifp->int_name)
+#endif
+
+  switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
-      tc_build4(message, ifp);
-      break;
+      return serialize_tc4(message, ifp);
     case(AF_INET6):
-      tc_build6(message, ifp);
-      break;
-    default:
-      return;
+      return serialize_tc6(message, ifp);
     }
-  return;
+  return OLSR_FALSE;
 }
 
 
@@ -157,21 +169,21 @@ tc_build(struct tc_message *message, struct interface *ifp)
  *@return 1 on success
  */
 
-void
-mid_build(struct interface *ifn)
+olsr_bool
+queue_mid(struct interface *ifp)
 {
-  switch(ipversion)
+#ifdef DEBUG
+  OLSR_PRINTF(BMSG_DBGLVL, "Building MID on %s\n-------------------\n", ifp->int_name)
+#endif
+
+  switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
-      mid_build4(ifn);
-      break;
+      return serialize_mid4(ifp);
     case(AF_INET6):
-      mid_build6(ifn);
-      break;
-    default:
-      return;
+      return serialize_mid6(ifp);
     }
-  return;
+  return OLSR_FALSE;
 }
 
 
@@ -182,21 +194,21 @@ mid_build(struct interface *ifn)
  *@param ifp the interface to send on
  *@return nada
  */
-void
-hna_build(struct interface *ifp)
+olsr_bool
+queue_hna(struct interface *ifp)
 {
-  switch(ipversion)
+#ifdef DEBUG
+  OLSR_PRINTF(BMSG_DBGLVL, "Building HNA on %s\n-------------------\n", ifp->int_name)
+#endif
+
+  switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
-      hna_build4(ifp);
-      break;
+      return serialize_hna4(ifp);
     case(AF_INET6):
-      hna_build6(ifp);
-      break;
-    default:
-      return;
+      return serialize_hna6(ifp);
     }
-  return;
+  return OLSR_FALSE;
 }
 
 /*
@@ -204,6 +216,16 @@ hna_build(struct interface *ifp)
  */
 
 
+static void
+check_buffspace(int msgsize, int buffsize, char *type)
+{
+  if(msgsize > buffsize)
+    {
+      OLSR_PRINTF(1, "%s build, outputbuffer to small(%d/%d)!\n", type, msgsize, buffsize)
+      olsr_syslog(OLSR_LOG_ERR, "%s build, outputbuffer to small(%d/%d)!\n", type, msgsize, buffsize);
+      olsr_exit(__func__, EXIT_FAILURE);
+    }
+}
 
 
 /**
@@ -216,29 +238,26 @@ hna_build(struct interface *ifp)
  *@return nada
  */
 
-static void
-hello_build4(struct hello_message *message, struct interface *ifp)
+static olsr_bool
+serialize_hello4(struct hello_message *message, struct interface *ifp)
 {
-  int remainsize, curr_size;
-  struct hello_neighbor *nb, *prev_nb;
+  olsr_u16_t remainsize, curr_size;
+  struct hello_neighbor *nb;
   union olsr_message *m;
   struct hellomsg *h;
   struct hellinfo *hinfo;
   union olsr_ip_addr *haddr;
-  int i, j, sametype;
-  int lastpacket = 0; /* number of neighbors with the same
-                        greater link status in the last packet */
-  if((!message) || (!ifp) || (ipversion != AF_INET))
-    return;
+  int i, j;
+  olsr_bool first_entry;
 
-  remainsize = net_outbuffer_bytes_left(ifp);
+  if((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET))
+    return OLSR_FALSE;
 
-  //printf("HELLO build outputsize: %d\n", outputsize);
+  remainsize = net_outbuffer_bytes_left(ifp);
 
   m = (union olsr_message *)msg_buffer;
 
-  curr_size = 12; /* OLSR message header */
-  curr_size += 4; /* Hello header */
+  curr_size = OLSR_HELLO_IPV4_HDRSIZE;
 
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
@@ -246,13 +265,13 @@ hello_build4(struct hello_message *message, struct interface *ifp)
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
+  /* Sanity check */
+  check_buffspace(curr_size, remainsize, "HELLO");
 
   h = &m->v4.message.hello;
   hinfo = h->hell_info;
   haddr = (union olsr_ip_addr *)hinfo->neigh_addr;
   
-  //printf("Neighbor addr: %s\n", olsr_ip_to_string(haddr));fflush(stdout);
-
   /* Fill message header */
   m->v4.ttl = message->ttl;
   m->v4.hopcnt = 0;
@@ -260,11 +279,11 @@ hello_build4(struct hello_message *message, struct interface *ifp)
   /* Set source(main) addr */
   COPY_IP(&m->v4.originator, &main_addr);
 
-  m->v4.olsr_vtime = ifp->is_wireless ? hello_vtime : hello_nw_vtime;
+  m->v4.olsr_vtime = ifp->valtimes.hello;
 
   /* Fill HELLO header */
   h->willingness = message->willingness; 
-  h->htime = ifp->is_wireless ? htime : htime_nw;
+  h->htime = ifp->hello_etime;
 
   memset(&h->reserved, 0, sizeof(olsr_u16_t));
   
@@ -277,148 +296,94 @@ hello_build4(struct hello_message *message, struct interface *ifp)
   /* Nighbor statuses */
   for (i = 0; i <= MAX_NEIGH; i++) 
     {
-      /* Link ststuses */
+      /* Link statuses */
       for(j = 0; j <= MAX_LINK; j++)
        {
 
-         /*
-          *HYSTERESIS
-          *Not adding neighbors with link type HIDE
-          */
+         /* HYSTERESIS - Not adding neighbors with link type HIDE */
          
          if(j == HIDE_LINK)
              continue;
 
-         lastpacket = sametype = 0;
-
-         //printf("Neighbortype %d outputsize %d\n", i, outputsize);
+         first_entry = OLSR_TRUE;
 
-         /*
-          *Looping trough neighbors
-          */
+         /* Looping trough neighbors */
          for (nb = message->neighbors; nb != NULL; nb = nb->next) 
            {     
-             if ((nb->status == i) && (nb->link == j))
+             if ((nb->status != i) || (nb->link != j))
+               continue;
+
+#ifdef DEBUG
+             OLSR_PRINTF(BMSG_DBGLVL, "\t%s - ", olsr_ip_to_string(&nb->address))
+             OLSR_PRINTF(BMSG_DBGLVL, "L:%d N:%d\n", j, i)
+#endif           
+             /*
+              * If there is not enough room left 
+              * for the data in the outputbuffer
+              * we must send a partial HELLO and
+              * continue building the rest of the
+              * data in a new HELLO message
+              *
+              * If this is the first neighbor in 
+              * a group, we must check for an extra
+              * 4 bytes
+              */
+             if((curr_size + ipsize + (first_entry ? 4 : 0)) > remainsize)
                {
-                 sametype++;
-                 if (sametype == 1)
+                 /* Only send partial HELLO if it contains data */
+                 if(curr_size > OLSR_HELLO_IPV4_HDRSIZE)
                    {
-
-                     /*
-                      * If there is not enough room left 
-                      * for the data in tho outputbuffer
-                      * we must send a partial HELLO and
-                      * continue building the rest of the
-                      * data in a new HELLO message
-                      * Add ipsize in check since there is
-                      * no use sending just the type header
-                      */
-                     if((curr_size + 4 + ipsize) > remainsize)
-                       {
-                         /* Only send partial HELLO if it contains data */
-                         if(curr_size > (12 + 4))
-                           {
-                             /* Complete the headers */
-                             m->v4.seqno = htons(get_msg_seqno());
-                             m->v4.olsr_msgsize = htons(curr_size);
-                             
-                             hinfo->size = (char *)haddr - (char *)hinfo;
-                             hinfo->size = ntohs(hinfo->size);
-                             
-                             /* Send partial packet */
-                             net_outbuffer_push(ifp, msg_buffer, curr_size);
-
-                             curr_size = 12; /* OLSR message header */
-                             curr_size += 4; /* Hello header */
-                             
-                             h = &m->v4.message.hello;
-                             hinfo = h->hell_info;
-                             haddr = (union olsr_ip_addr *)hinfo->neigh_addr;
-                           }
-
-                         net_output(ifp);                        
-                         /* Reset size and pointers */
-                         remainsize = net_outbuffer_bytes_left(ifp);
-                       }
-                     memset(&hinfo->reserved, 0, sizeof(olsr_u8_t));
-                     /* Set link and status for this group of neighbors (this is the first) */
-                     hinfo->link_code = CREATE_LINK_CODE(i, j);//j | (i<<2);
-                     //printf("(2)Setting neighbor link status: %x\n", hinfo->link_code);
-                     curr_size += 4; /* HELLO type section header */
-                   }
-
 #ifdef DEBUG
-                 olsr_printf(5, "\tLink status of %s: ", olsr_ip_to_string(&nb->address));
-                 olsr_printf(5, "%d\n", nb->link);
+                     OLSR_PRINTF(BMSG_DBGLVL, "Sending partial(size: %d, buff left:%d)\n", curr_size, remainsize)
 #endif
-                 
-                 /*
-                  * If there is not enough room left 
-                  * for the data in tho outputbuffer
-                  * we must send a partial HELLO and
-                  * continue building the rest of the
-                  * data in a new HELLO message
-                  */
-                 if((curr_size + ipsize) > remainsize)
-                   {
-                     /* If we get here the message contains data
-                      * - no need to check 
-                      */
                      /* Complete the headers */
                      m->v4.seqno = htons(get_msg_seqno());
                      m->v4.olsr_msgsize = htons(curr_size);
                      
-                     hinfo->size = (char *)haddr - (char *)hinfo;
-                     hinfo->size = ntohs(hinfo->size);
+                     hinfo->size = htons((char *)haddr - (char *)hinfo);
                      
                      /* Send partial packet */
                      net_outbuffer_push(ifp, msg_buffer, curr_size);
-                     net_output(ifp);
                      
-                     /* Reset size and pointers */
-                     remainsize = net_outbuffer_bytes_left(ifp);
-                     curr_size = 12; /* OLSR message header */
-                     curr_size += 4; /* Hello header */
+                     curr_size = OLSR_HELLO_IPV4_HDRSIZE;
                      
                      h = &m->v4.message.hello;
                      hinfo = h->hell_info;
-                     haddr = (union olsr_ip_addr *)hinfo->neigh_addr;
-                     
-                     /* Rebuild TYPE header */
-                     memset(&hinfo->reserved, 0, sizeof(olsr_u8_t));
-                     /* Set link and status for this group of neighbors (this is the first) */
-                     hinfo->link_code = CREATE_LINK_CODE(i, j);//j | (i<<2);
-                     //printf("(2)Setting neighbor link status: %x\n", hinfo->link_code);
-                     curr_size += 4; /* HELLO type section header */
-                     
+                     haddr = (union olsr_ip_addr *)hinfo->neigh_addr;                    
+                     /* Make sure typeheader is added */
+                     first_entry = OLSR_TRUE;
                    }
-
-                 COPY_IP(haddr, &nb->address);
-
-                 //printf("\n\n1: %d\n", (char *)haddr - packet);
-                 /*
-                  *Point to next address
-                  */
-                 haddr = (union olsr_ip_addr *)&haddr->v6.s6_addr[4];
-                 curr_size += ipsize; /* IP address added */
-
-                 //printf("\n2: %d\n\n", (char *)haddr - packet); 
-                 //printf("Ipsize: %d\n", ipsize);
-
-                 //printf("Adding neighbor %s\n", olsr_ip_to_string(&nb->address));
-   
+                 
+                 net_output(ifp);                        
+                 /* Reset size and pointers */
+                 remainsize = net_outbuffer_bytes_left(ifp);
+                 
+                 /* Sanity check */
+                 check_buffspace(curr_size + ipsize + 4, remainsize, "HELLO2");
                }
-    
-           }/* looping trough neighbors */
-           
+             
+             if (first_entry)
+               {
+                 memset(&hinfo->reserved, 0, sizeof(olsr_u8_t));
+                 /* Set link and status for this group of neighbors (this is the first) */
+                 hinfo->link_code = CREATE_LINK_CODE(i, j);
+                 curr_size += 4; /* HELLO type section header */
+               }
+             
+             COPY_IP(haddr, &nb->address);
+             
+             /* Point to next address */
+             haddr = (union olsr_ip_addr *)&haddr->v6.s6_addr[4];
+             curr_size += ipsize; /* IP address added */
 
-         if (sametype && sametype > lastpacket)
+             first_entry = OLSR_FALSE;
+           }
+    
+         if(!first_entry)
            {
-             hinfo->size = (char *)haddr - (char *)hinfo;
-             hinfo->size = ntohs(hinfo->size);
+             hinfo->size = htons((char *)haddr - (char *)hinfo);
              hinfo = (struct hellinfo *)((char *)haddr);
              haddr = (union olsr_ip_addr *)&hinfo->neigh_addr;
-             
            }
        } /* for j */
     } /* for i*/
@@ -428,19 +393,8 @@ hello_build4(struct hello_message *message, struct interface *ifp)
   
   net_outbuffer_push(ifp, msg_buffer, curr_size);
 
-  /*
-   * Delete the list of neighbor messages.
-   */
-     
-  nb = message->neighbors;
-     
-  while (nb)
-    {
-      prev_nb = nb;
-      nb = nb->next;
-      free(prev_nb);
-    }
-
+  /* HELLO will always be generated */
+  return OLSR_TRUE;
 }
 
 
@@ -457,31 +411,25 @@ hello_build4(struct hello_message *message, struct interface *ifp)
  */
 
 
-static void
-hello_build6(struct hello_message *message, struct interface *ifp)
+static olsr_bool
+serialize_hello6(struct hello_message *message, struct interface *ifp)
 {
-  int remainsize, curr_size;
-  struct hello_neighbor *nb, *prev_nb;
+  olsr_u16_t remainsize, curr_size;
+  struct hello_neighbor *nb;
   union olsr_message *m;
   struct hellomsg6 *h6;
   struct hellinfo6 *hinfo6;
   union olsr_ip_addr *haddr;
+  int i, j;
+  olsr_bool first_entry;
 
-  int i, j, sametype;
-  int lastpacket = 0; /* number of neighbors with the same
-                        greater link status in the last packet */
-  if((!message) || (!ifp) || (ipversion != AF_INET6))
-    return;
-
+  if((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET6))
+    return OLSR_FALSE;
 
   remainsize = net_outbuffer_bytes_left(ifp);
-
-  //printf("HELLO build outputsize: %d\n", outputsize);
-
   m = (union olsr_message *)msg_buffer;
 
-  curr_size = 24; /* OLSR message header */
-  curr_size += 4; /* Hello header */
+  curr_size = OLSR_HELLO_IPV6_HDRSIZE; /* OLSR message header */
 
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
@@ -489,8 +437,8 @@ hello_build6(struct hello_message *message, struct interface *ifp)
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
+  check_buffspace(curr_size + ipsize + 4, remainsize, "HELLO");
 
-  //printf("HELLO build outputsize: %d\n", outputsize);
   h6 = &m->v6.message.hello;
   hinfo6 = h6->hell_info;
   haddr = (union olsr_ip_addr *)hinfo6->neigh_addr;
@@ -503,16 +451,12 @@ hello_build6(struct hello_message *message, struct interface *ifp)
   COPY_IP(&m->v6.originator, &main_addr);
   m->v6.olsr_msgtype = HELLO_MESSAGE;
 
-  m->v6.olsr_vtime = ifp->is_wireless ? hello_vtime : hello_nw_vtime;
+  m->v6.olsr_vtime = ifp->valtimes.hello;
   
   /* Fill packet header */
   h6->willingness = message->willingness; 
-
-  h6->htime = ifp->is_wireless ? htime : htime_nw;
-
+  h6->htime = ifp->hello_etime;
   memset(&h6->reserved, 0, sizeof(olsr_u16_t));
-  
-  
 
   /*
    *Loops trough all possible neighbor statuses
@@ -523,124 +467,87 @@ hello_build6(struct hello_message *message, struct interface *ifp)
     {
       for(j = 0; j <= MAX_LINK; j++)
        {
-         
-         
-         lastpacket = sametype = 0;
-         
-         
-         //printf("Neighbortype %d outputsize %d\n", i, outputsize);
+         first_entry = OLSR_TRUE;
                  
          /*
           *Looping trough neighbors
           */
          for (nb = message->neighbors; nb != NULL; nb = nb->next) 
            {         
-             if ((nb->status == i) && (nb->link == j))
-               {             
-                 sametype++;
-                 if (sametype == 1)
-                   {
-                     /* Check if there is room for header + one address */
-                     if((curr_size + 4 + ipsize) > remainsize)
-                       {
-                         /* Only send partial HELLO if it contains data */
-                         if(curr_size > (24 + 4))
-                           {
-                             /* Complete the headers */
-                             m->v6.seqno = htons(get_msg_seqno());
-                             m->v6.olsr_msgsize = htons(curr_size);
-                             
-                             hinfo6->size = (char *)haddr - (char *)hinfo6;
-                             hinfo6->size = ntohs(hinfo6->size);
-                             
-                             /* Send partial packet */
-                             net_outbuffer_push(ifp, msg_buffer, curr_size);
-                             curr_size = 24; /* OLSR message header */
-                             curr_size += 4; /* Hello header */
-                             
-                             h6 = &m->v6.message.hello;
-                             hinfo6 = h6->hell_info;
-                             haddr = (union olsr_ip_addr *)hinfo6->neigh_addr;
-                           }
-                         net_output(ifp);
-                         /* Reset size and pointers */
-                         remainsize = net_outbuffer_bytes_left(ifp);
-                       }
-                     memset(&hinfo6->reserved, 0, sizeof(olsr_u8_t));
-                     /* Set link and status for this group of neighbors (this is the first) */
-                     hinfo6->link_code = CREATE_LINK_CODE(i, j);//j | (i<<2);
-                     //printf("(2)Setting neighbor link status: %x\n", hinfo->link_code);
-                     curr_size += 4; /* HELLO type section header */
-                   }
+             if ((nb->status != i) || (nb->link != j))
+               continue;
 
 #ifdef DEBUG
-                 olsr_printf(5, "\tLink status of %s: ", olsr_ip_to_string(&nb->address));
-                 olsr_printf(5, "%d\n", nb->link);
-#endif
-
-                 /*
-                  * If there is not enough room left 
-                  * for the data in the outputbuffer
-                  * we must send a partial HELLO and
-                  * continue building the rest of the
-                  * data in a new HELLO message
-                  */
-                 if((curr_size + ipsize) > remainsize)
+             OLSR_PRINTF(BMSG_DBGLVL, "\t%s - ", olsr_ip_to_string(&nb->address))
+             OLSR_PRINTF(BMSG_DBGLVL, "L:%d N:%d\n", j, i)
+#endif           
+
+
+             /*
+              * If there is not enough room left 
+              * for the data in the outputbuffer
+              * we must send a partial HELLO and
+              * continue building the rest of the
+              * data in a new HELLO message
+              *
+              * If this is the first neighbor in 
+              * a group, we must check for an extra
+              * 4 bytes
+              */
+             if((curr_size + ipsize + (first_entry ? 4 : 0)) > remainsize)
+               {
+                 /* Only send partial HELLO if it contains data */
+                 if(curr_size > OLSR_HELLO_IPV6_HDRSIZE)
                    {
-                     /* If we get here the message contains data
-                      * - no need to check 
-                      */
+#ifdef DEBUG
+                     OLSR_PRINTF(BMSG_DBGLVL, "Sending partial(size: %d, buff left:%d)\n", curr_size, remainsize)
+#endif
                      /* Complete the headers */
                      m->v6.seqno = htons(get_msg_seqno());
                      m->v6.olsr_msgsize = htons(curr_size);
-                     
+                         
                      hinfo6->size = (char *)haddr - (char *)hinfo6;
-                     hinfo6->size = ntohs(hinfo6->size);
-                     
+                     hinfo6->size = htons(hinfo6->size);
+                         
                      /* Send partial packet */
-                         net_outbuffer_push(ifp, msg_buffer, curr_size);
-                     curr_size = 24; /* OLSR message header */
-                     curr_size += 4; /* Hello header */
-                     
+                     net_outbuffer_push(ifp, msg_buffer, curr_size);
+                     curr_size = OLSR_HELLO_IPV6_HDRSIZE;
+                         
                      h6 = &m->v6.message.hello;
                      hinfo6 = h6->hell_info;
                      haddr = (union olsr_ip_addr *)hinfo6->neigh_addr;
-                     
-                     /* Rebuild TYPE header */
-                     memset(&hinfo6->reserved, 0, sizeof(olsr_u8_t));
-                     /* Set link and status for this group of neighbors (this is the first) */
-                     hinfo6->link_code = CREATE_LINK_CODE(i, j);//j | (i<<2);
-                     //printf("(2)Setting neighbor link status: %x\n", hinfo->link_code);
-                     curr_size += 4; /* HELLO type section header */
-
-                     net_output(ifp);                
-                     /* Reset size */
-                     remainsize = net_outbuffer_bytes_left(ifp);
-                     
+                     /* Make sure typeheader is added */
+                     first_entry = OLSR_TRUE;
                    }
+                 net_output(ifp);
+                 /* Reset size and pointers */
+                 remainsize = net_outbuffer_bytes_left(ifp);
+
+                 check_buffspace(curr_size + ipsize + 4, remainsize, "HELLO2");
+                     
+               }
 
-                 COPY_IP(haddr, &nb->address);
+             if(first_entry)
+               {
+                 memset(&hinfo6->reserved, 0, sizeof(olsr_u8_t));
+                 /* Set link and status for this group of neighbors (this is the first) */
+                 hinfo6->link_code = CREATE_LINK_CODE(i, j);
+                 curr_size += 4; /* HELLO type section header */
+               }
                  
-                 //printf("\n\n1: %d\n", (char *)haddr - packet);
-                 /*
-                  *Point to next address
-                  */
-                 haddr++;
-                 curr_size += ipsize; /* IP address added */ 
-                 //printf("\n2: %d\n\n", (char *)haddr - packet); 
-                 //printf("Ipsize: %d\n", ipsize);
+             COPY_IP(haddr, &nb->address);
                  
-                 //printf("Adding neighbor %s\n", olsr_ip_to_string(&nb->address));
+             /* Point to next address */
+             haddr++;
+             curr_size += ipsize; /* IP address added */ 
                  
-               }
-             
+             first_entry = OLSR_FALSE;
            }/* looping trough neighbors */
-           
          
-         if (sametype && sametype > lastpacket)
+         
+         if (!first_entry)
            {
-             hinfo6->size = (char *)haddr - (char *)hinfo6;
-             hinfo6->size = ntohs(hinfo6->size);
+             hinfo6->size = htons((char *)haddr - (char *)hinfo6);
              hinfo6 = (struct hellinfo6 *)((char *)haddr);
              haddr = (union olsr_ip_addr *)&hinfo6->neigh_addr;
            }
@@ -653,25 +560,12 @@ hello_build6(struct hello_message *message, struct interface *ifp)
 
   net_outbuffer_push(ifp, msg_buffer, curr_size);
 
-  /*
-   * Delete the list of neighbor messages.
-   */
-     
-  nb = message->neighbors;
-     
-  while (nb)
-    {
-      prev_nb = nb;
-      nb = nb->next;
-      free(prev_nb);
-    }
-
+  /* HELLO is always buildt */
+  return OLSR_TRUE;
 }
 
 
 
-
-
 /**
  *IP version 4
  *
@@ -682,19 +576,19 @@ hello_build6(struct hello_message *message, struct interface *ifp)
  *@return nada
  */
 
-static void
-tc_build4(struct tc_message *message, struct interface *ifp)           
+static olsr_bool
+serialize_tc4(struct tc_message *message, struct interface *ifp)           
 {
 
-  int remainsize, curr_size;;
-  struct tc_mpr_addr *mprs, *prev_mprs;
+  olsr_u16_t remainsize, curr_size;
+  struct tc_mpr_addr *mprs;
   union olsr_message *m;
   struct tcmsg *tc;
   struct neigh_info *mprsaddr; 
-  int found = 0, partial_sent = 0;
+  olsr_bool found = OLSR_FALSE, partial_sent = OLSR_FALSE;
 
-  if((!message) || (!ifp) || (ipversion != AF_INET))
-    return;
+  if((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET))
+    return OLSR_FALSE;
 
   remainsize = net_outbuffer_bytes_left(ifp);
 
@@ -704,8 +598,7 @@ tc_build4(struct tc_message *message, struct interface *ifp)
 
 
   mprsaddr = tc->neigh;
-  curr_size = 12; /* OLSR message header */
-  curr_size += 4; /* TC header */
+  curr_size = OLSR_TC_IPV4_HDRSIZE;
 
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
@@ -713,9 +606,10 @@ tc_build4(struct tc_message *message, struct interface *ifp)
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
+  check_buffspace(curr_size, remainsize, "TC");
 
   /* Fill header */
-  m->v4.olsr_vtime = tc_vtime;
+  m->v4.olsr_vtime = ifp->valtimes.tc;
   m->v4.olsr_msgtype = TC_MESSAGE;
   m->v4.hopcnt = message->hop_count;
   m->v4.ttl = message->ttl;
@@ -734,8 +628,12 @@ tc_build4(struct tc_message *message, struct interface *ifp)
        {
 
          /* Only add TC message if it contains data */
-         if(curr_size > (12 + 4 ))
+         if(curr_size > OLSR_TC_IPV4_HDRSIZE)
            {
+#ifdef DEBUG
+             OLSR_PRINTF(BMSG_DBGLVL, "Sending partial(size: %d, buff left:%d)\n", curr_size, remainsize)
+#endif
+
              m->v4.olsr_msgsize = htons(curr_size);
              m->v4.seqno = htons(get_msg_seqno());
 
@@ -743,18 +641,21 @@ tc_build4(struct tc_message *message, struct interface *ifp)
              
              /* Reset stuff */
              mprsaddr = tc->neigh;
-             curr_size = 12; /* OLSR message header */
-             curr_size += 4; /* TC header */
-             found = 0;
-             partial_sent = 1;
+             curr_size = OLSR_TC_IPV4_HDRSIZE;
+             found = OLSR_FALSE;
+             partial_sent = OLSR_TRUE;
            }
 
          net_output(ifp);
          remainsize = net_outbuffer_bytes_left(ifp);
+         check_buffspace(curr_size + ipsize, remainsize, "TC2");
 
        }
-      found = 1;
-      
+      found = OLSR_TRUE;
+#ifdef DEBUG
+         OLSR_PRINTF(BMSG_DBGLVL, "\t%s\n", 
+                     olsr_ip_to_string(&mprs->address))
+#endif 
       COPY_IP(&mprsaddr->addr, &mprs->address);
 
       curr_size += ipsize;
@@ -772,38 +673,25 @@ tc_build4(struct tc_message *message, struct interface *ifp)
     }
   else
     {
-      if((!partial_sent) && (!TIMED_OUT(&send_empty_tc)))
+      if((!partial_sent) && (!TIMED_OUT(send_empty_tc)))
        {
-         olsr_printf(1, "TC: Sending empty package\n");
+         if(!TIMED_OUT(send_empty_tc))
+           OLSR_PRINTF(1, "TC: Sending empty package - (%d/%d/%d/%d)\n", partial_sent, (int)send_empty_tc, (int)now_times, (int)((send_empty_tc) - now_times))
 
          m->v4.olsr_msgsize = htons(curr_size);
          m->v4.seqno = htons(get_msg_seqno());
 
          net_outbuffer_push(ifp, msg_buffer, curr_size);
 
+         found = OLSR_TRUE;
        }
     }
 
-
-  /*
-   * Delete the list of mprs messages
-   */
-       
-  mprs = message->multipoint_relay_selector_address;
-       
-  while (mprs)
-    {
-      prev_mprs = mprs;
-      mprs = mprs->next;
-      free(prev_mprs);
-    }
-       
-       
+  return found;        
 }
 
 
 
-
 /**
  *IP version 6
  *
@@ -814,19 +702,19 @@ tc_build4(struct tc_message *message, struct interface *ifp)
  *@return nada
  */
 
-static void
-tc_build6(struct tc_message *message, struct interface *ifp)           
+static olsr_bool
+serialize_tc6(struct tc_message *message, struct interface *ifp)           
 {
 
-  int remainsize, curr_size;;
-  struct tc_mpr_addr *mprs, *prev_mprs;
+  olsr_u16_t remainsize, curr_size;
+  struct tc_mpr_addr *mprs;
   union olsr_message *m;
   struct tcmsg6 *tc6;
   struct neigh_info6 *mprsaddr6; 
-  int found = 0, partial_sent = 0;
+  olsr_bool found = OLSR_FALSE, partial_sent = OLSR_FALSE;
 
-  if ((!message) || (!ifp) || (ipversion != AF_INET6))
-    return;
+  if ((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET6))
+    return OLSR_FALSE;
 
   remainsize = net_outbuffer_bytes_left(ifp);
 
@@ -835,8 +723,7 @@ tc_build6(struct tc_message *message, struct interface *ifp)
   tc6 = &m->v6.message.tc;
 
   mprsaddr6 = tc6->neigh;
-  curr_size = 24; /* OLSR message header */
-  curr_size += 4; /* TC header */
+  curr_size = OLSR_TC_IPV6_HDRSIZE;
 
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
@@ -844,9 +731,10 @@ tc_build6(struct tc_message *message, struct interface *ifp)
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
+  check_buffspace(curr_size, remainsize, "TC");
 
   /* Fill header */
-  m->v6.olsr_vtime = tc_vtime;
+  m->v6.olsr_vtime = ifp->valtimes.tc;
   m->v6.olsr_msgtype = TC_MESSAGE;
   m->v6.hopcnt = message->hop_count;
   m->v6.ttl = message->ttl;
@@ -865,27 +753,30 @@ tc_build6(struct tc_message *message, struct interface *ifp)
       if((curr_size + ipsize) > remainsize)
        {
          /* Only add TC message if it contains data */
-         if(curr_size > (24 + 4 ))
+         if(curr_size > OLSR_TC_IPV6_HDRSIZE)
            {
+#ifdef DEBUG
+             OLSR_PRINTF(BMSG_DBGLVL, "Sending partial(size: %d, buff left:%d)\n", curr_size, remainsize)
+#endif
              m->v6.olsr_msgsize = htons(curr_size);
              m->v6.seqno = htons(get_msg_seqno());
 
              net_outbuffer_push(ifp, msg_buffer, curr_size);
              mprsaddr6 = tc6->neigh;
-             curr_size = 24; /* OLSR message header */
-             curr_size += 4; /* TC header */
-             found = 0;
-             partial_sent = 1;
+             curr_size = OLSR_TC_IPV6_HDRSIZE;
+             found = OLSR_FALSE;
+             partial_sent = OLSR_TRUE;
            }
          net_output(ifp);
          remainsize = net_outbuffer_bytes_left(ifp);
-               
+         check_buffspace(curr_size + ipsize, remainsize, "TC2");
 
        }
-      found = 1;
-
-      //printf("mprsaddr6 is %x\n", (char *)mprsaddr6 - packet);
-      //printf("Adding MPR-selector: %s\n", olsr_ip_to_string(&mprs->address));fflush(stdout);     
+      found = OLSR_TRUE;
+#ifdef DEBUG
+         OLSR_PRINTF(BMSG_DBGLVL, "\t%s\n", 
+                     olsr_ip_to_string(&mprs->address))
+#endif
       COPY_IP(&mprsaddr6->addr, &mprs->address);
       curr_size += ipsize;
 
@@ -902,31 +793,20 @@ tc_build6(struct tc_message *message, struct interface *ifp)
     }
   else
     {
-      if((!partial_sent) && (!TIMED_OUT(&send_empty_tc)))
+      if((!partial_sent) && (!TIMED_OUT(send_empty_tc)))
        {
-         olsr_printf(1, "TC: Sending empty package\n");
+         OLSR_PRINTF(1, "TC: Sending empty package\n")
            
          m->v6.olsr_msgsize = htons(curr_size);
          m->v6.seqno = htons(get_msg_seqno());
 
          net_outbuffer_push(ifp, msg_buffer, curr_size);
+
+         found = OLSR_TRUE;
        }
     }
 
-  /*
-   * Delete the list of mprs messages
-   */
-       
-  mprs = message->multipoint_relay_selector_address;
-       
-  while (mprs)
-    {
-      prev_mprs = mprs;
-      mprs = mprs->next;
-      free(prev_mprs);
-    }
-       
-       
+  return found;        
 }
 
 
@@ -940,24 +820,24 @@ tc_build6(struct tc_message *message, struct interface *ifp)
  *@return 1 on success
  */
 
-static void
-mid_build4(struct interface *ifp)
+static olsr_bool
+serialize_mid4(struct interface *ifp)
 {
-  int remainsize, curr_size;
+  olsr_u16_t remainsize, curr_size;
   /* preserve existing data in output buffer */
   union olsr_message *m;
   struct midaddr *addrs;
   struct interface *ifs;  
 
-  if((ipversion != AF_INET) || (!ifp) || (nbinterf <= 1))
-    return;
+  if((olsr_cnf->ip_version != AF_INET) || (!ifp) || (ifnet == NULL || ifnet->int_next == NULL))
+    return OLSR_FALSE;
 
 
   remainsize = net_outbuffer_bytes_left(ifp);
 
   m = (union olsr_message *)msg_buffer;
 
-  curr_size = 12; /* OLSR message header */
+  curr_size = OLSR_MID_IPV4_HDRSIZE;
 
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
@@ -965,6 +845,7 @@ mid_build4(struct interface *ifp)
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
+  check_buffspace(curr_size, remainsize, "MID");
 
   /* Fill header */
   m->v4.hopcnt = 0;
@@ -972,7 +853,7 @@ mid_build4(struct interface *ifp)
   /* Set main(first) address */
   COPY_IP(&m->v4.originator, &main_addr);
   m->v4.olsr_msgtype = MID_MESSAGE;
-  m->v4.olsr_vtime = mid_vtime;
+  m->v4.olsr_vtime = ifp->valtimes.mid;
  
   addrs = m->v4.message.mid.mid_addr;
 
@@ -985,19 +866,28 @@ mid_build4(struct interface *ifp)
          if((curr_size + ipsize) > remainsize)
            {
              /* Only add MID message if it contains data */
-             if(curr_size > 12)
+             if(curr_size > OLSR_MID_IPV4_HDRSIZE)
                {
+#ifdef DEBUG
+                 OLSR_PRINTF(BMSG_DBGLVL, "Sending partial(size: %d, buff left:%d)\n", curr_size, remainsize)
+#endif
                  /* set size */
                  m->v4.olsr_msgsize = htons(curr_size);
                  m->v4.seqno = htons(get_msg_seqno());/* seqnumber */
                  
                  net_outbuffer_push(ifp, msg_buffer, curr_size);
-                 curr_size = 12; /* OLSR message header */
+                 curr_size = OLSR_MID_IPV4_HDRSIZE;
                  addrs = m->v4.message.mid.mid_addr;
                }
              net_output(ifp);
              remainsize = net_outbuffer_bytes_left(ifp);
+             check_buffspace(curr_size, remainsize, "MID2");
            }
+#ifdef DEBUG
+         OLSR_PRINTF(BMSG_DBGLVL, "\t%s(%s)\n", 
+                     olsr_ip_to_string(&ifs->ip_addr), 
+                     ifs->int_name)
+#endif
          
          COPY_IP(&addrs->addr, &ifs->ip_addr);
          addrs++;
@@ -1013,7 +903,7 @@ mid_build4(struct interface *ifp)
   net_outbuffer_push(ifp, msg_buffer, curr_size);
 
 
-  return;
+  return OLSR_TRUE;
 }
 
 
@@ -1026,10 +916,10 @@ mid_build4(struct interface *ifp)
  *@return 1 on success
  */
 
-static void
-mid_build6(struct interface *ifp)
+static olsr_bool
+serialize_mid6(struct interface *ifp)
 {
-  int remainsize, curr_size;
+  olsr_u16_t remainsize, curr_size;
   /* preserve existing data in output buffer */
   union olsr_message *m;
   struct midaddr6 *addrs6;
@@ -1038,12 +928,12 @@ mid_build6(struct interface *ifp)
   //printf("\t\tGenerating mid on %s\n", ifn->int_name);
 
 
-  if((ipversion != AF_INET6) || (!ifp) || (nbinterf <= 1))
-    return;
+  if((olsr_cnf->ip_version != AF_INET6) || (!ifp) || (ifnet == NULL || ifnet->int_next == NULL))
+    return OLSR_FALSE;
 
   remainsize = net_outbuffer_bytes_left(ifp);
 
-  curr_size = 24; /* OLSR message header */
+  curr_size = OLSR_MID_IPV6_HDRSIZE;
 
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
@@ -1051,6 +941,7 @@ mid_build6(struct interface *ifp)
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
+  check_buffspace(curr_size + ipsize, remainsize, "MID");
 
   m = (union olsr_message *)msg_buffer;
     
@@ -1058,7 +949,7 @@ mid_build6(struct interface *ifp)
   m->v6.hopcnt = 0;
   m->v6.ttl = MAX_TTL;      
   m->v6.olsr_msgtype = MID_MESSAGE;
-  m->v6.olsr_vtime = mid_vtime;
+  m->v6.olsr_vtime = ifp->valtimes.mid;
   /* Set main(first) address */
   COPY_IP(&m->v6.originator, &main_addr);
    
@@ -1073,19 +964,28 @@ mid_build6(struct interface *ifp)
          if((curr_size + ipsize) > remainsize)
            {
              /* Only add MID message if it contains data */
-             if(curr_size > 24)
+             if(curr_size > OLSR_MID_IPV6_HDRSIZE)
                {
+#ifdef DEBUG
+                 OLSR_PRINTF(BMSG_DBGLVL, "Sending partial(size: %d, buff left:%d)\n", curr_size, remainsize)
+#endif
                  /* set size */
                  m->v6.olsr_msgsize = htons(curr_size);
                  m->v6.seqno = htons(get_msg_seqno());/* seqnumber */
                  
                  net_outbuffer_push(ifp, msg_buffer, curr_size);
-                 curr_size = 24; /* OLSR message header */
+                 curr_size = OLSR_MID_IPV6_HDRSIZE;
                  addrs6 = m->v6.message.mid.mid_addr;
                }
              net_output(ifp);
              remainsize = net_outbuffer_bytes_left(ifp);
+             check_buffspace(curr_size + ipsize, remainsize, "MID2");
            }
+#ifdef DEBUG
+                 OLSR_PRINTF(BMSG_DBGLVL, "\t%s(%s)\n", 
+                             olsr_ip_to_string(&ifs->ip_addr), 
+                             ifs->int_name)
+#endif
 
          COPY_IP(&addrs6->addr, &ifs->ip_addr);
          addrs6++;
@@ -1099,7 +999,7 @@ mid_build6(struct interface *ifp)
   //printf("Sending MID (%d bytes)...\n", outputsize);
   net_outbuffer_push(ifp, msg_buffer, curr_size);
 
-  return;
+  return OLSR_TRUE;
 }
 
 
@@ -1111,65 +1011,74 @@ mid_build6(struct interface *ifp)
  *@param ifp the interface to send on
  *@return nada
  */
-static void
-hna_build4(struct interface *ifp)
+static olsr_bool
+serialize_hna4(struct interface *ifp)
 {
-  int remainsize, curr_size;
+  olsr_u16_t remainsize, curr_size;
   /* preserve existing data in output buffer */
   union olsr_message *m;
   struct hnapair *pair;
-  struct local_hna_entry *h;
+  struct hna4_entry *h = olsr_cnf->hna4_entries;
 
   /* No hna nets */
-  if((ipversion != AF_INET) || (!ifp) || (local_hna4_set.next == &local_hna4_set))
-    return;
+  if((olsr_cnf->ip_version != AF_INET) || (!ifp) || h == NULL)
+    return OLSR_FALSE;
     
   remainsize = net_outbuffer_bytes_left(ifp);
-
-  curr_size = 12; /* OLSR message header */
-
+  
+  curr_size = OLSR_HNA_IPV4_HDRSIZE;
+  
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
     {
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
+  check_buffspace(curr_size, remainsize, "HNA");
 
   m = (union olsr_message *)msg_buffer;
-    
-
+  
+  
   /* Fill header */
   COPY_IP(&m->v4.originator, &main_addr);
   m->v4.hopcnt = 0;
   m->v4.ttl = MAX_TTL;
   m->v4.olsr_msgtype = HNA_MESSAGE;
-  m->v4.olsr_vtime = hna_vtime;
-
+  m->v4.olsr_vtime = ifp->valtimes.hna;
+  
 
   pair = m->v4.message.hna.hna_net;
-
-  for(h = local_hna4_set.next;
-      h != &local_hna4_set;
-      h = h->next)
+  
+  while(h)
     {
       if((curr_size + (2 * ipsize)) > remainsize)
        {
          /* Only add HNA message if it contains data */
-         if(curr_size > 12)
+         if(curr_size > OLSR_HNA_IPV4_HDRSIZE)
            {
+#ifdef DEBUG
+             OLSR_PRINTF(BMSG_DBGLVL, "Sending partial(size: %d, buff left:%d)\n", curr_size, remainsize)
+#endif
              m->v4.seqno = htons(get_msg_seqno());
              m->v4.olsr_msgsize = htons(curr_size);
              net_outbuffer_push(ifp, msg_buffer, curr_size);
-             curr_size = 12; /* OLSR message header */
+             curr_size = OLSR_HNA_IPV4_HDRSIZE;
              pair = m->v4.message.hna.hna_net;
            }
          net_output(ifp);
          remainsize = net_outbuffer_bytes_left(ifp);
+         check_buffspace(curr_size + (2 * ipsize), remainsize, "HNA2");
        }
-      COPY_IP(&pair->addr, &h->A_network_addr);
-      COPY_IP(&pair->netmask, &h->A_netmask);
+#ifdef DEBUG
+      OLSR_PRINTF(BMSG_DBGLVL, "\tNet: %s/%s\n", 
+                 olsr_ip_to_string(&h->net),
+                 olsr_ip_to_string(&h->netmask))
+#endif
+      COPY_IP(&pair->addr, &h->net);
+      COPY_IP(&pair->netmask, &h->netmask);
       pair++;
       curr_size += (2 * ipsize);
+      h = h->next;
     }
 
   m->v4.seqno = htons(get_msg_seqno());
@@ -1178,7 +1087,7 @@ hna_build4(struct interface *ifp)
   net_outbuffer_push(ifp, msg_buffer, curr_size);
 
   //printf("Sending HNA (%d bytes)...\n", outputsize);
-  return;
+  return OLSR_FALSE;
 }
 
 
@@ -1191,24 +1100,24 @@ hna_build4(struct interface *ifp)
  *@param ifp the interface to send on
  *@return nada
  */
-static void
-hna_build6(struct interface *ifp)
+static olsr_bool
+serialize_hna6(struct interface *ifp)
 {
-  int remainsize, curr_size;
+  olsr_u16_t remainsize, curr_size;
   /* preserve existing data in output buffer */
   union olsr_message *m;
   struct hnapair6 *pair6;
   union olsr_ip_addr tmp_netmask;
-  struct local_hna_entry *h;
+  struct hna6_entry *h = olsr_cnf->hna6_entries;
   
   /* No hna nets */
-  if((ipversion != AF_INET6) || (!ifp) || (local_hna6_set.next == &local_hna6_set))
-    return;
+  if((olsr_cnf->ip_version != AF_INET6) || (!ifp) || h == NULL)
+    return OLSR_FALSE;
 
     
   remainsize = net_outbuffer_bytes_left(ifp);
 
-  curr_size = 24; /* OLSR message header */
+  curr_size = OLSR_HNA_IPV6_HDRSIZE;
 
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
@@ -1216,6 +1125,7 @@ hna_build6(struct interface *ifp)
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
+  check_buffspace(curr_size, remainsize, "HNA");
 
   m = (union olsr_message *)msg_buffer;   
 
@@ -1224,45 +1134,50 @@ hna_build6(struct interface *ifp)
   m->v6.hopcnt = 0;
   m->v6.ttl = MAX_TTL;
   m->v6.olsr_msgtype = HNA_MESSAGE;
-  m->v6.olsr_vtime = hna_vtime;
+  m->v6.olsr_vtime = ifp->valtimes.hna;
 
   pair6 = m->v6.message.hna.hna_net;
 
 
-  for(h = local_hna6_set.next;
-      h != &local_hna6_set;
-      h = h->next)
+  while(h)
     {
       if((curr_size + (2 * ipsize)) > remainsize)
        {
          /* Only add HNA message if it contains data */
-         if(curr_size > 24)
+         if(curr_size > OLSR_HNA_IPV6_HDRSIZE)
            {
+#ifdef DEBUG
+             OLSR_PRINTF(BMSG_DBGLVL, "Sending partial(size: %d, buff left:%d)\n", curr_size, remainsize)
+#endif
              m->v6.seqno = htons(get_msg_seqno());
              m->v6.olsr_msgsize = htons(curr_size);
              net_outbuffer_push(ifp, msg_buffer, curr_size);
-             curr_size = 24; /* OLSR message header */
+             curr_size = OLSR_HNA_IPV6_HDRSIZE;
              pair6 = m->v6.message.hna.hna_net;
            }
          net_output(ifp);
          remainsize = net_outbuffer_bytes_left(ifp);
+         check_buffspace(curr_size + (2 * ipsize), remainsize, "HNA2");
        }
-
-      //printf("Adding %s\n", olsr_ip_to_string(&h->hna_net.addr));
-      COPY_IP(&pair6->addr, &h->A_network_addr);
-      olsr_prefix_to_netmask(&tmp_netmask, h->A_netmask.v6);
+#ifdef DEBUG
+      OLSR_PRINTF(BMSG_DBGLVL, "\tNet: %s/%d\n", 
+                 olsr_ip_to_string(&h->net),
+                 h->prefix_len)
+#endif
+      COPY_IP(&pair6->addr, &h->net);
+      olsr_prefix_to_netmask(&tmp_netmask, h->prefix_len);
       COPY_IP(&pair6->netmask, &tmp_netmask);
       pair6++;
       curr_size += (2 * ipsize);
+      h = h->next;
     }
   
   m->v6.olsr_msgsize = htons(curr_size);
   m->v6.seqno = htons(get_msg_seqno());
-
+  
   net_outbuffer_push(ifp, msg_buffer, curr_size);
   
   //printf("Sending HNA (%d bytes)...\n", outputsize);
-  return;
+  return OLSR_FALSE;
 
 }
-