---
name: "erl_connect"
section: "3erl"
platform: "linux"
package: "erlang-manpages"
version: "1:21.2.6+dfsg-1"
release: "Debian 10 (Buster)"
platforms: ["linux"]
url: "https://new.carta.tech/erl_connect"
---

# erl\_connect(3erl)

Communicate with distributed Erlang.

## NAME

erl\_connect - Communicate with distributed Erlang.

## DESCRIPTION

This module provides support for communication between distributed Erlang nodes and C-nodes, in a manner that is transparent to Erlang processes.

A C-node appears to Erlang as a *hidden node*. That is, Erlang processes that know the name of the C-node can communicate with it in a normal manner, but the node name does not appear in the listing provided by *erlang:nodes/0* in *ERTS*.

## EXPORTS

**int erl\_accept(listensock, conp)**\

Types:

int listensock;\
 ErlConnect \*conp;\

This function is used by a server process to accept a connection from a client process.

- \*

  *listensock* is an open socket descriptor on which *listen()* has previously been called.

- \*

  *conp* is a pointer to an *ErlConnect* struct, described as follows:

```
typedef struct {
  char ipadr[4];
  char nodename[MAXNODELEN];
} ErlConnect;
```

On success, *conp* is filled in with the address and node name of the connecting client and a file descriptor is returned. On failure, *ERL\_ERROR* is returned and *erl\_errno* is set to *EIO*.

**int erl\_close\_connection(fd)**\

Types:

int fd;\

Closes an open connection to an Erlang node.

*Fd* is a file descriptor obtained from *erl\_connect()* or *erl\_xconnect()*.

Returns *0* on success. If the call fails, a non-zero value is returned, and the reason for the error can be obtained with the appropriate platform-dependent call.

**int erl\_connect(node)**\
 **int erl\_xconnect(addr, alive)**\

Types:

char \*node, \*alive;\
 struct in\_addr \*addr;\

Sets up a connection to an Erlang node.

*erl\_xconnect()* requires the IP address of the remote host and the alivename of the remote node to be specified. *erl\_connect()* provides an alternative interface, and determines the information from the node name provided.

- \*

  *addr* is the 32-bit IP address of the remote host.

- \*

  *alive* is the alivename of the remote node.

- \*

  *node* is the name of the remote node.

Returns an open file descriptor on success, otherwise a negative value. In the latter case *erl\_errno* is set to one of:

- ***EHOSTUNREACH*:**

  The remote host *node* is unreachable.

- ***ENOMEM*:**

  No more memory is available.

- ***EIO*:**

  I/O error.

Also, *errno* values from *[socket(2)](/socket/2.md)* and *[connect(2)](/connect/2.md)* system calls can be propagated into *erl\_errno*.

*Example:*

```
#define NODE   "madonna@chivas.du.etx.ericsson.se"
#define ALIVE  "madonna"
#define IP_ADDR "150.236.14.75"
/*** Variant 1 ***/
erl_connect( NODE );
/*** Variant 2 ***/
struct in_addr addr;
addr = inet_addr(IP_ADDR);
erl_xconnect( &addr , ALIVE );
```

**int erl\_connect\_init(number, cookie, creation)**\
 **int erl\_connect\_xinit(host, alive, node, addr, cookie, creation)**\

Types:

int number;\
 char \*cookie;\
 short creation;\
 char \*host,\*alive,\*node;\
 struct in\_addr \*addr;\

Initializes the *erl\_connect* module. In particular, these functions are used to identify the name of the C-node from which they are called. One of these functions must be called before any of the other functions in the *erl\_connect* module are used.

*erl\_connect\_xinit()* stores for later use information about:

- \*

  Hostname of the node, *host*

- \*

  Alivename, *alive*

- \*

  Node name, *node*

- \*

  IP address, *addr*

- \*

  Cookie, *cookie*

- \*

  Creation number, *creation*

*erl\_connect\_init()* provides an alternative interface that does not require as much information from the caller. Instead, *erl\_connect\_init()* uses *gethostbyname()* to obtain default values.

If you use *erl\_connect\_init()*, your node will have a short name, that is, it will not be fully qualified. If you need to use fully qualified (long) names, use *erl\_connect\_xinit()* instead.

- \*

  *host* is the name of the host on which the node is running.

- \*

  *alive* is the alivename of the node.

- \*

  *node* is the node name. It is to be of the form *alivename@hostname*.

- \*

  *addr* is the 32-bit IP address of *host*.

- \*

  *cookie* is the authorization string required for access to the remote node. If *NULL*, the user *HOME* directory is searched for a cookie file *.erlang.cookie*. The path to the home directory is retrieved from environment variable *HOME* on Unix and from the *HOMEDRIVE* and *HOMEPATH* variables on Windows. For more details, see the *auth* module in Kernel.

- \*

  *creation* helps identifying a particular instance of a C-node. In particular, it can help prevent us from receiving messages sent to an earlier process with the same registered name.

A C-node acting as a server is assigned a creation number when it calls *erl\_publish()*.

*number* is used by *erl\_connect\_init()* to construct the actual node name. In Example 2 below, *"c17@a.DNS.name"* is the resulting node name.

*Example 1:*

```
struct in_addr addr;
addr = inet_addr("150.236.14.75");
if (!erl_connect_xinit("chivas",
                       "madonna",
                       "madonna@chivas.du.etx.ericsson.se",
                       &addr;
                       "samplecookiestring..."),
                       0)
  erl_err_quit("<ERROR> when initializing !");
```

*Example 2:*

```
if (!erl_connect_init(17, "samplecookiestring...", 0))
  erl_err_quit("<ERROR> when initializing !");
```

**int erl\_publish(port)**\

Types:

int port;\

This function is used by a server process to register with the local name server EPMD, thereby allowing other processes to send messages by using the registered name. Before calling this function, the process should have called *bind()* and *listen()* on an open socket.

*port* is the local name to register, and is to be the same as the port number that was previously bound to the socket.

To unregister with EPMD, simply close the returned descriptor.

On success, a descriptor connecting the calling process to EPMD is returned. On failure, *-1* is returned and *erl\_errno* is set to:

- ***EIO*:**

  I/O error.

Also, *errno* values from *[socket(2)](/socket/2.md)* and *[connect(2)](/connect/2.md)* system calls can be propagated into *erl\_errno*.

**int erl\_receive(fd, bufp, bufsize)**\

Types:

int fd;\
 char \*bufp;\
 int bufsize;\

Receives a message consisting of a sequence of bytes in the Erlang external format.

- \*

  *fd* is an open descriptor to an Erlang connection.

- \*

  *bufp* is a buffer large enough to hold the expected message.

- \*

  *bufsize* indicates the size of *bufp*.

If a *tick* occurs, that is, the Erlang node on the other end of the connection has polled this node to see if it is still alive, the function returns *ERL\_TICK* and no message is placed in the buffer. Also, *erl\_errno* is set to *EAGAIN*.

On success, the message is placed in the specified buffer and the function returns the number of bytes actually read. On failure, the function returns a negative value and sets *erl\_errno* to one of:

- ***EAGAIN*:**

  Temporary error: Try again.

- ***EMSGSIZE*:**

  Buffer is too small.

- ***EIO*:**

  I/O error.

**int erl\_receive\_msg(fd, bufp, bufsize, emsg)**\

Types:

int fd;\
 unsigned char \*bufp;\
 int bufsize;\
 ErlMessage \*emsg;\

Receives the message into the specified buffer and decodes into *(ErlMessage \*) emsg*.

- \*

  *fd* is an open descriptor to an Erlang connection.

- \*

  *bufp* is a buffer large enough to hold the expected message.

- \*

  *bufsize* indicates the size of *bufp*.

- \*

  \>*emsg* is a pointer to an *ErlMessage* structure into which the message will be decoded. *ErlMessage* is defined as follows:

```
typedef struct {
  int type;
  ETERM *msg;
  ETERM *to;
  ETERM *from;
  char to_name[MAXREGLEN];
} ErlMessage;
```

**Note:**

The definition of *ErlMessage* has changed since earlier versions of *Erl\_Interface*.

*type* identifies the type of message, one of the following:

- ***ERL\_SEND*:**

  An ordinary send operation has occurred and *emsg-\>to* contains the pid of the recipient. The message is in *emsg-\>msg*.

- ***ERL\_REG\_SEND*:**

  A registered send operation has occurred and *emsg-\>from* contains the pid of the sender. The message is in *emsg-\>msg*.

- ***ERL\_LINK* or *ERL\_UNLINK*:**

  *emsg-\>to* and *emsg-\>from* contain the pids of the sender and recipient of the link or unlink. *emsg-\>msg* is not used.

- ***ERL\_EXIT*:**

  A link is broken. *emsg-\>to* and *emsg-\>from* contain the pids of the linked processes, and *emsg-\>msg* contains the reason for the exit.

**Note:**

It is the caller's responsibility to release the memory pointed to by *emsg-\>msg*, *emsg-\>to*, and *emsg-\>from*.

If a *tick* occurs, that is, the Erlang node on the other end of the connection has polled this node to see if it is still alive, the function returns *ERL\_TICK* indicating that the tick has been received and responded to, but no message is placed in the buffer. In this case you are to call *erl\_receive\_msg()* again.

On success, the function returns *ERL\_MSG* and the *Emsg* struct is initialized as described above, or *ERL\_TICK*, in which case no message is returned. On failure, the function returns *ERL\_ERROR* and sets *erl\_errno* to one of:

- ***EMSGSIZE*:**

  Buffer is too small.

- ***ENOMEM*:**

  No more memory is available.

- ***EIO*:**

  I/O error.

**int erl\_reg\_send(fd, to, msg)**\

Types:

int fd;\
 char \*to;\
 ETERM \*msg;\

Sends an Erlang term to a registered process.

- \*

  *fd* is an open descriptor to an Erlang connection.

- \*

  *to* is a string containing the registered name of the intended recipient of the message.

- \*

  *msg* is the Erlang term to be sent.

Returns *1* on success, otherwise *0*. In the latter case *erl\_errno* is set to one of:

- ***ENOMEM*:**

  No more memory is available.

- ***EIO*:**

  I/O error.

**ETERM \*erl\_rpc(fd, mod, fun, args)**\
 **int erl\_rpc\_from(fd, timeout, emsg)**\
 **int erl\_rpc\_to(fd, mod, fun, args)**\

Types:

int fd, timeout;\
 char \*mod, \*fun;\
 ETERM \*args;\
 ErlMessage \*emsg;\

Supports calling Erlang functions on remote nodes. *erl\_rpc\_to()* sends an RPC request to a remote node and *erl\_rpc\_from()* receives the results of such a call. *erl\_rpc()* combines the functionality of these two functions by sending an RPC request and waiting for the results. See also *rpc:call/4* in *Kernel*.

- \*

  *fd* is an open descriptor to an Erlang connection.

- \*

  *timeout* is the maximum time (in milliseconds) to wait for results. To wait forever, specify *ERL\_NO\_TIMEOUT*. When *erl\_rpc()* calls *erl\_rpc\_from()*, the call will never timeout.

- \*

  *mod* is the name of the module containing the function to be run on the remote node.

- \*

  *fun* is the name of the function to run.

- \*

  *args* is an Erlang list, containing the arguments to be passed to the function.

- \*

  *emsg* is a message containing the result of the function call.

The actual message returned by the RPC server is a 2-tuple *{rex,Reply}*. If you use *erl\_rpc\_from()* in your code, this is the message you will need to parse. If you use *erl\_rpc()*, the tuple itself is parsed for you, and the message returned to your program is the Erlang term containing *Reply* only. Replies to RPC requests are always *ERL\_SEND* messages.

**Note:**

It is the caller's responsibility to free the returned *ETERM* structure and the memory pointed to by *emsg-\>msg* and *emsg-\>to*.

*erl\_rpc()* returns the remote function's return value on success, otherwise *NULL*.

*erl\_rpc\_to()* returns *0* on success, otherwise a negative number.

*erl\_rcp\_from()* returns *ERL\_MSG* on success (with *Emsg* now containing the reply tuple), otherwise one of *ERL\_TICK*, *ERL\_TIMEOUT*, or *ERL\_ERROR*.

When failing, all three functions set *erl\_errno* to one of:

- ***ENOMEM*:**

  No more memory is available.

- ***EIO*:**

  I/O error.

- ***ETIMEDOUT*:**

  Timeout has expired.

- ***EAGAIN*:**

  Temporary error: Try again.

**int erl\_send(fd, to, msg)**\

Types:

int fd;\
 ETERM \*to, \*msg;\

Sends an Erlang term to a process.

- \*

  *fd* is an open descriptor to an Erlang connection.

- \*

  *to* is an Erlang term containing the pid of the intended recipient of the message.

- \*

  \>*msg* is the Erlang term to be sent.

Returns *1* on success, otherwise *0*. In the latter case *erl\_errno* is set to one of:

- ***EINVAL*:**

  Invalid argument: *to* is not a valid Erlang pid.

- ***ENOMEM*:**

  No more memory is available.

- ***EIO*:**

  I/O error.

**const char \*erl\_thisalivename()**\
 **const char \*erl\_thiscookie()**\
 **short erl\_thiscreation()**\
 **const char \*erl\_thishostname()**\
 **const char \*erl\_thisnodename()**\

Retrieves information about the C-node. These values are initially set with *erl\_connect\_init()* or *erl\_connect\_xinit()*.

**int erl\_unpublish(alive)**\

Types:

char \*alive;\

This function can be called by a process to unregister a specified node from EPMD on the local host. This is, however, usually not allowed, unless EPMD was started with flag *-relaxed\_command\_check*, which it normally is not.

To unregister a node you have published, you should instead close the descriptor that was returned by *ei\_publish()*.

**Warning:**

This function is deprecated and will be removed in a future release.

*alive* is the name of the node to unregister, that is, the first component of the node name, without *@hostname*.

If the node was successfully unregistered from EPMD, *0* is returned, otherwise *-1* is returned and *erl\_errno* is set to *EIO*.

**int erl\_xreceive\_msg(fd, bufpp, bufsizep, emsg)**\

Types:

int fd;\
 unsigned char \*\*bufpp;\
 int \*bufsizep;\
 ErlMessage \*emsg;\

Similar to *erl\_receive\_msg*. The difference is that *erl\_xreceive\_msg* expects the buffer to have been allocated by *malloc*, and reallocates it if the received message does not fit into the original buffer. Therefore both buffer and buffer length are given as pointers; their values can change by the call.

On success, the function returns *ERL\_MSG* and the *Emsg* struct is initialized as described above, or *ERL\_TICK*, in which case no message is returned. On failure, the function returns *ERL\_ERROR* and sets *erl\_errno* to one of:

- ***EMSGSIZE*:**

  Buffer is too small.

- ***ENOMEM*:**

  No more memory is available.

- ***EIO*:**

  I/O error.

**struct hostent \*erl\_gethostbyaddr(addr, length, type)**\
 **struct hostent \*erl\_gethostbyaddr\_r(addr, length, type, hostp, buffer, buflen, h\_errnop)**\
 **struct hostent \*erl\_gethostbyname(name)**\
 **struct hostent \*erl\_gethostbyname\_r(name, hostp, buffer, buflen, h\_errnop)**\

Types:

const char \*name;\
 const char \*addr;\
 int length;\
 int type;\
 struct hostent \*hostp;\
 char \*buffer;\
 int buflen;\
 int \*h\_errnop;\

Convenience functions for some common name lookup functions.

## DEBUG INFORMATION

If a connection attempt fails, the following can be checked:

- \*

  *erl\_errno*

- \*

  That the correct cookie was used

- \*

  That EPMD is running

- \*

  That the remote Erlang node on the other side is running the same version of Erlang as the *erl\_interface* library
