<--previous | contents | next-->

# Packet class

Each packet is represented as an instance of the Packet class. Conceptually, each Packet instance acts like a stack of protocols, starting with the protocol at the beginning of the packet, and continuing through all the enclosed protocols to the end of the packet. So, for example, an ICMP packet from a PF log might consist of the following stacked protocols:

pf -> Ethernet -> IP -> ICMP

It is the task of the Protocol class to make the protocols in this stack easy to access and manipulate.

## Interface

### Protocol(klass, packet)

klass is a protocol class matching the first protocol in the protocol stack. packet is raw packet data as a Python string.

### __repr__()

Return a human-readable representation of the packet.

### getProtoList()

Retrieves a list of all protocols in the protocol stack.

### getRaw()

Retrieves the raw packet as a string.

### finalise()

Fixes calculated packet elements, including offsets, checksums, etc. This method should be called after packet contents have been modified.

### __getitem__(protocol)

Case-insensitive retrieval of protocols from the protocol stack by name. For example, the following retrieves the first IP protocol class found in the stack:

    # p is a Packet instance
    p["ip"]

Note that the individual protocol instances provide an interface polymorphic to this. Say we had a packet which contained IP-over-IP tunneled traffic. To retrieve the inner IP packet, we would use the following shorthand:

    # p is a Packet instance
    p["ip"]["ip"]

The first example can be read as "retrieve the first IP protocol class contained in the packet". The second example can be read as "retriee the first IP protocol class contained within the first IP protocol class in the packet".

### has_key(protocol)

Case-insensitive name-based check for the existence of a given protocol in the packet.

<--previous | contents | next--> | (12/21/04)

PyOpenbsd v0.1 Manual