| author | |
| committer | |
| log | 3e8cc9c4960043e87cd7fe3a12958d7b1e6a16e8 |
| tree | 2bbb1814b54da6920536c8c781a4782dea8a8612 |
| parent | 3e828b02bdcb0d63c47a3f1bbe7333af85a5b9a3 |
4 files changed, 151 insertions(+), 27 deletions(-)
lib/std/Io.zig+3-1| ... | ... | @@ -664,10 +664,12 @@ pub const VTable = struct { |
| 664 | 664 | sleep: *const fn (?*anyopaque, clockid: std.posix.clockid_t, deadline: Deadline) SleepError!void, |
| 665 | 665 | |
| 666 | 666 | 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, | |
| 667 | 668 | 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, | |
| 668 | 670 | netRead: *const fn (?*anyopaque, src: net.Stream, data: [][]u8) net.Stream.Reader.Error!usize, |
| 669 | 671 | 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, | |
| 671 | 673 | netInterfaceNameResolve: *const fn (?*anyopaque, *const net.Interface.Name) net.Interface.Name.ResolveError!net.Interface, |
| 672 | 674 | netInterfaceName: *const fn (?*anyopaque, net.Interface) net.Interface.NameError!net.Interface.Name, |
| 673 | 675 | }; |
lib/std/Io/net.zig+71-9| ... | ... | @@ -8,6 +8,8 @@ pub const HostName = @import("net/HostName.zig"); |
| 8 | 8 | |
| 9 | 9 | pub const ListenError = std.net.Address.ListenError || Io.Cancelable; |
| 10 | 10 | |
| 11 | pub const BindError = std.net.Address.BindError || Io.Cancelable; | |
| 12 | ||
| 11 | 13 | pub const ListenOptions = struct { |
| 12 | 14 | /// How many connections the kernel will accept on the application's behalf. |
| 13 | 15 | /// If more than this many connections pool in the kernel, clients will start |
| ... | ... | @@ -19,6 +21,13 @@ pub const ListenOptions = struct { |
| 19 | 21 | force_nonblocking: bool = false, |
| 20 | 22 | }; |
| 21 | 23 | |
| 24 | pub 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 | ||
| 22 | 31 | pub const IpAddress = union(enum) { |
| 23 | 32 | ip4: Ip4Address, |
| 24 | 33 | ip6: Ip6Address, |
| ... | ... | @@ -123,10 +132,21 @@ pub const IpAddress = union(enum) { |
| 123 | 132 | }; |
| 124 | 133 | } |
| 125 | 134 | |
| 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`. | |
| 127 | 137 | pub fn listen(address: IpAddress, io: Io, options: ListenOptions) ListenError!Server { |
| 128 | 138 | return io.vtable.listen(io.userdata, address, options); |
| 129 | 139 | } |
| 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 | } | |
| 130 | 150 | }; |
| 131 | 151 | |
| 132 | 152 | /// An IPv4 address in binary memory layout. |
| ... | ... | @@ -141,6 +161,13 @@ pub const Ip4Address = struct { |
| 141 | 161 | }; |
| 142 | 162 | } |
| 143 | 163 | |
| 164 | pub fn unspecified(port: u16) Ip4Address { | |
| 165 | return .{ | |
| 166 | .bytes = .{ 0, 0, 0, 0 }, | |
| 167 | .port = port, | |
| 168 | }; | |
| 169 | } | |
| 170 | ||
| 144 | 171 | pub const ParseError = error{ |
| 145 | 172 | Overflow, |
| 146 | 173 | InvalidEnd, |
| ... | ... | @@ -217,6 +244,31 @@ pub const Ip6Address = struct { |
| 217 | 244 | }; |
| 218 | 245 | } |
| 219 | 246 | |
| 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 | ||
| 220 | 272 | /// An IPv6 address but with `Interface` as a name rather than index. |
| 221 | 273 | pub const Unresolved = struct { |
| 222 | 274 | /// Big endian |
| ... | ... | @@ -626,11 +678,11 @@ pub const Interface = struct { |
| 626 | 678 | } |
| 627 | 679 | }; |
| 628 | 680 | |
| 629 | /// An open socket connection with a network protocol that guarantees | |
| 630 | /// sequencing, delivery, and prevents repetition. Typically TCP or UNIX domain | |
| 631 | /// socket. | |
| 632 | pub const Stream = struct { | |
| 681 | /// An open port with unspecified protocol. | |
| 682 | pub const Socket = struct { | |
| 633 | 683 | handle: Handle, |
| 684 | /// Contains the resolved ephemeral port number if requested. | |
| 685 | bind_address: IpAddress, | |
| 634 | 686 | |
| 635 | 687 | /// Underlying platform-defined type which may or may not be |
| 636 | 688 | /// interchangeable with a file system file descriptor. |
| ... | ... | @@ -639,8 +691,19 @@ pub const Stream = struct { |
| 639 | 691 | else => std.posix.fd_t, |
| 640 | 692 | }; |
| 641 | 693 | |
| 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. | |
| 702 | pub const Stream = struct { | |
| 703 | socket: Socket, | |
| 704 | ||
| 642 | 705 | 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); | |
| 644 | 707 | } |
| 645 | 708 | |
| 646 | 709 | pub const Reader = struct { |
| ... | ... | @@ -719,8 +782,7 @@ pub const Stream = struct { |
| 719 | 782 | }; |
| 720 | 783 | |
| 721 | 784 | pub const Server = struct { |
| 722 | listen_address: IpAddress, | |
| 723 | stream: Stream, | |
| 785 | socket: Socket, | |
| 724 | 786 | |
| 725 | 787 | pub const Connection = struct { |
| 726 | 788 | stream: Stream, |
| ... | ... | @@ -728,7 +790,7 @@ pub const Server = struct { |
| 728 | 790 | }; |
| 729 | 791 | |
| 730 | 792 | pub fn deinit(s: *Server, io: Io) void { |
| 731 | s.stream.close(io); | |
| 793 | s.socket.close(io); | |
| 732 | 794 | s.* = undefined; |
| 733 | 795 | } |
| 734 | 796 |
lib/std/Io/net/HostName.zig+71-17| ... | ... | @@ -218,6 +218,11 @@ fn lookupDnsSearch(host_name: HostName, io: Io, options: LookupOptions) !LookupR |
| 218 | 218 | return lookupDns(io, lookup_canon_name, &rc, options); |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | const DnsReply = struct { | |
| 222 | buf: [512]u8, | |
| 223 | len: usize, | |
| 224 | }; | |
| 225 | ||
| 221 | 226 | fn lookupDns(io: Io, lookup_canon_name: []const u8, rc: *const ResolvConf, options: LookupOptions) !LookupResult { |
| 222 | 227 | const family_records: [2]struct { af: IpAddress.Family, rr: u8 } = .{ |
| 223 | 228 | .{ .af = .ip6, .rr = std.posix.RR.A }, |
| ... | ... | @@ -225,21 +230,21 @@ fn lookupDns(io: Io, lookup_canon_name: []const u8, rc: *const ResolvConf, optio |
| 225 | 230 | }; |
| 226 | 231 | var query_buffers: [2][280]u8 = undefined; |
| 227 | 232 | 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] }; | |
| 230 | 233 | var nq: usize = 0; |
| 231 | 234 | |
| 232 | 235 | for (family_records) |fr| { |
| 233 | 236 | 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); | |
| 235 | 239 | queries_buffer[nq] = query_buffers[nq][0..len]; |
| 236 | 240 | nq += 1; |
| 237 | 241 | } |
| 238 | 242 | } |
| 239 | 243 | |
| 240 | 244 | 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); | |
| 243 | 248 | |
| 244 | 249 | for (replies) |reply| { |
| 245 | 250 | 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 { |
| 391 | 396 | } |
| 392 | 397 | |
| 393 | 398 | /// Writes DNS resolution query packet data to `w`; at most 280 bytes. |
| 394 | fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u8) usize { | |
| 399 | fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u8, entropy: [2]u8) usize { | |
| 395 | 400 | // This implementation is ported from musl libc. |
| 396 | 401 | // A more idiomatic "ziggy" implementation would be welcome. |
| 397 | 402 | var name = dname; |
| ... | ... | @@ -400,7 +405,8 @@ fn writeResolutionQuery(q: *[280]u8, op: u4, dname: []const u8, class: u8, ty: u |
| 400 | 405 | const n = 17 + name.len + @intFromBool(name.len != 0); |
| 401 | 406 | |
| 402 | 407 | // 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); | |
| 404 | 410 | q[2] = @as(u8, op) * 8 + 1; |
| 405 | 411 | q[5] = 1; |
| 406 | 412 | @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 |
| 416 | 422 | } |
| 417 | 423 | q[i + 1] = ty; |
| 418 | 424 | q[i + 3] = class; |
| 419 | ||
| 420 | std.crypto.random.bytes(q[0..2]); | |
| 421 | 425 | return n; |
| 422 | 426 | } |
| 423 | 427 | |
| ... | ... | @@ -519,12 +523,14 @@ pub fn connectTcp(host_name: HostName, io: Io, port: u16) ConnectTcpError!Stream |
| 519 | 523 | pub const ResolvConf = struct { |
| 520 | 524 | attempts: u32, |
| 521 | 525 | ndots: u32, |
| 522 | timeout: u32, | |
| 523 | nameservers_buffer: [3]IpAddress, | |
| 526 | timeout: Io.Duration, | |
| 527 | nameservers_buffer: [max_nameservers]IpAddress, | |
| 524 | 528 | nameservers_len: usize, |
| 525 | 529 | search_buffer: [max_len]u8, |
| 526 | 530 | search_len: usize, |
| 527 | 531 | |
| 532 | pub const max_nameservers = 3; | |
| 533 | ||
| 528 | 534 | /// Returns `error.StreamTooLong` if a line is longer than 512 bytes. |
| 529 | 535 | fn init(io: Io) !ResolvConf { |
| 530 | 536 | var rc: ResolvConf = .{ |
| ... | ... | @@ -620,13 +626,61 @@ pub const ResolvConf = struct { |
| 620 | 626 | rc: *const ResolvConf, |
| 621 | 627 | io: Io, |
| 622 | 628 | queries: []const []const u8, |
| 623 | answers: [][]u8, | |
| 629 | replies: *Io.Queue(DnsReply), | |
| 624 | 630 | ) !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) {}; | |
| 630 | 684 | } |
| 631 | 685 | }; |
| 632 | 686 |
lib/std/Random.zig+6| ... | ... | @@ -58,6 +58,12 @@ pub fn bytes(r: Random, buf: []u8) void { |
| 58 | 58 | r.fillFn(r.ptr, buf); |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | pub 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 | ||
| 61 | 67 | pub fn boolean(r: Random) bool { |
| 62 | 68 | return r.int(u1) != 0; |
| 63 | 69 | } |