switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / Raw / MACAddress.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief MACAddress inline non-template implementation */
30
31 // Custom includes
32 #include <algorithm>
33 #include <boost/lambda/lambda.hpp>
34 #include "EUI64.hh"
35
36 #define prefix_ inline
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40 // senf::MACAddress
41
42 prefix_ senf::MACAddress::MACAddress()
43 {
44     std::fill(begin(),end(),0u);
45 }
46
47 prefix_ senf::MACAddress::MACAddress(senf::NoInit_t)
48 {}
49
50 prefix_ senf::MACAddress::MACAddress(boost::uint64_t v)
51 {
52     (*this)[0] = boost::uint8_t( v>>40 );
53     (*this)[1] = boost::uint8_t( v>>32 );
54     (*this)[2] = boost::uint8_t( v>>24 );
55     (*this)[3] = boost::uint8_t( v>>16 );
56     (*this)[4] = boost::uint8_t( v>> 8 );
57     (*this)[5] = boost::uint8_t( v     );
58 }
59
60 prefix_ bool senf::MACAddress::local()
61     const
62 {
63     return (*this)[0] & 0x02;
64 }
65
66 prefix_ bool senf::MACAddress::multicast()
67     const
68 {
69     return (*this)[0] & 0x01;
70 }
71
72 prefix_ bool senf::MACAddress::broadcast()
73     const
74 {
75     using  boost::lambda::_1;
76     return std::find_if(begin(),end(), _1 != 0xffu) == end();
77 }
78
79 prefix_ bool senf::MACAddress::boolean_test()
80     const
81 {
82     using boost::lambda::_1;
83     return std::find_if(begin(),end(), _1 != 0x00u) != end();
84 }
85
86 prefix_ boost::uint32_t senf::MACAddress::oui()
87     const
88 {
89     return
90         (boost::uint32_t((*this)[0])<<16) |
91         (boost::uint32_t((*this)[1])<<8) |
92         (*this)[2];
93 }
94
95 prefix_ boost::uint32_t senf::MACAddress::nic()
96     const
97 {
98     return
99         (boost::uint32_t((*this)[3])<<16) |
100         (boost::uint32_t((*this)[4])<<8) |
101         (*this)[5];
102 }
103
104 prefix_ boost::uint64_t senf::MACAddress::eui64()
105     const
106 {
107     return
108         (boost::uint64_t( (*this)[0] ) << 56) |
109         (boost::uint64_t( (*this)[1] ) << 48) |
110         (boost::uint64_t( (*this)[2] ) << 40) |
111         (boost::uint64_t( 0xfffe     ) << 24) |
112         (boost::uint64_t( (*this)[3] ) << 16) |
113         (boost::uint64_t( (*this)[4] ) <<  8) |
114         (boost::uint64_t( (*this)[5] )      );
115 }
116
117 prefix_ boost::uint64_t senf::MACAddress::uint64()
118     const
119 {
120     return
121         (boost::uint64_t( (*this)[0] ) << 40) |
122         (boost::uint64_t( (*this)[1] ) << 32) |
123         (boost::uint64_t( (*this)[2] ) << 24) |
124         (boost::uint64_t( (*this)[3] ) << 16) |
125         (boost::uint64_t( (*this)[4] ) <<  8) |
126         (boost::uint64_t( (*this)[5] )      );
127 }
128
129 prefix_ bool senf::operator==(MACAddress const & mac, EUI64 const & eui64)
130 {
131     return eui64.isMACCompatible()
132         && std::equal(eui64.begin(), eui64.begin()+3, mac.begin())
133         && std::equal(eui64.begin()+5, eui64.end(), mac.begin()+3);
134 }
135
136 prefix_ bool senf::operator==(EUI64 const & eui64, MACAddress const & mac)
137 {
138     return mac == eui64;
139 }
140
141 //-/////////////////////////////////////////////////////////////////////////////////////////////////
142 #undef prefix_
143
144
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // comment-column: 40
149 // c-file-style: "senf"
150 // indent-tabs-mode: nil
151 // ispell-local-dictionary: "american"
152 // compile-command: "scons -u test"
153 // End: