hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 92e8ce06c1b01767c253c47ce1af9225bff4116f
parent d1c106b9410e7f4128f7c4e6d2db80d6448dcf25
Author: Pierre Curto <pierre.curto@gmail.com>
Date:   Wed, 31 Aug 2022 16:20:22 +0200

net::dial: handle IPv6 address with port

Fixes: https://todo.sr.ht/~sircmpwn/hare/454

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

Diffstat:
Mnet/dial/resolve.ha | 48+++++++++++++++++++++++++++++++++---------------
1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/net/dial/resolve.ha b/net/dial/resolve.ha @@ -18,24 +18,42 @@ export fn resolve( addr: str, service: str, ) (([]ip::addr, u16) | error) = { - let port = match (strings::index(addr, ':')) { - case void => - yield match (strconv::stou16(service)) { - case let u: u16 => - yield u; - case => - yield 0u16; - }; - case let i: size => - const sub = strings::sub(addr, i + 1, strings::end); - addr = strings::sub(addr, 0, i); - yield match (strconv::stou16(sub)) { - case let u: u16 => - yield u; - case => + let port = if (strings::hasprefix(addr, '[')) { + // [::1]:80 + yield match (strings::index(addr, "]:")) { + case let i: size => + const sub = strings::sub(addr, i + 2, strings::end); + addr = strings::sub(addr, 1, i); + yield match (strconv::stou16(sub)) { + case let u: u16 => + yield u; + case => + return invalid_address; + }; + case void => return invalid_address; }; + } else { + yield match (strings::index(addr, ':')) { + case void => + yield match (strconv::stou16(service)) { + case let u: u16 => + yield u; + case => + yield 0u16; + }; + case let i: size => + const sub = strings::sub(addr, i + 1, strings::end); + addr = strings::sub(addr, 0, i); + yield match (strconv::stou16(sub)) { + case let u: u16 => + yield u; + case => + return invalid_address; + }; + }; }; + if (service == "unknown" && port == 0) { return unknown_service; };