Move include files in debian packge into 'senf' subdirectory
[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         \ingroup addr_group
51      */
52     struct MACAddress
53         : public boost::array<boost::uint8_t,6>, 
54           public ComparableSafeBool<MACAddress>
55     {
56         static MACAddress const Broadcast; ///< The broadcast address
57         static MACAddress const None;   ///< The empty (0) address
58
59         enum NoInit_t { noinit };
60         
61         MACAddress();                   ///< Construct zero-initialized address
62         MACAddress(NoInit_t);           ///< Construct uninitialized (!) address
63         explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants
64
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. */
71
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
77                                                  least 6 elements. */
78
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
86                                                  EUI-64. */
87
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
92
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
95         
96         boost::uint64_t eui64() const;  ///< Build EUI-64 from the MAC address
97
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"; } };
101     };
102
103     /** \brief Write MAC address
104         \related MACAddress
105      */
106     std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
107
108 }
109
110 ///////////////////////////////hh.e////////////////////////////////////////
111 #include "MACAddress.cci"
112 #include "MACAddress.ct"
113 //#include "MACAddress.cti"
114 #endif
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
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"
125 // End: