c12b0ed4aaa3c324837b74c392c6d4daf01d016f
[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 <string>
32 #include <sstream>
33 #include <boost/tokenizer.hpp>
34 #include <boost/io/ios_state.hpp>
35 #include <boost/range.hpp>
36
37 //#include "MACAddress.mpp"
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace {
42
43     boost::uint8_t hexToNibble(char c)
44     {
45         if (c<'0')
46             throw senf::AddressSyntaxException();
47         else if (c<='9')
48             return c-'0';
49         else if (c<'A')
50             throw senf::AddressSyntaxException();
51         else if (c<='F')
52             return c-'A'+10;
53         else if (c<'a')
54             throw senf::AddressSyntaxException();
55         else if (c<='f')
56             return c-'a'+10;
57         else
58             throw senf::AddressSyntaxException();
59     }
60     
61     template <class Range>
62     boost::uint8_t hexToByte(Range const & range)
63     {
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]);
68     }
69
70 }
71
72 ///////////////////////////////////////////////////////////////////////////
73 // senf::MACAddress
74
75 prefix_ senf::MACAddress::MACAddress senf::MACAddress::from_string(std::string const & s)
76 {
77     MACAddress mac (senf::noinit);
78     typedef boost::char_separator<char> separator;
79     typedef boost::tokenizer<separator> tokenizer;
80     separator sep (":-");
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)
87         *j = hexToByte(*i);
88     if (i!=i_end || j!=j_end)
89         throw AddressSyntaxException();
90     return mac;
91 }
92
93 prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v)
94 {
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     );
104     return mac;
105 }
106
107 senf::MACAddress const senf::MACAddress::Broadcast = senf::MACAddress(0xFFFFFFFFFFFFull);
108 senf::MACAddress const senf::MACAddress::None;
109
110 prefix_ std::string senf::MACAddress::toString() const {
111         std::ostringstream tmp; 
112         tmp << (*this);
113         return tmp.str();
114 }
115
116 ///////////////////////////////////////////////////////////////////////////
117 // namespace members
118
119 prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac)
120 {
121     boost::io::ios_all_saver ias(os);
122     os << std::hex << std::setfill('0');
123     for (MACAddress::const_iterator i (mac.begin()); i != mac.end(); ++i) {
124         if (i != mac.begin())
125             os << ':';
126         os << std::setw(2) << unsigned(*i);
127     }
128     return os;
129 }
130
131 ///////////////////////////////cc.e////////////////////////////////////////
132 #undef prefix_
133 //#include "MACAddress.mpp"
134
135 \f
136 // Local Variables:
137 // mode: c++
138 // fill-column: 100
139 // comment-column: 40
140 // c-file-style: "senf"
141 // indent-tabs-mode: nil
142 // ispell-local-dictionary: "american"
143 // compile-command: "scons -u test"
144 // End: