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 er‐
15 lang: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-re‐
31 sponsive 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 im‐
51 plementation. 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. Currently the following fields
65 exist:
66
67 flags:
68 Flags informing ei about the behaviour of the callbacks. Flags
69 should be bitwise or:ed together. If no flag, is set, the flags
70 field should contain 0. Currently, supported flags:
71
72 EI_SCLBK_FLG_FULL_IMPL:
73 If set, the accept(), connect(), writev(), write(), and read()
74 callbacks implements timeouts. The timeout is passed in the tmo
75 argument and is given in milli seconds. Note that the tmo argu‐
76 ment to these callbacks differ from the timeout arguments in the
77 ei API. Zero means a zero timeout. That is, poll and timeout im‐
78 mediately unless the operation is successful. EI_SCLBK_INF_TMO
79 (max unsigned) means infinite timeout. The file descriptor is in
80 blocking mode when a callback is called, and it must be in block‐
81 ing mode when the callback returns.
82
83 If not set, ei will implement the timeout using select() in order
84 to determine when to call the callbacks and when to time out. The
85 tmo arguments of the accept(), connect(), writev(), write(), and
86 read() callbacks should be ignored. The callbacks may be called
87 in non-blocking mode. The callbacks are not allowed to change be‐
88 tween blocking and non-blocking mode. In order for this to work,
89 select() needs to interact with the socket primitives used the
90 same way as it interacts with the ordinary socket primitives. If
91 this is not the case, the callbacks need to implement timeouts
92 and this flag should be set.
93
94 More flags may be introduced in the future.
95
96 int (*socket)(void **ctx, void *setup_ctx):
97 Create a socket and a context for the socket.
98
99 On success it should set *ctx to point to a context for the created
100 socket. This context will be passed to all other socket callbacks.
101 This function will be passed the same setup_context as passed to
102 the preceeding ei_connect_init_ussi() or ei_connect_xinit_ussi()
103 call.
104
105 Note:
106 During the lifetime of a socket, the pointer *ctx has to remain the
107 same. That is, it cannot later be relocated.
108
109
110 This callback is mandatory.
111
112 int (*close)(void *ctx):
113 Close the socket identified by ctx and destroy the context.
114
115 This callback is mandatory.
116
117 int (*listen)(void *ctx, void *addr, int *len, int backlog):
118 Bind the socket identified by ctx to a local interface and then
119 listen on it.
120
121 The addr and len arguments are both input and output arguments.
122 When called addr points to an address structure of lenght *len con‐
123 taining information on how to bind the socket. Uppon return this
124 callback should have updated the structure referred by addr with
125 information on how the socket actually was bound. *len should be
126 updated to reflect the size of *addr updated. backlog identifies
127 the size of the backlog for the listen socket.
128
129 This callback is mandatory.
130
131 int (*accept)(void **ctx, void *addr, int *len, unsigned tmo):
132 Accept connections on the listen socket identified by *ctx.
133
134 When a connection is accepted, a new context for the accepted con‐
135 nection should be created and *ctx should be updated to point to
136 the new context for the accepted connection. When called addr
137 points to an uninitialized address structure of lenght *len. Uppon
138 return this callback should have updated this structure with infor‐
139 mation about the client address. *len should be updated to reflect
140 the size of *addr updated.
141
142 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
143 out time in milliseconds.
144
145 Note:
146 During the lifetime of a socket, the pointer *ctx has to remain the
147 same. That is, it cannot later be relocated.
148
149
150 This callback is mandatory.
151
152 int (*connect)(void *ctx, void *addr, int len, unsigned tmo):
153 Connect the socket identified by ctx to the address identified by
154 addr.
155
156 When called addr points to an address structure of lenght len con‐
157 taining information on where to connect.
158
159 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
160 out time in milliseconds.
161
162 This callback is mandatory.
163
164 int (*writev)(void *ctx, const void *iov, long iovcnt, ssize_t *len,
165 unsigned tmo):
166 Write data on the connected socket identified by ctx.
167
168 iov points to an array of struct iovec structures of length iovcnt
169 containing data to write to the socket. On success, this callback
170 should set *len to the amount of bytes successfully written on the
171 socket.
172
173 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
174 out time in milliseconds.
175
176 This callback is optional. Set the writev field in the the
177 ei_socket_callbacks structure to NULL if not implemented.
178
179 int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo):
180 Write data on the connected socket identified by ctx.
181
182 When called buf points to a buffer of length *len containing the
183 data to write on the socket. On success, this callback should set
184 *len to the amount of bytes successfully written on the socket.
185
186 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
187 out time in milliseconds.
188
189 This callback is mandatory.
190
191 int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo):
192 Read data on the connected socket identified by ctx.
193
194 buf points to a buffer of length *len where the read data should be
195 placed. On success, this callback should update *len to the amount
196 of bytes successfully read on the socket.
197
198 If the EI_SCLBK_FLG_FULL_IMPL flag has been set, tmo contains time‐
199 out time in milliseconds.
200
201 This callback is mandatory.
202
203 int (*handshake_packet_header_size)(void *ctx, int *sz):
204 Inform about handshake packet header size to use during the Erlang
205 distribution handshake.
206
207 On success, *sz should be set to the handshake packet header size
208 to use. Valid values are 2 and 4. Erlang TCP distribution use a
209 handshake packet size of 2 and Erlang TLS distribution use a hand‐
210 shake packet size of 4.
211
212 This callback is mandatory.
213
214 int (*connect_handshake_complete)(void *ctx):
215 Called when a locally started handshake has completed successfully.
216
217 This callback is optional. Set the connect_handshake_complete field
218 in the ei_socket_callbacks structure to NULL if not implemented.
219
220 int (*accept_handshake_complete)(void *ctx):
221 Called when a remotely started handshake has completed success‐
222 fully.
223
224 This callback is optional. Set the accept_handshake_complete field
225 in the ei_socket_callbacks structure to NULL if not implemented.
226
227 int (*get_fd)(void *ctx, int *fd):
228 Inform about file descriptor used by the socket which is identified
229 by ctx.
230
231 Note:
232 During the lifetime of a socket, the file descriptor has to remain
233 the same. That is, repeated calls to this callback with the same con‐
234 text should always report the same file descriptor.
235
236 The file descriptor has to be a real file descriptor. That is, no
237 other operation should be able to get the same file descriptor until
238 it has been released by the close() callback.
239
240
241 This callback is mandatory.
242
244 ei_cnode:
245 Opaque data type representing a C-node. A ei_cnode structure is
246 initialized by calling ei_connect_init() or friends.
247
248 ei_socket_callbacks:
249
250
251 typedef struct {
252 int flags;
253 int (*socket)(void **ctx, void *setup_ctx);
254 int (*close)(void *ctx);
255 int (*listen)(void *ctx, void *addr, int *len, int backlog);
256 int (*accept)(void **ctx, void *addr, int *len, unsigned tmo);
257 int (*connect)(void *ctx, void *addr, int len, unsigned tmo);
258 int (*writev)(void *ctx, const void *iov, int iovcnt, ssize_t *len, unsigned tmo);
259 int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo);
260 int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo);
261 int (*handshake_packet_header_size)(void *ctx, int *sz);
262 int (*connect_handshake_complete)(void *ctx);
263 int (*accept_handshake_complete)(void *ctx);
264 int (*get_fd)(void *ctx, int *fd);
265 } ei_socket_callbacks;
266
267 Callbacks functions for a User Supplied Socket Implementation .
268 Documentation of each field can be found in the User Supplied
269 Socket Implementation section above.
270
271 ErlConnect:
272
273
274 typedef struct {
275 char ipadr[4]; /* Ip v4 address in network byte order */
276 char nodename[MAXNODELEN];
277 } ErlConnect;
278
279 IP v4 address and nodename.
280
281 Erl_IpAddr:
282
283
284 typedef struct {
285 unsigned s_addr; /* Ip v4 address in network byte order */
286 } Erl_IpAddr;
287
288 IP v4 address.
289
290 erlang_msg:
291
292
293 typedef struct {
294 long msgtype;
295 erlang_pid from;
296 erlang_pid to;
297 char toname[MAXATOMLEN+1];
298 char cookie[MAXATOMLEN+1];
299 erlang_trace token;
300 } erlang_msg;
301
302 Information about a message received via ei_receive_msg() or
303 friends.
304
306 struct hostent *ei_gethostbyaddr(const char *addr, int len, int type)
307 struct hostent *ei_gethostbyaddr_r(const char *addr, int length, int
308 type, struct hostent *hostp, char *buffer, int buflen, int *h_er‐
309 rnop)
310 struct hostent *ei_gethostbyname(const char *name)
311 struct hostent *ei_gethostbyname_r(const char *name, struct hostent
312 *hostp, char *buffer, int buflen, int *h_errnop)
313
314 Convenience functions for some common name lookup functions.
315
316 int ei_accept(ei_cnode *ec, int listensock, ErlConnect *conp)
317
318 Types:
319
320 ei_cnode
321 ErlConnect
322
323 Used by a server process to accept a connection from a client
324 process.
325
326 * ec is the C-node structure.
327
328 * listensock is an open socket descriptor on which listen()
329 has previously been called.
330
331 * conp is a pointer to an ErlConnect struct.
332
333 On success, conp is filled in with the address and node name of
334 the connecting client and a file descriptor is returned. On
335 failure, ERL_ERROR is returned and erl_errno is set to EIO.
336
337 int ei_accept_tmo(ei_cnode *ec, int listensock, ErlConnect *conp, un‐
338 signed timeout_ms)
339
340 Types:
341
342 ei_cnode
343 ErlConnect
344
345 Equivalent to ei_accept with an optional time-out argument, see
346 the description at the beginning of this manual page.
347
348 int ei_close_connection(int fd)
349
350 Closes a previously opened connection or listen socket.
351
352 int ei_connect(ei_cnode* ec, char *nodename)
353 int ei_xconnect(ei_cnode* ec, Erl_IpAddr adr, char *alivename)
354 int ei_connect_host_port(ei_cnode* ec, char *hostname, int port)
355 int ei_xconnect_host_port(ei_cnode* ec, Erl_IpAddr adr, int port)
356
357 Types:
358
359 ei_cnode
360 Erl_IpAddr
361
362 Sets up a connection to an Erlang node.
363
364 ei_xconnect() requires the IP address of the remote host and the
365 alive name of the remote node to be specified. ei_connect() pro‐
366 vides an alternative interface and determines the information
367 from the node name provided. The ei_xconnect_host_port() func‐
368 tion provides yet another alternative that will work even if
369 there is no EPMD instance on the host where the remote node is
370 running. The ei_xconnect_host_port() function requires the IP
371 address and port of the remote node to be specified. The ei_con‐
372 nect_host_port() function is an alternative to ei_xcon‐
373 nect_host_port() that lets the user specify a hostname instead
374 of an IP address.
375
376 * adr is the 32-bit IP address of the remote host.
377
378 * alive is the alivename of the remote node.
379
380 * node is the name of the remote node.
381
382 * port is the port number of the remote node.
383
384 These functions return an open file descriptor on success, or a
385 negative value indicating that an error occurred. In the latter
386 case they set erl_errno to one of the following:
387
388 EHOSTUNREACH:
389 The remote host node is unreachable.
390
391 ENOMEM:
392 No more memory is available.
393
394 EIO:
395 I/O error.
396
397 Also, errno values from socket(2) and connect(2) system calls
398 may be propagated into erl_errno.
399
400 Example:
401
402 #define NODE "madonna@chivas.du.etx.ericsson.se"
403 #define ALIVE "madonna"
404 #define IP_ADDR "150.236.14.75"
405
406 /*** Variant 1 ***/
407 int fd = ei_connect(&ec, NODE);
408
409 /*** Variant 2 ***/
410 struct in_addr addr;
411 addr.s_addr = inet_addr(IP_ADDR);
412 fd = ei_xconnect(&ec, &addr, ALIVE);
413
414
415 int ei_connect_init(ei_cnode* ec, const char* this_node_name, const
416 char *cookie, short creation)
417 int ei_connect_init_ussi(ei_cnode* ec, const char* this_node_name,
418 const char *cookie, short creation, ei_socket_callbacks *cbs, int
419 cbs_sz, void *setup_context)
420 int ei_connect_xinit(ei_cnode* ec, const char *thishostname, const char
421 *thisalivename, const char *thisnodename, Erl_IpAddr thisipaddr, const
422 char *cookie, short creation)
423 int ei_connect_xinit_ussi(ei_cnode* ec, const char *thishostname, const
424 char *thisalivename, const char *thisnodename, Erl_IpAddr thisipaddr,
425 const char *cookie, short creation, ei_socket_callbacks *cbs, int
426 cbs_sz, void *setup_context)
427
428 Types:
429
430 ei_cnode
431 Erl_IpAddr
432 ei_socket_callbacks
433
434 Initializes the ec structure, to identify the node name and
435 cookie of the server. One of them must be called before other
436 functions that works on the ei_cnode type or a file descriptor
437 associated with a connection to another node is used.
438
439 * ec is a structure containing information about the C-node.
440 It is used in other ei functions for connecting and receiv‐
441 ing data.
442
443 * this_node_name is the name of the C-node (the name before
444 '@' in the full node name).
445
446 * cookie is the cookie for the node.
447
448 * creation identifies a specific instance of a C-node. It can
449 help prevent the node from receiving messages sent to an
450 earlier process with the same registered name.
451
452 * thishostname is the name of the machine we are running on.
453 If long names are to be used, they are to be fully qualified
454 (that is, durin.erix.ericsson.se instead of durin).
455
456 * thisalivename is the name of the local C-node (the name be‐
457 fore '@' in the full node name). Can be NULL (from OTP 23)
458 to get a dynamically assigned name from the peer node.
459
460 * thisnodename is the full name of the local C-node, that is,
461 mynode@myhost. Can be NULL if thisalivename is NULL.
462
463 * thispaddr if the IP address of the host.
464
465 * cbs is a pointer to a callback structure implementing and
466 alternative socket interface.
467
468 * cbs_sz is the size of the structure pointed to by cbs.
469
470 * setup_context is a pointer to a structure that will be
471 passed as second argument to the socket callback in the cbs
472 structure.
473
474 A C-node acting as a server is assigned a creation number when
475 it calls ei_publish().
476
477 A connection is closed by simply closing the socket. For infor‐
478 mation about how to close the socket gracefully (when there are
479 outgoing packets before close), see the relevant system documen‐
480 tation.
481
482 These functions return a negative value indicating that an error
483 occurred.
484
485 Example 1:
486
487 int n = 0;
488 struct in_addr addr;
489 ei_cnode ec;
490 addr.s_addr = inet_addr("150.236.14.75");
491 if (ei_connect_xinit(&ec,
492 "chivas",
493 "madonna",
494 "madonna@chivas.du.etx.ericsson.se",
495 &addr;
496 "cookie...",
497 n++) < 0) {
498 fprintf(stderr,"ERROR when initializing: %d",erl_errno);
499 exit(-1);
500 }
501
502
503 Example 2:
504
505 if (ei_connect_init(&ec, "madonna", "cookie...", n++) < 0) {
506 fprintf(stderr,"ERROR when initializing: %d",erl_errno);
507 exit(-1);
508 }
509
510
511 int ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned timeout_ms)
512 int ei_xconnect_tmo(ei_cnode* ec, Erl_IpAddr adr, char *alivename, un‐
513 signed timeout_ms)
514 int ei_connect_host_port_tmo(ei_cnode* ec, char *hostname, int port,
515 unsigned ms)
516 int ei_xconnect_host_port_tmo(ei_cnode* ec, Erl_IpAddr adr, int port,
517 unsigned ms)
518
519 Types:
520
521 ei_cnode
522 Erl_IpAddr
523
524 Equivalent to ei_connect, ei_xconnect, ei_connect_host_port and
525 ei_xconnect_host_port with an optional time-out argument, see
526 the description at the beginning of this manual page.
527
528 int ei_get_tracelevel(void)
529 void ei_set_tracelevel(int level)
530
531 Used to set tracing on the distribution. The levels are differ‐
532 ent verbosity levels. A higher level means more information. See
533 also section Debug Information.
534
535 These functions are not thread safe.
536
537 int ei_listen(ei_cnode *ec, int *port, int backlog)
538 int ei_xlisten(ei_cnode *ec, Erl_IpAddr adr, int *port, int backlog)
539
540 Types:
541
542 ei_cnode
543 Erl_IpAddr
544
545 Used by a server process to setup a listen socket which later
546 can be used for accepting connections from client processes.
547
548 * ec is the C-node structure.
549
550 * adr is local interface to bind to.
551
552 * port is a pointer to an integer containing the port number
553 to bind to. If *port equals 0 when calling ei_listen(), the
554 socket will be bound to an ephemeral port. On success,
555 ei_listen() will update the value of *port to the port actu‐
556 ally bound to.
557
558 * backlog is maximum backlog of pending connections.
559
560 ei_listen will create a socket, bind to a port on the local in‐
561 terface identified by adr (or all local interfaces if ei_lis‐
562 ten() is called), and mark the socket as a passive socket (that
563 is, a socket that will be used for accepting incoming connec‐
564 tions).
565
566 On success, a file descriptor is returned which can be used in a
567 call to ei_accept(). On failure, ERL_ERROR is returned and
568 erl_errno is set to EIO.
569
570 int ei_make_pid(ei_cnode *ec, erlang_pid *pid)
571
572 Types:
573
574 ei_cnode
575 erlang_pid
576
577 Creates a new process identifier in the argument pid. This
578 process identifier refers to a conseptual process residing on
579 the C-node identified by the argument ec. On success 0 is re‐
580 turned. On failure ERL_ERROR is returned and erl_errno is set.
581
582 The C-node identified by ec must have been initialized and must
583 have received a name prior to the call to ei_make_pid(). Ini‐
584 tialization of the C-node is done by a call to ei_connect_init()
585 or friends. If the name is dynamically assigned from the peer
586 node, the C-node also has to be connected.
587
588 int ei_make_ref(ei_cnode *ec, erlang_ref *ref)
589
590 Types:
591
592 ei_cnode
593 erlang_ref
594
595 Creates a new reference in the argument ref. This reference
596 originates from the C-node identified by the argument ec. On
597 success 0 is returned. On failure ERL_ERROR is returned and
598 erl_errno is set.
599
600 The C-node identified by ec must have been initialized and must
601 have received a name prior to the call to ei_make_ref(). Ini‐
602 tialization of the C-node is done by a call to ei_connect_init()
603 or friends. If the name is dynamically assigned from the peer
604 node, the C-node also has to be connected.
605
606 int ei_publish(ei_cnode *ec, int port)
607
608 Types:
609
610 ei_cnode
611
612 Used by a server process to register with the local name server
613 EPMD, thereby allowing other processes to send messages by using
614 the registered name. Before calling either of these functions,
615 the process should have called bind() and listen() on an open
616 socket.
617
618 * ec is the C-node structure.
619
620 * port is the local name to register, and is to be the same as
621 the port number that was previously bound to the socket.
622
623 * addr is the 32-bit IP address of the local host.
624
625 To unregister with EPMD, simply close the returned descriptor.
626 Do not use ei_unpublish(), which is deprecated anyway.
627
628 On success, the function returns a descriptor connecting the
629 calling process to EPMD. On failure, -1 is returned and erl_er‐
630 rno is set to EIO.
631
632 Also, errno values from socket(2) and connect(2) system calls
633 may be propagated into erl_errno.
634
635 int ei_publish_tmo(ei_cnode *ec, int port, unsigned timeout_ms)
636
637 Types:
638
639 ei_cnode
640
641 Equivalent to ei_publish with an optional time-out argument, see
642 the description at the beginning of this manual page.
643
644 int ei_receive(int fd, unsigned char* bufp, int bufsize)
645
646 Receives a message consisting of a sequence of bytes in the Er‐
647 lang external format.
648
649 * fd is an open descriptor to an Erlang connection. It is ob‐
650 tained from a previous ei_connect or ei_accept.
651
652 * bufp is a buffer large enough to hold the expected message.
653
654 * bufsize indicates the size of bufp.
655
656 If a tick occurs, that is, the Erlang node on the other end of
657 the connection has polled this node to see if it is still alive,
658 the function returns ERL_TICK and no message is placed in the
659 buffer. Also, erl_errno is set to EAGAIN.
660
661 On success, the message is placed in the specified buffer and
662 the function returns the number of bytes actually read. On fail‐
663 ure, the function returns ERL_ERROR and sets erl_errno to one of
664 the following:
665
666 EAGAIN:
667 Temporary error: Try again.
668
669 EMSGSIZE:
670 Buffer is too small.
671
672 EIO:
673 I/O error.
674
675 int ei_receive_encoded(int fd, char **mbufp, int *bufsz, erlang_msg
676 *msg, int *msglen)
677
678 Types:
679
680 erlang_msg
681
682 This function is retained for compatibility with code generated
683 by the interface compiler and with code following examples in
684 the same application.
685
686 In essence, the function performs the same operation as ei_xre‐
687 ceive_msg, but instead of using an ei_x_buff, the function ex‐
688 pects a pointer to a character pointer (mbufp), where the char‐
689 acter pointer is to point to a memory area allocated by malloc.
690 Argument bufsz is to be a pointer to an integer containing the
691 exact size (in bytes) of the memory area. The function may re‐
692 allocate the memory area and will in such cases put the new size
693 in *bufsz and update *mbufp.
694
695 Returns either ERL_TICK or the msgtype field of the erlang_msg
696 *msg. The length of the message is put in *msglen. On error a
697 value < 0 is returned.
698
699 It is recommended to use ei_xreceive_msg instead when possible,
700 for the sake of readability. However, the function will be re‐
701 tained in the interface for compatibility and will not be re‐
702 moved in future releases without prior notice.
703
704 int ei_receive_encoded_tmo(int fd, char **mbufp, int *bufsz, er‐
705 lang_msg *msg, int *msglen, unsigned timeout_ms)
706
707 Types:
708
709 erlang_msg
710
711 Equivalent to ei_receive_encoded with an optional time-out argu‐
712 ment, see the description at the beginning of this manual page.
713
714 int ei_receive_msg(int fd, erlang_msg* msg, ei_x_buff* x)
715 int ei_xreceive_msg(int fd, erlang_msg* msg, ei_x_buff* x)
716
717 Types:
718
719 ei_x_buff
720 erlang_msg
721
722 Receives a message to the buffer in x. ei_xreceive_msg allows
723 the buffer in x to grow, but ei_receive_msg fails if the message
724 is larger than the pre-allocated buffer in x.
725
726 * fd is an open descriptor to an Erlang connection.
727
728 * msg is a pointer to an erlang_msg structure and contains in‐
729 formation on the message received.
730
731 * x is buffer obtained from ei_x_new.
732
733 On success, the functions return ERL_MSG and the msg struct is
734 initialized.
735
736 msgtype identifies the type of message, and is one of the fol‐
737 lowing:
738
739 ERL_SEND:
740 Indicates that an ordinary send operation has occurred.
741 msg->to contains the pid of the recipient (the C-node).
742
743 ERL_REG_SEND:
744 A registered send operation occurred. msg->from contains the
745 pid of the sender.
746
747 ERL_LINK or ERL_UNLINK:
748 msg->to and msg->from contain the pids of the sender and re‐
749 cipient of the link or unlink.
750
751 ERL_EXIT:
752 Indicates a broken link. msg->to and msg->from contain the
753 pids of the linked processes.
754
755 The return value is the same as for ei_receive.
756
757 int ei_receive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned
758 imeout_ms)
759 int ei_xreceive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned
760 timeout_ms)
761
762 Types:
763
764 ei_x_buff
765 erlang_msg
766
767 Equivalent to ei_receive_msg and ei_xreceive_msg with an op‐
768 tional time-out argument, see the description at the beginning
769 of this manual page.
770
771 int ei_receive_tmo(int fd, unsigned char* bufp, int bufsize, unsigned
772 timeout_ms)
773
774 Equivalent to ei_receive with an optional time-out argument, see
775 the description at the beginning of this manual page.
776
777 int ei_reg_send(ei_cnode* ec, int fd, char* server_name, char* buf, int
778 len)
779
780 Types:
781
782 ei_cnode
783
784 Sends an Erlang term to a registered process.
785
786 * fd is an open descriptor to an Erlang connection.
787
788 * server_name is the registered name of the intended recipi‐
789 ent.
790
791 * buf is the buffer containing the term in binary format.
792
793 * len is the length of the message in bytes.
794
795 Returns 0 if successful, otherwise -1. In the latter case it
796 sets erl_errno to EIO.
797
798 Example:
799
800 Send the atom "ok" to the process "worker":
801
802 ei_x_buff x;
803 ei_x_new_with_version(&x);
804 ei_x_encode_atom(&x, "ok");
805 if (ei_reg_send(&ec, fd, x.buff, x.index) < 0)
806 handle_error();
807
808
809 int ei_reg_send_tmo(ei_cnode* ec, int fd, char* server_name, char* buf,
810 int len, unsigned timeout_ms)
811
812 Types:
813
814 ei_cnode
815
816 Equivalent to ei_reg_send with an optional time-out argument,
817 see the description at the beginning of this manual page.
818
819 int ei_rpc(ei_cnode *ec, int fd, char *mod, char *fun, const char *arg‐
820 buf, int argbuflen, ei_x_buff *x)
821 int ei_rpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char
822 *argbuf, int argbuflen)
823 int ei_xrpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char
824 *argbuf, int argbuflen, int flags)
825 int ei_rpc_from(ei_cnode *ec, int fd, int timeout, erlang_msg *msg,
826 ei_x_buff *x)
827
828 Types:
829
830 ei_cnode
831 ei_x_buff
832 erlang_msg
833
834 Supports calling Erlang functions on remote nodes. ei_rpc_to()
835 sends an RPC request to a remote node and ei_rpc_from() receives
836 the results of such a call. ei_rpc() combines the functionality
837 of these two functions by sending an RPC request and waiting for
838 the results.
839
840 The ei_xrpc_to() function is equivalent to ei_rpc_to() when its
841 flags parameter is set to 0. When the flags parameter of
842 ei_xrpc_to() is set to EI_RPC_FETCH_STDOUT, stdout (standard
843 output) data are forwarded. See the documentation for the flags
844 parameter for more information about the EI_RPC_FETCH_STDOUT
845 flag.
846
847 rpc:call/4 in Kernel.
848
849 * ec is the C-node structure previously initiated by a call to
850 ei_connect_init() or ei_connect_xinit().
851
852 * fd is an open descriptor to an Erlang connection.
853
854 * timeout is the maximum time (in milliseconds) to wait for
855 results. Specify ERL_NO_TIMEOUT to wait forever. ei_rpc()
856 waits infinitely for the answer, that is, the call will
857 never time out.
858
859 * mod is the name of the module containing the function to be
860 run on the remote node.
861
862 * fun is the name of the function to run.
863
864 * argbuf is a pointer to a buffer with an encoded Erlang list,
865 without a version magic number, containing the arguments to
866 be passed to the function.
867
868 * argbuflen is the length of the buffer containing the encoded
869 Erlang list.
870
871 * msg is structure of type erlang_msg and contains information
872 on the message received. For a description of the erlang_msg
873 format, see ei_receive_msg.
874
875 * x points to the dynamic buffer that receives the result. For
876 ei_rpc() this is the result without the version magic num‐
877 ber. For an ei_rpc_from() call the result consists of a ver‐
878 sion magic number and a 2-tuple. The 2-tuple can be in one
879 of the following two forms:
880
881 {rex,Reply}:
882 This response value means that the RPC has completed. The
883 result value is the Reply term. This is the only type of
884 response that one can get from an RPC triggered by a call
885 to ei_rpc_to() or ei_xrpc_to() without the
886 EI_RPC_FETCH_STDOUT flag. If the RPC was triggered by a
887 call to ei_xrpc_to() with the EI_RPC_FETCH_STDOUT flag
888 set, then all forwarded stdout data has been received.
889
890 {rex_stdout,StdOutUTF8Binary}:
891 This response value can only be obtained if the RPC call
892 was triggered by a call to ei_xrpc_to() with the
893 EI_RPC_FETCH_STDOUT flag set. This response value means
894 that forwarded stdout data has been received. The stdout
895 data is stored in a binary and is UTF-8 encoded. One may
896 need to call ei_rpc_from() multiple times to read all the
897 stdout data. The stdout data is received in the same order
898 as it was written. All forwarded stdout data have been re‐
899 ceived when a {rex,Reply} tuple has been obtained from an
900 ei_rpc_from() call.
901
902 * flags The flag EI_RPC_FETCH_STDOUT is currently the only
903 flag that is supported by ei_xrpc_to(). When
904 EI_RPC_FETCH_STDOUT is set, the called function is executed
905 in a new process with a group leader that forwards all std‐
906 out data. This means that stdout data that are written dur‐
907 ing the execution of the called function, by the called
908 function and by descendant processes, will be forwarded
909 (given that the group leader has not been changed by a call
910 to erlang:group_leader/2). The forwarded stdout data need to
911 be collected by a sequence of calls to ei_rpc_from(). See
912 the description of the x parameter for how ei_rpc_from() is
913 used to receive stdout data. See the documentation of the
914 the I/O protocol, for more information about the group
915 leader concept.
916
917 Note:
918 The flag EI_RPC_FETCH_STDOUT only works when interacting with
919 a node with a version greater or equal to OTP-24.
920
921
922 ei_rpc() returns the number of bytes in the result on success
923 and -1 on failure. ei_rpc_from() returns the number of bytes,
924 otherwise one of ERL_TICK, ERL_TIMEOUT, and ERL_ERROR. The func‐
925 tions ei_rpc_to() and ei_xrpc_to() returns 0 if successful, oth‐
926 erwise -1. When failing, all four functions set erl_errno to one
927 of the following:
928
929 EIO:
930 I/O error.
931
932 ETIMEDOUT:
933 Time-out expired.
934
935 EAGAIN:
936 Temporary error: Try again.
937
938 Example:
939
940 Check to see if an Erlang process is alive:
941
942 int index = 0, is_alive;
943 ei_x_buff args, result;
944
945 ei_x_new(&result);
946 ei_x_new(&args);
947 ei_x_encode_list_header(&args, 1);
948 ei_x_encode_pid(&args, &check_pid);
949 ei_x_encode_empty_list(&args);
950
951 if (ei_rpc(&ec, fd, "erlang", "is_process_alive",
952 args.buff, args.index, &result) < 0)
953 handle_error();
954
955 if (ei_decode_version(result.buff, &index) < 0
956 || ei_decode_bool(result.buff, &index, &is_alive) < 0)
957 handle_error();
958
959
960 erlang_pid *ei_self(ei_cnode *ec)
961
962 Types:
963
964 ei_cnode
965 erlang_pid
966
967 Retrieves a generic pid of the C-node. Every C-node has a
968 (pseudo) pid used in ei_send_reg, ei_rpc(), and others. This is
969 contained in a field in the ec structure. Do not modify this
970 structure.
971
972 On success a pointer to the process identifier is returned. On
973 failure NULL is returned and erl_errno is set.
974
975 The C-node identified by ec must have been initialized and must
976 have received a name prior to the call to ei_self(). Initializa‐
977 tion of the C-node is done by a call to ei_connect_init() or
978 friends. If the name is dynamically assigned from the peer node,
979 the C-node also has to be connected.
980
981 int ei_send(int fd, erlang_pid* to, char* buf, int len)
982
983 Types:
984
985 erlang_pid
986
987 Sends an Erlang term to a process.
988
989 * fd is an open descriptor to an Erlang connection.
990
991 * to is the pid of the intended recipient of the message.
992
993 * buf is the buffer containing the term in binary format.
994
995 * len is the length of the message in bytes.
996
997 Returns 0 if successful, otherwise -1. In the latter case it
998 sets erl_errno to EIO.
999
1000 int ei_send_encoded(int fd, erlang_pid* to, char* buf, int len)
1001
1002 Types:
1003
1004 erlang_pid
1005
1006 Works exactly as ei_send, the alternative name is retained for
1007 backward compatibility. The function will not be removed without
1008 prior notice.
1009
1010 int ei_send_encoded_tmo(int fd, erlang_pid* to, char* buf, int len, un‐
1011 signed timeout_ms)
1012
1013 Types:
1014
1015 erlang_pid
1016
1017 Equivalent to ei_send_encoded with an optional time-out argu‐
1018 ment, see the description at the beginning of this manual page.
1019
1020 int ei_send_reg_encoded(int fd, const erlang_pid *from, const char *to,
1021 const char *buf, int len)
1022
1023 Types:
1024
1025 erlang_pid
1026
1027 This function is retained for compatibility with code generated
1028 by the interface compiler and with code following examples in
1029 the same application.
1030
1031 The function works as ei_reg_send with one exception. Instead of
1032 taking ei_cnode as first argument, it takes a second argument,
1033 an erlang_pid, which is to be the process identifier of the
1034 sending process (in the Erlang distribution protocol).
1035
1036 A suitable erlang_pid can be retrieved from the ei_cnode struc‐
1037 ture by calling ei_self(cnode_pointer).
1038
1039 int ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, const char
1040 *to, const char *buf, int len, unsigned timeout_ms)
1041
1042 Types:
1043
1044 erlang_pid
1045
1046 Equivalent to ei_send_reg_encoded with an optional time-out ar‐
1047 gument, see the description at the beginning of this manual
1048 page.
1049
1050 int ei_send_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned
1051 timeout_ms)
1052
1053 Types:
1054
1055 erlang_pid
1056
1057 Equivalent to ei_send with an optional time-out argument, see
1058 the description at the beginning of this manual page.
1059
1060 const char *ei_thisnodename(ei_cnode *ec)
1061 const char *ei_thishostname(ei_cnode *ec)
1062 const char *ei_thisalivename(ei_cnode *ec)
1063
1064 Types:
1065
1066 ei_cnode
1067
1068 Can be used to retrieve information about the C-node. These val‐
1069 ues are initially set with ei_connect_init() or ei_con‐
1070 nect_xinit().
1071
1072 These function simply fetch the appropriate field from the ec
1073 structure. Read the field directly will probably be safe for a
1074 long time, so these functions are not really needed.
1075
1076 int ei_unpublish(ei_cnode *ec)
1077
1078 Types:
1079
1080 ei_cnode
1081
1082 Can be called by a process to unregister a specified node from
1083 EPMD on the local host. This is, however, usually not allowed,
1084 unless EPMD was started with flag -relaxed_command_check, which
1085 it normally is not.
1086
1087 To unregister a node you have published, you should close the
1088 descriptor that was returned by ei_publish().
1089
1090 Warning:
1091 This function is deprecated and will be removed in a future re‐
1092 lease.
1093
1094
1095 ec is the node structure of the node to unregister.
1096
1097 If the node was successfully unregistered from EPMD, the func‐
1098 tion returns 0. Otherwise, -1 is returned and erl_errno is set
1099 to EIO.
1100
1101 int ei_unpublish_tmo(ei_cnode *ec, unsigned timeout_ms)
1102
1103 Types:
1104
1105 ei_cnode
1106
1107 Equivalent to ei_unpublish with an optional time-out argument,
1108 see the description at the beginning of this manual page.
1109
1111 If a connection attempt fails, the following can be checked:
1112
1113 * erl_errno.
1114
1115 * That the correct cookie was used
1116
1117 * That EPMD is running
1118
1119 * That the remote Erlang node on the other side is running the same
1120 version of Erlang as the ei library
1121
1122 * That environment variable ERL_EPMD_PORT is set correctly
1123
1124 The connection attempt can be traced by setting a trace level by either
1125 using ei_set_tracelevel or by setting environment variable
1126 EI_TRACELEVEL. The trace levels have the following messages:
1127
1128 * 1: Verbose error messages
1129
1130 * 2: Above messages and verbose warning messages
1131
1132 * 3: Above messages and progress reports for connection handling
1133
1134 * 4: Above messages and progress reports for communication
1135
1136 * 5: Above messages and progress reports for data conversion
1137
1138Ericsson AB erl_interface 5.1 ei_connect(3)