2 * The olsr.org Optimized Link-State Routing daemon(olsrd)
3 * Copyright (c) 2004, Andreas Tønnesen(andreto@olsr.org)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
16 * * Neither the name of olsr.org, olsrd nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
33 * Visit http://www.olsr.org for more information.
35 * If you find this software useful feel free to make a donation
36 * to the project. For more information see the website or contact
37 * the copyright holders.
39 * $Id: rebuild_packet.c,v 1.26 2007/11/29 00:49:39 bernd67 Exp $
43 #include "rebuild_packet.h"
52 *Process/rebuild HNA message. Converts the OLSR
53 *packet to the internal hna_message format.
54 *@param hmsg the hna_message struct in wich infomation
56 *@param m the entire OLSR message revieved.
57 *@return negative on error
61 hna_chgestruct(struct hna_message *hmsg, const union olsr_message *m)
63 struct hna_net_addr *hna_pairs, *tmp_pairs;
66 /*Check if everyting is ok*/
67 if ((!m) || (m->v4.olsr_msgtype != HNA_MESSAGE))
71 if(olsr_cnf->ip_version == AF_INET)
74 const struct hnapair *haddr = m->v4.message.hna.hna_net;
78 * nextmsg contains size of
79 * the addresses + 12 bytes(nextmessage, from address and the header)
81 no_pairs = (ntohs(m->v4.olsr_msgsize) - 12) / 8;
83 //COPY_IP(&hmsg->originator, &m->v4.originator);
84 hmsg->originator.v4.s_addr = m->v4.originator;
86 hmsg->packet_seq_number = ntohs(m->v4.seqno);
87 hmsg->hop_count = m->v4.hopcnt;
89 //printf("HNA from %s\n\n", olsr_ip_to_string(&buf, (union olsr_ip_addr *)&hmsg->originator));
92 hmsg->vtime = me_to_double(m->v4.olsr_vtime);
97 for(i = 0; i < no_pairs; i++) {
98 hna_pairs = olsr_malloc(sizeof(struct hna_net_addr), "HNA chgestruct");
100 //COPY_IP(&hna_pairs->net, &haddr->addr);
101 hna_pairs->net.v4.s_addr = haddr->addr;
102 //COPY_IP(&hna_pairs->netmask, &haddr->netmask);
103 hna_pairs->prefixlen = olsr_netmask4_to_prefix(&haddr->netmask);
105 hna_pairs->next = tmp_pairs;
106 tmp_pairs = hna_pairs;
113 const struct hnapair6 *haddr6 = m->v6.message.hna.hna_net;
116 * How many HNA pairs?
117 * nextmsg contains size of
118 * the addresses + 12 bytes(nextmessage, from address and the header)
120 no_pairs = (ntohs(m->v6.olsr_msgsize) - 24) / 32; /* NB 32 not 8 */
122 //COPY_IP(&hmsg->originator, &m->v6.originator);
123 hmsg->originator.v6 = m->v6.originator;
124 hmsg->packet_seq_number = ntohs(m->v6.seqno);
125 hmsg->hop_count = m->v6.hopcnt;
128 hmsg->vtime = me_to_double(m->v6.olsr_vtime);
133 for(i = 0; i < no_pairs; i++)
136 hna_pairs = olsr_malloc(sizeof(struct hna_net_addr), "HNA chgestruct 2");
138 //COPY_IP(&hna_pairs->net, &haddr6->addr);
139 hna_pairs->net.v6 = haddr6->addr;
140 hna_pairs->prefixlen = olsr_netmask6_to_prefix(&haddr6->netmask);
142 hna_pairs->next = tmp_pairs;
144 tmp_pairs = hna_pairs;
150 tmp_pairs = hna_pairs;
154 printf("\t net: %s ", ip_to_string(&tmp_pairs->net));
155 printf("\t mask: %s\n", ip_to_string(&tmp_pairs->netmask));
156 tmp_pairs = tmp_pairs->next;
163 hmsg->hna_net = hna_pairs;
169 *Process/rebuild MID message. Converts the OLSR
170 *packet to the internal mid_message format.
171 *@param mmsg the mid_message struct in wich infomation
173 *@param m the entire OLSR message revieved.
174 *@return negative on error
178 mid_chgestruct(struct mid_message *mmsg, const union olsr_message *m)
181 struct mid_alias *alias, *alias_tmp;
184 /* Checking if everything is ok */
185 if ((!m) || (m->v4.olsr_msgtype != MID_MESSAGE))
190 if(olsr_cnf->ip_version == AF_INET)
193 const struct midaddr *maddr = m->v4.message.mid.mid_addr;
196 * nextmsg contains size of
197 * the addresses + 12 bytes(nextmessage, from address and the header)
199 no_aliases = ((ntohs(m->v4.olsr_msgsize) - 12) / 4);
201 //printf("Aliases: %d\n", no_aliases);
202 //COPY_IP(&mmsg->mid_origaddr, &m->v4.originator);
203 mmsg->mid_origaddr.v4.s_addr = m->v4.originator;
204 //COPY_IP(&mmsg->addr, &m->v4.originator);
205 mmsg->addr.v4.s_addr = m->v4.originator;
207 mmsg->mid_seqno = ntohs(m->v4.seqno);
208 mmsg->mid_addr = NULL;
211 mmsg->vtime = me_to_double(m->v4.olsr_vtime);
213 //printf("Sequencenuber of MID from %s is %d\n", ip_to_string(&mmsg->addr), mmsg->mid_seqno);
216 for(i = 0; i < no_aliases; i++)
218 alias = olsr_malloc(sizeof(struct mid_alias), "MID chgestruct");
220 //COPY_IP(&alias->alias_addr, &maddr->addr);
221 alias->alias_addr.v4.s_addr = maddr->addr;
222 alias->next = mmsg->mid_addr;
223 mmsg->mid_addr = alias;
228 if(olsr_cnf->debug_level > 1)
231 struct ipaddr_str buf;
233 OLSR_PRINTF(3, "Alias list for %s: ", olsr_ip_to_string(&buf, &mmsg->mid_origaddr));
234 OLSR_PRINTF(3, "%s", olsr_ip_to_string(&buf, &mmsg->addr));
235 alias_tmp = mmsg->mid_addr;
238 OLSR_PRINTF(3, " - %s", olsr_ip_to_string(&buf, &alias_tmp->alias_addr));
239 alias_tmp = alias_tmp->next;
241 OLSR_PRINTF(3, "\n");
247 const struct midaddr6 *maddr6 = m->v6.message.mid.mid_addr;
250 * nextmsg contains size of
251 * the addresses + 12 bytes(nextmessage, from address and the header)
253 no_aliases = ((ntohs(m->v6.olsr_msgsize) - 12) / 16); /* NB 16 */
255 //printf("Aliases: %d\n", no_aliases);
256 //COPY_IP(&mmsg->mid_origaddr, &m->v6.originator);
257 mmsg->mid_origaddr.v6 = m->v6.originator;
258 //COPY_IP(&mmsg->addr, &m->v6.originator);
259 mmsg->addr.v6 = m->v6.originator;
261 mmsg->mid_seqno = ntohs(m->v6.seqno);
262 mmsg->mid_addr = NULL;
265 mmsg->vtime = me_to_double(m->v6.olsr_vtime);
267 //printf("Sequencenuber of MID from %s is %d\n", ip_to_string(&mmsg->addr), mmsg->mid_seqno);
269 for(i = 0; i < no_aliases; i++)
271 alias = olsr_malloc(sizeof(struct mid_alias), "MID chgestruct 2");
273 //printf("Adding alias: %s\n", olsr_ip_to_string(&buf, (union olsr_ip_addr *)&maddr6->addr));
274 //COPY_IP(&alias->alias_addr, &maddr6->addr);
275 alias->alias_addr.v6 = maddr6->addr;
276 alias->next = mmsg->mid_addr;
277 mmsg->mid_addr = alias;
283 if(olsr_cnf->debug_level > 1)
286 struct ipaddr_str buf;
288 OLSR_PRINTF(3, "Alias list for %s", ip6_to_string(&buf, &mmsg->mid_origaddr.v6));
289 OLSR_PRINTF(3, "%s", ip6_to_string(&buf, &mmsg->addr.v6));
291 alias_tmp = mmsg->mid_addr;
294 OLSR_PRINTF(3, " - %s", ip6_to_string(&buf, &alias_tmp->alias_addr.v6));
295 alias_tmp = alias_tmp->next;
297 OLSR_PRINTF(3, "\n");
307 *Process/rebuild a message of unknown type. Converts the OLSR
308 *packet to the internal unknown_message format.
309 *@param umsg the unknown_message struct in wich infomation
311 *@param m the entire OLSR message revieved.
312 *@return negative on error
316 unk_chgestruct(struct unknown_message *umsg, const union olsr_message *m)
319 /* Checking if everything is ok */
324 if(olsr_cnf->ip_version == AF_INET)
328 //COPY_IP(&umsg->originator, &m->v4.originator);
329 umsg->originator.v4.s_addr = m->v4.originator;
331 umsg->seqno = ntohs(m->v4.seqno);
333 umsg->type = m->v4.olsr_msgtype;
339 //COPY_IP(&umsg->originator, &m->v6.originator);
340 umsg->originator.v6 = m->v6.originator;
342 umsg->seqno = ntohs(m->v6.seqno);
344 umsg->type = m->v4.olsr_msgtype;
352 *Process/rebuild HELLO message. Converts the OLSR
353 *packet to the internal hello_message format.
354 *@param hmsg the hello_message struct in wich infomation
356 *@param m the entire OLSR message revieved.
357 *@return negative on error
361 hello_chgestruct(struct hello_message *hmsg, const union olsr_message *m)
363 const union olsr_ip_addr *hadr;
364 struct hello_neighbor *nb;
366 hmsg->neighbors = NULL;
368 if ((!m) || (m->v4.olsr_msgtype != HELLO_MESSAGE))
371 if(olsr_cnf->ip_version == AF_INET)
373 const struct hellinfo *hinf;
376 //COPY_IP(&hmsg->source_addr, &m->v4.originator);
377 hmsg->source_addr.v4.s_addr = m->v4.originator;
378 hmsg->packet_seq_number = ntohs(m->v4.seqno);
382 hmsg->vtime = me_to_double(m->v4.olsr_vtime);
385 hmsg->htime = me_to_double(m->v4.message.hello.htime);
388 hmsg->willingness = m->v4.message.hello.willingness;
390 OLSR_PRINTF(3, "Got HELLO vtime: %f htime: %f\n", hmsg->vtime, hmsg->htime);
392 for (hinf = m->v4.message.hello.hell_info;
393 (const char *)hinf < ((const char *)m + (ntohs(m->v4.olsr_msgsize)));
394 hinf = (const struct hellinfo *)((const char *)hinf + ntohs(hinf->size)))
397 for (hadr = (const union olsr_ip_addr *)&hinf->neigh_addr;
398 (const char *)hadr < (const char *)hinf + ntohs(hinf->size);
399 hadr = (const union olsr_ip_addr *)&hadr->v6.s6_addr[4])
401 nb = olsr_malloc(sizeof (struct hello_neighbor), "HELLO chgestruct");
403 //COPY_IP(&nb->address, hadr);
406 /* Fetch link and status */
407 nb->link = EXTRACT_LINK(hinf->link_code);
408 nb->status = EXTRACT_STATUS(hinf->link_code);
410 nb->next = hmsg->neighbors;
411 hmsg->neighbors = nb;
419 const struct hellinfo6 *hinf6;
422 //COPY_IP(&hmsg->source_addr, &m->v6.originator);
423 hmsg->source_addr.v6 = m->v6.originator;
424 //printf("parsing HELLO from %s\n", olsr_ip_to_string(&buf, &hmsg->source_addr));
425 hmsg->packet_seq_number = ntohs(m->v6.seqno);
428 hmsg->vtime = me_to_double(m->v6.olsr_vtime);
431 hmsg->htime = me_to_double(m->v6.message.hello.htime);
434 hmsg->willingness = m->v6.message.hello.willingness;
436 OLSR_PRINTF(3, "Got HELLO vtime: %f htime: %f\n", hmsg->vtime, hmsg->htime);
439 for (hinf6 = m->v6.message.hello.hell_info;
440 (const char *)hinf6 < ((const char *)m + (ntohs(m->v6.olsr_msgsize)));
441 hinf6 = (const struct hellinfo6 *)((const char *)hinf6 + ntohs(hinf6->size)))
444 for (hadr = (const union olsr_ip_addr *)hinf6->neigh_addr;
445 (const char *)hadr < (const char *)hinf6 + ntohs(hinf6->size);
448 nb = olsr_malloc(sizeof (struct hello_neighbor), "OLSR chgestruct 2");
450 //COPY_IP(&nb->address, hadr);
453 /* Fetch link and status */
454 nb->link = EXTRACT_LINK(hinf6->link_code);
455 nb->status = EXTRACT_STATUS(hinf6->link_code);
457 nb->next = hmsg->neighbors;
458 hmsg->neighbors = nb;