diff --git a/include/ipv4_from_ipv6.h b/include/ipv4_from_ipv6.h new file mode 100644 index 00000000..f6a881f9 --- /dev/null +++ b/include/ipv4_from_ipv6.h @@ -0,0 +1,44 @@ +/* + * charybdis: An advanced ircd. + * ipv4_from_ipv6.h: Finds IPv4 addresses behind IPv6 transition technologies + * + * Copyright (C) 2012 Jilles Tjoelker + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * $Id$ + */ + +#ifndef _IPV4_FROM_IPV6_H_ +#define _IPV4_FROM_IPV6_H_ + +#ifdef RB_IPV6 +int ipv4_from_ipv6(const struct sockaddr_in6 *restrict ip6, + struct sockaddr_in *restrict ip4); +#endif + +#endif diff --git a/src/Makefile.in b/src/Makefile.in index ab69afbb..f435475b 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -66,6 +66,7 @@ SRCS = \ hash.c \ hook.c \ hostmask.c \ + ipv4_from_ipv6.c \ irc_dictionary.c \ ircd.c \ ircd_signal.c \ diff --git a/src/ipv4_from_ipv6.c b/src/ipv4_from_ipv6.c new file mode 100644 index 00000000..421977f1 --- /dev/null +++ b/src/ipv4_from_ipv6.c @@ -0,0 +1,64 @@ +/* + * charybdis: A slightly useful ircd. + * ipv4_from_ipv6.c: Finds IPv4 addresses behind IPv6 transition technologies + * + * Copyright (C) 2012 Jilles Tjoelker + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "stdinc.h" +#include "ipv4_from_ipv6.h" + +#ifdef RB_IPV6 +int +ipv4_from_ipv6(const struct sockaddr_in6 *restrict ip6, + struct sockaddr_in *restrict ip4) +{ + int i; + + if (!memcmp(ip6->sin6_addr.s6_addr, "\x20\x02", 2)) + { + /* 6to4 and similar */ + memcpy(&ip4->sin_addr, ip6->sin6_addr.s6_addr + 2, 4); + } + else if (!memcmp(ip6->sin6_addr.s6_addr, "\x20\x01\x00\x00", 4)) + { + /* Teredo */ + for (i = 0; i < 4; i++) + ((uint8_t *)&ip4->sin_addr)[i] = 0xFF ^ + ip6->sin6_addr.s6_addr[12 + i]; + } + else + return 0; + SET_SS_LEN(ip4, sizeof(struct sockaddr_in)); + ip4->sin_family = AF_INET; + ip4->sin_port = 0; + return 1; +} +#endif /* RB_IPV6 */