Contiki 3.x
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 "ip64-addr.h"
33 
34 #include <stdio.h>
35 #include <string.h>
36 
37 #define printf(...)
38 /*---------------------------------------------------------------------------*/
39 void
40 ip64_addr_copy4(uip_ip4addr_t *dest, const uip_ip4addr_t *src)
41 {
42  memcpy(dest, src, sizeof(uip_ip4addr_t));
43 }
44 /*---------------------------------------------------------------------------*/
45 void
46 ip64_addr_copy6(uip_ip6addr_t *dest, const uip_ip6addr_t *src)
47 {
48  memcpy(dest, src, sizeof(uip_ip6addr_t));
49 }
50 /*---------------------------------------------------------------------------*/
51 int
52 ip64_addr_4to6(const uip_ip4addr_t *ipv4addr,
53  uip_ip6addr_t *ipv6addr)
54 {
55  /* This function converts an IPv4 addresses into an IPv6
56  addresses. It returns 0 if it failed to convert the address and
57  non-zero if it could successfully convert the address. */
58 
59  /* The IPv4 address is encoded as an IPv6-encoded IPv4 address in
60  the ::ffff:0000/24 prefix.*/
61  ipv6addr->u8[0] = 0;
62  ipv6addr->u8[1] = 0;
63  ipv6addr->u8[2] = 0;
64  ipv6addr->u8[3] = 0;
65  ipv6addr->u8[4] = 0;
66  ipv6addr->u8[5] = 0;
67  ipv6addr->u8[6] = 0;
68  ipv6addr->u8[7] = 0;
69  ipv6addr->u8[8] = 0;
70  ipv6addr->u8[9] = 0;
71  ipv6addr->u8[10] = 0xff;
72  ipv6addr->u8[11] = 0xff;
73  ipv6addr->u8[12] = ipv4addr->u8[0];
74  ipv6addr->u8[13] = ipv4addr->u8[1];
75  ipv6addr->u8[14] = ipv4addr->u8[2];
76  ipv6addr->u8[15] = ipv4addr->u8[3];
77  printf("ip64_addr_4to6: IPv6-encoded IPv4 address %d.%d.%d.%d\n",
78  ipv4addr->u8[0], ipv4addr->u8[1],
79  ipv4addr->u8[2], ipv4addr->u8[3]);
80 
81  /* Conversion succeeded, we return non-zero. */
82  return 1;
83 }
84 /*---------------------------------------------------------------------------*/
85 int
86 ip64_addr_6to4(const uip_ip6addr_t *ipv6addr,
87  uip_ip4addr_t *ipv4addr)
88 {
89  /* This function converts IPv6 addresses to IPv4 addresses. It
90  returns 0 if it failed to convert the address and non-zero if it
91  could successfully convert the address. */
92 
93  /* If the IPv6 address is an IPv6-encoded
94  IPv4 address (i.e. in the ::ffff:0/8 prefix), we simply use the
95  IPv4 addresses directly. */
96  if(ipv6addr->u8[0] == 0 &&
97  ipv6addr->u8[1] == 0 &&
98  ipv6addr->u8[2] == 0 &&
99  ipv6addr->u8[3] == 0 &&
100  ipv6addr->u8[4] == 0 &&
101  ipv6addr->u8[5] == 0 &&
102  ipv6addr->u8[6] == 0 &&
103  ipv6addr->u8[7] == 0 &&
104  ipv6addr->u8[8] == 0 &&
105  ipv6addr->u8[9] == 0 &&
106  ipv6addr->u8[10] == 0xff &&
107  ipv6addr->u8[11] == 0xff) {
108  ipv4addr->u8[0] = ipv6addr->u8[12];
109  ipv4addr->u8[1] = ipv6addr->u8[13];
110  ipv4addr->u8[2] = ipv6addr->u8[14];
111  ipv4addr->u8[3] = ipv6addr->u8[15];
112 
113  printf("ip64_addr_6to4: IPv6-encoded IPv4 address %d.%d.%d.%d\n",
114  ipv4addr->u8[0], ipv4addr->u8[1],
115  ipv4addr->u8[2], ipv4addr->u8[3]);
116 
117  /* Conversion succeeded, we return non-zero. */
118  return 1;
119  }
120  /* We could not convert the IPv6 address, so we return 0. */
121  return 0;
122 }
123 /*---------------------------------------------------------------------------*/
Representation of an IP address.
Definition: uip.h:101