Packets: Fix VariantParser invalid parser access bug
[senf.git] / Packets / DefaultBundle / IPv6Packet.hh
1 // $Id: EthernetPacket.hh 299 2007-07-10 21:23:49Z g0dil $
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 IPv6Packet public header */
25
26 #ifndef HH_SENF_Packets_DefaultBundle_IPv6Packet_
27 #define HH_SENF_Packets_DefaultBundle_IPv6Packet_ 1
28
29 // Custom includes
30 #include "../../Socket/Protocols/INet/INet6Address.hh"
31 #include "../../Packets/Packets.hh"
32 #include "IPv4Packet.hh"
33
34 //#include "IPv6Packet.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     /** \brief Parse an IPv6 address
40         
41         \see INet6Address
42      */
43     struct INet6AddressParser : public PacketParserBase
44     {
45         INet6AddressParser(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
46
47         ///////////////////////////////////////////////////////////////////////////
48
49         typedef INet6Address value_type;
50         static const size_type fixed_bytes = 16u;
51
52         value_type value() const { return value_type::from_data(i()); }
53         void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
54         operator value_type() const { return value(); }
55         byte & operator[](size_type index) const { return *boost::next(i(),index); }
56         INet6AddressParser const & operator= (value_type const & other) 
57             { value(other); return *this; }
58     };
59
60     /** \brief Parse an IPv6 packet
61
62         \see IPv6PacketType \n
63             <a href="http://tools.ietf.org/html/rfc2460">RFC 2460</a>
64      */
65     struct IPv6PacketParser : public PacketParserBase
66     {
67 #       include SENF_FIXED_PARSER()
68
69         SENF_PARSER_BITFIELD( version,       4, unsigned );
70         SENF_PARSER_BITFIELD( trafficClass,  8, unsigned );
71         SENF_PARSER_BITFIELD( flowLabel,    20, unsigned );
72
73         SENF_PARSER_FIELD( length,       UInt16Parser       );
74         SENF_PARSER_FIELD( nextHeader,   UInt8Parser        );
75         SENF_PARSER_FIELD( hopLimit,     UInt8Parser        );
76         SENF_PARSER_FIELD( source,       INet6AddressParser );
77         SENF_PARSER_FIELD( destination,  INet6AddressParser );
78
79         SENF_PARSER_INIT() {
80             version() = 6;
81         }
82
83         SENF_PARSER_FINALIZE(IPv6PacketParser);
84     };
85
86     /** \brief IPv6 packet
87
88         \par Packet type (typedef):
89             \ref IPv6Packet
90         
91         \par Fields:
92             \ref IPv6PacketParser
93
94         \par Associated registries:
95             \ref IpTypes
96
97         \par Finalize action:
98             Set \a length from payload size\n
99             Set \a nextHeader from type of next packet if found in \ref IpTypes
100
101         \ingroup protocolbundle_default
102      */
103     struct IPv6PacketType
104         : public PacketTypeBase,
105           public PacketTypeMixin<IPv6PacketType, IpTypes>
106     {
107 #ifndef DOXYGEN
108         typedef PacketTypeMixin<IPv6PacketType, IpTypes> mixin;
109         typedef ConcretePacket<IPv6PacketType> packet;
110         typedef IPv6PacketParser parser;
111 #endif
112         using mixin::nextPacketRange;
113         using mixin::nextPacketType;
114         using mixin::initSize;
115         using mixin::init;
116
117         static key_t nextPacketKey(packet p) 
118             { return p->nextHeader(); }
119         
120         static void dump(packet p, std::ostream & os);
121
122         static void finalize(packet p);
123     };
124
125     /** \brief IPv6 packet typedef */
126     typedef ConcretePacket<IPv6PacketType> IPv6Packet;
127
128     ///@}
129 }
130
131 ///////////////////////////////hh.e////////////////////////////////////////
132 #endif
133 #ifndef SENF_PACKETS_DECL_ONLY
134 //#include "IPv6Packet.cci"
135 //#include "IPv6Packet.ct"
136 //#include "IPv6Packet.cti"
137 #endif
138
139 \f
140 // Local Variables:
141 // mode: c++
142 // fill-column: 100
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"
147 // comment-column: 40
148 // End: