| ... | @@ -702,8 +702,10 @@ pub const AddressList = struct { | ... | @@ -702,8 +702,10 @@ pub const AddressList = struct { |
| 702 | } | 702 | } |
| 703 | }; | 703 | }; |
| 704 | | 704 | |
| | 705 | pub const TcpConnectToHostError = GetAddressListError || TcpConnectToAddressError; |
| | 706 | |
| 705 | /// All memory allocated with `allocator` will be freed before this function returns. | 707 | /// All memory allocated with `allocator` will be freed before this function returns. |
| 706 | pub fn tcpConnectToHost(allocator: mem.Allocator, name: []const u8, port: u16) !Stream { | 708 | pub fn tcpConnectToHost(allocator: mem.Allocator, name: []const u8, port: u16) TcpConnectToHostError!Stream { |
| 707 | const list = try getAddressList(allocator, name, port); | 709 | const list = try getAddressList(allocator, name, port); |
| 708 | defer list.deinit(); | 710 | defer list.deinit(); |
| 709 | | 711 | |
| ... | @@ -720,7 +722,9 @@ pub fn tcpConnectToHost(allocator: mem.Allocator, name: []const u8, port: u16) ! | ... | @@ -720,7 +722,9 @@ pub fn tcpConnectToHost(allocator: mem.Allocator, name: []const u8, port: u16) ! |
| 720 | return std.os.ConnectError.ConnectionRefused; | 722 | return std.os.ConnectError.ConnectionRefused; |
| 721 | } | 723 | } |
| 722 | | 724 | |
| 723 | pub fn tcpConnectToAddress(address: Address) !Stream { | 725 | pub const TcpConnectToAddressError = std.os.SocketError || std.os.ConnectError; |
| | 726 | |
| | 727 | pub fn tcpConnectToAddress(address: Address) TcpConnectToAddressError!Stream { |
| 724 | const nonblock = if (std.io.is_async) os.SOCK.NONBLOCK else 0; | 728 | const nonblock = if (std.io.is_async) os.SOCK.NONBLOCK else 0; |
| 725 | const sock_flags = os.SOCK.STREAM | nonblock | | 729 | const sock_flags = os.SOCK.STREAM | nonblock | |
| 726 | (if (builtin.target.os.tag == .windows) 0 else os.SOCK.CLOEXEC); | 730 | (if (builtin.target.os.tag == .windows) 0 else os.SOCK.CLOEXEC); |
| ... | @@ -737,8 +741,32 @@ pub fn tcpConnectToAddress(address: Address) !Stream { | ... | @@ -737,8 +741,32 @@ pub fn tcpConnectToAddress(address: Address) !Stream { |
| 737 | return Stream{ .handle = sockfd }; | 741 | return Stream{ .handle = sockfd }; |
| 738 | } | 742 | } |
| 739 | | 743 | |
| | 744 | const GetAddressListError = std.mem.Allocator.Error || std.fs.File.OpenError || std.fs.File.ReadError || std.os.SocketError || std.os.BindError || error { |
| | 745 | // TODO: break this up into error sets from the various underlying functions |
| | 746 | |
| | 747 | TemporaryNameServerFailure, |
| | 748 | NameServerFailure, |
| | 749 | AddressFamilyNotSupported, |
| | 750 | UnknownHostName, |
| | 751 | ServiceUnavailable, |
| | 752 | Unexpected, |
| | 753 | |
| | 754 | HostLacksNetworkAddresses, |
| | 755 | |
| | 756 | InvalidCharacter, |
| | 757 | InvalidEnd, |
| | 758 | NonCanonical, |
| | 759 | Overflow, |
| | 760 | Incomplete, |
| | 761 | InvalidIpv4Mapping, |
| | 762 | InvalidIPAddressFormat, |
| | 763 | |
| | 764 | InterfaceNotFound, |
| | 765 | FileSystem, |
| | 766 | }; |
| | 767 | |
| 740 | /// Call `AddressList.deinit` on the result. | 768 | /// Call `AddressList.deinit` on the result. |
| 741 | pub fn getAddressList(allocator: mem.Allocator, name: []const u8, port: u16) !*AddressList { | 769 | pub fn getAddressList(allocator: mem.Allocator, name: []const u8, port: u16) GetAddressListError!*AddressList { |
| 742 | const result = blk: { | 770 | const result = blk: { |
| 743 | var arena = std.heap.ArenaAllocator.init(allocator); | 771 | var arena = std.heap.ArenaAllocator.init(allocator); |
| 744 | errdefer arena.deinit(); | 772 | errdefer arena.deinit(); |