switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Socket / Protocols / Raw / MACAddress.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief MACAddress public header */
30
31 #ifndef HH_SENF_Socket_Protocols_Raw_MACAddress_
32 #define HH_SENF_Socket_Protocols_Raw_MACAddress_ 1
33
34 // Custom includes
35 #include <iostream>
36 #include <boost/cstdint.hpp>
37 #include <boost/array.hpp>
38 #include <boost/utility.hpp>
39 #include <boost/type_traits.hpp>
40 #include <senf/Utils/safe_bool.hh>
41 #include <senf/Utils/Tags.hh>
42
43 //#include "MACAddress.mpp"
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45
46 namespace senf {
47
48     class EUI64;
49
50     /** \brief Ethernet MAC address
51
52         The Ethernet MAC is modeled as a fixed-size container/sequence of 6 bytes. A MACAddress can
53         be converted from/to the following representations
54
55         <table class="senf">
56         <tr><td><tt>boost::uint64_t</tt></td>
57                 <td><tt>senf::MACAddress(0x112233445566ull)</tt><br/>
58                     <i>mac</i><tt>.uint64()</tt></td></tr>
59         <tr><td><tt>std::string</tt></td>
60                 <td><tt>senf::MACAddress::from_string("11:22:33:44:55:66")</tt><br/>
61                     <tt>senf::str(</tt><i>mac</i><tt>)</tt></td></tr>
62         <tr><td><i>raw data</i><br/>&nbsp;&nbsp;&nbsp;&nbsp;(6 bytes)</td>
63                 <td><tt>senf::MACAddress::from_data(</tt><i>iterator</i><tt>)</tt><br/>
64                     <i>mac</i><tt>.begin()</tt></td></tr>
65         <tr><td>senf::EUI64</td>
66                 <td><tt>senf::MACAddress::from_eui64(</tt><i>eui64</i><tt>)</tt><br/>
67                     <tt>senf::EUI64::from_mac(</tt><i>mac</i><tt>)</tt></td></tr>
68         </table>
69
70         Since MACAddress is based on \c boost::array, you can access the raw data bytes of the
71         address using \c begin(), \c end() or \c operator[]:
72         \code
73         senf::MACAddress mac (...);
74         std::vector<char> data;
75         data.resize(6);
76         std::copy(mac.begin(), mac.end(), data.begin()); // Copy 6 bytes
77         \endcode
78
79         \implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
80             instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
81             doubles as string literal and as arbitrary data iterator. The iterator constructor can
82             therefore not be distinguished from initialization with a string literal. Therefore we
83             need to disambiguate using the named constructors.
84
85         \ingroup addr_group
86      */
87     struct MACAddress
88         : public boost::array<boost::uint8_t,6>,
89           public comparable_safe_bool<MACAddress>
90     {
91         static MACAddress const Broadcast; ///< The broadcast address
92         static MACAddress const None;   ///< The empty (0) address
93
94         MACAddress();                   ///< Construct zero-initialized address
95         MACAddress(senf::NoInit_t);     ///< Construct uninitialized (!) address
96         explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants
97
98         static MACAddress from_string(std::string const & s);
99                                         ///< Construct address from string representation
100                                         /**< The string representation must exactly match the form
101                                              <tt>dd:dd:dd:dd:dd:dd</tt> where <tt>d</tt> is any
102                                              hexadecimal digit. In place of ':', '-' is also
103                                              accepted as a delimiter.
104                                              \throws AddressSyntaxException */
105
106         template <class InputIterator>
107         static MACAddress from_data(InputIterator i);
108                                         ///< Construct address from raw data
109                                         /**< Copies the data from \a i into the MAC address.
110                                              \pre The input range at \a i must have a size of at
111                                                  least 6 elements. */
112
113         static MACAddress from_eui64(senf::EUI64 const & eui);
114                                         ///< Construct address from EUI-64
115                                         /**< This constructor takes an EUI-64 value and converts it
116                                              to a MAC address. This conversion is only possible, if
117                                              the EUI-64 is MAC compatible: the 4th/5th byte (in
118                                              transmission order) must be 0xFFFE.
119                                              \throws AddressSyntaxException if \a eui is not a MAC
120                                                  compatible EUI-64. */
121
122         bool local() const;             ///< \c true, if address is locally administered
123         bool multicast() const;         ///< \c true, if address is a group/multicast address
124         bool broadcast() const;         ///< \c true, if address is the broadcast address
125         bool boolean_test() const;      ///< \c true, if address is not the zero address
126
127         boost::uint32_t oui() const;    ///< Return first 3 bytes of the address
128         boost::uint32_t nic() const;    ///< Return last 3 bytes of the address
129
130         boost::uint64_t eui64() const;  ///< Build EUI-64 from the MAC address
131         boost::uint64_t uint64() const; ///< Return MAC address as uint64 value
132     };
133
134     /** \brief Output MAC instance as it's string representation
135         \related MACAddress
136      */
137     std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
138     /** \brief Try to initialize MACAddress instance from a string representation
139         sets std::ios::failbit on the stream if an error occurred
140         \see MACAddress from_string()
141         \related MACAddress
142      */
143     std::istream & operator>>(std::istream & os, MACAddress & mac);
144
145     bool operator==(MACAddress const & mac, EUI64 const & eui64);
146     bool operator==(EUI64 const & eui64, MACAddress const & mac);
147
148 }
149
150 //-/////////////////////////////////////////////////////////////////////////////////////////////////
151 #include "MACAddress.cci"
152 #include "MACAddress.ct"
153 //#include "MACAddress.cti"
154 #endif
155
156 \f
157 // Local Variables:
158 // mode: c++
159 // fill-column: 100
160 // comment-column: 40
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -u test"
165 // End: