4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
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 MACAddress non-inline non-template implementation */
26 #include "MACAddress.hh"
27 //#include "MACAddress.ih"
31 #include <boost/tokenizer.hpp>
32 #include <boost/io/ios_state.hpp>
33 #include <boost/range.hpp>
35 //#include "MACAddress.mpp"
37 ///////////////////////////////cc.p////////////////////////////////////////
41 boost::uint8_t hexToNibble(char c)
44 throw senf::MACAddress::SyntaxException();
48 throw senf::MACAddress::SyntaxException();
52 throw senf::MACAddress::SyntaxException();
56 throw senf::MACAddress::SyntaxException();
59 template <class Range>
60 boost::uint8_t hexToByte(Range const & range)
62 if (boost::size(range) != 2)
63 throw senf::MACAddress::SyntaxException();
64 typename boost::range_const_iterator<Range>::type i (boost::begin(range));
65 return hexToNibble(i[0])*16+hexToNibble(i[1]);
70 ///////////////////////////////////////////////////////////////////////////
73 prefix_ senf::MACAddress::MACAddress senf::MACAddress::from_string(std::string const & s)
75 MACAddress mac (MACAddress::noinit);
76 typedef boost::char_separator<char> separator;
77 typedef boost::tokenizer<separator> tokenizer;
79 tokenizer tok (s,sep);
80 tokenizer::iterator i (tok.begin());
81 tokenizer::iterator const i_end (tok.end());
82 iterator j (mac.begin());
83 iterator const j_end (mac.end());
84 for (; i!=i_end && j!=j_end; ++i, ++j)
86 if (i!=i_end || j!=j_end)
87 throw SyntaxException();
91 prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v)
93 if ( boost::uint16_t(v>>24) != 0xfffe )
94 throw SyntaxException();
95 MACAddress mac (MACAddress::noinit);
96 mac[0] = boost::uint8_t( v>>56 );
97 mac[1] = boost::uint8_t( v>>48 );
98 mac[2] = boost::uint8_t( v>>40 );
99 mac[3] = boost::uint8_t( v>>16 );
100 mac[4] = boost::uint8_t( v>> 8 );
101 mac[5] = boost::uint8_t( v );
105 senf::MACAddress const senf::MACAddress::Broadcast = senf::MACAddress(0xFFFFFFFFFFFFull);
106 senf::MACAddress const senf::MACAddress::None;
108 ///////////////////////////////////////////////////////////////////////////
111 prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac)
113 boost::io::ios_all_saver ias(os);
114 os << std::hex << std::setfill('0');
115 for (MACAddress::const_iterator i (mac.begin()); i != mac.end(); ++i) {
116 if (i != mac.begin())
118 os << std::setw(2) << unsigned(*i);
123 ///////////////////////////////cc.e////////////////////////////////////////
125 //#include "MACAddress.mpp"
131 // comment-column: 40
132 // c-file-style: "senf"
133 // indent-tabs-mode: nil
134 // ispell-local-dictionary: "american"
135 // compile-command: "scons -u test"