2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
22 \brief MACAddress public header */
24 #ifndef HH_MACAddress_
25 #define HH_MACAddress_ 1
29 #include <boost/cstdint.hpp>
30 #include <boost/array.hpp>
31 #include <boost/utility.hpp>
32 #include <boost/type_traits.hpp>
33 #include "../../../Utils/safe_bool.hh"
35 //#include "MACAddress.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
40 /** \brief Ethernet MAC address
42 The Ethernet MAC is modelled as a fixed-size container/sequence of 6 bytes.
44 \implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
45 instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
46 doubles as string literal and as arbitrary data iterator. The iterator constructor can
47 therefore not be distinguished from initialization with a string literal. Therefore we
48 need to disambiguate using the named constructors.
53 : public boost::array<boost::uint8_t,6>,
54 public comparable_safe_bool<MACAddress>
56 static MACAddress const Broadcast; ///< The broadcast address
57 static MACAddress const None; ///< The empty (0) address
59 enum NoInit_t { noinit };
61 MACAddress(); ///< Construct zero-initialized address
62 MACAddress(NoInit_t); ///< Construct uninitialized (!) address
63 explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants
65 static MACAddress from_string(std::string const & s);
66 ///< Construct address from string representation
67 /**< The string representation must exactly match the form
68 <tt>dd:dd:dd:dd:dd:dd</tt> where <tt>d</tt> is any
69 hexadecimal digit. In place of ':', '-' is also
70 accepted as a delimiter. */
72 template <class InputIterator>
73 static MACAddress from_data(InputIterator i);
74 ///< Construct address from raw data
75 /**< Copies the data from \a i into the MAC address.
76 \pre The input range at \a i must have a size of at
79 static MACAddress from_eui64(boost::uint64_t v);
80 ///< Construct address from EUI-64
81 /**< This constructor takes an EUI-64 value and converts it
82 to a MAC address. This conversion is only possible, if
83 the EUI-64 is MAC compatible: the 4th/5th byte (in
84 transmission order) must be 0xFFFE.
85 \throws SyntaxException if \a v is not a MAC compatible
88 bool local() const; ///< \c true, if address is locally administered
89 bool multicast() const; ///< \c true, if address is a group/multicast address
90 bool broadcast() const; ///< \c true, if address is the broadcast address
91 bool boolean_test() const; ///< \c true, if address is the zero address
93 boost::uint32_t oui() const; ///< Return first 3 bytes of the address
94 boost::uint32_t nic() const; ///< Return last 3 bytes of the address
96 boost::uint64_t eui64() const; ///< Build EUI-64 from the MAC address
98 /** \brief Bad MAC address syntax or conversion */
99 struct SyntaxException : public std::exception
100 { virtual char const * what() const throw() { return "invalid MAC address syntax"; } };
103 /** \brief Write MAC address
106 std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
110 ///////////////////////////////hh.e////////////////////////////////////////
111 #include "MACAddress.cci"
112 #include "MACAddress.ct"
113 //#include "MACAddress.cti"
120 // comment-column: 40
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"