webrtc_mdns_msg (webrtc v0.1.0)
View SourceSans-IO DNS message codec for multicast DNS (RFC 1035 §4, RFC 6762).
Only what mDNS ICE candidates need (draft-ietf-mmusic-mdns-ice-candidates): queries and responses with A and AAAA records. Other record types are decoded with their raw RDATA.
Q = webrtc_mdns_msg:encode_query(0, [{<<"x.local">>, a, true}]),
{ok, #{questions := [#{name := <<"x.local">>, type := a, unicast := true}]}} =
webrtc_mdns_msg:decode(Q).Decoding is bounded and never raises: compression pointers must point strictly backwards (so they cannot loop), a name follows at most ?MAX_HOPS pointers, names are at most 255 octets on the wire (RFC 1035 §2.3.4) and every count is checked against the bytes left. Names are returned lower-cased (RFC 6762 §16: ASCII case-insensitive), labels joined with .; a . or \ inside a label is escaped with \ so that distinct label sequences never give the same name.
In questions the top bit of the class is the unicast-response (QU) bit (RFC 6762 §5.4); in resource records it is the cache-flush bit (RFC 6762 §10.2). Both are reported separately from the class.
Summary
Functions
Decode a DNS message. Never raises: malformed input gives {error, Reason}.
A query with the given questions, class IN. Unicast sets the QU bit. Names are written without compression.
An authoritative response (QR and AA set) repeating Questions (needed for legacy unicast replies, RFC 6762 §6.7) with A or AAAA answers [{Name, IP, TTL, CacheFlush}] and additional records. An answer {Name, {nsec, Types}, TTL, CacheFlush} is the NSEC record saying Name has only Types (RFC 6762 §6.1).
Split a dotted host name into DNS labels: 1 to 63 octets each, at most 255 octets on the wire. A trailing dot is accepted.
Canonical form used to compare names: lower-case, no trailing dot. error for names that are not valid host names on the wire.
Types
-type rr() :: #{name := binary(), type := rr_type(), class := 0..32767, cache_flush := boolean(), ttl := non_neg_integer(), data := inet:ip_address() | binary()}.
-type rr_type() :: a | aaaa | ptr | txt | srv | nsec | any | 0..65535.
Functions
Decode a DNS message. Never raises: malformed input gives {error, Reason}.
A query with the given questions, class IN. Unicast sets the QU bit. Names are written without compression.
-spec encode_response(0..65535, [{binary(), rr_type()}], [{binary(), inet:ip_address() | {nsec, [rr_type()]}, non_neg_integer(), boolean()}], [{binary(), inet:ip_address() | {nsec, [rr_type()]}, non_neg_integer(), boolean()}]) -> binary().
An authoritative response (QR and AA set) repeating Questions (needed for legacy unicast replies, RFC 6762 §6.7) with A or AAAA answers [{Name, IP, TTL, CacheFlush}] and additional records. An answer {Name, {nsec, Types}, TTL, CacheFlush} is the NSEC record saying Name has only Types (RFC 6762 §6.1).
Split a dotted host name into DNS labels: 1 to 63 octets each, at most 255 octets on the wire. A trailing dot is accepted.
Canonical form used to compare names: lower-case, no trailing dot. error for names that are not valid host names on the wire.