1e4b1eddafbf745f5c3e1726175454a8724f0318
[senf.git] / Socket / Protocols / Raw / MACAddress.hh
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief MACAddress public header */
25
26 #ifndef HH_MACAddress_
27 #define HH_MACAddress_ 1
28
29 // Custom includes
30 #include <iostream>
31 #include <boost/cstdint.hpp>
32 #include <boost/array.hpp>
33 #include <boost/utility.hpp>
34 #include <boost/type_traits.hpp>
35 #include "../../../Utils/safe_bool.hh"
36
37 //#include "MACAddress.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41
42     /** \brief Ethernet MAC address
43         
44         The Ethernet MAC is modelled as a fixed-size container/sequence of 6 bytes.
45
46         \implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
47             instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
48             doubles as string literal and as arbitrary data iterator. The iterator constructor can
49             therefore not be distinguished from initialization with a string literal. Therefore we
50             need to disambiguate using the named constructors.
51
52         \ingroup addr_group
53      */
54     struct MACAddress
55         : public boost::array<boost::uint8_t,6>, 
56           public comparable_safe_bool<MACAddress>
57     {
58         static MACAddress const Broadcast; ///< The broadcast address
59         static MACAddress const None;   ///< The empty (0) address
60
61         enum NoInit_t { noinit };
62         
63         MACAddress();                   ///< Construct zero-initialized address
64         MACAddress(NoInit_t);           ///< Construct uninitialized (!) address
65         explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants
66
67         static MACAddress from_string(std::string const & s);
68                                         ///< Construct address from string representation
69                                         /**< The string representation must exactly match the form
70                                              <tt>dd:dd:dd:dd:dd:dd</tt> where <tt>d</tt> is any
71                                              hexadecimal digit. In place of ':', '-' is also
72                                              accepted as a delimiter. */
73
74         template <class InputIterator> 
75         static MACAddress from_data(InputIterator i);
76                                         ///< Construct address from raw data
77                                         /**< Copies the data from \a i into the MAC address.
78                                              \pre The input range at \a i must have a size of at
79                                                  least 6 elements. */
80
81         static MACAddress from_eui64(boost::uint64_t v);
82                                         ///< Construct address from EUI-64
83                                         /**< This constructor takes an EUI-64 value and converts it
84                                              to a MAC address. This conversion is only possible, if
85                                              the EUI-64 is MAC compatible: the 4th/5th byte (in
86                                              transmission order) must be 0xFFFE.
87                                              \throws SyntaxException if \a v is not a MAC compatible
88                                                  EUI-64. */
89
90         bool local() const;             ///< \c true, if address is locally administered
91         bool multicast() const;             ///< \c true, if address is a group/multicast address
92         bool broadcast() const;         ///< \c true, if address is the broadcast address
93         bool boolean_test() const;      ///< \c true, if address is the zero address
94
95         boost::uint32_t oui() const;    ///< Return first 3 bytes of the address
96         boost::uint32_t nic() const;    ///< Return last 3 bytes of the address
97         
98         boost::uint64_t eui64() const;  ///< Build EUI-64 from the MAC address
99
100         /** \brief Bad MAC address syntax or conversion */
101         struct SyntaxException : public std::exception
102         { virtual char const * what() const throw() { return "invalid MAC address syntax"; } };
103     };
104
105     /** \brief Write MAC address
106         \related MACAddress
107      */
108     std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
109
110 }
111
112 ///////////////////////////////hh.e////////////////////////////////////////
113 #include "MACAddress.cci"
114 #include "MACAddress.ct"
115 //#include "MACAddress.cti"
116 #endif
117
118 \f
119 // Local Variables:
120 // mode: c++
121 // fill-column: 100
122 // comment-column: 40
123 // c-file-style: "senf"
124 // indent-tabs-mode: nil
125 // ispell-local-dictionary: "american"
126 // compile-command: "scons -u test"
127 // End: