Renamed namespaces satcom::lib and satcom::pkf to senf
[senf.git] / Packets / IpV4Packet.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_IpV4Packet_
24 #define HH_IpV4Packet_ 1
25
26 // Custom includes
27 #include "Packet.hh"
28 #include "ParseInt.hh"
29 #include "ParseArray.hh"
30 #include "PacketRegistry.hh"
31
32 //#include "IpV4Packet.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace senf {
36
37     
38     template <class Iterator=nil, class IpV4Packet=nil>
39     struct Parse_IpV4 : public ParserBase<Iterator,IpV4Packet>
40     {
41         template <class I, class P=nil>
42         struct rebind { typedef Parse_IpV4<I,P> parser; };
43         typedef Iterator byte_iterator;
44
45         Parse_IpV4() {}
46         Parse_IpV4(Iterator const & i) : ParserBase<Iterator,IpV4Packet>(i) {}
47
48         static unsigned bytes() { return 20; }
49         
50         ///////////////////////////////////////////////////////////////////////////
51
52         typedef Parse_UIntField <  0,  4, Iterator > Parse_Version;
53         typedef Parse_UIntField <  4,  8, Iterator > Parse_IHL;
54         typedef Parse_UInt8     <         Iterator > Parse_8bit;
55         typedef Parse_UInt16    <         Iterator > Parse_16bit;
56         typedef Parse_Flag      <  0,     Iterator > Parse_R;
57         typedef Parse_Flag      <  1,     Iterator > Parse_DF;
58         typedef Parse_Flag      <  2,     Iterator > Parse_MF;
59         typedef Parse_UIntField <  3, 16, Iterator > Parse_Frag;
60         typedef Parse_UInt32    <         Iterator > Parse_32bit;  
61         
62         Parse_Version  version()     const { return Parse_Version (this->i()      ); }
63         Parse_IHL      ihl()         const { return Parse_IHL     (this->i()      ); }
64         Parse_8bit     tos()         const { return Parse_8bit    (this->i() + 1  ); }
65         Parse_16bit    length()      const { return Parse_16bit   (this->i() + 2  ); }
66         Parse_16bit    identifier()  const { return Parse_16bit   (this->i() + 4  ); }
67         Parse_R        reserved()    const { return Parse_R       (this->i() + 6  ); }
68         Parse_DF       df()          const { return Parse_DF      (this->i() + 6  ); }
69         Parse_MF       mf()          const { return Parse_MF      (this->i() + 6  ); }
70         Parse_Frag     frag()        const { return Parse_Frag    (this->i() + 6  ); }
71         Parse_8bit     ttl()         const { return Parse_8bit    (this->i() + 8  ); }
72         Parse_8bit     protocol()    const { return Parse_8bit    (this->i() + 9  ); } 
73         Parse_16bit    crc()         const { return Parse_16bit   (this->i() + 10 ); }
74         Parse_32bit    source()      const { return Parse_32bit   (this->i() + 12 ); }
75         Parse_32bit    destination() const { return Parse_32bit   (this->i() + 16 ); }
76     };
77
78     struct IpV4Types {
79         typedef boost::uint16_t key_t;
80     };
81
82     class IpV4Packet
83         : public Packet, 
84           public Parse_IpV4<Packet::iterator,IpV4Packet>, 
85           public PacketRegistryMixin<IpV4Types,IpV4Packet>
86     {
87         using PacketRegistryMixin<IpV4Types,IpV4Packet>::registerInterpreter;
88     public:
89         ///////////////////////////////////////////////////////////////////////////
90         // Types
91
92         typedef ptr_t<IpV4Packet>::ptr ptr;
93
94         ///////////////////////////////////////////////////////////////////////////
95
96     private:
97         template <class Arg>
98         IpV4Packet(Arg const & arg);
99
100         virtual void v_nextInterpreter() const;
101         virtual void v_finalize();
102         virtual void v_dump(std::ostream & os) const;
103
104         friend class Packet;
105     };
106 }
107
108
109 ///////////////////////////////hh.e////////////////////////////////////////
110 //#include IpV4Packet.cci"
111 //#include "IpV4Packet.ct"
112 #include "IpV4Packet.cti"
113 #endif
114
115 \f
116 // Local Variables:
117 // mode: c++
118 // c-file-style: "senf"
119 // End: