ARM crosscompiler fixes
[olsrd.git] / src / build_msg.c
index af6375c..d8b569b 100644 (file)
@@ -19,7 +19,7 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  * 
  * 
- * $ Id $
+ * $Id: build_msg.c,v 1.17 2004/10/20 19:43:04 kattemat Exp $
  *
  */
 
@@ -96,7 +96,7 @@ hna_build6(struct interface *);
 void
 hello_build(struct hello_message *message, struct interface *ifp)
 {
-  switch(ipversion)
+  switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
       hello_build4(message, ifp);
@@ -134,7 +134,7 @@ hello_build(struct hello_message *message, struct interface *ifp)
 void
 tc_build(struct tc_message *message, struct interface *ifp)           
 {
-  switch(ipversion)
+  switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
       tc_build4(message, ifp);
@@ -160,7 +160,7 @@ tc_build(struct tc_message *message, struct interface *ifp)
 void
 mid_build(struct interface *ifn)
 {
-  switch(ipversion)
+  switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
       mid_build4(ifn);
@@ -185,7 +185,7 @@ mid_build(struct interface *ifn)
 void
 hna_build(struct interface *ifp)
 {
-  switch(ipversion)
+  switch(olsr_cnf->ip_version)
     {
     case(AF_INET):
       hna_build4(ifp);
@@ -228,10 +228,10 @@ hello_build4(struct hello_message *message, struct interface *ifp)
   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))
+  if((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET))
     return;
 
-  remainsize = net_outbuffer_bytes_left();
+  remainsize = net_outbuffer_bytes_left(ifp);
 
   //printf("HELLO build outputsize: %d\n", outputsize);
 
@@ -244,7 +244,7 @@ hello_build4(struct hello_message *message, struct interface *ifp)
   if(curr_size > remainsize)
     {
       net_output(ifp);
-      remainsize = net_outbuffer_bytes_left();
+      remainsize = net_outbuffer_bytes_left(ifp);
     }
 
   h = &m->v4.message.hello;
@@ -260,17 +260,12 @@ hello_build4(struct hello_message *message, struct interface *ifp)
   /* Set source(main) addr */
   COPY_IP(&m->v4.originator, &main_addr);
 
-  if(ifp->is_wireless)
-    m->v4.olsr_vtime = hello_vtime;
-  else
-    m->v4.olsr_vtime = hello_nw_vtime;
+  m->v4.olsr_vtime = ifp->valtimes.hello;
 
   /* Fill HELLO header */
   h->willingness = message->willingness; 
-  if(ifp->is_wireless)
-    h->htime = htime;
-  else
-    h->htime = htime_nw;
+  h->htime = ifp->hello_etime;
+
   memset(&h->reserved, 0, sizeof(olsr_u16_t));
   
 
@@ -308,16 +303,95 @@ hello_build4(struct hello_message *message, struct interface *ifp)
                  sametype++;
                  if (sametype == 1)
                    {
+
+                     /*
+                      * 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);
 #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);
+                     
+                     /* 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 */
+                     
+                     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 */
+                     
+                   }
 
                  COPY_IP(haddr, &nb->address);
 
@@ -326,6 +400,7 @@ hello_build4(struct hello_message *message, struct interface *ifp)
                   *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);
@@ -349,10 +424,9 @@ hello_build4(struct hello_message *message, struct interface *ifp)
     } /* for i*/
      
   m->v4.seqno = htons(get_msg_seqno());
-  m->v4.olsr_msgsize = (char *)hinfo - (char *)m;
-  m->v4.olsr_msgsize = htons(m->v4.olsr_msgsize);
+  m->v4.olsr_msgsize = htons(curr_size);
   
-  net_outbuffer_push(msg_buffer, (char *)hinfo - (char *)m);
+  net_outbuffer_push(ifp, msg_buffer, curr_size);
 
   /*
    * Delete the list of neighbor messages.
@@ -396,11 +470,11 @@ hello_build6(struct hello_message *message, struct interface *ifp)
   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))
+  if((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET6))
     return;
 
 
-  remainsize = net_outbuffer_bytes_left();
+  remainsize = net_outbuffer_bytes_left(ifp);
 
   //printf("HELLO build outputsize: %d\n", outputsize);
 
@@ -413,7 +487,7 @@ hello_build6(struct hello_message *message, struct interface *ifp)
   if(curr_size > remainsize)
     {
       net_output(ifp);
-      remainsize = net_outbuffer_bytes_left();
+      remainsize = net_outbuffer_bytes_left(ifp);
     }
 
   //printf("HELLO build outputsize: %d\n", outputsize);
@@ -428,17 +502,14 @@ hello_build6(struct hello_message *message, struct interface *ifp)
   /* Set source(main) addr */
   COPY_IP(&m->v6.originator, &main_addr);
   m->v6.olsr_msgtype = HELLO_MESSAGE;
-  if(ifp->is_wireless)
-    m->v6.olsr_vtime = hello_vtime;
-  else
-    m->v6.olsr_vtime = hello_nw_vtime;
+
+  m->v6.olsr_vtime = ifp->valtimes.hello;
   
   /* Fill packet header */
   h6->willingness = message->willingness; 
-  if(ifp->is_wireless)
-    h6->htime = htime;
-  else
-    h6->htime = htime_nw;
+
+  h6->htime = ifp->hello_etime;
+
   memset(&h6->reserved, 0, sizeof(olsr_u16_t));
   
   
@@ -469,10 +540,37 @@ hello_build6(struct hello_message *message, struct interface *ifp)
                  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 */
                    }
 
 #ifdef DEBUG
@@ -480,6 +578,47 @@ hello_build6(struct hello_message *message, struct interface *ifp)
                  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)
+                   {
+                     /* If we get here the message contains data
+                      * - no need to check 
+                      */
+                     /* 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;
+                     
+                     /* 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);
+                     
+                   }
+
                  COPY_IP(haddr, &nb->address);
                  
                  //printf("\n\n1: %d\n", (char *)haddr - packet);
@@ -487,7 +626,7 @@ hello_build6(struct hello_message *message, struct interface *ifp)
                   *Point to next address
                   */
                  haddr++;
-                 
+                 curr_size += ipsize; /* IP address added */ 
                  //printf("\n2: %d\n\n", (char *)haddr - packet); 
                  //printf("Ipsize: %d\n", ipsize);
                  
@@ -510,10 +649,9 @@ hello_build6(struct hello_message *message, struct interface *ifp)
     } /* for i */
 
   m->v6.seqno = htons(get_msg_seqno());
-  m->v6.olsr_msgsize = (char *)hinfo6 - (char *)m;
-  m->v6.olsr_msgsize = htons(m->v6.olsr_msgsize);
+  m->v6.olsr_msgsize = htons(curr_size);
 
-  net_outbuffer_push(msg_buffer, (char *)hinfo6 - (char *)m);
+  net_outbuffer_push(ifp, msg_buffer, curr_size);
 
   /*
    * Delete the list of neighbor messages.
@@ -548,17 +686,17 @@ static void
 tc_build4(struct tc_message *message, struct interface *ifp)           
 {
 
-  int remainsize, curr_size;;
+  int remainsize, curr_size;
   struct tc_mpr_addr *mprs, *prev_mprs;
   union olsr_message *m;
   struct tcmsg *tc;
   struct neigh_info *mprsaddr; 
-  int found = 0;
+  int found = 0, partial_sent = 0;
 
-  if((!message) || (!ifp) || (ipversion != AF_INET))
+  if((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET))
     return;
 
-  remainsize = net_outbuffer_bytes_left();
+  remainsize = net_outbuffer_bytes_left(ifp);
 
   m = (union olsr_message *)msg_buffer;
 
@@ -573,11 +711,11 @@ tc_build4(struct tc_message *message, struct interface *ifp)
   if(curr_size > remainsize)
     {
       net_output(ifp);
-      remainsize = net_outbuffer_bytes_left();
+      remainsize = net_outbuffer_bytes_left(ifp);
     }
 
   /* 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;
@@ -595,21 +733,25 @@ tc_build4(struct tc_message *message, struct interface *ifp)
       if((curr_size + ipsize) > remainsize)
        {
 
-         olsr_printf(1, "Chomping TC!\n");
+         /* Only add TC message if it contains data */
+         if(curr_size > (12 + 4 ))
+           {
+             m->v4.olsr_msgsize = htons(curr_size);
+             m->v4.seqno = htons(get_msg_seqno());
 
-         m->v4.olsr_msgsize = htons(curr_size);
-         m->v4.seqno = htons(get_msg_seqno());
+             net_outbuffer_push(ifp, msg_buffer, curr_size);
+             
+             /* Reset stuff */
+             mprsaddr = tc->neigh;
+             curr_size = 12; /* OLSR message header */
+             curr_size += 4; /* TC header */
+             found = 0;
+             partial_sent = 1;
+           }
 
