~sircmpwn/hare-dev

hare: net::dial: Fix address parsing v1 NEEDS REVISION

Lassi Pulkkinen: 1
 net::dial: Fix address parsing

 1 files changed, 24 insertions(+), 33 deletions(-)
#941031 alpine.yml success
#941032 freebsd.yml success
Slight correction: the service parameter is also meant to be used for
SRV resolution, which I forgot about because it hasn't been implemented
yet. A service name provided in the address probably shouldn't affect
that, making the case for allowing it a bit questionable. Up to the
maintainers, I guess.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/hare-dev/patches/39029/mbox | git am -3
Learn more about email & git

[PATCH hare] net::dial: Fix address parsing Export this patch

Allow IPv6 without port
Allow service name in address string
Signed-off-by: Lassi Pulkkinen <lassi@pulk.fi>
---
 net/dial/resolve.ha | 57 +++++++++++++++++++--------------------------
 1 file changed, 24 insertions(+), 33 deletions(-)

diff --git a/net/dial/resolve.ha b/net/dial/resolve.ha
index 9fa4e799..9361f5dc 100644
--- a/net/dial/resolve.ha
+++ b/net/dial/resolve.ha
@@ -18,60 +18,51 @@ export fn resolve(
	addr: str,
	service: str,
) (([]ip::addr, u16) | error) = {
	let port = if (strings::hasprefix(addr, '[')) {
		// [::1]:80
		yield match (strings::index(addr, "]:")) {
	if (strings::hasprefix(addr, '[')) {
		match (strings::index(addr, "]")) {
		case let i: size =>
			const sub = strings::sub(addr, i + 2, strings::end);
			const sub = strings::sub(addr, i + 1, strings::end);
			addr = strings::sub(addr, 1, i);
			yield match (strconv::stou16(sub)) {
			case let u: u16 =>
				yield u;
			case =>
			// [::1]
			if (len(sub) == 0) yield;
			// [::1]:80
			if (!strings::hasprefix(sub, ":")) {
				return invalid_address;
			};
			service = strings::sub(sub, 1, strings::end);
		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;
			};
		match (strings::index(addr, ':')) {
		case let i: size =>
			const sub = strings::sub(addr, i + 1, strings::end);
			service = 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;
			};
		case void => void;
		};
	};

	if (service == "unknown" && port == 0) {
		return unknown_service;
	const port = match (strconv::stou16(service)) {
	case let p: u16 =>
		yield p;
	case =>
		if (service == "unknown") {
			return unknown_service;
		};
		yield match (lookup_service(proto, service)) {
		case let p: u16 =>
			yield p;
		case void =>
			return unknown_service;
		};
	};

	let addrs = resolve_addr(addr)?;
	if (port == 0) match (lookup_service(proto, service)) {
	case let p: u16 =>
		port = p;
	case void => void;
	};

	// TODO:
	// - Consult /etc/services
	// - Fetch the SRV record

	if (port == 0) {
		return unknown_service;
	};
	if (len(addrs) == 0) {
		return dns::name_error;
	};
-- 
2.39.1

hare/patches: SUCCESS in 1m43s

[net::dial: Fix address parsing][0] from [Lassi Pulkkinen][1]

[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/39029
[1]: mailto:lassi@pulk.fi

✓ #941031 SUCCESS hare/patches/alpine.yml  https://builds.sr.ht/~sircmpwn/job/941031
✓ #941032 SUCCESS hare/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/941032