1ei_connect(3) C Library Functions ei_connect(3)
2
3
4
6 ei_connect - Communicate with distributed Erlang.
7
9 This module enables C-programs to communicate with Erlang nodes, using
10 the Erlang distribution over TCP/IP.
11
12 A C-node appears to Erlang as a hidden node. That is, Erlang processes
13 that know the name of the C-node can communicate with it in a normal
14 manner, but the node name is not shown in the listing provided by
15 erlang:nodes/0 in ERTS.
16
17 The environment variable ERL_EPMD_PORT can be used to indicate which
18 logical cluster a C-node belongs to.
19
21 Most functions appear in a version with the suffix _tmo appended to the
22 function name. Those functions take an extra argument, a time-out in
23 milliseconds. The semantics is this: for each communication primitive
24 involved in the operation, if the primitive does not complete within
25 the time specified, the function returns an error and erl_errno is set
26 to ETIMEDOUT. With communication primitive is meant an operation on the
27 socket, like connect, accept, recv, or send.
28
29 Clearly the time-outs are for implementing fault tolerance, not to keep
30 hard real-time promises. The _tmo functions are for detecting non-
31 responsive peers and to avoid blocking on socket operations.
32
33 A time-out value of 0 (zero) means that time-outs are disabled. Calling
34 a _tmo function with the last argument as 0 is therefore the same thing
35 as calling the function without the _tmo suffix.
36
37 As with all other functions starting with ei_, you are not expected to
38 put the socket in non-blocking mode yourself in the program. Every use
39 of non-blocking mode is embedded inside the time-out functions. The
40 socket will always be back in blocking mode after the operations are
41 completed (regardless of the result). To avoid problems, leave the
42 socket options alone. ei handles any socket options that need modifica‐
43 tion.
44
45 In all other senses, the _tmo functions inherit all the return values
46 and the semantics from the functions without the _tmo suffix.
47
49 By default ei supplies a TCP/IPv4 socket interface that is used when
50 communicating. The user can however plug in his/her own IPv4 socket
51 implementation. This, for example, in order to communicate over TLS. A
52 user supplied socket implementation is plugged in by passing a callback
53 structure to either ei_connect_init_ussi() or ei_connect_xinit_ussi().
54
55 All callbacks in the ei_socket_callbacks structure should return zero
56 on success; and a posix error code on failure.
57
58 The addr argument of the listen, accept, and connect callbacks refer to
59 appropriate address structure for currently used protocol. Currently ei
60 only supports IPv4. That is, at this time addr always points to a
61 struct sockaddr_in structure.
62
63 The ei_socket_callbacks structure may be enlarged in the future. All
64 fields not set, needs to be zeroed out.
65
66 typedef struct {
67 int flags;
68 int (*socket)(void **ctx, void *setup_ctx);
69 int (*close)(void *ctx);
70 int (*listen)(void *ctx, void *addr, int *len, int backlog);
71 int (*accept)(void **ctx, void *addr, int *len, unsigned tmo);
72 int (*connect)(void *ctx, void *addr, int len, unsigned tmo);
73 int (*writev)(void *ctx, const void *iov, int iovcnt, ssize_t *len, unsigned tmo);
74 int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo);
75 int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo);
76 int (*handshake_packet_header_size)(void *ctx, int *sz);
77 int (*connect_handshake_complete)(void *ctx);
78 int (*accept_handshake_complete)(void *ctx);
79 int (*get_fd)(void *ctx, int *fd);
80 } ei_socket_callbacks;
81
82
83 flags:
84 Flags informing ei about the behaviour of the callbacks. Flags
85 should be bitwise or:ed together. If no flag, is set, the flags
86 field should contain 0. Currently, supported flags:
87
88 EI_SCLBK_FLG_FULL_IMPL:
89 If set, the accept(), connect(), writev(), write(), and read()
90 callbacks implements timeouts. The timeout is passed in the tmo
91 argument and is given in milli seconds. Note that the tmo argu‐
92 ment to these callbacks differ from the timeout arguments in the
93 ei API. Zero means a zero timeout. That is, poll and timeout
94 immediately unless the operation is successful. EI_SCLBK_INF_TMO
95 (max unsigned) means infinite timeout. The file descriptor is in
96 blocking mode when a callback is called, and it must be in block‐
97 ing mode when the callback returns.
98
99 If not set, ei will implement the timeout using select() in order
100 to determine when to call the callbacks and when to time out. The
101 tmo arguments of the accept(), connect(), writev(), write(), and
102 read() callbacks should be ignored. The callbacks may be called
103 in non-blocking mode. The callbacks are not allowed to change
104 between blocking and non-blocking mode. In order for this to
105 work, select() needs to interact with the socket primitives used
106 the same way as it interacts with the ordinary socket primitives.
107 If this is not the case, the callbacks need to implement timeouts
108 and this flag should be set.
109
110 More flags may be introduced in the future.
111
112 int (*socket)(void **ctx, void *setup_ctx):
113 Create a socket and a context for the socket.
114
115 On success it should set *ctx to point to a context for the created
116 socket. This context will be passed to all other socket callbacks.
117 This function will be passed the same setup_context as passed to
118 the preceeding ei_connect_init_ussi() or ei_connect_xinit_ussi()
119 call.
120
121 Note:
122 During the lifetime of a socket, the pointer *ctx has to remain the
123 same. That is, it cannot later be relocated.
124
125
126 This callback is mandatory.
127
128 int (*close)(void *ctx):
129 Close the socket identified by ctx and destroy the context.
130
131 This callback is mandatory.
132
133 int (*listen)(void *ctx, void *addr, int *len, int backlog):
134 Bind the socket identified by ctx to a local interface and then
135 listen on it.
136
137 The addr and len arguments are both input and output arguments.
138 When called addr points to an address structure of lenght *len con‐
139 taining information on how to bind the socket. Uppon return this
140 callback should have updated the structure referred by addr with
141 information on how the socket actually was bound. *len should be
142 updated to reflect the size of *addr updated. backlog identifies
143 the size of the backlog for the listen socket.
144
145 This callback is mandatory.
146
147 int (*accept)(void **ctx, void *addr, int *len, unsigned tmo):
148 Accept connections on the listen socket identified by *ctx.
149
150 When a connection is accepted, a new context for the accepted con‐
151 nection should be created and *ctx should be updated to point to
152 the new context for the accepted connection. When called addr
153 points to an uninitialized address structure of lenght *len. Uppon
154 return this callback should have updated this structure with infor‐
155 mation about the client address. *len should be updated to reflect
156 the size of *addr updated.
157
158 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
159 out time in milliseconds.
160
161 Note:
162 During the lifetime of a socket, the pointer *ctx has to remain the
163 same. That is, it cannot later be relocated.
164
165
166 This callback is mandatory.
167
168 int (*connect)(void *ctx, void *addr, int len, unsigned tmo):
169 Connect the socket identified by ctx to the address identified by
170 addr.
171
172 When called addr points to an address structure of lenght len con‐
173 taining information on where to connect.
174
175 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
176 out time in milliseconds.
177
178 This callback is mandatory.
179
180 int (*writev)(void *ctx, const void *iov, long iovcnt, ssize_t *len,
181 unsigned tmo):
182 Write data on the connected socket identified by ctx.
183
184 iov points to an array of struct iovec structures of length iovcnt
185 containing data to write to the socket. On success, this callback
186 should set *len to the amount of bytes successfully written on the
187 socket.
188
189 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
190 out time in milliseconds.
191
192 This callback is optional. Set the writev field in the the
193 ei_socket_callbacks structure to NULL if not implemented.
194
195 int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo):
196 Write data on the connected socket identified by ctx.
197
198 When called buf points to a buffer of length *len containing the
199 data to write on the socket. On success, this callback should set
200 *len to the amount of bytes successfully written on the socket.
201
202 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
203 out time in milliseconds.
204
205 This callback is mandatory.
206
207 int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo):
208 Read data on the connected socket identified by ctx.
209
210 buf points to a buffer of length *len where the read data should be
211 placed. On success, this callback should update *len to the amount
212 of bytes successfully read on the socket.
213
214 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
215 out time in milliseconds.
216
217 This callback is mandatory.
218
219 int (*handshake_packet_header_size)(void *ctx, int *sz):
220 Inform about handshake packet header size to use during the Erlang
221 distribution handshake.
222
223 On success, *sz should be set to the handshake packet header size
224 to use. Valid values are 2 and 4. Erlang TCP distribution use a
225 handshake packet size of 2 and Erlang TLS distribution use a hand‐
226 shake packet size of 4.
227
228 This callback is mandatory.
229
230 int (*connect_handshake_complete)(void *ctx):
231 Called when a locally started handshake has completed successfully.
232
233 This callback is optional. Set the connect_handshake_complete field
234 in the ei_socket_callbacks structure to NULL if not implemented.
235
236 int (*accept_handshake_complete)(void *ctx):
237 Called when a remotely started handshake has completed success‐
238 fully.
239
240 This callback is optional. Set the accept_handshake_complete field
241 in the ei_socket_callbacks structure to NULL if not implemented.
242
243 int (*get_fd)(void *ctx, int *fd):
244 Inform about file descriptor used by the socket which is identified
245 by ctx.
246
247 Note:
248 During the lifetime of a socket, the file descriptor has to remain
249 the same. That is, repeated calls to this callback with the same con‐
250 text should always report the same file descriptor.
251
252 The file descriptor has to be a real file descriptor. That is, no
253 other operation should be able to get the same file descriptor until
254 it has been released by the close() callback.
255
256
257 This callback is mandatory.
258
260 struct hostent *ei_gethostbyaddr(const char *addr, int len, int type)
261 struct hostent *ei_gethostbyaddr_r(const char *addr, int length, int
262 type, struct hostent *hostp, char *buffer, int buflen, int
263 *h_errnop)
264 struct hostent *ei_gethostbyname(const char *name)
265 struct hostent *ei_gethostbyname_r(const char *name, struct hostent
266 *hostp, char *buffer, int buflen, int *h_errnop)
267
268 Convenience functions for some common name lookup functions.
269
270 int ei_accept(ei_cnode *ec, int listensock, ErlConnect *conp)
271
272 Used by a server process to accept a connection from a client
273 process.
274
275 * ec is the C-node structure.
276
277 * listensock is an open socket descriptor on which listen()
278 has previously been called.
279
280 * conp is a pointer to an ErlConnect struct, described as fol‐
281 lows:
282
283 typedef struct {
284 char ipadr[4];
285 char nodename[MAXNODELEN];
286 } ErlConnect;
287
288
289 On success, conp is filled in with the address and node name of
290 the connecting client and a file descriptor is returned. On
291 failure, ERL_ERROR is returned and erl_errno is set to EIO.
292
293 int ei_accept_tmo(ei_cnode *ec, int listensock, ErlConnect *conp,
294 unsigned timeout_ms)
295
296 Equivalent to ei_accept with an optional time-out argument, see
297 the description at the beginning of this manual page.
298
299 int ei_close_connection(int fd)
300
301 Closes a previously opened connection or listen socket.
302
303 int ei_connect(ei_cnode* ec, char *nodename)
304 int ei_xconnect(ei_cnode* ec, Erl_IpAddr adr, char *alivename)
305
306 Sets up a connection to an Erlang node.
307
308 ei_xconnect() requires the IP address of the remote host and the
309 alive name of the remote node to be specified. ei_connect() pro‐
310 vides an alternative interface and determines the information
311 from the node name provided.
312
313 * addr is the 32-bit IP address of the remote host.
314
315 * alive is the alivename of the remote node.
316
317 * node is the name of the remote node.
318
319 These functions return an open file descriptor on success, or a
320 negative value indicating that an error occurred. In the latter
321 case they set erl_errno to one of the following:
322
323 EHOSTUNREACH:
324 The remote host node is unreachable.
325
326 ENOMEM:
327 No more memory is available.
328
329 EIO:
330 I/O error.
331
332 Also, errno values from socket(2) and connect(2) system calls
333 may be propagated into erl_errno.
334
335 Example:
336
337 #define NODE "madonna@chivas.du.etx.ericsson.se"
338 #define ALIVE "madonna"
339 #define IP_ADDR "150.236.14.75"
340
341 /*** Variant 1 ***/
342 int fd = ei_connect(&ec, NODE);
343
344 /*** Variant 2 ***/
345 struct in_addr addr;
346 addr.s_addr = inet_addr(IP_ADDR);
347 fd = ei_xconnect(&ec, &addr, ALIVE);
348
349
350 int ei_connect_init(ei_cnode* ec, const char* this_node_name, const
351 char *cookie, short creation)
352 int ei_connect_init_ussi(ei_cnode* ec, const char* this_node_name,
353 const char *cookie, short creation, ei_socket_callbacks *cbs, int
354 cbs_sz, void *setup_context)
355 int ei_connect_xinit(ei_cnode* ec, const char *thishostname, const char
356 *thisalivename, const char *thisnodename, Erl_IpAddr thisipaddr, const
357 char *cookie, short creation)
358 int ei_connect_xinit_ussi(ei_cnode* ec, const char *thishostname, const
359 char *thisalivename, const char *thisnodename, Erl_IpAddr thisipaddr,
360 const char *cookie, short creation, ei_socket_callbacks *cbs, int
361 cbs_sz, void *setup_context)
362
363 Initializes the ec structure, to identify the node name and
364 cookie of the server. One of them must be called before other
365 functions that works on the ei_cnode type or a file descriptor
366 associated with a connection to another node is used.
367
368 * ec is a structure containing information about the C-node.
369 It is used in other ei functions for connecting and receiv‐
370 ing data.
371
372 * this_node_name is the registered name of the process (the
373 name before '@').
374
375 * cookie is the cookie for the node.
376
377 * creation identifies a specific instance of a C-node. It can
378 help prevent the node from receiving messages sent to an
379 earlier process with the same registered name.
380
381 * thishostname is the name of the machine we are running on.
382 If long names are to be used, they are to be fully qualified
383 (that is, durin.erix.ericsson.se instead of durin).
384
385 * thisalivename is the registered name of the process.
386
387 * thisnodename is the full name of the node, that is, ein‐
388 ode@durin.
389
390 * thispaddr if the IP address of the host.
391
392 * cbs is a pointer to a callback structure implementing and
393 alternative socket interface.
394
395 * cbs_sz is the size of the structure pointed to by cbs.
396
397 * setup_context is a pointer to a structure that will be
398 passed as second argument to the socket callback in the cbs
399 structure.
400
401 A C-node acting as a server is assigned a creation number when
402 it calls ei_publish().
403
404 A connection is closed by simply closing the socket. For infor‐
405 mation about how to close the socket gracefully (when there are
406 outgoing packets before close), see the relevant system documen‐
407 tation.
408
409 These functions return a negative value indicating that an error
410 occurred.
411
412 Example 1:
413
414 int n = 0;
415 struct in_addr addr;
416 ei_cnode ec;
417 addr.s_addr = inet_addr("150.236.14.75");
418 if (ei_connect_xinit(&ec,
419 "chivas",
420 "madonna",
421 "madonna@chivas.du.etx.ericsson.se",
422 &addr;
423 "cookie...",
424 n++) < 0) {
425 fprintf(stderr,"ERROR when initializing: %d",erl_errno);
426 exit(-1);
427 }
428
429
430 Example 2:
431
432 if (ei_connect_init(&ec, "madonna", "cookie...", n++) < 0) {
433 fprintf(stderr,"ERROR when initializing: %d",erl_errno);
434 exit(-1);
435 }
436
437
438 int ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned timeout_ms)
439 int ei_xconnect_tmo(ei_cnode* ec, Erl_IpAddr adr, char *alivename,
440 unsigned timeout_ms)
441
442 Equivalent to ei_connect and ei_xconnect with an optional time-
443 out argument, see the description at the beginning of this man‐
444 ual page.
445
446 int ei_get_tracelevel(void)
447 void ei_set_tracelevel(int level)
448
449 Used to set tracing on the distribution. The levels are differ‐
450 ent verbosity levels. A higher level means more information. See
451 also section Debug Information.
452
453 These functions are not thread safe.
454
455 int ei_listen(ei_cnode *ec, int *port, int backlog)
456 int ei_xlisten(ei_cnode *ec, Erl_IpAddr adr, int *port, int backlog)
457
458 Used by a server process to setup a listen socket which later
459 can be used for accepting connections from client processes.
460
461 * ec is the C-node structure.
462
463 * adr is local interface to bind to.
464
465 * port is a pointer to an integer containing the port number
466 to bind to. If *port equals 0 when calling ei_listen(), the
467 socket will be bound to an ephemeral port. On success,
468 ei_listen() will update the value of *port to the port actu‐
469 ally bound to.
470
471 * backlog is maximum backlog of pending connections.
472
473 ei_listen will create a socket, bind to a port on the local
474 interface identified by adr (or all local interfaces if ei_lis‐
475 ten() is called), and mark the socket as a passive socket (that
476 is, a socket that will be used for accepting incoming connec‐
477 tions).
478
479 On success, a file descriptor is returned which can be used in a
480 call to ei_accept(). On failure, ERL_ERROR is returned and
481 erl_errno is set to EIO.
482
483 int ei_publish(ei_cnode *ec, int port)
484
485 Used by a server process to register with the local name server
486 EPMD, thereby allowing other processes to send messages by using
487 the registered name. Before calling either of these functions,
488 the process should have called bind() and listen() on an open
489 socket.
490
491 * ec is the C-node structure.
492
493 * port is the local name to register, and is to be the same as
494 the port number that was previously bound to the socket.
495
496 * addr is the 32-bit IP address of the local host.
497
498 To unregister with EPMD, simply close the returned descriptor.
499 Do not use ei_unpublish(), which is deprecated anyway.
500
501 On success, the function returns a descriptor connecting the
502 calling process to EPMD. On failure, -1 is returned and
503 erl_errno is set to EIO.
504
505 Also, errno values from socket(2) and connect(2) system calls
506 may be propagated into erl_errno.
507
508 int ei_publish_tmo(ei_cnode *ec, int port, unsigned timeout_ms)
509
510 Equivalent to ei_publish with an optional time-out argument, see
511 the description at the beginning of this manual page.
512
513 int ei_receive(int fd, unsigned char* bufp, int bufsize)
514
515 Receives a message consisting of a sequence of bytes in the
516 Erlang external format.
517
518 * fd is an open descriptor to an Erlang connection. It is
519 obtained from a previous ei_connect or ei_accept.
520
521 * bufp is a buffer large enough to hold the expected message.
522
523 * bufsize indicates the size of bufp.
524
525 If a tick occurs, that is, the Erlang node on the other end of
526 the connection has polled this node to see if it is still alive,
527 the function returns ERL_TICK and no message is placed in the
528 buffer. Also, erl_errno is set to EAGAIN.
529
530 On success, the message is placed in the specified buffer and
531 the function returns the number of bytes actually read. On fail‐
532 ure, the function returns ERL_ERROR and sets erl_errno to one of
533 the following:
534
535 EAGAIN:
536 Temporary error: Try again.
537
538 EMSGSIZE:
539 Buffer is too small.
540
541 EIO:
542 I/O error.
543
544 int ei_receive_encoded(int fd, char **mbufp, int *bufsz, erlang_msg
545 *msg, int *msglen)
546
547 This function is retained for compatibility with code generated
548 by the interface compiler and with code following examples in
549 the same application.
550
551 In essence, the function performs the same operation as ei_xre‐
552 ceive_msg, but instead of using an ei_x_buff, the function
553 expects a pointer to a character pointer (mbufp), where the
554 character pointer is to point to a memory area allocated by mal‐
555 loc. Argument bufsz is to be a pointer to an integer containing
556 the exact size (in bytes) of the memory area. The function may
557 reallocate the memory area and will in such cases put the new
558 size in *bufsz and update *mbufp.
559
560 Returns either ERL_TICK or the msgtype field of the erlang_msg
561 *msg. The length of the message is put in *msglen. On error a
562 value < 0 is returned.
563
564 It is recommended to use ei_xreceive_msg instead when possible,
565 for the sake of readability. However, the function will be
566 retained in the interface for compatibility and will not be
567 removed in future releases without prior notice.
568
569 int ei_receive_encoded_tmo(int fd, char **mbufp, int *bufsz,
570 erlang_msg *msg, int *msglen, unsigned timeout_ms)
571
572 Equivalent to ei_receive_encoded with an optional time-out argu‐
573 ment, see the description at the beginning of this manual page.
574
575 int ei_receive_msg(int fd, erlang_msg* msg, ei_x_buff* x)
576 int ei_xreceive_msg(int fd, erlang_msg* msg, ei_x_buff* x)
577
578 Receives a message to the buffer in x. ei_xreceive_msg allows
579 the buffer in x to grow, but ei_receive_msg fails if the message
580 is larger than the pre-allocated buffer in x.
581
582 * fd is an open descriptor to an Erlang connection.
583
584 * msg is a pointer to an erlang_msg structure and contains
585 information on the message received.
586
587 * x is buffer obtained from ei_x_new.
588
589 On success, the functions return ERL_MSG and the msg struct is
590 initialized. erlang_msg is defined as follows:
591
592 typedef struct {
593 long msgtype;
594 erlang_pid from;
595 erlang_pid to;
596 char toname[MAXATOMLEN+1];
597 char cookie[MAXATOMLEN+1];
598 erlang_trace token;
599 } erlang_msg;
600
601
602 msgtype identifies the type of message, and is one of the fol‐
603 lowing:
604
605 ERL_SEND:
606 Indicates that an ordinary send operation has occurred.
607 msg->to contains the pid of the recipient (the C-node).
608
609 ERL_REG_SEND:
610 A registered send operation occurred. msg->from contains the
611 pid of the sender.
612
613 ERL_LINK or ERL_UNLINK:
614 msg->to and msg->from contain the pids of the sender and
615 recipient of the link or unlink.
616
617 ERL_EXIT:
618 Indicates a broken link. msg->to and msg->from contain the
619 pids of the linked processes.
620
621 The return value is the same as for ei_receive.
622
623 int ei_receive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned
624 imeout_ms)
625 int ei_xreceive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned
626 timeout_ms)
627
628 Equivalent to ei_receive_msg and ei_xreceive_msg with an
629 optional time-out argument, see the description at the beginning
630 of this manual page.
631
632 int ei_receive_tmo(int fd, unsigned char* bufp, int bufsize, unsigned
633 timeout_ms)
634
635 Equivalent to ei_receive with an optional time-out argument, see
636 the description at the beginning of this manual page.
637
638 int ei_reg_send(ei_cnode* ec, int fd, char* server_name, char* buf, int
639 len)
640
641 Sends an Erlang term to a registered process.
642
643 * fd is an open descriptor to an Erlang connection.
644
645 * server_name is the registered name of the intended recipi‐
646 ent.
647
648 * buf is the buffer containing the term in binary format.
649
650 * len is the length of the message in bytes.
651
652 Returns 0 if successful, otherwise -1. In the latter case it
653 sets erl_errno to EIO.
654
655 Example:
656
657 Send the atom "ok" to the process "worker":
658
659 ei_x_buff x;
660 ei_x_new_with_version(&x);
661 ei_x_encode_atom(&x, "ok");
662 if (ei_reg_send(&ec, fd, x.buff, x.index) < 0)
663 handle_error();
664
665
666 int ei_reg_send_tmo(ei_cnode* ec, int fd, char* server_name, char* buf,
667 int len, unsigned timeout_ms)
668
669 Equivalent to ei_reg_send with an optional time-out argument,
670 see the description at the beginning of this manual page.
671
672 int ei_rpc(ei_cnode *ec, int fd, char *mod, char *fun, const char *arg‐
673 buf, int argbuflen, ei_x_buff *x)
674 int ei_rpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char
675 *argbuf, int argbuflen)
676 int ei_rpc_from(ei_cnode *ec, int fd, int timeout, erlang_msg *msg,
677 ei_x_buff *x)
678
679 Supports calling Erlang functions on remote nodes. ei_rpc_to()
680 sends an RPC request to a remote node and ei_rpc_from() receives
681 the results of such a call. ei_rpc() combines the functionality
682 of these two functions by sending an RPC request and waiting for
683 the results. See also rpc:call/4 in Kernel.
684
685 * ec is the C-node structure previously initiated by a call to
686 ei_connect_init() or ei_connect_xinit().
687
688 * fd is an open descriptor to an Erlang connection.
689
690 * timeout is the maximum time (in milliseconds) to wait for
691 results. Specify ERL_NO_TIMEOUT to wait forever. ei_rpc()
692 waits infinitely for the answer, that is, the call will
693 never time out.
694
695 * mod is the name of the module containing the function to be
696 run on the remote node.
697
698 * fun is the name of the function to run.
699
700 * argbuf is a pointer to a buffer with an encoded Erlang list,
701 without a version magic number, containing the arguments to
702 be passed to the function.
703
704 * argbuflen is the length of the buffer containing the encoded
705 Erlang list.
706
707 * msg is structure of type erlang_msg and contains information
708 on the message received. For a description of the erlang_msg
709 format, see ei_receive_msg.
710
711 * x points to the dynamic buffer that receives the result. For
712 ei_rpc() this is the result without the version magic num‐
713 ber. For ei_rpc_from() the result returns a version magic
714 number and a 2-tuple {rex,Reply}.
715
716 ei_rpc() returns the number of bytes in the result on success
717 and -1 on failure. ei_rpc_from() returns the number of bytes,
718 otherwise one of ERL_TICK, ERL_TIMEOUT, and ERL_ERROR. When
719 failing, all three functions set erl_errno to one of the follow‐
720 ing:
721
722 EIO:
723 I/O error.
724
725 ETIMEDOUT:
726 Time-out expired.
727
728 EAGAIN:
729 Temporary error: Try again.
730
731 Example:
732
733 Check to see if an Erlang process is alive:
734
735 int index = 0, is_alive;
736 ei_x_buff args, result;
737
738 ei_x_new(&result);
739 ei_x_new(&args);
740 ei_x_encode_list_header(&args, 1);
741 ei_x_encode_pid(&args, &check_pid);
742 ei_x_encode_empty_list(&args);
743
744 if (ei_rpc(&ec, fd, "erlang", "is_process_alive",
745 args.buff, args.index, &result) < 0)
746 handle_error();
747
748 if (ei_decode_version(result.buff, &index) < 0
749 || ei_decode_bool(result.buff, &index, &is_alive) < 0)
750 handle_error();
751
752
753 erlang_pid *ei_self(ei_cnode *ec)
754
755 Retrieves the pid of the C-node. Every C-node has a (pseudo) pid
756 used in ei_send_reg, ei_rpc, and others. This is contained in a
757 field in the ec structure. It will be safe for a long time to
758 fetch this field directly from the ei_cnode structure.
759
760 int ei_send(int fd, erlang_pid* to, char* buf, int len)
761
762 Sends an Erlang term to a process.
763
764 * fd is an open descriptor to an Erlang connection.
765
766 * to is the pid of the intended recipient of the message.
767
768 * buf is the buffer containing the term in binary format.
769
770 * len is the length of the message in bytes.
771
772 Returns 0 if successful, otherwise -1. In the latter case it
773 sets erl_errno to EIO.
774
775 int ei_send_encoded(int fd, erlang_pid* to, char* buf, int len)
776
777 Works exactly as ei_send, the alternative name is retained for
778 backward compatibility. The function will not be removed without
779 prior notice.
780
781 int ei_send_encoded_tmo(int fd, erlang_pid* to, char* buf, int len,
782 unsigned timeout_ms)
783
784 Equivalent to ei_send_encoded with an optional time-out argu‐
785 ment, see the description at the beginning of this manual page.
786
787 int ei_send_reg_encoded(int fd, const erlang_pid *from, const char *to,
788 const char *buf, int len)
789
790 This function is retained for compatibility with code generated
791 by the interface compiler and with code following examples in
792 the same application.
793
794 The function works as ei_reg_send with one exception. Instead of
795 taking ei_cnode as first argument, it takes a second argument,
796 an erlang_pid, which is to be the process identifier of the
797 sending process (in the Erlang distribution protocol).
798
799 A suitable erlang_pid can be constructed from the ei_cnode
800 structure by the following example code:
801
802 ei_cnode ec;
803 erlang_pid *self;
804 int fd; /* the connection fd */
805 self = ei_self(&ec);
806 self->num = fd;
807
808
809 int ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, const char
810 *to, const char *buf, int len)
811
812 Equivalent to ei_send_reg_encoded with an optional time-out
813 argument, see the description at the beginning of this manual
814 page.
815
816 int ei_send_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned
817 timeout_ms)
818
819 Equivalent to ei_send with an optional time-out argument, see
820 the description at the beginning of this manual page.
821
822 const char *ei_thisnodename(ei_cnode *ec)
823 const char *ei_thishostname(ei_cnode *ec)
824 const char *ei_thisalivename(ei_cnode *ec)
825
826 Can be used to retrieve information about the C-node. These val‐
827 ues are initially set with ei_connect_init() or ei_con‐
828 nect_xinit().
829
830 These function simply fetch the appropriate field from the ec
831 structure. Read the field directly will probably be safe for a
832 long time, so these functions are not really needed.
833
834 int ei_unpublish(ei_cnode *ec)
835
836 Can be called by a process to unregister a specified node from
837 EPMD on the local host. This is, however, usually not allowed,
838 unless EPMD was started with flag -relaxed_command_check, which
839 it normally is not.
840
841 To unregister a node you have published, you should close the
842 descriptor that was returned by ei_publish().
843
844 Warning:
845 This function is deprecated and will be removed in a future
846 release.
847
848
849 ec is the node structure of the node to unregister.
850
851 If the node was successfully unregistered from EPMD, the func‐
852 tion returns 0. Otherwise, -1 is returned and erl_errno is set
853 to EIO.
854
855 int ei_unpublish_tmo(ei_cnode *ec, unsigned timeout_ms)
856
857 Equivalent to ei_unpublish with an optional time-out argument,
858 see the description at the beginning of this manual page.
859
861 If a connection attempt fails, the following can be checked:
862
863 * erl_errno.
864
865 * That the correct cookie was used
866
867 * That EPMD is running
868
869 * That the remote Erlang node on the other side is running the same
870 version of Erlang as the ei library
871
872 * That environment variable ERL_EPMD_PORT is set correctly
873
874 The connection attempt can be traced by setting a trace level by either
875 using ei_set_tracelevel or by setting environment variable
876 EI_TRACELEVEL. The trace levels have the following messages:
877
878 * 1: Verbose error messages
879
880 * 2: Above messages and verbose warning messages
881
882 * 3: Above messages and progress reports for connection handling
883
884 * 4: Above messages and progress reports for communication
885
886 * 5: Above messages and progress reports for data conversion
887
888Ericsson AB erl_interface 3.11.3 ei_connect(3)