Whitespce cleanup: Remove whitespace at end-on-line, remove tabs, wrap
[senf.git] / HowTos / NewPacket / Mainpage.dox
index 0547543..4da44fe 100644 (file)
 // Free Software Foundation, Inc.,
 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
-/** \mainpage Defining and using a new 'libPacket' Packet Type 
+/** \mainpage Defining and using a new 'libPacket' Packet Type
 
     This howto will introduce the facilities needed to define a new packet type. As example, the
-    \c GREPacket type is defined. 
+    \c GREPacket type is defined.
 
     \autotoc
 
@@ -50,7 +50,7 @@
     \li It implements packet invariants
 
     To define a new packet type, we need to implement two classes which together provide all this
-    information: 
+    information:
 
     \li a \e parser (a class derived from senf::PacketParserBase). This class defines the data
         fields of the packet header and may also provide additional packet specific functionality.
     SENF_PARSER_BITFIELD  ( version,         3, unsigned );
     SENF_PARSER_BITFIELD  ( protocolType,   16, unsigned );
     \endcode
-    
+
     This is a correct \c GREPacket header definition, but there is room for a small optimization:
     Since the \a protocolType field is exactly 2 bytes wide and is aligned on a byte boundary, we
     can define it as a UInt16 field (instead of a bitfield):
-    
+
     \code
     SENF_PARSER_BITFIELD  ( checksumPresent,  1, bool     );
     SENF_PARSER_SKIP_BITS (                  12           );
     suitable for a single optional field as well as for an optional contiguous sequence of fields.
 
     In our GRE example, there are two fields which need to be enabled/disabled en bloc. We first
-    define an auxiliary sub-parser which combines the two fields.    
+    define an auxiliary sub-parser which combines the two fields.
 
     \code
     struct GREPacketParser_OptFields : public senf::PacketParserBase
     {
     #   include SENF_FIXED_PARSER()
-        
+
         // typedef checksum_t uint16_t; XXX defined automatically???
 
         SENF_PARSER_FIELD ( checksum, senf::UInt16Parser );
     p.protocolType()                       = 0x86dd;
     p.optionalFields().get<1>().checksum() = 12345u;
     \endcode
-    
+
     This code has two problems:
     \li accessing the checksum field is quite unwieldy
     \li changing the checksumPresent() value will break the parser
     the selector (here \a checksumPresent) is changed, since the variant parser must ensure that the
     header data stays consistent. Whenever the checksumPresent field is enabled, the variant parser
     needs to insert additional 4 bytes of data. And it must remove those bytes whenever the
-    checksumPresent field is disabled. 
+    checksumPresent field is disabled.
 
     \subsection howto_newpacket_parser_fixvariant Fixing access by providing custom accessor members
 
     sub-parsers.
 
     In this case however, we still want to allow the user to change the field value, albeit not
-    directly. We will need to go through the collection parser, in this case the variant. 
+    directly. We will need to go through the collection parser, in this case the variant.
 
     The syntax for accessing a variant is quite cumbersome. Therefore we adjust the variant
     definition to generate a more usable interface:
     only access \a checksum() if the \a checksumPresent() field is set. Otherwise, the behavior is
     undefined (in debug builds, the parser will terminate the application with an assert).
 
-    
+
     \subsection howto_newpacket_parser_add Providing additional functionality
 
     We have now implemented parsing all the header fields. However, often packets would benefit from
     \code
     #include <senf/Utils/IpChecksum.hh>
 
-    checksum_t::checksum_t::value_type calculateChecksum() const 
+    checksum_t::checksum_t::value_type calculateChecksum() const
     {
-        if (!checksumEnabled()) 
+        if (!checksumEnabled())
             return 0;
 
         senf::IpChecksum cs;
 
     This code just implements what is defined in the RFC: The checksum covers the complete GRE
     packet including it's header with the checksum field temporarily set to 0. Instead of really
-    changing the checksum field we manually pass the correct data to \a cs. 
+    changing the checksum field we manually pass the correct data to \a cs.
 
     We use the special <tt>i(</tt><i>offset</i><tt>)</tt> helper to get iterators \a offset number
     of bytes into the data. This helper has the additional benefit of range-checking the returned
 
     \code
     #include <senf/Packets.hh>
-    
+
     struct GREPacketParser_OptFields : public senf::PacketParserBase
     {
     #   include SENF_FIXED_PARSER()
     // In the implementation (.cc) file:
 
     #include <senf/Utils/IpChecksum.hh>
-    
+
     GREPacketParser::checksum_t::value_type GREPacketParser::calculateChecksum() const
     {
-        if (!checksumEnabled()) 
+        if (!checksumEnabled())
             return 0;
 
         validate(6);
         typedef senf::PacketTypeMixin<GREPacketType, EtherTypes> mixin;
         typedef senf::ConcretePacket<GREPacketType> packet;
         typedef senf::GREPacketParser parser;
-    
+
         using mixin::nextPacketRange;
         using mixin::nextPacketType;
         using mixin::init;
 
     The next block of statements imports all the default implementations provided by the mixin
     class:
-    
+
     \li \a nextPacketRange provides information about where the next packet lives within the GRE
         packet.
     \li \a nextPacketType provides the type of the next packet from information in the GRE packet.
         GREPacketParser::init.
     \li \a initSize is called to find the size of an empty (newly create) GRE packet. This is also
         provided by GREPacketParser.
-    
+
     With these default implementations provided by the mixin, only a few additional members are
     needed to complete the \c GREPacketType: \a nextPacketKey, \a finalize, and \a dump.
 
     \endcode
 
     But wait -- what is \c GREPacket ? This question is answered a few section further down.
-    
+
     The last thing we need to do is, we need to set the \a protocolType() field to the correct value
     when packets are newly created or changed. This is done within \a finalize:
 
     have to be updated to be correct. This is the responsibility of the \a finalize() member.
 
     \code
-    static void finalize(packet p) 
+    static void finalize(packet p)
     {
         p->protocolType() << key(p.next(senf::nothrow));
         if (p->checksumPresent())
     For diagnostic purposes, every packet should provide a meaningful \a dump() member which writes
     out the complete packet. This member is simple to implement and is often very helpful when
     tracking down problems.
-    
+
     \code
     #include <boost/io/ios_state.hpp>
 
                                                      << p->protocolType() << "\n";
         if (p->checksumPresent())
             os << "  checksum                     : 0x" << std::hex << std::setw(4)
-                                                        << std::setfill('0') << p->checksum() << "\n"; 
+                                                        << std::setfill('0') << p->checksum() << "\n";
     }
     \endcode
-    
+
     This member is quite straight forward. We should try to adhere to the formating standard shown
     above: The first line should be the type of packet/header being dumped followed by one line for
     each protocol field. The colon's should be aligned at column 33 with the field name indented by
-    2 spaces. 
+    2 spaces.
 
     The \c boost::ios_all_saver is just used to ensure, that the stream formatting state is restored
     correctly at the end of the method. An instance of this type will save the stream state when
         typedef senf::PacketTypeMixin<GREPacketType, EtherTypes> mixin;
         typedef senf::ConcretePacket<GREPacketType> packet;
         typedef senf::GREPacketParser parser;
-    
+
         using mixin::nextPacketRange;
         using mixin::nextPacketType;
         using mixin::init;
         using mixin::initSize;
 
         static key_t nextPacketKey(packet p) { return p->protocolType(); }
-    
+
         static void finalize(packet p) {
-            p->protocolType() << key(p.next(senf::nothrow)); 
+            p->protocolType() << key(p.next(senf::nothrow));
             if (p->checksumPresent()) p->checksum() << p->calculateChecksum();
        }
-    
+
         static void dump(packet p, std::ostream & os);
     };
 
     typedef GREPacketType::packet GREPacket;
-    
+
     // In the implementation (.cc) file:
 
     #include <senf/Packets/DefaultBundle/EthernetPacket.hh>
                                                      << p->protocolType() << "\n";
         if (p->checksumPresent())
             os << "  checksum                     : 0x" << std::hex << std::setw(4)
-                                                        << std::setfill('0') << p->checksum() << "\n"; 
+                                                        << std::setfill('0') << p->checksum() << "\n";
     }
     \endcode
 
     needs to obey to be considered valid. If the packet is not valid it cannot be parsed and should
     be dropped. We can't drop it here but if the packet is invalid, we certainly must refrain from
     trying to parser any payload since we cannot assume the packet to have the format we assume our
-    GRE packet to have. 
+    GRE packet to have.
 
     There are two conditions defined in the RFC which render a GRE packet invalid: If one of the \a
     reserved0() fields first 5 bits is set or if the version is not 0. We will add a \a valid()
 
     factory_t nextPacketType(packet p) { return p->valid() ? lookup(p->protocolType()) : no_factory(); }
     \endcode
-    
+
     As we see, this is still quite simple. \c factory_t is provided by senf::PacketTypeBase. For our
     purpose it is an opaque type which somehow enables the packet library to create a new packet of
     a specified packet type. The \c factory_t has a special value, \c no_factory() which stands for
     senf::EtherTypes registry ? Here one way to do this:
 
     \code
-    factory_t nextPacketType(packet p) { 
+    factory_t nextPacketType(packet p) {
         if (p->valid()) {
             if (p->protocolType() == 0x6558)  return senf::EthernetPacket::factory();
             else                              return lookup(p->protocolType());
     #define HH_GREPacket_
 
     #include <senf/Packets.hh>
-    
+
     struct GREPacketParser_OptFields : public senf::PacketParserBase
     {
     #   include SENF_FIXED_PARSER()
         typedef senf::PacketTypeMixin<GREPacketType, EtherTypes> mixin;
         typedef senf::ConcretePacket<GREPacketType> packet;
         typedef senf::GREPacketParser parser;
-    
+
         using mixin::nextPacketRange;
         using mixin::init;
         using mixin::initSize;
 
-        factory_t nextPacketType(packet p) 
+        factory_t nextPacketType(packet p)
             { return p->valid() ? lookup(p->protocolType()) : no_factory(); }
-    
+
         static void finalize(packet p) {
             p->protocolType() << key(p.next(senf::nothrow));
             if (p->checksumPresent()) p->checksum() << p->calculateChecksum();
         }
-    
+
         static void dump(packet p, std::ostream & os);
     };
 
     typedef GREPacketType::packet GREPacket;
-    
+
     #endif
     \endcode
 
 
     GREPacketParser::checksum_t::checksum_t::value_type GREPacketParser::calculateChecksum() const
     {
-        if (!checksumEnabled()) 
+        if (!checksumEnabled())
             return 0;
 
         validate(6);
                                                      << p->protocolType() << "\n";
         if (p->checksumPresent())
             os << "  checksum                     : 0x" << std::hex << std::setw(4)
-                                                        << std::setfill('0') << p->checksum() << "\n"; 
+                                                        << std::setfill('0') << p->checksum() << "\n";
     }
     \endcode
 
 
         senf::TapSocketHandle tap ("tap0");
         senf::ConnectedRawV6ClientSocketHandle osock (47u, senf::INet6SocketAddress(argv[1]));
-    
+
         while (true) {
             senf::EthernetPacket eth (senf::EthernetPacket::create(senf::noinit));
             isock.read(eth.data(),0u);
     }
     \endcode
 
-    
+
     \section howto_newpacket_further Further reading
 
     Lets start with references to the important API's (Use the <i>List of all members</i> link to
     When implementing new packet's, the following information will be helpful:
 
     <table class="senf fixedcolumn">
-    
+
     <tr><td>senf::PacketTypeBase</td> <td>here you find a description of the members which need to
     be implemented to provide a 'packet type'. Most of these members will normally be provided by
     the mixin helper.</td></tr>
     use it.</td></tr>
 
     <tr><td>\ref packetparser</td> <td>This section describes the packet parser facility.</td></tr>
-    
+
     <tr><td>\link packetparsermacros Packet parser macros\endlink</td> <td>A complete list and
     documentation of all the packet parser macros.</td></tr>
-    
+
     <tr><td>\ref parseint, \n \ref parsecollection</td> <td>There are several lists of available
     reusable packet parsers. However, these lists are not complete as there are other protocol
     specific reusable parsers (without claiming to be exhaustive: senf::INet4AddressParser,