b801426d726022ff8593839e234a18e24ba17d76
[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 IpTypes {
79         // See http://www.iana.org/assignments/protocol-numbers
80         // Also used by IPv6
81         typedef boost::uint16_t key_t;
82     };
83
84     class IpV4Packet
85         : public Packet, 
86           public Parse_IpV4<Packet::iterator,IpV4Packet>, 
87           public PacketRegistryMixin<IpTypes,IpV4Packet>
88     {
89         using PacketRegistryMixin<IpTypes,IpV4Packet>::registerInterpreter;
90     public:
91         ///////////////////////////////////////////////////////////////////////////
92         // Types
93
94         typedef ptr_t<IpV4Packet>::ptr ptr;
95
96         ///////////////////////////////////////////////////////////////////////////
97
98     private:
99         template <class Arg>
100         IpV4Packet(Arg const & arg);
101
102         virtual void v_nextInterpreter() const;
103         virtual void v_finalize();
104         virtual void v_dump(std::ostream & os) const;
105
106         friend class Packet;
107     };
108 }
109
110
111 ///////////////////////////////hh.e////////////////////////////////////////
112 //#include IpV4Packet.cci"
113 //#include "IpV4Packet.ct"
114 #include "IpV4Packet.cti"
115 #endif
116
117 \f
118 // Local Variables:
119 // mode: c++
120 // c-file-style: "senf"
121 // End: