General cleanups, removed unused variables and declarations
[olsrd.git] / src / packet.c
1 /*
2  * OLSR ad-hoc routing table management protocol
3  * Copyright (C) 2004 Andreas T√łnnesen (andreto@ifi.uio.no)
4  *
5  * This file is part of the olsr.org OLSR daemon.
6  *
7  * olsr.org is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * olsr.org is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with olsr.org; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  * 
21  * 
22  * $Id: packet.c,v 1.7 2004/10/19 20:19:32 kattemat Exp $
23  *
24  */
25
26
27 #include "defs.h"
28 #include "link_set.h"
29 #include "mpr_selector_set.h"
30 #include "mpr.h"
31 #include "olsr.h"
32
33
34 static int sending_tc = 0;
35
36 /**
37  *Build an internal HELLO package for this
38  *node. This MUST be done for each interface.
39  *
40  *@param message the hello_message struct to fill with info
41  *@param outif the interface to send the message on - messages
42  *are created individually for each interface!
43  *@return 0
44  */
45 int
46 olsr_build_hello_packet(struct hello_message *message, struct interface *outif)
47 {
48   struct hello_neighbor   *message_neighbor, *tmp_neigh;
49   struct link_entry       *links;
50   struct neighbor_entry   *neighbor;
51   olsr_u16_t              index;
52   int                     found, link;
53
54   olsr_printf(3, "\tBuilding HELLO on interface %d\n", outif->if_nr);
55
56   message->neighbors=NULL;
57   message->packet_seq_number=0;
58   
59   //message->mpr_seq_number=neighbortable.neighbor_mpr_seq;
60
61   /* Set willingness */
62
63   message->willingness = olsr_cnf->willingness;
64   //printf("Willingness: %d\n", olsr_cnf->willingness);
65
66
67   /* Set TTL */
68
69   message->ttl = 1;
70   
71   //olsr_printf(3, "mpr is %d\n",message->mpr_seq_number);
72
73   COPY_IP(&message->source_addr, &main_addr);
74
75
76   /* Get the links of this interface */
77   links = link_set;
78
79   while(links != NULL)
80     {      
81       
82       link = lookup_link_status(links);
83       /* Update the status */
84       
85       /* Update neighbor */
86       /* UPDATED ! */
87       //update_neighbor_status(links->neighbor, link);
88       //update_neighbor_status(links->neighbor);
89
90       //printf("\nLINK: %d\nSTATUS: %d\n\n", link, neighbor->status);
91       //printf("\nProcessing %s\n", olsr_ip_to_string(&links->neighbor_iface_addr));
92
93
94       /* Check if this link tuple is registered on the outgoing interface */
95       if(!COMP_IP(&links->local_iface_addr, &outif->ip_addr))
96         {
97           olsr_printf(3, "ADDR: %s - ", olsr_ip_to_string(&outif->ip_addr));
98           olsr_printf(3, "Wrong interface for %s ", olsr_ip_to_string(&links->local_iface_addr));
99
100           links = links->next;
101           continue;
102         }
103       
104       //printf("\tAdding link to %s\n", olsr_ip_to_string(&links->neighbor_main_address));
105
106
107       //printf("\tStatus: %d\n", neighbor->status);
108       //printf("\tLink: %d\n", message_neighbor->link);
109
110       message_neighbor = olsr_malloc(sizeof(struct hello_neighbor), "Build HELLO");
111       
112
113       /* Find the link status */
114       message_neighbor->link = link;
115
116    
117       /*
118        * Calculate neighbor status
119        */
120       /* 
121        * 2.1  If the main address, corresponding to
122        *      L_neighbor_iface_addr, is included in the MPR set:
123        *
124        *            Neighbor Type = MPR_NEIGH
125        */
126       if(links->neighbor->is_mpr)
127         {
128           message_neighbor->status = MPR_NEIGH;
129         }
130       /*
131        *  2.2  Otherwise, if the main address, corresponding to
132        *       L_neighbor_iface_addr, is included in the neighbor set:
133        */
134       
135       /* NOTE:
136        * It is garanteed to be included when come this far
137        * due to the extentions made in the link sensing
138        * regarding main addresses.
139        */
140       else
141         {
142           /*
143            *   2.2.1
144            *        if N_status == SYM
145            *
146            *             Neighbor Type = SYM_NEIGH
147            */
148           if(links->neighbor->status == SYM)
149             {
150               message_neighbor->status = SYM_NEIGH;
151             }
152           /*
153            *   2.2.2
154            *        Otherwise, if N_status == NOT_SYM
155            *             Neighbor Type = NOT_NEIGH
156            */
157           else
158             if(links->neighbor->status == NOT_SYM)
159               {
160                 message_neighbor->status = NOT_NEIGH;
161               }
162         }
163   
164       /* Set the remote interface address */
165       COPY_IP(&message_neighbor->address, &links->neighbor_iface_addr);
166       
167       /* Set the main address */
168       COPY_IP(&message_neighbor->main_address, &links->neighbor->neighbor_main_addr);
169       
170       olsr_printf(5, "%s - ", olsr_ip_to_string(&message_neighbor->address));
171       olsr_printf(5, " status %d\n", message_neighbor->status);
172       
173       message_neighbor->next=message->neighbors;
174       message->neighbors=message_neighbor;          
175       
176       links = links->next;
177     }
178   
179   /* Add the links */
180
181
182
183
184   /* Add the rest of the neighbors if running on multiple interfaces */
185   
186   if(ifnet != NULL && ifnet->int_next != NULL)
187     for(index=0;index<HASHSIZE;index++)
188       {       
189         for(neighbor = neighbortable[index].next;
190             neighbor != &neighbortable[index];
191             neighbor=neighbor->next)
192           {         
193             found = 0;
194             /* Check that the neighbor is not added yet */
195             tmp_neigh = message->neighbors;
196             //printf("Checking that the neighbor is not yet added\n");
197             while(tmp_neigh)
198               {
199                 if(COMP_IP(&tmp_neigh->main_address, &neighbor->neighbor_main_addr))
200                   {
201                     //printf("Not adding duplicate neighbor %s\n", olsr_ip_to_string(&neighbor->neighbor_main_addr));
202                     found = 1;
203                     break;
204                   }
205                 tmp_neigh = tmp_neigh->next;
206               }
207
208             if(found)
209               continue;
210             
211             message_neighbor = olsr_malloc(sizeof(struct hello_neighbor), "Build HELLO 2");
212             
213             message_neighbor->link = UNSPEC_LINK;
214             
215             /*
216              * Calculate neighbor status
217              */
218             /* 
219              * 2.1  If the main address, corresponding to
220              *      L_neighbor_iface_addr, is included in the MPR set:
221              *
222              *            Neighbor Type = MPR_NEIGH
223              */
224             if(neighbor->is_mpr)
225               {
226                 message_neighbor->status = MPR_NEIGH;
227               }
228             /*
229              *  2.2  Otherwise, if the main address, corresponding to
230              *       L_neighbor_iface_addr, is included in the neighbor set:
231              */
232             
233             /* NOTE:
234              * It is garanteed to be included when come this far
235              * due to the extentions made in the link sensing
236              * regarding main addresses.
237              */
238             else
239               {
240                 /*
241                  *   2.2.1
242                  *        if N_status == SYM
243                  *
244                  *             Neighbor Type = SYM_NEIGH
245                  */
246                 if(neighbor->status == SYM)
247                   {
248                     message_neighbor->status = SYM_NEIGH;
249                   }
250                 /*
251                  *   2.2.2
252                  *        Otherwise, if N_status == NOT_SYM
253                  *             Neighbor Type = NOT_NEIGH
254                  */
255                 else
256                   if(neighbor->status == NOT_SYM)
257                     {
258                       message_neighbor->status = NOT_NEIGH;                   
259                     }
260               }
261             
262
263             COPY_IP(&message_neighbor->address, &neighbor->neighbor_main_addr);
264
265             COPY_IP(&message_neighbor->main_address, &neighbor->neighbor_main_addr);
266             
267             olsr_printf(5, "%s           \n ", olsr_ip_to_string(&message_neighbor->address));
268             olsr_printf(5, " status  %d\n", message_neighbor->status);
269             
270             message_neighbor->next=message->neighbors;
271             message->neighbors=message_neighbor;            
272           }
273       }
274   
275
276   return 0;
277 }
278
279
280 /**
281  *Build an internal TC package for this
282  *node.
283  *
284  *@param message the tc_message struct to fill with info
285  *@return 0
286  */
287 int
288 olsr_build_tc_packet(struct tc_message *message)
289 {
290   struct tc_mpr_addr        *message_mpr;
291   //struct mpr_selector       *mprs;
292   olsr_u8_t              index;
293   struct neighbor_entry  *entry;
294   //struct mpr_selector_hash  *mprs_hash;
295   //olsr_u16_t          index;
296   int entry_added;
297   struct timeval tmp_timer;
298
299
300   entry_added = 0;
301
302   message->multipoint_relay_selector_address=NULL;
303   message->packet_seq_number=0;
304  
305   message->hop_count = 0;
306   message->ttl = MAX_TTL;
307   message->ansn = ansn;
308
309   COPY_IP(&message->originator, &main_addr);
310   COPY_IP(&message->source_addr, &main_addr);
311   
312
313   /* Loop trough all neighbors */  
314   for(index=0;index<HASHSIZE;index++)
315     {
316       for(entry = neighbortable[index].next;
317           entry != &neighbortable[index];
318           entry = entry->next)
319         {
320           if(entry->status != SYM)
321             continue;
322
323           switch(olsr_cnf->tc_redundancy)
324             {
325             case(2):
326               {
327                 /* 2 = Add all neighbors */
328                 //printf("\t%s\n", olsr_ip_to_string(&mprs->mpr_selector_addr));
329                 message_mpr = olsr_malloc(sizeof(struct tc_mpr_addr), "Build TC");
330                 
331                 COPY_IP(&message_mpr->address, &entry->neighbor_main_addr);
332                 message_mpr->next = message->multipoint_relay_selector_address;
333                 message->multipoint_relay_selector_address = message_mpr;
334                 entry_added = 1;
335                 
336                 break;
337               }
338             case(1):
339               {
340                 /* 1 = Add all MPR selectors and selected MPRs */
341                 if((entry->is_mpr == 1) ||
342                    (olsr_lookup_mprs_set(&entry->neighbor_main_addr) != NULL))
343                   {
344                     //printf("\t%s\n", olsr_ip_to_string(&mprs->mpr_selector_addr));
345                     message_mpr = olsr_malloc(sizeof(struct tc_mpr_addr), "Build TC 2");
346                     
347                     COPY_IP(&message_mpr->address, &entry->neighbor_main_addr);
348                     message_mpr->next = message->multipoint_relay_selector_address;
349                     message->multipoint_relay_selector_address = message_mpr;
350                     entry_added = 1;
351                   }
352                 break;
353               }
354             default:
355               {
356                 /* 0 = Add only MPR selectors(default) */
357                 if(olsr_lookup_mprs_set(&entry->neighbor_main_addr) != NULL)
358                   {
359                     //printf("\t%s\n", olsr_ip_to_string(&mprs->mpr_selector_addr));
360                     message_mpr = olsr_malloc(sizeof(struct tc_mpr_addr), "Build TC 3");
361                     
362                     COPY_IP(&message_mpr->address, &entry->neighbor_main_addr);
363                     message_mpr->next = message->multipoint_relay_selector_address;
364                     message->multipoint_relay_selector_address = message_mpr;
365                     entry_added = 1;
366                   }
367                 break;
368               }         
369           
370             } /* Switch */
371         } /* For */
372     } /* For index */
373
374   if(entry_added)
375     {
376       sending_tc = 1;
377     }
378   else
379     {
380       if(sending_tc)
381         {
382           /* Send empty TC */
383           olsr_init_timer((olsr_u32_t) (max_tc_vtime*3)*1000, &tmp_timer);
384           olsr_printf(3, "No more MPR selectors - will send empty TCs\n");
385           timeradd(&now, &tmp_timer, &send_empty_tc);
386
387           sending_tc = 0;
388         }
389     }
390
391
392   return 0;
393 }
394
395
396
397
398 /**
399  *Free the memory allocated for a HELLO packet.
400  *
401  *@param message the pointer to the packet to erase
402  *
403  *@return nada
404  */
405 void
406 olsr_destroy_hello_message(struct hello_message *message)
407 {
408   struct hello_neighbor  *neighbors;
409   struct hello_neighbor  *neighbors_tmp;
410
411
412   neighbors=message->neighbors;
413   
414   while(neighbors!=NULL)
415     {
416       neighbors_tmp=neighbors;
417       neighbors=neighbors->next;
418       free(neighbors_tmp);
419     }
420 }
421
422
423 /**
424  *Free the memory allocated for a TC packet.
425  *
426  *@param message the pointer to the packet to erase
427  *
428  *@return nada
429  */
430
431 void
432 olsr_destroy_tc_message(struct tc_message *message)
433 {
434   struct tc_mpr_addr  *mpr_set;
435   struct tc_mpr_addr  *mpr_set_tmp;
436
437   mpr_set=message->multipoint_relay_selector_address;
438
439   while( mpr_set!=NULL)
440     {
441       mpr_set_tmp=mpr_set;
442       mpr_set=mpr_set->next;
443       free(mpr_set_tmp);
444     }
445 }
446
447
448
449 /**
450  *Free the memory allocated for a HNA packet.
451  *
452  *@param message the pointer to the packet to erase
453  *
454  *@return nada
455  */
456
457 void
458 olsr_destroy_hna_message(struct hna_message *message)
459 {
460   struct hna_net_addr  *hna_tmp, *hna_tmp2;
461
462   hna_tmp = message->hna_net;
463
464   while(hna_tmp)
465     {
466       hna_tmp2 = hna_tmp;
467       hna_tmp = hna_tmp->next;
468       free(hna_tmp2);
469     }
470 }
471
472
473
474 /**
475  *Free the memory allocated for a MID packet.
476  *
477  *@param message the pointer to the packet to erase
478  *
479  *@return nada
480  */
481
482 void
483 olsr_destroy_mid_message(struct mid_message *message)
484 {
485   struct mid_alias *tmp_adr, *tmp_adr2;
486
487   tmp_adr = message->mid_addr;
488
489   while(tmp_adr)
490     {
491       tmp_adr2 = tmp_adr;
492       tmp_adr = tmp_adr->next;
493       free(tmp_adr2);
494     }
495 }
496
497