Message is sent as well.
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 are however stable and mature 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 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 | Nlen | 2 | Elen |
120 | PortNo | NodeType | Protocol | DistrvsnRange | 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.
The response PORT2_RESP contains other valuable information such as protocol version in addition to the distribution port.
1 | N |
122 | NodeName |
where N = Length - 1
1 | 1 |
119 | Result |
Or
1 | 1 | 2 | 1 | 1 | 2 | 2 | Nlen | 2 | Elen |
119 | Result | PortNo | NodeType | Protocol | DistrvsnRange | Nlen | NodeName | Elen | Extra |
If Result > 0, the packet only consists of [119, Result].
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.
4 | 1 | n | m |
Length | Type | ControlMsg | Message |
where:
Length is equal to 1 + n + m Type is: 112 - pass through ControlMsg is a tuple passed using the external format of Erlang. Message is the message sent to another node using the '!' (in external format). But, Message is only passed in combination with a ControlMsg encoding a send ('!').
The control message is a tuple, where the first element indicates which distributed operation it encodes.
Message is sent as well.
Message is sent as well.
{12, Cookie, ToPid, TraceToken}
Message is sent as well.
{13, FromPid, ToPid, TraceToken, Reason}
{16, FromPid, Cookie, ToName, TraceToken}
Message is sent as well.
{18, FromPid, ToPid, TraceToken, Reason}
distrvsn 2 was never used.
None, but the version number was increased anyway.
These are only recognized by Erlang nodes, not by hidden nodes.
{19, FromPid, ToProc, Ref} FromPid = monitoring process ToProc = monitored process pid or name (atom)
{20, FromPid, ToProc, Ref} We include the FromPid just in case we want to trace this. FromPid = monitoring process ToProc = monitored process pid or name (atom)
{21, FromProc, ToPid, Ref, Reason} FromProc = monitored process pid or name (atom) ToPid = monitoring process Reason = exit reason for the monitored process