1 // $id: EthernetPacket.hh 299 2007-07-10 21:23:49Z g0dil $
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
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.
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.
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.
23 #ifndef HH_EthernetPacket_
24 #define HH_EthernetPacket_ 1
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"
34 //#include "EthernetPacket.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
39 ///\addtogroup protocolbundle_default
43 : boost::array<PacketParserBase::byte,6>
45 MACAddress(std::string addr);
46 template <class InputIterator>
47 MACAddress(InputIterator i);
49 struct SyntaxException : public std::exception
50 { virtual char const * what() const throw() { return "invalid mac address syntax"; } };
53 struct Parse_MAC : public PacketParserBase
55 Parse_MAC(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
57 ///////////////////////////////////////////////////////////////////////////
59 typedef MACAddress value_type;
60 static const size_type fixed_bytes = 6u;
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); }
67 Parse_MAC const & operator= (value_type const & other) { value(other); return *this; }
70 struct Parse_Ethernet : public PacketParserBase
72 SENF_PACKET_PARSER_INIT(Parse_Ethernet);
74 ///////////////////////////////////////////////////////////////////////////
76 typedef Parse_UInt16 Parse_Type;
78 SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
79 ((Field)( destination, Parse_MAC ))
80 ((Field)( source, Parse_MAC ))
81 ((Field)( type, Parse_Type )) );
85 // See http://www.iana.org/assignments/ethernet-numbers
86 typedef boost::uint16_t key_t;
89 struct EthernetPacketType
90 : public PacketTypeBase,
91 public PacketTypeMixin<EthernetPacketType, EtherTypes>
93 typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
94 typedef ConcretePacket<EthernetPacketType> packet;
95 typedef Parse_Ethernet parser;
97 using mixin::nextPacketRange;
98 using mixin::nextPacketType;
99 using mixin::initSize;
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(); }
107 static void dump(packet p, std::ostream & os);
110 typedef EthernetPacketType::packet EthernetPacket;
112 struct Parse_EthVLan : public PacketParserBase
114 SENF_PACKET_PARSER_INIT(Parse_EthVLan);
116 ///////////////////////////////////////////////////////////////////////////
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;
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 )) );
130 struct EthVLanPacketType
131 : public PacketTypeBase,
132 public PacketTypeMixin<EthVLanPacketType, EtherTypes>
134 typedef PacketTypeMixin<EthVLanPacketType, EtherTypes> mixin;
135 typedef ConcretePacket<EthVLanPacketType> packet;
136 typedef Parse_EthVLan parser;
138 using mixin::nextPacketRange;
139 using mixin::nextPacketType;
140 using mixin::initSize;
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(); }
148 static void dump(packet p, std::ostream & os);
151 typedef EthVLanPacketType::packet EthVLanPacket;
157 ///////////////////////////////hh.e////////////////////////////////////////
158 //#include "EthernetPacket.cci"
159 #include "EthernetPacket.ct"
160 //#include "EthernetPacket.cti"
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