4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief INet6Address inline non-template implementation */
26 #include "INet6Address.ih"
30 #include <boost/lambda/lambda.hpp>
32 #define prefix_ inline
33 ///////////////////////////////cci.p///////////////////////////////////////
35 prefix_ senf::INet6Address::INet6Address(NoInit_t)
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)
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);
61 prefix_ senf::INet6Address senf::INet6Address::from_inet4address(INet4Address addr4)
66 std::copy(addr4.begin(), addr4.end(), addr.begin()+12);
70 prefix_ boost::uint64_t senf::INet6Address::network()
74 ((boost::uint64_t((*this)[0]) & 0xff) << 56 ) |
75 ((boost::uint64_t((*this)[1]) & 0xff) << 48 ) |
76 ((boost::uint64_t((*this)[2]) & 0xff) << 40 ) |
77 ((boost::uint64_t((*this)[3]) & 0xff) << 32 ) |
78 ((boost::uint64_t((*this)[4]) & 0xff) << 24 ) |
79 ((boost::uint64_t((*this)[5]) & 0xff) << 16 ) |
80 ((boost::uint64_t((*this)[6]) & 0xff) << 8 ) |
81 ((boost::uint64_t((*this)[7]) & 0xff) );
84 prefix_ bool senf::INet6Address::hasEuid64()
87 return unicast() && ((*this)[0]&0xE0u) != 0u;
90 prefix_ boost::uint64_t senf::INet6Address::id()
94 ((boost::uint64_t((*this)[ 8]) & 0xff) << 56 ) |
95 ((boost::uint64_t((*this)[ 9]) & 0xff) << 48 ) |
96 ((boost::uint64_t((*this)[10]) & 0xff) << 40 ) |
97 ((boost::uint64_t((*this)[11]) & 0xff) << 32 ) |
98 ((boost::uint64_t((*this)[12]) & 0xff) << 24 ) |
99 ((boost::uint64_t((*this)[13]) & 0xff) << 16 ) |
100 ((boost::uint64_t((*this)[14]) & 0xff) << 8 ) |
101 ((boost::uint64_t((*this)[15]) & 0xff) );
104 prefix_ bool senf::INet6Address::universalId()
107 return (*this)[8] & 2u;
110 prefix_ bool senf::INet6Address::groupId()
113 return (*this)[8] & 1u;
116 prefix_ bool senf::INet6Address::unicast()
119 return ! multicast();
122 prefix_ bool senf::INet6Address::multicast()
125 return (*this)[0] == 0xFFu;
128 prefix_ senf::INet6Address::ScopeId senf::INet6Address::scope()
131 static ScopeId const scopeMap[]
132 = { ReservedScope, InterfaceScope, LinkScope, ReservedScope,
133 AdminScope, SiteScope, UnassignedScope, UnassignedScope,
134 OrganizationScope, UnassignedScope, UnassignedScope, UnassignedScope,
135 UnassignedScope, UnassignedScope, GlobalScope, ReservedScope };
136 return multicast() ? scopeMap[(*this)[1] & 0x0Fu] :
137 (*this)[0] == 0xFEu ? (((*this)[1]&0xC0) == 0x80 ? LinkScope :
138 ((*this)[1]&0xC0) == 0xC0 ? SiteScope : GlobalScope )
142 prefix_ bool senf::INet6Address::globalScope()
145 return scope() == GlobalScope;
148 prefix_ bool senf::INet6Address::linkScope()
151 return scope() == LinkScope;
154 prefix_ senf::INet4Address senf::INet6Address::inet4address()
157 return INet4Address::from_data(&(*this)[12]);
160 prefix_ bool senf::INet6Address::ipv4Compatible()
163 return CheckINet6Network<0u,96>::match(*this);
166 prefix_ bool senf::INet6Address::ipv4Mapped()
169 return CheckINet6Network<0u,0u,0u,0u,0u,0xFFFFu,96>::match(*this);
172 prefix_ bool senf::INet6Address::globalMulticastAddr()
175 return multicast() && ! ((*this)[1] & 0x10u);
178 prefix_ bool senf::INet6Address::prefixMulticastAddr()
181 return multicast() && ((*this)[1] & 0x20u);
184 prefix_ bool senf::INet6Address::embeddedRpAddr()
187 return multicast() && ((*this)[1] & 0x40u);
190 prefix_ bool senf::INet6Address::boolean_test()
193 using boost::lambda::_1;
194 return std::find_if(begin(),end(), _1 != 0x00) != end();
197 prefix_ void senf::INet6Address::network(boost::uint64_t net)
199 (*this)[ 0] = net >> 56;
200 (*this)[ 1] = net >> 48;
201 (*this)[ 2] = net >> 40;
202 (*this)[ 3] = net >> 32;
203 (*this)[ 4] = net >> 24;
204 (*this)[ 5] = net >> 16;
205 (*this)[ 6] = net >> 8;
209 prefix_ void senf::INet6Address::id(boost::uint64_t id)
211 (*this)[ 8] = id >> 56;
212 (*this)[ 9] = id >> 48;
213 (*this)[10] = id >> 40;
214 (*this)[11] = id >> 32;
215 (*this)[12] = id >> 24;
216 (*this)[13] = id >> 16;
217 (*this)[14] = id >> 8;
221 ///////////////////////////////////////////////////////////////////////////
222 // senf::INet6Network
224 prefix_ senf::INet6Network::INet6Network()
225 : prefix_len_(), address_()
228 prefix_ senf::INet6Network::INet6Network(INet6Address address, unsigned prefix_len)
229 : prefix_len_(prefix_len), address_(address)
231 using boost::lambda::_1;
232 using boost::lambda::_2;
233 detail::apply_mask(prefix_len_, address_.begin(), address_.end(), _1 &= _2);
236 prefix_ senf::INet6Address const & senf::INet6Network::address()
242 prefix_ unsigned senf::INet6Network::prefix_len()
248 prefix_ bool senf::INet6Network::boolean_test()
251 return prefix_len() && address();
254 prefix_ bool senf::INet6Network::operator==(INet6Network const & other)
257 return prefix_len() == other.prefix_len() && address() == other.address();
260 prefix_ bool senf::INet6Network::match(INet6Address addr)
263 using boost::lambda::_1;
264 using boost::lambda::_2;
265 using boost::lambda::_3;
266 return detail::find_if_mask(prefix_len_, address_.begin(), address_.end(), addr.begin(),
267 _1 != (_2 & _3)) == address_.end();
270 prefix_ bool senf::INet6Network::match(INet6Network net)
273 return net.prefix_len() >= prefix_len() && match(net.address());
276 prefix_ senf::INet6Address senf::INet6Network::host(boost::uint64_t id)
278 INet6Address addr (address());
283 prefix_ senf::INet6Network senf::INet6Network::subnet(boost::uint64_t net, unsigned prefix_len)
285 using boost::lambda::_1;
286 using boost::lambda::_2;
287 using boost::lambda::var;
288 using boost::lambda::ret;
289 INet6Address addr (address());
290 net <<= (64-prefix_len);
291 detail::apply_mask(prefix_len, addr.begin(), addr.end(),
292 ( ( _1 |= ret<boost::uint8_t>((var(net) >> 56) & _2) ),
293 ( var(net) <<= 8 ) ));
294 return INet6Network(addr, prefix_len);
297 prefix_ std::ostream & senf::operator<<(std::ostream & os, INet6Network const & addr)
299 os << addr.address() << '/' << addr.prefix_len();
303 ///////////////////////////////////////////////////////////////////////////
304 // namespace senf::detail members
306 prefix_ boost::uint8_t senf::detail::low_bits_mask(unsigned bits)
308 return ((1<<bits)-1);
311 ///////////////////////////////cci.e///////////////////////////////////////
318 // comment-column: 40
319 // c-file-style: "senf"
320 // indent-tabs-mode: nil
321 // ispell-local-dictionary: "american"
322 // compile-command: "scons -u test"