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  *
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/ip/uip.h"
33 #include "net/ipv6/uip-ds6.h"
34 #include "dev/slip.h"
35 
36 #include "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/ip/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[0] == '!') {
61  PRINTF("Got configuration message of type %c\n", uip_buf[1]);
62  uip_clear_buf();
63 #if 0
64  if(uip_buf[1] == 'P') {
65  uip_ipaddr_t prefix;
66  /* Here we set a prefix !!! */
67  memset(&prefix, 0, 16);
68  memcpy(&prefix, &uip_buf[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[0] == '?') {
76  PRINTF("Got request message of type %c\n", uip_buf[1]);
77  if(uip_buf[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;
87  slip_send();
88 
89  }
90  uip_clear_buf();
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_LLH_LEN], uip_len,
98  ip64_packet_buffer);
99  if(len > 0) {
100  memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len);
101  uip_len = len;
102  /* PRINTF("send len %d\n", len); */
103  } else {
104  uip_clear_buf();
105  }
106  }
107 }
108 /*---------------------------------------------------------------------------*/
109 static void
110 init(void)
111 {
112  PRINTF("ip64-slip-interface: init\n");
113  // slip_arch_init(BAUD2UBR(115200));
114  process_start(&slip_process, NULL);
115  slip_set_input_callback(input_callback);
116 }
117 /*---------------------------------------------------------------------------*/
118 static int
119 output(void)
120 {
121  int len;
122 
123  PRINTF("ip64-slip-interface: output source ");
124 
125  /*
126  PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
127  PRINTF(" destination ");
128  PRINT6ADDR(&UIP_IP_BUF->destipaddr);
129  PRINTF("\n");
130  */
131  if(uip_ipaddr_cmp(&last_sender, &UIP_IP_BUF->srcipaddr)) {
132  PRINTF("ip64-interface: output, not sending bounced message\n");
133  } else {
134  len = ip64_6to4(&uip_buf[UIP_LLH_LEN], uip_len,
135  ip64_packet_buffer);
136  PRINTF("ip64-interface: output len %d\n", len);
137  if(len > 0) {
138  memcpy(&uip_buf[UIP_LLH_LEN], ip64_packet_buffer, len);
139  uip_len = len;
140  slip_send();
141  return len;
142  }
143  }
144  return 0;
145 }
146 /*---------------------------------------------------------------------------*/
147 const struct uip_fallback_interface ip64_slip_interface = {
148  init, output
149 };
150 /*---------------------------------------------------------------------------*/
151 
152 
153 
uip_len
The length of the packet in the uip_buf buffer.
Definition: tcp_loader.c:75
uint8_t slip_send(void)
Send an IP packet from the uIP buffer with SLIP.
Definition: slip.c:193
Header file for IPv6-related data structures.
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:160
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:523
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
#define UIP_IP_BUF
Pointer to IP header.
Definition: uip-nd6.c:104
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:143
#define NULL
The null pointer.
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
A set of debugging macros for the IP stack
static uint8_t output(const uip_lladdr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1275
CCIF uip_lladdr_t uip_lladdr
Host L2 address.
Definition: uip.c:118