Contiki-NG
Loading...
Searching...
No Matches
border-router-native.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011, 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 * \file
34 * border-router
35 * \author
36 * Niclas Finne <nfi@sics.se>
37 * Joakim Eriksson <joakime@sics.se>
38 * Nicolas Tsiftes <nvt@sics.se>
39 */
40
41#include "contiki.h"
42#include "contiki-net.h"
43
44#include "net/routing/routing.h"
45#include "rpl-border-router.h"
46#include "cmd.h"
47#include "border-router.h"
48#include "border-router-cmds.h"
49#include "tun6-net.h"
50
51#if BUILD_WITH_NAT64
52#include "nat64-platform.h"
53#endif /* BUILD_WITH_NAT64 */
54
55/*---------------------------------------------------------------------------*/
56/* Log configuration */
57#include "sys/log.h"
58#define LOG_MODULE "BR"
59#define LOG_LEVEL LOG_LEVEL_INFO
60
61#include <stdlib.h>
62#include <stdbool.h>
63
64extern long slip_sent;
65extern long slip_received;
66
67static bool is_mac_set;
68
69extern int contiki_argc;
70extern char **contiki_argv;
71
72CMD_HANDLERS(border_router_cmd_handler);
73
74PROCESS(border_router_process, "Border router process");
75
76/*---------------------------------------------------------------------------*/
77static void
78request_mac(void)
79{
80 write_to_slip((uint8_t *)"?M", 2);
81}
82/*---------------------------------------------------------------------------*/
83void
84border_router_set_mac(const uint8_t *data)
85{
86 if(is_mac_set) {
87 /* only set MAC address once */
88 return;
89 }
90
91 memcpy(uip_lladdr.addr, data, sizeof(uip_lladdr.addr));
92 linkaddr_set_node_addr((linkaddr_t *)uip_lladdr.addr);
93
94 /* is this ok - should instead remove all addresses and
95 add them back again - a bit messy... ?*/
96 PROCESS_CONTEXT_BEGIN(&tcpip_process);
98 NETSTACK_ROUTING.init();
99 PROCESS_CONTEXT_END(&tcpip_process);
100
101 is_mac_set = true;
102}
103/*---------------------------------------------------------------------------*/
104void
105border_router_print_stat()
106{
107 printf("bytes received over SLIP: %ld\n", slip_received);
108 printf("bytes sent over SLIP: %ld\n", slip_sent);
109}
110/*---------------------------------------------------------------------------*/
111PROCESS_THREAD(border_router_process, ev, data)
112{
113 static struct etimer et;
114
116
117 is_mac_set = false;
118 prefix_set = 0;
119
121
122 process_start(&border_router_cmd_process, NULL);
123
124 LOG_INFO("RPL-Border router started\n");
125
126 slip_config_handle_arguments(contiki_argc, contiki_argv);
127
128 /* tun init is also responsible for setting up the SLIP connection */
129 tun_init();
130
131#if BUILD_WITH_NAT64
132 if(nat64_is_enabled()) {
133 if(!nat64_platform_init()) {
134 LOG_ERR("Failed to initialize NAT64\n");
135 }
136 }
137#endif /* BUILD_WITH_NAT64 */
138
139 while(!is_mac_set) {
141 request_mac();
143 }
144
145 const char *config_ipaddr = tun6_net_get_prefix();
146 if(config_ipaddr != NULL) {
147 uip_ipaddr_t prefix;
148
149 if(uiplib_ipaddrconv(config_ipaddr, &prefix)) {
150 LOG_INFO("Setting prefix ");
151 LOG_INFO_6ADDR(&prefix);
152 LOG_INFO_("\n");
153 set_prefix_64(&prefix);
154 } else {
155 LOG_ERR("Parse error: %s\n", config_ipaddr);
156 exit(EXIT_FAILURE);
157 }
158 }
159
160 print_local_addresses();
161
162 while(1) {
163 etimer_set(&et, CLOCK_SECOND * 2);
165 /* do anything here??? */
166 }
167
168 PROCESS_END();
169}
170/*---------------------------------------------------------------------------*/
Sets up some commands for the border router.
Border router header file.
Simple command handler.
#define CLOCK_SECOND
A second, measured in system clock time.
Definition clock.h:105
static bool etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition etimer.h:201
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition etimer.c:177
static void linkaddr_set_node_addr(linkaddr_t *addr)
Set the address of the current node.
Definition linkaddr.h:151
bool nat64_platform_init(void)
Initialize the platform layer.
Definition nat64-sock.c:683
bool nat64_is_enabled(void)
Check whether the NAT64 gateway has been enabled at runtime.
Definition nat64-sock.c:717
#define PROCESS(name, strname)
Declare a process.
Definition process.h:309
#define PROCESS_PAUSE()
Yield the process for a short while.
Definition process.h:223
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition process.h:122
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition process.h:159
#define PROCESS_END()
Define the end of a process.
Definition process.h:133
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition process.h:429
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:275
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition process.h:443
#define uiplib_ipaddrconv
Convert a textual representation of an IP address to a numerical representation.
Definition uiplib.h:71
uip_lladdr_t uip_lladdr
Host L2 address.
Definition uip6.c:107
void uip_ds6_init(void)
Initialize data structures.
Definition uip-ds6.c:115
Header file for the logging system.
NAT64 platform interface — socket-based.
Routing driver header file.
A timer.
Definition etimer.h:79
void(* init)(void)
Initialize the routing protocol.
Definition routing.h:63