git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@129 270642c3-0616-0410...
[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 satcom {
36 namespace pkf {
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         typedef boost::uint16_t key_t;
62     };
63
64     class EthernetPacket
65         : public Packet, 
66           public Parse_Ethernet<Packet::iterator, EthernetPacket>, 
67           public PacketRegistryMixin<EtherTypes,EthernetPacket>
68     {
69         using PacketRegistryMixin<EtherTypes,EthernetPacket>::registerInterpreter;
70     public:
71         ///////////////////////////////////////////////////////////////////////////
72         // Types
73
74         typedef ptr_t<EthernetPacket>::ptr ptr;
75
76         ///////////////////////////////////////////////////////////////////////////
77
78     private:
79         template <class Arg>
80         EthernetPacket(Arg const & arg);
81
82         virtual void v_nextInterpreter() const;
83         virtual void v_finalize();
84         virtual void v_dump(std::ostream & os) const;
85
86         friend class Packet;
87     };
88
89     template <class Iterator=nil, class IPacket=nil>
90     struct Parse_EthVLan : public ParserBase<Iterator,IPacket>
91     {
92         template <class I, class P=nil>
93         struct rebind { typedef Parse_Ethernet<I,P> parser; };
94         typedef Iterator byte_iterator;
95
96         Parse_EthVLan() {}
97         Parse_EthVLan(Iterator const & i) : ParserBase<Iterator,IPacket>(i) {}
98
99         static unsigned bytes() { return 4; }
100         
101         ///////////////////////////////////////////////////////////////////////////
102         
103         typedef Parse_UIntField < 0,  3, Iterator > Parse_Priority;
104         typedef Parse_Flag          < 3, Iterator > Parse_CFI;
105         typedef Parse_UIntField < 4, 16, Iterator > Parse_VLanId;
106         typedef Parse_UInt16           < Iterator > Parse_Type;
107
108         Parse_Priority priority() const { return Parse_Priority(this->i()); }
109         Parse_CFI      cfi()      const { return Parse_CFI(this->i()); }
110         Parse_VLanId   vlanId()   const { return Parse_VLanId(this->i()); }
111         Parse_Type     type()     const { return Parse_Type(this->i()+2); }
112     };
113
114     class EthVLanPacket
115         : public Packet,
116           public Parse_EthVLan<Packet::iterator, EthVLanPacket>,
117           public PacketRegistryMixin<EtherTypes, EthVLanPacket>
118     {
119         using PacketRegistryMixin<EtherTypes, EthVLanPacket>::registerInterpreter;
120     public:
121         ///////////////////////////////////////////////////////////////////////////
122         // Types
123
124         typedef ptr_t<EthVLanPacket>::ptr ptr;
125
126         ///////////////////////////////////////////////////////////////////////////
127
128     private:
129         template <class Arg>
130         EthVLanPacket(Arg const & arg);
131
132         virtual void v_nextInterpreter() const;
133         virtual void v_finalize();
134         virtual void v_dump(std::ostream & os) const;
135
136         friend class Packet;
137     };
138
139 }}
140
141
142 ///////////////////////////////hh.e////////////////////////////////////////
143 //#include "EthernetPacket.cci"
144 //#include "EthernetPacket.ct"
145 #include "EthernetPacket.cti"
146 #endif
147
148 \f
149 // Local Variables:
150 // mode: c++
151 // c-file-style: "satcom"
152 // End: