4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Packet public header */
30 #include <boost/operators.hpp>
32 #include "../Utils/Exception.hh"
33 #include "../Utils/safe_bool.hh"
34 #include "PacketInterpreter.hh"
36 //#include "Packet.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
41 /** \defgroup packet_module Packet Handling
43 The basic groundwork of the Packet library is the packet handling:
45 \li The packet classes provide access to a chain of packet headers (more generically called
47 \li They automatically manage the required memory resources and the shared packet data.
49 \section packet_module_chain The Interpreter Chain
51 The central data structure for a packet is the interpreter chain
53 \image html structure.png The Interpreter Chain
55 This image depicts a packet with several headers. Each interpreter is responsible for a
56 specific sub-range of the complete packet. This range always \e includes the packets payload
57 (This is, why we call the data structure interpreter and not header: The interpreter is
58 responsible for interpreting a range of the packet according to a specific protocol), the
59 packet interpreters are nested inside each other.
61 For each interpreter, this structure automatically divides the packet into three areas (each
62 of which are optional): The header, the payload and the trailer. Every packet will have
63 either a header or a payload section while most don't have a trailer.
65 As user of the library you always interact with the chain through one (or more) of the
66 interpreters. The interpreter provides methods to traverse to the following or preceding
67 header (interpreter) and provides two levels of access to the packet data: Generic low-level
68 access in the form of an STL compatible sequence and access to the parsed fields which are
69 provided by the parser associated with the concrete packet type.
71 \section packet_module_management Resource Management
73 The interface to the packet library is provided using a handle class (\ref Packet for
74 generic, protocol agnostic access and \ref ConcretePacket derived from \ref Packet to access
75 a specific protocol). This handle automatically manages the resources associated with the
76 packet (the interpreter chain and the data storage holding the packet data). The resources
77 are automatically released when the last packet handle referencing a specific packet is
80 \implementation The packet chain is provided on two levels: The internal representation \ref
81 PacketInterpreterBase and \ref PacketInterpreter which are referenced by the Handle
82 classes \ref Packet and \ref ConcretePacket. \n
83 The internal representation classes are pertinent in the sense, that they exist
84 regardless of the existence of a handle referencing them (as long as the packet
85 exists). Still the interpreter chain is lazy and packet interpreters beside the first
86 are only created dynamically when accessed (this is implemented in the handle not in the
87 internal representation). \n
88 The packet interpreters make use of a pool allocator. This provides extremely efficient
89 creation and destruction of packet interpreter's and removes the dynamic memory
90 management overhead from the packet interpreter management. The packet implementation
91 class (\ref PacketImpl which holds the packet data itself) however is still dynamically
92 managed (however there is only a single instance for each packet).
95 template <class PackeType> class ConcretePacket;
97 ///\addtogroup packet_module
100 /** \brief Main Packet class
102 Packet is the main externally visible class of the packet library. Packet is a handle into
103 the internal packet representation. From Packet you may access the data of that specific
104 sub-packet/header/interpreter and navigate to the neighboring
105 sub-packets/headers/interpreters.
107 Packet is protocol agnostic. This class only provides non-protocol dependent members. To
108 access the protocol specific features of a packet (like header fields) the ConcretePacket
109 class extending Packet is provided.
111 \section packet_semantics Semantics
113 All operations accessing the data of \c this packet in some way will ignore any preceding
114 packets/headers/interpreters in the chain. It does not matter, whether a given packet is
115 taken from the middle or the beginning of the chain, all operations (except those explicitly
116 accessing the chain of course) should work the same.
118 This especially includes members like clone() or append(): clone() will clone \e only from
119 \c this packet until the end of the chain, append() will append the given packet \e ignoring
120 any possibly preceding packets/headers/interpreters.
122 In the same way, the data() member provides an STL-sequence compatible view of the packet
123 data. This only includes the data which is part of \c this packet including header, trailer
124 \e and payload but \e not the headers or trailers of packets \e before \c this packet in the
125 packet/header/interpreter chain (nonetheless, this data overlaps with the data of other
128 Several members are member templates taking an \a OtherPacket template parameter. This
129 parameter must be the ConcretePacket instantiation associated with some concrete packet type
130 (protocol). For each implemented protocol, typedefs should be provided for these
131 instantiations (Example: \ref EthernetPacket is a typedef for
132 \ref ConcretePacket < \ref EthernetPacketType >).
135 \ref ConcretePacket for the type specific interface\n
136 \ref PacketData for the sequence interface\n
137 \ref packetparser for a specification of the parser interface
140 : public safe_bool<Packet>,
141 public boost::equality_comparable<Packet>
144 ///////////////////////////////////////////////////////////////////////////
147 typedef void type; ///< Type of the packet.
148 typedef senf::detail::packet::size_type size_type;
149 ///< Unsigned type to represent packet size
150 typedef PacketInterpreterBase::factory_t factory_t; ///< Packet factory type (see below)
152 enum NoInit_t { noinit }; ///< Special argument flag
153 /**< Used in some ConcretePacket constructors */
155 ///////////////////////////////////////////////////////////////////////////
156 ///\name Structors and default members
159 // default copy constructor
160 // default copy assignment
161 // default destructor
163 Packet(); ///< Create uninitialized packet handle
164 /**< An uninitialized handle is in - valid(). It does not
165 allow any operation except assignment and checking for
167 Packet clone() const; ///< Create copy packet
168 /**< clone() will create a complete copy of \c this
169 packet. The returned packet will have the same data and
170 packet chain. It does however not share any data with
171 the original packet. */
173 // conversion constructors
175 template <class PacketType>
176 Packet(ConcretePacket<PacketType> packet); ///< Copy-construct Packet from ConcretePacket
177 /**< This constructor allows to convert an arbitrary
178 ConcretePacket into a general Packet, loosing the
179 protocol specific interface. */
182 ///////////////////////////////////////////////////////////////////////////
184 ///\name Interpreter chain access
188 ///< Get next packet in chain
189 /**< \returns in - valid() packet, if no next packet
191 template <class OtherPacket> OtherPacket next() const;
192 ///< Get next packet in chain and cast to \a OtherPacket
193 /**< \throws std::bad_cast if the next() packet is not of
195 \returns in - valid() packet, if no next packet
197 template <class OtherPacket> OtherPacket find() const;
198 ///< Search chain forward for packet of type \a OtherPacket
199 /**< The search will start with the current packet.
200 \returns in - valid() packet, if no packet of type \a
201 OtherPacket can be found. */
204 ///< Get previous packet in chain
205 /**< \returns in - valid() packet, if no previous packet
207 template <class OtherPacket> OtherPacket prev() const;
208 ///< Get previous packet in chain and cast to \a OtherPacket
209 /**< \throws std::bad_cast, if the previous packet is not of
211 \returns in - valid() packet, if no previous packet
213 template <class OtherPacket> OtherPacket rfind() const;
214 ///< Search chain backwards for packet of type \a OtherPacket
215 /**< The search will start with the current packet.
216 \returns in - valid() packet, if no packet of type \a
217 OtherPacket can be found. */
220 Packet first() const;
221 ///< Return first packet in chain
222 template <class OtherPacket> OtherPacket first() const;
223 ///< Return first packet in chain and cast
224 /**< \throws std::bad_cast if the first() packet is not of
225 type \a OtherPacket */
228 ///< Return last packet in chain
229 template <class OtherPacket> OtherPacket last() const;
230 ///< Return last packet in chain and cast
231 /**< \throws std::bad_cast if the last() packet is not of
232 type \a OtherPacket */
235 template <class OtherPacket> OtherPacket parseNextAs() const;
236 ///< Interpret payload of \c this as \a OtherPacket
237 /**< parseNextAs() will throw away the packet chain after
238 the current packet if necessary. It will then parse the
239 payload section of \c this packet as given by \a
240 OtherPacket. The new packet is added to the chain after
242 \returns new packet instance sharing the same data and
243 placed after \c this packet in the chain. */
244 Packet parseNextAs(factory_t factory) const;
245 ///< Interpret payload of \c this as \a factory type packet
246 /**< parseNextAs() will throw away the packet chain after
247 the current packet if necessary. It will then parse the
248 payload section of \c this packet as given by \a
249 factory. The new packet is added to the chain after
251 \returns new packet instance sharing the same data and
252 placed after \c this packet in the chain. */
254 template <class OtherPacket> bool is() const;
255 ///< Check, whether \c this packet is of the given type
256 template <class OtherPacket> OtherPacket as() const;
257 ///< Cast current packet to the given type
258 /**< This operations returns a handle to the same packet
259 header/interpreter however cast to the given
261 \throws std::bad_cast if the current packet is not of
262 type \a OtherPacket */
264 Packet append(Packet packet) const; ///< Append the given packet to \c this packet
265 /**< This operation will replace the payload section of \c
266 this packet with \a packet. This operation will replace
267 the packet chain after \c this packet with a clone of
268 \a packet and will replace the raw data of the payload
269 of \c this with the raw data of \a packet. \c this
270 packet will not share any date with \a packet.
271 \returns Packet handle to the cloned \a packet, placed
272 after \c this in the packet/header/interpreter
280 PacketData & data() const; ///< Access the packets raw data container
281 size_type size() const; ///< Return size of packet in bytes
282 /**< This size does \e not include the size of any preceding
283 headers/packets/interpreters. It does however include
284 \c this packets payload. */
288 ///\name Other methods
291 bool operator==(Packet other) const; ///< Check for packet identity
292 /**< Two packet handles compare equal if they really are the
293 same packet header in the same packet chain. */
294 bool boolean_test() const; ///< Check, whether the packet is valid()
296 bool valid() const; ///< Check, whether the packet is valid()
297 /**< An in - valid() packet does not allow any operation
298 except checking for validity and assignment. in -
299 valid() packets serve the same role as 0-pointers.
301 This is an alias for boolean_test() which is called
302 when using a packet in a boolean context. */
304 void finalize() const; ///< Update calculated fields
305 /**< This call will update all calculated fields of the
306 packet after it has been created or changed. This
307 includes checksums, payload size fields or other
308 fields, which can be set from other information in the
309 packet. Each concrete packet type should document,
310 which fields are set by finalize().
312 finalize() will automatically process all
313 packets/headers/interpreters from the end of the chain
314 backwards up to \c this. */
316 void dump(std::ostream & os) const; ///< Write out a printable packet representation
317 /**< This method is provided mostly to help debugging packet
318 problems. Each concrete packet should implement a dump
319 method writing out all fields of the packet in a
320 readable representation. dump() will call this member
321 for each packet/header/interpreter in the chain from \c
322 this packet up to the end of the chain. */
324 TypeIdValue typeId() const; ///< Get id of \c this packet
325 /**< This value is used e.g. in the packet registry to
326 associate packet types with other information.
327 \returns A type holding the same information as a
328 type_info object, albeit assignable */
329 factory_t factory() const; ///< Return factory instance of \c this packet
330 /**< The returned factory instance can be used to create new
331 packets of the given type without knowing the concrete
332 type of the packet. The value may be stored away for
333 later use if needed. */
338 explicit Packet(PacketInterpreterBase::ptr packet);
340 PacketInterpreterBase::ptr ptr() const;
343 Packet checkNext() const;
344 Packet checkLast() const;
346 PacketInterpreterBase::ptr packet_;
348 template <class PacketType>
349 friend class ConcretePacket;
350 friend class PacketParserBase;
353 /** \brief Protocol specific packet handle
355 The ConcretePacket template class extends Packet to provide protocol/packet type specific
356 aspects. These are packet constructors and access to the parsed packet fields.
358 The \c PacketType template argument to ConcretePacket is a protocol specific and internal
359 policy class which defines the protocol specific behavior. To access a specific type of
360 packet, the library provides corresponding typedefs of ConcretePacket < \a SomePacketType >
361 (e.g. \ref EthernetPacket as typedef for \ref ConcretePacket < \ref EthernetPacketType >).
363 The new members provided by ConcretePacket over packet are mostly comprised of the packet
364 constructors. These come in three major flavors:
366 \li The create() family of constructors will create completely new packets.
367 \li The createAfter() family of constructors will create new packets (with new data for the
368 packet) \e after a given existing packet.
369 \li The createBefore() family of constructors will create new packets (again with new data)
370 \e before a given existing packet.
372 Whereas create() will create a completely new packet with it's own chain and data storage,
373 createAfter() and createBefore() extend a packet with additional
374 headers/interpreters. createAfter() will set the payload of the given packet to the new
375 packet whereas createBefore() will create a new packet with the existing packet as it's
378 createAfter() differs from Packet::parseNextAs() in that the former creates a new packet \e
379 replacing any possibly existing data whereas the latter will interpret the already \e
380 existing data as given by the type argument.
382 \see \ref PacketTypeBase for a specification of the interface to be provided by the \a
383 PacketType policy class.
385 template <class PacketType>
390 ///////////////////////////////////////////////////////////////////////////
393 typedef PacketType type;
394 typedef typename PacketType::parser Parser;
396 ///////////////////////////////////////////////////////////////////////////
397 ///\name Structors and default members
400 // default copy constructor
401 // default copy assignment
402 // default destructor
403 // no conversion constructors
405 ConcretePacket(); ///< Create uninitialized packet handle
406 /**< An uninitialized handle is not valid(). It does not
407 allow any operation except assignment and checking for
410 static factory_t factory(); ///< Return factory for packets of specific type
411 /**< This \e static member is like Packet::factory() for a
412 specific packet of type \a PacketType */
414 // Create completely new packet
416 static ConcretePacket create(); ///< Create default initialized packet
417 /**< The packet will be initialized to it's default empty
419 static ConcretePacket create(NoInit_t); ///< Create uninitialized empty packet
420 /**< This will create a completely empty and uninitialized
421 packet with <tt>size() == 0</tt>.
422 \param[in] noinit This parameter must always have the
423 value \c senf::noinit. */
424 static ConcretePacket create(size_type size); ///< Create default initialized packet
425 /**< This member will create a default initialized packet
426 with the given size. If the size parameter is smaller
427 than the minimum allowed packet size an exception will
429 \param[in] size Size of the packet to create in bytes.
430 \throws TruncatedPacketException if \a size is smaller
431 than the smallest permissible size for this type of
433 static ConcretePacket create(size_type size, NoInit_t); ///< Create uninitialized packet
434 /**< Creates an uninitialized (all-zero) packet of the exact
436 \param[in] size Size of the packet to create in bytes
437 \param[in] noinit This parameter must always have the
438 value \c senf::noinit. */
439 template <class ForwardReadableRange>
440 static ConcretePacket create(ForwardReadableRange const & range);
441 ///< Create packet from given data
442 /**< The packet will be created from a copy of the given
443 data. The data from the range will be copied directly
444 into the packet representation. The data will \e not be
445 validated in any way.
447 href="http://www.boost.org/libs/range/index.html">Boost.Range</a>
448 of data to construct packet from. */
450 // Create packet as new packet after a given packet
452 static ConcretePacket createAfter(Packet packet);
453 ///< Create default initialized packet after \a packet
454 /**< The packet will be initialized to it's default empty
455 state. It will be appended as next header/interpreter
456 after \a packet in that packets interpreter chain.
457 \param[in] packet Packet to append new packet to. */
458 static ConcretePacket createAfter(Packet packet, NoInit_t);
459 ///< Create uninitialized empty packet after\a packet
460 /**< This will create a completely empty and uninitialized
461 packet with <tt>size() == 0</tt>. It will be appended
462 as next header/interpreter after \a packet in that
463 packets interpreter chain.
464 \param[in] packet Packet to append new packet to.
465 \param[in] noinit This parameter must always have the
466 value \c senf::noinit. */
467 static ConcretePacket createAfter(Packet packet, size_type size);
468 ///< Create default initialized packet after \a packet
469 /**< This member will create a default initialized packet
470 with the given size. If the size parameter is smaller
471 than the minimum allowed packet size an exception will
472 be thrown. It will be appended as next
473 header/interpreter after \a packet in that packets
475 \param[in] packet Packet to append new packet to.
476 \param[in] size Size of the packet to create in bytes.
477 \throws TruncatedPacketException if \a size is smaller
478 than the smallest permissible size for this type of
480 static ConcretePacket createAfter(Packet packet, size_type size, NoInit_t);
481 ///< Create uninitialized packet after \a packet
482 /**< Creates an uninitialized (all-zero) packet of the exact
483 given size. It will be appended as next
484 header/interpreter after \a packet in that packets
486 \param[in] packet Packet to append new packet to.
487 \param[in] size Size of the packet to create in bytes
488 \param[in] noinit This parameter must always have the
489 value \c senf::noinit. */
490 template <class ForwardReadableRange>
491 static ConcretePacket createAfter(Packet packet,
492 ForwardReadableRange const & range);
493 ///< Create packet from given data after \a packet
494 /**< The packet will be created from a copy of the given
495 data. The data from the range will be copied directly
496 into the packet representation. The data will \e not be
497 validated in any way. It will be appended as next
498 header/interpreter after \a packet in that packets
500 \param[in] packet Packet to append new packet to.
502 href="http://www.boost.org/libs/range/index.html">Boost.Range</a>
503 of data to construct packet from. */
505 // Create packet as new packet (header) before a given packet
507 static ConcretePacket createBefore(Packet packet);
508 ///< Create default initialized packet before \a packet
509 /**< The packet will be initialized to it's default empty
510 state. It will be prepended as previous
511 header/interpreter before \a packet in that packets
513 \param[in] packet Packet to prepend new packet to. */
514 static ConcretePacket createBefore(Packet packet, NoInit_t);
515 ///< Create uninitialized empty packet before \a packet
516 /**< Creates a completely empty and uninitialized packet. It
517 will be prepended as previous header/interpreter before
518 \a packet in that packets interpreter chain.
519 \param[in] packet Packet to prepend new packet to. */
521 // Create a clone of the current packet
523 ConcretePacket clone() const;
526 ///////////////////////////////////////////////////////////////////////////
530 Parser * operator->() const; ///< Access packet fields
531 /**< This operator allows to access the parsed fields of the
532 packet using the notation <tt>packet->field()</tt>. The
533 fields of the packet are specified by the PacketType's
536 The members are not strictly restricted to simple field
537 access. The parser class may have any member which is
538 needed for full packet access (e.g. checksum validation
540 \see \ref packetparser for the parser interface. */
545 typedef PacketInterpreter<PacketType> interpreter;
547 ConcretePacket(typename interpreter::ptr packet_);
549 typename interpreter::ptr ptr() const;
552 friend class PacketInterpreter<PacketType>;
559 ///////////////////////////////hh.e////////////////////////////////////////
561 #if !defined(HH_Packets__decls_) && !defined(HH_Packet_i_)
563 #include "Packet.cci"
565 #include "Packet.cti"
572 // c-file-style: "senf"
573 // indent-tabs-mode: nil
574 // ispell-local-dictionary: "american"
575 // compile-command: "scons -u test"
576 // comment-column: 40