Contiki-NG
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 
36 #include "ip64/ip64.h"
37 
38 #include <string.h>
39 #include <stdio.h>
40 
41 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
42 
43 #define DEBUG DEBUG_NONE
44 #include "net/ipv6/uip-debug.h"
45 
46 static uip_ipaddr_t last_sender;
47 
48 /*---------------------------------------------------------------------------*/
49 void
50 ip64_slip_interface_input(uint8_t *packet, uint16_t len)
51 {
52  /* Dummy definition: this function is not actually called, but must
53  be here to conform to the ip65-interface.h structure. */
54 }
55 /*---------------------------------------------------------------------------*/
56 static void
57 input_callback(void)
58 {
59  /*PRINTF("SIN: %u\n", uip_len);*/
60  if(uip_buf[UIP_LLH_LEN] == '!') {
61  PRINTF("Got configuration message of type %c\n", uip_buf[UIP_LLH_LEN + 1]);
62  uip_clear_buf();
63 #if 0
64  if(uip_buf[UIP_LLH_LEN + 1] == 'P') {
65  uip_ipaddr_t prefix;
66  /* Here we set a prefix !!! */
67  memset(&prefix, 0, 16);
68  memcpy(&prefix, &uip_buf[UIP_LLH_LEN + 2], 8);
69  PRINTF("Setting prefix ");
70  PRINT6ADDR(&prefix);
71  PRINTF("\n");
72  set_prefix_64(&prefix);
73  }
74 #endif
75  } else if(uip_buf[UIP_LLH_LEN] == '?') {
76  PRINTF("Got request message of type %c\n", uip_buf[UIP_LLH_LEN + 1]);
77  if(uip_buf[UIP_LLH_LEN + 1] == 'M') {
78  const char *hexchar = "0123456789abcdef";
79  int j;
80  /* this is just a test so far... just to see if it works */
81  uip_buf[0] = '!';
82  for(j = 0; j < 8; j++) {
83  uip_buf[2 + j * 2] = hexchar[uip_lladdr.addr[j] >> 4];
84  uip_buf[3 + j * 2] = hexchar[uip_lladdr.addr[j] & 15];
85  }
86  uip_len = 18;
88  }
89  uip_clear_buf();
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_clear_buf();
104  }
105  }
106 }
107 /*---------------------------------------------------------------------------*/
108 static void
109 init(void)
110 {
111  PRINTF("ip64-slip-interface: init\n");
112  process_start(&slip_process, NULL);
113  slip_set_input_callback(input_callback);
114 }
115 /*---------------------------------------------------------------------------*/
116 static int
117 output(void)
118 {
119  int len;
120 
121  PRINTF("ip64-slip-interface: output source ");
122 
123  /*
124  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
125  PRINTF(" destination ");
126  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
127  PRINTF("\n");
128  */
129  if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) {
130  PRINTF("ip64-interface: output, not sending bounced message\n");
131  } else {
132  len = ip64_6to4(&uip_buf[UIP_LLH_LEN], uip_len,
133  ip64_packet_buffer);
134  PRINTF("ip64-interface: output len %d\n", len);
135  if(len > 0) {
136  memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len);
137  uip_len = len;
138  slip_send();
139  return len;
140  }
141  }
142  return 0;
143 }
144 /*---------------------------------------------------------------------------*/
145 const struct uip_fallback_interface ip64_slip_interface = {
146  init, output
147 };
148 /*---------------------------------------------------------------------------*/
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:97
void slip_set_input_callback(void(*c)(void))
Set a function to be called when there is activity on the SLIP interface; used for detecting if a nod...
Definition: slip.c:142
uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip6.c:107
void slip_write(const void *_ptr, int len)
Send using SLIP len bytes starting from the location pointed to by ptr.
Definition: slip.c:171
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:179
A set of debugging macros for the IP stack
void slip_send(void)
Send an IP packet from the uIP buffer with SLIP.
Definition: slip.c:190
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1549
Header file for IPv6-related data structures.
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:141
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:513
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1018
Header file for the uIP TCP/IP stack.
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99