comparison dcclib/ipv6_conv.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * Copyright (c) 2008 by Rhyolite Software, LLC
4 *
5 * This agreement is not applicable to any entity which sells anti-spam
6 * solutions to others or provides an anti-spam solution as part of a
7 * security solution sold to other entities, or to a private network
8 * which employs the DCC or uses data provided by operation of the DCC
9 * but does not provide corresponding data to other users.
10 *
11 * Permission to use, copy, modify, and distribute this software without
12 * changes for any purpose with or without fee is hereby granted, provided
13 * that the above copyright notice and this permission notice appear in all
14 * copies and any distributed versions or copies are either unchanged
15 * or not called anything similar to "DCC" or "Distributed Checksum
16 * Clearinghouse".
17 *
18 * Parties not eligible to receive a license under this agreement can
19 * obtain a commercial license to use DCC by contacting Rhyolite Software
20 * at sales@rhyolite.com.
21 *
22 * A commercial license would be for Distributed Checksum and Reputation
23 * Clearinghouse software. That software includes additional features. This
24 * free license for Distributed ChecksumClearinghouse Software does not in any
25 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
26 * software
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
29 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
31 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
32 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
33 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
34 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 * SOFTWARE.
36 *
37 * Rhyolite Software DCC 1.3.103-1.21 $Revision$
38 */
39
40 #include "dcc_defs.h"
41
42
43 /* convert IPv4 address to IPv6 */
44 void
45 dcc_ipv4toipv6(struct in6_addr *dst, const struct in_addr src)
46 {
47 /* do it in an order that allows dst==src */
48 dst->s6_addr32[3] = src.s_addr;
49 dst->s6_addr32[0] = 0;
50 dst->s6_addr32[1] = 0;
51 dst->s6_addr32[2] = htonl(0xffff);
52 }
53
54
55
56 /* try to convert IPv6 address to IPv4 address */
57 u_char /* 1=did it */
58 dcc_ipv6toipv4(struct in_addr *dst, const struct in6_addr *src)
59 {
60 if (DCC_IN6_ADDR_V4MAPPED(src)) {
61 dst->s_addr = src->s6_addr32[3];
62 return 1;
63 }
64
65 if (src->s6_addr32[0] != 0
66 || src->s6_addr32[1] != 0
67 || src->s6_addr32[2] != 0)
68 return 0;
69
70 if (src->s6_addr32[3] == ntohl(1))
71 dst->s_addr = ntohl(0x7f000001);
72 else
73 dst->s_addr = src->s6_addr32[3];
74 return 1;
75 }
76
77
78
79 void
80 dcc_su2ip(DCC_IP *ip, const DCC_SOCKU *su)
81 {
82 memset(ip, 0, sizeof(*ip));
83 if ((ip->family = su->sa.sa_family) == AF_UNSPEC)
84 return;
85 ip->port = DCC_SU_PORT(su);
86 if (su->sa.sa_family == AF_INET)
87 ip->u.v4 = su->ipv4.sin_addr;
88 else
89 ip->u.v6 = su->ipv6.sin6_addr;
90 }
91
92
93
94 /* try to convert IPv6 DCC_SOCKU to IPv4 */
95 u_char /* 1=did it */
96 dcc_ipv6sutoipv4(DCC_SOCKU *dst, const DCC_SOCKU *src)
97 {
98 struct in_addr addr4;
99
100 if (src->sa.sa_family != AF_INET6) {
101 if (src != dst)
102 *dst = *src;
103 return (src->sa.sa_family == AF_INET);
104 }
105
106 if (!dcc_ipv6toipv4(&addr4, &src->ipv6.sin6_addr)) {
107 if (src != dst)
108 *dst = *src;
109 return 0;
110 }
111
112 dcc_mk_su(dst, AF_INET, &addr4, *DCC_SU_PORTP(src));
113 return 1;
114 }
115
116
117
118 /* try to convert IPv4 DCC_SOCKU to IPv6
119 * This function is mentioned in dccifd/dccif-test/dccif-test.c
120 * and so cannot change lightly. */
121 u_char /* 1=did it */
122 dcc_ipv4sutoipv6(DCC_SOCKU *dst, const DCC_SOCKU *src)
123 {
124 struct in6_addr addr6;
125
126 if (src->sa.sa_family != AF_INET) {
127 if (src != dst)
128 *dst = *src;
129 return (src->sa.sa_family == AF_INET6);
130 }
131
132 dcc_ipv4toipv6(&addr6, src->ipv4.sin_addr);
133 dcc_mk_su(dst, AF_INET6, &addr6, *DCC_SU_PORTP(src));
134 return 1;
135 }