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 also:
NewPacket HowTo

1. 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 -> 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 Helper macros for defining new packet parsers for a detailed description of the macro language which is used to define composite parsers.

See also:
Helper macros for defining new packet parsers

2. 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:
  • It provides the type of the protocol parser to use
  • It provides information on how the next protocol can be found and where the payload resides in this packet
  • 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 also:
senf::PacketTypeMixin
senf::PacketTypeBase
senf::PacketRegistry