From f1bec7b955849ac8799029c8f3e1a513a2e06102 Mon Sep 17 00:00:00 2001 From: adamdunkels Date: Sun, 3 Feb 2008 20:56:07 +0000 Subject: [PATCH] Added the ability to send packets to an external network --- core/net/uip-over-mesh.c | 59 +++++++++++++++++++++++++++++++++++----- core/net/uip-over-mesh.h | 12 ++++++-- 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/core/net/uip-over-mesh.c b/core/net/uip-over-mesh.c index 02064f60e..bd1497cfa 100644 --- a/core/net/uip-over-mesh.c +++ b/core/net/uip-over-mesh.c @@ -28,12 +28,12 @@ * * This file is part of the Contiki operating system. * - * $Id: uip-over-mesh.c,v 1.5 2007/08/30 14:39:17 matsutsuka Exp $ + * $Id: uip-over-mesh.c,v 1.6 2008/02/03 20:56:07 adamdunkels Exp $ */ /** * \file - * Code for tunnelling uIP packets over the mesh routing module + * Code for tunnelling uIP packets over the Rime mesh routing module * \author * Adam Dunkels */ @@ -52,7 +52,7 @@ static rimeaddr_t queued_receiver; static struct route_discovery_conn route_discovery; static struct uc_conn dataconn; -#define DEBUG 0 +#define DEBUG 1 #if DEBUG #include #define PRINTF(...) printf(__VA_ARGS__) @@ -60,6 +60,12 @@ static struct uc_conn dataconn; #define PRINTF(...) #endif +#define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN]) + +static struct uip_fw_netif *gw_netif; +static rimeaddr_t gateway; +static uip_ipaddr_t netaddr, netmask; + /*---------------------------------------------------------------------------*/ static void recv_data(struct uc_conn *c, rimeaddr_t *from) @@ -119,10 +125,10 @@ void uip_over_mesh_init(u16_t channels) { - /* printf("Our address is %d.%d (%d.%d.%d.%d)\n", + printf("Our address is %d.%d (%d.%d.%d.%d)\n", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], uip_hostaddr.u8[0], uip_hostaddr.u8[1], - uip_hostaddr.u8[2], uip_hostaddr.u8[3]); */ + uip_hostaddr.u8[2], uip_hostaddr.u8[3]); uc_open(&dataconn, channels, &data_callbacks); route_discovery_open(&route_discovery, CLOCK_SECOND / 4, @@ -141,8 +147,28 @@ uip_over_mesh_send(void) packet. We try to send the IP packet to the next hop route, or we queue the packet and send out a route request for the final receiver of the packet. */ - - receiver.u16[0] = ((struct uip_udpip_hdr *)&uip_buf[UIP_LLH_LEN])->destipaddr.u16[1]; + + /* Packets destined to this network is sent using mesh, whereas + packets destined to a network outside this network is sent towards + the gateway node. */ + + if(uip_ipaddr_maskcmp(&BUF->destipaddr, &netaddr, &netmask)) { + receiver.u8[0] = BUF->destipaddr.u8[2]; + receiver.u8[1] = BUF->destipaddr.u8[3]; + } else { + if(rimeaddr_cmp(&gateway, &rimeaddr_node_addr)) { + PRINTF("I am gateway, should forward over a local interface\n"); + if(gw_netif != NULL) { + return gw_netif->output(); + } + return UIP_FW_DROPPED; + } else if(rimeaddr_cmp(&gateway, &rimeaddr_null)) { + PRINTF("No gateway setup, dropping packet\n"); + return UIP_FW_OK; + } else { + rimeaddr_copy(&receiver, &gateway); + } + } PRINTF("uIP over mesh send to %d.%d with len %d\n", receiver.u8[0], receiver.u8[1], @@ -168,3 +194,22 @@ uip_over_mesh_send(void) return UIP_FW_OK; } /*---------------------------------------------------------------------------*/ +void +uip_over_mesh_set_gateway_netif(struct uip_fw_netif *n) +{ + gw_netif = n; +} +/*---------------------------------------------------------------------------*/ +void +uip_over_mesh_set_gateway(rimeaddr_t *gw) +{ + rimeaddr_copy(&gateway, gw); +} +/*---------------------------------------------------------------------------*/ +void +uip_over_mesh_set_net(uip_ipaddr_t *addr, uip_ipaddr_t *mask) +{ + uip_ipaddr_copy(&netaddr, addr); + uip_ipaddr_copy(&netmask, mask); +} +/*---------------------------------------------------------------------------*/ diff --git a/core/net/uip-over-mesh.h b/core/net/uip-over-mesh.h index 6a2584448..436f2adac 100644 --- a/core/net/uip-over-mesh.h +++ b/core/net/uip-over-mesh.h @@ -28,12 +28,12 @@ * * This file is part of the Contiki operating system. * - * $Id: uip-over-mesh.h,v 1.1 2007/03/22 18:58:40 adamdunkels Exp $ + * $Id: uip-over-mesh.h,v 1.2 2008/02/03 20:56:07 adamdunkels Exp $ */ /** * \file - * A brief description of what this file is. + * Header file for tunnelling uIP over Rime mesh * \author * Adam Dunkels */ @@ -41,7 +41,15 @@ #ifndef __UIP_OVER_MESH_H__ #define __UIP_OVER_MESH_H__ +#include "net/uip.h" +#include "net/uip-fw.h" +#include "net/rime.h" + void uip_over_mesh_init(u16_t channels); u8_t uip_over_mesh_send(void); +void uip_over_mesh_set_gateway_netif(struct uip_fw_netif *netif); +void uip_over_mesh_set_gateway(rimeaddr_t *gw); +void uip_over_mesh_set_net(uip_ipaddr_t *addr, uip_ipaddr_t *mask); + #endif /* __UIP-OVER-MESH_H__ */