-         net_outbuffer_push(msg_buffer, curr_size);
          net_output(ifp);
-         remainsize = net_outbuffer_bytes_left();
+         remainsize = net_outbuffer_bytes_left(ifp);
 
-         /* Reset stuff */
-         mprsaddr = tc->neigh;
-         curr_size = 12; /* OLSR message header */
-         curr_size += 4; /* TC header */
-
-         found = 0;
        }
       found = 1;
       
@@ -625,19 +767,19 @@ tc_build4(struct tc_message *message, struct interface *ifp)
       m->v4.olsr_msgsize = htons(curr_size);
       m->v4.seqno = htons(get_msg_seqno());
       
-      net_outbuffer_push(msg_buffer, curr_size);
+      net_outbuffer_push(ifp, msg_buffer, curr_size);
 
     }
   else
     {
-      if(!TIMED_OUT(&send_empty_tc))
+      if((!partial_sent) && (!TIMED_OUT(&send_empty_tc)))
        {
          olsr_printf(1, "TC: Sending empty package\n");
 
          m->v4.olsr_msgsize = htons(curr_size);
          m->v4.seqno = htons(get_msg_seqno());
 
-         net_outbuffer_push(msg_buffer, curr_size);
+         net_outbuffer_push(ifp, msg_buffer, curr_size);
 
        }
     }
@@ -676,17 +818,17 @@ static void
 tc_build6(struct tc_message *message, struct interface *ifp)           
 {
 
-  int remainsize, curr_size;;
+  int remainsize, curr_size;
   struct tc_mpr_addr *mprs, *prev_mprs;
   union olsr_message *m;
   struct tcmsg6 *tc6;
   struct neigh_info6 *mprsaddr6; 
-  int found = 0;
+  int found = 0, partial_sent = 0;
 
-  if ((!message) || (!ifp) || (ipversion != AF_INET6))
+  if ((!message) || (!ifp) || (olsr_cnf->ip_version != AF_INET6))
     return;
 
-  remainsize = net_outbuffer_bytes_left();
+  remainsize = net_outbuffer_bytes_left(ifp);
 
   m = (union olsr_message *)msg_buffer;
 
@@ -700,11 +842,11 @@ tc_build6(struct tc_message *message, struct interface *ifp)
   if(curr_size > remainsize)
     {
       net_output(ifp);
-      remainsize = net_outbuffer_bytes_left();
+      remainsize = net_outbuffer_bytes_left(ifp);
     }
 
   /* 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;
@@ -722,25 +864,30 @@ tc_build6(struct tc_message *message, struct interface *ifp)
       /*If packet is to be chomped */
       if((curr_size + ipsize) > remainsize)
        {
-         m->v6.olsr_msgsize = htons(curr_size);
-         m->v6.seqno = htons(get_msg_seqno());
+         /* Only add TC message if it contains data */
+         if(curr_size > (24 + 4 ))
+           {
+             m->v6.olsr_msgsize = htons(curr_size);
+             m->v6.seqno = htons(get_msg_seqno());
 
-         net_outbuffer_push(msg_buffer, curr_size);
+             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;
+           }
          net_output(ifp);
-         remainsize = net_outbuffer_bytes_left();
-
-         mprsaddr6 = tc6->neigh;
-         curr_size = 24; /* OLSR message header */
-         curr_size += 4; /* TC header */
+         remainsize = net_outbuffer_bytes_left(ifp);
                
 
-         found = 0;
        }
       found = 1;
 
       //printf("mprsaddr6 is %x\n", (char *)mprsaddr6 - packet);
       //printf("Adding MPR-selector: %s\n", olsr_ip_to_string(&mprs->address));fflush(stdout);     
       COPY_IP(&mprsaddr6->addr, &mprs->address);
+      curr_size += ipsize;
 
       mprsaddr6++;
     }
