1b0eacdbfa67ac029da48c4c9cc15cfcab7ffedc
[senf.git] / senf / Socket / Protocols / Raw / EUI64.hh
1 // $Id$
2 //
3 // Copyright (C) 2009
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 EUI64 public header */
25
26 #ifndef HH_SENF_Socket_Protocols_Raw_EUI64_
27 #define HH_SENF_Socket_Protocols_Raw_EUI64_ 1
28
29 // Custom includes
30 #include <iostream>
31 #include <boost/cstdint.hpp>
32 #include <boost/array.hpp>
33 #include <senf/Utils/Tags.hh>
34 #include <senf/Utils/safe_bool.hh>
35
36 //#include "EUI64.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40
41     class MACAddress;
42
43     /** \brief EUI-64 data type
44
45         An EUI-64 is a 64 bit (8 octet) id. The id is represented as an 8 byte sequence in network
46         byte order. An EUI64 can be converted from/to several other representations
47
48         <table class="senf">
49         <tr><td><tt>boost::uint64_t</tt></td>
50                 <td><tt>senf::EUI64(0x1a2b3c4d5f607182ull)</tt><br/>
51                     <i>eui64</i><tt>.uint64()</tt></td></tr>
52         <tr><td><tt>std::string</tt></td>
53                 <td><tt>senf::EUI64::from_string("1a:2b:3c:4d-5f:60:71:82")</tt><br/>
54                     <tt>senf::str(</tt><i>eui64</i><tt>)</tt></td></tr>
55         <tr><td><i>raw data</i><br/>&nbsp;&nbsp;&nbsp;&nbsp;(8 bytes)</td>
56                 <td><tt>senf::EUI64::from_data(</tt><i>iterator</i><tt>)</tt><br/>
57                     <i>eui64</i><tt>.begin()</tt></td></tr>
58         <tr><td>senf::MACAddress<br/>&nbsp;&nbsp;&nbsp;&nbsp;(aka EUI-48)</td>
59                 <td><tt>senf::EUI64::from_mac(</tt><i>mac-address</i><tt>)</tt><br/>
60                     <tt>senf::MACAddress::from_eui64(</tt><i>eui64</i><tt>)</tt></td></tr>
61         </table>
62
63         Since senf::EUI64 is based on \c boost::array, you can access the raw data bytes of the
64         address using \c begin(), \c end() or \c operator[]:
65         \code
66         senf::EUI64 eui64 (...);
67         std::vector<char> data;
68         data.resize(8);
69         std::copy(eui64.begin(), eui64.end(), data.begin()); // Copy 8 bytes
70         \endcode
71
72         \see <a href="http://tools.ietf.org/html/rfc4291">RFC 4291</a>
73
74         \ingroup addr_group
75      */
76     class EUI64
77         : public boost::array<boost::uint8_t,8>,
78           public senf::comparable_safe_bool<EUI64>
79     {
80     public:
81         ///////////////////////////////////////////////////////////////////////////
82         ///\name Structors and default members
83         ///@{
84
85         static EUI64 const None;        ///< The empty (0) address
86
87         // default copy constructor
88         // default copy assignment
89         // default destructor
90         // no conversion constructors
91
92         explicit EUI64(boost::uint64_t v=0u); ///< Construct EUI-64
93         explicit EUI64(senf::NoInit_t);       ///< Construct <em>uninitialized</em> EUI-64
94
95         static EUI64 from_mac(MACAddress const & mac);
96                                         ///< Construct EUI-64 from senf::MACAddress
97         static EUI64 from_string(std::string const & s);
98                                         ///< Construct EUI-64 from string representation
99                                         /**< The string representation consists of 8 octets in
100                                              hexadecimal notation separated by ':' or '-'
101                                              \throws senf::AddressSyntaxException */
102         template <class InputIterator>
103         static EUI64 from_data(InputIterator i);
104                                         ///< Construct EUI-64 from 8 data octets
105                                         /**< The iterator \a i must point to a sequence of 8
106                                              octets in network byte order. */
107
108         ///@}
109         ///////////////////////////////////////////////////////////////////////////
110
111         bool isMACCompatible() const;   ///< \c true, if EUI64 is MAC compatible, \c false otherwise
112                                         /**< An EUI64 is MAC compatible if bytes 4th and 5th byte
113                                              (in network byte order) are 0xfffe. */
114         bool local() const;             ///< \c true if the \e local bit is set, \c false otherwise
115                                         /**< The \e local bit is the second least significant bit of
116                                              the first octet (bit 6 in standard RFC bit numbering).
117                                           */
118         bool group() const;             ///< \c true if the \e group bit is set, \c false otherwise
119                                         /**< The \e group bit is the least significant bit of the
120                                              first octet (bit 7 in standard RFC bit numbering). */
121         bool boolean_test() const;      ///< \c true, if EUI64 is != 0, \c false otherwise
122         boost::uint64_t uint64() const; ///< Return EUI64 as integer number
123     };
124
125     /** \brief Write out EUI64 in it's string representation to stream
126         \related senf::EUI64
127      */
128     std::ostream & operator<<(std::ostream & os, EUI64 const & v);
129
130     /** \brief Read EUI64 string representation from stream
131         \related senf::EUI64
132      */
133     std::istream & operator>>(std::istream & is, EUI64 & v);
134
135 }
136
137 ///////////////////////////////hh.e////////////////////////////////////////
138 #include "EUI64.cci"
139 //#include "EUI64.ct"
140 #include "EUI64.cti"
141 #endif
142
143 \f
144 // Local Variables:
145 // mode: c++
146 // fill-column: 100
147 // comment-column: 40
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // End: