Socket: Moved PacketSocketHandle and related stuff into 'Raw' subdir
[senf.git] / Socket / Protocols / Raw / MACAddress.cc
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
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.
10 //
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.
15 //
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.
20
21 /** \file
22     \brief MACAddress non-inline non-template implementation */
23
24 #include "MACAddress.hh"
25 //#include "MACAddress.ih"
26
27 // Custom includes
28 #include <iomanip>
29 #include <boost/tokenizer.hpp>
30 #include <boost/io/ios_state.hpp>
31 #include <boost/range.hpp>
32
33 //#include "MACAddress.mpp"
34 #define prefix_
35 ///////////////////////////////cc.p////////////////////////////////////////
36
37 namespace {
38
39     boost::uint8_t hexToNibble(char c)
40     {
41         if (c<'0')
42             throw senf::MACAddress::SyntaxException();
43         else if (c<='9')
44             return c-'0';
45         else if (c<'A')
46             throw senf::MACAddress::SyntaxException();
47         else if (c<='F')
48             return c-'A'+10;
49         else if (c<'a')
50             throw senf::MACAddress::SyntaxException();
51         else if (c<='f')
52             return c-'a'+10;
53         else
54             throw senf::MACAddress::SyntaxException();
55     }
56     
57     template <class Range>
58     boost::uint8_t hexToByte(Range const & range)
59     {
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]);
64     }
65
66 }
67
68 ///////////////////////////////////////////////////////////////////////////
69 // senf::MACAddress
70
71 prefix_ senf::MACAddress::MACAddress senf::MACAddress::from_string(std::string const & s)
72 {
73     MACAddress mac (MACAddress::noinit);
74     typedef boost::char_separator<char> separator;
75     typedef boost::tokenizer<separator> tokenizer;
76     separator sep (":-");
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)
83         *j = hexToByte(*i);
84     if (i!=i_end || j!=j_end)
85         throw SyntaxException();
86     return mac;
87 }
88
89 prefix_ senf::MACAddress senf::MACAddress::from_eui64(boost::uint64_t v)
90 {
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     );
100     return mac;
101 }
102
103 ///////////////////////////////////////////////////////////////////////////
104 // namespace members
105
106 prefix_ std::ostream & senf::operator<<(std::ostream & os, MACAddress const & mac)
107 {
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())
112             os << ':';
113         os << std::setw(2) << unsigned(*i);
114     }
115     return os;
116 }
117
118 ///////////////////////////////cc.e////////////////////////////////////////
119 #undef prefix_
120 //#include "MACAddress.mpp"
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
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"
131 // End: