Packets: BUGFIX: ensure complete interpreter chain in finalize()
[senf.git] / Packets / DefaultBundle / EthernetPacket.hh
1 // $id: EthernetPacket.hh 299 2007-07-10 21:23:49Z g0dil $
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 #ifndef HH_EthernetPacket_
24 #define HH_EthernetPacket_ 1
25
26 // Custom includes
27 #include <algorithm>
28 #include <boost/array.hpp>
29 #include "Packets/PacketType.hh"
30 #include "Packets/ParseInt.hh"
31 #include "Packets/PacketRegistry.hh"
32 #include "Packets/PacketParser.hh"
33
34 //#include "EthernetPacket.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38
39     ///\addtogroup protocolbundle_default
40     ///@{
41
42     struct MACAddress
43         : boost::array<PacketParserBase::byte,6>
44     {
45         MACAddress(std::string addr);
46         template <class InputIterator>
47         MACAddress(InputIterator i);
48
49         struct SyntaxException : public std::exception
50         { virtual char const * what() const throw() { return "invalid mac address syntax"; } };
51     };
52
53     struct Parse_MAC : public PacketParserBase
54     {
55         Parse_MAC(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
56        
57         ///////////////////////////////////////////////////////////////////////////
58
59         typedef MACAddress value_type;
60         static const size_type fixed_bytes = 6u;
61
62         value_type value() const { return MACAddress(i()); }
63         void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
64         operator value_type () { return value(); }
65         byte & operator[](size_type index) { return *boost::next(i(),index);  }
66
67         Parse_MAC const & operator= (value_type const & other) { value(other); return *this; }
68     };
69
70     struct Parse_Ethernet : public PacketParserBase
71     {
72         SENF_PACKET_PARSER_INIT(Parse_Ethernet);
73
74         ///////////////////////////////////////////////////////////////////////////
75
76         typedef Parse_UInt16                      Parse_Type;
77
78         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
79             ((Field)( destination, Parse_MAC  ))
80             ((Field)( source,      Parse_MAC  ))
81             ((Field)( type,        Parse_Type )) );
82     };
83
84     struct EtherTypes {
85         // See http://www.iana.org/assignments/ethernet-numbers
86         typedef boost::uint16_t key_t;
87     };
88
89     struct EthernetPacketType
90         : public PacketTypeBase,
91           public PacketTypeMixin<EthernetPacketType, EtherTypes>
92     {
93         typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
94         typedef ConcretePacket<EthernetPacketType> packet;
95         typedef Parse_Ethernet parser;
96
97         using mixin::nextPacketRange;
98         using mixin::nextPacketType;
99         using mixin::initSize;
100         using mixin::init;
101
102         /** \todo Add LLC/SNAP support -> only use the registry
103             for type() values >=1536, otherwise expect an LLC header */
104         static registry_key_t nextPacketKey(packet p) 
105             { return p->type(); }
106
107         static void dump(packet p, std::ostream & os);
108     };
109
110     typedef EthernetPacketType::packet EthernetPacket;
111
112     struct Parse_EthVLan : public PacketParserBase
113     {
114         SENF_PACKET_PARSER_INIT(Parse_EthVLan);
115
116         ///////////////////////////////////////////////////////////////////////////
117
118         typedef Parse_UIntField < 0,  3 > Parse_Priority;
119         typedef Parse_Flag          < 3 > Parse_CFI;
120         typedef Parse_UIntField < 4, 16 > Parse_VLanId;
121         typedef Parse_UInt16              Parse_Type;
122
123         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
124             ((OverlayField)( priority, Parse_Priority ))
125             ((OverlayField)( cfi,      Parse_CFI      ))
126             ((Field       )( vlanId,   Parse_VLanId   ))
127             ((Field       )( type,     Parse_Type     )) );
128     };
129
130     struct EthVLanPacketType
131         : public PacketTypeBase, 
132           public PacketTypeMixin<EthVLanPacketType, EtherTypes>
133     {
134         typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
135         typedef ConcretePacket<EthVLanPacketType> packet;
136         typedef Parse_EthVLan parser;
137
138         using mixin::nextPacketRange;
139         using mixin::nextPacketType;
140         using mixin::initSize;
141         using mixin::init;
142
143         /** \todo Add LLC/SNAP support -> only use the registry
144             for type() values >=1536, otherwise expect an LLC header */
145         static registry_key_t nextPacketKey(packet p) 
146             { return p->type(); }
147
148         static void dump(packet p, std::ostream & os);
149     };
150
151     typedef EthVLanPacketType::packet EthVLanPacket;
152
153     ///@}
154 }
155
156
157 ///////////////////////////////hh.e////////////////////////////////////////
158 //#include "EthernetPacket.cci"
159 #include "EthernetPacket.ct"
160 //#include "EthernetPacket.cti"
161 #endif
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
167 // c-file-style: "senf"
168 // indent-tabs-mode: nil
169 // ispell-local-dictionary: "american"
170 // compile-command: "scons -u test"
171 // comment-column: 40
172 // End: