MACAddress.cc

Go to the documentation of this file.
00001 // $Id: MACAddress.cc 1792 2011-06-09 15:49:59Z tho $
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 #include "MACAddress.hh"
00027 //#include "MACAddress.ih"
00028 
00029 // Custom includes
00030 #include <iomanip>
00031 #include <boost/io/ios_state.hpp>
00032 #include <senf/Socket/Protocols/AddressExceptions.hh>
00033 #include "ParseString.hh"
00034 #include "EUI64.hh"
00035 
00036 //#include "MACAddress.mpp"
00037 #define prefix_
00038 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00039 
00040 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00041 // senf::MACAddress
00042 
00043 prefix_ senf::MACAddress senf::MACAddress::from_string(std::string const & s)
00044 {
00045     MACAddress mac (senf::noinit);
00046     detail::parseHexString(s, ":-", mac.begin(), mac.end());
00047     return mac;
00048 }
00049 
00050 prefix_ senf::MACAddress senf::MACAddress::from_eui64(senf::EUI64 const & eui)
00051 {
00052     if (eui[3] != 0xffu || eui[4] != 0xfeu)
00053         throw AddressSyntaxException() << ": EUI64 is not MAC compatible: " << eui;
00054     MACAddress mac (senf::noinit);
00055     mac[0] = eui[0];
00056     mac[1] = eui[1];
00057     mac[2] = eui[2];
00058     mac[3] = eui[5];
00059     mac[4] = eui[6];
00060     mac[5] = eui[7];
00061     return mac;
00062 }
00063 
00064 senf::MACAddress const senf::MACAddress::Broadcast = senf::MACAddress(0xFFFFFFFFFFFFull);
00065 senf::MACAddress const senf::MACAddress::None;
00066 
00067 
00068 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00069 // namespace members
00070 
00071 prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac)
00072 {
00073     boost::io::ios_all_saver ias (os);
00074     os << std::hex << std::setfill('0') << std::right;
00075     for (MACAddress::const_iterator i (mac.begin()); i != mac.end(); ++i) {
00076         if (i != mac.begin())
00077             os << ':';
00078         os << std::setw(2) << unsigned(*i);
00079     }
00080     return os;
00081 }
00082 
00083 prefix_ std::istream & senf::operator>>(std::istream & is, MACAddress & mac)
00084 {
00085     std::string s;
00086     if (!(is >> s))
00087         return is;
00088     try {
00089         mac = MACAddress::from_string(s);
00090     }
00091     catch (AddressException &) {
00092         is.setstate(std::ios::failbit);
00093     }
00094     return is;
00095 }
00096 
00097 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00098 #undef prefix_
00099 //#include "MACAddress.mpp"
00100 
00101 
00102 // Local Variables:
00103 // mode: c++
00104 // fill-column: 100
00105 // comment-column: 40
00106 // c-file-style: "senf"
00107 // indent-tabs-mode: nil
00108 // ispell-local-dictionary: "american"
00109 // compile-command: "scons -u test"
00110 // End: