libtins provides support for several network protocols. The
documentation contains information
about each class, method and function present in the library. However,
you might not want to read the whole documentation just to learn how to
craft a simple TCP
packet.
In this section, we'll have a look at how some of the protocols you will most likely see in your home network are implemented in the library.
The Ethernet II protocol is represented by the EthernetII
class,
and is actually very simple. It just contains methods to get and set the
destination and source addresses, and the payload type. It also contains
a constructor which lets you, optionally, indicate both of the addresses:
The IP
class contains much more methods, both for accessing the
protocol's fields and for adding and retrieving the stored options.
In this example, we'll modify some of those fields:
IP is one of many protocols that support TLV (type-length-value) encoded options. This means that each option stored in the protocol contains a field that indicates its type, another one that holds its length, and a third one that carries the actual data.
Every class that contains options, such as IP
, TCP
and
DHCP
, among others, behave the same way. For any type T
that contains options, T::option
is the type in which the option
is actually stored. Each of this protocols provides two member functions:
Using these member functions, it is necessary to know both the length
of the fields that compose each option type and their endianness. Since
this is very cumbersome, each of these protocols provides a getter and
a setter for every valid option. The getter will always throw a
option_not_found
exception if the option is not present in the
PDU
.
In the case of IP
, these are some of the supported options
getters/setters:
TCP
contains several option types as well, so everything mentioned
above about them is still valid. Let's have a look at how to craft
some basic TCP
frames: