/
socket.erl
4853 lines (4304 loc) · 165 KB
/
socket.erl
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
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2020-2023. All Rights Reserved.
%%
%% 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.
%%
%% %CopyrightEnd%
%%
-module(socket).
-compile({no_auto_import, [error/1, monitor/1]}).
%% Administrative and "global" utility functions
-export([
%% (registry) Socket functions
number_of/0,
which_sockets/0, which_sockets/1,
%% (registry) Socket monitor functions
number_of_monitors/0, number_of_monitors/1,
which_monitors/1,
monitored_by/1,
debug/1, socket_debug/1, use_registry/1,
info/0, info/1,
i/0, i/1, i/2,
tables/0, table/1,
monitor/1, cancel_monitor/1,
supports/0, supports/1, supports/2,
is_supported/1, is_supported/2, is_supported/3,
to_list/1
]).
-export([
open/1, open/2, open/3, open/4,
bind/2, bind/3,
connect/1, connect/2, connect/3,
listen/1, listen/2,
accept/1, accept/2,
send/2, send/3, send/4,
sendto/3, sendto/4, sendto/5,
sendmsg/2, sendmsg/3, sendmsg/4,
sendfile/2, sendfile/3, sendfile/4, sendfile/5,
recv/1, recv/2, recv/3, recv/4,
recvfrom/1, recvfrom/2, recvfrom/3, recvfrom/4,
recvmsg/1, recvmsg/2, recvmsg/3, recvmsg/4, recvmsg/5,
close/1,
shutdown/2,
setopt/3, setopt_native/3, setopt/4,
getopt/2, getopt_native/3, getopt/3,
sockname/1,
peername/1,
ioctl/2, ioctl/3, ioctl/4,
cancel/2
]).
%% Misc utility functions
-export([
which_socket_kind/1,
options/0, options/1, options/2, option/1, option/2,
protocols/0, protocol/1
]).
-export_type([
socket/0,
socket_handle/0,
select_tag/0,
select_handle/0,
select_info/0,
completion_tag/0,
completion_handle/0,
completion_info/0,
invalid/0,
eei/0,
socket_counters/0,
socket_info/0,
domain/0,
type/0,
protocol/0,
port_number/0,
in_addr/0,
in6_addr/0,
sockaddr/0, sockaddr_recv/0,
sockaddr_in/0,
sockaddr_in6/0,
sockaddr_un/0,
sockaddr_ll/0,
sockaddr_dl/0,
sockaddr_unspec/0,
sockaddr_native/0,
msg_flag/0,
level/0,
otp_socket_option/0,
socket_option/0,
%% Option values' types
linger/0,
timeval/0,
ip_mreq/0,
ip_mreq_source/0,
ip_msfilter/0,
ip_pmtudisc/0,
ip_tos/0,
ip_pktinfo/0,
ipv6_mreq/0,
ipv6_pmtudisc/0,
ipv6_hops/0,
ipv6_pktinfo/0,
sctp_assocparams/0,
sctp_event_subscribe/0,
sctp_initmsg/0,
sctp_rtoinfo/0,
msg/0, msg_send/0, msg_recv/0,
cmsg/0, cmsg_send/0, cmsg_recv/0,
ee_origin/0,
icmp_dest_unreach/0,
icmpv6_dest_unreach/0,
extended_err/0,
info_keys/0
]).
%% DUMMY
-export_type([ioctl_device_flag/0, ioctl_device_map/0]).
%% We need #file_descriptor{} for sendfile/2,3,4,5
-include("file_int.hrl").
%% -include("socket_int.hrl").
%% -define(DBG(T),
%% erlang:display({{self(), ?MODULE, ?LINE, ?FUNCTION_NAME}, T})).
%% Also in prim_socket
-define(REGISTRY, socket_registry).
-type invalid() :: {invalid, What :: term()}.
%% Extended Error Information
-type eei() :: #{info := econnreset | econnaborted |
netname_deleted | too_many_cmds | atom(),
raw_info := term()}.
-type info() ::
#{counters := #{atom() := non_neg_integer()},
iov_max := non_neg_integer(),
use_registry := boolean(),
io_backend := #{name := atom()}}.
-type socket_counters() :: #{read_byte := non_neg_integer(),
read_fails := non_neg_integer(),
read_pkg := non_neg_integer(),
read_pkg_max := non_neg_integer(),
read_tries := non_neg_integer(),
read_waits := non_neg_integer(),
write_byte := non_neg_integer(),
write_fails := non_neg_integer(),
write_pkg := non_neg_integer(),
write_pkg_max := non_neg_integer(),
write_tries := non_neg_integer(),
write_waits := non_neg_integer(),
sendfile => non_neg_integer(),
sendfile_byte => non_neg_integer(),
sendfile_fails => non_neg_integer(),
sendfile_max => non_neg_integer(),
sendfile_pkg => non_neg_integer(),
sendfile_pkg_max => non_neg_integer(),
sendfile_tries => non_neg_integer(),
sendfile_waits => non_neg_integer(),
acc_success := non_neg_integer(),
acc_fails := non_neg_integer(),
acc_tries := non_neg_integer(),
acc_waits := non_neg_integer()}.
-type socket_info() :: #{domain := domain() | integer(),
type := type() | integer(),
protocol := protocol() | integer(),
owner := pid(),
ctype := normal | fromfd | {fromfd, integer()},
counters := socket_counters(),
num_readers := non_neg_integer(),
num_writers := non_neg_integer(),
num_acceptors := non_neg_integer(),
writable := boolean(),
readable := boolean(),
rstates := [atom()],
wstates := [atom()]}.
%% We support only a subset of all domains.
-type domain() :: inet | inet6 | local | unspec.
%% We support only a subset of all types.
%% RDM - Reliably Delivered Messages
-type type() :: stream | dgram | raw | rdm | seqpacket.
%% We support all protocols enumerated by getprotoent(),
%% and all of ip | ipv6 | tcp | udp | sctp that are supported
%% by the platform, even if not enumerated by getprotoent()
-type protocol() :: atom().
-type port_number() :: 0..65535.
-type in_addr() :: {0..255, 0..255, 0..255, 0..255}.
-type in6_flow_info() :: 0..16#FFFFF.
-type in6_scope_id() :: 0..16#FFFFFFFF.
-type in6_addr() ::
{0..65535,
0..65535,
0..65535,
0..65535,
0..65535,
0..65535,
0..65535,
0..65535}.
-type linger() ::
#{onoff := boolean(),
linger := non_neg_integer()}.
-type timeval() ::
#{sec := integer(),
usec := integer()}.
-type ip_mreq() ::
#{multiaddr := in_addr(),
interface := in_addr()}.
-type ip_mreq_source() ::
#{multiaddr := in_addr(),
interface := in_addr(),
sourceaddr := in_addr()}.
-type ip_msfilter() ::
#{multiaddr := in_addr(),
interface := in_addr(),
mode := 'include' | 'exclude',
slist := [ in_addr() ]}.
-type ip_pmtudisc() ::
want | dont | do | probe.
%% If the integer value is used, its up to the caller to ensure its valid!
-type ip_tos() :: lowdelay |
throughput |
reliability |
mincost.
-type ip_pktinfo() ::
#{ifindex := non_neg_integer(), % Interface Index
spec_dst := in_addr(), % Local Address
addr := in_addr() % Header Destination address
}.
-type ipv6_mreq() ::
#{multiaddr := in6_addr(),
interface := non_neg_integer()}.
-type ipv6_pmtudisc() ::
want | dont | do | probe.
-type ipv6_hops() ::
default | 0..255.
-type ipv6_pktinfo() ::
#{addr := in6_addr(),
ifindex := integer()
}.
-type sctp_assocparams() ::
#{assoc_id := integer(),
asocmaxrxt := 0..16#ffff,
numbe_peer_destinations := 0..16#ffff,
peer_rwnd := 0..16#ffffffff,
local_rwnd := 0..16#ffffffff,
cookie_life := 0..16#ffffffff}.
-type sctp_event_subscribe() ::
#{data_io := boolean(),
association := boolean(),
address := boolean(),
send_failure := boolean(),
peer_error := boolean(),
shutdown := boolean(),
partial_delivery := boolean(),
adaptation_layer => boolean(),
sender_dry => boolean()}.
-type sctp_initmsg() ::
#{num_ostreams := 0..16#ffff,
max_instreams := 0..16#ffff,
max_attempts := 0..16#ffff,
max_init_timeo := 0..16#ffff}.
-type sctp_rtoinfo() ::
#{assoc_id := integer(),
initial := 0..16#ffffffff,
max := 0..16#ffffffff,
min := 0..16#ffffffff}.
-type packet_type() :: host | broadcast | multicast | otherhost |
outgoing | loopback | user | kernel | fastroute |
non_neg_integer().
-type hatype() :: netrom | eether | ether | ax25 | pronet | chaos |
ieee802 | arcnet | appletlk | dlci | atm | metricom |
ieee1394 | eui64 | infiniband |
tunnel | tunnel6 | loopback | localtlk |
none | void |
non_neg_integer().
-type sockaddr_un() ::
#{family := 'local',
path := binary() | string()}.
-type sockaddr_in() ::
#{family := 'inet',
port := port_number(),
%% The 'broadcast' here is the "limited broadcast"
addr := 'any' | 'broadcast' | 'loopback' | in_addr()}.
-type sockaddr_in6() ::
#{family := 'inet6',
port := port_number(),
addr := 'any' | 'loopback' | in6_addr(),
flowinfo := in6_flow_info(),
scope_id := in6_scope_id()}.
-type sockaddr_ll() ::
#{family := 'packet',
protocol := non_neg_integer(),
ifindex := integer(),
pkttype := packet_type(),
hatype := hatype(),
addr := binary()}.
-type sockaddr_dl() ::
#{family := 'link',
index := non_neg_integer(),
type := non_neg_integer(),
nlen := non_neg_integer(),
alen := non_neg_integer(),
slen := non_neg_integer(),
data := binary()}.
-type sockaddr_unspec() ::
#{family := 'unspec', addr := binary()}.
-type sockaddr_native() ::
#{family := integer(), addr := binary()}.
-type sockaddr() ::
sockaddr_in() |
sockaddr_in6() |
sockaddr_un() |
sockaddr_ll() |
sockaddr_dl() |
sockaddr_unspec() |
sockaddr_native().
-type sockaddr_recv() ::
sockaddr() | binary().
%% (otp) - This option is internal to our (OTP) implementation.
%% socket - The socket layer (SOL_SOCKET).
%% (Int) - Raw level, sent down and used "as is".
%% protocol() - Protocol number; ip | ipv6 | tcp | udp | sctp | ...
-type level() ::
%% otp | % Has got own clauses in setopt/getopt
%% integer() % Has also got own clauses
socket | %% Handled explicitly
protocol().
%% There are some options that are 'read-only'.
%% Should those be included here or in a special list?
%% Should we just document it and leave it to the user?
%% Or catch it in the encode functions?
%% A setopt for a readonly option leads to {error, invalid()}?
%% Do we really need a sndbuf?
-type otp_socket_option() ::
debug |
iow |
controlling_process |
rcvbuf | % sndbuf |
rcvctrlbuf |
sndctrlbuf |
meta |
use_registry |
fd |
domain.
-type socket_option() ::
{Level :: socket,
Opt ::
acceptconn |
acceptfilter |
bindtodevice |
broadcast |
bsp_state |
busy_poll |
debug |
domain |
dontroute |
error |
exclusiveaddruse |
keepalive |
linger |
mark |
maxdg |
max_msg_size |
oobinline |
passcred |
peek_off |
peercred |
priority |
protocol |
rcvbuf |
rcvbufforce |
rcvlowat |
rcvtimeo |
reuseaddr |
reuseport |
rxq_ovfl |
setfib |
sndbuf |
sndbufforce |
sndlowat |
sndtimeo |
timestamp |
type} |
{Level :: ip,
Opt ::
add_membership |
add_source_membership |
block_source |
dontfrag |
drop_membership |
drop_source_membership |
freebind |
hdrincl |
minttl |
msfilter |
mtu |
mtu_discover |
multicast_all |
multicast_if |
multicast_loop |
multicast_ttl |
nodefrag |
options |
pktinfo |
recvdstaddr |
recverr |
recvif |
recvopts |
recvorigdstaddr |
recvtos |
recvttl |
retopts |
router_alert |
sndsrcaddr |
tos |
transparent |
ttl |
unblock_source} |
{Level :: ipv6,
Opt ::
addrform |
add_membership |
authhdr |
auth_level |
checksum |
drop_membership |
dstopts |
esp_trans_level |
esp_network_level |
faith |
flowinfo |
hopopts |
ipcomp_level |
join_group |
leave_group |
mtu |
mtu_discover |
multicast_hops |
multicast_if |
multicast_loop |
portrange |
pktoptions |
recverr |
recvhoplimit | hoplimit |
recvpktinfo | pktinfo |
recvtclass |
router_alert |
rthdr |
tclass |
unicast_hops |
use_min_mtu |
v6only} |
{Level :: tcp,
Opt ::
congestion |
cork |
info |
keepcnt |
keepidle |
keepintvl |
maxseg |
md5sig |
nodelay |
noopt |
nopush |
syncnt |
user_timeout} |
{Level :: udp, Opt :: cork} |
{Level :: sctp,
Opt ::
adaption_layer |
associnfo |
auth_active_key |
auth_asconf |
auth_chunk |
auth_key |
auth_delete_key |
autoclose |
context |
default_send_params |
delayed_ack_time |
disable_fragments |
hmac_ident |
events |
explicit_eor |
fragment_interleave |
get_peer_addr_info |
initmsg |
i_want_mapped_v4_addr |
local_auth_chunks |
maxseg |
maxburst |
nodelay |
partial_delivery_point |
peer_addr_params |
peer_auth_chunks |
primary_addr |
reset_streams |
rtoinfo |
set_peer_primary_addr |
status |
use_ext_recvinfo}.
%% The names of these macros match the names of corresponding
%%C functions in the NIF code, so a search will match both
%%
-define(socket_tag, '$socket').
%%
%% Our socket abstract data type
-define(socket(Ref), {?socket_tag, (Ref)}).
%%
%% Messages sent from the nif-code to erlang processes:
-define(socket_msg(Socket, Tag, Info), {?socket_tag, (Socket), (Tag), (Info)}).
-type socket() :: ?socket(socket_handle()).
-opaque socket_handle() :: reference().
%% Some flags are used for send, others for recv, and yet again
%% others are found in a cmsg(). They may occur in multiple locations..
-type msg_flag() ::
cmsg_cloexec |
confirm |
ctrunc |
dontroute |
eor |
errqueue |
more |
oob |
peek |
trunc.
-type msg() :: msg_send() | msg_recv().
-type msg_send() ::
#{
%% *Optional* target address
%% Used on an unconnected socket to specify the
%% destination address for a message.
addr => sockaddr(),
iov := erlang:iovec(),
%% *Optional* control message list (ancillary data).
%% The maximum size of the control buffer is platform
%% specific. It is the users responsibility to ensure
%% that its not exceeded.
%%
ctrl =>
([cmsg_send() |
#{level := level() | integer(),
type := integer(),
data := binary()}])
}.
-type msg_recv() ::
#{
%% *Optional* target address
%% Used on an unconnected socket to return the
%% source address for a message.
addr => sockaddr_recv(),
iov := erlang:iovec(),
%% Control messages (ancillary data).
%% The maximum size of the control buffer is platform
%% specific. It is the users responsibility to ensure
%% that its not exceeded.
%%
ctrl :=
([cmsg_recv() |
#{level := level() | integer(),
type := integer(),
data := binary()}]),
%% Received message flags
flags := [msg_flag() | integer()]
}.
%% We are able to (completely) decode *some* control message headers.
%% Even if we are able to decode both level and type, we may not be
%% able to decode the data. The data is always delivered as a binary()
%% and a decoded value is delivered in the 'value' field, if decoding
%% is successful.
-type cmsg() :: cmsg_recv() | cmsg_send().
-type cmsg_recv() ::
#{level := socket, type := timestamp, data := binary(),
value => timeval()} |
#{level := socket, type := rights, data := binary()} |
#{level := socket, type := credentials, data := binary()} |
#{level := ip, type := tos, data := binary(),
value => ip_tos() | integer()} |
#{level := ip, type := recvtos, data := binary(),
value := ip_tos() | integer()} |
#{level := ip, type := ttl, data := binary(),
value => integer()} |
#{level := ip, type := recvttl, data := binary(),
value := integer()} |
#{level := ip, type := pktinfo, data := binary(),
value => ip_pktinfo()} |
#{level := ip, type := origdstaddr, data := binary(),
value => sockaddr_recv()} |
#{level := ip, type := recverr, data := binary(),
value => extended_err()} |
#{level := ipv6, type := hoplimit, data := binary(),
value => integer()} |
#{level := ipv6, type := pktinfo, data := binary(),
value => ipv6_pktinfo()} |
#{level := ipv6, type := recverr, data := binary(),
value => extended_err()} |
#{level := ipv6, type := tclass, data := binary(),
value => integer()}.
-type native_value() :: integer() | boolean() | binary().
%% Possible to add type tagged values a'la {uint16, 0..16#FFFF}
-type cmsg_send() ::
#{level := socket, type := timestamp, data => native_value(),
value => timeval()} |
#{level := socket, type := rights, data := native_value()} |
#{level := socket, type := credentials, data := native_value()} |
#{level := ip, type := tos, data => native_value(),
value => ip_tos() | integer()} |
#{level := ip, type := ttl, data => native_value(),
value => integer()} |
#{level := ip, type := hoplimit, data => native_value(),
value => integer()} |
#{level := ipv6, type := tclass, data => native_value(),
value => integer()}.
-type ee_origin() :: none | local | icmp | icmp6.
-type icmp_dest_unreach() ::
net_unreach | host_unreach | port_unreach | frag_needed |
net_unknown | host_unknown.
-type icmpv6_dest_unreach() ::
noroute | adm_prohibited | not_neighbour | addr_unreach |
port_unreach | policy_fail | reject_route.
-type extended_err() ::
#{error := posix(),
origin := icmp,
type := dest_unreach,
code := icmp_dest_unreach() | 0..16#FF,
info := 0..16#FFFFFFFF,
data := 0..16#FFFFFFFF,
offender := sockaddr_recv()} |
#{error := posix(),
origin := icmp,
type := time_exceeded | 0..16#FF,
code := 0..16#FF,
info := 0..16#FFFFFFFF,
data := 0..16#FFFFFFFF,
offender := sockaddr_recv()} |
#{error := posix(),
origin := icmp6,
type := dest_unreach,
code := icmpv6_dest_unreach() | 0..16#FF,
info := 0..16#FFFFFFFF,
data := 0..16#FFFFFFFF,
offender := sockaddr_recv()} |
#{error := posix(),
origin := icmp6,
type := pkt_toobig | time_exceeded | 0..16#FF,
code := 0..16#FF,
info := 0..16#FFFFFFFF,
data := 0..16#FFFFFFFF,
offender := sockaddr_recv()} |
#{error := posix(),
origin := ee_origin() | 0..16#FF,
type := 0..16#FF,
code := 0..16#FF,
info := 0..16#FFFFFFFF,
data := 0..16#FFFFFFFF,
offender := sockaddr_recv()}.
-type posix() :: inet:posix().
-type info_keys() :: [
'domain' | 'type' | 'protocol' |
'fd' | 'owner' |
'local_address' | 'remote_address' |
'recv' | 'sent' |
'state'
].
%% Note that not all flags exist on all platforms!
-type ioctl_device_flag() :: up | broadcast | debug | loopback | pointopoint |
notrailers | knowsepoch | running | noarp | promisc | allmulti |
master | oactive | slave | simplex |
link0 | link1 | link2 |
multicast | portsel | automedia |
cantconfig | ppromisc |
dynamic |
monitor | staticarp | dying | renaming | nogroup |
lower_up | dormant | echo.
%% When reading the device map (gifmap), the resulting map will be
%% "fully" populated.
%% <DOES-THIS-WORK>
%% When writing, it is expected that only the fields that is
%% to be set is present.
%% </DOES-THIS-WORK>
-type ioctl_device_map() :: #{mem_start := non_neg_integer(),
mem_end := non_neg_integer(),
base_addr := non_neg_integer(),
irq := non_neg_integer(),
dma := non_neg_integer(),
port := non_neg_integer()}.
%% ===========================================================================
%%
%% Interface term formats
%%
-define(ASYNCH_DATA_TAG, (recv | recvfrom | recvmsg |
send | sendto | sendmsg)).
-define(ASYNCH_TAG, ((accept | connect) | ?ASYNCH_DATA_TAG)).
%% -type asynch_data_tag() :: send | sendto | sendmsg |
%% recv | recvfrom | recvmsg |
%% sendfile.
%% -type asynch_tag() :: connect | accept |
%% asynch_data_tag().
%% -type select_tag() :: asynch_tag() |
%% {asynch_data_tag(), ContData :: term()}.
%% -type completion_tag() :: asynch_tag().
-type select_tag() :: ?ASYNCH_TAG | sendfile |
{?ASYNCH_DATA_TAG | sendfile, ContData :: term()}.
-type completion_tag() :: ?ASYNCH_TAG.
-type select_handle() :: reference().
-type completion_handle() :: reference().
-type select_info() ::
{select_info,
SelectTag :: select_tag(),
SelectHandle :: select_handle()}.
-type completion_info() ::
{completion_info,
CompletionTag :: completion_tag(),
CompletionHandle :: completion_handle()}.
-define(SELECT_INFO(Tag, SelectHandle),
{select_info, Tag, SelectHandle}).
-define(COMPLETION_INFO(Tag, CompletionHandle),
{completion_info, Tag, CompletionHandle}).
%% ===========================================================================
%%
%% Defaults
%%
-define(ESOCK_LISTEN_BACKLOG_DEFAULT, 5).
-define(ESOCK_ACCEPT_TIMEOUT_DEFAULT, infinity).
-define(ESOCK_SEND_FLAGS_DEFAULT, []).
-define(ESOCK_SEND_TIMEOUT_DEFAULT, infinity).
-define(ESOCK_SENDTO_FLAGS_DEFAULT, []).
-define(ESOCK_SENDTO_TIMEOUT_DEFAULT, ?ESOCK_SEND_TIMEOUT_DEFAULT).
-define(ESOCK_SENDMSG_FLAGS_DEFAULT, []).
-define(ESOCK_SENDMSG_TIMEOUT_DEFAULT, ?ESOCK_SEND_TIMEOUT_DEFAULT).
-define(ESOCK_RECV_FLAGS_DEFAULT, []).
-define(ESOCK_RECV_TIMEOUT_DEFAULT, infinity).
%% ===========================================================================
%%
%% Administrative and utility API
%%
%% ===========================================================================
%% *** number_of ***
%%
%% Interface function to the socket registry
%% returns the number of existing (and "alive") sockets.
%%
-spec number_of() -> non_neg_integer().
number_of() ->
?REGISTRY:number_of().
%% *** which_sockets/0,1 ***
%%
%% Interface function to the socket registry
%% Returns a list of all the sockets, according to the filter rule.
%%
-spec which_sockets() -> [socket()].
which_sockets() ->
?REGISTRY:which_sockets(true).
-spec which_sockets(FilterRule) -> [socket()] when
FilterRule :: 'inet' | 'inet6' | 'local' |
'stream' | 'dgram' | 'seqpacket' |
'sctp' | 'tcp' | 'udp' |
pid() |
fun((socket_info()) -> boolean()).
which_sockets(Domain)
when Domain =:= inet;
Domain =:= inet6;
Domain =:= local ->
?REGISTRY:which_sockets({domain, Domain});
which_sockets(Type)
when Type =:= stream;
Type =:= dgram;
Type =:= seqpacket ->
?REGISTRY:which_sockets({type, Type});
which_sockets(Proto)
when Proto =:= sctp;
Proto =:= tcp;
Proto =:= udp ->
?REGISTRY:which_sockets({protocol, Proto});
which_sockets(Owner)
when is_pid(Owner) ->
?REGISTRY:which_sockets({owner, Owner});
which_sockets(Filter) when is_function(Filter, 1) ->
?REGISTRY:which_sockets(Filter);
which_sockets(Other) ->
erlang:error(badarg, [Other]).
%% *** number_of_monitors ***
%%
%% Interface function to the socket registry
%% returns the number of existing socket monitors.
%%
-spec number_of_monitors() -> non_neg_integer().
number_of_monitors() ->
?REGISTRY:number_of_monitors().
-spec number_of_monitors(pid()) -> non_neg_integer().
number_of_monitors(Pid) when is_pid(Pid) ->
?REGISTRY:number_of_monitors(Pid).
%% *** which_monitors/1 ***
%%
%% Interface function to the socket registry
%% Returns a list of all the monitors of the process or socket.
%%
-spec which_monitors(Pid) -> [reference()] when
Pid :: pid();
(Socket) -> [reference()] when
Socket :: socket().
which_monitors(Pid) when is_pid(Pid) ->
?REGISTRY:which_monitors(Pid);
which_monitors(?socket(SockRef) = Socket) when is_reference(SockRef) ->
?REGISTRY:which_monitors(Socket);
which_monitors(Socket) ->
erlang:error(badarg, [Socket]).
%% *** monitor_by/1 ***
%%
%% Interface function to the socket registry
%% Returns a list of all the process'es monitoring the socket.
%%
-spec monitored_by(Socket) -> [reference()] when
Socket :: socket().
monitored_by(?socket(SockRef) = Socket) when is_reference(SockRef) ->
?REGISTRY:monitored_by(Socket);
monitored_by(Socket) ->
erlang:error(badarg, [Socket]).
%% *** to_list/1 ***
%%
%% This is intended to convert a socket() to a printable string.
%%
-spec to_list(Socket) -> list() when
Socket :: socket().
to_list(?socket(SockRef)) when is_reference(SockRef) ->
"#Ref" ++ Id = erlang:ref_to_list(SockRef),
"#Socket" ++ Id;
to_list(Socket) ->
erlang:error(badarg, [Socket]).
%% *** which_socket_kind/1 ***
%%
%% Utility function that returns the "kind" of socket.
%% That is, if its a "plain" socket or a compatibillity socket.
%%
-spec which_socket_kind(Socket :: socket()) -> plain | compat.
which_socket_kind(?socket(SockRef) = Socket) when is_reference(SockRef) ->
case prim_socket:getopt(SockRef, {otp,meta}) of
{ok, undefined} ->
plain;
{ok, _} ->
compat;
{error, _} ->
erlang:error(badarg, [Socket])
end;
which_socket_kind(Socket) ->