forked from holepunchto/libudx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
binding.c
989 lines (760 loc) · 27.1 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
#include <napi-macros.h>
#include <node_api.h>
#include <stdlib.h>
#include <string.h>
#include <uv.h>
#include "include/udx.h"
#define UDX_MIN_BUF 2 * UDX_DEFAULT_MTU
#define UDX_NAPI_THROW(err) \
{ \
napi_throw_error(env, uv_err_name(err), uv_strerror(err)); \
return NULL; \
}
#define UDX_NAPI_INTERACTIVE 0
#define UDX_NAPI_NON_INTERACTIVE 1
#define UDX_NAPI_FRAMED 2
#define UDX_NAPI_CALLBACK(self, fn, src) \
napi_env env = self->env; \
napi_handle_scope scope; \
napi_open_handle_scope(env, &scope); \
napi_value ctx; \
napi_get_reference_value(env, self->ctx, &ctx); \
napi_value callback; \
napi_get_reference_value(env, fn, &callback); \
src \
napi_close_handle_scope(env, scope);
#define UDX_NAPI_MAKE_DATA_ALLOC_CALLBACK(self, env, nil, ctx, cb, n, argv, res) \
if (napi_make_callback(env, nil, ctx, cb, n, argv, res) == napi_pending_exception) { \
napi_value fatal_exception; \
napi_get_and_clear_last_exception(env, &fatal_exception); \
napi_fatal_exception(env, fatal_exception); \
{ \
UDX_NAPI_CALLBACK(self, self->realloc_data, { \
NAPI_MAKE_CALLBACK(env, nil, ctx, callback, 0, NULL, res); \
UDX_NAPI_SET_READ_BUFFER(self, res); \
self->read_buf_head = self->read_buf; \
}) \
} \
} else { \
UDX_NAPI_SET_READ_BUFFER(self, res); \
self->read_buf_head = self->read_buf; \
}
#define UDX_NAPI_MAKE_MESSAGE_ALLOC_CALLBACK(self, env, nil, ctx, cb, n, argv, res) \
if (napi_make_callback(env, nil, ctx, cb, n, argv, res) == napi_pending_exception) { \
napi_value fatal_exception; \
napi_get_and_clear_last_exception(env, &fatal_exception); \
napi_fatal_exception(env, fatal_exception); \
{ \
UDX_NAPI_CALLBACK(self, self->realloc_message, { \
NAPI_MAKE_CALLBACK(env, nil, ctx, callback, 0, NULL, res); \
UDX_NAPI_SET_READ_BUFFER(self->udx, res); \
}) \
} \
} else { \
UDX_NAPI_SET_READ_BUFFER(self->udx, res); \
}
#define UDX_NAPI_SET_READ_BUFFER(self, res) \
napi_get_buffer_info(env, *res, (void **) &(self->read_buf), &(self->read_buf_free));
typedef struct {
udx_t udx;
char *read_buf;
size_t read_buf_free;
} 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 realloc_data;
napi_ref realloc_message;
} udx_napi_stream_t;
typedef struct {
udx_lookup_t handle;
char *host;
napi_env env;
napi_ref ctx;
napi_ref on_lookup;
} udx_napi_lookup_t;
typedef struct {
udx_interface_event_t handle;
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->handle;
UDX_NAPI_CALLBACK(n, n->on_send, {
napi_value argv[2];
napi_create_int32(env, (uintptr_t) req->data, &(argv[0]));
napi_create_int32(env, status, &(argv[1]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 2, argv, NULL)
})
}
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;
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);
UDX_NAPI_CALLBACK(n, n->on_message, {
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;
UDX_NAPI_MAKE_MESSAGE_ALLOC_CALLBACK(n, env, NULL, ctx, callback, 4, argv, &res);
})
}
static void
on_udx_close (udx_socket_t *self) {
udx_napi_socket_t *n = (udx_napi_socket_t *) self;
UDX_NAPI_CALLBACK(n, n->on_close, {NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL)})
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_stream_end (udx_stream_t *stream) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
size_t read = n->read_buf_head - n->read_buf;
UDX_NAPI_CALLBACK(n, n->on_end, {
napi_value argv[1];
napi_create_uint32(env, read, &(argv[0]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 1, argv, NULL)
})
}
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;
// 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 {
n->frame_len = 3 + (buf->base[0] | (buf->base[1] << 8) | (buf->base[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 >= UDX_MIN_BUF) {
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 < UDX_MIN_BUF) {
n->frame_len -= read;
} else {
return; // wait for more data
}
}
UDX_NAPI_CALLBACK(n, n->on_data, {
napi_value argv[1];
napi_create_uint32(env, read, &(argv[0]));
napi_value res;
UDX_NAPI_MAKE_DATA_ALLOC_CALLBACK(n, env, NULL, ctx, callback, 1, argv, &res);
})
}
static void
on_udx_stream_drain (udx_stream_t *stream) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
UDX_NAPI_CALLBACK(n, n->on_drain, {NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL)})
}
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->handle;
UDX_NAPI_CALLBACK(n, n->on_ack, {
napi_value argv[1];
napi_create_uint32(env, (uintptr_t) req->data, &(argv[0]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 1, argv, NULL)
})
}
static void
on_udx_stream_send (udx_stream_send_t *req, int status) {
udx_napi_stream_t *n = (udx_napi_stream_t *) req->handle;
UDX_NAPI_CALLBACK(n, n->on_send, {
napi_value argv[2];
napi_create_int32(env, (uintptr_t) req->data, &(argv[0]));
napi_create_int32(env, status, &(argv[1]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 2, argv, NULL)
})
}
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 (buf->len > n->udx->read_buf_free) return;
memcpy(n->udx->read_buf, buf->base, buf->len);
UDX_NAPI_CALLBACK(n, n->on_message, {
napi_value argv[1];
napi_create_uint32(env, read_len, &(argv[0]));
napi_value res;
UDX_NAPI_MAKE_MESSAGE_ALLOC_CALLBACK(n, env, NULL, ctx, callback, 1, argv, &res);
})
}
static void
on_udx_stream_close (udx_stream_t *stream, int status) {
udx_napi_stream_t *n = (udx_napi_stream_t *) stream;
if (status >= 0) {
UDX_NAPI_CALLBACK(n, n->on_close, {
napi_value argv[1];
napi_get_null(env, &(argv[0]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 1, argv, NULL)
})
} else {
UDX_NAPI_CALLBACK(n, n->on_close, {
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]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 1, argv, NULL)
})
}
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->realloc_data);
napi_delete_reference(n->env, n->realloc_message);
napi_delete_reference(n->env, n->ctx);
}
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
int port = 0;
char ip[INET6_ADDRSTRLEN];
int family = 0;
parse_address((struct sockaddr *) from, ip, INET6_ADDRSTRLEN, &port, &family);
UDX_NAPI_CALLBACK(n, n->on_firewall, {
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);
}
})
return fw;
}
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;
char ip[INET6_ADDRSTRLEN] = "";
int family = 0;
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;
}
UDX_NAPI_CALLBACK(n, n->on_lookup, {
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]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 3, argv, NULL)
})
} else {
UDX_NAPI_CALLBACK(n, n->on_lookup, {
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]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 1, argv, NULL)
})
}
napi_delete_reference(n->env, n->on_lookup);
napi_delete_reference(n->env, n->ctx);
free(n->host);
}
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;
UDX_NAPI_CALLBACK(e, e->on_event, {NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL)})
}
static void
on_udx_interface_event_close (udx_interface_event_t *handle) {
udx_napi_interface_event_t *e = (udx_napi_interface_event_t *) handle;
UDX_NAPI_CALLBACK(e, e->on_close, {NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 0, NULL, NULL)})
napi_delete_reference(env, e->on_event);
napi_delete_reference(env, e->on_close);
napi_delete_reference(env, e->ctx);
}
NAPI_METHOD(udx_napi_init) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_napi_t *, self, 0)
NAPI_ARGV_BUFFER(read_buf, 1)
uv_loop_t *loop;
napi_get_uv_event_loop(env, &loop);
udx_init(loop, &(self->udx));
self->read_buf = read_buf;
self->read_buf_free = read_buf_len;
return NULL;
}
NAPI_METHOD(udx_napi_socket_init) {
NAPI_ARGV(7)
NAPI_ARGV_BUFFER_CAST(udx_napi_t *, udx, 0)
NAPI_ARGV_BUFFER_CAST(udx_napi_socket_t *, self, 1)
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);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_socket_bind) {
NAPI_ARGV(4)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, self, 0)
NAPI_ARGV_UINT32(port, 1)
NAPI_ARGV_UTF8(ip, INET6_ADDRSTRLEN, 2)
NAPI_ARGV_UINT32(family, 3)
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) UDX_NAPI_THROW(err)
err = udx_socket_bind(self, (struct sockaddr *) &addr);
if (err < 0) UDX_NAPI_THROW(err)
// 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) UDX_NAPI_THROW(err)
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) UDX_NAPI_THROW(err)
NAPI_RETURN_UINT32(local_port)
}
NAPI_METHOD(udx_napi_socket_set_ttl) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, self, 0)
NAPI_ARGV_UINT32(ttl, 1)
int err = udx_socket_set_ttl(self, ttl);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_socket_get_recv_buffer_size) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, self, 0)
int size = 0;
int err = udx_socket_get_recv_buffer_size(self, &size);
if (err < 0) UDX_NAPI_THROW(err)
NAPI_RETURN_UINT32(size)
}
NAPI_METHOD(udx_napi_socket_set_recv_buffer_size) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, self, 0)
NAPI_ARGV_INT32(size, 1)
int err = udx_socket_set_recv_buffer_size(self, size);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_socket_get_send_buffer_size) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, self, 0)
int size = 0;
int err = udx_socket_get_send_buffer_size(self, &size);
if (err < 0) UDX_NAPI_THROW(err)
NAPI_RETURN_UINT32(size)
}
NAPI_METHOD(udx_napi_socket_set_send_buffer_size) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, self, 0)
NAPI_ARGV_INT32(size, 1)
int err = udx_socket_set_send_buffer_size(self, size);
if (err < 0) UDX_NAPI_THROW(err)
NAPI_RETURN_UINT32(size)
}
NAPI_METHOD(udx_napi_socket_send_ttl) {
NAPI_ARGV(8)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, self, 0)
NAPI_ARGV_BUFFER_CAST(udx_socket_send_t *, req, 1)
NAPI_ARGV_UINT32(rid, 2)
NAPI_ARGV_BUFFER(buf, 3)
NAPI_ARGV_UINT32(port, 4)
NAPI_ARGV_UTF8(ip, INET6_ADDRSTRLEN, 5)
NAPI_ARGV_UINT32(family, 6)
NAPI_ARGV_UINT32(ttl, 7)
req->data = (void *) ((uintptr_t) rid);
int err;
struct sockaddr_storage addr;
if (family == 4) {
err = uv_ip4_addr(ip, port, (struct sockaddr_in *) &addr);
} else {
err = uv_ip6_addr(ip, port, (struct sockaddr_in6 *) &addr);
}
if (err < 0) UDX_NAPI_THROW(err)
uv_buf_t b = uv_buf_init(buf, buf_len);
udx_socket_send_ttl(req, self, &b, 1, (const struct sockaddr *) &addr, ttl, on_udx_send);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_socket_close) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, self, 0)
int err = udx_socket_close(self, on_udx_close);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_stream_init) {
NAPI_ARGV(15)
NAPI_ARGV_BUFFER_CAST(udx_napi_t *, udx, 0)
NAPI_ARGV_BUFFER_CAST(udx_napi_stream_t *, self, 1)
NAPI_ARGV_UINT32(id, 2)
NAPI_ARGV_UINT32(framed, 3)
udx_stream_t *stream = (udx_stream_t *) self;
self->mode = framed ? UDX_NAPI_FRAMED : UDX_NAPI_INTERACTIVE;
self->frame_len = -1;
self->read_buf = NULL;
self->read_buf_head = NULL;
self->read_buf_free = 0;
self->udx = udx;
self->env = env;
napi_create_reference(env, argv[4], 1, &(self->ctx));
napi_create_reference(env, argv[5], 1, &(self->on_data));
napi_create_reference(env, argv[6], 1, &(self->on_end));
napi_create_reference(env, argv[7], 1, &(self->on_drain));
napi_create_reference(env, argv[8], 1, &(self->on_ack));
napi_create_reference(env, argv[9], 1, &(self->on_send));
napi_create_reference(env, argv[10], 1, &(self->on_message));
napi_create_reference(env, argv[11], 1, &(self->on_close));
napi_create_reference(env, argv[12], 1, &(self->on_firewall));
napi_create_reference(env, argv[13], 1, &(self->realloc_data));
napi_create_reference(env, argv[14], 1, &(self->realloc_message));
int err = udx_stream_init((udx_t *) udx, stream, id, on_udx_stream_close);
if (err < 0) UDX_NAPI_THROW(err)
udx_stream_firewall(stream, on_udx_stream_firewall);
udx_stream_write_resume(stream, on_udx_stream_drain);
return NULL;
}
NAPI_METHOD(udx_napi_stream_set_mtu) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, stream, 0)
NAPI_ARGV_UINT32(mtu, 1)
int err = udx_stream_set_mtu(stream, mtu);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_stream_set_seq) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, stream, 0)
NAPI_ARGV_UINT32(seq, 1)
int err = udx_stream_set_seq(stream, seq);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_stream_set_ack) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, stream, 0)
NAPI_ARGV_UINT32(ack, 1)
int err = udx_stream_set_ack(stream, ack);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_stream_set_mode) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_napi_stream_t *, stream, 0)
NAPI_ARGV_UINT32(mode, 1)
stream->mode = mode;
return NULL;
}
NAPI_METHOD(udx_napi_stream_recv_start) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_napi_stream_t *, stream, 0)
NAPI_ARGV_BUFFER(read_buf, 1)
stream->read_buf = read_buf;
stream->read_buf_head = read_buf;
stream->read_buf_free = read_buf_len;
udx_stream_read_start((udx_stream_t *) stream, on_udx_stream_read);
udx_stream_recv_start((udx_stream_t *) stream, on_udx_stream_recv);
return NULL;
}
NAPI_METHOD(udx_napi_stream_connect) {
NAPI_ARGV(6)
NAPI_ARGV_BUFFER_CAST(udx_napi_stream_t *, stream, 0)
NAPI_ARGV_BUFFER_CAST(udx_socket_t *, socket, 1)
NAPI_ARGV_UINT32(remote_id, 2)
NAPI_ARGV_UINT32(port, 3)
NAPI_ARGV_UTF8(ip, INET6_ADDRSTRLEN, 4)
NAPI_ARGV_UINT32(family, 5)
int err;
struct sockaddr_storage addr;
if (family == 4) {
err = uv_ip4_addr(ip, port, (struct sockaddr_in *) &addr);
} else {
err = uv_ip6_addr(ip, port, (struct sockaddr_in6 *) &addr);
}
if (err < 0) UDX_NAPI_THROW(err)
udx_stream_connect((udx_stream_t *) stream, socket, remote_id, (const struct sockaddr *) &addr);
return NULL;
}
NAPI_METHOD(udx_napi_stream_relay_to) {
NAPI_ARGV(2)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, stream, 0)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, destination, 1)
int err = udx_stream_relay_to(stream, destination);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_stream_send) {
NAPI_ARGV(4)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, stream, 0)
NAPI_ARGV_BUFFER_CAST(udx_stream_send_t *, req, 1)
NAPI_ARGV_UINT32(rid, 2)
NAPI_ARGV_BUFFER(buf, 3)
req->data = (void *) ((uintptr_t) rid);
uv_buf_t b = uv_buf_init(buf, buf_len);
int err = udx_stream_send(req, stream, &b, 1, on_udx_stream_send);
if (err < 0) UDX_NAPI_THROW(err)
NAPI_RETURN_UINT32(err);
}
NAPI_METHOD(udx_napi_stream_write) {
NAPI_ARGV(4)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, stream, 0)
NAPI_ARGV_BUFFER_CAST(udx_stream_write_t *, req, 1)
NAPI_ARGV_UINT32(rid, 2)
NAPI_ARGV_BUFFER(buf, 3)
req->data = (void *) ((uintptr_t) rid);
uv_buf_t b = uv_buf_init(buf, buf_len);
int err = udx_stream_write(req, stream, &b, 1, on_udx_stream_ack);
if (err < 0) UDX_NAPI_THROW(err)
NAPI_RETURN_UINT32(err);
}
NAPI_METHOD(udx_napi_stream_write_end) {
NAPI_ARGV(4)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, stream, 0)
NAPI_ARGV_BUFFER_CAST(udx_stream_write_t *, req, 1)
NAPI_ARGV_UINT32(rid, 2)
NAPI_ARGV_BUFFER(buf, 3)
req->data = (void *) ((uintptr_t) rid);
uv_buf_t b = uv_buf_init(buf, buf_len);
int err = udx_stream_write_end(req, stream, &b, 1, on_udx_stream_ack);
if (err < 0) UDX_NAPI_THROW(err)
NAPI_RETURN_UINT32(err);
}
NAPI_METHOD(udx_napi_stream_destroy) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(udx_stream_t *, stream, 0)
int err = udx_stream_destroy(stream);
if (err < 0) UDX_NAPI_THROW(err)
NAPI_RETURN_UINT32(err);
}
NAPI_METHOD(udx_napi_lookup) {
NAPI_ARGV(5)
NAPI_ARGV_BUFFER_CAST(udx_napi_lookup_t *, self, 0)
NAPI_ARGV_UTF8_MALLOC(host, 1)
NAPI_ARGV_UINT32(family, 2)
udx_lookup_t *lookup = (udx_lookup_t *) self;
uv_loop_t *loop;
napi_get_uv_event_loop(env, &loop);
self->host = host;
self->env = env;
napi_create_reference(env, argv[3], 1, &(self->ctx));
napi_create_reference(env, argv[4], 1, &(self->on_lookup));
int flags = 0;
if (family == 4) flags |= UDX_LOOKUP_FAMILY_IPV4;
if (family == 6) flags |= UDX_LOOKUP_FAMILY_IPV6;
int err = udx_lookup(loop, lookup, host, flags, on_udx_lookup);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_interface_event_init) {
NAPI_ARGV(4)
NAPI_ARGV_BUFFER_CAST(udx_napi_interface_event_t *, self, 0)
udx_interface_event_t *event = (udx_interface_event_t *) self;
uv_loop_t *loop;
napi_get_uv_event_loop(env, &loop);
self->env = env;
napi_create_reference(env, argv[1], 1, &(self->ctx));
napi_create_reference(env, argv[2], 1, &(self->on_event));
napi_create_reference(env, argv[3], 1, &(self->on_close));
int err = udx_interface_event_init(loop, event);
if (err < 0) UDX_NAPI_THROW(err)
err = udx_interface_event_start(event, on_udx_interface_event, 5000);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_interface_event_start) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(udx_interface_event_t *, event, 0)
int err = udx_interface_event_start(event, on_udx_interface_event, 5000);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_interface_event_stop) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(udx_interface_event_t *, event, 0)
int err = udx_interface_event_stop(event);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_interface_event_close) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(udx_interface_event_t *, event, 0)
int err = udx_interface_event_close(event, on_udx_interface_event_close);
if (err < 0) UDX_NAPI_THROW(err)
return NULL;
}
NAPI_METHOD(udx_napi_interface_event_get_addrs) {
NAPI_ARGV(1)
NAPI_ARGV_BUFFER_CAST(udx_interface_event_t *, event, 0)
char ip[INET6_ADDRSTRLEN];
int family = 0;
napi_value napi_result;
napi_create_array(env, &napi_result);
for (int i = 0, j = 0; i < event->addrs_len; i++) {
uv_interface_address_t addr = event->addrs[i];
if (addr.address.address4.sin_family == AF_INET) {
uv_ip4_name(&addr.address.address4, ip, sizeof(ip));
family = 4;
} else if (addr.address.address4.sin_family == AF_INET6) {
uv_ip6_name(&addr.address.address6, ip, sizeof(ip));
family = 6;
} else {
continue;
}
napi_value napi_item;
napi_create_object(env, &napi_item);
napi_set_element(env, napi_result, j++, napi_item);
napi_value napi_name;
napi_create_string_utf8(env, addr.name, NAPI_AUTO_LENGTH, &napi_name);
napi_set_named_property(env, napi_item, "name", napi_name);
napi_value napi_ip;
napi_create_string_utf8(env, ip, NAPI_AUTO_LENGTH, &napi_ip);
napi_set_named_property(env, napi_item, "host", napi_ip);
napi_value napi_family;
napi_create_uint32(env, family, &napi_family);
napi_set_named_property(env, napi_item, "family", napi_family);
napi_value napi_internal;
napi_get_boolean(env, addr.is_internal, &napi_internal);
napi_set_named_property(env, napi_item, "internal", napi_internal);
}
return napi_result;
}
NAPI_INIT() {
NAPI_EXPORT_OFFSETOF(udx_stream_t, inflight)
NAPI_EXPORT_OFFSETOF(udx_stream_t, mtu)
NAPI_EXPORT_OFFSETOF(udx_stream_t, cwnd)
NAPI_EXPORT_OFFSETOF(udx_stream_t, srtt)
NAPI_EXPORT_OFFSETOF(udx_stream_t, pkts_waiting)
NAPI_EXPORT_OFFSETOF(udx_stream_t, pkts_inflight)
NAPI_EXPORT_SIZEOF(udx_napi_t)
NAPI_EXPORT_SIZEOF(udx_napi_socket_t)
NAPI_EXPORT_SIZEOF(udx_napi_stream_t)
NAPI_EXPORT_SIZEOF(udx_napi_lookup_t)
NAPI_EXPORT_SIZEOF(udx_napi_interface_event_t)
NAPI_EXPORT_SIZEOF(udx_socket_send_t)
NAPI_EXPORT_SIZEOF(udx_stream_write_t)
NAPI_EXPORT_SIZEOF(udx_stream_send_t)
NAPI_EXPORT_FUNCTION(udx_napi_init)
NAPI_EXPORT_FUNCTION(udx_napi_socket_init)
NAPI_EXPORT_FUNCTION(udx_napi_socket_bind)
NAPI_EXPORT_FUNCTION(udx_napi_socket_set_ttl)
NAPI_EXPORT_FUNCTION(udx_napi_socket_get_recv_buffer_size)
NAPI_EXPORT_FUNCTION(udx_napi_socket_set_recv_buffer_size)
NAPI_EXPORT_FUNCTION(udx_napi_socket_get_send_buffer_size)
NAPI_EXPORT_FUNCTION(udx_napi_socket_set_send_buffer_size)
NAPI_EXPORT_FUNCTION(udx_napi_socket_send_ttl)
NAPI_EXPORT_FUNCTION(udx_napi_socket_close)
NAPI_EXPORT_FUNCTION(udx_napi_stream_init)
NAPI_EXPORT_FUNCTION(udx_napi_stream_set_mtu)
NAPI_EXPORT_FUNCTION(udx_napi_stream_set_seq)
NAPI_EXPORT_FUNCTION(udx_napi_stream_set_ack)
NAPI_EXPORT_FUNCTION(udx_napi_stream_set_mode)
NAPI_EXPORT_FUNCTION(udx_napi_stream_connect)
NAPI_EXPORT_FUNCTION(udx_napi_stream_relay_to)
NAPI_EXPORT_FUNCTION(udx_napi_stream_send)
NAPI_EXPORT_FUNCTION(udx_napi_stream_recv_start)
NAPI_EXPORT_FUNCTION(udx_napi_stream_write)
NAPI_EXPORT_FUNCTION(udx_napi_stream_write_end)
NAPI_EXPORT_FUNCTION(udx_napi_stream_destroy)
NAPI_EXPORT_FUNCTION(udx_napi_lookup)
NAPI_EXPORT_FUNCTION(udx_napi_interface_event_init)
NAPI_EXPORT_FUNCTION(udx_napi_interface_event_start)
NAPI_EXPORT_FUNCTION(udx_napi_interface_event_stop)
NAPI_EXPORT_FUNCTION(udx_napi_interface_event_close)
NAPI_EXPORT_FUNCTION(udx_napi_interface_event_get_addrs)
}