authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-23 00:12:16-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-02 16:30:59-07:00
log3e8cc9c4960043e87cd7fe3a12958d7b1e6a16e8
tree2bbb1814b54da6920536c8c781a4782dea8a8612
parent3e828b02bdcb0d63c47a3f1bbe7333af85a5b9a3

std.Io.net: progress towards DNS resolution


4 files changed, 151 insertions(+), 27 deletions(-)

lib/std/Io.zig+3-1
......@@ -664,10 +664,12 @@ pub const VTable = struct {
664664 sleep: *const fn (?*anyopaque, clockid: std.posix.clockid_t, deadline: Deadline) SleepError!void,
665665
666666 listen: *const fn (?*anyopaque, address: net.IpAddress, options: net.ListenOptions) net.ListenError!net.Server,
667 bind: *const fn (?*anyopaque, address: net.IpAddress, options: net.BindOptions) net.BindError!net.Socket,
667668 accept: *const fn (?*anyopaque, server: *net.Server) net.Server.AcceptError!net.Server.Connection,
669 netSend: *const fn (?*anyopaque, address: net.IpAddress, data: []const []const u8) net.SendError!void,
668670 netRead: *const fn (?*anyopaque, src: net.Stream, data: [][]u8) net.Stream.Reader.Error!usize,
669671 netWrite: *const fn (?*anyopaque, dest: net.Stream, header: []const u8, data: []const []const u8, splat: usize) net.Stream.Writer.Error!usize,
670 netClose: *const fn (?*anyopaque, stream: net.Stream) void,
672 netClose: *const fn (?*anyopaque, socket: net.Socket) void,
671673 netInterfaceNameResolve: *const fn (?*anyopaque, *const net.Interface.Name) net.Interface.Name.ResolveError!net.Interface,
672674 netInterfaceName: *const fn (?*anyopaque, net.Interface) net.Interface.NameError!net.Interface.Name,
673675};
lib/std/Io/net.zig+71-9
......@@ -8,6 +8,8 @@ pub const HostName = @import("net/HostName.zig");
88
99pub const ListenError = std.net.Address.ListenError || Io.Cancelable;
1010
11pub const BindError = std.net.Address.BindError || Io.Cancelable;
12
1113pub const ListenOptions = struct {
1214 /// How many connections the kernel will accept on the application's behalf.
1315 /// If more than this many connections pool in the kernel, clients will start
......@@ -19,6 +21,13 @@ pub const ListenOptions = struct {
1921 force_nonblocking: bool = false,
2022};
2123
24pub const BindOptions = struct {
25 /// The socket is restricted to sending and receiving IPv6 packets only.
26 /// In this case, an IPv4 and an IPv6 application can bind to a single port
27 /// at the same time.
28 ip6_only: bool = false,
29};
30
2231pub const IpAddress = union(enum) {
2332 ip4: Ip4Address,
2433 ip6: Ip6Address,
......@@ -123,10 +132,21 @@ pub const IpAddress = union(enum) {
123132 };
124133 }
125134
126 /// The returned `Server` has an open `stream`.
135 /// Waits for a TCP connection. When using this API, `bind` does not need
136 /// to be called. The returned `Server` has an open `stream`.
127137 pub fn listen(address: IpAddress, io: Io, options: ListenOptions) ListenError!Server {
128138 return io.vtable.listen(io.userdata, address, options);
129139 }
140
141 /// Associates an address with a `Socket` which can be used to receive UDP
142 /// packets and other kinds of non-streaming messages. See `listen` for a
143 /// streaming alternative.
144 ///
145 /// One bound `Socket` can be used to receive messages from multiple
146 /// different addresses.
147 pub fn bind(address: IpAddress, io: Io, options: BindOptions) BindError!Socket {
148 return io.vtable.bind(io.userdata, address, options);
149 }
130150};
131151
132152/// An IPv4 address in binary memory layout.
......@@ -141,6 +161,13 @@ pub const Ip4Address = struct {
141161 };
142162 }
143163
164 pub fn unspecified(port: u16) Ip4Address {
165 return .{
166 .bytes = .{ 0, 0, 0, 0 },
167 .port = port,
168 };
169 }
170
144171 pub const ParseError = error{
145172 Overflow,
146173 InvalidEnd,
......@@ -217,6 +244,31 @@ pub const Ip6Address = struct {
217244 };
218245 }
219246
247 pub fn unspecified(port: u16) Ip6Address {
248 return .{
249 .bytes = .{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
250 .port = port,
251 };
252 }
253
254 /// Constructs an IPv4-mapped IPv6 address.
255 pub fn fromIp4(ip4: Ip4Address) Ip6Address {
256 const b = &ip4.bytes;
257 return .{
258 .bytes = .{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, b[0], b[1], b[2], b[3] },
259 .port = ip4.port,
260 };
261 }
262
263 /// Given an `IpAddress`, converts it to an `Ip6Address` directly, or via
264 /// constructing an IPv4-mapped IPv6 address.
265 pub fn fromAny(addr: IpAddress) Ip6Address {
266 return switch (addr) {
267 .ip4 => |ip4| fromIp4(ip4),
268 .ip6 => |ip6| ip6,
269 };
270 }
271
220272 /// An IPv6 address but with `Interface` as a name rather than index.
221273 pub const Unresolved = struct {
222274 /// Big endian
......@@ -626,11 +678,11 @@ pub const Interface = struct {
626678 }
627679};
628680
629/// An open socket connection with a network protocol that guarantees
630/// sequencing, delivery, and prevents repetition. Typically TCP or UNIX domain
631/// socket.
632pub const Stream = struct {
681/// An open port with unspecified protocol.
682pub const Socket = struct {
633683 handle: Handle,
684 /// Contains the resolved ephemeral port number if requested.
685 bind_address: IpAddress,
634686
635687 /// Underlying platform-defined type which may or may not be
636688 /// interchangeable with a file system file descriptor.
......@@ -639,8 +691,19 @@ pub const Stream = struct {
639691 else => std.posix.fd_t,
640692 };
641693
694 pub fn close(s: Socket, io: Io) void {
695 return io.vtable.netClose(io.userdata, s);
696 }
697};
698
699/// An open socket connection with a network protocol that guarantees
700/// sequencing, delivery, and prevents repetition. Typically TCP or UNIX domain
701/// socket.
702pub const Stream = struct {
703 socket: Socket,
704
642705 pub fn close(s: Stream, io: Io) void {
643 return io.vtable.close(io.userdata, s);
706 return io.vtable.netClose(io.userdata, s.socket);
644707 }
645708
646709 pub const Reader = struct {
......@@ -719,8 +782,7 @@ pub const Stream = struct {
719782};
720783
721784pub const Server = struct {
722 listen_address: IpAddress,
723 stream: Stream,
785 socket: Socket,
724786
725787 pub const Connection = struct {
726788 stream: Stream,
......@@ -728,7 +790,7 @@ pub const Server = struct {
728790 };
729791
730792 pub fn deinit(s: *Server, io: Io) void {
731 s.stream.close(io);
793 s.socket.close(io);
732794 s.* = undefined;
733795 }
734796
lib/std/Io/net/HostName.zig+71-17
......@@ -218,6 +218,11 @@ fn lookupDnsSearch(host_name: HostName, io: Io, options: LookupOptions) !LookupR
218218 return lookupDns(io, lookup_canon_name, &rc, options);
219219}
220220
221const DnsReply = struct {
222 buf: [512]u8,
223 len: usize,
224};
225
221226fn lookupDns(io: Io, lookup_canon_name: []const u8, rc: *const ResolvConf, options: LookupOptions) !LookupResult {
222227 const family_records: [2]struct { af: IpAddress.Family, rr: u8 } = .{
223228 .{ .af = .ip6, .rr = std.posix.RR.A },
......@@ -225,21 +230,21 @@ fn lookupDns(io: Io, lookup_canon_name: []const u8, rc: *const ResolvConf, optio
225230 };
226231 var query_buffers: [2][280]u8 = undefined;
227232 var queries_buffer: [2][]const u8 = undefined;
228 var answer_buffers: [2][512]u8 = undefined;
229 var answers_buffer: [2][]u8 = .{ &answer_buffers[0], &answer_buffers[1] };
230233 var nq: usize = 0;
231234
232235 for (family_records) |fr| {
233236 if (options.family != fr.af) {
234 const len = writeResolutionQuery(&query_buffers[nq], 0, lookup_canon_name, 1, fr.rr);
237 const entropy = std.crypto.random.array(u8, 2);
238 const len = writeResolutionQuery(&query_buffers[nq], 0, lookup_canon_name, 1, fr.rr, entropy);
235239 queries_buffer[nq] = query_buffers[nq][0..len];
236240 nq += 1;
237241 }
238242 }
239243
240244 const queries = queries_buffer[0..nq];
241 const replies = answers_buffer[0..nq];
242 try rc.sendMessage(io, queries, replies);
245 var replies_buffer: [2]DnsReply = undefined;
246 var replies: Io.Queue(DnsReply) = .init(&replies_buffer);
247 try rc.sendMessage(io, queries, &replies);
243248
244249 for (replies) |reply| {
245250 if (reply.len < 4 or (reply[3] & 15) == 2) return error.TemporaryNameServerFailure;
......@@ -391,7 +396,7 @@ fn copyCanon(canonical_name_buffer: *[max_len]u8, name: []const u8) HostName {
391396}
392397
393398/// Writes DNS resolution query packet data to `w`; at most 280 bytes.
394fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u8) usize {
399fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u8, entropy: [2]u8) usize {
395400 // This implementation is ported from musl libc.
396401 // A more idiomatic "ziggy" implementation would be welcome.
397402 var name = dname;
......@@ -400,7 +405,8 @@ fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u
400405 const n = 17 + name.len + @intFromBool(name.len != 0);
401406
402407 // Construct query template - ID will be filled later
403 @memset(q[0..n], 0);
408 q[0..2].* = entropy;
409 @memset(q[2..n], 0);
404410 q[2] = @as(u8, op) * 8 + 1;
405411 q[5] = 1;
406412 @memcpy(q[13..][0..name.len], name);
......@@ -416,8 +422,6 @@ fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u
416422 }
417423 q[i + 1] = ty;
418424 q[i + 3] = class;
419
420 std.crypto.random.bytes(q[0..2]);
421425 return n;
422426}
423427
......@@ -519,12 +523,14 @@ pub fn connectTcp(host_name: HostName, io: Io, port: u16) ConnectTcpError!Stream
519523pub const ResolvConf = struct {
520524 attempts: u32,
521525 ndots: u32,
522 timeout: u32,
523 nameservers_buffer: [3]IpAddress,
526 timeout: Io.Duration,
527 nameservers_buffer: [max_nameservers]IpAddress,
524528 nameservers_len: usize,
525529 search_buffer: [max_len]u8,
526530 search_len: usize,
527531
532 pub const max_nameservers = 3;
533
528534 /// Returns `error.StreamTooLong` if a line is longer than 512 bytes.
529535 fn init(io: Io) !ResolvConf {
530536 var rc: ResolvConf = .{
......@@ -620,13 +626,61 @@ pub const ResolvConf = struct {
620626 rc: *const ResolvConf,
621627 io: Io,
622628 queries: []const []const u8,
623 answers: [][]u8,
629 replies: *Io.Queue(DnsReply),
624630 ) !void {
625 _ = rc;
626 _ = io;
627 _ = queries;
628 _ = answers;
629 @panic("TODO");
631 var ip4_mapped: [ResolvConf.max_nameservers]IpAddress = undefined;
632 var any_ip6 = false;
633 for (rc.nameservers(), &ip4_mapped) |*ns, *m| {
634 m.* = .{ .ip6 = .fromAny(ns.*) };
635 any_ip6 = any_ip6 or ns.* == .ip6;
636 }
637
638 const socket = s: {
639 if (any_ip6) ip6: {
640 const ip6_addr: IpAddress = .{ .ip6 = .unspecified(0) };
641 const socket = ip6_addr.bind(io, .{ .ip6_only = true }) catch |err| switch (err) {
642 error.AddressFamilyNotSupported => break :ip6,
643 };
644 break :s socket;
645 }
646 any_ip6 = false;
647 const ip4_addr: IpAddress = .{ .ip4 = .unspecified(0) };
648 const socket = try ip4_addr.bind(io, .{});
649 break :s socket;
650 };
651 defer socket.close();
652
653 const mapped_nameservers = if (any_ip6) ip4_mapped[0..rc.nameservers_len] else rc.nameservers();
654
655 var group: Io.Group = .{};
656 defer group.cancel();
657
658 for (queries) |query| {
659 for (mapped_nameservers) |*ns| {
660 group.async(sendOneMessage, .{ io, query, ns });
661 }
662 }
663
664 const deadline: Io.Deadline = .fromDuration(rc.timeout);
665
666 for (0..queries.len) |_| {
667 const msg = socket.receiveDeadline(deadline) catch |err| switch (err) {
668 error.Timeout => return error.Timeout,
669 error.Canceled => return error.Canceled,
670 else => continue,
671 };
672 _ = msg;
673 _ = replies;
674 @panic("TODO check msg for dns reply and put into replies queue");
675 }
676 }
677
678 fn sendOneMessage(
679 io: Io,
680 query: []const u8,
681 ns: *const IpAddress,
682 ) void {
683 io.vtable.netSend(io.userdata, ns.*, &.{query}) catch |err| switch (err) {};
630684 }
631685};
632686
lib/std/Random.zig+6
......@@ -58,6 +58,12 @@ pub fn bytes(r: Random, buf: []u8) void {
5858 r.fillFn(r.ptr, buf);
5959}
6060
61pub fn array(r: Random, comptime E: type, comptime N: usize) [N]E {
62 var result: [N]E = undefined;
63 bytes(r, &result);
64 return result;
65}
66
6167pub fn boolean(r: Random) bool {
6268 return r.int(u1) != 0;
6369}