Contiki-NG
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 * \addtogroup uip-multicast
33 * @{
34 */
35/**
36 * \file
37 * Multicast routing table manipulation
38 *
39 * \author
40 * George Oikonomou - <oikonomou@users.sourceforge.net>
41 */
42
43#include "contiki.h"
44#include "lib/list.h"
45#include "lib/memb.h"
46#include "net/ipv6/uip.h"
48
49#include <stdint.h>
50#include <string.h>
51
52/*---------------------------------------------------------------------------*/
53/* Size of the multicast routing table */
54#ifdef UIP_MCAST6_ROUTE_CONF_ROUTES
55#define UIP_MCAST6_ROUTE_ROUTES UIP_MCAST6_ROUTE_CONF_ROUTES
56#else
57#define UIP_MCAST6_ROUTE_ROUTES 1
58#endif /* UIP_CONF_DS6_MCAST_ROUTES */
59/*---------------------------------------------------------------------------*/
60LIST(mcast_route_list);
61MEMB(mcast_route_memb, uip_mcast6_route_t, UIP_MCAST6_ROUTE_ROUTES);
62
63static uip_mcast6_route_t *locmcastrt;
64/*---------------------------------------------------------------------------*/
66uip_mcast6_route_lookup(uip_ipaddr_t *group)
67{
68 locmcastrt = NULL;
69 for(locmcastrt = list_head(mcast_route_list);
70 locmcastrt != NULL;
71 locmcastrt = list_item_next(locmcastrt)) {
72 if(uip_ipaddr_cmp(&locmcastrt->group, group)) {
73 return locmcastrt;
74 }
75 }
76
77 return NULL;
78}
79/*---------------------------------------------------------------------------*/
81uip_mcast6_route_add(uip_ipaddr_t *group)
82{
83 /* _lookup must return NULL, i.e. the prefix does not exist in our table */
84 locmcastrt = uip_mcast6_route_lookup(group);
85 if(locmcastrt == NULL) {
86 /* Allocate an entry and add the group to the list */
87 locmcastrt = memb_alloc(&mcast_route_memb);
88 if(locmcastrt == NULL) {
89 return NULL;
90 }
91 list_add(mcast_route_list, locmcastrt);
92 }
93
94 /* Reaching here means we either found the prefix or allocated a new one */
95
96 uip_ipaddr_copy(&(locmcastrt->group), group);
97
98 return locmcastrt;
99}
100/*---------------------------------------------------------------------------*/
101void
103{
104 /* Make sure it's actually in the list */
105 for(locmcastrt = list_head(mcast_route_list);
106 locmcastrt != NULL;
107 locmcastrt = list_item_next(locmcastrt)) {
108 if(locmcastrt == route) {
109 list_remove(mcast_route_list, route);
110 memb_free(&mcast_route_memb, route);
111 return;
112 }
113 }
114}
115/*---------------------------------------------------------------------------*/
118{
119 return list_head(mcast_route_list);
120}
121/*---------------------------------------------------------------------------*/
122int
124{
125 return list_length(mcast_route_list);
126}
127/*---------------------------------------------------------------------------*/
128void
130{
131 memb_init(&mcast_route_memb);
132 list_init(mcast_route_list);
133}
134/*---------------------------------------------------------------------------*/
135/** @} */
void list_init(list_t list)
Initialize a list.
Definition: list.c:57
#define LIST(name)
Declare a linked list.
Definition: list.h:89
int list_length(const_list_t list)
Get the length of a list.
Definition: list.c:178
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:89
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
Definition: list.c:152
void * list_item_next(const void *item)
Get the next item following this item.
Definition: list.c:203
void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Definition: list.c:63
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:78
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
void memb_init(struct memb *m)
Initialize a memory block that was declared with MEMB().
Definition: memb.c:52
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:91
int uip_mcast6_route_count(void)
Retrieve the count of multicast routes.
uip_mcast6_route_t * uip_mcast6_route_list_head(void)
Retrieve a pointer to the start of the multicast routes list.
uip_mcast6_route_t * uip_mcast6_route_add(uip_ipaddr_t *group)
Add a multicast route.
void uip_mcast6_route_rm(uip_mcast6_route_t *route)
Remove a multicast route.
uip_mcast6_route_t * uip_mcast6_route_lookup(uip_ipaddr_t *group)
Lookup a multicast route.
void uip_mcast6_route_init()
Multicast routing table init routine.
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:969
Linked list manipulation routines.
Memory block allocation routines.
An entry in the multicast routing table.
uip_ipaddr_t group
The multicast group.
Header file for multicast routing table manipulation.
Header file for the uIP TCP/IP stack.