## 9 Distribution Protocol

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:

- 1. Low level socket connection.
- 2. Handshake, interchange node name and authenticate.
- 3. Authentication (done by net_kernel).
- 4. Connected.

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

### 9.1 EPMD Protocol

The requests served by the EPMD (Erlang Port Mapper Daemon) are summarized in the figure below.

Summary of EPMD requests.

Each request *_REQ is preceeded by a two-byte length field. Thus, the overall request format is:

2 | n
Length | Request

#### 9.1.1 Register a node in the EPMD

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.

ALIVE2_REQ (120)
1 | 2 | 1 | 1 | 2 | 2 | Nlen | 2 | Elen
120 | PortNo | NodeType | Protocol | DistrvsnRange | Nlen | NodeName | Elen | Extra

PortNo
  The port number on which the node accept connection requests.
NodeType
  77 = normal Erlang node, 72 = hidden node (C-node),...
Protocol
  0 = tcp/ip-v4, ...
DistrvsnRange
  Two bytes where MSB = Highestvsn and LSB = Lowestvsn. For erts-4.6.x (OTP-R3)the vsn = 0 For erts-4.7.x (OTP-R4) = ?????.
Nlen
  The length of the NodeName.
NodeName
  The NodeName as a string of length Nlen.
Elen
  The length of the Extra field.
Extra
  Extra field of Elen bytes.

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.

ALIVE2_RESP (121)
1 | 1 | 2
121 | Result | Creation

Result = 0 -> ok, Result > 0 -> error

#### 9.1.2 Unregister a node from the EPMD

A node unregister itself from the EPMD by simply closing the TCP connection towards EPMD established when the node was registered.

#### 9.1.3 Get the distribution port of another node

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.

PORT_PLEASE2_REQ (122)
1 | N
122 | NodeName

where N = Length - 1

PORT2_RESP (119) response indicating error, Result > 0.
1 | 1
119 | Result

Or

PORT2_RESP when Result = 0.
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].

#### 9.1.4 Get all registered names from EPMD

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.

NAMES_REQ (110)
1
110

The response for a NAMES_REQ looks like this:

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

#### 9.1.5 Dump all data from EPMD

This request is not really used, it should be regarded as a debug feature.

DUMP_REQ
1
100

The response for a DUMP_REQ looks like this:

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

#### 9.1.6 Kill the EPMD

This request will kill the running EPMD. It is almost never used.

KILL_REQ
1
107

The response fo a KILL_REQ looks like this:

KILL_RESP
2
OKString

where OKString is "OK".

#### 9.1.7 STOP_REQ (Not Used)

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

STOP_RESP
7
OKString

where OKString is "STOPPED".

A negative response can look like this.

STOP_NOTOK_RESP
7
NOKString

where NOKString is "NOEXIST".

### 9.2 Handshake

The handshake is discussed in detail in the internal documentation for the kernel (Erlang) application.

### 9.3 Protocol between connected nodes

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.

LINK
  {1, FromPid, ToPid}
SEND
  {2, Cookie, ToPid}

Note

Message is sent as well.

EXIT
  {3, FromPid, ToPid, Reason}
UNLINK
  {4, FromPid, ToPid}
NODE_LINK
  {5}
REG_SEND
  {6, FromPid, Cookie, ToName}

Note

Message is sent as well.

GROUP_LEADER
  {7, FromPid, ToPid}
EXIT2
  {8, FromPid, ToPid, Reason}

### 9.4 New Ctrlmessages for distrvsn = 1 (OTP R4)

#### 9.4.1 SEND_TT

{12, Cookie, ToPid, TraceToken}

Note

Message is sent as well.

#### 9.4.2 EXIT_TT

{13, FromPid, ToPid, TraceToken, Reason}

#### 9.4.3 REG_SEND_TT

{16, FromPid, Cookie, ToName, TraceToken}

Note

Message is sent as well.

#### 9.4.4 EXIT2_TT

{18, FromPid, ToPid, TraceToken, Reason}

### 9.5 New Ctrlmessages for distrvsn = 2

distrvsn 2 was never used.

### 9.6 New Ctrlmessages for distrvsn = 3 (OTP R5C)

None, but the version number was increased anyway.

### 9.7 New Ctrlmessages for distrvsn = 4 (OTP R6)

These are only recognized by Erlang nodes, not by hidden nodes.

#### 9.7.1 MONITOR_P

{19, FromPid, ToProc, Ref} FromPid = monitoring process ToProc = monitored process pid or name (atom)

#### 9.7.2 DEMONITOR_P

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

#### 9.7.3 MONITOR_P_EXIT

{21, FromProc, ToPid, Ref, Reason} FromProc = monitored process pid or name (atom) ToPid = monitoring process Reason = exit reason for the monitored process

erts 5.6.4
Copyright © 1991-2008 Ericsson AB