2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 // Stefan Bund <g0dil@berlios.de>
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 \brief MACAddress non-inline non-template implementation */
24 #include "MACAddress.hh"
25 //#include "MACAddress.ih"
29 #include <boost/tokenizer.hpp>
30 #include <boost/io/ios_state.hpp>
31 #include <boost/range.hpp>
33 //#include "MACAddress.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
39 boost::uint8_t hexToNibble(char c)
42 throw senf::MACAddress::SyntaxException();
46 throw senf::MACAddress::SyntaxException();
50 throw senf::MACAddress::SyntaxException();
54 throw senf::MACAddress::SyntaxException();
57 template <class Range>
58 boost::uint8_t hexToByte(Range const & range)
60 if (boost::size(range) != 2)
61 throw senf::MACAddress::SyntaxException();
62 typename boost::range_const_iterator<Range>::type i (boost::begin(range));
63 return hexToNibble(i[0])*16+hexToNibble(i[1]);
68 ///////////////////////////////////////////////////////////////////////////
71 prefix_ senf::MACAddress::MACAddress senf::MACAddress::from_string(std::string const & s)
73 MACAddress mac (MACAddress::noinit);
74 typedef boost::char_separator<char> separator;
75 typedef boost::tokenizer<separator> tokenizer;
77 tokenizer tok (s,sep);
78 tokenizer::iterator i (tok.begin());
79 tokenizer::iterator const i_end (tok.end());
80 iterator j (mac.begin());
81 iterator const j_end (mac.end());
82 for (; i!=i_end && j!=j_end; ++i, ++j)
84 if (i!=i_end || j!=j_end)
85 throw SyntaxException();
89 prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v)
91 if ( boost::uint16_t(v>>24) != 0xfffe )
92 throw SyntaxException();
93 MACAddress mac (MACAddress::noinit);
94 mac[0] = boost::uint8_t( v>>56 );
95 mac[1] = boost::uint8_t( v>>48 );
96 mac[2] = boost::uint8_t( v>>40 );
97 mac[3] = boost::uint8_t( v>>16 );
98 mac[4] = boost::uint8_t( v>> 8 );
99 mac[5] = boost::uint8_t( v );
103 ///////////////////////////////////////////////////////////////////////////
106 prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac)
108 boost::io::ios_all_saver ias(os);
109 os << std::hex << std::setfill('0');
110 for (MACAddress::const_iterator i (mac.begin()); i != mac.end(); ++i) {
111 if (i != mac.begin())
113 os << std::setw(2) << unsigned(*i);
118 ///////////////////////////////cc.e////////////////////////////////////////
120 //#include "MACAddress.mpp"
126 // comment-column: 40
127 // c-file-style: "senf"
128 // indent-tabs-mode: nil
129 // ispell-local-dictionary: "american"
130 // compile-command: "scons -u test"