2007-03-31 20:31:27 +02:00
|
|
|
/**
|
|
|
|
* \addtogroup routediscovery
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2007-03-22 18:34:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Route discovery protocol
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
2013-12-12 20:50:07 +01:00
|
|
|
#include "net/rime/rime.h"
|
2007-03-22 18:34:16 +01:00
|
|
|
#include "net/rime/route.h"
|
|
|
|
#include "net/rime/route-discovery.h"
|
|
|
|
|
|
|
|
#include <stddef.h> /* For offsetof */
|
2009-05-10 23:10:23 +02:00
|
|
|
#include <stdio.h>
|
2007-03-22 18:34:16 +01:00
|
|
|
|
|
|
|
struct route_msg {
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t dest;
|
2008-02-24 23:05:27 +01:00
|
|
|
uint8_t rreq_id;
|
|
|
|
uint8_t pad;
|
2007-03-22 18:34:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct rrep_hdr {
|
2008-02-24 23:05:27 +01:00
|
|
|
uint8_t rreq_id;
|
|
|
|
uint8_t hops;
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t dest;
|
|
|
|
linkaddr_t originator;
|
2007-03-22 18:34:16 +01:00
|
|
|
};
|
|
|
|
|
2008-11-17 23:52:10 +01:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2008-02-03 21:52:41 +01:00
|
|
|
#include "ether.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2007-03-22 18:34:16 +01:00
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
2009-10-08 18:30:26 +02:00
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
2007-03-22 18:34:16 +01:00
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2009-10-08 18:30:26 +02:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static char rrep_pending; /* A reply for a request is pending. */
|
2007-03-22 18:34:16 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
send_rreq(struct route_discovery_conn *c, const linkaddr_t *dest)
|
2007-03-22 18:34:16 +01:00
|
|
|
{
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t dest_copy;
|
2007-03-22 18:34:16 +01:00
|
|
|
struct route_msg *msg;
|
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&dest_copy, dest);
|
2009-04-06 15:16:39 +02:00
|
|
|
dest = &dest_copy;
|
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_clear();
|
|
|
|
msg = packetbuf_dataptr();
|
|
|
|
packetbuf_set_datalen(sizeof(struct route_msg));
|
2007-03-22 18:34:16 +01:00
|
|
|
|
|
|
|
msg->pad = 0;
|
2007-05-15 10:09:21 +02:00
|
|
|
msg->rreq_id = c->rreq_id;
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&msg->dest, dest);
|
2007-03-22 18:34:16 +01:00
|
|
|
|
2008-07-04 00:02:09 +02:00
|
|
|
netflood_send(&c->rreqconn, c->rreq_id);
|
2007-05-15 10:09:21 +02:00
|
|
|
c->rreq_id++;
|
2007-03-22 18:34:16 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
send_rrep(struct route_discovery_conn *c, const linkaddr_t *dest)
|
2007-03-22 18:34:16 +01:00
|
|
|
{
|
|
|
|
struct rrep_hdr *rrepmsg;
|
|
|
|
struct route_entry *rt;
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t saved_dest;
|
2007-03-22 18:34:16 +01:00
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&saved_dest, dest);
|
2009-04-06 15:16:39 +02:00
|
|
|
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_clear();
|
2009-04-06 15:16:39 +02:00
|
|
|
dest = &saved_dest;
|
2009-03-12 22:58:20 +01:00
|
|
|
rrepmsg = packetbuf_dataptr();
|
|
|
|
packetbuf_set_datalen(sizeof(struct rrep_hdr));
|
2007-03-22 18:34:16 +01:00
|
|
|
rrepmsg->hops = 0;
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&rrepmsg->dest, dest);
|
|
|
|
linkaddr_copy(&rrepmsg->originator, &linkaddr_node_addr);
|
2007-03-22 18:34:16 +01:00
|
|
|
rt = route_lookup(dest);
|
|
|
|
if(rt != NULL) {
|
|
|
|
PRINTF("%d.%d: send_rrep to %d.%d via %d.%d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2007-03-22 18:34:16 +01:00
|
|
|
dest->u8[0],dest->u8[1],
|
|
|
|
rt->nexthop.u8[0],rt->nexthop.u8[1]);
|
2008-06-26 13:19:22 +02:00
|
|
|
unicast_send(&c->rrepconn, &rt->nexthop);
|
2009-04-06 15:16:39 +02:00
|
|
|
} else {
|
|
|
|
PRINTF("%d.%d: no route for rrep to %d.%d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2009-04-06 15:16:39 +02:00
|
|
|
dest->u8[0],dest->u8[1]);
|
2007-03-22 18:34:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
insert_route(const linkaddr_t *originator, const linkaddr_t *last_hop,
|
2009-11-08 20:40:16 +01:00
|
|
|
uint8_t hops)
|
2007-03-22 18:34:16 +01:00
|
|
|
{
|
2009-05-10 23:10:23 +02:00
|
|
|
PRINTF("%d.%d: Inserting %d.%d into routing table, next hop %d.%d, hop count %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2009-05-10 23:10:23 +02:00
|
|
|
originator->u8[0], originator->u8[1],
|
|
|
|
last_hop->u8[0], last_hop->u8[1],
|
|
|
|
hops);
|
|
|
|
|
|
|
|
route_add(originator, last_hop, hops, 0);
|
|
|
|
/*
|
|
|
|
struct route_entry *rt;
|
2007-03-22 18:34:16 +01:00
|
|
|
|
|
|
|
rt = route_lookup(originator);
|
|
|
|
if(rt == NULL || hops < rt->hop_count) {
|
|
|
|
PRINTF("%d.%d: Inserting %d.%d into routing table, next hop %d.%d, hop count %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2007-03-22 18:34:16 +01:00
|
|
|
originator->u8[0], originator->u8[1],
|
|
|
|
last_hop->u8[0], last_hop->u8[1],
|
|
|
|
hops);
|
|
|
|
route_add(originator, last_hop, hops, 0);
|
2008-11-17 23:52:10 +01:00
|
|
|
#if CONTIKI_TARGET_NETSIM
|
2008-02-03 21:52:41 +01:00
|
|
|
ether_set_line(last_hop->u8[0], last_hop->u8[1]);
|
|
|
|
#endif
|
|
|
|
|
2009-05-10 23:10:23 +02:00
|
|
|
}*/
|
2007-03-22 18:34:16 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2013-12-12 23:58:52 +01:00
|
|
|
rrep_packet_received(struct unicast_conn *uc, const linkaddr_t *from)
|
2007-03-22 18:34:16 +01:00
|
|
|
{
|
2009-03-12 22:58:20 +01:00
|
|
|
struct rrep_hdr *msg = packetbuf_dataptr();
|
2007-03-22 18:34:16 +01:00
|
|
|
struct route_entry *rt;
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t dest;
|
2007-03-22 18:34:16 +01:00
|
|
|
struct route_discovery_conn *c = (struct route_discovery_conn *)
|
|
|
|
((char *)uc - offsetof(struct route_discovery_conn, rrepconn));
|
|
|
|
|
|
|
|
PRINTF("%d.%d: rrep_packet_received from %d.%d towards %d.%d len %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2007-03-22 18:34:16 +01:00
|
|
|
from->u8[0],from->u8[1],
|
|
|
|
msg->dest.u8[0],msg->dest.u8[1],
|
2009-03-12 22:58:20 +01:00
|
|
|
packetbuf_datalen());
|
2007-03-22 18:34:16 +01:00
|
|
|
|
2009-05-10 23:10:23 +02:00
|
|
|
PRINTF("from %d.%d hops %d rssi %d lqi %d\n",
|
|
|
|
from->u8[0], from->u8[1],
|
|
|
|
msg->hops,
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_RSSI),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
|
|
|
|
|
2007-03-22 18:34:16 +01:00
|
|
|
insert_route(&msg->originator, from, msg->hops);
|
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
if(linkaddr_cmp(&msg->dest, &linkaddr_node_addr)) {
|
2007-03-22 18:34:16 +01:00
|
|
|
PRINTF("rrep for us!\n");
|
2009-10-08 18:30:26 +02:00
|
|
|
rrep_pending = 0;
|
2009-01-21 15:29:24 +01:00
|
|
|
ctimer_stop(&c->t);
|
2007-03-22 18:34:16 +01:00
|
|
|
if(c->cb->new_route) {
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_t originator;
|
2010-01-27 09:12:56 +01:00
|
|
|
|
|
|
|
/* If the callback modifies the packet, the originator address
|
|
|
|
will be lost. Therefore, we need to copy it into a local
|
|
|
|
variable before calling the callback. */
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&originator, &msg->originator);
|
2010-01-27 09:12:56 +01:00
|
|
|
c->cb->new_route(c, &originator);
|
2007-03-22 18:34:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&dest, &msg->dest);
|
2007-03-22 18:34:16 +01:00
|
|
|
|
|
|
|
rt = route_lookup(&msg->dest);
|
|
|
|
if(rt != NULL) {
|
2009-01-21 15:29:24 +01:00
|
|
|
PRINTF("forwarding to %d.%d\n", rt->nexthop.u8[0], rt->nexthop.u8[1]);
|
2007-03-22 18:34:16 +01:00
|
|
|
msg->hops++;
|
2008-06-26 13:19:22 +02:00
|
|
|
unicast_send(&c->rrepconn, &rt->nexthop);
|
2007-03-22 18:34:16 +01:00
|
|
|
} else {
|
2013-12-12 23:58:52 +01:00
|
|
|
PRINTF("%d.%d: no route to %d.%d\n", linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1], msg->dest.u8[0], msg->dest.u8[1]);
|
2007-03-22 18:34:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2013-12-12 23:58:52 +01:00
|
|
|
rreq_packet_received(struct netflood_conn *nf, const linkaddr_t *from,
|
|
|
|
const linkaddr_t *originator, uint8_t seqno, uint8_t hops)
|
2007-03-22 18:34:16 +01:00
|
|
|
{
|
2009-03-12 22:58:20 +01:00
|
|
|
struct route_msg *msg = packetbuf_dataptr();
|
2007-03-22 18:34:16 +01:00
|
|
|
struct route_discovery_conn *c = (struct route_discovery_conn *)
|
|
|
|
((char *)nf - offsetof(struct route_discovery_conn, rreqconn));
|
|
|
|
|
2009-01-21 15:29:24 +01:00
|
|
|
PRINTF("%d.%d: rreq_packet_received from %d.%d hops %d rreq_id %d last %d.%d/%d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2007-03-22 18:34:16 +01:00
|
|
|
from->u8[0], from->u8[1],
|
|
|
|
hops, msg->rreq_id,
|
2009-01-21 15:29:24 +01:00
|
|
|
c->last_rreq_originator.u8[0],
|
|
|
|
c->last_rreq_originator.u8[1],
|
2007-03-22 18:34:16 +01:00
|
|
|
c->last_rreq_id);
|
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
if(!(linkaddr_cmp(&c->last_rreq_originator, originator) &&
|
2007-03-22 18:34:16 +01:00
|
|
|
c->last_rreq_id == msg->rreq_id)) {
|
|
|
|
|
|
|
|
PRINTF("%d.%d: rreq_packet_received: request for %d.%d originator %d.%d / %d\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1],
|
2007-03-22 18:34:16 +01:00
|
|
|
msg->dest.u8[0], msg->dest.u8[1],
|
|
|
|
originator->u8[0], originator->u8[1],
|
|
|
|
msg->rreq_id);
|
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_copy(&c->last_rreq_originator, originator);
|
2007-03-22 18:34:16 +01:00
|
|
|
c->last_rreq_id = msg->rreq_id;
|
|
|
|
|
2013-12-12 23:58:52 +01:00
|
|
|
if(linkaddr_cmp(&msg->dest, &linkaddr_node_addr)) {
|
2007-03-22 18:34:16 +01:00
|
|
|
PRINTF("%d.%d: route_packet_received: route request for our address\n",
|
2013-12-12 23:58:52 +01:00
|
|
|
linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1]);
|
2009-05-10 23:10:23 +02:00
|
|
|
PRINTF("from %d.%d hops %d rssi %d lqi %d\n",
|
|
|
|
from->u8[0], from->u8[1],
|
|
|
|
hops,
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_RSSI),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
|
|
|
|
|
2007-03-22 18:34:16 +01:00
|
|
|
insert_route(originator, from, hops);
|
|
|
|
|
|
|
|
/* Send route reply back to source. */
|
|
|
|
send_rrep(c, originator);
|
|
|
|
return 0; /* Don't continue to flood the rreq packet. */
|
|
|
|
} else {
|
|
|
|
/* PRINTF("route request for %d\n", msg->dest_id);*/
|
2009-05-10 23:10:23 +02:00
|
|
|
PRINTF("from %d.%d hops %d rssi %d lqi %d\n",
|
|
|
|
from->u8[0], from->u8[1],
|
|
|
|
hops,
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_RSSI),
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY));
|
2007-03-22 18:34:16 +01:00
|
|
|
insert_route(originator, from, hops);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0; /* Don't forward packet. */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-06-26 13:19:22 +02:00
|
|
|
static const struct unicast_callbacks rrep_callbacks = {rrep_packet_received};
|
2008-07-04 00:02:09 +02:00
|
|
|
static const struct netflood_callbacks rreq_callbacks = {rreq_packet_received, NULL, NULL};
|
2007-03-22 18:34:16 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2007-03-25 14:06:28 +02:00
|
|
|
route_discovery_open(struct route_discovery_conn *c,
|
2007-03-25 14:13:04 +02:00
|
|
|
clock_time_t time,
|
2008-02-24 23:05:27 +01:00
|
|
|
uint16_t channels,
|
2007-03-22 18:34:16 +01:00
|
|
|
const struct route_discovery_callbacks *callbacks)
|
|
|
|
{
|
2008-07-04 00:02:09 +02:00
|
|
|
netflood_open(&c->rreqconn, time, channels + 0, &rreq_callbacks);
|
2008-06-26 13:19:22 +02:00
|
|
|
unicast_open(&c->rrepconn, channels + 1, &rrep_callbacks);
|
2007-03-22 18:34:16 +01:00
|
|
|
c->cb = callbacks;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
route_discovery_close(struct route_discovery_conn *c)
|
|
|
|
{
|
2008-06-26 13:19:22 +02:00
|
|
|
unicast_close(&c->rrepconn);
|
2008-07-04 00:02:09 +02:00
|
|
|
netflood_close(&c->rreqconn);
|
2007-03-22 18:34:16 +01:00
|
|
|
ctimer_stop(&c->t);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
timeout_handler(void *ptr)
|
|
|
|
{
|
|
|
|
struct route_discovery_conn *c = ptr;
|
|
|
|
PRINTF("route_discovery: timeout, timed out\n");
|
2009-10-08 18:30:26 +02:00
|
|
|
rrep_pending = 0;
|
2007-03-22 18:34:16 +01:00
|
|
|
if(c->cb->timedout) {
|
2007-03-30 00:22:44 +02:00
|
|
|
c->cb->timedout(c);
|
2007-03-22 18:34:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-10-08 18:30:26 +02:00
|
|
|
int
|
2013-12-12 23:58:52 +01:00
|
|
|
route_discovery_discover(struct route_discovery_conn *c, const linkaddr_t *addr,
|
2007-03-22 18:34:16 +01:00
|
|
|
clock_time_t timeout)
|
|
|
|
{
|
2009-10-08 18:30:26 +02:00
|
|
|
if(rrep_pending) {
|
|
|
|
PRINTF("route_discovery_send: ignoring request because of pending response\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-22 18:34:16 +01:00
|
|
|
PRINTF("route_discovery_send: sending route request\n");
|
|
|
|
ctimer_set(&c->t, timeout, timeout_handler, c);
|
2009-10-08 18:30:26 +02:00
|
|
|
rrep_pending = 1;
|
2007-03-22 18:34:16 +01:00
|
|
|
send_rreq(c, addr);
|
2009-10-08 18:30:26 +02:00
|
|
|
return 1;
|
2007-03-22 18:34:16 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2007-03-31 20:31:27 +02:00
|
|
|
/** @} */
|