Contiki 3.x
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  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 #include "net/ip/uip.h"
32 #include "net/ipv6/uip-ds6.h"
33 #include "dev/slip.h"
34 
35 #include "ip64.h"
36 
37 #include <string.h>
38 #include <stdio.h>
39 
40 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
41 
42 #define DEBUG DEBUG_NONE
43 #include "net/ip/uip-debug.h"
44 
45 static uip_ipaddr_t last_sender;
46 
47 /*---------------------------------------------------------------------------*/
48 void
49 ip64_slip_interface_input(uint8_t *packet, uint16_t len)
50 {
51  /* Dummy definition: this function is not actually called, but must
52  be here to conform to the ip65-interface.h structure. */
53 }
54 /*---------------------------------------------------------------------------*/
55 static void
56 input_callback(void)
57 {
58  /*PRINTF("SIN: %u\n", uip_len);*/
59  if(uip_buf[0] == '!') {
60  PRINTF("Got configuration message of type %c\n", uip_buf[1]);
61  uip_len = 0;
62 #if 0
63  if(uip_buf[1] == 'P') {
64  uip_ipaddr_t prefix;
65  /* Here we set a prefix !!! */
66  memset(&prefix, 0, 16);
67  memcpy(&prefix, &uip_buf[2], 8);
68  PRINTF("Setting prefix ");
69  PRINT6ADDR(&prefix);
70  PRINTF("\n");
71  set_prefix_64(&prefix);
72  }
73 #endif
74  } else if(uip_buf[0] == '?') {
75  PRINTF("Got request message of type %c\n", uip_buf[1]);
76  if(uip_buf[1] == 'M') {
77  const char *hexchar = "0123456789abcdef";
78  int j;
79  /* this is just a test so far... just to see if it works */
80  uip_buf[0] = '!';
81  for(j = 0; j < 8; j++) {
82  uip_buf[2 + j * 2] = hexchar[uip_lladdr.addr[j] >> 4];
83  uip_buf[3 + j * 2] = hexchar[uip_lladdr.addr[j] & 15];
84  }
85  uip_len = 18;
86  slip_send();
87 
88  }
89  uip_len = 0;
90  } else {
91 
92  /* Save the last sender received over SLIP to avoid bouncing the
93  packet back if no route is found */
94  uip_ipaddr_copy(&last_sender, &UIP_IP_BUF->srcipaddr);
95 
96  uint16_t len = ip64_4to6(&uip_buf[UIP_LLH_LEN], uip_len,
97  ip64_packet_buffer);
98  if(len > 0) {
99  memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len);
100  uip_len = len;
101  /* PRINTF("send len %d\n", len); */
102  } else {
103  uip_len = 0;
104  }
105  }
106 }
107 /*---------------------------------------------------------------------------*/
108 static void
109 init(void)
110 {
111  PRINTF("ip64-slip-interface: init\n");
112  // slip_arch_init(BAUD2UBR(115200));
113  process_start(&slip_process, NULL);
114  slip_set_input_callback(input_callback);
115 }
116 /*---------------------------------------------------------------------------*/
117 static void
118 output(void)
119 {
120  int len;
121 
122  PRINTF("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  PRINTF("ip64-interface: output, not sending bounced message\n");
132  } else {
133  len = ip64_6to4(&uip_buf[UIP_LLH_LEN], uip_len,
134  ip64_packet_buffer);
135  PRINTF("ip64-interface: output len %d\n", len);
136  if(len > 0) {
137  memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len);
138  uip_len = len;
139  slip_send();
140  }
141  }
142 }
143 /*---------------------------------------------------------------------------*/
144 const struct uip_fallback_interface ip64_slip_interface = {
145  init, output
146 };
147 /*---------------------------------------------------------------------------*/
148 
149 
150 
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:115
Header file for the uIP TCP/IP stack.
Network interface and stateless autoconfiguration (RFC 4862)
#define NULL
The null pointer.
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:104
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:160
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
A set of debugging macros.