Packet Construction and Protocol Testing...

gnn at freebsd.org gnn at freebsd.org
Fri Jul 21 02:47:28 UTC 2006


At Thu, 20 Jul 2006 10:48:14 -0400 (EDT),
Andrew R. Reiter wrote:
> 
> 
> Aren't there already tools for doing this -- libnet / libdnet that both 
> have py wrappers?

I looked at all those, and more, but they miss an important point.
That is, in PCS you define a packet like this (from
pcs/packets/ipv4.py):

    def __init__(self, bytes = None):
        """ define the fields of an IPv4 packet, from RFC 791
        This version does not include options."""
        version = pcs.Field("version", 4, default = 4)
        hlen = pcs.Field("hlen", 4)
        tos = pcs.Field("tos", 8)
        length = pcs.Field("length", 16)
        id = pcs.Field("id", 16)
        flags = pcs.Field("flags", 3)
        offset = pcs.Field("offset", 13)
        ttl = pcs.Field("ttl", 8, default = 64)
        protocol = pcs.Field("protocol", 8)
        checksum = pcs.Field("checksum", 16)
        src = pcs.Field("src", 32)
        dst = pcs.Field("dst", 32)
        pcs.Packet.__init__(self,
                            [version, hlen, tos, length, id, flags, offset,
                             ttl, protocol, checksum, src, dst],
                            bytes = bytes)
        # Description MUST be set after the PCS layer init
        self.description = "IPv4"

which creates a properties in the object to hold the named field.
This is what makes it possible to do:

ip = ipv4()
ip.ttl = 64
ip.src = inet_pton("128.32.1.1")

etc. in your program.  Also note that the bit lengths can be odd, such
as getting the 13 bit offset field.  So, PCS is doing all the packing
and unpacking of the bytes for you.  I intend to put in automatic
bounds checking in an upcoming version.

There is much more about this in the documentation, docs/pcs.pdf in
the package.

Future versions will allow import/export to various formats as well,
such as XML, so that defining packets will be even easier as will
writing tools.

Later,
George


More information about the freebsd-net mailing list