hare

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

commit 5a8b05d4b148dd594e5dca27ba1c18d38883a4fb
parent a6923d1f370a1198a8bb7d849f309287fc1a02a4
Author: Drew DeVault <sir@cmpwn.com>
Date:   Wed, 23 Jun 2021 11:51:53 -0400

net::dns: add parse_domain, unparse_domain

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mnet/dns/encode.ha | 13++++++++++++-
Mscripts/gen-stdlib | 2+-
Mstdlib.mk | 4++--
3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/net/dns/encode.ha b/net/dns/encode.ha @@ -1,12 +1,23 @@ -use errors; use endian; +use errors; use fmt; +use strings; type encoder = struct { buf: []u8, offs: size, }; +// Converts a human-readable domain name (e.g. "example.org") into a DNS-ready +// name slice (e.g. ["example", "org"]). The slice returned must be freed by the +// caller, but the members of the slice themselves are borrowed from the input. +export fn parse_domain(in: str) []str = strings::split(in, "."); + +// Converts a DNS name slice (e.g. ["example", "org"]) into a human-readable +// domain name (e.g. "example.org"). The return value must be freed by the +// caller. +export fn unparse_domain(in: []str) str = strings::join(".", in...); + // Encodes a DNS message, returning its size, or an error. export fn encode(buf: []u8, msg: *message) (size | error) = { let enc = encoder { buf = buf, offs = 0z }; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -568,7 +568,7 @@ net_dns() { encode.ha \ query.ha \ types.ha - gen_ssa net::dns ascii endian net net::udp net::ip fmt \ + gen_ssa net::dns ascii endian net net::udp net::ip fmt strings \ unix::resolvconf } diff --git a/stdlib.mk b/stdlib.mk @@ -855,7 +855,7 @@ stdlib_net_dns_srcs= \ $(STDLIB)/net/dns/query.ha \ $(STDLIB)/net/dns/types.ha -$(HARECACHE)/net/dns/net_dns.ssa: $(stdlib_net_dns_srcs) $(stdlib_rt) $(stdlib_ascii) $(stdlib_endian) $(stdlib_net) $(stdlib_net_udp) $(stdlib_net_ip) $(stdlib_fmt) $(stdlib_unix_resolvconf) +$(HARECACHE)/net/dns/net_dns.ssa: $(stdlib_net_dns_srcs) $(stdlib_rt) $(stdlib_ascii) $(stdlib_endian) $(stdlib_net) $(stdlib_net_udp) $(stdlib_net_ip) $(stdlib_fmt) $(stdlib_strings) $(stdlib_unix_resolvconf) @printf 'HAREC \t$@\n' @mkdir -p $(HARECACHE)/net/dns @HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nnet::dns \ @@ -2025,7 +2025,7 @@ testlib_net_dns_srcs= \ $(STDLIB)/net/dns/query.ha \ $(STDLIB)/net/dns/types.ha -$(TESTCACHE)/net/dns/net_dns.ssa: $(testlib_net_dns_srcs) $(testlib_rt) $(testlib_ascii) $(testlib_endian) $(testlib_net) $(testlib_net_udp) $(testlib_net_ip) $(testlib_fmt) $(testlib_unix_resolvconf) +$(TESTCACHE)/net/dns/net_dns.ssa: $(testlib_net_dns_srcs) $(testlib_rt) $(testlib_ascii) $(testlib_endian) $(testlib_net) $(testlib_net_udp) $(testlib_net_ip) $(testlib_fmt) $(testlib_strings) $(testlib_unix_resolvconf) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/net/dns @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nnet::dns \