-
Notifications
You must be signed in to change notification settings - Fork 4
/
etclface-code.w
2358 lines (1822 loc) · 64.5 KB
/
etclface-code.w
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
%% etclface-code.w
%% The actual code for the etclface cweb files.
%% Copyright (c) 2013-2024 Fred Youhanaie
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% * Redistributions of source code must retain the above copyright
%% notice, this list of conditions and the following disclaimer.
%%
%% * Redistributions in binary form must reproduce the above copyright
%% notice, this list of conditions and the following disclaimer
%% in the documentation and/or other materials provided with the
%% distribution.
%%
%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
%% A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
%% HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
%% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
%% TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
%% PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
%% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
%% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
%% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@*The Source Code.
The \etf commands are collected in a number of groups.
@c
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <tcl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <ei.h>
@<Command declarations@>;
@<Internal helper functions@>;
@<Initialisation commands@>;
@<Connection commands@>;
@<Send commands@>;
@<Receive commands@>;
@<Buffer commands@>;
@<Encode commands@>;
@<Decode commands@>;
@<Data handling commands@>;
@<Utility commands@>;
@<AppInit@>;
@ We follow the standard format for all Tcl extensions. \.{Etclface\_Init}
initialises the library and declares the commands. We require \.{Tcl}
version 8.5 or higher, This version has been around for some time now,
so we can expect it to be available at most sites.
@<AppInit@>=
#define TCLVERSION "8.5"
int
Etclface_Init(Tcl_Interp *ti)
{
#ifdef USE_TCL_STUBS
if (Tcl_InitStubs(ti, TCLVERSION, 0) == NULL) {
return TCL_ERROR;
}
#endif
if (Tcl_PkgRequire(ti, "Tcl", TCLVERSION, 0) == NULL) {
return TCL_ERROR;
}
if (Tcl_PkgProvide(ti, "etclface", "0.1") != TCL_OK) {
return TCL_ERROR;
}
EtclfaceCommand_t *etfcmd = EtclfaceCommand;
while (etfcmd->proc != NULL) {
Tcl_CreateObjCommand(ti, etfcmd->name, etfcmd->proc, NULL, NULL);
etfcmd++;
}
return TCL_OK;
}
@*1The Commands.
All the Tcl commands and their associated functions are defined in the
|EtclfaceCommand| array, which is then added to Tcl in the |Etclface_Init|
function.
@<Command declarations@>=
typedef struct EtclfaceCommand_s {
char *name;
Tcl_ObjCmdProc *proc;
} EtclfaceCommand_t;
@ We need to forward declare the functions first.
@<Command declarations@>=
static Tcl_ObjCmdProc Etclface_init, Etclface_xinit;
@#
static Tcl_ObjCmdProc Etclface_accept, Etclface_connect, Etclface_disconnect,
Etclface_listen, Etclface_make_chan, Etclface_publish,
Etclface_socket, Etclface_xconnect;
@#
static Tcl_ObjCmdProc Etclface_receive, Etclface_reg_send, Etclface_send;
@#
static Tcl_ObjCmdProc Etclface_decode_atom, Etclface_decode_boolean, Etclface_decode_char,
Etclface_decode_double, Etclface_decode_list, Etclface_decode_long,
Etclface_decode_pid, Etclface_decode_ref, Etclface_decode_string,
Etclface_decode_term, Etclface_decode_tuple, Etclface_decode_version;
@#
static Tcl_ObjCmdProc Etclface_encode_atom, Etclface_encode_boolean, Etclface_encode_char,
Etclface_encode_double, Etclface_encode_empty_list,
Etclface_encode_list_header, Etclface_encode_long, Etclface_encode_pid,
Etclface_encode_ref, Etclface_encode_string, Etclface_encode_tuple_header;
@#
static Tcl_ObjCmdProc Etclface_ref_free, Etclface_ref_new, Etclface_ref_print, Etclface_ref_show;
@#
static Tcl_ObjCmdProc Etclface_xb_free, Etclface_xb_new, Etclface_xb_print, Etclface_xb_reset,
Etclface_xb_show, Etclface_xb_skip;
@#
static Tcl_ObjCmdProc Etclface_ec_free, Etclface_ec_show;
@#
static Tcl_ObjCmdProc Etclface_nodename, Etclface_pid_show, Etclface_self, Etclface_tracelevel;
@ These are the command names and their associated functions, in
alphabetical order. The last element must be a \.{\{NULL, NULL\}}.
@<Command declarations@>=
static EtclfaceCommand_t EtclfaceCommand[] = {@/
{"etclface::accept", Etclface_accept},@/
{"etclface::connect", Etclface_connect},@/
{"etclface::decode_atom", Etclface_decode_atom},@/
{"etclface::decode_boolean", Etclface_decode_boolean},@/
{"etclface::decode_char", Etclface_decode_char},@/
{"etclface::decode_double", Etclface_decode_double},@/
{"etclface::decode_list", Etclface_decode_list},@/
{"etclface::decode_long", Etclface_decode_long},@/
{"etclface::decode_pid", Etclface_decode_pid},@/
{"etclface::decode_ref", Etclface_decode_ref},@/
{"etclface::decode_string", Etclface_decode_string},@/
{"etclface::decode_term", Etclface_decode_term},@/
{"etclface::decode_tuple", Etclface_decode_tuple},@/
{"etclface::decode_version", Etclface_decode_version},@/
{"etclface::disconnect", Etclface_disconnect},@/
{"etclface::ec_free", Etclface_ec_free},@/
{"etclface::ec_show", Etclface_ec_show},@/
{"etclface::encode_atom", Etclface_encode_atom},@/
{"etclface::encode_boolean", Etclface_encode_boolean},@/
{"etclface::encode_char", Etclface_encode_char},@/
{"etclface::encode_double", Etclface_encode_double},@/
{"etclface::encode_empty_list", Etclface_encode_empty_list},@/
{"etclface::encode_list_header", Etclface_encode_list_header},@/
{"etclface::encode_long", Etclface_encode_long},@/
{"etclface::encode_pid", Etclface_encode_pid},@/
{"etclface::encode_ref", Etclface_encode_ref},@/
{"etclface::encode_string", Etclface_encode_string},@/
{"etclface::encode_tuple_header", Etclface_encode_tuple_header},@/
{"etclface::init", Etclface_init},@/
{"etclface::listen", Etclface_listen},@/
{"etclface::make_chan", Etclface_make_chan},@/
{"etclface::nodename", Etclface_nodename},@/
{"etclface::pid_show", Etclface_pid_show},@/
{"etclface::publish", Etclface_publish},@/
{"etclface::receive", Etclface_receive},@/
{"etclface::ref_free", Etclface_ref_free},@/
{"etclface::ref_new", Etclface_ref_new},@/
{"etclface::ref_print", Etclface_ref_print},@/
{"etclface::ref_show", Etclface_ref_show},@/
{"etclface::reg_send", Etclface_reg_send},@/
{"etclface::self", Etclface_self},@/
{"etclface::send", Etclface_send},@/
{"etclface::socket", Etclface_socket},@/
{"etclface::tracelevel", Etclface_tracelevel},@/
{"etclface::xb_free", Etclface_xb_free},@/
{"etclface::xb_new", Etclface_xb_new},@/
{"etclface::xb_print", Etclface_xb_print},@/
{"etclface::xb_reset", Etclface_xb_reset},@/
{"etclface::xb_show", Etclface_xb_show},@/
{"etclface::xb_skip", Etclface_xb_skip},@/
{"etclface::xconnect", Etclface_xconnect},@/
{"etclface::xinit", Etclface_xinit},@/
@#
{NULL, NULL} /* marks the end of the list*/
};
@*1Initialisation Commands.
\erliface provides two functions for initialising
the local \.{cnode} data structures, \.{ei\_connect\_init()} and
\.{ei\_connect\_xinit()}. Although it is possible to use a single command
with two distinct calling sequences, at least for now, we will stay with
two separate commands.
If successful, both commands will return a stringified handle to the
\.{ec} structure in the form of a hexadecimal number prefixed with
\.{ec0x}, e.g. \.{ec0x88074f0}. The storage for the structure is allocated
dynamically, so it will need to be de-allocated when not needed.
If the \.{cookie} parameter is missing, it will be obtained from
\.{erlang.cookie} file in user's home directory.
The \.{ei\_connect\_init()} and \.{ei\_connect\_xinit()} functions in
\erliface expect a \.{creation} value to be passed from the caller. For
now, we are setting this to zero on all calls, however, in future we
may maintain an internal autoincremented counter for this. The reason
behind the decision is to keep the extension simple and stateless.
@ \.{etclface::init nodename ?cookie?}.
Initialize and return a handle to an \.{ec} structure, with own name
\.{nodename} and \.{cookie}.
@<Initialisation commands@>=
static int
Etclface_init(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
char *nodename, *cookie;
ei_cnode *ec;
if ((objc!=2) && (objc!=3)) {
Tcl_WrongNumArgs(ti, 1, objv, "nodename ?cookie?");
return TCL_ERROR;
}
nodename = Tcl_GetString(objv[1]);
cookie = (objc == 2) ? NULL : Tcl_GetString(objv[2]);
ec = (ei_cnode *)Tcl_AttemptAlloc(sizeof(ei_cnode));
if (ec == NULL) {
ErrorReturn(ti, "ERROR", "Could not allocate memory for ei_cnode", 0);
return TCL_ERROR;
}
if (ei_connect_init(ec, nodename, cookie, 0) < 0) {
ErrorReturn(ti, "ERROR", "ei_connect_init failed", erl_errno);
return TCL_ERROR;
}
Tcl_SetObjResult(ti, Tcl_ObjPrintf("ec0x%lx", (long unsigned int)ec));
return TCL_OK;
}
@ \.{etclface::xinit host alive node ipaddr ?cookie?}.
Initialize and return a handle to an \.{ec} structure, with own name
\.{nodename} and \.{cookie}.
@<Initialisation commands@>=
static int
Etclface_xinit(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
char *alive, *cookie, *host, *node;
Erl_IpAddr ipaddr;
ei_cnode *ec;
if ((objc!=5) && (objc!=6)) {
Tcl_WrongNumArgs(ti, 1, objv, "host alive node ipaddr ?cookie?");
return TCL_ERROR;
}
host = Tcl_GetString(objv[1]);
alive = Tcl_GetString(objv[2]);
node = Tcl_GetString(objv[3]);
if (get_ipaddr(ti, objv[4], &ipaddr) == TCL_ERROR)
return TCL_ERROR;
cookie = (objc == 5) ? NULL : Tcl_GetString(objv[5]);
ec = (ei_cnode *)Tcl_AttemptAlloc(sizeof(ei_cnode));
if (ec == NULL) {
ErrorReturn(ti, "ERROR", "Could not allocate memory for ei_cnode", 0);
return TCL_ERROR;
}
int res = ei_connect_xinit(ec, host, alive, node, ipaddr, cookie, (short)0);
Tcl_Free((char *)ipaddr);
if (res < 0) {
ErrorReturn(ti, "ERROR", "ei_connect_xinit failed", erl_errno);
return TCL_ERROR;
}
Tcl_SetObjResult(ti, Tcl_ObjPrintf("ec0x%lx", (long unsigned int)ec));
return TCL_OK;
}
@*1Connection Commands.
\erliface provides four functions for establishing a connection
to another node. Two, \.{ei\_connect()} and \.{ei\_connect\_tmo()},
expect a single remote nodename in the form of \.{alivename@@hostname},
while the other two, \.{ei\_xconnect()} and \.{ei\_xconnect\_tmo()},
expect an IP address and an alivename. Within each pair, one function
accepts a \.{timeout} value in milliseconds, while the other will wait
indefinitely for a connection. Using $0$ for the timeout value is the
same as having no timeout.
Here we provide just two commands, \.{connect} and \.{xconnect}. Both
can be called with an optional timeout value.
If successful, both commands will return the socket file descriptor
\.{fd}, which should be used for subsequent calls to various send/receive
commands.
@ \.{etclface::connect ec nodename ?timeout?}.
Establish a connection to node \.{nodename} using the \.{ec} handle
obtained from \.{etclface::init} or \.{xinit}.
@<Connection commands@>=
static int
Etclface_connect(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
ei_cnode *ec;
char *nodename;
unsigned timeout;
if ((objc!=3) && (objc!=4)) {
Tcl_WrongNumArgs(ti, 1, objv, "ec nodename ?timeout?");
return TCL_ERROR;
}
if (get_ec(ti, objv[1], &ec) == TCL_ERROR)
return TCL_ERROR;
nodename = Tcl_GetString(objv[2]);
if (objc == 3) {
timeout = 0;
} @+else {
if (get_timeout(ti, objv[3], &timeout) == TCL_ERROR)
return TCL_ERROR;
}
int fd;
if ((fd = ei_connect_tmo(ec, nodename, timeout)) < 0) {
ErrorReturn(ti, "ERROR", "ei_connect_tmo failed", erl_errno);
return TCL_ERROR;
}
Tcl_SetObjResult(ti, Tcl_NewIntObj(fd));
return TCL_OK;
}
@ \.{etclface::xconnect ec ipaddr alivename ?timeout?}.
Establish a connection to node \.{alivename@@ipaddr} using the \.{ec}
handle obtained from \.{etclface::init} or \.{xinit}.
@<Connection commands@>=
static int
Etclface_xconnect(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
char *alivename;
ei_cnode *ec;
Erl_IpAddr ipaddr;
unsigned timeout;
int fd;
if ((objc!=4) && (objc!=5)) {
Tcl_WrongNumArgs(ti, 1, objv, "ec ipaddr alivename ?timeout?");
return TCL_ERROR;
}
if (get_ec(ti, objv[1], &ec) == TCL_ERROR)
return TCL_ERROR;
if (get_ipaddr(ti, objv[2], &ipaddr) == TCL_ERROR)
return TCL_ERROR;
alivename = Tcl_GetString(objv[3]);
if (objc == 4) {
timeout = 0;
} @+else {
if (get_timeout(ti, objv[4], &timeout) == TCL_ERROR)
return TCL_ERROR;
}
if ((fd = ei_xconnect_tmo(ec, ipaddr, alivename, timeout)) < 0) {
ErrorReturn(ti, "ERROR", "ei_xconnect_tmo failed", erl_errno);
return TCL_ERROR;
}
Tcl_SetObjResult(ti, Tcl_NewIntObj(fd));
return TCL_OK;
}
@ \.{etclface::disconnect fd}.
Closes the socket connection with \.{fd} file descriptor.
@<Connection commands@>=
static int
Etclface_disconnect(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
int fd;
if (objc != 2) {
Tcl_WrongNumArgs(ti, 1, objv, "fd");
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(ti, objv[1], &fd) == TCL_ERROR)
return TCL_ERROR;
if (close(fd) < 0) {
ErrorReturn(ti, "ERROR", "close failed", errno);
return TCL_ERROR;
}
return TCL_OK;
}
@ \.{etclface::socket addr port}.
Create socket to listen on for connections from other nodes. The command
will also bind to the socket ready to be listened on. If successful,
the file descriptor for the socket will be returned.
The command will not do any name translations for \.{addr} or \.{port}. It
is left to the caller to do any translation needed, although, a named
version of the command can be provided as part of the higher level \etf
Tcl library.
@<Connection commands@>=
static int
Etclface_socket(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
char *host;
int fd, port;
struct sockaddr_in sinaddr;
if (objc != 3) {
Tcl_WrongNumArgs(ti, 1, objv, "host port");
return TCL_ERROR;
}
host = Tcl_GetString(objv[1]);
if (Tcl_GetIntFromObj(ti, objv[2], &port) == TCL_ERROR) {
return TCL_ERROR;
}
if ((port<0) || port > USHRT_MAX) {
ErrorReturn(ti, "ERROR", "Port number value is too high", 0);
return TCL_ERROR;
}
memset(&sinaddr, 0, sizeof(struct sockaddr_in));
sinaddr.sin_family = AF_INET;
if (strcmp(host, "-")) {
if (inet_aton(host, &sinaddr.sin_addr) == 0) {
ErrorReturn(ti, "ERROR", "Invalid address", 0);
return TCL_ERROR;
}
} @+else {
sinaddr.sin_addr.s_addr = INADDR_ANY;
}
sinaddr.sin_port = htons(port);
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
ErrorReturn(ti, "ERROR", "failed to get socket", errno);
return TCL_ERROR;
}
if (bind(fd, (struct sockaddr *)&sinaddr, sizeof(struct sockaddr)) < 0) {
close(fd);
ErrorReturn(ti, "ERROR", "failed to bind to socket", errno);
return TCL_ERROR;
}
Tcl_SetObjResult(ti, Tcl_NewIntObj(fd));
return TCL_OK;
}
@ \.{etclface::make\_chan fd flag}.
Given an already open file descriptor, \.{fd}, create a corresponding
Tcl channel. If successful, the channel name is returned. The channel
name can be used to create Tcl event handlers, such as when a new message
has arrived on the open \.{fd}, however, in order to receive the message
the caller should use the corresponding \.{fd} with \.{etclface::receive}.
It is up to the caller to keep track of the channel/\.{fd} mappings.
The \.{flag} should be one of \.{R}, \.{W} or \.{RW}, indicating whether
the \.{fd} is readable, writable or both, respectively.
@<Connection commands@>=
static int
Etclface_make_chan(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
long int fd;
int flag;
Tcl_Channel chan;
char *flagstr;
if (objc != 3) {
Tcl_WrongNumArgs(ti, 1, objv, "fd flag");
return TCL_ERROR;
}
if (Tcl_GetLongFromObj(ti, objv[1], &fd) == TCL_ERROR)
return TCL_ERROR;
if (fcntl(fd, F_GETFD) < 0) {
ErrorReturn(ti, "ERROR", "file descriptor is not open", errno);
return TCL_ERROR;
}
flagstr = Tcl_GetString(objv[2]);
if (!strcmp(flagstr, "R")) {
flag = TCL_READABLE;
} @+else if (!strcmp(flagstr, "W")) {
flag = TCL_WRITABLE;
} @+else if (!strcmp(flagstr, "RW")) {
flag = TCL_READABLE | TCL_WRITABLE;
} @+else {
ErrorReturn(ti, "ERROR", "Invalid flag, should be R, W or RW", 0);
return TCL_ERROR;
}
chan = Tcl_MakeFileChannel((ClientData)fd, flag);
if (chan == NULL) {
ErrorReturn(ti, "ERROR", "Tcl_MakeFileChannel failed", 0);
return TCL_ERROR;
}
Tcl_RegisterChannel(ti, chan);
Tcl_SetObjResult(ti, Tcl_NewStringObj(Tcl_GetChannelName(chan), -1));
return TCL_OK;
}
@ \.{etclface::listen fd backlog}.
Listen on a socket that has been set up with \.{etclface::socket}.
@<Connection commands@>=
static int
Etclface_listen(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
int fd, backlog;
if (objc != 3) {
Tcl_WrongNumArgs(ti, 1, objv, "fd backlog");
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(ti, objv[1], &fd) == TCL_ERROR)
return TCL_ERROR;
if (Tcl_GetIntFromObj(ti, objv[2], &backlog) == TCL_ERROR)
return TCL_ERROR;
if (listen(fd, backlog) < 0) {
ErrorReturn(ti, "ERROR", "failed to listen on socket", errno);
return TCL_ERROR;
}
return TCL_OK;
}
@ \.{etclface::publish ec port ?timeout?}.
Register a port with epmd by calling \.{ei\_publish()}.
@<Connection commands@>=
static int
Etclface_publish(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
ei_cnode *ec;
int port, timeout, fd;
if ((objc<3) || (objc>4)) {
Tcl_WrongNumArgs(ti, 1, objv, "ec port ?timeout?");
return TCL_ERROR;
}
if (get_ec(ti, objv[1], &ec) == TCL_ERROR)
return TCL_ERROR;
if (Tcl_GetIntFromObj(ti, objv[2], &port) == TCL_ERROR)
return TCL_ERROR;
if (objc == 3) {
timeout = 0;
} @+else {
if (get_timeout(ti, objv[3], &timeout) == TCL_ERROR)
return TCL_ERROR;
}
if ((fd=ei_publish_tmo(ec, port, timeout)) < 0) {
ErrorReturn(ti, "ERROR", "ei_publish failed", erl_errno);
return TCL_ERROR;
}
Tcl_SetObjResult(ti, Tcl_NewIntObj(fd));
return TCL_OK;
}
@ \.{etclface::accept ec fd ?timeout?}.
Wait for and accept a connection from another erlang node. if successful,
i.e. no error or timeout, then the contents of the \.{ErlConnect}
structure is returned in the form of a dictionary.
@<Connection commands@>=
static int
Etclface_accept(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
ei_cnode *ec;
int fd, timeout, newfd;
ErlConnect econn;
struct in_addr addr;
if ((objc<3) || (objc>4)) {
Tcl_WrongNumArgs(ti, 1, objv, "ec fd ?timeout?");
return TCL_ERROR;
}
if (get_ec(ti, objv[1], &ec) == TCL_ERROR)
return TCL_ERROR;
if (Tcl_GetIntFromObj(ti, objv[2], &fd) == TCL_ERROR)
return TCL_ERROR;
if (objc == 3) {
timeout = 0;
} @+else {
if (get_timeout(ti, objv[3], &timeout) == TCL_ERROR)
return TCL_ERROR;
}
if ((newfd = ei_accept_tmo(ec, fd, &econn, timeout)) < 0) {
ErrorReturn(ti, "ERROR", "ei_accept failed", erl_errno);
return TCL_ERROR;
}
Tcl_Obj *dict = Tcl_NewDictObj();
Tcl_DictObjPut(ti, dict, Tcl_NewStringObj("fd", -1), Tcl_NewIntObj(newfd));
Tcl_DictObjPut(ti, dict, Tcl_NewStringObj("nodename", -1), Tcl_NewStringObj(econn.nodename,-1));
memcpy(&addr, econn.ipadr, sizeof(addr));
Tcl_DictObjPut(ti, dict, Tcl_NewStringObj("nodeaddr", -1), Tcl_NewStringObj(inet_ntoa(addr), -1));
Tcl_SetObjResult(ti, dict);
return TCL_OK;
}
@*1Send Commands.
@ \.{etclface::reg\_send ec fd server xb ?timeout?}.
Send a message consisting of an Erlang term stored in \.{xb} to a
registered process \.{server}, using the \.{ec} handle obtained from
\.{etclface::init} or \.{etclface::xinit}, and \.{fd} obtained from
\.{etclface::connect}.
@<Send commands@>=
static int
Etclface_reg_send(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
ei_cnode *ec;
int fd;
char *serverport;
unsigned int timeout;
if ((objc < 5) || (objc>6)) {
Tcl_WrongNumArgs(ti, 1, objv, "ec fd server xb ?timeout?");
return TCL_ERROR;
}
if (get_ec(ti, objv[1], &ec) == TCL_ERROR)
return TCL_ERROR;
if (Tcl_GetIntFromObj(ti, objv[2], &fd) == TCL_ERROR)
return TCL_ERROR;
serverport = Tcl_GetString(objv[3]);
ei_x_buff *xb;
if (get_xb(ti, objv[4], &xb) == TCL_ERROR)
return TCL_ERROR;
if (objc = 5) {
timeout = 0U;
} @+else {
if (get_timeout(ti, objv[5], &timeout) < 0)
return TCL_ERROR;
}
if (ei_reg_send_tmo(ec, fd, serverport, xb->buff, xb->index, timeout) < 0) {
ErrorReturn(ti, "ERROR", "ei_reg_send_tmo failed", erl_errno);
return TCL_ERROR;
}
return TCL_OK;
}
@ \.{etclface::send fd pid xb ?timeout?}.
Send a message consisting of an Erlang term stored in \.{xb} to a process
identified by \.{pid}, and \.{fd} obtained from \.{etclface::connect},
or \.{etclface::xconnect}.
@<Send commands@>=
static int
Etclface_send(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
int fd;
erlang_pid *pid;
unsigned int timeout;
ei_x_buff *xb;
if ((objc < 4) || (objc>5)) {
Tcl_WrongNumArgs(ti, 1, objv, "fd pid xb ?timeout?");
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(ti, objv[1], &fd) == TCL_ERROR)
return TCL_ERROR;
if (get_pid(ti, objv[2], &pid) == TCL_ERROR)
return TCL_ERROR;
if (get_xb(ti, objv[3], &xb) == TCL_ERROR)
return TCL_ERROR;
if (objc = 4) {
timeout = 0U;
} @+else {
if (get_timeout(ti, objv[4], &timeout) < 0)
return TCL_ERROR;
}
if (ei_send_tmo(fd, pid, xb->buff, xb->index, timeout) < 0) {
ErrorReturn(ti, "ERROR", "ei_send_tmo failed", erl_errno);
return TCL_ERROR;
}
return TCL_OK;
}
@*1Receive Commands.
\erliface provides many receive functions, however, here we only provide
a single command to receive a message.
The command expects the file descriptor (\.{fd}) of an existing connection
on the command line, and an optional timeout value, which will default
to an indefinite wait.
Once a message is received successfully, the command will return the
type of message received, along with the message, if relevant.
@ \.{etclface::receive fd xb ?timeout?}.
Wait for a message. If received succeefully, put the message into an
xbuff identified by \.{xb}.
@<Receive commands@>=
static int
Etclface_receive(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
int fd, timeout;
erlang_msg msg;
ei_x_buff *xb;
if ((objc!=3) && (objc!=4)) {
Tcl_WrongNumArgs(ti, 1, objv, "fd xb ?timeout?");
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(ti, objv[1], &fd) == TCL_ERROR)
return TCL_ERROR;
if (get_xb(ti, objv[2], &xb) == TCL_ERROR)
return TCL_ERROR;
if (objc == 3) {
timeout = 0;
} @+else {
if (get_timeout(ti, objv[3], &timeout) == TCL_ERROR)
return TCL_ERROR;
}
@<Receive message@>;
@<Unpack received message@>;
return TCL_OK;
}
@ Wait for a message. If we get a tick (keep alive message) from another
node, we return \.{TICK} to the caller. Timeouts and errors are returned
as error, although, in future a timeout may be treated as a normal return.
@<Receive message@>=
int res;
res = ei_xreceive_msg_tmo(fd, &msg, xb, timeout);
if (res == ERL_TICK) {
Tcl_SetObjResult(ti, Tcl_NewStringObj("TICK", -1));
return TCL_OK;
}
if (res == ERL_TIMEOUT) {
ErrorReturn(ti, "TIMEOUT", "ei_xreceive_msg_tmo timed out", 0);
return TCL_ERROR;
}
if (res != ERL_MSG) {
ErrorReturn(ti, "ERROR", "ei_xreceive_msg_tmo failed", erl_errno);
return TCL_ERROR;
}
@ Check the received message. Unpack the message meta data and add to
the Tcl result as a dictionary.
@<Unpack received message@>=
Tcl_Obj *msgdict = Tcl_NewDictObj();
Tcl_DictObjPut(ti, msgdict, Tcl_NewStringObj("msgtype", -1), Tcl_NewLongObj(msg.msgtype));
switch (msg.msgtype) {
case ERL_SEND:
Tcl_DictObjPut(ti, msgdict, Tcl_NewStringObj("to", -1), pid2dict(ti, &msg.to));
break;
case ERL_REG_SEND:
Tcl_DictObjPut(ti, msgdict, Tcl_NewStringObj("from", -1), pid2dict(ti, &msg.from));
break;
case ERL_LINK:
case ERL_UNLINK:
case ERL_EXIT:
Tcl_DictObjPut(ti, msgdict, Tcl_NewStringObj("to", -1), pid2dict(ti, &msg.to));
Tcl_DictObjPut(ti, msgdict, Tcl_NewStringObj("from", -1), pid2dict(ti, &msg.from));
break;
}
Tcl_SetObjResult(ti, msgdict);
@*1Buffer Commands.
Erlang has several data types, while Tcl treats everything as character
strings, although, for efficiency, Tcl can internally maintain numeric
data as numbers. The encode commands will convert from Tcl data types
to Erlang types ready for transmission to other Erlang nodes.
\erliface provides two groups of encode functions, and within
each group there is one function for each Erlang data type. For now,
at least, a limited useful subset of these functions will be exposed as
Tcl commands. Of the two groups, only those with the \.{ei\_x\_} prefix
are implemented, and of these we shall start with a limited main group.
The \.{ei\_x\_encode\_*} functions encode the data into the
\.{ei\_x\_buff} data structure.
@ \.{etclface::xb\_new ?-withversion?}.
Creates a new \.{ei\_x\_buff} structure and initialises the buffer
using \.{ei\_x\_new()}, or optionally with an initial version byte using
\.{ei\_x\_new\_with\_version()}.
@<Buffer commands@>=
static int
Etclface_xb_new(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
ei_x_buff *xb;
if ((objc!=1) && (objc!=2)) {
Tcl_WrongNumArgs(ti, 1, objv, "?-withversion?");
return TCL_ERROR;
}
if ((objc==2) && strcmp(Tcl_GetString(objv[1]), "-withversion")) {
ErrorReturn(ti, "ERROR", "Only -withversion allowed as argument", 0);
return TCL_ERROR;
}
xb = (ei_x_buff *)Tcl_AttemptAlloc(sizeof(ei_x_buff));
if (xb == NULL) {
ErrorReturn(ti, "ERROR", "Could not allocate memory for ei_x_buff", 0);
return TCL_ERROR;
}
int res = (objc == 1) ? ei_x_new(xb) : ei_x_new_with_version(xb);
if (res < 0) {
Tcl_Free((char *)xb);
ErrorReturn(ti, "ERROR", "ei_x_new/ei_x_new_with_version failed", erl_errno);
return TCL_ERROR;
}
Tcl_SetObjResult(ti, Tcl_ObjPrintf("xb0x%lx", (long unsigned int)xb));
return TCL_OK;
}
@ \.{etclface::xb\_free xb}.
Free up memory taken up by \.{xb} as well as the internal buffer allocated
to \.{xb}.
@<Buffer commands@>=
static int
Etclface_xb_free(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
ei_x_buff *xb;
if (objc!=2) {
Tcl_WrongNumArgs(ti, 1, objv, "xb");
return TCL_ERROR;
}
if (get_xb(ti, objv[1], &xb) == TCL_ERROR)
return TCL_ERROR;
if (ei_x_free(xb) < 0) {
ErrorReturn(ti, "ERROR", "ei_x_free failed", erl_errno);
return TCL_ERROR;
}
Tcl_Free((char *)xb);
return TCL_OK;
}
@ \.{etclface::xb\_show xb}.
Show the contents of the \.{xb} structure. This is mainly for debugging,
or for those who are curious about the workings of the encode/decode
commands.
The output will be in the form of \.{buff p buffsz d index d}, where \.{p}
is a pointer and the \.{d}s are integers. This lends itself to being parsed as an array, e.g.
\.{array set [etclface::xb\_show \$xb]}
@<Buffer commands@>=
static int
Etclface_xb_show(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
ei_x_buff *xb;
if (objc!=2) {
Tcl_WrongNumArgs(ti, 1, objv, "xb");
return TCL_ERROR;
}
if (get_xb(ti, objv[1], &xb) == TCL_ERROR)
return TCL_ERROR;
Tcl_Obj *xbdict = Tcl_NewDictObj();
Tcl_DictObjPut(ti, xbdict, Tcl_NewStringObj("buff", -1), Tcl_ObjPrintf("0x%0lx", (long unsigned int)xb->buff));
Tcl_DictObjPut(ti, xbdict, Tcl_NewStringObj("buffsz", -1), Tcl_NewIntObj(xb->buffsz));
Tcl_DictObjPut(ti, xbdict, Tcl_NewStringObj("index", -1), Tcl_NewIntObj(xb->index));
Tcl_SetObjResult(ti, xbdict);
return TCL_OK;
}
@ \.{etclface::xb\_print xb}.
Print the term in \.{xb} in human readable form. If successful, the
contents of the term will be returned in the form familiar to the Erlang
folk, but it is not readily parsable in Tcl.
We normally use \.{Tcl\_AttemptAlloc} to allocate dynamic memory, however,
in here we use malloc and free instead, since \.{ei\_s\_print\_term()}
may use \.{realloc} if needed.
@<Buffer commands@>=
static int
Etclface_xb_print(ClientData cd, Tcl_Interp *ti, int objc, Tcl_Obj *const objv[])
{
ei_x_buff *xb;
char *buff = NULL;
if (objc!=2) {
Tcl_WrongNumArgs(ti, 1, objv, "xb");
return TCL_ERROR;
}
if (get_xb(ti, objv[1], &xb) == TCL_ERROR)