Contiki-NG
Loading...
Searching...
No Matches
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/* Log configuration */
38#include "sys/log.h"
39#define LOG_MODULE "IPv6"
40#define LOG_LEVEL LOG_LEVEL_NONE
41
42/*
43 * By defult we do use the well-known-prefix for the IP64 address prefix, see
44 * RFC6052, https://tools.ietf.org/html/rfc6052#section-2.1 .
45 * To make use of ::ffff:<IPv4> define IP64_NAT64_USE_WELL_KNOWN_PREFIX to zero.
46 */
47#ifndef IP64_NAT64_USE_WELL_KNOWN_PREFIX
48#define IP64_NAT64_USE_WELL_KNOWN_PREFIX 1
49#endif /* IP64_NAT64_USE_WELL_KNOWN_PREFIX */
50
51#if IP64_NAT64_USE_WELL_KNOWN_PREFIX
52static uip_ip6addr_t ip64_prefix = {{ 0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
53#else
54static uip_ip6addr_t ip64_prefix = {{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 0, 0, 0}};
55#endif /* IP64_NAT64_USE_WELL_KNOWN_PREFIX */
56
57static uint8_t ip64_prefix_len = 96;
58
59/*---------------------------------------------------------------------------*/
60void
61ip64_addr_copy4(uip_ip4addr_t *dest, const uip_ip4addr_t *src)
62{
63 memcpy(dest, src, sizeof(uip_ip4addr_t));
64}
65/*---------------------------------------------------------------------------*/
66void
67ip64_addr_copy6(uip_ip6addr_t *dest, const uip_ip6addr_t *src)
68{
69 memcpy(dest, src, sizeof(uip_ip6addr_t));
70}
71/*---------------------------------------------------------------------------*/
72int
73ip64_addr_4to6(const uip_ip4addr_t *ipv4addr,
74 uip_ip6addr_t *ipv6addr)
75{
76 /* This function converts an IPv4 addresses into an IPv6
77 addresses. It returns 0 if it failed to convert the address and
78 non-zero if it could successfully convert the address. */
79
80 uip_ipaddr_copy(ipv6addr, &ip64_prefix);
81 ipv6addr->u8[12] = ipv4addr->u8[0];
82 ipv6addr->u8[13] = ipv4addr->u8[1];
83 ipv6addr->u8[14] = ipv4addr->u8[2];
84 ipv6addr->u8[15] = ipv4addr->u8[3];
85 LOG_DBG("ip64_addr_4to6: IPv6-encoded IPv4 address %d.%d.%d.%d => ",
86 ipv4addr->u8[0], ipv4addr->u8[1],
87 ipv4addr->u8[2], ipv4addr->u8[3]);
88 LOG_DBG_6ADDR(ipv6addr);
89 LOG_DBG("\n");
90
91 /* Conversion succeeded, we return non-zero. */
92 return 1;
93}
94/*---------------------------------------------------------------------------*/
95int
96ip64_addr_6to4(const uip_ip6addr_t *ipv6addr,
97 uip_ip4addr_t *ipv4addr)
98{
99 /* This function converts IPv6 addresses to IPv4 addresses. It
100 returns 0 if it failed to convert the address and non-zero if it
101 could successfully convert the address. */
102
103 if(ip64_addr_is_ip64(ipv6addr)) {
104 ipv4addr->u8[0] = ipv6addr->u8[12];
105 ipv4addr->u8[1] = ipv6addr->u8[13];
106 ipv4addr->u8[2] = ipv6addr->u8[14];
107 ipv4addr->u8[3] = ipv6addr->u8[15];
108
109 LOG_DBG("ip64_addr_6to4: IPv6-encoded IPv4 address %d.%d.%d.%d <=",
110 ipv4addr->u8[0], ipv4addr->u8[1],
111 ipv4addr->u8[2], ipv4addr->u8[3]);
112 LOG_DBG_6ADDR(ipv6addr);
113 LOG_DBG("\n");
114
115 /* Conversion succeeded, we return non-zero. */
116 return 1;
117 }
118 /* We could not convert the IPv6 address, so we return 0. */
119 return 0;
120}
121/*---------------------------------------------------------------------------*/
122int
123ip64_addr_is_ip64(const uip_ip6addr_t *ipv6addr)
124{
125 return uip_ipaddr_prefixcmp(ipv6addr, &ip64_prefix, ip64_prefix_len);
126}
127/*---------------------------------------------------------------------------*/
128void
129ip64_addr_set_prefix(const uip_ip6addr_t *prefix, uint8_t prefix_len)
130{
131 uip_ipaddr_copy(&ip64_prefix, prefix);
132 ip64_prefix_len = prefix_len;
133}
134/*---------------------------------------------------------------------------*/
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition uip.h:969
Header file for the logging system.
Representation of an IP address.
Definition uip.h:95