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 struct hostent *ei_gethostbyaddr(const char *addr, int len, int type)
50 struct hostent *ei_gethostbyaddr_r(const char *addr, int length, int
51 type, struct hostent *hostp, char *buffer, int buflen, int
52 *h_errnop)
53 struct hostent *ei_gethostbyname(const char *name)
54 struct hostent *ei_gethostbyname_r(const char *name, struct hostent
55 *hostp, char *buffer, int buflen, int *h_errnop)
56
57 Convenience functions for some common name lookup functions.
58
59 int ei_accept(ei_cnode *ec, int listensock, ErlConnect *conp)
60
61 Used by a server process to accept a connection from a client
62 process.
63
64 * ec is the C-node structure.
65
66 * listensock is an open socket descriptor on which listen()
67 has previously been called.
68
69 * conp is a pointer to an ErlConnect struct, described as fol‐
70 lows:
71
72 typedef struct {
73 char ipadr[4];
74 char nodename[MAXNODELEN];
75 } ErlConnect;
76
77
78 On success, conp is filled in with the address and node name of
79 the connecting client and a file descriptor is returned. On
80 failure, ERL_ERROR is returned and erl_errno is set to EIO.
81
82 int ei_accept_tmo(ei_cnode *ec, int listensock, ErlConnect *conp,
83 unsigned timeout_ms)
84
85 Equivalent to ei_accept with an optional time-out argument, see
86 the description at the beginning of this manual page.
87
88 int ei_connect(ei_cnode* ec, char *nodename)
89 int ei_xconnect(ei_cnode* ec, Erl_IpAddr adr, char *alivename)
90
91 Sets up a connection to an Erlang node.
92
93 ei_xconnect() requires the IP address of the remote host and the
94 alive name of the remote node to be specified. ei_connect() pro‐
95 vides an alternative interface and determines the information
96 from the node name provided.
97
98 * addr is the 32-bit IP address of the remote host.
99
100 * alive is the alivename of the remote node.
101
102 * node is the name of the remote node.
103
104 These functions return an open file descriptor on success, or a
105 negative value indicating that an error occurred. In the latter
106 case they set erl_errno to one of the following:
107
108 EHOSTUNREACH:
109 The remote host node is unreachable.
110
111 ENOMEM:
112 No more memory is available.
113
114 EIO:
115 I/O error.
116
117 Also, errno values from socket(2) and connect(2) system calls
118 may be propagated into erl_errno.
119
120 Example:
121
122 #define NODE "madonna@chivas.du.etx.ericsson.se"
123 #define ALIVE "madonna"
124 #define IP_ADDR "150.236.14.75"
125
126 /*** Variant 1 ***/
127 int fd = ei_connect(&ec, NODE);
128
129 /*** Variant 2 ***/
130 struct in_addr addr;
131 addr.s_addr = inet_addr(IP_ADDR);
132 fd = ei_xconnect(&ec, &addr, ALIVE);
133
134
135 int ei_connect_init(ei_cnode* ec, const char* this_node_name, const
136 char *cookie, short creation)
137 int ei_connect_xinit(ei_cnode* ec, const char *thishostname, const char
138 *thisalivename, const char *thisnodename, Erl_IpAddr thisipaddr, const
139 char *cookie, short creation)
140
141 Initializes the ec structure, to identify the node name and
142 cookie of the server. One of them must be called before other
143 functions that works on the ei_cnode type or a file descriptor
144 associated with a connection to another node is used.
145
146 * ec is a structure containing information about the C-node.
147 It is used in other ei functions for connecting and receiv‐
148 ing data.
149
150 * this_node_name is the registered name of the process (the
151 name before '@').
152
153 * cookie is the cookie for the node.
154
155 * creation identifies a specific instance of a C-node. It can
156 help prevent the node from receiving messages sent to an
157 earlier process with the same registered name.
158
159 * thishostname is the name of the machine we are running on.
160 If long names are to be used, they are to be fully qualified
161 (that is, durin.erix.ericsson.se instead of durin).
162
163 * thisalivename is the registered name of the process.
164
165 * thisnodename is the full name of the node, that is, ein‐
166 ode@durin.
167
168 * thispaddr if the IP address of the host.
169
170 A C-node acting as a server is assigned a creation number when
171 it calls ei_publish().
172
173 A connection is closed by simply closing the socket. For infor‐
174 mation about how to close the socket gracefully (when there are
175 outgoing packets before close), see the relevant system documen‐
176 tation.
177
178 These functions return a negative value indicating that an error
179 occurred.
180
181 Example 1:
182
183 int n = 0;
184 struct in_addr addr;
185 ei_cnode ec;
186 addr.s_addr = inet_addr("150.236.14.75");
187 if (ei_connect_xinit(&ec,
188 "chivas",
189 "madonna",
190 "madonna@chivas.du.etx.ericsson.se",
191 &addr;
192 "cookie...",
193 n++) < 0) {
194 fprintf(stderr,"ERROR when initializing: %d",erl_errno);
195 exit(-1);
196 }
197
198
199 Example 2:
200
201 if (ei_connect_init(&ec, "madonna", "cookie...", n++) < 0) {
202 fprintf(stderr,"ERROR when initializing: %d",erl_errno);
203 exit(-1);
204 }
205
206
207 int ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned timeout_ms)
208 int ei_xconnect_tmo(ei_cnode* ec, Erl_IpAddr adr, char *alivename,
209 unsigned timeout_ms)
210
211 Equivalent to ei_connect and ei_xconnect with an optional time-
212 out argument, see the description at the beginning of this man‐
213 ual page.
214
215 int ei_get_tracelevel(void)
216 void ei_set_tracelevel(int level)
217
218 Used to set tracing on the distribution. The levels are differ‐
219 ent verbosity levels. A higher level means more information. See
220 also section Debug Information.
221
222 These functions are not thread safe.
223
224 int ei_publish(ei_cnode *ec, int port)
225
226 Used by a server process to register with the local name server
227 EPMD, thereby allowing other processes to send messages by using
228 the registered name. Before calling either of these functions,
229 the process should have called bind() and listen() on an open
230 socket.
231
232 * ec is the C-node structure.
233
234 * port is the local name to register, and is to be the same as
235 the port number that was previously bound to the socket.
236
237 * addr is the 32-bit IP address of the local host.
238
239 To unregister with EPMD, simply close the returned descriptor.
240 Do not use ei_unpublish(), which is deprecated anyway.
241
242 On success, the function returns a descriptor connecting the
243 calling process to EPMD. On failure, -1 is returned and
244 erl_errno is set to EIO.
245
246 Also, errno values from socket(2) and connect(2) system calls
247 may be propagated into erl_errno.
248
249 int ei_publish_tmo(ei_cnode *ec, int port, unsigned timeout_ms)
250
251 Equivalent to ei_publish with an optional time-out argument, see
252 the description at the beginning of this manual page.
253
254 int ei_receive(int fd, unsigned char* bufp, int bufsize)
255
256 Receives a message consisting of a sequence of bytes in the
257 Erlang external format.
258
259 * fd is an open descriptor to an Erlang connection. It is
260 obtained from a previous ei_connect or ei_accept.
261
262 * bufp is a buffer large enough to hold the expected message.
263
264 * bufsize indicates the size of bufp.
265
266 If a tick occurs, that is, the Erlang node on the other end of
267 the connection has polled this node to see if it is still alive,
268 the function returns ERL_TICK and no message is placed in the
269 buffer. Also, erl_errno is set to EAGAIN.
270
271 On success, the message is placed in the specified buffer and
272 the function returns the number of bytes actually read. On fail‐
273 ure, the function returns ERL_ERROR and sets erl_errno to one of
274 the following:
275
276 EAGAIN:
277 Temporary error: Try again.
278
279 EMSGSIZE:
280 Buffer is too small.
281
282 EIO:
283 I/O error.
284
285 int ei_receive_encoded(int fd, char **mbufp, int *bufsz, erlang_msg
286 *msg, int *msglen)
287
288 This function is retained for compatibility with code generated
289 by the interface compiler and with code following examples in
290 the same application.
291
292 In essence, the function performs the same operation as ei_xre‐
293 ceive_msg, but instead of using an ei_x_buff, the function
294 expects a pointer to a character pointer (mbufp), where the
295 character pointer is to point to a memory area allocated by mal‐
296 loc. Argument bufsz is to be a pointer to an integer containing
297 the exact size (in bytes) of the memory area. The function may
298 reallocate the memory area and will in such cases put the new
299 size in *bufsz and update *mbufp.
300
301 Returns either ERL_TICK or the msgtype field of the erlang_msg
302 *msg. The length of the message is put in *msglen. On error a
303 value < 0 is returned.
304
305 It is recommended to use ei_xreceive_msg instead when possible,
306 for the sake of readability. However, the function will be
307 retained in the interface for compatibility and will not be
308 removed in future releases without prior notice.
309
310 int ei_receive_encoded_tmo(int fd, char **mbufp, int *bufsz,
311 erlang_msg *msg, int *msglen, unsigned timeout_ms)
312
313 Equivalent to ei_receive_encoded with an optional time-out argu‐
314 ment, see the description at the beginning of this manual page.
315
316 int ei_receive_msg(int fd, erlang_msg* msg, ei_x_buff* x)
317 int ei_xreceive_msg(int fd, erlang_msg* msg, ei_x_buff* x)
318
319 Receives a message to the buffer in x. ei_xreceive_msg allows
320 the buffer in x to grow, but ei_receive_msg fails if the message
321 is larger than the pre-allocated buffer in x.
322
323 * fd is an open descriptor to an Erlang connection.
324
325 * msg is a pointer to an erlang_msg structure and contains
326 information on the message received.
327
328 * x is buffer obtained from ei_x_new.
329
330 On success, the functions return ERL_MSG and the msg struct is
331 initialized. erlang_msg is defined as follows:
332
333 typedef struct {
334 long msgtype;
335 erlang_pid from;
336 erlang_pid to;
337 char toname[MAXATOMLEN+1];
338 char cookie[MAXATOMLEN+1];
339 erlang_trace token;
340 } erlang_msg;
341
342
343 msgtype identifies the type of message, and is one of the fol‐
344 lowing:
345
346 ERL_SEND:
347 Indicates that an ordinary send operation has occurred.
348 msg->to contains the pid of the recipient (the C-node).
349
350 ERL_REG_SEND:
351 A registered send operation occurred. msg->from contains the
352 pid of the sender.
353
354 ERL_LINK or ERL_UNLINK:
355 msg->to and msg->from contain the pids of the sender and
356 recipient of the link or unlink.
357
358 ERL_EXIT:
359 Indicates a broken link. msg->to and msg->from contain the
360 pids of the linked processes.
361
362 The return value is the same as for ei_receive.
363
364 int ei_receive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned
365 imeout_ms)
366 int ei_xreceive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned
367 timeout_ms)
368
369 Equivalent to ei_receive_msg and ei_xreceive_msg with an
370 optional time-out argument, see the description at the beginning
371 of this manual page.
372
373 int ei_receive_tmo(int fd, unsigned char* bufp, int bufsize, unsigned
374 timeout_ms)
375
376 Equivalent to ei_receive with an optional time-out argument, see
377 the description at the beginning of this manual page.
378
379 int ei_reg_send(ei_cnode* ec, int fd, char* server_name, char* buf, int
380 len)
381
382 Sends an Erlang term to a registered process.
383
384 * fd is an open descriptor to an Erlang connection.
385
386 * server_name is the registered name of the intended recipi‐
387 ent.
388
389 * buf is the buffer containing the term in binary format.
390
391 * len is the length of the message in bytes.
392
393 Returns 0 if successful, otherwise -1. In the latter case it
394 sets erl_errno to EIO.
395
396 Example:
397
398 Send the atom "ok" to the process "worker":
399
400 ei_x_buff x;
401 ei_x_new_with_version(&x);
402 ei_x_encode_atom(&x, "ok");
403 if (ei_reg_send(&ec, fd, x.buff, x.index) < 0)
404 handle_error();
405
406
407 int ei_reg_send_tmo(ei_cnode* ec, int fd, char* server_name, char* buf,
408 int len, unsigned timeout_ms)
409
410 Equivalent to ei_reg_send with an optional time-out argument,
411 see the description at the beginning of this manual page.
412
413 int ei_rpc(ei_cnode *ec, int fd, char *mod, char *fun, const char *arg‐
414 buf, int argbuflen, ei_x_buff *x)
415 int ei_rpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char
416 *argbuf, int argbuflen)
417 int ei_rpc_from(ei_cnode *ec, int fd, int timeout, erlang_msg *msg,
418 ei_x_buff *x)
419
420 Supports calling Erlang functions on remote nodes. ei_rpc_to()
421 sends an RPC request to a remote node and ei_rpc_from() receives
422 the results of such a call. ei_rpc() combines the functionality
423 of these two functions by sending an RPC request and waiting for
424 the results. See also rpc:call/4 in Kernel.
425
426 * ec is the C-node structure previously initiated by a call to
427 ei_connect_init() or ei_connect_xinit().
428
429 * fd is an open descriptor to an Erlang connection.
430
431 * timeout is the maximum time (in milliseconds) to wait for
432 results. Specify ERL_NO_TIMEOUT to wait forever. ei_rpc()
433 waits infinitely for the answer, that is, the call will
434 never time out.
435
436 * mod is the name of the module containing the function to be
437 run on the remote node.
438
439 * fun is the name of the function to run.
440
441 * argbuf is a pointer to a buffer with an encoded Erlang list,
442 without a version magic number, containing the arguments to
443 be passed to the function.
444
445 * argbuflen is the length of the buffer containing the encoded
446 Erlang list.
447
448 * msg is structure of type erlang_msg and contains information
449 on the message received. For a description of the erlang_msg
450 format, see ei_receive_msg.
451
452 * x points to the dynamic buffer that receives the result. For
453 ei_rpc() this is the result without the version magic num‐
454 ber. For ei_rpc_from() the result returns a version magic
455 number and a 2-tuple {rex,Reply}.
456
457 ei_rpc() returns the number of bytes in the result on success
458 and -1 on failure. ei_rpc_from() returns the number of bytes,
459 otherwise one of ERL_TICK, ERL_TIMEOUT, and ERL_ERROR. When
460 failing, all three functions set erl_errno to one of the follow‐
461 ing:
462
463 EIO:
464 I/O error.
465
466 ETIMEDOUT:
467 Time-out expired.
468
469 EAGAIN:
470 Temporary error: Try again.
471
472 Example:
473
474 Check to see if an Erlang process is alive:
475
476 int index = 0, is_alive;
477 ei_x_buff args, result;
478
479 ei_x_new(&result);
480 ei_x_new(&args);
481 ei_x_encode_list_header(&args, 1);
482 ei_x_encode_pid(&args, &check_pid);
483 ei_x_encode_empty_list(&args);
484
485 if (ei_rpc(&ec, fd, "erlang", "is_process_alive",
486 args.buff, args.index, &result) < 0)
487 handle_error();
488
489 if (ei_decode_version(result.buff, &index) < 0
490 || ei_decode_bool(result.buff, &index, &is_alive) < 0)
491 handle_error();
492
493
494 erlang_pid *ei_self(ei_cnode *ec)
495
496 Retrieves the pid of the C-node. Every C-node has a (pseudo) pid
497 used in ei_send_reg, ei_rpc, and others. This is contained in a
498 field in the ec structure. It will be safe for a long time to
499 fetch this field directly from the ei_cnode structure.
500
501 int ei_send(int fd, erlang_pid* to, char* buf, int len)
502
503 Sends an Erlang term to a process.
504
505 * fd is an open descriptor to an Erlang connection.
506
507 * to is the pid of the intended recipient of the message.
508
509 * buf is the buffer containing the term in binary format.
510
511 * len is the length of the message in bytes.
512
513 Returns 0 if successful, otherwise -1. In the latter case it
514 sets erl_errno to EIO.
515
516 int ei_send_encoded(int fd, erlang_pid* to, char* buf, int len)
517
518 Works exactly as ei_send, the alternative name is retained for
519 backward compatibility. The function will not be removed without
520 prior notice.
521
522 int ei_send_encoded_tmo(int fd, erlang_pid* to, char* buf, int len,
523 unsigned timeout_ms)
524
525 Equivalent to ei_send_encoded with an optional time-out argu‐
526 ment, see the description at the beginning of this manual page.
527
528 int ei_send_reg_encoded(int fd, const erlang_pid *from, const char *to,
529 const char *buf, int len)
530
531 This function is retained for compatibility with code generated
532 by the interface compiler and with code following examples in
533 the same application.
534
535 The function works as ei_reg_send with one exception. Instead of
536 taking ei_cnode as first argument, it takes a second argument,
537 an erlang_pid, which is to be the process identifier of the
538 sending process (in the Erlang distribution protocol).
539
540 A suitable erlang_pid can be constructed from the ei_cnode
541 structure by the following example code:
542
543 ei_cnode ec;
544 erlang_pid *self;
545 int fd; /* the connection fd */
546 self = ei_self(&ec);
547 self->num = fd;
548
549
550 int ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, const char
551 *to, const char *buf, int len)
552
553 Equivalent to ei_send_reg_encoded with an optional time-out
554 argument, see the description at the beginning of this manual
555 page.
556
557 int ei_send_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned
558 timeout_ms)
559
560 Equivalent to ei_send with an optional time-out argument, see
561 the description at the beginning of this manual page.
562
563 const char *ei_thisnodename(ei_cnode *ec)
564 const char *ei_thishostname(ei_cnode *ec)
565 const char *ei_thisalivename(ei_cnode *ec)
566
567 Can be used to retrieve information about the C-node. These val‐
568 ues are initially set with ei_connect_init() or ei_con‐
569 nect_xinit().
570
571 These function simply fetch the appropriate field from the ec
572 structure. Read the field directly will probably be safe for a
573 long time, so these functions are not really needed.
574
575 int ei_unpublish(ei_cnode *ec)
576
577 Can be called by a process to unregister a specified node from
578 EPMD on the local host. This is, however, usually not allowed,
579 unless EPMD was started with flag -relaxed_command_check, which
580 it normally is not.
581
582 To unregister a node you have published, you should close the
583 descriptor that was returned by ei_publish().
584
585 Warning:
586 This function is deprecated and will be removed in a future
587 release.
588
589
590 ec is the node structure of the node to unregister.
591
592 If the node was successfully unregistered from EPMD, the func‐
593 tion returns 0. Otherwise, -1 is returned and erl_errno is set
594 to EIO.
595
596 int ei_unpublish_tmo(ei_cnode *ec, unsigned timeout_ms)
597
598 Equivalent to ei_unpublish with an optional time-out argument,
599 see the description at the beginning of this manual page.
600
602 If a connection attempt fails, the following can be checked:
603
604 * erl_errno.
605
606 * That the correct cookie was used
607
608 * That EPMD is running
609
610 * That the remote Erlang node on the other side is running the same
611 version of Erlang as the ei library
612
613 * That environment variable ERL_EPMD_PORT is set correctly
614
615 The connection attempt can be traced by setting a trace level by either
616 using ei_set_tracelevel or by setting environment variable
617 EI_TRACELEVEL. The trace levels have the following messages:
618
619 * 1: Verbose error messages
620
621 * 2: Above messages and verbose warning messages
622
623 * 3: Above messages and progress reports for connection handling
624
625 * 4: Above messages and progress reports for communication
626
627 * 5: Above messages and progress reports for data conversion
628
629Ericsson AB erl_interface 3.10.2.2 ei_connect(3)