@@ -750,19 +897,19 @@ tc_build6(struct tc_message *message, struct interface *ifp)
       m->v6.olsr_msgsize = htons(curr_size);
       m->v6.seqno = htons(get_msg_seqno());
 
-      net_outbuffer_push(msg_buffer, curr_size);
+      net_outbuffer_push(ifp, msg_buffer, curr_size);
 
     }
   else
     {
-      if(!TIMED_OUT(&send_empty_tc))
+      if((!partial_sent) && (!TIMED_OUT(&send_empty_tc)))
        {
          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(msg_buffer, curr_size);
+         net_outbuffer_push(ifp, msg_buffer, curr_size);
        }
     }
 
@@ -802,11 +949,11 @@ mid_build4(struct interface *ifp)
   struct midaddr *addrs;
   struct interface *ifs;  
 
-  if((ipversion != AF_INET) || (!ifp) || (nbinterf <= 1))
+  if((olsr_cnf->ip_version != AF_INET) || (!ifp) || (ifnet == NULL || ifnet->int_next == NULL))
     return;
 
 
-  remainsize = net_outbuffer_bytes_left();
+  remainsize = net_outbuffer_bytes_left(ifp);
 
   m = (union olsr_message *)msg_buffer;
 
@@ -816,7 +963,7 @@ mid_build4(struct interface *ifp)
   if(curr_size > remainsize)
     {
       net_output(ifp);
-      remainsize = net_outbuffer_bytes_left();
+      remainsize = net_outbuffer_bytes_left(ifp);
     }
 
   /* Fill header */
@@ -825,7 +972,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;
 
@@ -837,15 +984,19 @@ mid_build4(struct interface *ifp)
 
          if((curr_size + ipsize) > remainsize)
            {
-             /* set size */
-             m->v4.olsr_msgsize = htons(curr_size);
-             m->v4.seqno = htons(get_msg_seqno());/* seqnumber */
-             
-             net_outbuffer_push(msg_buffer, curr_size);
+             /* Only add MID message if it contains data */
+             if(curr_size > 12)
+               {
+                 /* 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 */
+                 addrs = m->v4.message.mid.mid_addr;
+               }
              net_output(ifp);
-             remainsize = net_outbuffer_bytes_left();
-             curr_size = 12; /* OLSR message header */
-             addrs = m->v4.message.mid.mid_addr;
+             remainsize = net_outbuffer_bytes_left(ifp);
            }
          
          COPY_IP(&addrs->addr, &ifs->ip_addr);
@@ -859,7 +1010,7 @@ mid_build4(struct interface *ifp)
   m->v4.olsr_msgsize = htons(curr_size);
 
   //printf("Sending MID (%d bytes)...\n", outputsize);
-  net_outbuffer_push(msg_buffer, curr_size);
+  net_outbuffer_push(ifp, msg_buffer, curr_size);
 
 
   return;
@@ -887,10 +1038,10 @@ mid_build6(struct interface *ifp)
   //printf("\t\tGenerating mid on %s\n", ifn->int_name);
 
 
-  if((ipversion != AF_INET6) || (!ifp) || (nbinterf <= 1))
+  if((olsr_cnf->ip_version != AF_INET6) || (!ifp) || (ifnet == NULL || ifnet->int_next == NULL))
     return;
 
-  remainsize = net_outbuffer_bytes_left();
+  remainsize = net_outbuffer_bytes_left(ifp);
 
   curr_size = 24; /* OLSR message header */
 
@@ -898,7 +1049,7 @@ mid_build6(struct interface *ifp)
   if(curr_size > remainsize)
     {
       net_output(ifp);
-      remainsize = net_outbuffer_bytes_left();
+      remainsize = net_outbuffer_bytes_left(ifp);
     }
 
   m = (union olsr_message *)msg_buffer;
@@ -907,7 +1058,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);
    
