4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief MACAddress public header */
31 #ifndef HH_SENF_Socket_Protocols_Raw_MACAddress_
32 #define HH_SENF_Socket_Protocols_Raw_MACAddress_ 1
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>
43 //#include "MACAddress.mpp"
44 //-/////////////////////////////////////////////////////////////////////////////////////////////////
50 /** \brief Ethernet MAC address
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
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/> (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>
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[]:
73 senf::MACAddress mac (...);
74 std::vector<char> data;
76 std::copy(mac.begin(), mac.end(), data.begin()); // Copy 6 bytes
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.
88 : public boost::array<boost::uint8_t,6>,
89 public comparable_safe_bool<MACAddress>
91 static MACAddress const Broadcast; ///< The broadcast address
92 static MACAddress const None; ///< The empty (0) address
94 MACAddress(); ///< Construct zero-initialized address
95 MACAddress(senf::NoInit_t); ///< Construct uninitialized (!) address
96 explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants
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 */
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
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. */
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
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
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
134 /** \brief Output MAC instance as it's string representation
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()
143 std::istream & operator>>(std::istream & os, MACAddress & mac);
145 bool operator==(MACAddress const & mac, EUI64 const & eui64);
146 bool operator==(EUI64 const & eui64, MACAddress const & mac);
150 //-/////////////////////////////////////////////////////////////////////////////////////////////////
151 #include "MACAddress.cci"
152 #include "MACAddress.ct"
153 //#include "MACAddress.cti"
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"