tangled
alpha
login
or
join now
altagos.dev
/
aether
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
error handling
altagos.dev
9 months ago
c8214f3c
aae38607
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+38
-10
1 changed file
expand all
collapse all
unified
split
src
root.zig
+38
-10
src/root.zig
···
6
6
buf: [512]u8 = undefined,
7
7
pos: usize = 0,
8
8
9
9
-
pub const ReadError = error{EndOfBuffer};
9
9
+
pub const ReadError = error{
10
10
+
EndOfBuffer,
11
11
+
JumpLimitExceeded,
12
12
+
};
10
13
11
14
pub const Reader = io.Reader(*BytePacketBuffer, ReadError, read);
12
15
···
26
29
27
30
/// Read a single byte and move the position one step forward
28
31
pub fn read(self: *BytePacketBuffer, dest: []u8) ReadError!usize {
29
29
-
const size = @min(dest.len, self.buf.len - self.pos);
32
32
+
if (self.pos + dest.len > self.buf.len)
33
33
+
return ReadError.EndOfBuffer;
34
34
+
const size = dest.len;
30
35
const end = self.pos + size;
31
31
-
32
36
@memcpy(dest[0..size], self.buf[self.pos..end]);
33
37
self.pos = end;
34
34
-
35
38
return size;
36
39
}
37
40
···
52
55
/// The tricky part: Reading domain names, taking labels into consideration.
53
56
/// Will take something like [3]www[6]google[3]com and append
54
57
/// www.google.com to outstr.
55
55
-
pub fn read_qname(self: *BytePacketBuffer, outstr: []u8) !void {
58
58
+
pub fn read_qname(self: *BytePacketBuffer, outstr: []u8) ReadError!void {
56
59
// We might encounter jumps, therefore we need to keep thrack of our position locally
57
60
var pos = self.pos;
58
61
var out_pos: usize = 0;
···
64
67
65
68
var delim: ?[]const u8 = null;
66
69
while (true) {
67
67
-
if (jumps_performed > max_jumps) return error.JumpLimitExceeded;
70
70
+
if (jumps_performed > max_jumps) return ReadError.JumpLimitExceeded;
68
71
69
72
// Each label starts with a length byte
70
73
const len = try self.get(pos);
···
139
142
try testing.expectEqual(0x1010101, try buf.reader().readInt(u32, .big));
140
143
}
141
144
142
142
-
test "BytePacketBuffer.read_qname" {
145
145
+
test "BytePacketBuffer.read last byte" {
146
146
+
const testing = std.testing;
147
147
+
var buf = BytePacketBuffer{};
148
148
+
buf.buf[buf.buf.len - 1] = 0x1;
149
149
+
buf.pos = buf.buf.len - 1;
150
150
+
try testing.expectEqual(0x1, try buf.reader().readInt(u8, .big));
151
151
+
try testing.expectError(
152
152
+
BytePacketBuffer.ReadError.EndOfBuffer,
153
153
+
buf.reader().readInt(u8, .big),
154
154
+
);
155
155
+
}
156
156
+
157
157
+
test "BytePacketBuffer.read_qname google.com" {
143
158
const testing = std.testing;
144
159
const allocator = testing.allocator;
145
160
146
146
-
const input = [_]u8{ 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00 };
147
147
-
var buf = BytePacketBuffer{};
161
161
+
const input = [_]u8{
162
162
+
0x06, // [6]
163
163
+
0x67, // g
164
164
+
0x6f, // o
165
165
+
0x6f, // o
166
166
+
0x67, // g
167
167
+
0x6c, // l
168
168
+
0x65, // e
169
169
+
0x03, // [3]
170
170
+
0x63, // c
171
171
+
0x6f, // o
172
172
+
0x6d, // m
173
173
+
0x00, // [0]
174
174
+
};
175
175
+
const expected = "google.com";
148
176
177
177
+
var buf = BytePacketBuffer{};
149
178
for (input, 0..) |char, idx| {
150
179
buf.buf[idx] = char;
151
180
}
152
181
153
153
-
const expected = "google.com";
154
182
const outstr = try allocator.alloc(u8, expected.len);
155
183
defer allocator.free(outstr);
156
184