webrtc_mdns_msg (webrtc v0.1.0)

View Source

Sans-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

message/0

-type message() ::
          #{id := 0..65535,
            qr := boolean(),
            opcode := 0..15,
            aa := boolean(),
            tc := boolean(),
            rcode := 0..15,
            questions := [question()],
            answers := [rr()],
            authority := [rr()],
            additional := [rr()]}.

question/0

-type question() :: #{name := binary(), type := rr_type(), class := 0..32767, unicast := boolean()}.

rr/0

-type rr() ::
          #{name := binary(),
            type := rr_type(),
            class := 0..32767,
            cache_flush := boolean(),
            ttl := non_neg_integer(),
            data := inet:ip_address() | binary()}.

rr_type/0

-type rr_type() :: a | aaaa | ptr | txt | srv | nsec | any | 0..65535.

Functions

decode(Msg)

-spec decode(binary()) -> {ok, message()} | {error, term()}.

Decode a DNS message. Never raises: malformed input gives {error, Reason}.

encode_query(Id, Questions)

-spec encode_query(0..65535, [{binary(), rr_type(), boolean()}]) -> binary().

A query with the given questions, class IN. Unicast sets the QU bit. Names are written without compression.

encode_response(Id, Questions, Answers, Additional)

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

name_labels(Name)

-spec name_labels(binary()) -> {ok, [binary()]} | error.

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.

normalize_name(Name)

-spec normalize_name(binary()) -> {ok, binary()} | error.

Canonical form used to compare names: lower-case, no trailing dot. error for names that are not valid host names on the wire.