Fixed signed comparison warnings
[olsrd.git] / src / build_msg.c
index 8268a32..ac57b4d 100644 (file)
  * to the project. For more information see the website or contact
  * the copyright holders.
  *
- * $Id: build_msg.c,v 1.23 2004/12/12 18:57:50 kattemat Exp $
+ * $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 */
@@ -55,30 +70,30 @@ static char msg_buffer[MAXMESSAGESIZE - OLSR_HEADERSIZE];
 /* IPv4 */
 
 static olsr_bool
-hello_build4(struct hello_message *, struct interface *);
+serialize_hello4(struct hello_message *, struct interface *);
 
 static olsr_bool
-tc_build4(struct tc_message *, struct interface *);
+serialize_tc4(struct tc_message *, struct interface *);
 
 static olsr_bool
-mid_build4(struct interface *);
+serialize_mid4(struct interface *);
 
 static olsr_bool
-hna_build4(struct interface *);
+serialize_hna4(struct interface *);
 
 /* IPv6 */
 
 static olsr_bool
-hello_build6(struct hello_message *, struct interface *);
+serialize_hello6(struct hello_message *, struct interface *);
 
 static olsr_bool
-tc_build6(struct tc_message *, struct interface *);
+serialize_tc6(struct tc_message *, struct interface *);
 
 static olsr_bool
-mid_build6(struct interface *);
+serialize_mid6(struct interface *);
 
 static olsr_bool
-hna_build6(struct interface *);
+serialize_hna6(struct interface *);
 
 
 
@@ -87,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
@@ -98,14 +113,18 @@ hna_build6(struct interface *);
  */
 
 olsr_bool
-hello_build(struct hello_message *message, struct interface *ifp)
+queue_hello(struct hello_message *message, struct interface *ifp)
 {
+#ifdef DEBUG
+  OLSR_PRINTF(BMSG_DBGLVL, "Building HELLO on %s\n-------------------\n", ifp->int_name)
+#endif
+
   switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
-      return hello_build4(message, ifp);
+      return serialize_hello4(message, ifp);
     case(AF_INET6):
-      return hello_build6(message, ifp);
+      return serialize_hello6(message, ifp);
     }
   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
@@ -132,14 +144,18 @@ hello_build(struct hello_message *message, struct interface *ifp)
  */
 
 olsr_bool
-tc_build(struct tc_message *message, struct interface *ifp)           
+queue_tc(struct tc_message *message, struct interface *ifp)           
 {
+#ifdef DEBUG
+  OLSR_PRINTF(BMSG_DBGLVL, "Building TC on %s\n-------------------\n", ifp->int_name)
+#endif
+
   switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
-      return tc_build4(message, ifp);
+      return serialize_tc4(message, ifp);
     case(AF_INET6):
-      return tc_build6(message, ifp);
+      return serialize_tc6(message, ifp);
     }
   return OLSR_FALSE;
 }
@@ -154,14 +170,18 @@ tc_build(struct tc_message *message, struct interface *ifp)
  */
 
 olsr_bool
-mid_build(struct interface *ifn)
+queue_mid(struct interface *ifp)
 {
+#ifdef DEBUG
+  OLSR_PRINTF(BMSG_DBGLVL, "Building MID on %s\n-------------------\n", ifp->int_name)
+#endif
+
   switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
-      return mid_build4(ifn);
+      return serialize_mid4(ifp);
     case(AF_INET6):
-      return mid_build6(ifn);
+      return serialize_mid6(ifp);
     }
   return OLSR_FALSE;
 }
@@ -175,14 +195,18 @@ mid_build(struct interface *ifn)
  *@return nada
  */
 olsr_bool
-hna_build(struct interface *ifp)
+queue_hna(struct interface *ifp)
 {
+#ifdef DEBUG
+  OLSR_PRINTF(BMSG_DBGLVL, "Building HNA on %s\n-------------------\n", ifp->int_name)
+#endif
+
   switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
-      return hna_build4(ifp);
+      return serialize_hna4(ifp);
     case(AF_INET6):
-      return hna_build6(ifp);
+      return serialize_hna6(ifp);
     }
   return OLSR_FALSE;
 }
@@ -192,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);
+    }
+}
 
 
 /**
@@ -205,28 +239,25 @@ hna_build(struct interface *ifp)
  */
 
 static olsr_bool
-hello_build4(struct hello_message *message, struct interface *ifp)
+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 */
+  int i, j;
+  olsr_bool first_entry;
+
   if((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET))
     return OLSR_FALSE;
 
   remainsize = net_outbuffer_bytes_left(ifp);
 
-  //printf("HELLO build outputsize: %d\n", outputsize);
-
   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)
@@ -234,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;
@@ -265,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*/
@@ -416,19 +393,6 @@ 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;
 }
@@ -448,30 +412,24 @@ hello_build4(struct hello_message *message, struct interface *ifp)
 
 
 static olsr_bool
-hello_build6(struct hello_message *message, struct interface *ifp)
+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) || (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)
@@ -479,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;
@@ -497,12 +455,8 @@ hello_build6(struct hello_message *message, struct interface *ifp)
   
   /* Fill packet header */
   h6->willingness = message->willingness; 
-
   h6->htime = ifp->hello_etime;
-
   memset(&h6->reserved, 0, sizeof(olsr_u16_t));
-  
-  
 
   /*
    *Loops trough all possible neighbor statuses
@@ -513,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;
            }
@@ -643,27 +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
  *
@@ -675,11 +577,11 @@ hello_build6(struct hello_message *message, struct interface *ifp)
  */
 
 static olsr_bool
-tc_build4(struct tc_message *message, struct interface *ifp)           
+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; 
@@ -696,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)
@@ -705,6 +606,7 @@ 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 = ifp->valtimes.tc;
@@ -726,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());
 
@@ -735,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 */
+             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 = 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;
@@ -764,9 +673,10 @@ 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());
@@ -777,27 +687,11 @@ tc_build4(struct tc_message *message, struct interface *ifp)
        }
     }
 
-
-  /*
-   * 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 ? OLSR_TRUE : OLSR_FALSE;       
+  return found;        
 }
 
 
 
-
 /**
  *IP version 6
  *
@@ -809,11 +703,11 @@ tc_build4(struct tc_message *message, struct interface *ifp)
  */
 
 static olsr_bool
-tc_build6(struct tc_message *message, struct interface *ifp)           
+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; 
@@ -829,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)
@@ -838,6 +731,7 @@ 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 = ifp->valtimes.tc;
@@ -859,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 */
+             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 = OLSR_TRUE;
-
-      //printf("mprsaddr6 is %x\n", (char *)mprsaddr6 - packet);
-      //printf("Adding MPR-selector: %s\n", olsr_ip_to_string(&mprs->address));fflush(stdout);     
+#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;
 
@@ -896,9 +793,9 @@ 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());
@@ -909,20 +806,7 @@ tc_build6(struct tc_message *message, struct interface *ifp)
        }
     }
 
-  /*
-   * 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 ? OLSR_TRUE : OLSR_FALSE;       
+  return found;        
 }
 
 
@@ -937,9 +821,9 @@ tc_build6(struct tc_message *message, struct interface *ifp)
  */
 
 static olsr_bool
-mid_build4(struct interface *ifp)
+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;
@@ -953,7 +837,7 @@ mid_build4(struct interface *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)
@@ -961,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;
@@ -981,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++;
@@ -1023,9 +917,9 @@ mid_build4(struct interface *ifp)
  */
 
 static olsr_bool
-mid_build6(struct interface *ifp)
+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;
@@ -1039,7 +933,7 @@ mid_build6(struct interface *ifp)
 
   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)
@@ -1047,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;
     
@@ -1069,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++;
@@ -1108,9 +1012,9 @@ mid_build6(struct interface *ifp)
  *@return nada
  */
 static olsr_bool
-hna_build4(struct interface *ifp)
+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;
@@ -1122,7 +1026,7 @@ hna_build4(struct interface *ifp)
     
   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)
@@ -1130,7 +1034,8 @@ hna_build4(struct interface *ifp)
       net_output(ifp);
       remainsize = net_outbuffer_bytes_left(ifp);
     }
-  
+  check_buffspace(curr_size, remainsize, "HNA");
+
   m = (union olsr_message *)msg_buffer;
   
   
@@ -1149,17 +1054,26 @@ hna_build4(struct interface *ifp)
       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");
        }
+#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++;
@@ -1187,9 +1101,9 @@ hna_build4(struct interface *ifp)
  *@return nada
  */
 static olsr_bool
-hna_build6(struct interface *ifp)
+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;
@@ -1203,7 +1117,7 @@ hna_build6(struct interface *ifp)
     
   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)
@@ -1211,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;   
 
@@ -1229,19 +1144,26 @@ hna_build6(struct interface *ifp)
       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));
+#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);