de52a30736a0224d4ac91a973853640f65709d0d
[senf.git] / Socket / Protocols / Raw / MACAddress.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
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 #include "../../../Utils/Tags.hh"
37 #include "../AddressExceptions.hh"
38
39 //#include "MACAddress.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
41
42 namespace senf {
43
44     /** \brief Ethernet MAC address
45         
46         The Ethernet MAC is modelled as a fixed-size container/sequence of 6 bytes.
47
48         The following statements all create the same MAC address <code>00:1A:2B:3C:4D:5F</code>
49         \code
50         // Used to construct constant MAC addresses
51         MACAddress(0x001A2B3C4D5Flu)
52
53         // Construct a MAC address from it's string representation:
54         MACAddress::from_string("00:1a:2b:3c:4d:5f")   // case is ignored
55         MACAddress::from_string("00-1A-2B-3C-4D-5F")   // '-' as separator is allowed too
56
57         // Construct a MAC address from raw data.  'from_data' takes an arbitrary iterator (e.g. a
58         // pointer) as argument. Here we use a fixed array but normally you will need this to build
59         // a MAC address in a packet parser
60         char rawBytes[] = { 0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5F };
61         MACAddress::from_data(rawBytes)
62
63         // Construct a MAC from the EUID64 as used by INet6 interfaces.  The eui64 will come from an
64         // INet6 address:
65         MACAddress::from_eui64(0x001A2BFFFE3C4D5Ful)
66         MACAddress::from_eui64(
67             INet6Address(0x2001u,0xDB8u,0x1u,0x0u,0x001Au,0x2BFFu,0xFE3Cu,0x3D5Fu).id())
68         \endcode
69
70         Since MACAddress is based on \c boo Ziel, aber nur ein paar davon sind standardkomform.
71
72 Der aktuelle C++ Standard unterstützt drei verschiedene Möglichkeiten eine Zahl in einen String umzuwandeln. Diese Möglichkeiten sind:
73
74     * sprintf
75     * std::strstream
76     * std::stringstream
77         st::array, you can access the raw data bytes of the
78         address using \c begin(), \c end() or \c operator[]:
79         \code
80         MACAddress mac = ...;
81         Packet::iterator i = ...;
82
83         std::copy(mac.begin(), mac.end(), i); // Copies 6 bytes
84         \endcode
85
86         \implementation We awkwardly need to use static named constructors (<tt>from_</tt> members)
87             instead of ordinarily overloaded constructors for one simple reason: <tt>char *</tt>
88             doubles as string literal and as arbitrary data iterator. The iterator constructor can
89             therefore not be distinguished from initialization with a string literal. Therefore we
90             need to disambiguate using the named constructors.
91
92         \ingroup addr_group
93      */
94     struct MACAddress
95         : public boost::array<boost::uint8_t,6>, 
96           public comparable_safe_bool<MACAddress>
97     {
98         static MACAddress const Broadcast; ///< The broadcast address
99         static MACAddress const None;   ///< The empty (0) address
100
101         MACAddress();                   ///< Construct zero-initialized address
102         MACAddress(senf::NoInit_t);     ///< Construct uninitialized (!) address
103         explicit MACAddress(boost::uint64_t v); ///< Construct MACAddress constants
104
105         static MACAddress from_string(std::string const & s);
106                                         ///< Construct address from string representation
107                                         /**< The string representation must exactly match the form
108                                              <tt>dd:dd:dd:dd:dd:dd</tt> where <tt>d</tt> is any
109                                              hexadecimal digit. In place of ':', '-' is also
110                                              accepted as a delimiter.
111                                              \throws AddressSyntaxException */
112
113         template <class InputIterator> 
114         static MACAddress from_data(InputIterator i);
115                                         ///< Construct address from raw data
116                                         /**< Copies the data from \a i into the MAC address.
117                                              \pre The input range at \a i must have a size of at
118                                                  least 6 elements. */
119
120         static MACAddress from_eui64(boost::uint64_t v);
121                                         ///< Construct address from EUI-64
122                                         /**< This constructor takes an EUI-64 value and converts it
123                                              to a MAC address. This conversion is only possible, if
124                                              the EUI-64 is MAC compatible: the 4th/5th byte (in
125                                              transmission order) must be 0xFFFE.
126                                              \throws AddressSyntaxException if \a v is not a MAC
127                                                  compatible EUI-64. */
128
129         bool local() const;             ///< \c true, if address is locally administered
130         bool multicast() const;             ///< \c true, if address is a group/multicast address
131         bool broadcast() const;         ///< \c true, if address is the broadcast address
132         bool boolean_test() const;      ///< \c true, if address is not the zero address
133
134         boost::uint32_t oui() const;    ///< Return first 3 bytes of the address
135         boost::uint32_t nic() const;    ///< Return last 3 bytes of the address
136         
137         boost::uint64_t eui64() const;  ///< Build EUI-64 from the MAC address
138         
139         std::string toString() const; ///< Return string representation of MAC address like 12:34:56:78:90:ab
140
141     };
142
143     /** \brief Write MAC address
144         \related MACAddress
145      */
146     std::ostream & operator<<(std::ostream & os, MACAddress const & mac);
147
148 }
149
150 ///////////////////////////////hh.e////////////////////////////////////////
151 #include "MACAddress.cci"
152 #include "MACAddress.ct"
153 //#include "MACAddress.cti"
154 #endif
155
156 \f
157 // Local Variables:
158 // mode: c++
159 // fill-column: 100
160 // comment-column: 40
161 // c-file-style: "senf"
162 // indent-tabs-mode: nil
163 // ispell-local-dictionary: "american"
164 // compile-command: "scons -u test"
165 // End: