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"
33 #include <boost/tokenizer.hpp>
34 #include <boost/io/ios_state.hpp>
35 #include <boost/range.hpp>
37 //#include "MACAddress.mpp"
39 ///////////////////////////////cc.p////////////////////////////////////////
43 boost::uint8_t hexToNibble(char c)
46 throw senf::AddressSyntaxException();
50 throw senf::AddressSyntaxException();
54 throw senf::AddressSyntaxException();
58 throw senf::AddressSyntaxException();
61 template <class Range>
62 boost::uint8_t hexToByte(Range const & range)
64 if (boost::size(range) != 2)
65 throw senf::AddressSyntaxException();
66 typename boost::range_const_iterator<Range>::type i (boost::begin(range));
67 return hexToNibble(i[0])*16+hexToNibble(i[1]);
72 ///////////////////////////////////////////////////////////////////////////
75 prefix_ senf::MACAddress::MACAddress senf::MACAddress::from_string(std::string const & s)
77 MACAddress mac (senf::noinit);
78 typedef boost::char_separator<char> separator;
79 typedef boost::tokenizer<separator> tokenizer;
81 tokenizer tok (s,sep);
82 tokenizer::iterator i (tok.begin());
83 tokenizer::iterator const i_end (tok.end());
84 iterator j (mac.begin());
85 iterator const j_end (mac.end());
86 for (; i!=i_end && j!=j_end; ++i, ++j)
88 if (i!=i_end || j!=j_end)
89 throw AddressSyntaxException();
93 prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v)
95 if ( boost::uint16_t(v>>24) != 0xfffe )
96 throw AddressSyntaxException();
97 MACAddress mac (senf::noinit);
98 mac[0] = boost::uint8_t( v>>56 );
99 mac[1] = boost::uint8_t( v>>48 );
100 mac[2] = boost::uint8_t( v>>40 );
101 mac[3] = boost::uint8_t( v>>16 );
102 mac[4] = boost::uint8_t( v>> 8 );
103 mac[5] = boost::uint8_t( v );
107 senf::MACAddress const senf::MACAddress::Broadcast = senf::MACAddress(0xFFFFFFFFFFFFull);
108 senf::MACAddress const senf::MACAddress::None;
111 ///////////////////////////////////////////////////////////////////////////
114 prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac)
116 boost::io::ios_all_saver ias(os);
117 os << std::hex << std::setfill('0');
118 for (MACAddress::const_iterator i (mac.begin()); i != mac.end(); ++i) {
119 if (i != mac.begin())
121 os << std::setw(2) << unsigned(*i);
126 prefix_ std::istream & senf::operator>>(std::istream & is, MACAddress & mac)
132 mac = MACAddress::from_string(s);
134 catch (AddressException &) {
135 is.setstate(std::ios::failbit);
140 ///////////////////////////////cc.e////////////////////////////////////////
142 //#include "MACAddress.mpp"
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"