8b1aa70f996a2a5e6ff357917838c2baa025f4a4
[senf.git] / Packets / DefaultBundle / UDPPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.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 UDPPacket public header */
25
26 #ifndef HH_SENF_Packets_DefaultBundle_UDPPacket_
27 #define HH_SENF_Packets_DefaultBundle_UDPPacket_ 1
28
29 // Custom includes
30 #include "../../Packets/Packets.hh"
31
32 //#include "UDPPacket.mpp"
33 ///////////////////////////////hh.p////////////////////////////////////////
34
35 namespace senf {
36
37     /** \brief Parse a UDP packet
38
39         Parser implementing the UDP header. The fields implemented are:
40
41         \see UDPPacketType \n
42             <a href="http://tools.ietf.org/html/rfc768">RFC 768</a>
43      */
44     struct UDPPacketParser : public PacketParserBase
45     {
46 #       include SENF_FIXED_PARSER()
47
48         SENF_PARSER_FIELD( source,      senf::UInt16Parser );
49         SENF_PARSER_FIELD( destination, senf::UInt16Parser );
50         SENF_PARSER_FIELD( length,      senf::UInt16Parser );
51         SENF_PARSER_FIELD( checksum,    senf::UInt16Parser );
52
53         SENF_PARSER_FINALIZE(UDPPacketParser);
54
55         boost::uint16_t calcChecksum() const;
56
57         bool validateChecksum() const {
58             return checksum() == 0u || checksum() == calcChecksum();
59         }
60     };
61
62     /** \brief UDP packet
63         
64         \par Packet type (typedef):
65             \ref UDPPacket
66
67         \par Fields:
68             \ref UDPPacketParser
69
70         \par Finalize action:
71             Set \a length from payload size\n
72             Calculate \a checksum
73
74         \ingroup protocolbundle_default
75      */
76     struct UDPPacketType
77         : public PacketTypeBase,
78           public PacketTypeMixin<UDPPacketType>
79     {
80 #ifndef DOXYGEN
81         typedef PacketTypeMixin<UDPPacketType> mixin;
82         typedef ConcretePacket<UDPPacketType> packet;
83         typedef UDPPacketParser parser;
84 #endif
85         using mixin::nextPacketRange;
86         using mixin::initSize;
87         using mixin::init;
88
89         static void dump(packet p, std::ostream & os);
90
91         static void finalize(packet p);
92     };
93
94     /** \brief UDP packet typedef */
95     typedef ConcretePacket<UDPPacketType> UDPPacket;
96 }
97
98
99 ///////////////////////////////hh.e////////////////////////////////////////
100 #endif
101 #ifndef SENF_PACKETS_DECL_ONLY
102 //#include UDPPacket.cci"
103 //#include "UDPPacket.ct"
104 //#include "UDPPacket.cti"
105 #endif
106
107 \f
108 // Local Variables:
109 // mode: c++
110 // fill-column: 100
111 // c-file-style: "senf"
112 // indent-tabs-mode: nil
113 // ispell-local-dictionary: "american"
114 // compile-command: "scons -u test"
115 // comment-column: 40
116 // End: