As you should already know, every protocol must derive the PDU
class. Therefore, if you wanted to add new protocols, then your class
must do so. Now, what member functions should you implement to make it
work? Here's a dummy PDU you could use as a template:
Registering the new protocol
Okay, we've defined a new PDU, but now we need to register it,
so that the layers below it can recognize it while sniffing and
Registering a protocol is very simple. Let's imagine our DummyPDU
is a network layer protocol. Therefore, we'd like EthernetII,
Dot3 and the rest of the link layer protocols to recognize it.
In order to do so, the following line of code should be used:
You are probably wondering what is that 0x8ae constant used above.
That is the identifier which link layer PDUs will use to identify
our protocol, just like ARP is identified inside EthernetII
using the 0x806 constant.
Now that our PDU is registered, the following will happen:
When sniffing a packet, if the sniffed link layer PDU
finds that the network layer protocol identifier field is 0x8ae,
it will construct a DummyPDU from the sniffed bytes using the
DummyPDU::DummyPDU(const uint8_t*, uint32_t) constructor
When serializing a PDU, our constant will be used in the
network layer's protocol identifier field.
Note that Allocators::register_allocator's first template
parameter can only be one of the following types:
As a final example, let's sniff a packet that contains a DummyPDU.
This is the packet I've generated:
A DummyPDU serialized and inspected using wireshark.
As you can see, the network layer protocol type is 0x8ae. Now, let's sniff the
packet and display the payload!
After executing this example, this is the output I get: