-
Notifications
You must be signed in to change notification settings - Fork 6
/
binding.c
2051 lines (1538 loc) · 60.9 KB
/
binding.c
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
#include <node_api.h>
#include <stdlib.h>
#include <string.h>
#include <udx.h>
#include <uv.h>
#define UDX_NAPI_INTERACTIVE 0
#define UDX_NAPI_NON_INTERACTIVE 1
#define UDX_NAPI_FRAMED 2
typedef struct {
udx_t udx;
char *read_buf;
size_t read_buf_free;
napi_async_cleanup_hook_handle teardown;
bool exiting;
bool has_teardown;
} udx_napi_t;
typedef struct {
udx_socket_t socket;
udx_napi_t *udx;
napi_env env;
napi_ref ctx;
napi_ref on_send;
napi_ref on_message;
napi_ref on_close;
napi_ref realloc_message;
} udx_napi_socket_t;
typedef struct {
udx_stream_t stream;
udx_napi_t *udx;
int mode;
char *read_buf;
char *read_buf_head;
size_t read_buf_free;
ssize_t frame_len;
napi_env env;
napi_ref ctx;
napi_ref on_data;
napi_ref on_end;
napi_ref on_drain;
napi_ref on_ack;
napi_ref on_send;
napi_ref on_message;
napi_ref on_close;
napi_ref on_firewall;
napi_ref on_remote_changed;
napi_ref realloc_data;
napi_ref realloc_message;
} udx_napi_stream_t;
typedef struct {
udx_lookup_t handle;
udx_napi_t *udx;
char *host;
napi_env env;
napi_ref ctx;
napi_ref on_lookup;
} udx_napi_lookup_t;
typedef struct {
udx_interface_event_t handle;
udx_napi_t *udx;
napi_env env;
napi_ref ctx;
napi_ref on_event;
napi_ref on_close;
} udx_napi_interface_event_t;
inline static void
parse_address (struct sockaddr *name, char *ip, size_t size, int *port, int *family) {
if (name->sa_family == AF_INET) {
*port = ntohs(((struct sockaddr_in *) name)->sin_port);
*family = 4;
uv_ip4_name((struct sockaddr_in *) name, ip, size);
} else if (name->sa_family == AF_INET6) {
*port = ntohs(((struct sockaddr_in6 *) name)->sin6_port);
*family = 6;
uv_ip6_name((struct sockaddr_in6 *) name, ip, size);
}
}
static void
on_udx_send (udx_socket_send_t *req, int status) {
udx_napi_socket_t *n = (udx_napi_socket_t *) req->socket;
if (n->udx->exiting) return;
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_send, &callback);
napi_value argv[2];
napi_create_int32(env, (uintptr_t) req->data, &(argv[0]));
napi_create_int32(env, status, &(argv[1]));
if (napi_make_callback(env, NULL, ctx, callback, 2, argv, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_message (udx_socket_t *self, ssize_t read_len, const uv_buf_t *buf, const struct sockaddr *from) {
udx_napi_socket_t *n = (udx_napi_socket_t *) self;
if (n->udx->exiting) return;
int port = 0;
char ip[INET6_ADDRSTRLEN];
int family = 0;
parse_address((struct sockaddr *) from, ip, INET6_ADDRSTRLEN, &port, &family);
if (buf->len > n->udx->read_buf_free) return;
memcpy(n->udx->read_buf, buf->base, buf->len);
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_message, &callback);
napi_value argv[4];
napi_create_uint32(env, read_len, &(argv[0]));
napi_create_uint32(env, port, &(argv[1]));
napi_create_string_utf8(env, ip, NAPI_AUTO_LENGTH, &(argv[2]));
napi_create_uint32(env, family, &(argv[3]));
napi_value res;
if (napi_make_callback(env, NULL, ctx, callback, 4, argv, &res) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
// avoid reentry
if (!(n->udx->exiting)) {
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->realloc_message, &callback);
if (napi_make_callback(env, NULL, ctx, callback, 0, NULL, &res) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_get_buffer_info(env, res, (void **) &(n->udx->read_buf), &(n->udx->read_buf_free));
napi_close_handle_scope(env, scope);
}
} else {
napi_get_buffer_info(env, res, (void **) &(n->udx->read_buf), &(n->udx->read_buf_free));
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_close (udx_socket_t *self) {
udx_napi_socket_t *n = (udx_napi_socket_t *) self;
napi_env env = n->env;
if (!(n->udx->exiting)) {
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_close, &callback);
if (napi_make_callback(env, NULL, ctx, callback, 0, NULL, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
napi_delete_reference(env, n->on_send);
napi_delete_reference(env, n->on_message);
napi_delete_reference(env, n->on_close);
napi_delete_reference(env, n->realloc_message);
napi_delete_reference(env, n->ctx);
}
static void
on_udx_teardown (napi_async_cleanup_hook_handle handle, void *data) {
udx_napi_t *self = (udx_napi_t *) data;
udx_t *udx = (udx_t *) data;
self->exiting = true;
udx_teardown(udx);
}
static void
ensure_teardown (napi_env env, udx_napi_t *udx) {
if (udx->has_teardown) return;
udx->has_teardown = true;
napi_add_async_cleanup_hook(env, on_udx_teardown, (void *) udx, &(udx->teardown));
}
static void
on_udx_stream_end (udx_stream_t *stream) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
if (n->udx->exiting) return;
size_t read = n->read_buf_head - n->read_buf;
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_end, &callback);
napi_value argv[1];
napi_create_uint32(env, read, &(argv[0]));
if (napi_make_callback(env, NULL, ctx, callback, 1, argv, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_stream_read (udx_stream_t *stream, ssize_t read_len, const uv_buf_t *buf) {
if (read_len == UV_EOF) return on_udx_stream_end(stream);
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
if (n->udx->exiting) return;
// ignore the message if it doesn't fit in the read buffer
if (buf->len > n->read_buf_free) return;
if (n->mode == UDX_NAPI_FRAMED && n->frame_len == -1) {
if (buf->len < 3) {
n->mode = UDX_NAPI_INTERACTIVE;
} else {
uint8_t *b = (uint8_t *) buf->base;
n->frame_len = 3 + (b[0] | (b[1] << 8) | (b[2] << 16));
}
}
memcpy(n->read_buf_head, buf->base, buf->len);
n->read_buf_head += buf->len;
n->read_buf_free -= buf->len;
if (n->mode == UDX_NAPI_NON_INTERACTIVE && n->read_buf_free >= 2 * stream->mtu) {
return;
}
ssize_t read = n->read_buf_head - n->read_buf;
if (n->mode == UDX_NAPI_FRAMED) {
if (n->frame_len < read) {
n->mode = UDX_NAPI_INTERACTIVE;
} else if (n->frame_len == read) {
n->frame_len = -1;
} else if (n->read_buf_free < 2 * stream->mtu) {
n->frame_len -= read;
} else {
return; // wait for more data
}
}
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_data, &callback);
napi_value argv[1];
napi_create_uint32(env, read, &(argv[0]));
napi_value res;
if (napi_make_callback(env, NULL, ctx, callback, 1, argv, &res) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
// avoid re-entry
if (!(n->udx->exiting)) {
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->realloc_data, &callback);
if (napi_make_callback(env, NULL, ctx, callback, 0, NULL, &res) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_get_buffer_info(env, res, (void **) &(n->read_buf), &(n->read_buf_free));
n->read_buf_head = n->read_buf;
napi_close_handle_scope(env, scope);
}
} else {
napi_get_buffer_info(env, res, (void **) &(n->read_buf), &(n->read_buf_free));
n->read_buf_head = n->read_buf;
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_stream_drain (udx_stream_t *stream) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
if (n->udx->exiting) return;
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_drain, &callback);
if (napi_make_callback(env, NULL, ctx, callback, 0, NULL, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_stream_ack (udx_stream_write_t *req, int status, int unordered) {
udx_napi_stream_t *n = (udx_napi_stream_t *) req->stream;
if (n->udx->exiting) return;
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_ack, &callback);
napi_value argv[1];
napi_create_uint32(env, (uintptr_t) req->data, &(argv[0]));
if (napi_make_callback(env, NULL, ctx, callback, 1, argv, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_stream_send (udx_stream_send_t *req, int status) {
udx_napi_stream_t *n = (udx_napi_stream_t *) req->stream;
if (n->udx->exiting) return;
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_send, &callback);
napi_value argv[2];
napi_create_int32(env, (uintptr_t) req->data, &(argv[0]));
napi_create_int32(env, status, &(argv[1]));
if (napi_make_callback(env, NULL, ctx, callback, 2, argv, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_stream_recv (udx_stream_t *stream, ssize_t read_len, const uv_buf_t *buf) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
if (n->udx->exiting) return;
if (buf->len > n->udx->read_buf_free) return;
memcpy(n->udx->read_buf, buf->base, buf->len);
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_message, &callback);
napi_value argv[1];
napi_create_uint32(env, read_len, &(argv[0]));
napi_value res;
if (napi_make_callback(env, NULL, ctx, callback, 1, argv, &res) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
// avoid re-entry
if (!(n->udx->exiting)) {
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->realloc_message, &callback);
if (napi_make_callback(env, NULL, ctx, callback, 0, NULL, &res) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_get_buffer_info(env, res, (void **) &(n->udx->read_buf), &(n->udx->read_buf_free));
napi_close_handle_scope(env, scope);
}
} else {
napi_get_buffer_info(env, res, (void **) &(n->udx->read_buf), &(n->udx->read_buf_free));
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_stream_finalize (udx_stream_t *stream) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
napi_delete_reference(n->env, n->on_data);
napi_delete_reference(n->env, n->on_end);
napi_delete_reference(n->env, n->on_drain);
napi_delete_reference(n->env, n->on_ack);
napi_delete_reference(n->env, n->on_send);
napi_delete_reference(n->env, n->on_message);
napi_delete_reference(n->env, n->on_close);
napi_delete_reference(n->env, n->on_firewall);
napi_delete_reference(n->env, n->on_remote_changed);
napi_delete_reference(n->env, n->realloc_data);
napi_delete_reference(n->env, n->realloc_message);
napi_delete_reference(n->env, n->ctx);
}
static void
on_udx_stream_close (udx_stream_t *stream, int status) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
if (n->udx->exiting) return;
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_close, &callback);
napi_value argv[1];
if (status >= 0) {
napi_get_null(env, &(argv[0]));
} else {
napi_value code;
napi_value msg;
napi_create_string_utf8(env, uv_err_name(status), NAPI_AUTO_LENGTH, &code);
napi_create_string_utf8(env, uv_strerror(status), NAPI_AUTO_LENGTH, &msg);
napi_create_error(env, code, msg, &(argv[0]));
}
if (napi_make_callback(env, NULL, ctx, callback, 1, argv, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
static int
on_udx_stream_firewall (udx_stream_t *stream, udx_socket_t *socket, const struct sockaddr *from) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
udx_napi_socket_t *s = (udx_napi_socket_t *) socket;
uint32_t fw = 1; // assume error means firewall it, whilst reporting the uncaught
if (n->udx->exiting) return fw;
int port = 0;
char ip[INET6_ADDRSTRLEN];
int family = 0;
parse_address((struct sockaddr *) from, ip, INET6_ADDRSTRLEN, &port, &family);
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_firewall, &callback);
napi_value res;
napi_value argv[4];
napi_get_reference_value(env, s->ctx, &(argv[0]));
napi_create_uint32(env, port, &(argv[1]));
napi_create_string_utf8(env, ip, NAPI_AUTO_LENGTH, &(argv[2]));
napi_create_uint32(env, family, &(argv[3]));
if (napi_make_callback(env, NULL, ctx, callback, 4, argv, &res) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
} else {
napi_get_value_uint32(env, res, &fw);
}
napi_close_handle_scope(env, scope);
return fw;
}
static void
on_udx_stream_remote_changed (udx_stream_t *stream) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
if (n->udx->exiting) return;
napi_env env = n->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_remote_changed, &callback);
if (napi_make_callback(env, NULL, ctx, callback, 0, NULL, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_lookup (udx_lookup_t *lookup, int status, const struct sockaddr *addr, int addr_len) {
udx_napi_lookup_t *n = (udx_napi_lookup_t *) lookup;
if (n->udx->exiting) return;
napi_env env = n->env;
char ip[INET6_ADDRSTRLEN] = "";
int family = 0;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, n->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, n->on_lookup, &callback);
if (status >= 0) {
if (addr->sa_family == AF_INET) {
uv_ip4_name((struct sockaddr_in *) addr, ip, addr_len);
family = 4;
} else if (addr->sa_family == AF_INET6) {
uv_ip6_name((struct sockaddr_in6 *) addr, ip, addr_len);
family = 6;
}
napi_value argv[3];
napi_get_null(env, &(argv[0]));
napi_create_string_utf8(env, ip, NAPI_AUTO_LENGTH, &(argv[1]));
napi_create_uint32(env, family, &(argv[2]));
if (napi_make_callback(env, NULL, ctx, callback, 3, argv, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
} else {
napi_value argv[1];
napi_value code;
napi_value msg;
napi_create_string_utf8(env, uv_err_name(status), NAPI_AUTO_LENGTH, &code);
napi_create_string_utf8(env, uv_strerror(status), NAPI_AUTO_LENGTH, &msg);
napi_create_error(env, code, msg, &(argv[0]));
if (napi_make_callback(env, NULL, ctx, callback, 1, argv, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
}
free(n->host);
napi_close_handle_scope(env, scope);
napi_delete_reference(n->env, n->on_lookup);
napi_delete_reference(n->env, n->ctx);
}
static void
on_udx_interface_event (udx_interface_event_t *handle, int status) {
udx_napi_interface_event_t *e = (udx_napi_interface_event_t *) handle;
if (e->udx->exiting) return;
napi_env env = e->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, e->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, e->on_event, &callback);
if (napi_make_callback(env, NULL, ctx, callback, 0, NULL, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
}
static void
on_udx_interface_event_close (udx_interface_event_t *handle) {
udx_napi_interface_event_t *e = (udx_napi_interface_event_t *) handle;
if (e->udx->exiting) return;
napi_env env = e->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, e->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, e->on_close, &callback);
if (napi_make_callback(env, NULL, ctx, callback, 0, NULL, NULL) == napi_pending_exception) {
napi_value fatal_exception;
napi_get_and_clear_last_exception(env, &fatal_exception);
napi_fatal_exception(env, fatal_exception);
}
napi_close_handle_scope(env, scope);
napi_delete_reference(env, e->on_event);
napi_delete_reference(env, e->on_close);
napi_delete_reference(env, e->ctx);
}
static void
on_udx_idle (udx_t *u) {
udx_napi_t *self = (udx_napi_t *) u;
if (!self->has_teardown) return;
self->has_teardown = false;
napi_remove_async_cleanup_hook(self->teardown);
}
napi_value
udx_napi_init (napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
udx_napi_t *self;
size_t self_len;
napi_get_buffer_info(env, argv[0], (void **) &self, &self_len);
char *read_buf;
size_t read_buf_len;
napi_get_buffer_info(env, argv[1], (void **) &read_buf, &read_buf_len);
uv_loop_t *loop;
napi_get_uv_event_loop(env, &loop);
udx_init(loop, &(self->udx), on_udx_idle);
self->read_buf = read_buf;
self->read_buf_free = read_buf_len;
self->exiting = false;
self->has_teardown = false;
return NULL;
}
napi_value
udx_napi_socket_init (napi_env env, napi_callback_info info) {
napi_value argv[7];
size_t argc = 7;
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
udx_napi_t *udx;
size_t udx_len;
napi_get_buffer_info(env, argv[0], (void **) &udx, &udx_len);
udx_napi_socket_t *self;
size_t self_len;
napi_get_buffer_info(env, argv[1], (void **) &self, &self_len);
udx_socket_t *socket = (udx_socket_t *) self;
self->udx = udx;
self->env = env;
napi_create_reference(env, argv[2], 1, &(self->ctx));
napi_create_reference(env, argv[3], 1, &(self->on_send));
napi_create_reference(env, argv[4], 1, &(self->on_message));
napi_create_reference(env, argv[5], 1, &(self->on_close));
napi_create_reference(env, argv[6], 1, &(self->realloc_message));
int err = udx_socket_init((udx_t *) udx, socket, on_udx_close);
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
ensure_teardown(env, udx);
return NULL;
}
napi_value
udx_napi_socket_bind (napi_env env, napi_callback_info info) {
napi_value argv[5];
size_t argc = 5;
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
udx_socket_t *self;
size_t self_len;
napi_get_buffer_info(env, argv[0], (void **) &self, &self_len);
uint32_t port;
napi_get_value_uint32(env, argv[1], &port);
char ip[INET6_ADDRSTRLEN];
size_t ip_len;
napi_get_value_string_utf8(env, argv[2], (char *) &ip, INET6_ADDRSTRLEN, &ip_len);
uint32_t family;
napi_get_value_uint32(env, argv[3], &family);
uint32_t flags;
napi_get_value_uint32(env, argv[4], &flags);
int err;
struct sockaddr_storage addr;
int addr_len;
if (family == 4) {
addr_len = sizeof(struct sockaddr_in);
err = uv_ip4_addr(ip, port, (struct sockaddr_in *) &addr);
} else {
addr_len = sizeof(struct sockaddr_in6);
err = uv_ip6_addr(ip, port, (struct sockaddr_in6 *) &addr);
}
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
err = udx_socket_bind(self, (struct sockaddr *) &addr, flags);
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
// TODO: move the bottom stuff into another function, start, so error handling is easier
struct sockaddr_storage name;
// wont error in practice
err = udx_socket_getsockname(self, (struct sockaddr *) &name, &addr_len);
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
int local_port;
if (family == 4) {
local_port = ntohs(((struct sockaddr_in *) &name)->sin_port);
} else {
local_port = ntohs(((struct sockaddr_in6 *) &name)->sin6_port);
}
// wont error in practice
err = udx_socket_recv_start(self, on_udx_message);
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
napi_value return_uint32;
napi_create_uint32(env, local_port, &return_uint32);
return return_uint32;
}
napi_value
udx_napi_socket_set_ttl (napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
udx_socket_t *self;
size_t self_len;
napi_get_buffer_info(env, argv[0], (void **) &self, &self_len);
uint32_t ttl;
napi_get_value_uint32(env, argv[1], &ttl);
int err = udx_socket_set_ttl(self, ttl);
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
return NULL;
}
napi_value
udx_napi_socket_set_membership (napi_env env, napi_callback_info info) {
napi_value argv[4];
size_t argc = 4;
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
udx_socket_t *socket;
size_t socket_len;
napi_get_buffer_info(env, argv[0], (void **) &socket, &socket_len);
char mcast_addr[INET6_ADDRSTRLEN];
size_t mcast_addr_len;
napi_get_value_string_utf8(env, argv[1], mcast_addr, INET6_ADDRSTRLEN, &mcast_addr_len);
char iface_addr[INET6_ADDRSTRLEN];
size_t iface_addr_len;
napi_get_value_string_utf8(env, argv[2], iface_addr, INET6_ADDRSTRLEN, &iface_addr_len);
char *iface_param = iface_addr_len > 0 ? iface_addr : NULL;
bool join; // true for join, false for leave
napi_get_value_bool(env, argv[3], &join);
int err = udx_socket_set_membership(socket, mcast_addr, iface_param, join ? UV_JOIN_GROUP : UV_LEAVE_GROUP);
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
return NULL;
}
napi_value
udx_napi_socket_get_recv_buffer_size (napi_env env, napi_callback_info info) {
napi_value argv[1];
size_t argc = 1;
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
udx_socket_t *self;
size_t self_len;
napi_get_buffer_info(env, argv[0], (void **) &self, &self_len);
int size = 0;
int err = udx_socket_get_recv_buffer_size(self, &size);
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
napi_value return_uint32;
napi_create_uint32(env, size, &return_uint32);
return return_uint32;
}
napi_value
udx_napi_socket_set_recv_buffer_size (napi_env env, napi_callback_info info) {
napi_value argv[2];
size_t argc = 2;
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
udx_socket_t *self;
size_t self_len;
napi_get_buffer_info(env, argv[0], (void **) &self, &self_len);
int32_t size;
napi_get_value_int32(env, argv[1], &size);
int err = udx_socket_set_recv_buffer_size(self, size);
if (err < 0) {
napi_throw_error(env, uv_err_name(err), uv_strerror(err));
return NULL;
}
return NULL;
}
napi_value
udx_napi_socket_get_send_buffer_size (napi_env env, napi_callback_info info) {
napi_value argv[1];
size_t argc = 1;
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
udx_socket_t *self;