<--previous | contents | next-->

# packet

NB: The packet module documentation is not yet complete. At the moment the authoritative docmentation is the source. You might also want to look at the unit tests, and the example programs included with PyOpenBSD.

The packet module is a pure-Python library for creating, inspecting and modifying network packets. Since a complete set of documentation for packet would duplicate much of "TCP/IP Illustrated", this manual will only discuss useful top-level functions and modules, and give a general introduction to working with actual protocol classes. We suggest that documentation for the individual protocol classes be accessed using the "pydoc" tool distributed with Python (e.g. try "pydoc openbsd.packet.TCP").

To ease the common task of interpreting packet data obtained via pcap, the pcap module can automatically instantiate the appropriate packet module protocol classes when packet data is passed back to the user (see the pcap documentation for more information). Most examples in the packet module documentation use the pcap module to read packet information.

## Supported Protocols

The packet module currently supports the following protocols:

- AH
- ARP
- ESP
- enc
- Ethernet
- ICMP IPv6 (See numerous subclasses for specific types.)
- ICMP IPv4 (See numerous subclasses for specific types.)
- IGMP
- IP
- IPv6
- Loopback
- pf
- pf for OpenBSD 3.3 and older
- TCP
- UDP

## Example

The example below does the following:

- Reads a single interpreted IP packet from a pcap dump file
- Prints it in human readable form
- Modifies the packet source address
- Updates calculated packet fields (checksums, etc.) by calling "finalise()"
- Prints the modified packet in human-readable form

    #!/usr/bin/env python

    import openbsd

    o = openbsd.pcap.Offline("foo")
    o.filter("ip")
    packet = o.next(interpret=1)[0]
    print packet
    packet["ip"].src = "192.168.100.1"
    packet.finalise()
    print packet

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

PyOpenbsd v0.1 Manual