ei_connect
C Library
ei_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.
This module enables C-programs to communicate with Erlang nodes, using the Erlang distribution over TCP/IP.
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 is not shown in the listing provided by erlang:nodes/0
in ERTS
.
The environment variable ERL_EPMD_PORT
can be used to indicate which logical cluster a C-node belongs to.
Time-Out Functions
Most functions appear in a version with the suffix _tmo
appended to the function name. Those functions take an extra argument, a time-out in milliseconds. The semantics is this: for each communication primitive involved in the operation, if the primitive does not complete within the time specified, the function returns an error and erl_errno
is set to ETIMEDOUT
. With communication primitive is meant an operation on the socket, like connect
, accept
, recv
, or send
.
Clearly the time-outs are for implementing fault tolerance, not to keep hard real-time promises. The _tmo
functions are for detecting non-responsive peers and to avoid blocking on socket operations.
A time-out value of 0
(zero) means that time-outs are disabled. Calling a _tmo
function with the last argument as 0
is therefore the same thing as calling the function without the _tmo
suffix.
As with all other functions starting with ei_
, you are not expected to put the socket in non-blocking mode yourself in the program. Every use of non-blocking mode is embedded inside the time-out functions. The socket will always be back in blocking mode after the operations are completed (regardless of the result). To avoid problems, leave the socket options alone. ei
handles any socket options that need modification.
In all other senses, the _tmo
functions inherit all the return values and the semantics from the functions without the _tmo
suffix.
User Supplied Socket Implementation
By default ei
supplies a TCP/IPv4 socket interface that is used when communicating. The user can however plug in his/her own IPv4 socket implementation. This, for example, in order to communicate over TLS. A user supplied socket implementation is plugged in by passing a callback structure
to either ei_connect_init_ussi()
or ei_connect_xinit_ussi()
.
All callbacks in the ei_socket_callbacks
structure should return zero on success; and a posix error code on failure.
The addr
argument of the listen
, accept
, and connect
callbacks refer to appropriate address structure for currently used protocol. Currently ei
only supports IPv4. That is, at this time addr
always points to a struct sockaddr_in
structure.
The ei_socket_callbacks
structure may be enlarged in the future. All fields not set, needs to be zeroed out.
typedef struct { int flags; int (*socket)(void **ctx, void *setup_ctx); int (*close)(void *ctx); int (*listen)(void *ctx, void *addr, int *len, int backlog); int (*accept)(void **ctx, void *addr, int *len, unsigned tmo); int (*connect)(void *ctx, void *addr, int len, unsigned tmo); int (*writev)(void *ctx, const void *iov, int iovcnt, ssize_t *len, unsigned tmo); int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo); int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo); int (*handshake_packet_header_size)(void *ctx, int *sz); int (*connect_handshake_complete)(void *ctx); int (*accept_handshake_complete)(void *ctx); int (*get_fd)(void *ctx, int *fd); } ei_socket_callbacks;
flags
-
Flags informing
ei
about the behaviour of the callbacks. Flags should be bitwise or:ed together. If no flag, is set, theflags
field should contain0
. Currently, supported flags:EI_SCLBK_FLG_FULL_IMPL
-
If set, the
accept()
,connect()
,writev()
,write()
, andread()
callbacks implements timeouts. The timeout is passed in thetmo
argument and is given in milli seconds. Note that thetmo
argument to these callbacks differ from the timeout arguments in theei
API. Zero means a zero timeout. That is, poll and timeout immediately unless the operation is successful.EI_SCLBK_INF_TMO
(maxunsigned
) means infinite timeout. The file descriptor is in blocking mode when a callback is called, and it must be in blocking mode when the callback returns.If not set,
ei
will implement the timeout usingselect()
in order to determine when to call the callbacks and when to time out. Thetmo
arguments of theaccept()
,connect()
,writev()
,write()
, andread()
callbacks should be ignored. The callbacks may be called in non-blocking mode. The callbacks are not allowed to change between blocking and non-blocking mode. In order for this to work,select()
needs to interact with the socket primitives used the same way as it interacts with the ordinary socket primitives. If this is not the case, the callbacks need to implement timeouts and this flag should be set.
More flags may be introduced in the future.
int (*socket)(void **ctx, void *setup_ctx)
-
Create a socket and a context for the socket.
On success it should set
*ctx
to point to a context for the created socket. This context will be passed to all other socket callbacks. This function will be passed the samesetup_context
as passed to the preceedingei_connect_init_ussi()
orei_connect_xinit_ussi()
call.NoteDuring the lifetime of a socket, the pointer
*ctx
has to remain the same. That is, it cannot later be relocated.This callback is mandatory.
int (*close)(void *ctx)
-
Close the socket identified by
ctx
and destroy the context.This callback is mandatory.
int (*listen)(void *ctx, void *addr, int *len, int backlog)
-
Bind the socket identified by
ctx
to a local interface and then listen on it.The
addr
andlen
arguments are both input and output arguments. When calledaddr
points to an address structure of lenght*len
containing information on how to bind the socket. Uppon return this callback should have updated the structure referred byaddr
with information on how the socket actually was bound.*len
should be updated to reflect the size of*addr
updated.backlog
identifies the size of the backlog for the listen socket.This callback is mandatory.
int (*accept)(void **ctx, void *addr, int *len, unsigned tmo)
-
Accept connections on the listen socket identified by
*ctx
.When a connection is accepted, a new context for the accepted connection should be created and
*ctx
should be updated to point to the new context for the accepted connection. When calledaddr
points to an uninitialized address structure of lenght*len
. Uppon return this callback should have updated this structure with information about the client address.*len
should be updated to reflect the size of*addr
updated.If the
EI_SCLBK_FLG_FULL_IMPL
flag has been set,tmo
contains timeout time in milliseconds.NoteDuring the lifetime of a socket, the pointer
*ctx
has to remain the same. That is, it cannot later be relocated.This callback is mandatory.
int (*connect)(void *ctx, void *addr, int len, unsigned tmo)
-
Connect the socket identified by
ctx
to the address identified byaddr
.When called
addr
points to an address structure of lenghtlen
containing information on where to connect.If the
EI_SCLBK_FLG_FULL_IMPL
flag has been set,tmo
contains timeout time in milliseconds.This callback is mandatory.
int (*writev)(void *ctx, const void *iov, long iovcnt, ssize_t *len, unsigned tmo)
-
Write data on the connected socket identified by
ctx
.iov
points to an array ofstruct iovec
structures of lengthiovcnt
containing data to write to the socket. On success, this callback should set*len
to the amount of bytes successfully written on the socket.If the
EI_SCLBK_FLG_FULL_IMPL
flag has been set,tmo
contains timeout time in milliseconds.This callback is optional. Set the
writev
field in the theei_socket_callbacks
structure toNULL
if not implemented. int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo)
-
Write data on the connected socket identified by
ctx
.When called
buf
points to a buffer of length*len
containing the data to write on the socket. On success, this callback should set*len
to the amount of bytes successfully written on the socket.If the
EI_SCLBK_FLG_FULL_IMPL
flag has been set,tmo
contains timeout time in milliseconds.This callback is mandatory.
int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo)
-
Read data on the connected socket identified by
ctx
.buf
points to a buffer of length*len
where the read data should be placed. On success, this callback should update*len
to the amount of bytes successfully read on the socket.If the
EI_SCLBK_FLG_FULL_IMPL
flag has been set,tmo
contains timeout time in milliseconds.This callback is mandatory.
int (*handshake_packet_header_size)(void *ctx, int *sz)
-
Inform about handshake packet header size to use during the Erlang distribution handshake.
On success,
*sz
should be set to the handshake packet header size to use. Valid values are2
and4
. Erlang TCP distribution use a handshake packet size of2
and Erlang TLS distribution use a handshake packet size of4
.This callback is mandatory.
int (*connect_handshake_complete)(void *ctx)
-
Called when a locally started handshake has completed successfully.
This callback is optional. Set the
connect_handshake_complete
field in theei_socket_callbacks
structure toNULL
if not implemented. int (*accept_handshake_complete)(void *ctx)
-
Called when a remotely started handshake has completed successfully.
This callback is optional. Set the
accept_handshake_complete
field in theei_socket_callbacks
structure toNULL
if not implemented. int (*get_fd)(void *ctx, int *fd)
-
Inform about file descriptor used by the socket which is identified by
ctx
.NoteDuring the lifetime of a socket, the file descriptor has to remain the same. That is, repeated calls to this callback with the same context
should
always report the same file descriptor.The file descriptor has to be a real file descriptor. That is, no other operation should be able to get the same file descriptor until it has been released by the
close()
callback.This callback is mandatory.
Exports
struct hostent *ei_gethostbyaddr( |
struct hostent *ei_gethostbyaddr_r( |
struct hostent *ei_gethostbyname( |
struct hostent *ei_gethostbyname_r( |
Convenience functions for some common name lookup functions.
int ei_accept( |
Used by a server process to accept a connection from a client process.
-
ec
is the C-node structure. -
listensock
is an open socket descriptor on whichlisten()
has previously been called. -
conp
is a pointer to anErlConnect
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 ei_accept_tmo( |
Equivalent to ei_accept
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_close_connection( | OTP 21.3 |
Closes a previously opened connection or listen socket.
int ei_connect( |
int ei_xconnect( |
Sets up a connection to an Erlang node.
ei_xconnect()
requires the IP address of the remote host and the alive name of the remote node to be specified. ei_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.
These functions return an open file descriptor on success, or a negative value indicating that an error occurred. In the latter case they set erl_errno
to one of the following:
EHOSTUNREACH
- The remote host
node
is unreachable. ENOMEM
- No more memory is available.
EIO
- I/O error.
Also, errno
values from socket
(2) and connect
(2) system calls may be propagated into erl_errno
.
Example:
#define NODE "[email protected]" #define ALIVE "madonna" #define IP_ADDR "150.236.14.75" /*** Variant 1 ***/ int fd = ei_connect(&ec, NODE); /*** Variant 2 ***/ struct in_addr addr; addr.s_addr = inet_addr(IP_ADDR); fd = ei_xconnect(&ec, &addr, ALIVE);
int ei_connect_init( |
int ei_connect_init_ussi( | OTP 21.3 |
int ei_connect_xinit( |
int ei_connect_xinit_ussi( | OTP 21.3 |
Initializes the ec
structure, to identify the node name and cookie of the server. One of them must be called before other functions that works on the ei_cnode
type or a file descriptor associated with a connection to another node is used.
-
ec
is a structure containing information about the C-node. It is used in otherei
functions for connecting and receiving data. -
this_node_name
is the registered name of the process (the name before '@'). -
cookie
is the cookie for the node. -
creation
identifies a specific instance of a C-node. It can help prevent the node from receiving messages sent to an earlier process with the same registered name. -
thishostname
is the name of the machine we are running on. If long names are to be used, they are to be fully qualified (that is,durin.erix.ericsson.se
instead ofdurin
). -
thisalivename
is the registered name of the process. -
thisnodename
is the full name of the node, that is,einode@durin
. -
thispaddr
if the IP address of the host. -
cbs
is a pointer to acallback structure
implementing and alternative socket interface. -
cbs_sz
is the size of the structure pointed to bycbs
. -
setup_context
is a pointer to a structure that will be passed as second argument to thesocket
callback in thecbs
structure.
A C-node acting as a server is assigned a creation number when it calls ei_publish()
.
A connection is closed by simply closing the socket. For information about how to close the socket gracefully (when there are outgoing packets before close), see the relevant system documentation.
These functions return a negative value indicating that an error occurred.
Example 1:
int n = 0; struct in_addr addr; ei_cnode ec; addr.s_addr = inet_addr("150.236.14.75"); if (ei_connect_xinit(&ec, "chivas", "madonna", "[email protected]", &addr; "cookie...", n++) < 0) { fprintf(stderr,"ERROR when initializing: %d",erl_errno); exit(-1); }
Example 2:
if (ei_connect_init(&ec, "madonna", "cookie...", n++) < 0) { fprintf(stderr,"ERROR when initializing: %d",erl_errno); exit(-1); }
int ei_connect_tmo( |
int ei_xconnect_tmo( |
Equivalent to ei_connect
and ei_xconnect
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_get_tracelevel(void) | OTP R13B04 |
void ei_set_tracelevel( | OTP R13B04 |
Used to set tracing on the distribution. The levels are different verbosity levels. A higher level means more information. See also section Debug Information
.
These functions are not thread safe.
int ei_listen( | OTP 21.3 |
int ei_xlisten( | OTP 21.3 |
Used by a server process to setup a listen socket which later can be used for accepting connections from client processes.
-
ec
is the C-node structure. -
adr
is local interface to bind to. -
port
is a pointer to an integer containing the port number to bind to. If*port
equals0
when callingei_listen()
, the socket will be bound to an ephemeral port. On success,ei_listen()
will update the value of*port
to the port actually bound to. -
backlog
is maximum backlog of pending connections.
ei_listen
will create a socket, bind to a port on the local interface identified by adr
(or all local interfaces if ei_listen()
is called), and mark the socket as a passive socket (that is, a socket that will be used for accepting incoming connections).
On success, a file descriptor is returned which can be used in a call to ei_accept()
. On failure, ERL_ERROR
is returned and erl_errno
is set to EIO
.
int ei_publish( |
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 either of these functions, the process should have called bind()
and listen()
on an open socket.
-
ec
is the C-node structure. -
port
is the local name to register, and is to be the same as the port number that was previously bound to the socket. -
addr
is the 32-bit IP address of the local host.
To unregister with EPMD, simply close the returned descriptor. Do not use ei_unpublish()
, which is deprecated anyway.
On success, the function returns a descriptor connecting the calling process to EPMD. On failure, -1
is returned and erl_errno
is set to EIO
.
Also, errno
values from socket
(2) and connect
(2) system calls may be propagated into erl_errno
.
int ei_publish_tmo( |
Equivalent to ei_publish
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_receive( |
Receives a message consisting of a sequence of bytes in the Erlang external format.
-
fd
is an open descriptor to an Erlang connection. It is obtained from a previousei_connect
orei_accept
. -
bufp
is a buffer large enough to hold the expected message. -
bufsize
indicates 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 ERL_ERROR
and sets erl_errno
to one of the following:
EAGAIN
- Temporary error: Try again.
EMSGSIZE
- Buffer is too small.
EIO
- I/O error.
int ei_receive_encoded( |
This function is retained for compatibility with code generated by the interface compiler and with code following examples in the same application.
In essence, the function performs the same operation as ei_xreceive_msg
, but instead of using an ei_x_buff
, the function expects a pointer to a character pointer (mbufp
), where the character pointer is to point to a memory area allocated by malloc
. Argument bufsz
is to be a pointer to an integer containing the exact size (in bytes) of the memory area. The function may reallocate the memory area and will in such cases put the new size in *bufsz
and update *mbufp
.
Returns either ERL_TICK
or the msgtype
field of the erlang_msg *msg
. The length of the message is put in *msglen
. On error a value < 0
is returned.
It is recommended to use ei_xreceive_msg
instead when possible, for the sake of readability. However, the function will be retained in the interface for compatibility and will not be removed in future releases without prior notice.
int ei_receive_encoded_tmo( |
Equivalent to ei_receive_encoded
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_receive_msg( |
int ei_xreceive_msg( |
Receives a message to the buffer in x
. ei_xreceive_msg
allows the buffer in x
to grow, but ei_receive_msg
fails if the message is larger than the pre-allocated buffer in x
.
-
fd
is an open descriptor to an Erlang connection. -
msg
is a pointer to anerlang_msg
structure and contains information on the message received. -
x
is buffer obtained fromei_x_new
.
On success, the functions return ERL_MSG
and the msg
struct is initialized. erlang_msg
is defined as follows:
typedef struct { long msgtype; erlang_pid from; erlang_pid to; char toname[MAXATOMLEN+1]; char cookie[MAXATOMLEN+1]; erlang_trace token; } erlang_msg;
msgtype
identifies the type of message, and is one of the following:
ERL_SEND
-
Indicates that an ordinary send operation has occurred.
msg->to
contains the pid of the recipient (the C-node). ERL_REG_SEND
-
A registered send operation occurred.
msg->from
contains the pid of the sender. ERL_LINK
orERL_UNLINK
-
msg->to
andmsg->from
contain the pids of the sender and recipient of the link or unlink. ERL_EXIT
-
Indicates a broken link.
msg->to
andmsg->from
contain the pids of the linked processes.
The return value is the same as for ei_receive
.
int ei_receive_msg_tmo( |
int ei_xreceive_msg_tmo( |
Equivalent to ei_receive_msg
and ei_xreceive_msg
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_receive_tmo( |
Equivalent to ei_receive
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_reg_send( |
Sends an Erlang term to a registered process.
-
fd
is an open descriptor to an Erlang connection. -
server_name
is the registered name of the intended recipient. -
buf
is the buffer containing the term in binary format. -
len
is the length of the message in bytes.
Returns 0
if successful, otherwise -1
. In the latter case it sets erl_errno
to EIO
.
Example:
Send the atom "ok" to the process "worker":
ei_x_buff x; ei_x_new_with_version(&x); ei_x_encode_atom(&x, "ok"); if (ei_reg_send(&ec, fd, x.buff, x.index) < 0) handle_error();
int ei_reg_send_tmo( |
Equivalent to ei_reg_send
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_rpc( |
int ei_rpc_to( |
int ei_rpc_from( |
Supports calling Erlang functions on remote nodes. ei_rpc_to()
sends an RPC request to a remote node and ei_rpc_from()
receives the results of such a call. ei_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.
-
ec
is the C-node structure previously initiated by a call toei_connect_init()
orei_connect_xinit()
. -
fd
is an open descriptor to an Erlang connection. -
timeout
is the maximum time (in milliseconds) to wait for results. SpecifyERL_NO_TIMEOUT
to wait forever.ei_rpc()
waits infinitely for the answer, that is, the call will never time out. -
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. -
argbuf
is a pointer to a buffer with an encoded Erlang list, without a version magic number, containing the arguments to be passed to the function. -
argbuflen
is the length of the buffer containing the encoded Erlang list. -
msg
is structure of typeerlang_msg
and contains information on the message received. For a description of theerlang_msg
format, seeei_receive_msg
. -
x
points to the dynamic buffer that receives the result. Forei_rpc()
this is the result without the version magic number. Forei_rpc_from()
the result returns a version magic number and a 2-tuple{rex,Reply}
.
ei_rpc()
returns the number of bytes in the result on success and -1
on failure. ei_rpc_from()
returns the number of bytes, otherwise one of ERL_TICK
, ERL_TIMEOUT
, and ERL_ERROR
. When failing, all three functions set erl_errno
to one of the following:
EIO
- I/O error.
ETIMEDOUT
- Time-out expired.
EAGAIN
- Temporary error: Try again.
Example:
Check to see if an Erlang process is alive:
int index = 0, is_alive; ei_x_buff args, result; ei_x_new(&result); ei_x_new(&args); ei_x_encode_list_header(&args, 1); ei_x_encode_pid(&args, &check_pid); ei_x_encode_empty_list(&args); if (ei_rpc(&ec, fd, "erlang", "is_process_alive", args.buff, args.index, &result) < 0) handle_error(); if (ei_decode_version(result.buff, &index) < 0 || ei_decode_bool(result.buff, &index, &is_alive) < 0) handle_error();
erlang_pid *ei_self( |
Retrieves the pid of the C-node. Every C-node has a (pseudo) pid used in ei_send_reg
, ei_rpc
, and others. This is contained in a field in the ec
structure. It will be safe for a long time to fetch this field directly from the ei_cnode
structure.
int ei_send( |
Sends an Erlang term to a process.
-
fd
is an open descriptor to an Erlang connection. -
to
is the pid of the intended recipient of the message. -
buf
is the buffer containing the term in binary format. -
len
is the length of the message in bytes.
Returns 0
if successful, otherwise -1
. In the latter case it sets erl_errno
to EIO
.
int ei_send_encoded( |
Works exactly as ei_send
, the alternative name is retained for backward compatibility. The function will not be removed without prior notice.
int ei_send_encoded_tmo( |
Equivalent to ei_send_encoded
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_send_reg_encoded( |
This function is retained for compatibility with code generated by the interface compiler and with code following examples in the same application.
The function works as ei_reg_send
with one exception. Instead of taking ei_cnode
as first argument, it takes a second argument, an erlang_pid
, which is to be the process identifier of the sending process (in the Erlang distribution protocol).
A suitable erlang_pid
can be constructed from the ei_cnode
structure by the following example code:
ei_cnode ec; erlang_pid *self; int fd; /* the connection fd */ ... self = ei_self(&ec); self->num = fd;
int ei_send_reg_encoded_tmo( |
Equivalent to ei_send_reg_encoded
with an optional time-out argument, see the description at the beginning of this manual page.
int ei_send_tmo( |
Equivalent to ei_send
with an optional time-out argument, see the description at the beginning of this manual page.
const char *ei_thisnodename( |
const char *ei_thishostname( |
const char *ei_thisalivename( |
Can be used to retrieve information about the C-node. These values are initially set with ei_connect_init()
or ei_connect_xinit()
.
These function simply fetch the appropriate field from the ec
structure. Read the field directly will probably be safe for a long time, so these functions are not really needed.
int ei_unpublish( |
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 close the descriptor that was returned by ei_publish()
.
This function is deprecated and will be removed in a future release.
ec
is the node structure of the node to unregister.
If the node was successfully unregistered from EPMD, the function returns 0
. Otherwise, -1
is returned and erl_errno
is set to EIO
.
int ei_unpublish_tmo( |
Equivalent to ei_unpublish
with an optional time-out argument, see the description at the beginning of this manual page.
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
ei
library - That environment variable
ERL_EPMD_PORT
is set correctly
The connection attempt can be traced by setting a trace level by either using ei_set_tracelevel
or by setting environment variable EI_TRACELEVEL
. The trace levels have the following messages:
- 1: Verbose error messages
- 2: Above messages and verbose warning messages
- 3: Above messages and progress reports for connection handling
- 4: Above messages and progress reports for communication
- 5: Above messages and progress reports for data conversion
© 2010–2020 Ericsson AB
Licensed under the Apache License, Version 2.0.