Socket/Protocols/Raw: Migrate LLSocketAddress to use MACAddress
[senf.git] / Socket / Protocols / Raw / MACAddress.hh
1 // Copyright (C) 2007 
2 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
3 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
4 //     Stefan Bund <g0dil@berlios.de>
5 //
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.
10 //
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.
15 //
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.
20
21 /** \file
22     \brief MACAddress public header */
23
24 #ifndef HH_MACAddress_
25 #define HH_MACAddress_ 1
26
27 // Custom includes
28 #include <iostream>
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"
34
35 //#include "MACAddress.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39
40     /** \brief Ethernet MAC address
41         
42         The Ethernet MAC is modelled as a fixed-size container/sequence of 6 bytes.
43
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.
49      */
50     struct MACAddress
51         : public boost::array<boost::uint8_t,6>, 
52           public ComparableSafeBool<MACAddress>
53     {
54         enum NoInit_t { noinit };
55         
56         MACAddress();                   ///< Construct zero-initialized address
57         MACAddress(NoInit_t);           ///< Construct uninitialized (!) address
58
59         static MACAddress from_string(std::string const & s);
60                                         ///< Construct address from string representation
61                                         /**< The string representation must exactly match the form
62                                              <tt>dd:dd:dd:dd:dd:dd</tt> where <tt>d</tt> is any
63                                              hexadecimal digit. In place of ':', '-' is also
64                                              accepted as a delimiter. */
65
66         template <class InputIterator> 
67         static MACAddress from_data(InputIterator i);
68                                         ///< Construct address from raw data
69                                         /**< Copies the data from \a i into the MAC address.
70                                              \pre The input range at \a i must have a size of at
71                                                  least 6 elements. */
72
73         static MACAddress from_eui64(boost::uint64_t v);
74                                         ///< Construct address from EUI-64
75                                         /**< This constructor takes an EUI-64 value and converts it
76                                              to a MAC address. This conversion is only possible, if
77                                              the EUI-64 is MAC compatible: the 4th/5th byte (in
78                                              transmission order) must be 0xFFFE.
79                                              \throws SyntaxException if \a v is not a MAC compatible
80                                                  EUI-64. */
81
82         bool local() const;             ///< \c true, if address is locally administered
83         bool group() const;             ///< \c true, if address is a group/multicast address
84         bool broadcast() const;         ///< \c true, if address is the broadcast address
85         bool boolean_test() const;      ///< \c true, if address is the zero address
86
87         boost::uint32_t oui() const;    ///< Return first 3 bytes of the address
88         boost::uint32_t nic() const;    ///< Return last 3 bytes of the address
89         
90         boost::uint64_t eui64() const;  ///< Build EUI-64 from the MAC address
91
92         /** \brief Bad MAC address syntax or conversion */
93         struct SyntaxException : public std::exception
94         { virtual char const * what() const throw() { return "invalid MAC address syntax"; } };
95     };
96
97     /** \brief Write MAC address
98         \related MACAddress
99      */
100     std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
101
102 }
103
104 ///////////////////////////////hh.e////////////////////////////////////////
105 #include "MACAddress.cci"
106 #include "MACAddress.ct"
107 //#include "MACAddress.cti"
108 #endif
109
110 \f
111 // Local Variables:
112 // mode: c++
113 // fill-column: 100
114 // comment-column: 40
115 // c-file-style: "senf"
116 // indent-tabs-mode: nil
117 // ispell-local-dictionary: "american"
118 // compile-command: "scons -u test"
119 // End: