#Types and the Wire
D-Bus is a binary protocol with strict alignment and signature rules. Every message header and body must follow specific memory boundaries: basic types are aligned to their natural size, while structs and dictionary entries must start on an 8-byte boundary.
In C, serializing data usually looks like this:
dbus_message_iter_init_append(msg, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub_iter);
dbus_message_iter_append_basic(&sub_iter, DBUS_TYPE_STRING, &str);
dbus_message_iter_close_container(&iter, &sub_iter);
In Goose, you don't write container iterators. You pass native Zig types, and Goose's compile-time reflection figures out the signatures, padding, and alignments automatically.
#Type Mapping Table
Here is how native Zig types correspond to D-Bus wire signatures:
| D-Bus Type | Signature | Wire Size and Alignment | Zig Native / Wrapper Type | Wire Description |
|---|---|---|---|---|
| BYTE | y |
1 byte (align 1) | u8 |
Single byte |
| BOOLEAN | b |
4 bytes (align 4) | bool |
32-bit unsigned: 0 or 1 |
| INT16 | n |
2 bytes (align 2) | i16 |
Signed 16-bit integer |
| UINT16 | q |
2 bytes (align 2) | u16 |
Unsigned 16-bit integer |
| INT32 | i |
4 bytes (align 4) | i32 |
Signed 32-bit integer |
| UINT32 | u |
4 bytes (align 4) | u32 |
Unsigned 32-bit integer |
| INT64 | x |
8 bytes (align 8) | i64 |
Signed 64-bit integer |
| UINT64 | t |
8 bytes (align 8) | u64 |
Unsigned 64-bit integer |
| DOUBLE | d |
8 bytes (align 8) | f64 |
64-bit IEEE 754 float |
| STRING | s |
4-byte len + data + \0 (align 4) |
GStr |
Null-terminated UTF-8 string |
| OBJECT_PATH | o |
4-byte len + data + \0 (align 4) |
GPath |
Validated D-Bus path |
| SIGNATURE | g |
1-byte len + data + \0 (align 1) |
GSig |
Type signature string |
| UNIX_FD | h |
4 bytes (align 4) | GUFd / ResolvedFd |
Index into out-of-band descriptor table |
| VARIANT | v |
1-byte sig len + sig + data (align 1) | Value.Variant(T) |
Tagged envelope holding dynamic value |
| ARRAY | a |
4-byte total byte length (align 4) | []const T, []T |
Slice of homogeneous elements |
| STRUCT | (..) |
8-byte container boundary (align 8) | Normal Zig struct or @Tuple |
Sequence of fields in memory |
| DICT_ENTRY | {..} |
8-byte container boundary (align 8) | std.StringHashMap(V) |
Key-value pair in an array (a{sv}) |
#Wrapper Types
Because Zig slices []const u8 can represent raw bytes, strings, or arbitrary buffers, Goose provides simple zero-cost wrapper structs in goose.core.value so the compiler knows your exact intent:
#1. `GStr`: Strings (`'s'`)
D-Bus strings are length-prefixed and null-terminated:
const GStr = goose.core.value.GStr;
// Wraps a null-terminated slice
const greeting = GStr.new("Hello, world!");
std.debug.print("Message: {s}\n", .{greeting.s});
#2. `GPath`: Object Paths (`'o'`)
Represents a hierarchy path on D-Bus (like /org/freedesktop/DBus):
const GPath = goose.core.value.GPath;
const path = GPath.new("/com/example/MyService");
#3. `ResolvedFd`: Open File Descriptors (`'h'`)
Represents a real operating system file descriptor handle transferred out-of-band:
const ResolvedFd = goose.ResolvedFd;
pub fn HandleFile(rfd: ResolvedFd) !void {
std.debug.print("Received open OS descriptor: {d}\n", .{rfd.handle});
}
#Composite Types and Memory Alignment
D-Bus has strict alignment rules that trip up many hand-rolled parsers:
- A
u32must be at an offset divisible by 4. - An
i64orf64must be at an offset divisible by 8. - Any struct or dictionary entry container must start at an offset divisible by 8.
Goose takes care of these rules under the hood.
#Structs and Tuples
Zig structs and tuples map directly to D-Bus structs:
// Signature on wire: "(si)"
const UserRecord = struct {
name: GStr,
id: i32,
};
// Anonymous tuple mapped to "sb"
const TupleType = @Tuple(&[_]type{ GStr, bool });
const status: TupleType = .{ GStr.new("online"), true };
#Arrays
Arrays are prefixed with a 32-bit unsigned integer containing the total length in bytes of the elements that follow:
// Signature: "ai"
const numbers = [_]i32{ 1, 2, 3, 4, 5 };
// Signature: "as"
const words = [_]GStr{
GStr.new("apple"),
GStr.new("banana"),
};
#Dictionaries
D-Bus doesn't have a dedicated map type; it encodes dictionaries as arrays of key-value pairs (like a{sv} or a{ss}). Goose works with standard std.StringHashMap:
// Dictionary of variants: "a{sv}"
var map = std.StringHashMap(GVariant).init(allocator);
defer map.deinit();
try map.put("volume", .{ .uint32 = 70 });
try map.put("active", .{ .boolean = true });
#The Wire Header
Every D-Bus packet begins with a fixed 12-byte header, followed by an array of structured header fields:
pub const HeaderFieldCode = enum(u8) {
Invalid = 0x0,
Path = 0x1,
Interface = 0x2,
Member = 0x3,
ErrorName = 0x4,
ReplySerial = 0x5,
Destination = 0x6,
Sender = 0x7,
Signature = 0x8,
UnixFds = 0x9,
};These header codes specify:
Path (0x1): Target object path.Interface (0x2): Target interface.Member (0x3): Method or signal name.ErrorName (0x4): Name of the error if this message is a failure reply.ReplySerial (0x5): Serial number of the message being replied to.Destination (0x6): Destination bus address.Sender (0x7): Verified sender bus address.Signature (0x8): Signature of the body payload.UnixFds (0x9): Number of file descriptors passed out-of-band.
#Direct Encoding and Decoding
If you ever need to manually encode or decode payloads without a proxy, use BodyEncoder and BodyDecoder:
const message = goose.message;
const GStr = goose.core.value.GStr;
// Encode a tuple of values
var encoder = try message.BodyEncoder.encode(allocator, .{
GStr.new("Ping"),
@as(u32, 100),
true,
});
defer encoder.deinit();
std.debug.print("Signature: {s}\n", .{encoder.signature()}); // "sub"
// Decode from raw bytes
var decoder = message.BodyDecoder.init(
allocator,
encoder.body(),
encoder.signature(),
.little,
);
const action = try decoder.decode(GStr);
const count = try decoder.decode(u32);
const active = try decoder.decode(bool);
std.debug.print("Decoded: {s}, count={d}, active={}\n", .{ action.s, count, active });