56b1f49607bf8907d23a2506c8cb328b4c93e0f9
[senf.git] / Packets / EthernetPacket.hh
1 // $Id$
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 "Packet.hh"
28 #include "ParseInt.hh"
29 #include "ParseArray.hh"
30 #include "PacketRegistry.hh"
31
32 //#include "EthernetPacket.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace senf {
36
37     
38     template <class Iterator=nil, class IPacket=nil>
39     struct Parse_Ethernet : public ParserBase<Iterator,IPacket>
40     {
41         template <class I, class P=nil>
42         struct rebind { typedef Parse_Ethernet<I,P> parser; };
43         typedef Iterator byte_iterator;
44
45         Parse_Ethernet() {}
46         Parse_Ethernet(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
47
48         static unsigned bytes() { return 14; }
49         
50         ///////////////////////////////////////////////////////////////////////////
51
52         typedef Parse_Array  < 6, Parse_UInt8<>, Iterator > Parse_MAC;
53         typedef Parse_UInt16 < Iterator  >                  Parse_Type;
54         
55         Parse_MAC  destination() const { return Parse_MAC  (this->i() ); }
56         Parse_MAC  source()      const { return Parse_MAC  (this->i() + Parse_MAC::size() ); }
57         Parse_Type type()        const { return Parse_Type (this->i() + 2*Parse_MAC::size() ); }
58     };
59
60     struct EtherTypes {
61         // See http://www.iana.org/assignments/ethernet-numbers
62         typedef boost::uint16_t key_t;
63     };
64
65     class EthernetPacket
66         : public Packet, 
67           public Parse_Ethernet<Packet::iterator, EthernetPacket>, 
68           public PacketRegistryMixin<EtherTypes,EthernetPacket>
69     {
70         using PacketRegistryMixin<EtherTypes,EthernetPacket>::registerInterpreter;
71     public:
72         ///////////////////////////////////////////////////////////////////////////
73         // Types
74
75         typedef ptr_t<EthernetPacket>::ptr ptr;
76
77         ///////////////////////////////////////////////////////////////////////////
78
79     private:
80         template <class Arg>
81         EthernetPacket(Arg const & arg);
82
83         virtual void v_nextInterpreter() const;
84         virtual void v_finalize();
85         virtual void v_dump(std::ostream & os) const;
86
87         friend class Packet;
88     };
89
90     template <class Iterator=nil, class IPacket=nil>
91     struct Parse_EthVLan : public ParserBase<Iterator,IPacket>
92     {
93         template <class I, class P=nil>
94         struct rebind { typedef Parse_Ethernet<I,P> parser; };
95         typedef Iterator byte_iterator;
96
97         Parse_EthVLan() {}
98         Parse_EthVLan(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
99
100         static unsigned bytes() { return 4; }
101         
102         ///////////////////////////////////////////////////////////////////////////
103         
104         typedef Parse_UIntField < 0,  3, Iterator > Parse_Priority;
105         typedef Parse_Flag          < 3, Iterator > Parse_CFI;
106         typedef Parse_UIntField < 4, 16, Iterator > Parse_VLanId;
107         typedef Parse_UInt16           < Iterator > Parse_Type;
108
109         Parse_Priority priority() const { return Parse_Priority(this->i()); }
110         Parse_CFI      cfi()      const { return Parse_CFI(this->i()); }
111         Parse_VLanId   vlanId()   const { return Parse_VLanId(this->i()); }
112         Parse_Type     type()     const { return Parse_Type(this->i()+2); }
113     };
114
115     class EthVLanPacket
116         : public Packet,
117           public Parse_EthVLan<Packet::iterator, EthVLanPacket>,
118           public PacketRegistryMixin<EtherTypes, EthVLanPacket>
119     {
120         using PacketRegistryMixin<EtherTypes, EthVLanPacket>::registerInterpreter;
121     public:
122         ///////////////////////////////////////////////////////////////////////////
123         // Types
124
125         typedef ptr_t<EthVLanPacket>::ptr ptr;
126
127         ///////////////////////////////////////////////////////////////////////////
128
129     private:
130         template <class Arg>
131         EthVLanPacket(Arg const & arg);
132
133         virtual void v_nextInterpreter() const;
134         virtual void v_finalize();
135         virtual void v_dump(std::ostream & os) const;
136
137         friend class Packet;
138     };
139
140 }
141
142
143 ///////////////////////////////hh.e////////////////////////////////////////
144 //#include "EthernetPacket.cci"
145 //#include "EthernetPacket.ct"
146 #include "EthernetPacket.cti"
147 #endif
148
149 \f
150 // Local Variables:
151 // mode: c++
152 // c-file-style: "senf"
153 // End: