The packet Reference Manual

Next: , Previous: , Up: (dir)   [Contents][Index]

The packet Reference Manual

This is the packet Reference Manual, generated automatically by Declt version 4.0 beta 2 "William Riker" on Mon Aug 15 05:32:01 2022 GMT+0.

Table of Contents


1 Introduction

PACKET

PACKET is a simple library for defining C-style structures and packing/unpacking them into Common Lisp arrays.

It is typically used for handling UDP packets, but could also be used for unpacking e.g. mmap'ed data files.

Packet type definitions

Packet comes with some pre-defined intrinsic data types:

Users define their packet type and associated CLOS class using:

(defpacket name
  ((slot-name slot-type &rest slot-options)
   ...)
  &rest options)

This expands to a defclass for the structure and code for defining the new packet type.

The slot-type can either be a symbol referring to a previously defined type (either primitive or a previous defpacket) or it can be a form (slot-type length) which means an array of length of slot-type objects.

The slot-options are passed into the defclass slot specifier.

Options can include:

(:packing <integer packing>) ;; sets the packing width for the slots
(:size <integer size>)       ;; sets the total packet buffer size

All other options are passed as options to defclass.

Usage

Make a packet buffer using:

(pack object type) 

This returns the buffer filled in with information.

Extract an object from a buffer using:

(unpack buffer type)

Utilities

Along with the serialization functions, there are also several other utilities which provide commonly required functionality:

(defflags *example-flags*
  ((:myflag1 0 "My flag1")
   (:myflag2 1 "My flag2")))
(pack-flags '(:myflag1 :myflag2) *example-flags*)
(defenum *example-enum*
  ((:a 0 "A")
   (:b 1 "B")))
(enum :a *example-enum*)
(hd #(1 2 3 4))

Both the flags and enums expand to eval-when forms so that they can be computed at compile time e.g. like:

(defun test ()
  (list #.(enum :a *example-enum*)))

Example

We are trying to send packets to a UDP server (written in C) expecting something like

struct person_s {
  char name[20];
  uint32_t flags;
};

struct people_s {
  uint32_t magic;
  int32_t npeople;
  struct person_s people[20];
  uint32_t flags;
};

We define our packet type using:

(defconstant +magic-number+ #x1E2B3A4D)

(defpacket person-s
  ((name (:string 20) :initform "" :initarg :name)
   (flags :uint32 :initform 0 :initarg :flags))
  (:documentation "Person structure."))

(defpacket people-s 
  ((magic :uint32 :initform +magic-number+)
   (npeople :int32 :initform 0 :initarg :npeople)
   (people (person-s 20) :initform nil :initarg :people)
   (flags :uint32 :initform 0 :initarg :flags))
  (:documentation "Collection of people."))

;; pack an instance into a buffer
(pack (make-instance 'people-s 
                     :npeople 2
                     :people (list (make-instance 'person-s :name "frank" :flags 123)
		                   (make-instance 'person-s :name "james" :flags 321))
                     :flags 456)
      'people-s)
-> #(77 58 43 30 2 0 0 0 102 114 97 110 107 ... )

;; unpack a buffer into an instance
(unpack buffer 'people-s)
-> #<PEOPLE-S {24CB5061}>

See example.lisp for more simple usages.

TODO

Frank James March 2014


2 Systems

The main system appears first, followed by any subsystem dependency.


Previous: , Up: Systems   [Contents][Index]

2.1 packet

Simple binary serialization library.

Author

Frank James <frank.a.james@gmail.com>

License

BSD

Dependency

ieee-floats (system).

Source

packet.asd.

Child Components

3 Files

Files are sorted by type and then listed depth-first from the systems components trees.


Previous: , Up: Files   [Contents][Index]

3.1 Lisp


Next: , Previous: , Up: Lisp   [Contents][Index]

3.1.1 packet/packet.asd

Source

packet.asd.

Parent Component

packet (system).

ASDF Systems

packet.


Next: , Previous: , Up: Lisp   [Contents][Index]

3.1.2 packet/package.lisp

Source

packet.asd.

Parent Component

packet (system).

Packages

packet.


3.1.3 packet/utils.lisp

Dependency

package.lisp (file).

Source

packet.asd.

Parent Component

packet (system).

Public Interface

Previous: , Up: Lisp   [Contents][Index]

3.1.4 packet/packet.lisp

Dependency

utils.lisp (file).

Source

packet.asd.

Parent Component

packet (system).

Public Interface
Internals

4 Packages

Packages are listed by definition order.


Previous: , Up: Packages   [Contents][Index]

4.1 packet

Source

package.lisp.

Use List

common-lisp.

Public Interface
Internals

5 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


Next: , Previous: , Up: Definitions   [Contents][Index]

5.1 Public Interface


5.1.1 Macros

Macro: defenum (name enums)

Define a list of enums

Package

packet.

Source

utils.lisp.

Macro: defflags (name flags &optional documentation)

Macro to define a set of flags

Package

packet.

Source

utils.lisp.

Macro: defpacket (name slots &rest options)

Macro to define the CLOS class and packet type.

SLOTS should be a list of format (SLOT-NAME SLOT-TYPE &rest SLOT-OPTIONS).

SLOT-NAME should be a symbol used for refering to the CLOS class slot.

SLOT-TYPE should be a symbol refering to a previously defined packet type (forward references are forbidden because we need to know the total packet size at definition time).
Use (SLOT-TYPE LENGTH) for arrays.

SLOT-OPTIONS are passed to the corresponding defclass slot specifier.

OPTIONS can contain (:packing PACKING) and (:size SIZE). These are used to
set the packing width and total packet size. If not specified PACKING defaults to *DEFAULT-PACKING*, SIZE defaults to the total size of packet type. If specified, SIZE may not be less than this, but can be more, to allow for unused space at the end of the structure.

All other options are passed to defclass.

Package

packet.

Source

packet.lisp.

Macro: defpacket* (name slots &rest options)

Like DEFPACKET but defines a DEFSTRUCT instead of a DEFCLASS.

Each slot should be of the form (slot-name slot-type &optional initial-value).

Initial value defaults to 0.

Package

packet.

Source

packet.lisp.


Previous: , Up: Public Interface   [Contents][Index]

5.1.2 Ordinary functions

Function: enum (enum enums)
Package

packet.

Source

utils.lisp.

Function: enum-id (number enums)
Package

packet.

Source

utils.lisp.

Function: enum-p (number enum enums)
Package

packet.

Source

utils.lisp.

Function: flag-p (number flag-name flags)
Package

packet.

Source

utils.lisp.

Function: hd (data)

Hexdump output

Package

packet.

Source

utils.lisp.

Function: list-types ()

List all defined PACKET types.

Package

packet.

Source

packet.lisp.

Function: pack (object type)

Make a packet buffer from the initial object.

Package

packet.

Source

packet.lisp.

Function: pack-flags (flag-names flags)

Combine flags

Package

packet.

Source

utils.lisp.

Function: pad (array len)

Pad array with zeros if its too short

Package

packet.

Source

utils.lisp.

Function: pad* (array &optional width)

Pad to a length multiple of WIDTH

Package

packet.

Source

utils.lisp.

Function: subseq* (sequence start &optional len)

Subsequence with length

Package

packet.

Source

utils.lisp.

Function: type-size (type)

Get the total size in bytes for this type.

Package

packet.

Source

packet.lisp.

Function: unpack (buffer type)

Unpack a buffer into an object. Returns any extra (unused) bytes as the 2nd value

Package

packet.

Source

packet.lisp.

Function: unpack-flags (number flags)

Split the number into its flags.

Package

packet.

Source

utils.lisp.

Function: usb8 (&rest sequences)

Make an (unsigned byte 8) vector from the sequences

Package

packet.

Source

utils.lisp.

Function: usb8* (&rest numbers)

Make an (unsigned-byte 8) vector from the numbers

Package

packet.

Source

utils.lisp.


5.2 Internals


Next: , Previous: , Up: Internals   [Contents][Index]

5.2.1 Special variables

Special Variable: *default-packing*

Default packing boundary.

Package

packet.

Source

packet.lisp.

Special Variable: *type-definitions*

Global table storing type definitions.

Package

packet.

Source

packet.lisp.


5.2.2 Macros

Macro: define-alias (alias name)
Package

packet.

Source

packet.lisp.

Macro: define-type (name ((pobject pbuffer pstart) &body pbody) ((ubuffer ustart) &body ubody) size)
Package

packet.

Source

packet.lisp.


Previous: , Up: Internals   [Contents][Index]

5.2.3 Ordinary functions

Function: %define-alias (alias name)

Define an alias for a type. Allows refering to the same type by a different name.

Package

packet.

Source

packet.lisp.

Function: %define-type (name pack-handler unpack-handler size)

Intern a new type definition.

PACK-HANDLER is a function with lambda-list (object buffer start)
and should encode the object into the buffer starting at offset START.

UNPACK-HANDLE is a function with lambda-list (buffer start)
and should extract information about the desired object starting at offset START. It should return the object.

SIZE is the total number of bytes this object consumes.

Package

packet.

Source

packet.lisp.

Function: bytes (integer size)

Expand an INTEGER into its consituent number of SIZE bytes.

Package

packet.

Source

packet.lisp.

Function: compute-real-slots (slots packing &optional size)

Compute the offsets of each slot. We use this information in the closures generated by DEFPACKET to pack/unpack the object.

Package

packet.

Source

packet.lisp.

Function: get-type (name)

Find the type definition.

Package

packet.

Source

packet.lisp.

Function: merge-bytes (bytes &optional signed)

Convert a list of bytes into a signed/unsigned integer

Package

packet.

Source

packet.lisp.

Function: pack-array (array type buffer start)

Pack an array of object into the buffer.

Package

packet.

Source

packet.lisp.

Function: pack-bytes (bytes buffer start)

Pack a list of bytes into a buffer

Package

packet.

Source

packet.lisp.

Function: pack-object (object slots buffer start)

Pack an OBJECT specified by SLOTS into the BUFFER starting at offset START.

Package

packet.

Source

packet.lisp.

Function: pack-string (string length buffer start)

Pack a string into the buffer.

Package

packet.

Source

packet.lisp.

Function: pack-type (object type buffer start)

Pack an object into the buffer.

Package

packet.

Source

packet.lisp.

Function: pack-wstring (string length buffer start)

Pack a string into the buffer.

Package

packet.

Source

packet.lisp.

Function: round-offset (offset packing)

Round the offset to the nearest packing boundary

Package

packet.

Source

packet.lisp.

Function: type-packer (type)

Get the packing handler for this type.

Package

packet.

Source

packet.lisp.

Function: type-unpacker (type)

Get the unpacking handler for this type.

Package

packet.

Source

packet.lisp.

Function: unpack-array (length type buffer start)

Extract an array of objects from the buffer.

Package

packet.

Source

packet.lisp.

Function: unpack-bytes (buffer start size &optional signed)

Get an integer from the buffer.

Package

packet.

Source

packet.lisp.

Function: unpack-object (object slots buffer start)

Unpack the SLOTS of OBJECT from BUFFER starting at offset START.

Package

packet.

Source

packet.lisp.

Function: unpack-string (length buffer start)

Unpack a string from the buffer.

Package

packet.

Source

packet.lisp.

Function: unpack-type (type buffer start)

Unpack an objcet from the buffer.

Package

packet.

Source

packet.lisp.

Function: unpack-wstring (length buffer start)

Unpack a string from the buffer.

Package

packet.

Source

packet.lisp.


Appendix A Indexes


Next: , Previous: , Up: Indexes   [Contents][Index]

A.1 Concepts


Next: , Previous: , Up: Indexes   [Contents][Index]

A.2 Functions

Jump to:   %  
B   C   D   E   F   G   H   L   M   P   R   S   T   U  
Index Entry  Section

%
%define-alias: Private ordinary functions
%define-type: Private ordinary functions

B
bytes: Private ordinary functions

C
compute-real-slots: Private ordinary functions

D
defenum: Public macros
defflags: Public macros
define-alias: Private macros
define-type: Private macros
defpacket: Public macros
defpacket*: Public macros

E
enum: Public ordinary functions
enum-id: Public ordinary functions
enum-p: Public ordinary functions

F
flag-p: Public ordinary functions
Function, %define-alias: Private ordinary functions
Function, %define-type: Private ordinary functions
Function, bytes: Private ordinary functions
Function, compute-real-slots: Private ordinary functions
Function, enum: Public ordinary functions
Function, enum-id: Public ordinary functions
Function, enum-p: Public ordinary functions
Function, flag-p: Public ordinary functions
Function, get-type: Private ordinary functions
Function, hd: Public ordinary functions
Function, list-types: Public ordinary functions
Function, merge-bytes: Private ordinary functions
Function, pack: Public ordinary functions
Function, pack-array: Private ordinary functions
Function, pack-bytes: Private ordinary functions
Function, pack-flags: Public ordinary functions
Function, pack-object: Private ordinary functions
Function, pack-string: Private ordinary functions
Function, pack-type: Private ordinary functions
Function, pack-wstring: Private ordinary functions
Function, pad: Public ordinary functions
Function, pad*: Public ordinary functions
Function, round-offset: Private ordinary functions
Function, subseq*: Public ordinary functions
Function, type-packer: Private ordinary functions
Function, type-size: Public ordinary functions
Function, type-unpacker: Private ordinary functions
Function, unpack: Public ordinary functions
Function, unpack-array: Private ordinary functions
Function, unpack-bytes: Private ordinary functions
Function, unpack-flags: Public ordinary functions
Function, unpack-object: Private ordinary functions
Function, unpack-string: Private ordinary functions
Function, unpack-type: Private ordinary functions
Function, unpack-wstring: Private ordinary functions
Function, usb8: Public ordinary functions
Function, usb8*: Public ordinary functions

G
get-type: Private ordinary functions

H
hd: Public ordinary functions

L
list-types: Public ordinary functions

M
Macro, defenum: Public macros
Macro, defflags: Public macros
Macro, define-alias: Private macros
Macro, define-type: Private macros
Macro, defpacket: Public macros
Macro, defpacket*: Public macros
merge-bytes: Private ordinary functions

P
pack: Public ordinary functions
pack-array: Private ordinary functions
pack-bytes: Private ordinary functions
pack-flags: Public ordinary functions
pack-object: Private ordinary functions
pack-string: Private ordinary functions
pack-type: Private ordinary functions
pack-wstring: Private ordinary functions
pad: Public ordinary functions
pad*: Public ordinary functions

R
round-offset: Private ordinary functions

S
subseq*: Public ordinary functions

T
type-packer: Private ordinary functions
type-size: Public ordinary functions
type-unpacker: Private ordinary functions

U
unpack: Public ordinary functions
unpack-array: Private ordinary functions
unpack-bytes: Private ordinary functions
unpack-flags: Public ordinary functions
unpack-object: Private ordinary functions
unpack-string: Private ordinary functions
unpack-type: Private ordinary functions
unpack-wstring: Private ordinary functions
usb8: Public ordinary functions
usb8*: Public ordinary functions

Jump to:   %  
B   C   D   E   F   G   H   L   M   P   R   S   T   U