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