Socket: Moved PacketSocketHandle and related stuff into 'Raw' subdir
[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     struct MACAddress
45         : public boost::array<boost::uint8_t,6>, 
46           public ComparableSafeBool<MACAddress>
47     {
48         enum NoInit_t { noinit };
49         
50         MACAddress();                   ///< Construct zero-initialized address
51         MACAddress(NoInit_t);           ///< Construct uninitialized (!) address
52
53         static MACAddress from_string(std::string const & s);
54                                         ///< Construct address from string representation
55                                         /**< The string representation must exactly match the form
56                                              <tt>dd:dd:dd:dd:dd:dd</tt> where <tt>d</tt> is any
57                                              hexadecimal digit. In place of ':', '-' is also
58                                              accepted as a delimiter. */
59
60         template <class InputIterator> 
61         static MACAddress from_data(InputIterator i);
62                                         ///< Construct address from raw data
63                                         /**< Copies the data from \a i into the MAC address.
64                                              \pre The input range at \a i must have a size of at
65                                                  least 6 elements. */
66
67         static MACAddress from_eui64(boost::uint64_t v);
68                                         ///< Construct address from EUI-64
69                                         /**< This constructor takes an EUI-64 value and converts it
70                                              to a MAC address. This conversion is only possible, if
71                                              the EUI-64 is MAC compatible: the 4th/5th byte (in
72                                              transmission order) must be 0xFFFE.
73                                              \throws SyntaxException if \a v is not a MAC compatible
74                                                  EUI-64. */
75
76         bool local() const;             ///< \c true, if address is locally administered
77         bool group() const;             ///< \c true, if address is a group/multicast address
78         bool broadcast() const;         ///< \c true, if address is the broadcast address
79         bool boolean_test() const;      ///< \c true, if address is the zero address
80
81         boost::uint32_t oui() const;    ///< Return first 3 bytes of the address
82         boost::uint32_t nic() const;    ///< Return last 3 bytes of the address
83         
84         boost::uint64_t eui64() const;  ///< Build EUI-64 from the MAC address
85
86         /** \brief Bad MAC address syntax or conversion */
87         struct SyntaxException : public std::exception
88         { virtual char const * what() const throw() { return "invalid MAC address syntax"; } };
89     };
90
91     /** \brief Write MAC address
92         \related MACAddress
93      */
94     std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
95
96 }
97
98 ///////////////////////////////hh.e////////////////////////////////////////
99 #include "MACAddress.cci"
100 #include "MACAddress.ct"
101 //#include "MACAddress.cti"
102 #endif
103
104 \f
105 // Local Variables:
106 // mode: c++
107 // fill-column: 100
108 // comment-column: 40
109 // c-file-style: "senf"
110 // indent-tabs-mode: nil
111 // ispell-local-dictionary: "american"
112 // compile-command: "scons -u test"
113 // End: