d0d5932b4effb794bbd4487a936e41c4ff05855f
[senf.git] / senf / Socket / Protocols / Raw / MACAddress.hh
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 public header */
25
26 #ifndef HH_SENF_Socket_Protocols_Raw_MACAddress_
27 #define HH_SENF_Socket_Protocols_Raw_MACAddress_ 1
28
29 // Custom includes
30 #include <iostream>
31 #include <boost/cstdint.hpp>
32 #include <boost/array.hpp>
33 #include <boost/utility.hpp>
34 #include <boost/type_traits.hpp>
35 #include <senf/Utils/safe_bool.hh>
36 #include <senf/Utils/Tags.hh>
37
38 //#include "MACAddress.mpp"
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 namespace senf {
42
43     class EUI64;
44
45     /** \brief Ethernet MAC address
46
47         The Ethernet MAC is modeled as a fixed-size container/sequence of 6 bytes. A MACAddress can
48         be converted from/to the following representations
49
50         <table class="senf">
51         <tr><td><tt>boost::uint64_t</tt></td>
52                 <td><tt>senf::MACAddress(0x112233445566ull)</tt><br/>
53                     <i>mac</i><tt>.uint64()</tt></td></tr>
54         <tr><td><tt>std::string</tt></td>
55                 <td><tt>senf::MACAddress::from_string("11:22:33:44:55:66")</tt><br/>
56                     <tt>senf::str(</tt><i>mac</i><tt>)</tt></td></tr>
57         <tr><td><i>raw data</i><br/>&nbsp;&nbsp;&nbsp;&nbsp;(6 bytes)</td>
58                 <td><tt>senf::MACAddress::from_data(</tt><i>iterator</i><tt>)</tt><br/>
59                     <i>mac</i><tt>.begin()</tt></td></tr>
60         <tr><td>senf::EUI64</td>
61                 <td><tt>senf::MACAddress::from_eui64(</tt><i>eui64</i><tt>)</tt><br/>
62                     <tt>senf::EUI64::from_mac(</tt><i>mac</i><tt>)</tt></td></tr>
63         </table>
64
65         Since MACAddress is based on \c boost::array, you can access the raw data bytes of the
66         address using \c begin(), \c end() or \c operator[]:
67         \code
68         senf::MACAddress mac (...);
69         std::vector<char> data;
70         data.resize(6);
71         std::copy(mac.begin(), mac.end(), data.begin()); // Copy 6 bytes
72         \endcode
73
74         \implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
75             instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
76             doubles as string literal and as arbitrary data iterator. The iterator constructor can
77             therefore not be distinguished from initialization with a string literal. Therefore we
78             need to disambiguate using the named constructors.
79
80         \ingroup addr_group
81      */
82     struct MACAddress
83         : public boost::array<boost::uint8_t,6>,
84           public comparable_safe_bool<MACAddress>
85     {
86         static MACAddress const Broadcast; ///< The broadcast address
87         static MACAddress const None;   ///< The empty (0) address
88
89         MACAddress();                   ///< Construct zero-initialized address
90         MACAddress(senf::NoInit_t);     ///< Construct uninitialized (!) address
91         explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants
92
93         static MACAddress from_string(std::string const & s);
94                                         ///< Construct address from string representation
95                                         /**< The string representation must exactly match the form
96                                              <tt>dd:dd:dd:dd:dd:dd</tt> where <tt>d</tt> is any
97                                              hexadecimal digit. In place of ':', '-' is also
98                                              accepted as a delimiter.
99                                              \throws AddressSyntaxException */
100
101         template <class InputIterator>
102         static MACAddress from_data(InputIterator i);
103                                         ///< Construct address from raw data
104                                         /**< Copies the data from \a i into the MAC address.
105                                              \pre The input range at \a i must have a size of at
106                                                  least 6 elements. */
107
108         static MACAddress from_eui64(senf::EUI64 const & eui);
109                                         ///< Construct address from EUI-64
110                                         /**< This constructor takes an EUI-64 value and converts it
111                                              to a MAC address. This conversion is only possible, if
112                                              the EUI-64 is MAC compatible: the 4th/5th byte (in
113                                              transmission order) must be 0xFFFE.
114                                              \throws AddressSyntaxException if \a eui is not a MAC
115                                                  compatible EUI-64. */
116
117         bool local() const;             ///< \c true, if address is locally administered
118         bool multicast() const;         ///< \c true, if address is a group/multicast address
119         bool broadcast() const;         ///< \c true, if address is the broadcast address
120         bool boolean_test() const;      ///< \c true, if address is not the zero address
121
122         boost::uint32_t oui() const;    ///< Return first 3 bytes of the address
123         boost::uint32_t nic() const;    ///< Return last 3 bytes of the address
124
125         boost::uint64_t eui64() const;  ///< Build EUI-64 from the MAC address
126         boost::uint64_t uint64() const; ///< Return MAC address as uint64 value
127     };
128
129     /** \brief Output MAC instance as it's string representation
130         \related MACAddress
131      */
132     std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
133     /** \brief Try to initialize MACAddress instance from a string representation
134         sets std::ios::failbit on the stream if an error occurred
135         \see MACAddress from_string()
136         \related MACAddress
137      */
138     std::istream & operator>>(std::istream & os, MACAddress & mac);
139
140     bool operator==(MACAddress const & mac, EUI64 const & eui64);
141     bool operator==(EUI64 const & eui64, MACAddress const & mac);
142
143 }
144
145 //-/////////////////////////////////////////////////////////////////////////////////////////////////
146 #include "MACAddress.cci"
147 #include "MACAddress.ct"
148 //#include "MACAddress.cti"
149 #endif
150
151 \f
152 // Local Variables:
153 // mode: c++
154 // fill-column: 100
155 // comment-column: 40
156 // c-file-style: "senf"
157 // indent-tabs-mode: nil
158 // ispell-local-dictionary: "american"
159 // compile-command: "scons -u test"
160 // End: