2acb090a7892cdd600ce23bd890d3d9ffba220a2
[senf.git] / senf / Socket / Protocols / Raw / EUI64.hh
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief EUI64 public header */
30
31 #ifndef HH_SENF_Socket_Protocols_Raw_EUI64_
32 #define HH_SENF_Socket_Protocols_Raw_EUI64_ 1
33
34 // Custom includes
35 #include <iostream>
36 #include <boost/cstdint.hpp>
37 #include <boost/array.hpp>
38 #include <senf/Utils/Tags.hh>
39 #include <senf/Utils/safe_bool.hh>
40
41 //#include "EUI64.mpp"
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace senf {
45
46     class MACAddress;
47
48     /** \brief EUI-64 data type
49
50         An EUI-64 is a 64 bit (8 octet) id. The id is represented as an 8 byte sequence in network
51         byte order. An EUI64 can be converted from/to several other representations
52
53         <table class="senf">
54         <tr><td><tt>boost::uint64_t</tt></td>
55                 <td><tt>senf::EUI64(0x1a2b3c4d5f607182ull)</tt><br/>
56                     <i>eui64</i><tt>.uint64()</tt></td></tr>
57         <tr><td><tt>std::string</tt></td>
58                 <td><tt>senf::EUI64::from_string("1a:2b:3c:4d-5f:60:71:82")</tt><br/>
59                     <tt>senf::str(</tt><i>eui64</i><tt>)</tt></td></tr>
60         <tr><td><i>raw data</i><br/>&nbsp;&nbsp;&nbsp;&nbsp;(8 bytes)</td>
61                 <td><tt>senf::EUI64::from_data(</tt><i>iterator</i><tt>)</tt><br/>
62                     <i>eui64</i><tt>.begin()</tt></td></tr>
63         <tr><td>senf::MACAddress<br/>&nbsp;&nbsp;&nbsp;&nbsp;(aka EUI-48)</td>
64                 <td><tt>senf::EUI64::from_mac(</tt><i>mac-address</i><tt>)</tt><br/>
65                     <tt>senf::MACAddress::from_eui64(</tt><i>eui64</i><tt>)</tt></td></tr>
66         </table>
67
68         Since senf::EUI64 is based on \c boost::array, you can access the raw data bytes of the
69         address using \c begin(), \c end() or \c operator[]:
70         \code
71         senf::EUI64 eui64 (...);
72         std::vector<char> data;
73         data.resize(8);
74         std::copy(eui64.begin(), eui64.end(), data.begin()); // Copy 8 bytes
75         \endcode
76
77         \see <a href="http://tools.ietf.org/html/rfc4291">RFC 4291</a>
78
79         \ingroup addr_group
80      */
81     class EUI64
82         : public boost::array<boost::uint8_t,8>,
83           public senf::comparable_safe_bool<EUI64>
84     {
85     public:
86         //-////////////////////////////////////////////////////////////////////////
87         ///\name Structors and default members
88         //\{
89
90         static EUI64 const None;        ///< The empty (0) address
91
92         // default copy constructor
93         // default copy assignment
94         // default destructor
95         // no conversion constructors
96
97         explicit EUI64(boost::uint64_t v=0u); ///< Construct EUI-64
98         explicit EUI64(senf::NoInit_t);       ///< Construct <em>uninitialized</em> EUI-64
99
100         static EUI64 from_mac(MACAddress const & mac);
101                                         ///< Construct EUI-64 from senf::MACAddress
102         static EUI64 from_string(std::string const & s);
103                                         ///< Construct EUI-64 from string representation
104                                         /**< The string representation consists of 8 octets in
105                                              hexadecimal notation separated by ':' or '-'
106                                              \throws senf::AddressSyntaxException */
107         template <class InputIterator>
108         static EUI64 from_data(InputIterator i);
109                                         ///< Construct EUI-64 from 8 data octets
110                                         /**< The iterator \a i must point to a sequence of 8
111                                              octets in network byte order. */
112
113         //\}
114         //-////////////////////////////////////////////////////////////////////////
115
116         bool isMACCompatible() const;   ///< \c true, if EUI64 is MAC compatible, \c false otherwise
117                                         /**< An EUI64 is MAC compatible if bytes 4th and 5th byte
118                                              (in network byte order) are 0xfffe. */
119         bool local() const;             ///< \c true if the \e local bit is set, \c false otherwise
120                                         /**< The \e local bit is the second least significant bit of
121                                              the first octet (bit 6 in standard RFC bit numbering).
122                                           */
123         bool group() const;             ///< \c true if the \e group bit is set, \c false otherwise
124                                         /**< The \e group bit is the least significant bit of the
125                                              first octet (bit 7 in standard RFC bit numbering). */
126         bool boolean_test() const;      ///< \c true, if EUI64 is != 0, \c false otherwise
127         boost::uint64_t uint64() const; ///< Return EUI64 as integer number
128     };
129
130     /** \brief Write out EUI64 in it's string representation to stream
131         \related senf::EUI64
132      */
133     std::ostream & operator<<(std::ostream & os, EUI64 const & v);
134
135     /** \brief Read EUI64 string representation from stream
136         \related senf::EUI64
137      */
138     std::istream & operator>>(std::istream & is, EUI64 & v);
139
140 }
141
142 //-/////////////////////////////////////////////////////////////////////////////////////////////////
143 #include "EUI64.cci"
144 //#include "EUI64.ct"
145 #include "EUI64.cti"
146 #endif
147
148 \f
149 // Local Variables:
150 // mode: c++
151 // fill-column: 100
152 // comment-column: 40
153 // c-file-style: "senf"
154 // indent-tabs-mode: nil
155 // ispell-local-dictionary: "american"
156 // compile-command: "scons -u test"
157 // End: