fix hex gen from relative invocation path
15 KiB
WireProto Specification
1. License
In a nutshell, means you can:
-
Use it in commercial/proprietary/internal works…
-
Expand upon/change the specification…
-
(As long as it is released under the same Creative Commons license)
-
As long as you attribute the original (this document). This can be as simple as something like:
Based on WireProto version <protocol version> as found at https://wireproto.io/.
More details certainly helps, though; you may want to mention the exact date you "forked" it, etc.
Please see the full text as collapsed above or the online version of the license for full legal copy.
Note
|
In the event of the embedded text in this document differing from the online version, the online version is assumed to take precedence as the valid license applicable to this work. |
2. Protocol
The WireProto data packing API is a custom wire protocol//message format designed for incredibly performant, unambiguous, predictable, platform-agnostic, client-agnostic communication. It is based heavily on the OpenSSH "v1" key format (example/details) packing method.
It supports arbitrary binary values, which means they can be anything according to the implementation-specific details; a common practice is to encode ("marshal") a Go struct to JSON bytes, and set that as a WireProto field’s value.
It supports both static construction/parsing/dissection and stream approaches in a single format, as well as multiple commands per request message/multiple answers per response message.
All packed uint32 values are big-endian.
This specification Protocol Version is 1
(0x00000001
).
2.1. Library
This protocol specification is accompanied with a reference library for Golang, "WireProto" (source):
2.2. Why a Custom Message Format?
Because existing ones (e.g. JSON, XML, YAML) are slow/bloaty, inaccurate, and/or inflexible. They struggle with binary or abritrary data (or in e.g. XML’s case requiring intermediate conditional encoding/decoding).
If it can be represented as bytes (which all digital data can), WireProto can send and receive it.
Additionally:
-
Protobuf has performance issues (yes, really; protobufs have large overhead) and is restrictive on data types for future-proofing.
-
Gob is very language-limiting and does not support e.g. nil pointers and cyclical values.
-
Cap’n Proto has wide language support and excellent performance but is terribly non-idiomatic, requiring the code to be generated from the schema and not vice versa (which is only ideal if you have only one communication interface).
-
JSON streams have no delimiters defined, and thus this makes it an inconvenience if using a parser that does not know when the message ends/is complete, or if it is expecting a standalone JSON object.
Tip
|
WireProto is only used for binary packing/unpacking; this means it can be used with any e.g. Thus it is transport/storage-agnostic, and can be used with a TCP socket, UDP socket, IPC (InterProcess Communication)/UDS (UNIX Domain Socket) handle, TLS-tunneled TCP socket, etc. |
3. Message Format
Tip
|
Throughout this document, you may see references to things like These refer to ASCII control characters. You will also see many values represented in hex. You can find more details about this (along with a full ASCII reference) here. Note that the socket API fully supports UTF-8 — just be sure that your Size Allocator are aligned to the byte count, not character count. |
Each message is generally composed of:
-
The Response Status[1]
-
One (or more) Record Group(s), each of which contain:
-
One (or more) Record(s), each of which contain:
-
One (or more) Field/Value pair(s), each of which contain:
-
A Copy Record[1]
-
-
3.1. Response Status
For responses, their messages have an additional byte prepended; a status indicator. This allows client programs to quickly bail in the case of an error if no further parsing is desired.
The status will be indicated by one of two values: an ASCII ACK
(0x06
) for all requests being returned successfully or an ASCII NAK
(0x15
) if one or more errors were encountered across all records.
3.2. Protocol Version
The protocol version is a packed uint32 that denotes which version of this protocol specification is being used.
It is maintained seperately from the library version/repo tags.
The current protocol version (as demonstrated in this document) is 1
(0x00000001
).
Note
|
Version 0 is reserved for current HEAD of the master branch of this specification and should be considered experimental.
|
3.3. Record Group
A record group contains multiple related Records. It is common to only have a single Record Group.
Its structure is:
-
One (or more) Records
3.3.1. Record
A record contains multiple related Field/Value Pairs (FVP). It is typical to only have a single Record.
Its structure is:
Important
|
For response messages, the record’s size allocator (but NOT the count allocator) includes the Copy Record size for each response record copy![1] |
3.3.1.1. Field/Value Pair (Key/Value Pair)
A field/value pair (also referred to as a key/value pair) contains a matched Field Name and its Field Value.
Its structure is:
-
A single Field Name
-
A single matching Field Value
Important
|
Unlike most/all other Allocators for other sections/levels, the field name and value allocators are consecutive Size Allocators! This is because there is only one field name and value per record. |
3.3.1.1.1. Field Name
The field name is usually from a finite set of allowed names. The Field Value, while written as bytes, often contains a data structure defined by the field name. (A field name is closer to a "value type".) It must be a UTF-8 string.
Its structure is:
-
The name in bytes
3.3.1.1.2. Field Value
A field’s value is, on the wire, just a series of bytes. The actual content of those bytes, including any structure or encoding, is likely to/probably depends on the paired Field Name.
Its structure is:
-
The value in bytes
3.3.1.2. Copy Record (Response Copy of Request)
This contains a "copy" of the original/request’s Record that this record is in response to.
It is a variant of a Record used exclusively in responses, and is tied to (included in) each response’s FVP.
Its structure is:
3.3.1.2.1. Field/Value Pair (Key/Value Pair) (Response Copy)
A field/value pair (also referred to as a key/value pair) contains a matched Field Name and its Field Value.
It is a variant of a Field/Value Pair used exclusively in response copies of the original request’s FVP.
Its structure is:
-
A single Field Name
-
A single matching Field Value
Important
|
Unlike most/all other Allocators for other sections/levels, the field name and value allocators are consecutive Size Allocators! This is because there is only one field name and value per record. |
4. Checksums
Checksums are optional for the client but the server will always send them. If present in the request, the server will validate to ensure the checksum matches the message body (body start to body end, headers included). If the checksum does not match, an error will be returned.
They are represented as a big-endian-packed uint32.
The checksum must be prefixed with a CKSUM
Header Prefix. If no checksum is provided, this prefix must not be included in the sequence.
Tip
|
You can quickly check if a checksum is present by checking the first byte in requests or the second byte in responses. If it is |
The checksum method used is the IEEE 802.3 CRC-32, which should be natively available for all/most client implementations as it is perhaps the most ubiquitous of CRC-32 variants. (Polynomial 0x04c11db7
, reversed polynomial 0xedb88320
.)
To confirm you are using the correct CRC32 implementation (as there are a ton of "CRC-32" algorithms and methods out there), use the following validations:
String | Bytes | Checksum (integer) | Checksum (bytes, little-endian) | Checksum (bytes, big-endian) |
---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
5. Headers
Certain sections are wrapped with an identifying header. Those headers are included below for reference.
5.1. RESPSTART
Byte Sequence
Responses have a Response Status.[1]
It is either an ACK
(0x06
) or NAK
(0x15
).
5.2. CKSUM
Header Prefix
A checksum, if provided, will have a prefix header of ESC
(0x1b
).
5.3. MSGSTART
Header Prefix
The message start header indicates a start of a message.
It is an SOH
(0x01
).
5.4. BODYSTART
Header Prefix
The body start header indicates that actual data/records follows.
It is an STX
(0x02
).
5.6. MSGEND
Sequence
The message end prefix indicates that a message in its entirety has ended.
It is an EOT
(0x04
).
6. Allocators
There are two type of allocators included for each following sequence of bytes: count allocators
and size allocators
.
They can be used by clients to determine the size of destination buffers, and are used by the server to efficiently unpack requests.
They are usually paired together with the count allocator preceding the size allocator, but not always (e.g. Field/Value Pair (Key/Value Pair) have two Size Allocator).
All allocators are unsigned 32-bit integers, little-endian-packed.
6.1. Count Allocator
Count allocators indicate how many children objects are contained.
6.2. Size Allocator
Size allocators indicate how much (in bytes) all children objects are combined together. It includes e.g. separators, etc.
7. Reference Model and Examples
For a more visual explanation, given the following e.g. Golang structs from the Golang reference library (wireproto.Request{}
and wireproto.Response{}
):
7.1. Single/Simple
7.1.1. Single/Simple Request
Example Message Structure (Simple Request)
link:https://git.r00t2.io/r00t2/go_wireproto/raw/branch/master/test_obj_simple_req.go[role=include]
Would then serialize as (in hex):
Annotated Hex
Unresolved directive in <stdin> - include::docs/data/request.simple.txt[]
Or, non-annotated:
Unresolved directive in <stdin> - include::docs/data/request.simple.hex[]
7.1.2. Single/Simple Response
Example Message Structure (Simple Response)
link:https://git.r00t2.io/r00t2/go_wireproto/raw/branch/master/test_obj_simple_resp.go[role=include]
Would then serialize as (in hex):
Annotated Hex
Unresolved directive in <stdin> - include::docs/data/response.simple.txt[]
Or, non-annotated:
Unresolved directive in <stdin> - include::docs/data/response.simple.hex[]
7.2. Multiple/Many/Complex
Multiple commands, parameters, etc. can be specified in one message.
7.2.1. Complex Request
Example Message Structure (Multiple/Many Requests, Single Message)
link:https://git.r00t2.io/r00t2/go_wireproto/raw/branch/master/test_obj_multi_req.go[role=include]
Would then serialize as (in hex):
Annotated Hex
Unresolved directive in <stdin> - include::docs/data/request.multi.txt[]
Or, non-annotated:
Unresolved directive in <stdin> - include::docs/data/request.multi.hex[]
7.2.2. Complex Response
Example Message Structure (Response to Multiple/Many Requests, Single Message)
link:https://git.r00t2.io/r00t2/go_wireproto/raw/branch/master/test_obj_multi_resp.go[role=include]
Would then serialize as (in hex):
Annotated Hex
Unresolved directive in <stdin> - include::docs/data/response.multi.txt[]
Or, non-annotated:
Unresolved directive in <stdin> - include::docs/data/response.multi.hex[]