bbc884c2eb53eba012ab3e66313b8bffdfbb6713
[senf.git] / Packets / DefaultBundle / 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 /** \file
24     \brief IpV4Packet public header */
25
26 #ifndef HH_IpV4Packet_
27 #define HH_IpV4Packet_ 1
28
29 // Custom includes
30 #include "../../Socket/Protocols/INet/INet4Address.hh"
31 #include "../../Packets/Packets.hh"
32
33 //#include "IpV4Packet.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37
38     /** \brief Parse in IpV4 address
39
40         \see INet4Address
41      */
42     struct Parse_INet4Address : public PacketParserBase
43     {
44         Parse_INet4Address(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {}
45
46         ///////////////////////////////////////////////////////////////////////////
47
48         typedef INet4Address value_type;
49         static const size_type fixed_bytes = 4u;
50
51         value_type value() const { return value_type::from_data(i()); }
52         void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); }
53         operator value_type() { return value(); }
54         byte & operator[](size_type index) { return *boost::next(i(),index); }
55         Parse_INet4Address const & operator= (value_type const & other) 
56             { value(other); return *this; }
57     };
58
59     /** \brief Parse an IpV4 packet
60
61         Parser implementing the IpV4 header. The fields implemented are:
62
63         \see IpV4PacketType \n
64             <a href="http://tools.ietf.org/html/rfc791">RFC 791</a>
65
66         \todo Implement options
67      */
68     struct Parse_IpV4 : public PacketParserBase
69     {
70 #       include SENF_FIXED_PARSER()
71
72         SENF_PARSER_BITFIELD( version,   4, unsigned );
73         SENF_PARSER_BITFIELD( ihl,       4, unsigned );
74
75         SENF_PARSER_FIELD( tos,         Parse_UInt8        );
76         SENF_PARSER_FIELD( length,      Parse_UInt16       );
77         SENF_PARSER_FIELD( identifier,  Parse_UInt16       );
78
79         SENF_PARSER_BITFIELD( reserved,  1, bool     );
80         SENF_PARSER_BITFIELD( df,        1, bool     );
81         SENF_PARSER_BITFIELD( mf,        1, bool     );
82         SENF_PARSER_BITFIELD( frag,     13, unsigned );
83
84         SENF_PARSER_FIELD( ttl,         Parse_UInt8        );
85         SENF_PARSER_FIELD( protocol,    Parse_UInt8        );
86         SENF_PARSER_FIELD( checksum,    Parse_UInt16       );
87         SENF_PARSER_FIELD( source,      Parse_INet4Address );
88         SENF_PARSER_FIELD( destination, Parse_INet4Address );
89
90         SENF_PARSER_INIT() {
91             version() = 4;
92             // We don't support option headers at the moment ...
93             ihl() = 5;
94         }
95
96         SENF_PARSER_FINALIZE(Parse_IpV4);
97         
98         boost::uint16_t calcChecksum() const;
99
100         bool validateChecksum() const {
101             return checksum() == calcChecksum();
102         }
103     };
104
105     /** \brief IP protocol number registry
106
107         This registeres packets with their IP protocol number.
108
109         \see <a href="http://www.iana.org/assignments/protocol-numbers">Protocol numbers</a> \n
110             PacketRegistry
111      */
112     struct IpTypes {
113         typedef boost::uint16_t key_t;
114     };
115
116     /** \brief IpV4 packet
117         
118         \par Packet type (typedef):
119             \ref IpV4Packet
120
121         \par Fields:
122             \ref Parse_IpV4
123
124         \par Associated registries:
125             \ref IpTypes
126
127         \ingroup protocolbundle_default
128      */
129     struct IpV4PacketType
130         : public PacketTypeBase,
131           public PacketTypeMixin<IpV4PacketType, IpTypes>
132     {
133         typedef PacketTypeMixin<IpV4PacketType, IpTypes> mixin;
134         typedef ConcretePacket<IpV4PacketType> packet;
135         typedef Parse_IpV4 parser;
136
137         using mixin::nextPacketRange;
138         using mixin::nextPacketType;
139         using mixin::initSize;
140         using mixin::init;
141
142         static registry_key_t nextPacketKey(packet p) 
143             { return p->protocol(); }
144
145         static void dump(packet p, std::ostream & os);
146         static void finalize(packet p);
147     };
148         
149     /** \brief IpV4 packet typedef */
150     typedef IpV4PacketType::packet IpV4Packet;
151 }
152
153
154 ///////////////////////////////hh.e////////////////////////////////////////
155 #endif
156 #ifndef SENF_PACKETS_DECL_ONLY
157 //#include IpV4Packet.cci"
158 //#include "IpV4Packet.ct"
159 //#include "IpV4Packet.cti"
160 #endif
161
162 \f
163 // Local Variables:
164 // mode: c++
165 // fill-column: 100
166 // c-file-style: "senf"
167 // indent-tabs-mode: nil
168 // ispell-local-dictionary: "american"
169 // compile-command: "scons -u test"
170 // comment-column: 40
171 // End: