erl_connect
C Library
erl_connect
Library Summary
Communicate with distributed Erlang.
Description
The support for VxWorks is deprecated as of OTP 22, and will be removed in OTP 23.
The old legacy erl_interface library (functions with prefix erl_) is deprecated as of OTP 22, and will be removed in OTP 23. This does not apply to the ei library. Reasonably new gcc compilers will issue deprecation warnings. In order to disable these warnings, define the macro EI_NO_DEPR_WARN.
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( | 
Types
This function is used by a server process to accept a connection from a client process.
-  listensockis an open socket descriptor on whichlisten()has previously been called.
-  conpis a pointer to anErlConnectstruct, 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( | 
Types
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( | 
| int erl_xconnect( | 
Types
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.
-  addris the 32-bit IP address of the remote host.
-  aliveis the alivename of the remote node.
-  nodeis 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 nodeis unreachable.
- ENOMEM
- No more memory is available.
- EIO
- I/O error.
Also, errno values from socket(2) and connect(2) system calls can be propagated into erl_errno.
Example:
#define NODE "[email protected]" #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( | 
| int erl_connect_xinit( | 
Types
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.
-  hostis the name of the host on which the node is running.
-  aliveis the alivename of the node.
-  nodeis the node name. It is to be of the form alivename@hostname.
-  addris the 32-bit IP address ofhost.
-  cookieis the authorization string required for access to the remote node. IfNULL, the userHOMEdirectory is searched for a cookie file.erlang.cookie. The path to the home directory is retrieved from environment variableHOMEon Unix and from theHOMEDRIVEandHOMEPATHvariables on Windows. For more details, see theauthmodule in Kernel.
-  creationhelps 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, "[email protected]" is the resulting node name.
Example 1:
struct in_addr addr;
addr = inet_addr("150.236.14.75");
if (!erl_connect_xinit("chivas",
                       "madonna",
                       "[email protected]",
                       &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( | 
Types
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) and connect(2) system calls can be propagated into erl_errno.
| int erl_receive( | 
Types
Receives a message consisting of a sequence of bytes in the Erlang external format.
-  fdis an open descriptor to an Erlang connection.
-  bufpis a buffer large enough to hold the expected message.
-  bufsizeindicates the size ofbufp.
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( | 
Types
Receives the message into the specified buffer and decodes into (ErlMessage *) emsg.
-  fdis an open descriptor to an Erlang connection.
-  bufpis a buffer large enough to hold the expected message.
-  bufsizeindicates the size ofbufp.
- >emsgis a pointer to anErlMessagestructure into which the message will be decoded.ErlMessageis defined as follows:
typedef struct {
  int type;
  ETERM *msg;
  ETERM *to;
  ETERM *from;
  char to_name[MAXREGLEN];
} ErlMessage; 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->tocontains the pid of the recipient. The message is inemsg->msg.
- ERL_REG_SEND
-  A registered send operation has occurred and emsg->fromcontains the pid of the sender. The message is inemsg->msg.
- ERL_LINKor- ERL_UNLINK
-  emsg->toandemsg->fromcontain the pids of the sender and recipient of the link or unlink.emsg->msgis not used.
- ERL_EXIT
-  A link is broken. emsg->toandemsg->fromcontain the pids of the linked processes, andemsg->msgcontains the reason for the exit.
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( | 
Types
Sends an Erlang term to a registered process.
-  fdis an open descriptor to an Erlang connection.
-  tois a string containing the registered name of the intended recipient of the message.
-  msgis 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( | 
| int erl_rpc_from( | 
| int erl_rpc_to( | 
Types
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.
-  fdis an open descriptor to an Erlang connection.
-  timeoutis the maximum time (in milliseconds) to wait for results. To wait forever, specifyERL_NO_TIMEOUT. Whenerl_rpc()callserl_rpc_from(), the call will never timeout.
-  modis the name of the module containing the function to be run on the remote node.
-  funis the name of the function to run.
-  argsis an Erlang list, containing the arguments to be passed to the function.
-  emsgis 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.
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( | 
Types
Sends an Erlang term to a process.
-  fdis an open descriptor to an Erlang connection.
-  tois an Erlang term containing the pid of the intended recipient of the message.
- >msgis 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: tois not a valid Erlang pid.
- ENOMEM
- No more memory is available.
- EIO
- I/O error.
| const char *erl_thisalivename() | 
|  | 
| 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( | 
Types
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().
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( | 
Types
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( | 
| struct hostent *erl_gethostbyaddr_r( | 
| struct hostent *erl_gethostbyname( | 
| struct hostent *erl_gethostbyname_r( | 
Types
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_interfacelibrary
    © 2010–2020 Ericsson AB
Licensed under the Apache License, Version 2.0.