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