summaryrefslogtreecommitdiff
path: root/lib/erl_interface/doc/src/ei_connect.xml
blob: 390122ae94a8d333e73478684d750c6666a0177e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE cref SYSTEM "cref.dtd">

<cref>
  <header>
    <copyright>
      <year>2001</year><year>2021</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.

    </legalnotice>

    <title>ei_connect</title>
    <prepared>Jakob Cederlund</prepared>
    <docno></docno>
    <approved></approved>
    <checked></checked>
    <date>2001-09-01</date>
    <rev>A</rev>
    <file>ei_connect.xml</file>
  </header>
  <lib>ei_connect</lib>
  <libsummary>Communicate with distributed Erlang.</libsummary>
  <description>
    <p>This module enables C-programs to communicate with Erlang nodes,
      using the Erlang distribution over TCP/IP.</p>

    <p>A C-node appears to Erlang as a <em>hidden node</em>.
      That is, Erlang processes that know the name of the
      C-node can communicate with it in a normal manner, but
      the node name is not shown in the listing provided by
      <seemfa marker="erts:erlang#nodes/0"><c>erlang:nodes/0</c></seemfa>
      in <c>ERTS</c>.</p>

    <p>The environment variable <c>ERL_EPMD_PORT</c> can be used
      to indicate which logical cluster a C-node belongs to.</p>
  </description>

  <section>
    <title>Time-Out Functions</title>
    <p>Most functions appear in a version with the suffix
      <c>_tmo</c> appended to the function name. Those functions
      take an extra argument, a time-out in <em>milliseconds</em>. The
      semantics is this: for each communication primitive involved in
      the operation, if the primitive does not complete within the time
      specified, the function returns an error and
      <c>erl_errno</c> is set to <c>ETIMEDOUT</c>.
      With communication primitive is meant an operation on the socket, like
      <c>connect</c>, <c>accept</c>,
      <c>recv</c>, or <c>send</c>.</p>

    <p>Clearly the time-outs are for implementing fault tolerance,
      not to keep hard real-time promises. The <c>_tmo</c> functions
      are for detecting non-responsive peers and to avoid blocking on
      socket operations.</p>

    <p>A time-out value of <c>0</c> (zero) means that time-outs are
      disabled. Calling a <c>_tmo</c> function with the last
      argument as <c>0</c> is therefore the same thing as calling
      the function without the <c>_tmo</c> suffix.</p>

    <p>As with all other functions starting with <c>ei_</c>,
      you are <em>not</em> expected
      to put the socket in non-blocking mode yourself in the program. Every
      use of non-blocking mode is embedded inside the time-out
      functions. The socket will always be back in blocking mode after
      the operations are completed (regardless of the result). To
      avoid problems, leave the socket options alone. <c>ei</c> handles
      any socket options that need modification.</p>

    <p>In all other senses, the <c>_tmo</c> functions inherit all
      the return values and the semantics from the functions without
      the <c>_tmo</c> suffix.</p>
  </section>

  <section>
    <marker id="ussi"/>
    <title>User Supplied Socket Implementation</title>
    <p>By default <c>ei</c> supplies a TCP/IPv4 socket interface
    that is used when communicating. The user can however plug in
    his/her own IPv4 socket implementation. This, for example, in order
    to communicate over TLS. A user supplied socket implementation
    is plugged in by passing a
    <seecref marker="#ei_socket_callbacks">callback structure</seecref>
    to either
    <seecref marker="#ei_connect_init"><c>ei_connect_init_ussi()</c></seecref>
    or
    <seecref marker="#ei_connect_init"><c>ei_connect_xinit_ussi()</c></seecref>.</p>
    
    <p>All callbacks in the <c>ei_socket_callbacks</c> structure
    <em>should</em> return zero on success; and a posix error
    code on failure.</p>
    
    <p>The <c>addr</c> argument of the <c>listen</c>, <c>accept</c>,
    and <c>connect</c> callbacks refer to appropriate address
    structure for currently used protocol. Currently <c>ei</c>
    only supports IPv4. That is, at this time <c>addr</c> always
    points to a <c>struct sockaddr_in</c> structure.</p>

    <p><marker id="ei_socket_callbacks_fields"/>The
    <seecref marker="#ei_socket_callbacks"><c>ei_socket_callbacks</c></seecref>
    structure may be enlarged in the future. All fields not set, <em>needs</em>
    to be zeroed out. Currently the following fields exist:</p>

    <taglist>
      
      <tag><c>flags</c></tag>
      <item>
	<p>Flags informing <c>ei</c> about the behaviour of the
	callbacks. Flags should be bitwise or:ed together. If no flag,
	is set, the <c>flags</c> field should contain <c>0</c>. Currently,
	supported flags:</p>
	<taglist>
	  <tag><c>EI_SCLBK_FLG_FULL_IMPL</c></tag>
	  <item>
	    <p>
	      If set, the <c>accept()</c>, <c>connect()</c>,
	      <c>writev()</c>, <c>write()</c>, and <c>read()</c> callbacks
	      implements timeouts. The timeout is passed in the <c>tmo</c>
	      argument and is given in milli seconds. Note that the
	      <c>tmo</c> argument to these callbacks differ from the
	      timeout arguments in the <c>ei</c> API. Zero means a zero
	      timeout. That is, poll and timeout immediately unless the
	      operation is successful. <c>EI_SCLBK_INF_TMO</c>
	      (max <c>unsigned</c>) means infinite timeout. The file
	      descriptor is in blocking mode when a callback is called,
	      and it must be in blocking mode when the callback returns.
	    </p>
	    <p>
	      If not set, <c>ei</c> will implement the timeout using
	      <c>select()</c> in order to determine when to call the
	      callbacks and when to time out. The <c>tmo</c> arguments
	      of the <c>accept()</c>, <c>connect()</c>, <c>writev()</c>,
	      <c>write()</c>, and <c>read()</c> callbacks should be
	      ignored. The callbacks may be called in non-blocking mode.
	      The callbacks are not allowed to change between blocking
	      and non-blocking mode. In order for this to work,
	      <c>select()</c> needs to interact with the socket primitives
	      used the same way as it interacts with the ordinary socket
	      primitives. If this is not the case, the callbacks
	      <em>need</em> to implement timeouts and this flag should
	      be set.
	    </p>
	  </item>
	</taglist>
	<p>More flags may be introduced in the future.</p>
      </item>

      <tag><c>int (*socket)(void **ctx, void *setup_ctx)</c></tag>
      <item>
	<p>Create a socket and a context for the socket.</p>
	
	<p>On success it should set <c>*ctx</c> to point to a context for
	the created socket. This context will be passed to all other
	socket callbacks. This function will be passed the same
	<c>setup_context</c> as passed to the preceding
	<seecref marker="#ei_connect_init"><c>ei_connect_init_ussi()</c></seecref>
	or
	<seecref marker="#ei_connect_init"><c>ei_connect_xinit_ussi()</c></seecref>
	call.</p>
	
	<note><p>During the lifetime of a socket, the pointer <c>*ctx</c>
	<em>has</em> to remain the same. That is, it cannot later be
	relocated.</p></note>
	
	<p>This callback is mandatory.</p>
      </item>
      
      <tag><c>int (*close)(void *ctx)</c></tag>
      <item>
	<p>Close the socket identified by <c>ctx</c> and destroy the context.</p>
	
	<p>This callback is mandatory.</p>
      </item>
      
      <tag><c>int (*listen)(void *ctx, void *addr, int *len, int backlog)</c></tag>
      <item>
	<p>Bind the socket identified by <c>ctx</c> to a local interface
	and then listen on it.</p>
	
	<p>The <c>addr</c> and <c>len</c> arguments are both input and output
	arguments. When called <c>addr</c> points to an address structure of
	length <c>*len</c> containing information on how to bind the socket.
	Upon return this callback should have updated the structure referred
	by <c>addr</c> with information on how the socket actually was bound.
	<c>*len</c> should be updated to reflect the size of <c>*addr</c>
	updated. <c>backlog</c> identifies the size of the backlog for the
	listen socket.</p>
	
	<p>This callback is mandatory.</p>
      </item>
      
      <tag><c>int (*accept)(void **ctx, void *addr, int *len, unsigned tmo)</c></tag>
      <item>
	<p>Accept connections on the listen socket identified by
	<c>*ctx</c>.</p>

	<p>When a connection is accepted, a new context for the accepted
	connection should be created and <c>*ctx</c> should be updated
	to point to the new context for the accepted connection. When
	called <c>addr</c> points to an uninitialized address structure
	of length <c>*len</c>. Upon return this callback should have
	updated this structure with information about the client address.
	<c>*len</c> should be updated to reflect the size of <c>*addr</c>
	updated.
	</p>

	<p>If the <c>EI_SCLBK_FLG_FULL_IMPL</c> flag has been set,
	<c>tmo</c> contains timeout time in milliseconds.</p>

	<note><p>During the lifetime of a socket, the pointer <c>*ctx</c>
	<em>has</em> to remain the same. That is, it cannot later be
	relocated.</p></note>

	<p>This callback is mandatory.</p>
      </item>
      
      <tag><c>int (*connect)(void *ctx, void *addr, int len, unsigned tmo)</c></tag>
      <item>
	<p>Connect the socket identified by <c>ctx</c> to the address
	identified by <c>addr</c>.</p>

	<p>When called <c>addr</c> points to an address structure of
	length <c>len</c> containing information on where to connect.</p>

	<p>If the <c>EI_SCLBK_FLG_FULL_IMPL</c> flag has been set,
	<c>tmo</c> contains timeout time in milliseconds.</p>
	
	<p>This callback is mandatory.</p>
      </item>
      
      <tag><c>int (*writev)(void *ctx, const void *iov, long iovcnt, ssize_t *len, unsigned tmo)</c></tag>
      <item>
	<p>Write data on the connected socket identified by <c>ctx</c>.</p>

	<p><c>iov</c> points to an array of <c>struct iovec</c> structures of
	length <c>iovcnt</c> containing data to write to the socket. On success,
	this callback should set <c>*len</c> to the amount of bytes successfully
	written on the socket.</p>
	
	<p>If the <c>EI_SCLBK_FLG_FULL_IMPL</c> flag has been set,
	<c>tmo</c> contains timeout time in milliseconds.</p>
	
	<p>This callback is optional. Set the <c>writev</c> field
	in the the <c>ei_socket_callbacks</c> structure to <c>NULL</c> if not
	implemented.</p>
      </item>
      
      <tag><c>int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo)</c></tag>
      <item>
	<p>Write data on the connected socket identified by <c>ctx</c>.</p>
	
	<p>When called <c>buf</c> points to a buffer of length <c>*len</c>
	containing the data to write on the socket. On success, this callback
	should set <c>*len</c> to the amount of bytes successfully written on
	the socket.</p>

	<p>If the <c>EI_SCLBK_FLG_FULL_IMPL</c> flag has been set,
	<c>tmo</c> contains timeout time in milliseconds.</p>
	
	<p>This callback is mandatory.</p>
      </item>
      
      <tag><c>int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo)</c></tag>
      <item>
	<p>Read data on the connected socket identified by <c>ctx</c>.</p>
	
	<p><c>buf</c> points to a buffer of length <c>*len</c> where the
	read data should be placed. On success, this callback should update
	<c>*len</c> to the amount of bytes successfully read on the socket.</p>
	
	<p>If the <c>EI_SCLBK_FLG_FULL_IMPL</c> flag has been set,
	<c>tmo</c> contains timeout time in milliseconds.</p>
	
	<p>This callback is mandatory.</p>
      </item>
      
      <tag><c>int (*handshake_packet_header_size)(void *ctx, int *sz)</c></tag>
      <item>
	<p>Inform about handshake packet header size to use during the Erlang
	distribution handshake.</p>

	<p>On success, <c>*sz</c> should be set to the handshake packet header
	size to use. Valid values are <c>2</c> and <c>4</c>. Erlang TCP
	distribution use a handshake packet size of <c>2</c> and Erlang TLS
	distribution use a handshake packet size of <c>4</c>.</p>

	<p>This callback is mandatory.</p>
      </item>
      
      <tag><c>int (*connect_handshake_complete)(void *ctx)</c></tag>
      <item>
	<p>Called when a locally started handshake has completed successfully.</p>
	
	<p>This callback is optional. Set the <c>connect_handshake_complete</c> field
	in the <c>ei_socket_callbacks</c> structure to <c>NULL</c> if not implemented.</p>	
      </item>
      
      <tag><c>int (*accept_handshake_complete)(void *ctx)</c></tag>
      <item>
	<p>Called when a remotely started handshake has completed successfully.</p>

	<p>This callback is optional. Set the <c>accept_handshake_complete</c> field in
	the <c>ei_socket_callbacks</c> structure to <c>NULL</c> if not implemented.</p>
      </item>
      
      <tag><c>int (*get_fd)(void *ctx, int *fd)</c></tag>
      <item>
	<p>Inform about file descriptor used by the socket which is identified
	by <c>ctx</c>.</p>

	<note><p>During the lifetime of a socket, the file descriptor
	<em>has</em> to remain the same. That is, repeated calls to this
	callback with the same context <c>should</c> always report the same
	file descriptor.</p>
	<p>The file descriptor <em>has</em> to be a real file descriptor.
	That is, no other operation should be able to get the same file
	descriptor until it has been released by the <c>close()</c>
	callback.</p>
	</note>

	<p>This callback is mandatory.</p>
      </item>
    </taglist>
  </section>
  <section>
    <title>Data Types</title>
    <taglist>
      <tag><marker id="ei_cnode"/><c>ei_cnode</c></tag>
      <item><p>
	Opaque data type representing a C-node. A <c>ei_cnode</c>
	structure is initialized by calling
	<seecref marker="#ei_connect_init"><c>ei_connect_init()</c></seecref>
	or friends.
      </p></item>

      <tag><marker id="ei_socket_callbacks"/><c>ei_socket_callbacks</c></tag>
      <item><code type="none">
