View Source MdnsLite (mdns_lite v0.8.11)

MdnsLite is a simple, limited, no frills mDNS implementation

Advertising hostnames and services is generally done using the application config. See MdnsLite.Options for documentation.

To change the advertised hostnames or services at runtime, see set_host/1, add_mdns_service/1 and remove_mdns_service/1.

MdnsLite's mDNS record tables and caches can be inspected using MdnsLite.Info if you're having trouble.

Finally, check out the MdnsLite README.md for more information.

Summary

Types

A user-visible name for a service advertisement

mDNS service description

A user-specified ID for referring to a service

Functions

Start advertising a service

Lookup a hostname using mDNS

Stop advertising a service

Set the list of host names

Updates the advertised instance name for service records

Types

@type instance_name() :: String.t() | :unspecified

A user-visible name for a service advertisement

@type service() :: %{
  :id => service_id(),
  :instance_name => instance_name(),
  :port => 1..65535,
  optional(:txt_payload) => [String.t()],
  optional(:priority) => 0..255,
  optional(:protocol) => String.t(),
  optional(:transport) => String.t(),
  optional(:type) => String.t(),
  optional(:weight) => 0..255
}

mDNS service description

Keys include:

  • :id - an atom for referring to this service (only required if you want to reference the service at runtime)
  • :port - the TCP/UDP port number for the service (required)
  • :transport - the transport protocol. E.g., "tcp" (specify this and :protocol, or :type) * :protocol - the application protocol. E.g., "ssh" (specify this and :transport, or :type)
  • :type - the transport/protocol to advertize. E.g., "_ssh._tcp" (only needed if :protocol and :transport aren't specified)
  • :weight - the service weight. Defaults to 0. (optional)
  • :priority - the service priority. Defaults to 0. (optional)
  • :txt_payload - a list of strings to advertise

Example:

%{id: :my_ssh, port: 22, protocol: "ssh", transport: "tcp"}
@type service_id() :: atom() | binary()

A user-specified ID for referring to a service

Atoms are recommended, but binaries are still supported since they were used in the past.

Functions

Link to this function

add_mdns_service(service)

View Source
@spec add_mdns_service(service()) :: :ok

Start advertising a service

Services can be added at compile-time via the :services key in the mdns_lite application environment or they can be added at runtime using this function. See the service type for information on what's needed.

Example:

iex> service = %{
    id: :my_web_server,
    protocol: "http",
    transport: "tcp",
    port: 80
  }
iex> MdnsLite.add_mdns_service(service)
:ok
Link to this function

gethostbyname(hostname, timeout \\ 500)

View Source
@spec gethostbyname(String.t(), non_neg_integer()) ::
  {:ok, :inet.ip_address()} | {:error, any()}

Lookup a hostname using mDNS

The hostname should be a .local name since the query only goes out via mDNS. On success, an IP address is returned.

@spec remove_mdns_service(service_id()) :: :ok

Stop advertising a service

Example:

iex> MdnsLite.remove_mdns_service(:my_ssh)
:ok
@spec set_hosts([:hostname | String.t()]) :: :ok

Set the list of host names

This replaces the list of hostnames that MdnsLite will respond to. The first hostname in the list is special. Service advertisements will use it. The remainder are aliases.

Hostnames should not have the ".local" extension. MdnsLite will add it.

To specify the hostname returned by :inet.gethostname/0, use :hostname.

To make MdnsLite respond to queries for "<hostname>.local" and "nerves.local", run this:

iex> MdnsLite.set_hosts([:hostname, "nerves"])
:ok
Link to this function

set_instance_name(instance_name)

View Source
@spec set_instance_name(instance_name()) :: :ok

Updates the advertised instance name for service records

To specify the first hostname specified in hosts, use :unspecified