Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ The projects is structured into modules, each module is a separate library that
* `rtp` - [RTP (Real-time Transport Protocol)](https://datatracker.ietf.org/doc/html/rfc3550) implementation for media streaming based on RFC 3550.
* `sdp` - [SDP (Session Description Protocol)](https://datatracker.ietf.org/doc/html/rfc4566) implementation for describing multimedia sessions based on RFC 4566.
* `rtsp` - [RTSP (Real Time Streaming Protocol)](https://datatracker.ietf.org/doc/html/rfc2326) implementation for controlling streaming media servers based on RFC 2326.
* `stun` - [STUN (Session Traversal Utilities for NAT)](https://datatracker.ietf.org/doc/html/rfc8489) implementation for NAT traversal based on RFC 5389.
97 changes: 97 additions & 0 deletions bench/stun/message.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const std = @import("std");
const zbench = @import("zbench");
const stun = @import("stun");

// Binding request, no attributes (header only)
const empty_request = [_]u8{
0x00, 0x01, 0x00, 0x00,
0x21, 0x12, 0xA4, 0x42,
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B,
};

// Binding success response with MAPPED-ADDRESS (IPv4) + XOR-MAPPED-ADDRESS (IPv4)
const mapped_response = [_]u8{
0x01, 0x01, 0x00, 0x18,
0x21, 0x12, 0xA4, 0x42,
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B,
// MAPPED-ADDRESS IPv4
0x00, 0x01, 0x00, 0x08,
0x00, 0x01, 0x80, 0x55,
192, 0, 2, 1,
// XOR-MAPPED-ADDRESS IPv4
0x00, 0x20, 0x00, 0x08,
0x00, 0x01, 0xA1, 0x47,
0xE1, 0x12, 0xA6, 0x43,
};

// RFC 5769 §2.1 sample binding request: SOFTWARE, PRIORITY, ICE-CONTROLLED,
// USERNAME, MESSAGE-INTEGRITY, FINGERPRINT
const rfc5769_request = [_]u8{
0x00, 0x01, 0x00, 0x58,
0x21, 0x12, 0xa4, 0x42,
0xb7, 0xe7, 0xa7, 0x01,
0xbc, 0x34, 0xd6, 0x86,
0xfa, 0x87, 0xdf, 0xae,
0x80, 0x22, 0x00, 0x10,
0x53, 0x54, 0x55, 0x4e,
0x20, 0x74, 0x65, 0x73,
0x74, 0x20, 0x63, 0x6c,
0x69, 0x65, 0x6e, 0x74,
0x00, 0x24, 0x00, 0x04,
0x6e, 0x00, 0x01, 0xff,
0x80, 0x29, 0x00, 0x08,
0x93, 0x2f, 0xf9, 0xb1,
0x51, 0x26, 0x3b, 0x36,
0x00, 0x06, 0x00, 0x09,
0x65, 0x76, 0x74, 0x6a,
0x3a, 0x68, 0x36, 0x76,
0x59, 0x20, 0x20, 0x20,
0x00, 0x08, 0x00, 0x14,
0x9a, 0xea, 0xa7, 0x0c,
0xbf, 0xd8, 0xcb, 0x56,
0x78, 0x1e, 0xf2, 0xb5,
0xb2, 0xd3, 0xf2, 0x49,
0xc1, 0xb5, 0x71, 0xa2,
0x80, 0x28, 0x00, 0x04,
0xe5, 0x7a, 0x3b, 0xcf,
};

const rfc5769_password = "VOkJxbRl1RmTxUk/WvJxBt";

fn parseHeaderOnly(_: std.mem.Allocator) void {
const msg = stun.Message.parse(&empty_request) catch unreachable;
var it = msg.iterateAttributes(&.{});
while (it.next() catch unreachable) |attr| std.mem.doNotOptimizeAway(attr);
std.mem.doNotOptimizeAway(msg);
}

fn parseMappedAddresses(_: std.mem.Allocator) void {
const msg = stun.Message.parse(&mapped_response) catch unreachable;
var it = msg.iterateAttributes(&.{});
while (it.next() catch unreachable) |attr| std.mem.doNotOptimizeAway(attr);
std.mem.doNotOptimizeAway(msg);
}

fn parseRfc5769(_: std.mem.Allocator) void {
const msg = stun.Message.parse(&rfc5769_request) catch unreachable;
var it = msg.iterateAttributes(rfc5769_password);
while (it.next() catch unreachable) |attr| std.mem.doNotOptimizeAway(attr);
std.mem.doNotOptimizeAway(msg);
}

pub fn main(init: std.process.Init) !void {
const io = init.io;
const stdout: std.Io.File = .stdout();

var bench = zbench.Benchmark.init(init.gpa, .{});
defer bench.deinit();

try bench.add("STUN: header only", parseHeaderOnly, .{});
try bench.add("STUN: mapped + xor-mapped (IPv4)", parseMappedAddresses, .{});
try bench.add("STUN: RFC 5769 with integrity + fingerprint", parseRfc5769, .{});
try bench.run(io, stdout);
}
16 changes: 16 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ pub fn build(b: *std.Build) void {
},
});

const stun = b.addModule("stun", .{
.root_source_file = b.path("src/stun/stun.zig"),
.target = target,
.optimize = optimize,
});

_ = b.addModule("protocols", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.imports = &.{
.{ .name = "rtp", .module = rtp },
.{ .name = "sdp", .module = sdp },
.{ .name = "rtsp", .module = rtsp },
.{ .name = "stun", .module = stun },
},
});

Expand All @@ -61,10 +68,17 @@ pub fn build(b: *std.Build) void {
});
const run_rtsp_tests = b.addRunArtifact(rtsp_tests);

const stun_tests = b.addTest(.{
.root_module = stun,
.filters = test_filters,
});
const run_stun_tests = b.addRunArtifact(stun_tests);

const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_rtp_tests.step);
test_step.dependOn(&run_sdp_tests.step);
test_step.dependOn(&run_rtsp_tests.step);
test_step.dependOn(&run_stun_tests.step);
}

{
Expand All @@ -73,6 +87,7 @@ pub fn build(b: *std.Build) void {
const benches = .{
.{ .name = "rtp_packet", .src = "bench/rtp/packet.zig" },
.{ .name = "sdp_session", .src = "bench/sdp/session.zig" },
.{ .name = "stun_message", .src = "bench/stun/message.zig" },
};

inline for (benches) |bench| {
Expand All @@ -86,6 +101,7 @@ pub fn build(b: *std.Build) void {
.{ .name = "zbench", .module = zbench.module("zbench") },
.{ .name = "rtp", .module = rtp },
.{ .name = "sdp", .module = sdp },
.{ .name = "stun", .module = stun },
},
}),
});
Expand Down
1 change: 1 addition & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const rtp = @import("rtp");
pub const sdp = @import("sdp");
pub const rtsp = @import("rtsp");
pub const stun = @import("stun");
Loading
Loading