authorgravatar for ian.simonson@protonmail.comIan Simonson <ian.simonson@protonmail.com> 2020-06-07 21:04:15+10:00
committergravatar for ian.simonson@protonmail.comIan Simonson <ian.simonson@protonmail.com> 2020-06-07 22:39:35+10:00
loga6d1ef64d753f8be59d68695f927e03777bb6514
tree2e88fe63dc386fab3afe04773c9d520a57ac6196
parent983f93c84061c667b9286074428d0a0aecaddb15

tcpConnectToHost try all addresses in AddressList

The AddressList returned can contain more than one item e.g. the ipv4 and ipv6 addresses for a given hostname. Previously if a server had multiple addresses but was not listening on one of them Zig would give up immediately. Now on std.os.ConnectError.ConnectionRefused Zig will try the next address in the list. Zig still gives up on all other errors as they are related to the system and system resources rather than whether the remote server is listening on a particular address.

1 files changed, 9 insertions(+), 1 deletions(-)

lib/std/net.zig+9-1
...@@ -573,7 +573,15 @@ pub fn tcpConnectToHost(allocator: *mem.Allocator, name: []const u8, port: u16)...@@ -573,7 +573,15 @@ pub fn tcpConnectToHost(allocator: *mem.Allocator, name: []const u8, port: u16)
573573
574 if (list.addrs.len == 0) return error.UnknownHostName;574 if (list.addrs.len == 0) return error.UnknownHostName;
575575
576 return tcpConnectToAddress(list.addrs[0]);576 for (list.addrs) |addr| {
577 return tcpConnectToAddress(addr) catch |err| switch (err) {
578 error.ConnectionRefused => {
579 continue;
580 },
581 else => return err,
582 };
583 }
584 return std.os.ConnectError.ConnectionRefused;
577}585}
578586
579pub fn tcpConnectToAddress(address: Address) !fs.File {587pub fn tcpConnectToAddress(address: Address) !fs.File {