removed some useless spaces; not very important, I know :)
[senf.git] / Socket / Protocols / Raw / MACAddress.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief MACAddress non-inline non-template implementation */
25
26 #include "MACAddress.hh"
27 //#include "MACAddress.ih"
28
29 // Custom includes
30 #include <iomanip>
31 #include <boost/tokenizer.hpp>
32 #include <boost/io/ios_state.hpp>
33 #include <boost/range.hpp>
34
35 //#include "MACAddress.mpp"
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 namespace {
40
41     boost::uint8_t hexToNibble(char c)
42     {
43         if (c<'0')
44             throw senf::MACAddress::SyntaxException();
45         else if (c<='9')
46             return c-'0';
47         else if (c<'A')
48             throw senf::MACAddress::SyntaxException();
49         else if (c<='F')
50             return c-'A'+10;
51         else if (c<'a')
52             throw senf::MACAddress::SyntaxException();
53         else if (c<='f')
54             return c-'a'+10;
55         else
56             throw senf::MACAddress::SyntaxException();
57     }
58     
59     template <class Range>
60     boost::uint8_t hexToByte(Range const & range)
61     {
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]);
66     }
67
68 }
69
70 ///////////////////////////////////////////////////////////////////////////
71 // senf::MACAddress
72
73 prefix_ senf::MACAddress::MACAddress senf::MACAddress::from_string(std::string const & s)
74 {
75     MACAddress mac (senf::noinit);
76     typedef boost::char_separator<char> separator;
77     typedef boost::tokenizer<separator> tokenizer;
78     separator sep (":-");
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)
85         *j = hexToByte(*i);
86     if (i!=i_end || j!=j_end)
87         throw SyntaxException();
88     return mac;
89 }
90
91 prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v)
92 {
93     if ( boost::uint16_t(v>>24)  != 0xfffe )
94         throw SyntaxException();
95     MACAddress mac (senf::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     );
102     return mac;
103 }
104
105 senf::MACAddress const senf::MACAddress::Broadcast = senf::MACAddress(0xFFFFFFFFFFFFull);
106 senf::MACAddress const senf::MACAddress::None;
107
108 ///////////////////////////////////////////////////////////////////////////
109 // namespace members
110
111 prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac)
112 {
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())
117             os << ':';
118         os << std::setw(2) << unsigned(*i);
119     }
120     return os;
121 }
122
123 ///////////////////////////////cc.e////////////////////////////////////////
124 #undef prefix_
125 //#include "MACAddress.mpp"
126
127 \f
128 // Local Variables:
129 // mode: c++
130 // fill-column: 100
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"
136 // End: