]> jfr.im git - solanum.git/blame - ircd/ipv4_from_ipv6.c
rename src to ircd, libcore to libircd
[solanum.git] / ircd / ipv4_from_ipv6.c
CommitLineData
524a5b3a
JT
1/*
2 * charybdis: A slightly useful ircd.
3 * ipv4_from_ipv6.c: Finds IPv4 addresses behind IPv6 transition technologies
4 *
5 * Copyright (C) 2012 Jilles Tjoelker
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35#include "stdinc.h"
36#include "ipv4_from_ipv6.h"
37
38#ifdef RB_IPV6
39int
40ipv4_from_ipv6(const struct sockaddr_in6 *restrict ip6,
41 struct sockaddr_in *restrict ip4)
42{
43 int i;
44
45 if (!memcmp(ip6->sin6_addr.s6_addr, "\x20\x02", 2))
46 {
47 /* 6to4 and similar */
48 memcpy(&ip4->sin_addr, ip6->sin6_addr.s6_addr + 2, 4);
49 }
50 else if (!memcmp(ip6->sin6_addr.s6_addr, "\x20\x01\x00\x00", 4))
51 {
52 /* Teredo */
53 for (i = 0; i < 4; i++)
54 ((uint8_t *)&ip4->sin_addr)[i] = 0xFF ^
55 ip6->sin6_addr.s6_addr[12 + i];
56 }
57 else
58 return 0;
59 SET_SS_LEN(ip4, sizeof(struct sockaddr_in));
60 ip4->sin_family = AF_INET;
61 ip4->sin_port = 0;
62 return 1;
63}
64#endif /* RB_IPV6 */