The description here is far from complete and will therefore be further refined in upcoming releases. The protocols both from Erlang nodes towards EPMD (Erlang Port Mapper Daemon) and between Erlang nodes, however, are stable since many years.
The distribution protocol can be divided into four (4) parts:
A node fetches the Port number of another node through the EPMD (at the other host) in order to initiate a connection request.
For each host where a distributed Erlang node is running there should also be an EPMD running. The EPMD can be started explicitly or automatically as a result of the Erlang node startup.
By default EPMD listens on port 4369.
3 and 4 are performed at the same level but the net_kernel disconnects the other node if it communicates using an invalid cookie (after one (1) second).
The integers in all multi-byte fields are in big-endian order.
The requests served by the EPMD (Erlang Port Mapper Daemon) are summarized in the figure below.
Each request *_REQ is preceeded by a two-byte length field. Thus, the overall request format is:
2 | n |
Length | Request |
When a distributed node is started it registers itself in EPMD. The message ALIVE2_REQ described below is sent from the node towards EPMD. The response from EPMD is ALIVE2_RESP.
1 | 2 | 1 | 1 | 2 | 2 | 2 | Nlen | 2 | Elen |
120 | PortNo | NodeType | Protocol | LowestVersion | HighestVersion | Nlen | NodeName | Elen | Extra |
The connection created to the EPMD must be kept as long as the node is a distributed node. When the connection is closed the node is automatically unregistered from the EPMD.
The response message ALIVE2_RESP is described below.
1 | 1 | 2 |
121 | Result | Creation |
Result = 0 -> ok, Result > 0 -> error
A node unregister itself from the EPMD by simply closing the TCP connection towards EPMD established when the node was registered.
When one node wants to connect to another node it starts with a PORT_PLEASE2_REQ request towards EPMD on the host where the node resides in order to get the distribution port that the node listens to.
1 | N |
122 | NodeName |
where N = Length - 1
1 | 1 |
119 | Result |
Or
1 | 1 | 2 | 1 | 1 | 2 | 2 | 2 | Nlen | 2 | Elen |
119 | Result | PortNo | NodeType | Protocol | HighestVersion | LowestVersion | Nlen | NodeName | Elen | Extra |
If Result > 0, the packet only consists of [119, Result].
EPMD will close the socket as soon as it has sent the information.
This request is used via the Erlang function net_adm:names/1,2. A TCP connection is opened towards EPMD and this request is sent.
1 |
110 |
The response for a NAMES_REQ looks like this:
4 | |
EPMDPortNo | NodeInfo* |
NodeInfo is a string written for each active node. When all NodeInfo has been written the connection is closed by EPMD.
NodeInfo is, as expressed in Erlang:
io:format("name ~s at port ~p~n", [NodeName, Port]).
This request is not really used, it should be regarded as a debug feature.
1 |
100 |
The response for a DUMP_REQ looks like this:
4 | |
EPMDPortNo | NodeInfo* |
NodeInfo is a string written for each node kept in EPMD. When all NodeInfo has been written the connection is closed by EPMD.
NodeInfo is, as expressed in Erlang:
io:format("active name ~s at port ~p, fd = ~p ~n", [NodeName, Port, Fd]).
or
io:format("old/unused name ~s at port ~p, fd = ~p~n", [NodeName, Port, Fd]).
This request will kill the running EPMD. It is almost never used.
1 |
107 |
The response fo a KILL_REQ looks like this:
2 |
OKString |
where OKString is "OK".
1 | n |
115 | NodeName |
where n = Length - 1
The current implementation of Erlang does not care if the connection to the EPMD is broken.
The response for a STOP_REQ looks like this.
7 |
OKString |
where OKString is "STOPPED".
A negative response can look like this.
7 |
NOKString |
where NOKString is "NOEXIST".
The handshake is discussed in detail in the internal documentation for the kernel (Erlang) application.
As of erts version 5.7.2 the runtime system passes a distribution flag in the handshake stage that enables the use of a distribution header on all messages passed. Messages passed between nodes are in this case on the following format:
4 | d | n | m |
Length | DistributionHeader | ControlMessage | Message |
where:
Length is equal to d + n + m
ControlMessage is a tuple passed using the external format of Erlang.
Message is the message sent to another node using the '!' (in external format). Note that Message is only passed in combination with a ControlMessage encoding a send ('!').
Also note that the version number is omitted from the terms that follow a distribution header.
Nodes with an erts version less than 5.7.2 does not pass the distribution flag that enables the distribution header. Messages passed between nodes are in this case on the following format:
4 | 1 | n | m |
Length | Type | ControlMessage | Message |
where:
Length is equal to 1 + n + m
Type is: 112 (pass through)
ControlMessage is a tuple passed using the external format of Erlang.
Message is the message sent to another node using the '!' (in external format). Note that Message is only passed in combination with a ControlMessage encoding a send ('!').
The ControlMessage is a tuple, where the first element indicates which distributed operation it encodes.
distrvsn 2 was never used.
None, but the version number was increased anyway.
These are only recognized by Erlang nodes, not by hidden nodes.