typedef struct {
    int flags;
    int (*socket)(void **ctx, void *setup_ctx);
    int	(*close)(void *ctx);
    int (*listen)(void *ctx, void *addr, int *len, int backlog);
    int (*accept)(void **ctx, void *addr, int *len, unsigned tmo);
    int (*connect)(void *ctx, void *addr, int len, unsigned tmo);
    int (*writev)(void *ctx, const void *iov, int iovcnt, ssize_t *len, unsigned tmo);
    int (*write)(void *ctx, const char *buf, ssize_t *len, unsigned tmo);
    int (*read)(void *ctx, char *buf, ssize_t *len, unsigned tmo);
    int (*handshake_packet_header_size)(void *ctx, int *sz);
    int (*connect_handshake_complete)(void *ctx);
    int (*accept_handshake_complete)(void *ctx);
    int (*get_fd)(void *ctx, int *fd);
} ei_socket_callbacks;</code>
        <p>
	  Callbacks functions for a <seecref marker="#ussi"><i>User
	  Supplied Socket Implementation</i></seecref>. 
	  <seecref marker="#ei_socket_callbacks_fields">Documentation
	  of each field</seecref> can be found in the
	  <i>User Supplied Socket Implementation</i> section
	  above.
	</p>

      </item>

      <tag><marker id="ErlConnect"/><c>ErlConnect</c></tag>
      <item><code type="none">
