Contiki-NG
ip64-addr.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 "ipv6/ip64-addr.h"
33 
34 #include <stdio.h>
35 #include <string.h>
36 
37 #define printf(...)
38 
39 static uip_ip6addr_t ip64_prefix = {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0}};
40 static uint8_t ip64_prefix_len = 96;
41 
42 /*---------------------------------------------------------------------------*/
43 void
44 ip64_addr_copy4(uip_ip4addr_t *dest, const uip_ip4addr_t *src)
45 {
46  memcpy(dest, src, sizeof(uip_ip4addr_t));
47 }
48 /*---------------------------------------------------------------------------*/
49 void
50 ip64_addr_copy6(uip_ip6addr_t *dest, const uip_ip6addr_t *src)
51 {
52  memcpy(dest, src, sizeof(uip_ip6addr_t));
53 }
54 /*---------------------------------------------------------------------------*/
55 int
56 ip64_addr_4to6(const uip_ip4addr_t *ipv4addr,
57  uip_ip6addr_t *ipv6addr)
58 {
59  /* This function converts an IPv4 addresses into an IPv6
60  addresses. It returns 0 if it failed to convert the address and
61  non-zero if it could successfully convert the address. */
62 
63  uip_ipaddr_copy(ipv6addr, &ip64_prefix);
64  ipv6addr->u8[12] = ipv4addr->u8[0];
65  ipv6addr->u8[13] = ipv4addr->u8[1];
66  ipv6addr->u8[14] = ipv4addr->u8[2];
67  ipv6addr->u8[15] = ipv4addr->u8[3];
68  printf("ip64_addr_4to6: IPv6-encoded IPv4 address %d.%d.%d.%d\n",
69  ipv4addr->u8[0], ipv4addr->u8[1],
70  ipv4addr->u8[2], ipv4addr->u8[3]);
71 
72  /* Conversion succeeded, we return non-zero. */
73  return 1;
74 }
75 /*---------------------------------------------------------------------------*/
76 int
77 ip64_addr_6to4(const uip_ip6addr_t *ipv6addr,
78  uip_ip4addr_t *ipv4addr)
79 {
80  /* This function converts IPv6 addresses to IPv4 addresses. It
81  returns 0 if it failed to convert the address and non-zero if it
82  could successfully convert the address. */
83 
84  if(ip64_addr_is_ip64(ipv6addr)) {
85  ipv4addr->u8[0] = ipv6addr->u8[12];
86  ipv4addr->u8[1] = ipv6addr->u8[13];
87  ipv4addr->u8[2] = ipv6addr->u8[14];
88  ipv4addr->u8[3] = ipv6addr->u8[15];
89 
90  printf("ip64_addr_6to4: IPv6-encoded IPv4 address %d.%d.%d.%d\n",
91  ipv4addr->u8[0], ipv4addr->u8[1],
92  ipv4addr->u8[2], ipv4addr->u8[3]);
93 
94  /* Conversion succeeded, we return non-zero. */
95  return 1;
96  }
97  /* We could not convert the IPv6 address, so we return 0. */
98  return 0;
99 }
100 /*---------------------------------------------------------------------------*/
101 int
102 ip64_addr_is_ip64(const uip_ip6addr_t *ipv6addr)
103 {
104  return uip_ipaddr_prefixcmp(ipv6addr, &ip64_prefix, ip64_prefix_len);
105 }
106 /*---------------------------------------------------------------------------*/
107 void
108 ip64_addr_set_prefix(const uip_ip6addr_t *prefix, uint8_t prefix_len)
109 {
110  uip_ipaddr_copy(&ip64_prefix, prefix);
111  ip64_prefix_len = prefix_len;
112 }
113 /*---------------------------------------------------------------------------*/
Representation of an IP address.
Definition: uip.h:93
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1018