Crate pnet_macros [] [src]

The pnet_macros crate provides the #[packet] macro and compiler plugin, which is used to specify the format of on-the-wire packets, and automatically generate zero-copy accessors and mutators for the fields. It is used as follows:

#![feature(core, collections, custom_attribute, plugin)]
#![plugin(pnet_macros)]

extern crate pnet;

/// This module contains a list of type aliases which may be used
use pnet_macros::types::{u4, u12be};

/// Packets are specified in the same way as normal Rust structs, but with a `#[packet]`
/// attribute.
#[packet]
pub struct Example {
    /// This is a simple field which contains a 4-bit, unsigned integer.
    /// Note that `u4` is simply an alias for `u8` - the name is a hint
    /// to the compiler plugin, it is NOT a usable 4 bit type!
    simple_field1: u4,

    /// This specifies that `simple_field2` should be a 12-bit field,
    /// with bits stored in big endian
    simple_field2: u12be,

    /// All packets must specify a `#[payload]`, which should be a
    /// `Vec<u8>`. This represents the packet's payload, for example in
    /// an IPv4 packet, the payload could be a UDP packet, or in a UDP
    /// packet the payload could be the application data. All the
    /// remaining space in the packet is considered to be the payload
    /// (this doesn't have to be the case, see the documentation for
    /// `#[payload]` below.
    #[payload]
    payload: Vec<u8>
}

A number of things will then be generated. You can see this in action in the documentation and source of each of the packet types in the pnet::packet module. Things generated include (assuming the Example struct from above):

Attributes

There are a number of attributes which fields may have, these include:

Modules

types

Provides type aliases for various primitive integer types

Functions

plugin_registrar

The entry point for the syntax extension