Defining new Packet types

Each packet is specified by the following two components:

  • A protocol parser which defines the protocol specific fields
  • A packet type class which is a policy class defining the packet

\see <a href="../../../../HowTos/NewPacket/doc/html/index.html">NewPacket HowTo</a>

The protocol parser

The protocol parser is simply a composite parser. It defines all the protocol
fields. Additionally, the protocol parser may have additional members which will then be
accessible via the \c -> operator of the packet. Possibilities here are e.g. checksum
calculation and validation, packet validation as a whole and so on.

Defining a protocol parser is quite simple:
struct EthernetPacketParser : public PacketParserBase
{
# include SENF_FIXED_PARSER()
SENF_PARSER_FIELD( destination, MACAddressParser );
SENF_PARSER_FIELD( source, MACAddressParser );
SENF_PARSER_FIELD( type_length, UInt16Parser );
SENF_PARSER_FINALIZE(EthernetPacketParser);
};
There are a lot of other possibilities to define fields. See \ref packetparsermacros for a
detailed description of the macro language which is used to define composite parsers.

\see
    \ref packetparsermacros

The packet type policy class

This is a class which provides all the information needed to integrate the new packet type into
the packet library:

\li It provides the type of the protocol parser to use
\li It provides information on how the next protocol can be found and where the payload resides
    in this packet
\li It provides methods to initialize a new packet and get information about the packet size

All this information is provided via static or typedef members.
struct EthernetPacketType
: public PacketTypeBase,
public PacketTypeMixin<EthernetPacketType, EtherTypes>
{
typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
typedef ConcretePacket<EthernetPacketType> packet;
typedef EthernetPacketParser parser;
using mixin::nextPacketRange;
using mixin::initSize;
using mixin::init;
static factory_t nextPacketType(packet p);
static void dump(packet p, std::ostream & os);
static void finalize(packet p);
};
typedef EthernetPacketType::packet EthernetPacket;
The definition of senf::EthernetPacket is quite straight forward. This template works for most
simple packet types.

\see \ref senf::PacketTypeMixin \n
    \ref senf::PacketTypeBase \n
    \ref senf::PacketRegistry