switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / DefaultBundle / UDPPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief UDPPacket public header */
30
31 #ifndef HH_SENF_Packets_DefaultBundle_UDPPacket_
32 #define HH_SENF_Packets_DefaultBundle_UDPPacket_ 1
33
34 // Custom includes
35 #include <senf/Packets/Packets.hh>
36
37 //#include "UDPPacket.mpp"
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 namespace senf {
41
42     /** \brief Parse a UDP packet
43
44         Parser implementing the UDP header. The fields implemented are:
45         \image html UDPPacket.png
46
47         \see UDPPacketType \n
48             <a href="http://tools.ietf.org/html/rfc768">RFC 768</a>
49      */
50     struct UDPPacketParser : public PacketParserBase
51     {
52 #       include SENF_FIXED_PARSER()
53
54         SENF_PARSER_FIELD( source,      senf::UInt16Parser );
55         SENF_PARSER_FIELD( destination, senf::UInt16Parser );
56         SENF_PARSER_FIELD( length,      senf::UInt16Parser );
57         SENF_PARSER_FIELD( checksum,    senf::UInt16Parser );
58
59         SENF_PARSER_FINALIZE(UDPPacketParser);
60
61         boost::uint16_t calcChecksum() const; ///< calculate (pseudo-)header checksum
62                                               /**< calculate and return the checksum of the
63                                                    (pseudo-)header \see \ref senf::IpChecksum */
64         bool validateChecksum() const {
65             return checksum() == 0u || checksum() == calcChecksum();
66         }                               ///< validate header checksum
67                                         /**< return \c true if the \ref checksum() "checksum"
68                                              field is equal to the \ref calcChecksum() "calculated checksum" */
69     };
70
71     /** \brief UDP packet
72
73         \par Packet type (typedef):
74             \ref UDPPacket
75
76         \par Fields:
77             \ref UDPPacketParser
78
79         <table class="packet" cellpadding="5" cellspacing="1" border="1">
80           <tr>
81             <th width="25%">0</th> <th width="25%">8</th> <th width="25%">16</th>
82             <th width="15%">24</th> <th style="text-align:right" width="10%">31</th>
83           </tr><tr>
84             <td colspan="2">\ref UDPPacketParser::source() "Source Port"</td>
85             <td colspan="3">\ref UDPPacketParser::destination() "Destination Port"</td>
86           </tr><tr>
87             <td colspan="2">\ref UDPPacketParser::length() "Length"</td>
88             <td colspan="3">\ref UDPPacketParser::checksum() "Checksum"</td>
89           </tr>
90         </table>
91
92         \par Finalize action:
93             \copydetails finalize()
94
95         \ingroup protocolbundle_default
96      */
97     struct UDPPacketType
98         : public PacketTypeBase,
99           public PacketTypeMixin<UDPPacketType>
100     {
101 #ifndef DOXYGEN
102         typedef PacketTypeMixin<UDPPacketType> mixin;
103 #endif
104         typedef ConcretePacket<UDPPacketType> packet; ///< UDP packet typedef
105         typedef UDPPacketParser parser;               ///< typedef to the parser of UDP packet
106
107         using mixin::nextPacketRange;
108         using mixin::initSize;
109         using mixin::init;
110
111         /** \brief Dump given UDPPacket in readable form to given output stream */
112         static void dump(packet p, std::ostream & os);
113
114         static void finalize(packet p); ///< Finalize packet.
115                                         /**< \li set \ref UDPPacketParser::length() "length" from
116                                                payload size
117                                              \li calculate and set \ref UDPPacketParser::checksum()
118                                                "checksum" */
119     };
120
121     /** \brief UDP packet typedef
122         \ingroup protocolbundle_default
123      */
124     typedef ConcretePacket<UDPPacketType> UDPPacket;
125 }
126
127
128 //-/////////////////////////////////////////////////////////////////////////////////////////////////
129 //#include "UDPPacket.cci"
130 //#include "UDPPacket.ct"
131 //#include "UDPPacket.cti"
132 #endif
133
134 \f
135 // Local Variables:
136 // mode: c++
137 // fill-column: 100
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"
142 // comment-column: 40
143 // End: