bytes: []const u8Externally managed memory. Already checked to be valid.
The maximum number of bytes needed to store the text representation of a max length host name, where labels are separated by dots, including the trailing dot and the zero-length root label.
The max length of a host name is determined by its packet representation, where each label has a length prefix of 1 octet, the root label has a length of 0, and the maximum total number of octets is 255.
pub fn assert(ok: bool) voidInvokes detectable illegal behavior when ok is false.
In debug and safe modes, calls to this function are always generated, and the unreachable statement triggers a panic.
In fast and small modes, calls to this function are optimized away, and in fact the optimizer is able to use the assertion in its heuristics.
Inside a test block, it is best to use the testing module rather than this function, because this function may not detect a test failure in fast and small mode. Outside of a test block, this assert function is the correct function to use.
pub fn fromUri(uri: std.Uri, buffer: *[HostName.max_len]u8) FromUriError!HostNameReturned HostName.bytes may point into buffer or uri.host.
pub fn validate(bytes: []const u8) ValidateError!voidValidates a hostname according to RFC 1123
pub fn init(bytes: []const u8) ValidateError!HostNamepub fn sameParentDomain(parent_host: HostName, child_host: HostName) boolpub fn eql(a: HostName, b: HostName) boolDomain names are case-insensitive (RFC 5890, Section 2.3.2.4)
pub fn lookup(host_name: HostName, io: Io, resolved: *Io.Queue(LookupResult), options: LookupOptions) LookupError!voidAdds any number of LookupResult.address into resolved, and exactly one LookupResult.canonical_name.
Guaranteed not to block if provided queue has capacity at least 16.
Closes resolved before return, even on error.
Asserts resolved is not closed until this call returns.
pub fn expand(noalias packet: []const u8, start_i: usize, noalias dest_buffer: []u8) ExpandError!structDecompresses a DNS name.
Returns number of bytes consumed from packet starting at i, along with the expanded HostName.
Asserts buffer is has length at least max_len.
pub fn connect(host_name: HostName, io: Io, port: u16, options: IpAddress.ConnectOptions) ConnectError!Streampub fn connectMany(host_name: HostName, io: Io, port: u16, results: *Io.Queue(IpAddress.ConnectError!Stream), options: IpAddress.ConnectOptions) LookupError!voidAsynchronously establishes a connection to all IP addresses associated with a host name, adding them to a results queue upon completion.
error.Canceled will never be added to the queue, but other errors may be.
Closes results before return, even on error.
Asserts results is not closed until this call returns.
fn enqueueConnection(address: IpAddress, io: Io, queue: *Io.Queue(IpAddress.ConnectError!Stream), options: IpAddress.ConnectOptions) Io.Cancelable!void1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537//! An already-validated host name. A valid host name:
//! * Has length less than or equal to `max_len`.
//! * Is valid UTF-8.
//! * Lacks ASCII characters other than alphanumeric, '-', and '.'.
const HostName = @This();
const builtin = @import("builtin");
const native_os = builtin.os.tag;
const std = @import("../../std.zig");
const Io = std.Io;
const IpAddress = Io.net.IpAddress;
const Ip6Address = Io.net.Ip6Address;
const assert = std.debug.assert;
const Stream = Io.net.Stream;
/// Externally managed memory. Already checked to be valid.
bytes: []const u8,
/// The maximum number of bytes needed to store the text representation of
/// a max length host name, where labels are separated by dots, including
/// the trailing dot and the zero-length root label.
///
/// The max length of a host name is determined by its packet representation,
/// where each label has a length prefix of 1 octet, the root label has a
/// length of 0, and the maximum total number of octets is 255.
///
/// See [RFC 1035, Section 3.1](https://datatracker.ietf.org/doc/html/rfc1035#section-3.1)
pub const max_len = 254;
const max_len_without_root = max_len - 1;
pub const FromUriError = error{UriMissingHost} || ValidateError;
/// Returned `HostName.bytes` may point into `buffer` or `uri.host`.
pub fn fromUri(uri: std.Uri, buffer: *[HostName.max_len]u8) FromUriError!HostName {
const component = uri.host orelse return error.UriMissingHost;
const bytes = component.toRaw(buffer) catch |err| switch (err) {
error.NoSpaceLeft => return error.NameTooLong,
};
try validate(bytes);
return .{ .bytes = bytes };
}
pub const ValidateError = error{
NameTooLong,
InvalidHostName,
};
/// Validates a hostname according to [RFC 1123](https://www.rfc-editor.org/rfc/rfc1123)
pub fn validate(bytes: []const u8) ValidateError!void {
if (bytes.len == 0) return error.InvalidHostName;
// Ignore trailing dot (FQDN).
const end = if (bytes[bytes.len - 1] == '.') bytes.len - 1 else bytes.len;
// The accepted maximum length of a hostname, including labels and dots.
if (end > max_len_without_root) return error.NameTooLong;
// Hostnames are divided into dot-separated "labels", which:
//
// - Start with a letter or digit
// - Can contain letters, digits, or hyphens
// - Must end with a letter or digit
// - Have a minimum of 1 character and a maximum of 63
var label_len: usize = 0;
for (bytes[0..end], 0..) |c, i| {
switch (c) {
'.' => {
if (label_len == 0 or label_len > 63) return error.InvalidHostName;
if (!std.ascii.isAlphanumeric(bytes[i - 1])) return error.InvalidHostName;
label_len = 0;
},
'-' => {
if (label_len == 0) return error.InvalidHostName;
label_len += 1;
},
else => {
if (!std.ascii.isAlphanumeric(c)) return error.InvalidHostName;
label_len += 1;
},
}
}
// Validate the final label
if (label_len == 0 or label_len > 63) return error.InvalidHostName;
if (!std.ascii.isAlphanumeric(bytes[end - 1])) return error.InvalidHostName;
}
test validate {
// Valid hostnames
try validate("example");
try validate("example.com");
try validate("www.example.com");
try validate("sub.domain.example.com");
try validate("example.com.");
try validate("host-name.example.com.");
try validate("123.example.com.");
try validate("a-b.com");
try validate("a.b.c.d.e.f.g");
try validate("127.0.0.1"); // Also a valid hostname
const many_a: [63]u8 = @splat('a');
try validate(&many_a ++ ".com"); // Label exactly 63 chars (valid)
const many_a_dot_buf: [126][2]u8 = @splat(.{ 'a', '.' });
const many_a_dot: []const u8 = @ptrCast(&many_a_dot_buf);
try validate(many_a_dot ++ "a"); // Total length 253 (without the trailing dot)
try validate(many_a_dot ++ "a."); // Total length 254 (with the trailing dot)
// Invalid hostnames
try std.testing.expectError(error.InvalidHostName, validate(""));
try std.testing.expectError(error.InvalidHostName, validate(".example.com"));
try std.testing.expectError(error.InvalidHostName, validate("example.com.."));
try std.testing.expectError(error.InvalidHostName, validate("host..domain"));
try std.testing.expectError(error.InvalidHostName, validate("-hostname"));
try std.testing.expectError(error.InvalidHostName, validate("hostname-"));
try std.testing.expectError(error.InvalidHostName, validate("hostname-.com"));
try std.testing.expectError(error.InvalidHostName, validate("a.-.b"));
try std.testing.expectError(error.InvalidHostName, validate("host_name.com"));
try std.testing.expectError(error.InvalidHostName, validate("."));
try std.testing.expectError(error.InvalidHostName, validate(".."));
try std.testing.expectError(error.InvalidHostName, validate(&many_a ++ "a.com")); // Label length 64 (too long)
try std.testing.expectError(error.NameTooLong, validate(many_a_dot ++ "ab")); // Total length 254 (without the trailing dot)
try std.testing.expectError(error.NameTooLong, validate(many_a_dot ++ "ab.")); // Total length 255 (with the trailing dot)
}
pub fn init(bytes: []const u8) ValidateError!HostName {
try validate(bytes);
return .{ .bytes = bytes };
}
pub fn sameParentDomain(parent_host: HostName, child_host: HostName) bool {
const parent_bytes = parent_host.bytes;
const child_bytes = child_host.bytes;
if (!std.ascii.endsWithIgnoreCase(child_bytes, parent_bytes)) return false;
if (child_bytes.len == parent_bytes.len) return true;
if (parent_bytes.len > child_bytes.len) return false;
return child_bytes[child_bytes.len - parent_bytes.len - 1] == '.';
}
test sameParentDomain {
try std.testing.expect(!sameParentDomain(try .init("foo.com"), try .init("bar.com")));
try std.testing.expect(sameParentDomain(try .init("foo.com"), try .init("foo.com")));
try std.testing.expect(sameParentDomain(try .init("foo.com"), try .init("bar.foo.com")));
try std.testing.expect(!sameParentDomain(try .init("bar.foo.com"), try .init("foo.com")));
}
/// Domain names are case-insensitive (RFC 5890, Section 2.3.2.4)
pub fn eql(a: HostName, b: HostName) bool {
return std.ascii.eqlIgnoreCase(a.bytes, b.bytes);
}
pub const LookupOptions = struct {
port: u16,
/// `null` means either.
canonical_name_buffer: ?*[max_len]u8 = null,
family: ?IpAddress.Family = null,
};
pub const LookupError = error{
UnknownHostName,
ResolvConfParseFailed,
InvalidDnsARecord,
InvalidDnsAAAARecord,
InvalidDnsCnameRecord,
NameServerFailure,
NoAddressReturned,
/// Failed to open or read "/etc/hosts" or "/etc/resolv.conf".
DetectingNetworkConfigurationFailed,
} || IpAddress.BindError || Io.Cancelable;
pub const LookupResult = union(enum) {
address: IpAddress,
canonical_name: HostName,
};
/// Adds any number of `LookupResult.address` into `resolved`, and exactly one
/// `LookupResult.canonical_name`.
///
/// Guaranteed not to block if provided queue has capacity at least 16.
///
/// Closes `resolved` before return, even on error.
///
/// Asserts `resolved` is not closed until this call returns.
pub fn lookup(
host_name: HostName,
io: Io,
resolved: *Io.Queue(LookupResult),
options: LookupOptions,
) LookupError!void {
return io.vtable.netLookup(io.userdata, host_name, resolved, options);
}
pub const ExpandError = error{InvalidDnsPacket} || ValidateError;
/// Decompresses a DNS name.
///
/// Returns number of bytes consumed from `packet` starting at `i`,
/// along with the expanded `HostName`.
///
/// Asserts `buffer` is has length at least `max_len`.
pub fn expand(noalias packet: []const u8, start_i: usize, noalias dest_buffer: []u8) ExpandError!struct { usize, HostName } {
const dest = dest_buffer[0..max_len];
var i = start_i;
var dest_i: usize = 0;
var len: ?usize = null;
// Detect reference loop using an iteration counter.
for (0..packet.len / 2) |_| {
if (i >= packet.len) return error.InvalidDnsPacket;
const c = packet[i];
if ((c & 0xc0) != 0) {
if (i + 1 >= packet.len) return error.InvalidDnsPacket;
const j: usize = (@as(usize, c & 0x3F) << 8) | packet[i + 1];
if (j >= packet.len) return error.InvalidDnsPacket;
if (len == null) len = (i + 2) - start_i;
i = j;
} else if (c != 0) {
if (dest_i != 0) {
dest[dest_i] = '.';
dest_i += 1;
}
const label_len: usize = c;
if (i + 1 + label_len > packet.len) return error.InvalidDnsPacket;
if (dest_i + label_len + 1 > dest.len) return error.InvalidDnsPacket;
@memcpy(dest[dest_i..][0..label_len], packet[i + 1 ..][0..label_len]);
dest_i += label_len;
i += 1 + label_len;
} else {
return .{
len orelse i - start_i + 1,
try .init(dest[0..dest_i]),
};
}
}
return error.InvalidDnsPacket;
}
pub const DnsRecord = enum(u8) {
A = 1,
CNAME = 5,
AAAA = 28,
_,
};
pub const DnsResponse = struct {
bytes: []const u8,
bytes_index: u32,
answers_remaining: u16,
pub const Answer = struct {
rr: DnsRecord,
packet: []const u8,
data_off: u32,
data_len: u16,
};
pub const Error = error{InvalidDnsPacket};
pub fn init(r: []const u8) Error!DnsResponse {
if (r.len < 12) return error.InvalidDnsPacket;
if ((r[3] & 15) != 0) return .{ .bytes = r, .bytes_index = 3, .answers_remaining = 0 };
var i: u32 = 12;
var query_count = std.mem.readInt(u16, r[4..6], .big);
while (query_count != 0) : (query_count -= 1) {
while (i < r.len and r[i] -% 1 < 127) i += 1;
if (r.len - i < 6) return error.InvalidDnsPacket;
i = i + 5 + @intFromBool(r[i] != 0);
}
return .{
.bytes = r,
.bytes_index = i,
.answers_remaining = std.mem.readInt(u16, r[6..8], .big),
};
}
pub fn next(dr: *DnsResponse) Error!?Answer {
if (dr.answers_remaining == 0) return null;
dr.answers_remaining -= 1;
const r = dr.bytes;
var i = dr.bytes_index;
while (i < r.len and r[i] -% 1 < 127) i += 1;
if (r.len - i < 12) return error.InvalidDnsPacket;
i = i + 1 + @intFromBool(r[i] != 0);
const len = std.mem.readInt(u16, r[i + 8 ..][0..2], .big);
if (i + 10 + len > r.len) return error.InvalidDnsPacket;
defer dr.bytes_index = i + 10 + len;
return .{
.rr = @fromBackingInt(@intCast(r[i + 1])),
.packet = r,
.data_off = i + 10,
.data_len = len,
};
}
};
pub const ConnectError = LookupError || IpAddress.ConnectError;
pub fn connect(
host_name: HostName,
io: Io,
port: u16,
options: IpAddress.ConnectOptions,
) ConnectError!Stream {
var connect_many_buffer: [32]IpAddress.ConnectError!Stream = undefined;
var connect_many_queue: Io.Queue(IpAddress.ConnectError!Stream) = .init(&connect_many_buffer);
var connect_many = io.async(connectMany, .{ host_name, io, port, &connect_many_queue, options });
defer {
connect_many.cancel(io) catch {};
while (connect_many_queue.getOneUncancelable(io)) |loser| {
if (loser) |s| s.close(io) else |_| {}
} else |err| switch (err) {
error.Closed => {},
}
}
var ip_connect_error: ?IpAddress.ConnectError = null;
while (connect_many_queue.getOne(io)) |result| {
if (result) |stream| {
return stream;
} else |err| switch (err) {
error.Canceled => unreachable,
error.SystemResources,
error.OptionUnsupported,
error.ProcessFdQuotaExceeded,
error.SystemFdQuotaExceeded,
=> |e| return e,
error.WouldBlock => return error.Unexpected,
else => |e| ip_connect_error = e,
}
} else |err| switch (err) {
error.Canceled => |e| return e,
error.Closed => {
// There was no successful connection attempt. If there was a lookup error, return that.
try connect_many.await(io);
// Otherwise, return the error from a failed IP connection attempt.
return ip_connect_error orelse
return error.UnknownHostName;
},
}
}
/// Asynchronously establishes a connection to all IP addresses associated with
/// a host name, adding them to a results queue upon completion.
///
/// `error.Canceled` will never be added to the queue, but other errors may be.
///
/// Closes `results` before return, even on error.
///
/// Asserts `results` is not closed until this call returns.
pub fn connectMany(
host_name: HostName,
io: Io,
port: u16,
results: *Io.Queue(IpAddress.ConnectError!Stream),
options: IpAddress.ConnectOptions,
) LookupError!void {
defer results.close(io);
var canonical_name_buffer: [max_len]u8 = undefined;
var lookup_buffer: [32]HostName.LookupResult = undefined;
var lookup_queue: Io.Queue(LookupResult) = .init(&lookup_buffer);
var lookup_future = io.async(lookup, .{ host_name, io, &lookup_queue, .{
.port = port,
.canonical_name_buffer = &canonical_name_buffer,
} });
defer lookup_future.cancel(io) catch {};
var group: Io.Group = .init;
defer group.cancel(io);
while (lookup_queue.getOne(io)) |dns_result| switch (dns_result) {
.address => |address| group.async(io, enqueueConnection, .{ address, io, results, options }),
.canonical_name => continue,
} else |err| switch (err) {
error.Canceled => |e| return e,
error.Closed => {
try group.await(io);
return lookup_future.await(io);
},
}
}
fn enqueueConnection(
address: IpAddress,
io: Io,
queue: *Io.Queue(IpAddress.ConnectError!Stream),
options: IpAddress.ConnectOptions,
) Io.Cancelable!void {
const result = address.connect(io, options) catch |err| switch (err) {
error.Canceled => |e| return e,
else => |e| e, // other errors go in the result queue
};
errdefer if (result) |s| s.close(io) else |_| {};
queue.putOne(io, result) catch |err| switch (err) {
error.Canceled => |e| return e,
error.Closed => unreachable, // `queue` must not be closed
};
}
pub const ResolvConf = struct {
attempts: u32,
ndots: u32,
timeout_seconds: u32,
nameservers_buffer: [max_nameservers]IpAddress,
nameservers_len: usize,
search_buffer: [max_len]u8,
search_len: usize,
/// According to resolv.conf(5) there is a maximum of 3 nameservers in this
/// file.
pub const max_nameservers = 3;
/// Returns `error.StreamTooLong` if a line is longer than 512 bytes.
pub fn init(io: Io) !ResolvConf {
var rc: ResolvConf = .{
.nameservers_buffer = undefined,
.nameservers_len = 0,
.search_buffer = undefined,
.search_len = 0,
.ndots = 1,
.timeout_seconds = 5,
.attempts = 2,
};
const file = Io.Dir.openFileAbsolute(io, "/etc/resolv.conf", .{}) catch |err| switch (err) {
error.FileNotFound,
error.NotDir,
error.AccessDenied,
=> {
try addNumeric(&rc, io, "127.0.0.1", 53);
return rc;
},
else => |e| return e,
};
defer file.close(io);
var line_buf: [512]u8 = undefined;
var file_reader = file.reader(io, &line_buf);
parse(&rc, io, &file_reader.interface) catch |err| switch (err) {
error.ReadFailed => return file_reader.err.?,
else => |e| return e,
};
return rc;
}
const Directive = enum { options, nameserver, domain, search };
const Option = enum { ndots, attempts, timeout };
pub fn parse(rc: *ResolvConf, io: Io, reader: *Io.Reader) !void {
while (reader.takeSentinel('\n')) |line_with_comment| {
const line = line: {
var split = std.mem.splitScalar(u8, line_with_comment, '#');
break :line split.first();
};
var line_it = std.mem.tokenizeAny(u8, line, " \t");
const token = line_it.next() orelse continue;
switch (std.meta.stringToEnum(Directive, token) orelse continue) {
.options => while (line_it.next()) |sub_tok| {
var colon_it = std.mem.splitScalar(u8, sub_tok, ':');
const name = colon_it.first();
const value_txt = colon_it.next() orelse continue;
const value = std.fmt.parseInt(u8, value_txt, 10) catch |err| switch (err) {
error.Overflow => 255,
error.InvalidCharacter => continue,
};
switch (std.meta.stringToEnum(Option, name) orelse continue) {
.ndots => rc.ndots = @min(value, 15),
.attempts => rc.attempts = @min(value, 10),
.timeout => rc.timeout_seconds = @min(value, 60),
}
},
.nameserver => {
const ip_txt = line_it.next() orelse continue;
try addNumeric(rc, io, ip_txt, 53);
},
.domain, .search => {
const rest = line_it.rest();
@memcpy(rc.search_buffer[0..rest.len], rest);
rc.search_len = rest.len;
},
}
} else |err| switch (err) {
error.EndOfStream => if (reader.bufferedLen() != 0) return error.EndOfStream,
else => |e| return e,
}
if (rc.nameservers_len == 0) {
try addNumeric(rc, io, "127.0.0.1", 53);
}
}
fn addNumeric(rc: *ResolvConf, io: Io, name: []const u8, port: u16) !void {
if (rc.nameservers_len < rc.nameservers_buffer.len) {
rc.nameservers_buffer[rc.nameservers_len] = try .resolve(io, name, port);
rc.nameservers_len += 1;
}
}
pub fn nameservers(rc: *const ResolvConf) []const IpAddress {
return rc.nameservers_buffer[0..rc.nameservers_len];
}
};
test ResolvConf {
const input =
\\# Generated by resolvconf
\\nameserver 1.0.0.1
\\nameserver 1.1.1.1
\\nameserver fe80::e0e:76ff:fed4:cf22
\\options edns0
\\
;
var reader: Io.Reader = .fixed(input);
var rc: ResolvConf = .{
.nameservers_buffer = undefined,
.nameservers_len = 0,
.search_buffer = undefined,
.search_len = 0,
.ndots = 1,
.timeout_seconds = 5,
.attempts = 2,
};
try rc.parse(std.testing.io, &reader);
try std.testing.expectEqual(3, rc.nameservers().len);
}
An already-validated host name. A valid host name:
max_len.