hare

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

commit eb503b8ee40a0b9b6ee28ecee7382b9fc579ab95
parent 07b84f8aef941ee094ace5d9aab648c11befc9a7
Author: Drew DeVault <sir@cmpwn.com>
Date:   Mon, 21 Jun 2021 10:27:01 -0400

net::dns: add decode_a, decode_aaaa

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

Diffstat:
Mnet/dns/encoding.ha | 33+++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+), 0 deletions(-)

diff --git a/net/dns/encoding.ha b/net/dns/encoding.ha @@ -1,5 +1,6 @@ use endian; use fmt; +use net::ip; use strings; export type decoder = struct { @@ -126,6 +127,38 @@ export fn decode_name(dec: *decoder, buf: []u8) (([]u8, str) | format) = { return (buf[z + 1..], strings::fromutf8(buf[1..z + 1])); }; +// Decodes the rdata field of an A (address) record. The return value is +// borrowed from the rdata buffer. +export fn decode_a(rdata: []u8) (ip::addr4 | format) = { + if (len(rdata) != 4) { + return format; + }; + let ip: ip::addr4 = [0...]; + ip[..] = rdata[..]; + return ip; +}; + +// Decodes the rdata field of an AAAA (address) record. The return value is +// borrowed from the rdata buffer. +export fn decode_aaaa(rdata: []u8) (ip::addr6 | format) = { + if (len(rdata) != 8) { + return format; + }; + let ip: ip::addr6 = [0...]; + ip[..] = rdata[..]; + return ip; +}; + +// Decodes the rdata field of a [[rrecord]]. The return value is borrowed from +// the rdata buffer. +export fn decode_rdata(rr: *rrecord) (ip::addr | format) = { + return switch (rr.rtype) { + rtype::A => decode_a(rr.rdata)?: ip::addr, + rtype::AAAA => decode_aaaa(rr.rdata)?: ip::addr, + * => format, + }; +}; + // Encodes a DNS message, returning its size. export fn encode(buf: []u8, msg: *message) size = { let z = 0z;