Contiki 3.x
uip-mcast6-route.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Loughborough University - Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29  * OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 /**
32  * \file
33  * Multicast routing table manipulation
34  *
35  * \author
36  * George Oikonomou - <oikonomou@users.sourceforge.net>
37  */
38 
39 #include "contiki.h"
40 #include "lib/list.h"
41 #include "lib/memb.h"
42 #include "net/ip/uip.h"
44 
45 #include <stdint.h>
46 #include <string.h>
47 
48 #if UIP_CONF_IPV6
49 /*---------------------------------------------------------------------------*/
50 /* Size of the multicast routing table */
51 #ifdef UIP_MCAST6_ROUTE_CONF_ROUTES
52 #define UIP_MCAST6_ROUTE_ROUTES UIP_MCAST6_ROUTE_CONF_ROUTES
53 #else
54 #define UIP_MCAST6_ROUTE_ROUTES 1
55 #endif /* UIP_CONF_DS6_MCAST_ROUTES */
56 /*---------------------------------------------------------------------------*/
57 LIST(mcast_route_list);
58 MEMB(mcast_route_memb, uip_mcast6_route_t, UIP_MCAST6_ROUTE_ROUTES);
59 
60 static uip_mcast6_route_t *locmcastrt;
61 /*---------------------------------------------------------------------------*/
63 uip_mcast6_route_lookup(uip_ipaddr_t *group)
64 {
65  locmcastrt = NULL;
66  for(locmcastrt = list_head(mcast_route_list);
67  locmcastrt != NULL;
68  locmcastrt = list_item_next(locmcastrt)) {
69  if(uip_ipaddr_cmp(&locmcastrt->group, group)) {
70  return locmcastrt;
71  }
72  }
73 
74  return NULL;
75 }
76 /*---------------------------------------------------------------------------*/
78 uip_mcast6_route_add(uip_ipaddr_t *group)
79 {
80  /* _lookup must return NULL, i.e. the prefix does not exist in our table */
81  locmcastrt = uip_mcast6_route_lookup(group);
82  if(locmcastrt == NULL) {
83  /* Allocate an entry and add the group to the list */
84  locmcastrt = memb_alloc(&mcast_route_memb);
85  if(locmcastrt == NULL) {
86  return NULL;
87  }
88  list_add(mcast_route_list, locmcastrt);
89  }
90 
91  /* Reaching here means we either found the prefix or allocated a new one */
92 
93  uip_ipaddr_copy(&(locmcastrt->group), group);
94 
95  return locmcastrt;
96 }
97 /*---------------------------------------------------------------------------*/
98 void
99 uip_mcast6_route_rm(uip_mcast6_route_t *route)
100 {
101  /* Make sure it's actually in the list */
102  for(locmcastrt = list_head(mcast_route_list);
103  locmcastrt != NULL;
104  locmcastrt = list_item_next(locmcastrt)) {
105  if(locmcastrt == route) {
106  list_remove(mcast_route_list, route);
107  memb_free(&mcast_route_memb, route);
108  return;
109  }
110  }
111 }
112 /*---------------------------------------------------------------------------*/
114 uip_mcast6_route_list_head(void)
115 {
116  return list_head(mcast_route_list);
117 }
118 /*---------------------------------------------------------------------------*/
119 int
120 uip_mcast6_route_count(void)
121 {
122  return list_length(mcast_route_list);
123 }
124 /*---------------------------------------------------------------------------*/
125 void
127 {
128  memb_init(&mcast_route_memb);
129  list_init(mcast_route_list);
130 }
131 /*---------------------------------------------------------------------------*/
132 
133 #endif /* UIP_CONF_IPV6 */
Linked list manipulation routines.
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
Header file for the uIP TCP/IP stack.
void * list_item_next(void *item)
Get the next item following this item.
Definition: list.c:325
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
#define NULL
The null pointer.
An entry in the multicast routing table.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
void uip_mcast6_route_init()
Multicast routing table init routine.
int list_length(list_t list)
Get the length of a list.
Definition: list.c:275
void list_init(list_t list)
Initialize a list.
Definition: list.c:66
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
#define LIST(name)
Declare a linked list.
Definition: list.h:86
Multicast routing table manipulation
Memory block allocation routines.