Lunatik

structModule

Binary codec for fixed C struct layouts.

Calling the module on a layout descriptor (as generated by autogen, e.g. require("linux.netlink").layout.nlmsghdr) derives a string.pack format once and returns a codec exposing pack/unpack/size, so callers never hand-write the byte layout. A codec also reads a struct where it lies, a field at a time and by name, through a view over a data object.

Usage

local struct = require("struct")
local nlmsghdr = struct(require("linux.netlink").layout.nlmsghdr)
local bytes = nlmsghdr:pack(len, mtype, flags, seq, pid)
local len, mtype = nlmsghdr:unpack(bytes)

Class codec

codec:fieldsize (name) Returns the size in bytes of a named field.
codec:pack (...) Serializes the field values (in offset order) into the struct’s bytes.
codec:unpack (buf[, pos]) Deserializes the struct’s fields from a buffer.
codec:view (data, at) A view of the struct as it lies in data from at, which reads a field by its name: view.ttl reads the field at at plus its offset, with its width and sign, each time it is indexed, as the data object’s reads do: in the kernel’s byte order, but for a field the layout marks bigendian, read most significant first, and for a bitfield, taken out of its load.

struct

struct (layout) Builds a codec from a layout descriptor.

Class codec in detail

Codec a layout builds, packing its fields in native byte order but those the layout marks bigendian, which it packs big-endian, as the kernel keeps a __be16. Its size field holds the struct’s size in bytes, and its fields field the layout’s fields in offset order, the order pack takes and unpack returns their values.
codec:fieldsize (name)
Returns the size in bytes of a named field.

Parameters

Returns

    integer or nil the field size, or nil if the layout has no such field.
codec:pack (...)
Serializes the field values (in offset order) into the struct’s bytes.

Parameters

  • ... the field values

Returns

    string
codec:unpack (buf[, pos])
Deserializes the struct’s fields from a buffer.

Parameters

  • buf string
  • pos integer starting offset (1-based) (optional)

Returns

    the field values in offset order, then the position past the struct
codec:view (data, at)
A view of the struct as it lies in data from at, which reads a field by its name: view.ttl reads the field at at plus its offset, with its width and sign, each time it is indexed, as the data object’s reads do: in the kernel’s byte order, but for a field the layout marks bigendian, read most significant first, and for a bitfield, taken out of its load. A read past the end of data raises as theirs does, a field or a bitfield’s load is 1, 2, 4 or 8 bytes wide, and a name the layout lacks raises.

Parameters

  • data data the bytes, a data object or one that reads as it does
  • at integer the offset in data the struct starts at

Returns

    table the view

Raises

“struct: a view is read-only”, on an assignment to a field

Usage

local iphdr = struct(layout)
local ip = iphdr:view(packet, 14)
if ip.protocol == 17 then ... end

struct in detail

struct (layout)
Builds a codec from a layout descriptor.

Parameters

  • layout table {size = bytes, fields = {{name, offset, size, signed, bigendian}, ...}, bitfields = {{name, offset, size, signed, shift, mask}, ...}}: bigendian true for a field kept big-endian whatever the kernel’s order, and bitfields, which pack and unpack skip and which may be absent, each the size bytes at offset shifted right by shift and masked with mask

Returns

    codec a codec exposing :pack, :unpack, :fieldsize, :view and the size and fields fields.

Raises

“struct: derived format does not match the layout size (overlapping fields?)”