b2d0e2a6cc689c1541e42c6befe9119c3906244e
[senf.git] / Socket / Protocols / INet / INet6Address.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief INet6Address inline non-template implementation */
25
26 #include "INet6Address.ih"
27
28 // Custom includes
29 #include <algorithm>
30 #include <boost/lambda/lambda.hpp>
31
32 #define prefix_ inline
33 ///////////////////////////////cci.p///////////////////////////////////////
34
35 prefix_ senf::INet6Address::INet6Address(senf::NoInit_t)
36 {}
37
38 prefix_ senf::INet6Address::INet6Address(boost::uint16_t a0, boost::uint16_t a1,
39                                          boost::uint16_t a2, boost::uint16_t a3,
40                                          boost::uint16_t a4, boost::uint16_t a5,
41                                          boost::uint16_t a6, boost::uint16_t a7)
42 {
43     (*this)[ 0] = boost::uint8_t(a0>>8);
44     (*this)[ 1] = boost::uint8_t(a0);
45     (*this)[ 2] = boost::uint8_t(a1>>8);
46     (*this)[ 3] = boost::uint8_t(a1);
47     (*this)[ 4] = boost::uint8_t(a2>>8);
48     (*this)[ 5] = boost::uint8_t(a2);
49     (*this)[ 6] = boost::uint8_t(a3>>8);
50     (*this)[ 7] = boost::uint8_t(a3);
51     (*this)[ 8] = boost::uint8_t(a4>>8);
52     (*this)[ 9] = boost::uint8_t(a4);
53     (*this)[10] = boost::uint8_t(a5>>8);
54     (*this)[11] = boost::uint8_t(a5);
55     (*this)[12] = boost::uint8_t(a6>>8);
56     (*this)[13] = boost::uint8_t(a6);
57     (*this)[14] = boost::uint8_t(a7>>8);
58     (*this)[15] = boost::uint8_t(a7);
59 }
60
61 prefix_ senf::INet6Address senf::INet6Address::from_in6addr(in6_addr const & in6addr)
62 {
63     return senf::INet6Address::from_data(&in6addr.s6_addr[0]);
64 }
65
66 prefix_ senf::INet6Address senf::INet6Address::from_inet4address(INet4Address addr4)
67 {
68     INet6Address addr;
69     addr[10] = 0xffu;
70     addr[11] = 0xffu;
71     std::copy(addr4.begin(), addr4.end(), addr.begin()+12);
72     return addr;
73 }
74
75 prefix_ boost::uint64_t senf::INet6Address::network()
76     const
77 {
78     return 
79         ((boost::uint64_t((*this)[0]) & 0xff) << 56 ) |
80         ((boost::uint64_t((*this)[1]) & 0xff) << 48 ) |
81         ((boost::uint64_t((*this)[2]) & 0xff) << 40 ) |
82         ((boost::uint64_t((*this)[3]) & 0xff) << 32 ) |
83         ((boost::uint64_t((*this)[4]) & 0xff) << 24 ) |
84         ((boost::uint64_t((*this)[5]) & 0xff) << 16 ) |
85         ((boost::uint64_t((*this)[6]) & 0xff) <<  8 ) |
86         ((boost::uint64_t((*this)[7]) & 0xff)       );
87 }
88
89 prefix_ boost::uint64_t senf::INet6Address::id()
90     const
91 {
92     return 
93         ((boost::uint64_t((*this)[ 8]) & 0xff) << 56 ) |
94         ((boost::uint64_t((*this)[ 9]) & 0xff) << 48 ) |
95         ((boost::uint64_t((*this)[10]) & 0xff) << 40 ) |
96         ((boost::uint64_t((*this)[11]) & 0xff) << 32 ) |
97         ((boost::uint64_t((*this)[12]) & 0xff) << 24 ) |
98         ((boost::uint64_t((*this)[13]) & 0xff) << 16 ) |
99         ((boost::uint64_t((*this)[14]) & 0xff) <<  8 ) |
100         ((boost::uint64_t((*this)[15]) & 0xff)       );
101 }
102
103 prefix_ bool senf::INet6Address::universalId()
104     const
105 {
106     return (*this)[8] & 2u;
107 }
108
109 prefix_ bool senf::INet6Address::groupId()
110     const
111 {
112     return (*this)[8] & 1u;
113 }
114
115 prefix_ senf::INet4Address senf::INet6Address::inet4address()
116     const
117 {
118     return INet4Address::from_data(&(*this)[12]);
119 }
120
121 prefix_ bool senf::INet6Address::inet4Mapped()
122     const
123 {
124     return CheckINet6Network<0u,0u,0u,0u,0u,0xFFFFu,96>::match(*this);
125 }
126
127 prefix_ bool senf::INet6Address::multicast()
128     const
129 {
130     return (*this)[0] == 0xFFu || inet4Mapped() && inet4address().multicast();
131 }
132
133 prefix_ senf::INet6Address::ScopeId senf::INet6Address::scope()
134     const
135 {
136     static ScopeId const scopeMap[]
137         = { ReservedScope, InterfaceScope, LinkScope, ReservedScope,
138             AdminScope, SiteScope, UnassignedScope, UnassignedScope,
139             OrganizationScope, UnassignedScope, UnassignedScope, UnassignedScope,
140             UnassignedScope, UnassignedScope, GlobalScope, ReservedScope };
141     return multicast() ? scopeMap[(*this)[1] & 0x0Fu] : 
142         (*this)[0] == 0xFEu ? (((*this)[1]&0xC0) == 0x80 ? LinkScope : 
143                                ((*this)[1]&0xC0) == 0xC0 ? SiteScope : GlobalScope ) 
144         : GlobalScope;
145 }
146
147 prefix_ bool senf::INet6Address::unicast()
148     const
149 {
150     return ! multicast();
151 }
152
153 prefix_ bool senf::INet6Address::hasEuid64()
154     const
155 {
156     return unicast() && ((*this)[0]&0xE0u) != 0u;
157 }
158
159 prefix_ bool senf::INet6Address::globalScope()
160     const
161 {
162     return scope() == GlobalScope;
163 }
164  
165 prefix_ bool senf::INet6Address::linkScope()
166     const
167 {
168     return scope() == LinkScope;
169 }
170
171 prefix_ bool senf::INet6Address::inet4Compatible()
172     const
173 {
174     return CheckINet6Network<0u,96>::match(*this);
175 }
176
177 prefix_ bool senf::INet6Address::globalMulticastAddr()
178     const
179 {
180     return multicast() && ! ((*this)[1] & 0x10u);
181 }
182
183 prefix_ bool senf::INet6Address::prefixMulticastAddr()
184     const
185 {
186     return multicast() && ((*this)[1] & 0x20u);
187 }
188
189 prefix_ bool senf::INet6Address::embeddedRpAddr()
190     const
191 {
192     return multicast() && ((*this)[1] & 0x40u);
193 }
194
195 prefix_ bool senf::INet6Address::boolean_test()
196     const
197 {
198     using boost::lambda::_1;
199     return std::find_if(begin(),end(), _1 != 0x00) != end();
200 }
201
202 prefix_ void senf::INet6Address::network(boost::uint64_t net)
203 {
204     (*this)[ 0] = net >> 56;
205     (*this)[ 1] = net >> 48;
206     (*this)[ 2] = net >> 40;
207     (*this)[ 3] = net >> 32;
208     (*this)[ 4] = net >> 24;
209     (*this)[ 5] = net >> 16;
210     (*this)[ 6] = net >>  8;
211     (*this)[ 7] = net      ;
212 }
213
214 prefix_ void senf::INet6Address::id(boost::uint64_t id)
215 {
216     (*this)[ 8] = id >> 56;
217     (*this)[ 9] = id >> 48;
218     (*this)[10] = id >> 40;
219     (*this)[11] = id >> 32;
220     (*this)[12] = id >> 24;
221     (*this)[13] = id >> 16;
222     (*this)[14] = id >>  8;
223     (*this)[15] = id      ;
224 }
225
226 ///////////////////////////////////////////////////////////////////////////
227 // senf::INet6Network
228
229 prefix_ senf::INet6Network::INet6Network()
230     : prefix_len_(), address_()
231 {}
232
233 prefix_ senf::INet6Network::INet6Network(INet6Address address, unsigned prefix_len)
234     : prefix_len_(prefix_len), address_(address)
235 {
236     using boost::lambda::_1;
237     using boost::lambda::_2;
238     detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
239 }
240
241 prefix_ senf::INet6Address const & senf::INet6Network::address()
242     const
243 {
244     return address_;
245 }
246
247 prefix_ unsigned senf::INet6Network::prefix_len()
248     const
249 {
250     return prefix_len_;
251 }
252
253 prefix_ bool senf::INet6Network::boolean_test()
254     const
255 {
256     return prefix_len() && address();
257 }
258
259 prefix_ bool senf::INet6Network::operator==(INet6Network const & other)
260     const
261 {
262     return prefix_len() == other.prefix_len() && address() == other.address();
263 }
264
265 prefix_ bool senf::INet6Network::match(INet6Address addr)
266     const
267 {
268     using boost::lambda::_1;
269     using boost::lambda::_2;
270     using boost::lambda::_3;
271     return detail::find_if_mask(prefix_len_, address_.begin(), address_.end(), addr.begin(),
272                                 _1 != (_2 & _3)) == address_.end();
273 }
274
275 prefix_ bool senf::INet6Network::match(INet6Network net)
276     const
277 {
278     return net.prefix_len() >= prefix_len() && match(net.address());
279 }
280
281 prefix_ senf::INet6Address senf::INet6Network::host(boost::uint64_t id)
282 {
283     INet6Address addr (address());
284     addr.id(id);
285     return addr;
286 }
287
288 prefix_ senf::INet6Network senf::INet6Network::subnet(boost::uint64_t net, unsigned prefix_len)
289 {
290     using boost::lambda::_1;
291     using boost::lambda::_2;
292     using boost::lambda::var;
293     using boost::lambda::ret;
294     INet6Address addr (address());
295     net <<= (64-prefix_len);
296     detail::apply_mask(prefix_len, addr.begin(), addr.end(),
297                        ( ( _1 |= ret<boost::uint8_t>((var(net) >> 56) & _2) ),
298                          ( var(net) <<= 8 ) ));
299     return INet6Network(addr, prefix_len);
300 }
301
302 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Network const & addr)
303 {
304     os << addr.address() << '/' << addr.prefix_len();
305     return os;
306 }
307
308 ///////////////////////////////////////////////////////////////////////////
309 // namespace senf::detail members
310
311 prefix_ boost::uint8_t senf::detail::low_bits_mask(unsigned bits)
312 {
313     return ((1<<bits)-1);
314 }
315
316 ///////////////////////////////cci.e///////////////////////////////////////
317 #undef prefix_
318
319 \f
320 // Local Variables:
321 // mode: c++
322 // fill-column: 100
323 // comment-column: 40
324 // c-file-style: "senf"
325 // indent-tabs-mode: nil
326 // ispell-local-dictionary: "american"
327 // compile-command: "scons -u test"
328 // End: