tiny.stun.Message
Defined in tiny.stun.
API (7)
Actions
Public operations.
Fields and members
Public fields and members.
Source
Source: lib/stun/src/message.zig:98
zig
pub const Message = struct { header: Header, bytes: []const u8, attribute_count: u14, pub fn decode(bytes: []const u8) DecodeError!Message { if (bytes.len < header_bytes) return error.MessageTooShort; const message_type = std.mem.readInt(u16, bytes[0..2], .big); if ((message_type & 0xc000) != 0) return error.InvalidMessageType; const payload_len = std.mem.readInt(u16, bytes[2..4], .big); if ((payload_len & 3) != 0) return error.InvalidMessageLength; const total = std.math.add(usize, header_bytes, payload_len) catch return error.InvalidMessageLength; if (total > bytes.len) return error.InvalidMessageLength; if (total < bytes.len) return error.TrailingBytes; if (std.mem.readInt(u32, bytes[4..8], .big) != magic_cookie) { return error.InvalidMagicCookie; } const header = Header{ .class = decodeClass(message_type), .method = decodeMethod(message_type), .length = payload_len, .transaction_id = bytes[8..20].*, }; const count = try validateAttributes(bytes, header.transaction_id); return .{ .header = header, .bytes = bytes, .attribute_count = count }; } pub fn attributes(self: Message) Iterator { return .{ .bytes = self.bytes, .transaction_id = self.header.transaction_id, }; } pub fn verifyIntegrity(self: Message, key: []const u8) VerifyError!void { const item = try self.integrityItem() orelse return error.MissingIntegrity; const adjusted_len: u16 = @intCast(item.end - header_bytes); switch (item.attribute.value) { .message_integrity => |expected| { var actual: [integrity.sha1_bytes]u8 = undefined; integrity.hmacSha1Adjusted( &actual, self.bytes, item.start, adjusted_len, key, ); if (!integrity.equalSha1(actual, expected)) return error.IntegrityMismatch; }, .message_integrity_sha256 => |expected| { var actual: [integrity.sha256_bytes]u8 = undefined; integrity.hmacSha256Adjusted( &actual, self.bytes, item.start, adjusted_len, key, ); if (!integrity.equalSha256(actual, expected)) return error.IntegrityMismatch; }, else => unreachable, } } pub fn verifyFingerprint(self: Message) VerifyError!void { var iterator = self.attributes(); for (0..max_attribute_count) |_| { const item = try iterator.nextItem() orelse break; if (item.attribute.value == .fingerprint) { const expected = item.attribute.value.fingerprint; if (integrity.fingerprint(self.bytes[0..item.start]) != expected) { return error.FingerprintMismatch; } return; } } return error.MissingFingerprint; } fn integrityItem(self: Message) DecodeError!?Item { var iterator = self.attributes(); var sha1: ?Item = null; for (0..max_attribute_count) |_| { const item = try iterator.nextItem() orelse break; switch (item.attribute.value) { .message_integrity_sha256 => return item, .message_integrity => if (sha1 == null) { sha1 = item; }, else => {}, } } return sha1; }};Source: lib/stun/src/root.zig:38
zig
pub const Message = message_mod.Message;Audit
| Definitions | 5 |
|---|---|
| Public names | 5 |
| Members | 3 |
| Version | 26.7.0 |
| Revision | daab053ee433 |