Contiki-NG
Loading...
Searching...
No Matches
ip64-slip-interface.c
1/*
2 * Copyright (c) 2012, Thingsquare, http://www.thingsquare.com/.
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#include "net/ipv6/uip.h"
33#include "net/ipv6/uip-ds6.h"
34#include "dev/slip.h"
35#include "ip64/ip64.h"
36
37#include <string.h>
38
39/*---------------------------------------------------------------------------*/
40
41#include "sys/log.h"
42#define LOG_MODULE "IP64"
43#define LOG_LEVEL LOG_LEVEL_IP64
44
45/*---------------------------------------------------------------------------*/
46
47static uip_ipaddr_t last_sender;
48
49/*---------------------------------------------------------------------------*/
50void
51ip64_slip_interface_input(uint8_t *packet, uint16_t len)
52{
53 /* Dummy definition: this function is not actually called, but must
54 be here to conform to the ip65-interface.h structure. */
55}
56/*---------------------------------------------------------------------------*/
57static void
58input_callback(void)
59{
60 /*PRINTF("SIN: %u\n", uip_len);*/
61 if(uip_buf[0] == '!') {
62 LOG_DBG("Got configuration message of type %c\n", uip_buf[1]);
63 uipbuf_clear();
64#if 0
65 if(uip_buf[1] == 'P') {
66 uip_ipaddr_t prefix;
67 /* Here we set a prefix !!! */
68 memset(&prefix, 0, 16);
69 memcpy(&prefix, &uip_buf[2], 8);
70 LOG_DBG("Setting prefix ");
71 LOG_DBG_6ADDR(&prefix);
72 LOG_DBG_("\n");
73 set_prefix_64(&prefix);
74 }
75#endif
76 } else if(uip_buf[0] == '?') {
77 LOG_DBG("Got request message of type %c\n", uip_buf[1]);
78 if(uip_buf[1] == 'M') {
79 const char *hexchar = "0123456789abcdef";
80 int j;
81 /* this is just a test so far... just to see if it works */
82 uip_buf[0] = '!';
83 for(j = 0; j < 8; j++) {
84 uip_buf[2 + j * 2] = hexchar[uip_lladdr.addr[j] >> 4];
85 uip_buf[3 + j * 2] = hexchar[uip_lladdr.addr[j] & 15];
86 }
87 uip_len = 18;
88 slip_write(uip_buf, uip_len);
89 }
90 uipbuf_clear();
91 } else {
92
93 /* Save the last sender received over SLIP to avoid bouncing the
94 packet back if no route is found */
95 uip_ipaddr_copy(&last_sender, &UIP_IP_BUF->srcipaddr);
96
97 uint16_t len = ip64_4to6(uip_buf, uip_len,
98 ip64_packet_buffer);
99 if(len > 0) {
100 memcpy(uip_buf, ip64_packet_buffer, len);
101 uip_len = len;
102 /* LOG_DBG("send len %d\n", len); */
103 } else {
104 uipbuf_clear();
105 }
106 }
107}
108/*---------------------------------------------------------------------------*/
109static void
110init(void)
111{
112 LOG_INFO("ip64-slip-interface: init\n");
113 process_start(&slip_process, NULL);
114 slip_set_input_callback(input_callback);
115}
116/*---------------------------------------------------------------------------*/
117static int
118output(void)
119{
120 int len;
121
122 LOG_DBG("ip64-slip-interface: output source ");
123
124 /*
125 PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
126 PRINTF(" destination ");
127 PRINT6ADDR(&UIP_IP_BUF->destipaddr);
128 PRINTF("\n");
129 */
130 if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) {
131 LOG_WARN("ip64-interface: output, not sending bounced message\n");
132 } else {
133 len = ip64_6to4(uip_buf, uip_len,
134 ip64_packet_buffer);
135 LOG_DBG("ip64-interface: output len %d\n", len);
136 if(len > 0) {
137 memcpy(uip_buf, ip64_packet_buffer, len);
138 uip_len = len;
139 slip_send();
140 return len;
141 }
142 }
143 return 0;
144}
145/*---------------------------------------------------------------------------*/
146const struct uip_fallback_interface ip64_slip_interface = {
147 init, output
148};
149/*---------------------------------------------------------------------------*/
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:107
uip_lladdr_t uip_lladdr
Host L2 address.
Definition uip6.c:107
#define UIP_IP_BUF
Direct access to IPv6 header.
Definition uip.h:71
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition uip.h:969
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition uip.h:465
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition uip6.c:159
Header file for the logging system.
Header file for IPv6-related data structures.
Header file for the uIP TCP/IP stack.