typedef struct {
    char ipadr[4]; /* Ip v4 address in network byte order */
    char nodename[MAXNODELEN];
} ErlConnect;</code>
      <p>IP v4 address and nodename.</p>
      </item>

      <tag><marker id="Erl_IpAddr"/><c>Erl_IpAddr</c></tag>
      <item><code type="none">
typedef struct {
    unsigned s_addr; /* Ip v4 address in network byte order */
} Erl_IpAddr;</code>
      <p>IP v4 address.</p>
      </item>

      <tag><marker id="erlang_msg"/><c>erlang_msg</c></tag>
      <item>
	<code type="none">
typedef struct {
    long msgtype;
    erlang_pid from;
    erlang_pid to;
    char toname[MAXATOMLEN+1];
    char cookie[MAXATOMLEN+1];
    erlang_trace token;
} erlang_msg;</code>
        <p>Information about a message received via
	<seecref marker="#ei_receive_msg"><c>ei_receive_msg()</c></seecref>
	or friends.</p>
      </item>
    </taglist>
  </section>
  <funcs>
    <func>
      <name since=""><ret>struct hostent *</ret><nametext>ei_gethostbyaddr(const char *addr, int len, int type)</nametext></name>
      <name since=""><ret>struct hostent *</ret><nametext>ei_gethostbyaddr_r(const char *addr, int length,  int type,  struct hostent *hostp, char *buffer,   int buflen,  int *h_errnop)</nametext></name>
      <name since=""><ret>struct hostent *</ret><nametext>ei_gethostbyname(const char *name)</nametext></name>
      <name since=""><ret>struct hostent *</ret><nametext>ei_gethostbyname_r(const char *name,  struct hostent *hostp,  char *buffer,  int buflen,  int *h_errnop)</nametext></name>
      <fsummary>Name lookup functions.</fsummary>
      <desc>
        <p>Convenience functions for some common name lookup functions.</p>
      </desc>
    </func>
    

    <func>
      <name since=""><ret>int</ret><nametext>ei_accept(ei_cnode *ec, int listensock, ErlConnect *conp)</nametext></name>
      <fsummary>Accept a connection from another node.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="#ErlConnect"><c>ErlConnect</c></seecref></v>
      </type>
      <desc>
        <p>Used by a server process to accept a
          connection from a client process.</p>
        <list type="bulleted">
          <item>
            <p><c>ec</c> is the C-node structure.</p>
          </item>
          <item>
            <p><c>listensock</c> is an open socket descriptor on
              which <c>listen()</c> has previously been called.</p>
          </item>
          <item>
            <p><c>conp</c> is a pointer to an
	    <seecref marker="#ErlConnect"><c>ErlConnect</c></seecref>
	    struct.</p>
          </item>
        </list>
        <p>On success, <c>conp</c> is filled in with the address and
          node name of the connecting client and a file descriptor is
          returned. On failure, <c>ERL_ERROR</c> is returned and
          <c>erl_errno</c> is set to <c>EIO</c>.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_accept_tmo(ei_cnode *ec, int listensock, ErlConnect *conp, unsigned timeout_ms)</nametext></name>
      <fsummary>Accept a connection from another node with optional
        time-out.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="#ErlConnect"><c>ErlConnect</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to
          <c>ei_accept</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since="OTP 21.3"><ret>int</ret><nametext>ei_close_connection(int fd)</nametext></name>
      <fsummary>Close a connection.</fsummary>
      <desc>
        <p>Closes a previously opened connection or listen socket.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_connect(ei_cnode* ec, char *nodename)</nametext></name>
      <name since=""><ret>int</ret><nametext>ei_xconnect(ei_cnode* ec, Erl_IpAddr adr, char *alivename)</nametext></name>
      <name since="OTP 23.0"><ret>int</ret><nametext>ei_connect_host_port(ei_cnode* ec, char *hostname, int port)</nametext></name>
      <name since="OTP 23.0"><ret>int</ret><nametext>ei_xconnect_host_port(ei_cnode* ec, Erl_IpAddr adr, int port)</nametext></name>
      <fsummary>Establish a connection to an Erlang node.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="#Erl_IpAddr"><c>Erl_IpAddr</c></seecref></v>
      </type>
      <desc>
        <p>Sets up a connection to an Erlang node.</p>
        <p><c>ei_xconnect()</c> requires the IP address of the
          remote host and the alive name of the remote node to be
          specified. <c>ei_connect()</c> provides an alternative
          interface and determines the information from the node name
          provided. The <c>ei_xconnect_host_port()</c> function provides
          yet another alternative that will work even if there is no
          EPMD instance on the host where the remote node is running. The
          <c>ei_xconnect_host_port()</c> function requires the IP
          address and port of the remote node to be specified.
          The <c>ei_connect_host_port()</c> function is an alternative
          to <c>ei_xconnect_host_port()</c> that lets the user specify
          a hostname instead of an IP address.</p>
        <list type="bulleted">
          <item><c>adr</c> is the 32-bit IP address of the remote
            host.</item>
          <item><c>alive</c> is the alivename of the remote node.
          </item>
          <item><c>node</c> is the name of the remote node.</item>
          <item><c>port</c> is the port number of the remote node.</item>
        </list>
        <p>These functions return an open file descriptor on success, or
          a negative value indicating that an error occurred. In the latter
          case they set <c>erl_errno</c> to one of the
          following:</p>
        <taglist>
          <tag><c>EHOSTUNREACH</c></tag>
          <item>The remote host <c>node</c> is unreachable.</item>
          <tag><c>ENOMEM</c></tag>
          <item>No more memory is available.</item>
          <tag><c>EIO</c></tag>
          <item>I/O error.</item>
        </taglist>
        <p>Also, <c>errno</c> values from
          <c>socket</c><em>(2)</em> and
          <c>connect</c><em>(2)</em>
          system calls may be propagated into <c>erl_errno</c>.</p>
        <p><em>Example:</em></p>
        <code type="none"><![CDATA[
#define NODE   "madonna@chivas.du.etx.ericsson.se"
#define ALIVE  "madonna"
#define IP_ADDR "150.236.14.75"

/*** Variant 1 ***/
int fd = ei_connect(&ec, NODE);

/*** Variant 2 ***/
struct in_addr addr;
addr.s_addr = inet_addr(IP_ADDR);
fd = ei_xconnect(&ec, &addr, ALIVE);
        ]]></code>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_connect_init(ei_cnode* ec, const char* this_node_name, const char *cookie, unsigned creation)</nametext></name>
      <name since="OTP 21.3"><ret>int</ret><nametext>ei_connect_init_ussi(ei_cnode* ec, const char* this_node_name, const char *cookie, unsigned creation, ei_socket_callbacks *cbs, int cbs_sz, void *setup_context)</nametext></name>
      <name since=""><ret>int</ret><nametext>ei_connect_xinit(ei_cnode* ec, const char *thishostname, const char *thisalivename, const char *thisnodename, Erl_IpAddr thisipaddr, const char *cookie, unsigned creation)</nametext></name>
      <name since="OTP 21.3"><ret>int</ret><nametext>ei_connect_xinit_ussi(ei_cnode* ec, const char *thishostname, const char *thisalivename, const char *thisnodename, Erl_IpAddr thisipaddr, const char *cookie, unsigned creation, ei_socket_callbacks *cbs, int cbs_sz, void *setup_context)</nametext></name>
      <fsummary>Initialize for a connection.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="#Erl_IpAddr"><c>Erl_IpAddr</c></seecref></v>
	<v><seecref marker="#ei_socket_callbacks"><c>ei_socket_callbacks</c></seecref></v>
      </type>
      <desc>
        <p>Initializes the <c>ec</c> structure, to
          identify the node name and cookie of the server. One of them
          must be called before other functions that works on the
          <c>ei_cnode</c> type or a file descriptor associated with
          a connection to another node is used.</p>
        <list type="bulleted">
          <item>
            <p><c>ec</c> is a structure containing information about
              the C-node. It is used in other <c>ei</c> functions
              for connecting and receiving data.</p>
          </item>
          <item>
            <p><c>this_node_name</c> is the name of the C-node
              (the name before '@' in the full node name).</p>
          </item>
          <item>
            <p><c>cookie</c> is the cookie for the node.</p>
          </item>
          <item>
            <p><c>creation</c> identifies a specific instance of a
              C-node. It can help prevent the node from receiving messages
            sent to an earlier process with the same registered name.</p>
            <note>
              <p>
                The type of the <c>creation</c> argument was changed from
                <c>short</c> (16 bit) to <c>unsigned int</c> (32 bit) in OTP
                25. This should cause no practical problem other than maybe a
                compiler warning.
              </p>
            </note>
          </item>
          <item>
            <p><c>thishostname</c> is the name of the machine we are
              running on. If long names are to be used, they are to be fully
              qualified (that is, <c>durin.erix.ericsson.se</c>
              instead of <c>durin</c>).</p>
          </item>
          <item>
            <p><c>thisalivename</c> is the name of the local C-node (the name
	    before '@' in the full node name). Can be <c>NULL</c> (from OTP 23)
	    to get a dynamically assigned name from the peer node.</p>
          </item>
          <item>
            <p><c>thisnodename</c> is the full name of the local C-node,
              that is, <c>mynode@myhost</c>. Can be <c>NULL</c> if
	      <c>thisalivename</c> is <c>NULL</c>.</p>
          </item>
          <item>
            <p><c>thispaddr</c> if the IP address of the host.</p>
          </item>
	  <item>
	    <p><c>cbs</c> is a pointer to a
	    <seecref marker="#ei_socket_callbacks">callback structure</seecref>
	    implementing and alternative socket interface.</p>
	  </item>
	  <item>
	    <p><c>cbs_sz</c> is the size of the structure
	    pointed to by <c>cbs</c>.</p>
	  </item>
	  <item>
	    <p><c>setup_context</c> is a pointer to a structure that
	    will be passed as second argument to the <c>socket</c> callback
	    in the <c>cbs</c> structure.</p>
	  </item>

        </list>
        <p>A C-node acting as a server is assigned a creation
          number when it calls <c>ei_publish()</c>.</p>
        <p>A connection is closed by simply closing the socket.
          For information about how to close the socket gracefully (when
          there are outgoing packets before close), see the relevant system
          documentation.</p>
        <p>These functions return a negative value indicating that an error
          occurred.</p>
        <p><em>Example 1:</em></p>
        <code type="none"><![CDATA[
unsigned n = 0;
struct in_addr addr;
ei_cnode ec;
addr.s_addr = inet_addr("150.236.14.75");
if (ei_connect_xinit(&ec,
                     "chivas",
                     "madonna",
                     "madonna@chivas.du.etx.ericsson.se",
                     &addr;
                     "cookie...",
                     n++) < 0) {
    fprintf(stderr,"ERROR when initializing: %d",erl_errno);
    exit(-1);
}
        ]]></code>
        <p><em>Example 2:</em></p>
        <code type="none"><![CDATA[
if (ei_connect_init(&ec, "madonna", "cookie...", n++) < 0) {
    fprintf(stderr,"ERROR when initializing: %d",erl_errno);
    exit(-1);
}
        ]]></code>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_connect_tmo(ei_cnode* ec, char *nodename, unsigned timeout_ms)</nametext></name>
      <name since=""><ret>int</ret><nametext>ei_xconnect_tmo(ei_cnode* ec, Erl_IpAddr adr, char *alivename, unsigned timeout_ms)</nametext></name>
      <name since="OTP 23.0"><ret>int</ret><nametext>ei_connect_host_port_tmo(ei_cnode* ec, char *hostname, int port, unsigned ms)</nametext></name>
      <name since="OTP 23.0"><ret>int</ret><nametext>ei_xconnect_host_port_tmo(ei_cnode* ec, Erl_IpAddr adr, int port, unsigned ms)</nametext></name>
      <fsummary>Establish a connection to an Erlang node with optional
        time-out.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="#Erl_IpAddr"><c>Erl_IpAddr</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to <c>ei_connect</c>, <c>ei_xconnect</c>,
        <c>ei_connect_host_port</c> and
        <c>ei_xconnect_host_port</c> with an optional time-out
        argument, see the description at the beginning of this manual
        page.</p>
      </desc>
    </func>

    <func>
      <name since="OTP R13B04"><ret>int</ret><nametext>ei_get_tracelevel(void)</nametext></name>
      <name since="OTP R13B04"><ret>void</ret><nametext>ei_set_tracelevel(int level)</nametext></name>
      <fsummary>Get and set functions for tracing.</fsummary>
      <desc>
        <p>Used to set tracing on the distribution. The levels are different
          verbosity levels. A higher level means more information. See also
          section <seecref marker="#debug_information">
          Debug Information</seecref>.</p>
	<p>These functions are not thread safe.</p>
      </desc>
    </func>

    <func>
      <name since="OTP 21.3"><ret>int</ret><nametext>ei_listen(ei_cnode *ec, int *port, int backlog)</nametext></name>
      <name since="OTP 21.3"><ret>int</ret><nametext>ei_xlisten(ei_cnode *ec, Erl_IpAddr adr, int *port, int backlog)</nametext></name>
      <fsummary>Create a listen socket.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="#Erl_IpAddr"><c>Erl_IpAddr</c></seecref></v>
      </type>
      <desc>
        <p>Used by a server process to setup a listen socket which
	later can be used for accepting connections from client processes.
	</p>
        <list type="bulleted">
          <item>
            <p><c>ec</c> is the C-node structure.</p>
          </item>
	  <item>
	    <p><c>adr</c> is local interface to bind to.</p>
	  </item>
          <item>
            <p><c>port</c> is a pointer to an integer containing the
	    port number to bind to. If <c>*port</c> equals <c>0</c>
	    when calling <c>ei_listen()</c>, the socket will be bound to
	    an ephemeral port. On success, <c>ei_listen()</c> will update
	    the value of <c>*port</c> to the port actually bound to.
	    </p>
          </item>
          <item>
            <p><c>backlog</c> is maximum backlog of pending connections.</p>
          </item>
        </list>
	<p><c>ei_listen</c> will create a socket, bind to a port on the
	local interface identified by <c>adr</c> (or all local interfaces if
	<c>ei_listen()</c> is called), and mark the socket as a passive socket
	(that is, a socket that will be used for accepting incoming connections).
	</p>
	<p>
	  On success, a file descriptor is returned which can be used in a call to
	  <c>ei_accept()</c>. On failure, <c>ERL_ERROR</c> is returned and
        <c>erl_errno</c> is set to <c>EIO</c>.</p>
      </desc>
    </func>

    <func>
      <name since="OTP 23.0"><ret>int</ret><nametext>ei_make_pid(ei_cnode *ec, erlang_pid *pid)</nametext></name>
      <fsummary>Create a new process identifier</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="ei#erlang_pid"><c>erlang_pid</c></seecref></v>
      </type>
      <desc>
	<p>
	  Creates a new process identifier in the argument <c>pid</c>. This process identifier
	  refers to a conseptual process residing on the C-node identified by the argument
	  <c>ec</c>. On success <c>0</c> is returned. On failure <c>ERL_ERROR</c> is
	  returned and <c>erl_errno</c> is set.
	</p>
	<p>
	  The C-node identified by <c>ec</c> must have been initialized and must have
	  received a name prior to the call to <c>ei_make_pid()</c>. Initialization
	  of the C-node is done by a call to
	  <seecref marker="#ei_connect_init"><c>ei_connect_init()</c></seecref>
	  or friends. If the name is dynamically assigned from the peer node, the
	  C-node also has to be connected.
	</p>
      </desc>
    </func>
    
    <func>
      <name since="OTP 23.0"><ret>int</ret><nametext>ei_make_ref(ei_cnode *ec, erlang_ref *ref)</nametext></name>
      <fsummary>Create a new reference</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="ei#erlang_ref"><c>erlang_ref</c></seecref></v>
      </type>
      <desc>
	<p>
	  Creates a new reference in the argument <c>ref</c>. This reference originates
	  from the C-node identified by the argument <c>ec</c>. On success <c>0</c> is
	  returned. On failure <c>ERL_ERROR</c> is returned and <c>erl_errno</c> is set.
	</p>
	<p>
	  The C-node identified by <c>ec</c> must have been initialized and must have
	  received a name prior to the call to <c>ei_make_ref()</c>. Initialization
	  of the C-node is done by a call to
	  <seecref marker="#ei_connect_init"><c>ei_connect_init()</c></seecref>
	  or friends. If the name is dynamically assigned from the peer node, the
	  C-node also has to be connected.
	</p>
      </desc>
    </func>
    
    <func>
      <name since=""><ret>int</ret><nametext>ei_publish(ei_cnode *ec, int port)</nametext></name>
      <fsummary>Publish a node name.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
      </type>
      <desc>
        <p>Used by a server process to register
          with the local name server EPMD, thereby allowing
          other processes to send messages by using the registered name.
          Before calling either of these functions, the process should
          have called <c>bind()</c> and <c>listen()</c>
          on an open socket.</p>
        <list type="bulleted">
          <item>
            <p><c>ec</c> is the C-node structure.</p>
          </item>
          <item>
            <p><c>port</c> is the local name to register, and is to
              be the same as the port number that was previously bound to the
              socket.</p>
          </item>
          <item>
            <p><c>addr</c> is the 32-bit IP address of the local
              host.</p>
          </item>
        </list>
        <p>To unregister with EPMD, simply close the returned descriptor. Do
          not use <c>ei_unpublish()</c>, which is deprecated
          anyway.</p>
        <p>On success, the function returns a descriptor connecting the
          calling process to EPMD. On failure, <c>-1</c> is returned and
          <c>erl_errno</c> is set to <c>EIO</c>.</p>
        <p>Also, <c>errno</c> values from
          <c>socket</c><em>(2)</em> and
          <c>connect</c><em>(2)</em> system calls may be propagated
          into <c>erl_errno</c>.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_publish_tmo(ei_cnode *ec, int port, unsigned timeout_ms)</nametext></name>
      <fsummary>Publish a node name with optional time-out.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to
          <c>ei_publish</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_receive(int fd, unsigned char* bufp, int bufsize)</nametext></name>
      <fsummary>Receive a message.</fsummary>
      <desc>
        <p>Receives a message consisting of a sequence
          of bytes in the Erlang external format.</p>
        <list type="bulleted">
          <item>
            <p><c>fd</c> is an open descriptor to an Erlang
              connection. It is obtained from a previous
              <c>ei_connect</c> or <c>ei_accept</c>.</p>
          </item>
          <item>
            <p><c>bufp</c> is a buffer large enough to hold the
              expected message.</p>
          </item>
          <item>
            <p><c>bufsize</c> indicates the size of
              <c>bufp</c>.</p>
          </item>
        </list>
        <p>If a <em>tick</em> occurs, that is, the Erlang node on the
          other end of the connection has polled this node to see if it
          is still alive, the function returns <c>ERL_TICK</c> and
          no message is placed in the buffer. Also,
          <c>erl_errno</c> is set to <c>EAGAIN</c>.</p>
        <p>On success, the message is placed in the specified buffer
          and the function returns the number of bytes actually read. On
          failure, the function returns <c>ERL_ERROR</c> and sets
          <c>erl_errno</c> to one of the following:</p>
        <taglist>
          <tag><c>EAGAIN</c></tag>
          <item>Temporary error: Try again.</item>
          <tag><c>EMSGSIZE</c></tag>
          <item>Buffer is too small.</item>
          <tag><c>EIO</c></tag>
          <item>I/O error.</item>
        </taglist>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_receive_encoded(int fd, char **mbufp, int *bufsz,  erlang_msg *msg, int *msglen)</nametext></name>
      <fsummary>Obsolete function for receiving a message.</fsummary>
      <type>
	<v><seecref marker="#erlang_msg"><c>erlang_msg</c></seecref></v>
      </type>
      <desc>
        <p>This function is retained for compatibility with code
          generated by the interface compiler and with code following
          examples in the same application.</p>
        <p>In essence, the function performs the same operation as
          <c>ei_xreceive_msg</c>, but instead of using an
          <c>ei_x_buff</c>, the function expects a pointer to a character
          pointer (<c>mbufp</c>), where the character pointer
          is to point to a memory area allocated by <c>malloc</c>.
          Argument <c>bufsz</c> is to be a pointer to an integer
          containing the exact size (in bytes) of the memory area. The function
          may reallocate the memory area and will in such cases put the new
          size in <c>*bufsz</c> and update
          <c>*mbufp</c>.</p>
        <p>Returns either <c>ERL_TICK</c> or the
          <c>msgtype</c> field of the
          <c>erlang_msg *msg</c>. The length
          of the message is put in <c>*msglen</c>. On error
          a value <c>&lt; 0</c> is returned.</p>
        <p>It is recommended to use <c>ei_xreceive_msg</c> instead when
          possible, for the sake of readability. However, the function will
          be retained in the interface for compatibility and
          will <em>not</em> be removed in future releases without prior
          notice.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_receive_encoded_tmo(int fd, char **mbufp, int *bufsz,  erlang_msg *msg, int *msglen, unsigned timeout_ms)</nametext></name>
      <fsummary>Obsolete function for receiving a message with time-out.
      </fsummary>
      <type>
	<v><seecref marker="#erlang_msg"><c>erlang_msg</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to
          <c>ei_receive_encoded</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_receive_msg(int fd, erlang_msg* msg, ei_x_buff* x)</nametext></name>
      <name since=""><ret>int</ret><nametext>ei_xreceive_msg(int fd, erlang_msg* msg, ei_x_buff* x)</nametext></name>
      <fsummary>Receive a message.</fsummary>
      <type>
	<v><seecref marker="ei#ei_x_buff"><c>ei_x_buff</c></seecref></v>
	<v><seecref marker="#erlang_msg"><c>erlang_msg</c></seecref></v>
      </type>
      <desc>
        <p>Receives a message to the buffer in <c>x</c>.
          <c>ei_xreceive_msg</c> allows the buffer in
          <c>x</c> to grow, but <c>ei_receive_msg</c>
          fails if the message is larger than the pre-allocated buffer in
          <c>x</c>.</p>
        <list type="bulleted">
          <item><c>fd</c> is an open descriptor to an Erlang
            connection.</item>
          <item><c>msg</c> is a pointer to an
            <c>erlang_msg</c> structure
            and contains information on the message received.</item>
          <item><c>x</c> is buffer obtained from
            <c>ei_x_new</c>.</item>
        </list>
        <p>On success, the functions return <c>ERL_MSG</c> and the
        <seecref marker="#erlang_msg"><c>msg</c></seecref> struct
	is initialized.</p>
        <p><c>msgtype</c> identifies the type of message, and is
          one of the following:</p>
        <taglist>
          <tag><c>ERL_SEND</c></tag>
          <item>
            <p>Indicates that an ordinary send operation has occurred.
              <c>msg-&gt;to</c> contains the pid of the recipient (the
              C-node).</p>
          </item>
          <tag><c>ERL_REG_SEND</c></tag>
          <item>
            <p>A registered send operation occurred.
              <c>msg-&gt;from</c> contains the pid of the sender.</p>
          </item>
          <tag><c>ERL_LINK</c> or
            <c>ERL_UNLINK</c></tag>
          <item>
            <p><c>msg-&gt;to</c> and
              <c>msg-&gt;from</c> contain the pids of the
              sender and recipient of the link or unlink.</p>
          </item>
          <tag><c>ERL_EXIT</c></tag>
          <item>
            <p>Indicates a broken link. <c>msg-&gt;to</c> and
              <c>msg-&gt;from</c> contain the pids of the linked
               processes.</p>
          </item>
        </taglist>
        <p>The return value is the same as for
          <seecref marker="#ei_receive"><c>ei_receive</c></seecref>.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_receive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned imeout_ms)</nametext></name>
      <name since=""><ret>int</ret><nametext>ei_xreceive_msg_tmo(int fd, erlang_msg* msg, ei_x_buff* x, unsigned timeout_ms)</nametext></name>
      <fsummary>Receive a message with optional time-out.</fsummary>
      <type>
	<v><seecref marker="ei#ei_x_buff"><c>ei_x_buff</c></seecref></v>
	<v><seecref marker="#erlang_msg"><c>erlang_msg</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to <c>ei_receive_msg</c> and <c>ei_xreceive_msg</c>
          with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_receive_tmo(int fd, unsigned char* bufp, int bufsize, unsigned timeout_ms)</nametext></name>
      <fsummary>Receive a message with optional time-out.</fsummary>
      <desc>
        <p>Equivalent to
          <c>ei_receive</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_reg_send(ei_cnode* ec, int fd, char* server_name, char* buf, int len)</nametext></name>
      <fsummary>Send a message to a registered name.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
      </type>
      <desc>
        <p>Sends an Erlang term to a registered process.</p>
        <list type="bulleted">
          <item>
            <p><c>fd</c> is an open descriptor to an Erlang
              connection.</p>
          </item>
          <item><c>server_name</c> is the registered name of the
            intended recipient.</item>
          <item><c>buf</c> is the buffer containing the term in
            binary format.</item>
          <item><c>len</c> is the length of the message in bytes.
          </item>
        </list>
        <p>Returns <c>0</c> if successful, otherwise <c>-1</c>. In
          the latter case it sets <c>erl_errno</c> to
          <c>EIO</c>.</p>
        <p><em>Example:</em></p>
        <p>Send the atom "ok" to the process "worker":</p>
        <code type="none"><![CDATA[
ei_x_buff x;
ei_x_new_with_version(&x);
ei_x_encode_atom(&x, "ok");
if (ei_reg_send(&ec, fd, x.buff, x.index) < 0)
    handle_error();
        ]]></code>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_reg_send_tmo(ei_cnode* ec, int fd, char* server_name, char* buf, int len, unsigned timeout_ms)</nametext></name>
      <fsummary>Send a message to a registered name with optional time-out
      </fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to
          <c>ei_reg_send</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_rpc(ei_cnode *ec, int fd, char *mod, char *fun, const char *argbuf, int argbuflen, ei_x_buff *x)</nametext></name>
      <name since=""><ret>int</ret><nametext>ei_rpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char *argbuf, int argbuflen)</nametext></name>
      <name since="OTP 24.0"><ret>int</ret><nametext>ei_xrpc_to(ei_cnode *ec, int fd, char *mod, char *fun, const char *argbuf, int argbuflen, int flags)</nametext></name>
      <name since=""><ret>int</ret><nametext>ei_rpc_from(ei_cnode *ec, int fd, int timeout, erlang_msg *msg, ei_x_buff *x)</nametext></name>
      <fsummary>Remote Procedure Call from C to Erlang.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="ei#ei_x_buff"><c>ei_x_buff</c></seecref></v>
	<v><seecref marker="#erlang_msg"><c>erlang_msg</c></seecref></v>
      </type>
      <desc>
        <p>
          Supports calling Erlang functions on remote nodes.
          <c>ei_rpc_to()</c> sends an RPC request to a remote node and
          <c>ei_rpc_from()</c> receives the results of such a
          call. <c>ei_rpc()</c> combines the functionality of these two
          functions by sending an RPC request and waiting for the
          results.
        </p>
        <p>
          The <c>ei_xrpc_to()</c> function is equivalent to
          <c>ei_rpc_to()</c> when its <c>flags</c> parameter is set to
          <c>0</c>. When the flags parameter of <c>ei_xrpc_to()</c> is
          set to <c>EI_RPC_FETCH_STDOUT</c>, stdout (standard output)
          data are forwarded. See the documentation for the flags
          parameter for more information about the
          <c>EI_RPC_FETCH_STDOUT</c> flag.
        </p>
        <p>
        <seemfa marker="kernel:rpc#call/4"><c>rpc:call/4</c></seemfa>
        in Kernel.
        </p>
        <list type="bulleted">
          <item>
            <p><c>ec</c> is the C-node structure previously
              initiated by a call to <c>ei_connect_init()</c> or
              <c>ei_connect_xinit()</c>.</p>
          </item>
          <item>
            <p><c>fd</c> is an open descriptor to an Erlang
              connection.</p>
          </item>
          <item>
            <p><c>timeout</c> is the maximum time (in milliseconds)
              to wait for results. Specify <c>ERL_NO_TIMEOUT</c> to
              wait forever.
              <c>ei_rpc()</c> waits infinitely for the answer,
              that is, the call will never time out.</p>
          </item>
          <item>
            <p><c>mod</c> is the name of the module containing the
              function to be run on the remote node.</p>
          </item>
          <item>
            <p><c>fun</c> is the name of the function to run.</p>
          </item>
          <item>
            <p><c>argbuf</c> is a pointer to a buffer with an
              encoded Erlang list, without a version magic number, containing
              the arguments to be passed to the function.</p>
          </item>
          <item>
            <p><c>argbuflen</c> is the length of the buffer
              containing the encoded Erlang list.</p>
          </item>
          <item>
            <p><c>msg</c> is structure of type
              <c>erlang_msg</c> and contains information on the
              message
              received. For a description of the <c>erlang_msg</c>
              format, see <seecref marker="#ei_receive_msg">
              <c>ei_receive_msg</c></seecref>.</p>
          </item>
          <item>
            <p><c>x</c> points to the dynamic buffer that receives the
            result. For <c>ei_rpc()</c> this is the result without the
            version magic number. For an <c>ei_rpc_from()</c> call the
            result consists of a version magic number and a 2-tuple.
            The 2-tuple can be in one of the following two forms:</p>
            <taglist>
              <tag><c>{rex,Reply}</c></tag>
              <item>
                This response value means that the RPC has
                completed. The result value is the <c>Reply</c>
                term. This is the only type of response that one can
                get from an RPC triggered by a call to
                <c>ei_rpc_to()</c> or <c>ei_xrpc_to()</c> without the
                <c>EI_RPC_FETCH_STDOUT</c> flag. If the RPC was
                triggered by a call to <c>ei_xrpc_to()</c> with the
                <c>EI_RPC_FETCH_STDOUT</c> flag set, then all forwarded
                stdout data has been received.
              </item>
              <tag><c>{rex_stdout,StdOutUTF8Binary}</c></tag>
              <item>
                This response value can only be obtained if the RPC
                call was triggered by a call to <c>ei_xrpc_to()</c>
                with the <c>EI_RPC_FETCH_STDOUT</c> flag set. This
                response value means that forwarded stdout data has
                been received. The stdout data is stored in a binary
                and is UTF-8 encoded. One may need to call
                <c>ei_rpc_from()</c> multiple times to read all the
                stdout data. The stdout data is received in the same
                order as it was written. All forwarded stdout data have
                been received when a <c>{rex,Reply}</c> tuple has been
                obtained from an <c>ei_rpc_from()</c> call.
              </item>
            </taglist>
          </item>
           <item>
            <p><c>flags</c> The flag <c>EI_RPC_FETCH_STDOUT</c> is
            currently the only flag that is supported by
            <c>ei_xrpc_to()</c>. When <c>EI_RPC_FETCH_STDOUT</c> is
            set, the called function is executed in a new process with
            a <seemfa marker="erts:erlang#group_leader/0">group
            leader</seemfa> that forwards all stdout data. This means
            that stdout data that are written during the execution of
            the called function, by the called function and by
            descendant processes, will be forwarded (given that the
            group leader has not been changed by a call to <seemfa
            marker="erts:erlang#group_leader/2"><c>erlang:group_leader/2</c></seemfa>).
            The forwarded stdout data need to be collected by a
            sequence of calls to <c>ei_rpc_from()</c>. See the
            description of the <c>x</c> parameter for how
            <c>ei_rpc_from()</c> is used to receive stdout data. See
            the documentation of the <seeguide
            marker="stdlib:io_protocol">the I/O protocol</seeguide>,
            for more information about the group leader concept.</p>
            <note>
              <p>
                The flag <c>EI_RPC_FETCH_STDOUT</c> only works when
                interacting with a node with a version greater or
                equal to OTP-24.
              </p>
            </note>
          </item>
        </list>
        <p><c>ei_rpc()</c> returns the number of bytes in the result
        on success and <c>-1</c> on failure.  <c>ei_rpc_from()</c>
        returns the number of bytes, otherwise one of <c>ERL_TICK</c>,
        <c>ERL_TIMEOUT</c>, and <c>ERL_ERROR</c>. The functions
        <c>ei_rpc_to()</c> and <c>ei_xrpc_to()</c> returns 0 if
        successful, otherwise -1. When failing, all four functions set
        <c>erl_errno</c> to one of the following:</p>
        <taglist>
          <tag><c>EIO</c></tag>
          <item>I/O error.</item>
          <tag><c>ETIMEDOUT</c></tag>
          <item>Time-out expired.</item>
          <tag><c>EAGAIN</c></tag>
          <item>Temporary error: Try again.</item>
        </taglist>
        <p><em>Example:</em></p>
        <p>Check to see if an Erlang process is alive:</p>
        <code type="none"><![CDATA[
int index = 0, is_alive;
ei_x_buff args, result;

ei_x_new(&result);
ei_x_new(&args);
ei_x_encode_list_header(&args, 1);
ei_x_encode_pid(&args, &check_pid);
ei_x_encode_empty_list(&args);

if (ei_rpc(&ec, fd, "erlang", "is_process_alive",
           args.buff, args.index, &result) < 0)
    handle_error();

if (ei_decode_version(result.buff, &index) < 0
    || ei_decode_bool(result.buff, &index, &is_alive) < 0)
    handle_error();
        ]]></code>
      </desc>
    </func>

    <func>
      <name since=""><ret>erlang_pid *</ret><nametext>ei_self(ei_cnode *ec)</nametext></name>
      <fsummary>Retrieve the pid of the C-node.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
	<v><seecref marker="ei#erlang_pid"><c>erlang_pid</c></seecref></v>
      </type>
      <desc>
        <p>Retrieves a generic pid of the C-node. Every C-node
          has a (pseudo) pid used in <c>ei_send_reg</c>,
          <c>ei_rpc()</c>,
          and others. This is contained in a field in the <c>ec</c>
          structure. Do <em>not</em> modify this structure.
	</p>
	<p>
	  On success a pointer to the process identifier is returned.
	  On failure <c>NULL</c> is returned and <c>erl_errno</c> is
	  set.
	</p>
	<p>
	  The C-node identified by <c>ec</c> must have been initialized
	  and must have received a name prior to the call to <c>ei_self()</c>.
	  Initialization of the C-node is done by a call to
	  <seecref marker="#ei_connect_init"><c>ei_connect_init()</c></seecref>
	  or friends. If the name is dynamically assigned from the peer node, the
	  C-node also has to be connected.
	</p>

      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_send(int fd, erlang_pid* to, char* buf, int len)</nametext></name>
      <fsummary>Send a message.</fsummary>
      <type>
	<v><seecref marker="ei#erlang_pid"><c>erlang_pid</c></seecref></v>
      </type>
      <desc>
        <p>Sends an Erlang term to a process.</p>
        <list type="bulleted">
          <item><c>fd</c> is an open descriptor to an Erlang
            connection.</item>
          <item><c>to</c> is the pid of the intended recipient of
            the message.</item>
          <item><c>buf</c> is the buffer containing the term in
            binary format.</item>
          <item><c>len</c> is the length of the message in bytes.
          </item>
        </list>
        <p>Returns <c>0</c> if successful, otherwise <c>-1</c>. In
          the latter case it sets <c>erl_errno</c> to
          <c>EIO</c>.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_send_encoded(int fd, erlang_pid* to, char* buf, int len)</nametext></name>
      <fsummary>Obsolete function to send a message.</fsummary>
      <type>
	<v><seecref marker="ei#erlang_pid"><c>erlang_pid</c></seecref></v>
      </type>
      <desc>
        <p>Works exactly as <c>ei_send</c>, the alternative name is retained for
          backward compatibility. The function will <em>not</em> be
          removed without prior notice.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_send_encoded_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned timeout_ms)</nametext></name>
      <fsummary>Obsolete function to send a message with optional time-out.
      </fsummary>
      <type>
	<v><seecref marker="ei#erlang_pid"><c>erlang_pid</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to
          <c>ei_send_encoded</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_send_reg_encoded(int fd, const erlang_pid *from, const char *to, const char *buf, int len)</nametext></name>
      <fsummary>Obsolete function to send a message to a registered name.
      </fsummary>
      <type>
	<v><seecref marker="ei#erlang_pid"><c>erlang_pid</c></seecref></v>
      </type>
      <desc>
        <p>This function is retained for compatibility with code
          generated by the interface compiler and with code following
          examples in the same application.</p>
        <p>The function works as <c>ei_reg_send</c> with one
          exception. Instead of taking <c>ei_cnode</c> as first
          argument, it takes a second argument, an
          <c>erlang_pid</c>,
          which is to be the process identifier of the sending process
          (in the Erlang distribution protocol).</p>
        <p>A suitable <c>erlang_pid</c> can be retrieved from the
        <c>ei_cnode</c> structure by calling <c>ei_self(cnode_pointer)</c>.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, const char *to, const char *buf, int len, unsigned timeout_ms)</nametext></name>
      <fsummary>Obsolete function to send a message to a registered name with
        time-out.</fsummary>
      <type>
	<v><seecref marker="ei#erlang_pid"><c>erlang_pid</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to
          <c>ei_send_reg_encoded</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_send_tmo(int fd, erlang_pid* to, char* buf, int len, unsigned timeout_ms)</nametext></name>
      <fsummary>Send a message with optional time-out.</fsummary>
      <type>
	<v><seecref marker="ei#erlang_pid"><c>erlang_pid</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to
          <c>ei_send</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>const char *</ret><nametext>ei_thisnodename(ei_cnode *ec)</nametext></name>
      <name since=""><ret>const char *</ret><nametext>ei_thishostname(ei_cnode *ec)</nametext></name>
      <name since=""><ret>const char *</ret><nametext>ei_thisalivename(ei_cnode *ec)</nametext></name>
      <fsummary>Retrieve some values.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
      </type>
      <desc>
        <p>Can be used to retrieve information about
          the C-node. These values are initially set with
          <c>ei_connect_init()</c> or
          <c>ei_connect_xinit()</c>.</p>
        <p>These function simply fetch the appropriate field from the
          <c>ec</c>
          structure. Read the field directly will probably be safe for
          a long time, so these functions are not really needed.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_unpublish(ei_cnode *ec)</nametext></name>
      <fsummary>Forcefully unpublish a node name.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
      </type>
      <desc>
        <p>Can be called by a process to unregister a
          specified node from EPMD on the local host. This is, however, usually
	  not allowed, unless EPMD was started with flag
          <c>-relaxed_command_check</c>, which it normally is not.</p>
        <p>To unregister a node you have published, you should
	  close the descriptor that was returned by
	  <c>ei_publish()</c>.</p>
        <warning>
          <p>This function is deprecated and will be removed in a future
	  release.</p>
        </warning>
        <p><c>ec</c> is the node structure of the node to
          unregister.</p>
        <p>If the node was successfully unregistered from EPMD, the
          function returns <c>0</c>. Otherwise, <c>-1</c> is returned and
          <c>erl_errno</c> is set to <c>EIO</c>.</p>
      </desc>
    </func>

    <func>
      <name since=""><ret>int</ret><nametext>ei_unpublish_tmo(ei_cnode *ec, unsigned timeout_ms)</nametext></name>
      <fsummary>Unpublish a node name with optional time-out.</fsummary>
      <type>
	<v><seecref marker="#ei_cnode"><c>ei_cnode</c></seecref></v>
      </type>
      <desc>
        <p>Equivalent to
          <c>ei_unpublish</c> with an optional time-out argument,
          see the description at the beginning of this manual page.</p>
      </desc>
    </func>
  </funcs>

  <section>
    <marker id="debug_information"></marker>
    <title>Debug Information</title>
    <p>If a connection attempt fails, the following can be checked:</p>

    <list type="bulleted">
      <item><c>erl_errno</c>.</item>
      <item>That the correct cookie was used</item>
      <item>That EPMD is running</item>
      <item>That the remote Erlang node on the other side is running the
        same version of Erlang as the <c>ei</c> library</item>
      <item>That environment variable <c>ERL_EPMD_PORT</c>
        is set correctly</item>
    </list>

    <p>The connection attempt can be traced by setting a trace level by either
      using <c>ei_set_tracelevel</c> or by setting environment
      variable <c>EI_TRACELEVEL</c>.
      The trace levels have the following messages:</p>

    <list>
        <item>1: Verbose error messages</item>
        <item>2: Above messages and verbose warning messages</item>
        <item>3: Above messages and progress reports for connection handling
        </item>
        <item>4: Above messages and progress reports for communication</item>
        <item>5: Above messages and progress reports for data conversion</item>
    </list>
  </section>

</cref>