From 03874b8eca8f065f6a1b133f06f19c0ff1d1494e Mon Sep 17 00:00:00 2001 From: Linus Nordberg Date: Wed, 6 Apr 2016 10:07:48 +0200 Subject: Add unit test for validation, from dnssecport:handle_call(). - The port now returns the RRset (DS, chain, trust root and all RRSIG's). This in preparatino for when this data will be normalised. - dnssecport decodes and encodes DNS data. - v1 stores the DS RR in the leaf and the rest, including the DS RRSIG, in the chain. --- src/dns.erl | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/dns.erl (limited to 'src/dns.erl') diff --git a/src/dns.erl b/src/dns.erl new file mode 100644 index 0000000..24fb8fb --- /dev/null +++ b/src/dns.erl @@ -0,0 +1,69 @@ +%%% Copyright (c) 2016, NORDUnet A/S. +%%% See LICENSE for licensing information. + +-module(dns). +-export([split_rrset/1, encode_rr/1, encode_rrset/1]). + +decode_name_label(Name) -> + <> = Name, + {Label, Rest}. + +encode_name_label(Label) -> + Len = byte_size(Label), + <>. + +decode_name(RR) -> + decode_name(RR, []). +decode_name(<<0, Rest/binary>>, Acc) -> + {lists:reverse(Acc), Rest}; +decode_name(Name, Acc) -> + {Label, Rest} = decode_name_label(Name), + decode_name(Rest, [Label | Acc]). + +-spec encode_name(list()) -> binary(). +encode_name(Name) -> + encode_name(Name, []). +encode_name([], Acc) -> + Bin = list_to_binary(lists:reverse(Acc)), + <>; +encode_name([H|T], Acc) -> + encode_name(T, [encode_name_label(H) | Acc]). + +-spec decode_rr(binary()) -> {list(), binary()}. +decode_rr(RR) -> + {Name, RestRR} = decode_name(RR), + <> = RestRR, + {[Name, Type, Class, TTL, RDATA], Rest}. + +-spec split_rrset(binary()) -> list(). +split_rrset(RRSet) -> + split_rrset(RRSet, []). +split_rrset(<<>>, Acc) -> + lists:reverse(Acc); +split_rrset(RRSet, Acc) -> + {RR, Rest} = decode_rr(RRSet), + split_rrset(Rest, [RR | Acc]). + +-spec encode_rr(list()) -> binary(). +encode_rr([Name, Type, Class, TTL, RDATA]) -> + EncodedName = encode_name(Name), + RDLength = byte_size(RDATA), + <>. + +-spec encode_rrset(list()) -> binary(). +encode_rrset(RRSet) -> + encode_rrset(RRSet, []). +encode_rrset([], Acc) -> + list_to_binary(lists:reverse(Acc)); +encode_rrset([H|T], Acc) -> + encode_rrset(T, [encode_rr(H) | Acc]). -- cgit v1.1