feature. See also
. The project being documented here (as the example) is the Zig library itself.
Threaded.lookupDnsSearch
fn lookupDnsSearch(
t: *Threaded,
host_name: HostName,
resolved: *Io.Queue(HostName.LookupResult),
options: HostName.LookupOptions,
) (HostName.LookupError || Io.QueueClosedError)!void
File
Code
fn lookupDnsSearch(
t: *Threaded,
host_name: HostName,
resolved: *Io.Queue(HostName.LookupResult),
options: HostName.LookupOptions,
) (HostName.LookupError || Io.QueueClosedError)!void {
const t_io = io(t);
const rc = HostName.ResolvConf.init(t_io) catch return error.ResolvConfParseFailed;
// a dot, which is an explicit request for global scope.
const dots = std.mem.countScalar(u8, host_name.bytes, '.');
const search_len = if (dots >= rc.ndots or std.mem.endsWith(u8, host_name.bytes, ".")) 0 else rc.search_len;
const search = rc.search_buffer[0..search_len];
var canon_name = host_name.bytes;
if (std.mem.endsWith(u8, canon_name, ".")) canon_name.len -= 1;
if (std.mem.endsWith(u8, canon_name, ".")) return error.UnknownHostName;
// both provides the desired default canonical name (if the requested
// name is not a CNAME record) and serves as a buffer for passing the
// full requested name to `lookupDns`.
var local_buf: [HostName.max_len]u8 = undefined;
const canon_buf = options.canonical_name_buffer orelse &local_buf;
@memcpy(canon_buf[0..canon_name.len], canon_name);
canon_buf[canon_name.len] = '.';
var it = std.mem.tokenizeAny(u8, search, " \t");
while (it.next()) |token| {
@memcpy(canon_buf[canon_name.len + 1 ..][0..token.len], token);
const lookup_canon_name = canon_buf[0 .. canon_name.len + 1 + token.len];
if (t.lookupDns(lookup_canon_name, &rc, resolved, options)) |result| {
return result;
} else |err| switch (err) {
error.UnknownHostName, error.NoAddressReturned => continue,
else => |e| return e,
}
}
const lookup_canon_name = canon_buf[0..canon_name.len];
return t.lookupDns(lookup_canon_name, &rc, resolved, options);
}