MACAddress.cci

Go to the documentation of this file.
00001 // $Id: MACAddress.cci 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 // Custom includes
00027 #include <algorithm>
00028 #include <boost/lambda/lambda.hpp>
00029 #include "EUI64.hh"
00030 
00031 #define prefix_ inline
00032 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00033 
00034 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00035 // senf::MACAddress
00036 
00037 prefix_ senf::MACAddress::MACAddress()
00038 {
00039     std::fill(begin(),end(),0u);
00040 }
00041 
00042 prefix_ senf::MACAddress::MACAddress(senf::NoInit_t)
00043 {}
00044 
00045 prefix_ senf::MACAddress::MACAddress(boost::uint64_t v)
00046 {
00047     (*this)[0] = boost::uint8_t( v>>40 );
00048     (*this)[1] = boost::uint8_t( v>>32 );
00049     (*this)[2] = boost::uint8_t( v>>24 );
00050     (*this)[3] = boost::uint8_t( v>>16 );
00051     (*this)[4] = boost::uint8_t( v>> 8 );
00052     (*this)[5] = boost::uint8_t( v     );
00053 }
00054 
00055 prefix_ bool senf::MACAddress::local()
00056     const
00057 {
00058     return (*this)[0] & 0x02;
00059 }
00060 
00061 prefix_ bool senf::MACAddress::multicast()
00062     const
00063 {
00064     return (*this)[0] & 0x01;
00065 }
00066 
00067 prefix_ bool senf::MACAddress::broadcast()
00068     const
00069 {
00070     using  boost::lambda::_1;
00071     return std::find_if(begin(),end(), _1 != 0xffu) == end();
00072 }
00073 
00074 prefix_ bool senf::MACAddress::boolean_test()
00075     const
00076 {
00077     using boost::lambda::_1;
00078     return std::find_if(begin(),end(), _1 != 0x00u) != end();
00079 }
00080 
00081 prefix_ boost::uint32_t senf::MACAddress::oui()
00082     const
00083 {
00084     return
00085         (boost::uint32_t((*this)[0])<<16) |
00086         (boost::uint32_t((*this)[1])<<8) |
00087         (*this)[2];
00088 }
00089 
00090 prefix_ boost::uint32_t senf::MACAddress::nic()
00091     const
00092 {
00093     return
00094         (boost::uint32_t((*this)[3])<<16) |
00095         (boost::uint32_t((*this)[4])<<8) |
00096         (*this)[5];
00097 }
00098 
00099 prefix_ boost::uint64_t senf::MACAddress::eui64()
00100     const
00101 {
00102     return
00103         (boost::uint64_t( (*this)[0] ) << 56) |
00104         (boost::uint64_t( (*this)[1] ) << 48) |
00105         (boost::uint64_t( (*this)[2] ) << 40) |
00106         (boost::uint64_t( 0xfffe     ) << 24) |
00107         (boost::uint64_t( (*this)[3] ) << 16) |
00108         (boost::uint64_t( (*this)[4] ) <<  8) |
00109         (boost::uint64_t( (*this)[5] )      );
00110 }
00111 
00112 prefix_ boost::uint64_t senf::MACAddress::uint64()
00113     const
00114 {
00115     return
00116         (boost::uint64_t( (*this)[0] ) << 40) |
00117         (boost::uint64_t( (*this)[1] ) << 32) |
00118         (boost::uint64_t( (*this)[2] ) << 24) |
00119         (boost::uint64_t( (*this)[3] ) << 16) |
00120         (boost::uint64_t( (*this)[4] ) <<  8) |
00121         (boost::uint64_t( (*this)[5] )      );
00122 }
00123 
00124 prefix_ bool senf::operator==(MACAddress const & mac, EUI64 const & eui64)
00125 {
00126     return eui64.isMACCompatible()
00127         && std::equal(eui64.begin(), eui64.begin()+3, mac.begin())
00128         && std::equal(eui64.begin()+5, eui64.end(), mac.begin()+3);
00129 }
00130 
00131 prefix_ bool senf::operator==(EUI64 const & eui64, MACAddress const & mac)
00132 {
00133     return mac == eui64;
00134 }
00135 
00136 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00137 #undef prefix_
00138 
00139 
00140 // Local Variables:
00141 // mode: c++
00142 // fill-column: 100
00143 // comment-column: 40
00144 // c-file-style: "senf"
00145 // indent-tabs-mode: nil
00146 // ispell-local-dictionary: "american"
00147 // compile-command: "scons -u test"
00148 // End: