some documentation updates
[senf.git] / Socket / Protocols / INet / INet6Address.cci
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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 // Custom includes
27 #include <algorithm>
28 #include <boost/lambda/lambda.hpp>
29
30 #define prefix_ inline
31 ///////////////////////////////cci.p///////////////////////////////////////
32
33 prefix_ senf::INet6Address::INet6Address(NoInit_t)
34 {}
35
36 prefix_ senf::INet6Address::INet6Address(boost::uint16_t a0, boost::uint16_t a1,
37                                          boost::uint16_t a2, boost::uint16_t a3,
38                                          boost::uint16_t a4, boost::uint16_t a5,
39                                          boost::uint16_t a6, boost::uint16_t a7)
40 {
41     (*this)[ 0] = boost::uint8_t(a0>>8);
42     (*this)[ 1] = boost::uint8_t(a0);
43     (*this)[ 2] = boost::uint8_t(a1>>8);
44     (*this)[ 3] = boost::uint8_t(a1);
45     (*this)[ 4] = boost::uint8_t(a2>>8);
46     (*this)[ 5] = boost::uint8_t(a2);
47     (*this)[ 6] = boost::uint8_t(a3>>8);
48     (*this)[ 7] = boost::uint8_t(a3);
49     (*this)[ 8] = boost::uint8_t(a4>>8);
50     (*this)[ 9] = boost::uint8_t(a4);
51     (*this)[10] = boost::uint8_t(a5>>8);
52     (*this)[11] = boost::uint8_t(a5);
53     (*this)[12] = boost::uint8_t(a6>>8);
54     (*this)[13] = boost::uint8_t(a6);
55     (*this)[14] = boost::uint8_t(a7>>8);
56     (*this)[15] = boost::uint8_t(a7);
57 }
58
59 prefix_ senf::INet6Address senf::INet6Address::from_inet4address(INet4Address addr4)
60 {
61     INet6Address addr;
62     addr[10] = 0xffu;
63     addr[11] = 0xffu;
64     std::copy(addr4.begin(), addr4.end(), addr.begin()+12);
65     return addr;
66 }
67
68 prefix_ boost::uint64_t senf::INet6Address::network()
69     const
70 {
71     return 
72         ((boost::uint64_t((*this)[0]) & 0xff) << 56 ) |
73         ((boost::uint64_t((*this)[1]) & 0xff) << 48 ) |
74         ((boost::uint64_t((*this)[2]) & 0xff) << 40 ) |
75         ((boost::uint64_t((*this)[3]) & 0xff) << 32 ) |
76         ((boost::uint64_t((*this)[4]) & 0xff) << 24 ) |
77         ((boost::uint64_t((*this)[5]) & 0xff) << 16 ) |
78         ((boost::uint64_t((*this)[6]) & 0xff) <<  8 ) |
79         ((boost::uint64_t((*this)[7]) & 0xff)       );
80 }
81
82 prefix_ bool senf::INet6Address::hasEuid64()
83     const
84 {
85     return unicast() && ((*this)[0]&0xE0u) != 0u;
86 }
87
88 prefix_ boost::uint64_t senf::INet6Address::id()
89     const
90 {
91     return 
92         ((boost::uint64_t((*this)[ 8]) & 0xff) << 56 ) |
93         ((boost::uint64_t((*this)[ 9]) & 0xff) << 48 ) |
94         ((boost::uint64_t((*this)[10]) & 0xff) << 40 ) |
95         ((boost::uint64_t((*this)[11]) & 0xff) << 32 ) |
96         ((boost::uint64_t((*this)[12]) & 0xff) << 24 ) |
97         ((boost::uint64_t((*this)[13]) & 0xff) << 16 ) |
98         ((boost::uint64_t((*this)[14]) & 0xff) <<  8 ) |
99         ((boost::uint64_t((*this)[15]) & 0xff)       );
100 }
101
102 prefix_ bool senf::INet6Address::universalId()
103     const
104 {
105     return (*this)[8] & 2u;
106 }
107
108 prefix_ bool senf::INet6Address::groupId()
109     const
110 {
111     return (*this)[8] & 1u;
112 }
113
114 prefix_ bool senf::INet6Address::unicast()
115     const
116 {
117     return ! multicast();
118 }
119
120 prefix_ bool senf::INet6Address::multicast()
121     const
122 {
123     return (*this)[0] == 0xFFu;
124 }
125
126 prefix_ senf::INet6Address::ScopeId senf::INet6Address::scope()
127     const
128 {
129     static ScopeId const scopeMap[]
130         = { ReservedScope, InterfaceScope, LinkScope, ReservedScope,
131             AdminScope, SiteScope, UnassignedScope, UnassignedScope,
132             OrganizationScope, UnassignedScope, UnassignedScope, UnassignedScope,
133             UnassignedScope, UnassignedScope, GlobalScope, ReservedScope };
134     return multicast() ? scopeMap[(*this)[1] & 0x0Fu] : 
135         (*this)[0] == 0xFEu ? (((*this)[1]&0xC0) == 0x80 ? LinkScope : 
136                                ((*this)[1]&0xC0) == 0xC0 ? SiteScope : GlobalScope ) 
137         : GlobalScope;
138 }
139
140 prefix_ bool senf::INet6Address::globalScope()
141     const
142 {
143     return scope() == GlobalScope;
144 }
145  
146 prefix_ bool senf::INet6Address::linkScope()
147     const
148 {
149     return scope() == LinkScope;
150 }
151
152 prefix_ senf::INet4Address senf::INet6Address::inet4address()
153     const
154 {
155     return INet4Address::from_data(&(*this)[12]);
156 }
157
158 prefix_ bool senf::INet6Address::ipv4Compatible()
159     const
160 {
161     return CheckINet6Network<0u,96>::match(*this);
162 }
163
164 prefix_ bool senf::INet6Address::ipv4Mapped()
165     const
166 {
167     return CheckINet6Network<0u,0u,0u,0u,0u,0xFFFFu,96>::match(*this);
168 }
169
170 prefix_ bool senf::INet6Address::globalMulticastAddr()
171     const
172 {
173     return multicast() && ! ((*this)[1] & 0x10u);
174 }
175
176 prefix_ bool senf::INet6Address::prefixMulticastAddr()
177     const
178 {
179     return multicast() && ((*this)[1] & 0x20u);
180 }
181
182 prefix_ bool senf::INet6Address::embeddedRpAddr()
183     const
184 {
185     return multicast() && ((*this)[1] & 0x40u);
186 }
187
188 prefix_ bool senf::INet6Address::boolean_test()
189     const
190 {
191     using boost::lambda::_1;
192     return std::find_if(begin(),end(), _1 != 0x00) != end();
193 }
194
195 prefix_ void senf::INet6Address::network(boost::uint64_t net)
196 {
197     (*this)[ 0] = net >> 56;
198     (*this)[ 1] = net >> 48;
199     (*this)[ 2] = net >> 40;
200     (*this)[ 3] = net >> 32;
201     (*this)[ 4] = net >> 24;
202     (*this)[ 5] = net >> 16;
203     (*this)[ 6] = net >>  8;
204     (*this)[ 7] = net      ;
205 }
206
207 prefix_ void senf::INet6Address::id(boost::uint64_t id)
208 {
209     (*this)[ 8] = id >> 56;
210     (*this)[ 9] = id >> 48;
211     (*this)[10] = id >> 40;
212     (*this)[11] = id >> 32;
213     (*this)[12] = id >> 24;
214     (*this)[13] = id >> 16;
215     (*this)[14] = id >>  8;
216     (*this)[15] = id      ;
217 }
218
219 ///////////////////////////////cci.e///////////////////////////////////////
220 #undef prefix_
221
222 \f
223 // Local Variables:
224 // mode: c++
225 // fill-column: 100
226 // comment-column: 40
227 // c-file-style: "senf"
228 // indent-tabs-mode: nil
229 // ispell-local-dictionary: "american"
230 // compile-command: "scons -u test"
231 // End: