Contiki 3.x
netflood.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of 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  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Best-effort network flooding (netflood)
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup rimenetflood
42  * @{
43  */
44 
45 #include "net/rime/netflood.h"
46 
47 #include <string.h>
48 
49 #define HOPS_MAX 16
50 
51 struct netflood_hdr {
52  uint16_t originator_seqno;
53  linkaddr_t originator;
54  uint16_t hops;
55 };
56 
57 #define DEBUG 0
58 #if DEBUG
59 #include <stdio.h>
60 #define PRINTF(...) printf(__VA_ARGS__)
61 #else
62 #define PRINTF(...)
63 #endif
64 
65 /*---------------------------------------------------------------------------*/
66 static int
67 send(struct netflood_conn *c)
68 {
69  PRINTF("%d.%d: netflood send to ipolite\n",
71  return ipolite_send(&c->c, c->queue_time, 4);
72 }
73 /*---------------------------------------------------------------------------*/
74 static void
75 recv_from_ipolite(struct ipolite_conn *ipolite, const linkaddr_t *from)
76 {
77  struct netflood_conn *c = (struct netflood_conn *)ipolite;
78  struct netflood_hdr hdr;
79  uint8_t hops;
80  struct queuebuf *queuebuf;
81 
82  memcpy(&hdr, packetbuf_dataptr(), sizeof(struct netflood_hdr));
83  hops = hdr.hops;
84 
85  /* Remember packet if we need to forward it. */
86  queuebuf = queuebuf_new_from_packetbuf();
87 
88  packetbuf_hdrreduce(sizeof(struct netflood_hdr));
89  if(c->u->recv != NULL) {
90  if(!(linkaddr_cmp(&hdr.originator, &c->last_originator) &&
91  hdr.originator_seqno <= c->last_originator_seqno)) {
92 
93  if(c->u->recv(c, from, &hdr.originator, hdr.originator_seqno,
94  hops)) {
95 
96  if(queuebuf != NULL) {
97  queuebuf_to_packetbuf(queuebuf);
98  queuebuf_free(queuebuf);
99  queuebuf = NULL;
100  memcpy(&hdr, packetbuf_dataptr(), sizeof(struct netflood_hdr));
101 
102  /* Rebroadcast received packet. */
103  if(hops < HOPS_MAX) {
104  PRINTF("%d.%d: netflood rebroadcasting %d.%d/%d (%d.%d/%d) hops %d\n",
106  hdr.originator.u8[0], hdr.originator.u8[1],
107  hdr.originator_seqno,
108  c->last_originator.u8[0], c->last_originator.u8[1],
109  c->last_originator_seqno,
110  hops);
111  hdr.hops++;
112  memcpy(packetbuf_dataptr(), &hdr, sizeof(struct netflood_hdr));
113  send(c);
114  linkaddr_copy(&c->last_originator, &hdr.originator);
115  c->last_originator_seqno = hdr.originator_seqno;
116  }
117  }
118  }
119  }
120  }
121  if(queuebuf != NULL) {
122  queuebuf_free(queuebuf);
123  }
124 }
125 /*---------------------------------------------------------------------------*/
126 static void
127 sent(struct ipolite_conn *ipolite)
128 {
129  struct netflood_conn *c = (struct netflood_conn *)ipolite;
130  if(c->u->sent != NULL) {
131  c->u->sent(c);
132  }
133 }
134 /*---------------------------------------------------------------------------*/
135 static void
136 dropped(struct ipolite_conn *ipolite)
137 {
138  struct netflood_conn *c = (struct netflood_conn *)ipolite;
139  if(c->u->dropped != NULL) {
140  c->u->dropped(c);
141  }
142 }
143 /*---------------------------------------------------------------------------*/
144 static const struct ipolite_callbacks netflood = {recv_from_ipolite, sent, dropped};
145 /*---------------------------------------------------------------------------*/
146 void
147 netflood_open(struct netflood_conn *c, clock_time_t queue_time,
148  uint16_t channel, const struct netflood_callbacks *u)
149 {
150  ipolite_open(&c->c, channel, 1, &netflood);
151  c->u = u;
152  c->queue_time = queue_time;
153 }
154 /*---------------------------------------------------------------------------*/
155 void
156 netflood_close(struct netflood_conn *c)
157 {
158  ipolite_close(&c->c);
159 }
160 /*---------------------------------------------------------------------------*/
161 int
162 netflood_send(struct netflood_conn *c, uint8_t seqno)
163 {
164  if(packetbuf_hdralloc(sizeof(struct netflood_hdr))) {
165  struct netflood_hdr *hdr = packetbuf_hdrptr();
166  linkaddr_copy(&hdr->originator, &linkaddr_node_addr);
167  linkaddr_copy(&c->last_originator, &hdr->originator);
168  c->last_originator_seqno = hdr->originator_seqno = seqno;
169  hdr->hops = 0;
170  PRINTF("%d.%d: netflood sending '%s'\n",
172  (char *)packetbuf_dataptr());
173  return ipolite_send(&c->c, 0, 4);
174  }
175  return 0;
176 }
177 /*---------------------------------------------------------------------------*/
178 void
179 netflood_cancel(struct netflood_conn *c)
180 {
181  ipolite_cancel(&c->c);
182 }
183 /*---------------------------------------------------------------------------*/
184 /** @} */
A structure with callback functions for an ipolite connection.
Definition: ipolite.h:113
int packetbuf_hdralloc(int size)
Extend the header of the packetbuf, for outbound packets.
Definition: packetbuf.c:172
linkaddr_t linkaddr_node_addr
The Rime address of the node.
Definition: linkaddr.c:48
void ipolite_cancel(struct ipolite_conn *c)
Cancel a pending packet.
Definition: ipolite.c:181
#define NULL
The null pointer.
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
void ipolite_close(struct ipolite_conn *c)
Close an ipolite connection.
Definition: ipolite.c:133
void ipolite_open(struct ipolite_conn *c, uint16_t channel, uint8_t dups, const struct ipolite_callbacks *cb)
Open an ipolite connection.
Definition: ipolite.c:123
void * packetbuf_hdrptr(void)
Get a pointer to the header in the packetbuf, for outbound packets.
Definition: packetbuf.c:213
void(* dropped)(struct ipolite_conn *c)
Called when a packet is dropped because a packet was heard from a neighbor.
Definition: ipolite.h:128
An opaque structure with no user-visible elements that holds the state of an ipolite connection...
Definition: ipolite.h:135
Header file for the best-effort network flooding (netflood)
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two Rime addresses.
Definition: linkaddr.c:66
void(* sent)(struct ipolite_conn *c)
Called when a packet is sent on the connection.
Definition: ipolite.h:122
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
int ipolite_send(struct ipolite_conn *c, clock_time_t interval, uint8_t hdrsize)
Send a packet on an ipolite connection.
Definition: ipolite.c:144
int packetbuf_hdrreduce(int size)
Reduce the header in the packetbuf, for incoming packets.
Definition: packetbuf.c:188