/mdns

Multicast DNS in Erlang/OTP

Primary LanguageErlangApache License 2.0Apache-2.0

Multicast DNS

Build Status

mDNS is an implementation of the mDNS discovery protocol written in Erlang/OTP and enables two or more Erlang nodes to self discover and form a mesh network.

The implementation uses the method described in DNS-Based Service Discovery to register and discover services.

A service of type _erlang._tcp will be advertised by default. The registered services can be viewed using avahi-browse as follows:

avahi-browse _erlang._tcp

Or on OSX:

dns-sd -B _erlang._tcp

mDNS will automatically advertise its presence and discover other _erlang._tcp nodes. Whether a node will advertise or discover other nodes is controlled by the MDNS_CAN_ADVERTISE and MDNS_CAN_DISCOVER boolean environment variables.

mDNS will also automatically form an Erlang/OTP mesh network of nodes if the boolean variable MDNS_CAN_MESH is true, providing they all share the same MDNS_ENVIRONMENT (default: "dev") and have the same distribution cookie.

For example, starting mDNS on two separate machines (in this example dev001.local and dev002.local) reachable on the same local network:

On dev001.local:

MDNS_CAN_MESH=true make shell

and on dev002.local:

MDNS_CAN_MESH=true make shell

After a short period both machines will have automatically formed a mesh network:

(mdns@dev001.local)2> nodes().
['mdns@dev002.local']
(mdns@dev002.local)2> nodes().
['mdns@dev001.local']

The following variables are used to control the operation of mDNS:

application name environment variable default
can_advertise MDNS_CAN_ADVERTISE true
can_discover MDNS_CAN_DISCOVER true
can_mesh MDNS_CAN_MESH false
environment MDNS_ENVIRONMENT dev
multicast_address MDNS_MULTICAST_ADDRESS 224.0.0.251
udp_port MDNS_UDP_PORT 5353
domain MDNS_DOMAIN .local
service MDNS_SERVICE _erlang._tcp
ttl MDNS_TTL 120

mDNS uses gproc's pub/sub pattern. Consumers can subscribe to mDNS advertisements via mdns:subscribe(advertisement). The map accompanying the advertisement has the following structure:

Key Description
host The hostname of the advertised node
node The node name of the advertised node
port The advertised distribution protocol port
env The environment of this node

Only nodes that share the same environment can be automatically meshed together.