%%% 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]).