@@ -921,15 +1072,19 @@ mid_build6(struct interface *ifp)
        {
          if((curr_size + ipsize) > remainsize)
            {
-             /* set size */
-             m->v6.olsr_msgsize = htons(curr_size);
-             m->v6.seqno = htons(get_msg_seqno());/* seqnumber */
-             
-             net_outbuffer_push(msg_buffer, curr_size);
+             /* Only add MID message if it contains data */
+             if(curr_size > 24)
+               {
+                 /* 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 */
+                 addrs6 = m->v6.message.mid.mid_addr;
+               }
              net_output(ifp);
-             remainsize = net_outbuffer_bytes_left();
-             curr_size = 24; /* OLSR message header */
-             addrs6 = m->v6.message.mid.mid_addr;
+             remainsize = net_outbuffer_bytes_left(ifp);
            }
 
          COPY_IP(&addrs6->addr, &ifs->ip_addr);
@@ -942,7 +1097,7 @@ mid_build6(struct interface *ifp)
   m->v6.seqno = htons(get_msg_seqno());/* seqnumber */
 
   //printf("Sending MID (%d bytes)...\n", outputsize);
-  net_outbuffer_push(msg_buffer, curr_size);
+  net_outbuffer_push(ifp, msg_buffer, curr_size);
 
   return;
 }
@@ -963,61 +1118,63 @@ hna_build4(struct interface *ifp)
   /* 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))
+  if((olsr_cnf->ip_version != AF_INET) || (!ifp) || h == NULL)
     return;
     
-  remainsize = net_outbuffer_bytes_left();
-
+  remainsize = net_outbuffer_bytes_left(ifp);
+  
   curr_size = 12; /* OLSR message header */
-
+  
   /* Send pending packet if not room in buffer */
   if(curr_size > remainsize)
     {
       net_output(ifp);
-      remainsize = net_outbuffer_bytes_left();
+      remainsize = net_outbuffer_bytes_left(ifp);
     }
-
+  
   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)
        {
-         m->v4.seqno = htons(get_msg_seqno());
-         m->v4.olsr_msgsize = htons(curr_size);
-
-         net_outbuffer_push(msg_buffer, curr_size);
+         /* Only add HNA message if it contains data */
+         if(curr_size > 12)
+           {
+             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 */
+             pair = m->v4.message.hna.hna_net;
+           }
          net_output(ifp);
-         remainsize = net_outbuffer_bytes_left();
-         curr_size = 12; /* OLSR message header */
-         pair = m->v4.message.hna.hna_net;
+         remainsize = net_outbuffer_bytes_left(ifp);
        }
-      COPY_IP(&pair->addr, &h->A_network_addr);
-      COPY_IP(&pair->netmask, &h->A_netmask);
+      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());
   m->v4.olsr_msgsize = htons(curr_size);
 
-  net_outbuffer_push(msg_buffer, curr_size);
+  net_outbuffer_push(ifp, msg_buffer, curr_size);
 
   //printf("Sending HNA (%d bytes)...\n", outputsize);
   return;
@@ -1041,14 +1198,14 @@ hna_build6(struct interface *ifp)
   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))
+  if((olsr_cnf->ip_version != AF_INET6) || (!ifp) || h == NULL)
     return;
 
     
-  remainsize = net_outbuffer_bytes_left();
+  remainsize = net_outbuffer_bytes_left(ifp);
 
   curr_size = 24; /* OLSR message header */
 
@@ -1056,7 +1213,7 @@ hna_build6(struct interface *ifp)
   if(curr_size > remainsize)
     {
       net_output(ifp);
-      remainsize = net_outbuffer_bytes_left();
+      remainsize = net_outbuffer_bytes_left(ifp);
     }
 
   m = (union olsr_message *)msg_buffer;   
@@ -1066,39 +1223,41 @@ 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)
        {
-         m->v6.seqno = htons(get_msg_seqno());
-         m->v6.olsr_msgsize = htons(curr_size);
-
-         net_outbuffer_push(msg_buffer, curr_size);
+         /* Only add HNA message if it contains data */
+         if(curr_size > 24)
+           {
+             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 */
+             pair6 = m->v6.message.hna.hna_net;
+           }
          net_output(ifp);
-         remainsize = net_outbuffer_bytes_left();
-         curr_size = 24; /* OLSR message header */
-         pair6 = m->v6.message.hna.hna_net;
+         remainsize = net_outbuffer_bytes_left(ifp);
        }
-
+      
       //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);
+      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(msg_buffer, curr_size);
+  
+  net_outbuffer_push(ifp, msg_buffer, curr_size);
   
   //printf("Sending HNA (%d bytes)...\n", outputsize);
   return;