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/SafeBool.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.
51 : public boost::array<boost::uint8_t,6>,
52 public ComparableSafeBool<MACAddress>
54 static MACAddress const Broadcast; ///< The broadcast address
55 static MACAddress const None; ///< The empty (0) address
57 enum NoInit_t { noinit };
59 MACAddress(); ///< Construct zero-initialized address
60 MACAddress(NoInit_t); ///< Construct uninitialized (!) address
61 explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants
63 static MACAddress from_string(std::string const & s);
64 ///< Construct address from string representation
65 /**< The string representation must exactly match the form
66 <tt>dd:dd:dd:dd:dd:dd</tt> where <tt>d</tt> is any
67 hexadecimal digit. In place of ':', '-' is also
68 accepted as a delimiter. */
70 template <class InputIterator>
71 static MACAddress from_data(InputIterator i);
72 ///< Construct address from raw data
73 /**< Copies the data from \a i into the MAC address.
74 \pre The input range at \a i must have a size of at
77 static MACAddress from_eui64(boost::uint64_t v);
78 ///< Construct address from EUI-64
79 /**< This constructor takes an EUI-64 value and converts it
80 to a MAC address. This conversion is only possible, if
81 the EUI-64 is MAC compatible: the 4th/5th byte (in
82 transmission order) must be 0xFFFE.
83 \throws SyntaxException if \a v is not a MAC compatible
86 bool local() const; ///< \c true, if address is locally administered
87 bool multicast() const; ///< \c true, if address is a group/multicast address
88 bool broadcast() const; ///< \c true, if address is the broadcast address
89 bool boolean_test() const; ///< \c true, if address is the zero address
91 boost::uint32_t oui() const; ///< Return first 3 bytes of the address
92 boost::uint32_t nic() const; ///< Return last 3 bytes of the address
94 boost::uint64_t eui64() const; ///< Build EUI-64 from the MAC address
96 /** \brief Bad MAC address syntax or conversion */
97 struct SyntaxException : public std::exception
98 { virtual char const * what() const throw() { return "invalid MAC address syntax"; } };
101 /** \brief Write MAC address
104 std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
108 ///////////////////////////////hh.e////////////////////////////////////////
109 #include "MACAddress.cci"
110 #include "MACAddress.ct"
111 //#include "MACAddress.cti"
118 // comment-column: 40
119 // c-file-style: "senf"
120 // indent-tabs-mode: nil
121 // ispell-local-dictionary: "american"
122 // compile-command: "scons -u test"