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
|
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">
<erlref>
<header>
<copyright>
<year>2018</year><year>2018</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
</legalnotice>
<title>socket</title>
<prepared></prepared>
<docno></docno>
<date></date>
<rev></rev>
<file>socket.xml</file>
</header>
<module>socket</module>
<modulesummary>Socket interface.</modulesummary>
<description>
<p>This module provides an API for the socket interface.
It is used to create, delete and manipulate sockets.</p>
</description>
<datatypes>
<datatype>
<name name="domain"/>
</datatype>
<datatype>
<name name="type"/>
</datatype>
<datatype>
<name name="protocol"/>
</datatype>
<datatype>
<name>socket()</name>
<desc><p>As returned by
<seealso marker="#open/1"><c>open/2,3,4</c></seealso> and
<seealso marker="#accept/1"><c>accept/1,2</c></seealso>.</p>
</desc>
</datatype>
<datatype>
<name name="ip4_address"/>
</datatype>
<datatype>
<name name="ip6_address"/>
</datatype>
<datatype>
<name name="ip_address"/>
</datatype>
<datatype>
<name name="sockaddr"/>
</datatype>
<datatype>
<name name="sockaddr_in4"/>
</datatype>
<datatype>
<name name="sockaddr_in6"/>
</datatype>
<datatype>
<name name="sockaddr_un"/>
</datatype>
<datatype>
<name name="port_number"/>
</datatype>
<datatype>
<name name="accept_flags"/>
</datatype>
<datatype>
<name name="accept_flag"/>
</datatype>
<datatype>
<name name="send_flags"/>
</datatype>
<datatype>
<name name="send_flag"/>
</datatype>
<datatype>
<name name="shutdown_how"/>
</datatype>
<datatype>
<name name="sockopt_level"/>
</datatype>
<datatype>
<name name="otp_socket_option"/>
</datatype>
<datatype>
<name name="socket_option"/>
</datatype>
<datatype>
<name name="ip_socket_option"/>
</datatype>
<datatype>
<name name="ipv6_socket_option"/>
</datatype>
<datatype>
<name name="tcp_socket_option"/>
</datatype>
<datatype>
<name name="udp_socket_option"/>
</datatype>
<datatype>
<name name="sctp_socket_option"/>
</datatype>
<datatype>
<name name="timeval"/>
</datatype>
<datatype>
<name name="ip_tos"/>
</datatype>
<datatype>
<name name="ip_mreq"/>
</datatype>
<datatype>
<name name="ip_mreq_source"/>
</datatype>
<datatype>
<name name="ip_pmtudisc"/>
</datatype>
<datatype>
<name name="ip_msfilter_mode"/>
</datatype>
<datatype>
<name name="ip_msfilter"/>
</datatype>
<datatype>
<name name="ip_pktinfo"/>
</datatype>
<datatype>
<name name="ipv6_mreq"/>
</datatype>
<datatype>
<name name="ipv6_pmtudisc"/>
</datatype>
<datatype>
<name name="sctp_sndrcvinfo"/>
</datatype>
<datatype>
<name name="sctp_event_subscribe"/>
</datatype>
<datatype>
<name name="sctp_assocparams"/>
</datatype>
<datatype>
<name name="sctp_initmsg"/>
</datatype>
<datatype>
<name name="sctp_rtoinfo"/>
</datatype>
<datatype>
<name name="msghdr_flag"/>
</datatype>
<datatype>
<name name="msghdr_flags"/>
</datatype>
<datatype>
<name name="msghdr"/>
</datatype>
<datatype>
<name name="cmsghdr_level"/>
</datatype>
<datatype>
<name name="cmsghdr_type"/>
</datatype>
<!--
<datatype>
<name name="cmsghdr_data"/>
</datatype>
-->
<datatype>
<name name="cmsghdr_recv"/>
</datatype>
<datatype>
<name name="cmsghdr_send"/>
</datatype>
<datatype>
<name name="uint8"/>
</datatype>
<datatype>
<name name="uint16"/>
</datatype>
<datatype>
<name name="uint20"/>
</datatype>
<datatype>
<name name="uint32"/>
</datatype>
<datatype>
<name name="int32"/>
</datatype>
</datatypes>
<funcs>
<func>
<name name="accept" arity="1"/>
<name name="accept" arity="2"/>
<fsummary>Accept a connection on a socket.</fsummary>
<desc>
<p>Accept a connection on a socket.</p>
<p>This call is used with connection-based socket types
(<c>stream</c> or <c>seqpacket</c>). It extracs the first pending
connection request for the listen socket and returns the (newly)
connected socket.</p>
</desc>
</func>
<func>
<name name="bind" arity="2"/>
<fsummary>Bind a name to a socket.</fsummary>
<desc>
<p>Bind a name to a socket.</p>
<p>When a socket is created
(with <seealso marker="#open/2"><c>open</c></seealso>),
it has no address assigned to it. <c>bind</c> assigns the
address specified by the <c>Addr</c> argument.</p>
<p>The rules used for name binding vary between domains.</p>
</desc>
</func>
<func>
<name name="close" arity="1"/>
<fsummary>Close a socket.</fsummary>
<desc>
<p>Closes the socket.</p>
<note>
<p>Note that for e.g. <c>protocol</c> = <c>tcp</c>, most implementations
doing a close does not guarantee that any data sent is delivered to
the recipient before the close is detected at the remote side. </p>
<p>One way to handle this is to use the
<seealso marker="#shutdown/2"><c>shutdown</c></seealso> function
(<c>socket:shutdown(Socket, write)</c>) to signal that no more data is
to be sent and then wait for the read side of the socket to be closed.</p>
</note>
</desc>
</func>
<func>
<name name="connect" arity="2"/>
<name name="connect" arity="3"/>
<fsummary>Initiate a connection on a socket.</fsummary>
<desc>
<p>This function connects the socket to the address
specied by the <c>SockAddr</c> argument.</p>
</desc>
</func>
<func>
<name name="getopt" arity="3" clause_i="1"/>
<name name="getopt" arity="3" clause_i="2"/>
<name name="getopt" arity="3" clause_i="3"/>
<name name="getopt" arity="3" clause_i="4"/>
<name name="getopt" arity="3" clause_i="5"/>
<name name="getopt" arity="3" clause_i="6"/>
<name name="getopt" arity="3" clause_i="7"/>
<fsummary>Get an option on a socket.</fsummary>
<desc>
<p>Get an option on a socket.</p>
<p>What properties are valid depend both on <c>Level</c> and
on what kind of socket it is (<c>domain</c>, <c>type</c> and
<c>protocol</c>).</p>
<note><p>Not all options are valid on all platforms. That is,
even if "we" support an option, that does not mean that the
underlying OS does.</p></note>
<!--
We need these tables (one for each level) somewhere,
but they don't work here, so where?
<p>Socket level options: </p>
<table>
<row>
<cell><em>Option Name</em></cell>
<cell><em>Value Type</em></cell>
<cell><em>Set</em></cell>
<cell><em>Get</em></cell>
<cell><em>Other</em></cell>
</row>
<row>
<cell>acceptcon</cell>
<cell>boolean()</cell>
<cell>no</cell>
<cell>yes</cell>
<cell>none</cell>
</row>
<row>
<cell>broadcast</cell>
<cell>boolean()</cell>
<cell>yes</cell>
<cell>yes</cell>
<cell>dgram</cell>
</row>
<row>
<cell>debug</cell>
<cell>integer()</cell>
<cell>yes</cell>
<cell>yes</cell>
<cell>may require admin capability</cell>
</row>
<row>
<cell>domain</cell>
<cell>domain()</cell>
<cell>no</cell>
<cell>yes</cell>
<cell>none</cell>
</row>
</table>
-->
</desc>
</func>
<func>
<name name="getopt" arity="3" clause_i="8"/>
<fsummary>Get an option on a socket.</fsummary>
<desc>
<p>Get an option on a socket.</p>
<p>What properties are valid depend both on <c>Level</c> and
on what kind of socket it is (<c>domain</c>, <c>type</c> and
<c>protocol</c>).</p>
<p>When specifying <c>Level</c> as an integer, and therefor
using "native mode", it is *currently* up to the caller to
know how to interpret the result.</p>
<note><p>Not all options are valid on all platforms. That is,
even if "we" support an option, that does not mean that the
underlying OS does.</p></note>
</desc>
</func>
<func>
<name name="listen" arity="1"/>
<name name="listen" arity="2"/>
<fsummary>Listen for connections on a socket.</fsummary>
<desc>
<p>Listen for connections on a socket.</p>
</desc>
</func>
<func>
<name name="open" arity="2"/>
<name name="open" arity="3"/>
<name name="open" arity="4"/>
<fsummary>Create an endpoint for communication.</fsummary>
<desc>
<p>Creates an endpoint (socket) for communication.</p>
<p>For some <c>types</c> there is a default protocol, which will
be used if no protocol is specified: </p>
<list>
<item><p><c>stream</c>: <c>tcp</c></p></item>
<item><p><c>dgram</c>: <c>udp</c></p></item>
<item><p><c>seqpacket</c>: <c>sctp</c></p></item>
</list>
<p>The <c>Extra</c> argument is intended for "obscure" options.
Currently the only supported option is <c>netns</c>, which
is only supported on the linux platform.</p>
</desc>
</func>
<func>
<name name="peername" arity="1"/>
<fsummary>Get name of connected socket peer.</fsummary>
<desc>
<p>Returns the address of the peer connected to the socket.</p>
</desc>
</func>
<func>
<name name="recv" arity="1"/>
<name name="recv" arity="2"/>
<name name="recv" arity="3" clause_i="1"/>
<name name="recv" arity="3" clause_i="2"/>
<name name="recv" arity="4"/>
<fsummary>Receive a message from a socket.</fsummary>
<desc>
<p>Receive a message from a socket.</p>
<p>There is a special case for the argument <c>Length</c>.
If it is set to zero (0), it means "give me everything you
currently have".</p>
</desc>
</func>
<func>
<name name="recvfrom" arity="1"/>
<name name="recvfrom" arity="2"/>
<name name="recvfrom" arity="3" clause_i="1"/>
<name name="recvfrom" arity="3" clause_i="2"/>
<name name="recvfrom" arity="3" clause_i="3"/>
<name name="recvfrom" arity="4"/>
<fsummary>Receive a message from a socket.</fsummary>
<desc>
<p>Receive a message from a socket.</p>
<p>This function reads "messages", which means that regardless of
how much we want to read, it returns when we get a message.</p>
<p>The <c>BufSz</c> argument basically defines the size of the
receive buffer. By setting the value to zero (0), the configured
size (setopt with <c>Level</c> = <c>otp</c>) is used.</p>
<p>It may be impossible to know what (buffer) size is appropriate
"in advance", and in those cases it may be convenient to use the
(recv) 'peek' flag. When this flag is provided, the message is *not*
"consumed" from the underlying buffers, so another recvfrom call
is needed, possibly with a then adjusted buffer size.</p>
</desc>
</func>
<func>
<name name="recvmsg" arity="1"/>
<name name="recvmsg" arity="2" clause_i="1"/>
<name name="recvmsg" arity="2" clause_i="2"/>
<name name="recvmsg" arity="3"/>
<name name="recvmsg" arity="5"/>
<fsummary>Receive a message from a socket.</fsummary>
<desc>
<p>Receive a message from a socket.</p>
<p>This function reads "messages", which means that regardless of
how much we want to read, it returns when we get a message.</p>
<p>The message will be delivered in the form of a <c>msghdr()</c>,
which may contain the source address (if socket not connected),
a list of <c>cmsghdr_recv()</c> (depends on what socket options have
been set and what the protocol and platform supports) and
also a set of flags, providing further info about the read . </p>
<p>The <c>BufSz</c> argument basically defines the size of the
receive buffer. By setting the value to zero (0), the configured
size (setopt with <c>Level</c> = <c>otp</c>) is used.</p>
<p>The <c>CtrlSz</c> argument basically defines the size of the
receive buffer for the control messages.
By setting the value to zero (0), the configured size (setopt
with <c>Level</c> = <c>otp</c>) is used.</p>
<p>It may be impossible to know what (buffer) size is appropriate
"in advance", and in those cases it may be convenient to use the
(recv) 'peek' flag. When this flag is provided, the message is *not*
"consumed" from the underlying buffers, so another recvmsg call
is needed, possibly with a then adjusted buffer size.</p>
</desc>
</func>
<func>
<name name="send" arity="2"/>
<name name="send" arity="3" clause_i="1"/>
<name name="send" arity="3" clause_i="2"/>
<name name="send" arity="4"/>
<fsummary>Send a message on a socket.</fsummary>
<desc>
<p>Send a message on a connected socket.</p>
</desc>
</func>
<func>
<name name="sendmsg" arity="2"/>
<name name="sendmsg" arity="3" clause_i="1"/>
<name name="sendmsg" arity="3" clause_i="2"/>
<name name="sendmsg" arity="4"/>
<fsummary>Send a message on a socket.</fsummary>
<desc>
<p>Send a message on a socket. The destination, if needed
(socket <em>not</em> connected) is provided in the <c>MsgHdr</c>,
which also contains the message to send,
The <c>MsgHdr</c> may also contain an list of optional <c>cmsghdr_send()</c>
(depends on what the protocol and platform supports).</p>
</desc>
</func>
<func>
<name name="sendto" arity="3"/>
<name name="sendto" arity="4"/>
<name name="sendto" arity="5"/>
<fsummary>Send a message on a socket.</fsummary>
<desc>
<p>Send a message on a socket, to the specified destination.</p>
</desc>
</func>
<func>
<name name="setopt" arity="4" clause_i="1"/>
<name name="setopt" arity="4" clause_i="2"/>
<name name="setopt" arity="4" clause_i="3"/>
<name name="setopt" arity="4" clause_i="4"/>
<name name="setopt" arity="4" clause_i="5"/>
<name name="setopt" arity="4" clause_i="6"/>
<name name="setopt" arity="4" clause_i="7"/>
<name name="setopt" arity="4" clause_i="8"/>
<fsummary>Set options on a socket.</fsummary>
<desc>
<p>Set options on a socket.</p>
<p>What properties are valid depend both on <c>Level</c> and on
what kind of socket it is (<c>domain</c>, <c>type</c> and
<c>protocol</c>).</p>
<note><p>Not all options are valid on all platforms. That is,
even if "we" support an option, that does not mean that the
underlying OS does.</p></note>
<note><p>Sockets are set 'non-blocking' when created, so this option
is *not* available (as it would adversely effect the Erlang VM
to set a socket 'blocking').</p></note>
</desc>
</func>
<func>
<name name="shutdown" arity="2"/>
<fsummary>Shut down part of a full-duplex connection.</fsummary>
<desc>
<p>Shut down all or part of a full-duplex connection.</p>
</desc>
</func>
<func>
<name name="sockname" arity="1"/>
<fsummary>Get socket name.</fsummary>
<desc>
<p>Returns the current address to which the socket is bound.</p>
</desc>
</func>
</funcs>
<section>
<title>Examples</title>
<marker id="examples"></marker>
<p>TBD: Need to implement a receiver process in order to be able to
implement active!</p>
<p>x_tcp.erl:</p>
<code type="none">
listen(Addr, Port) ->
try
begin
Socket = case socket:open(inet, stream, tcp) of
{ok, Socket} ->
Socket;
{error, _} = OERROR ->
throw(OERROR)
end,
SockAddr = #in4_sockaddr{port = Port,
addr = Addr},
ok = case socket:bind(Socket, SockAddr) of
ok ->
ok;
{error, _} = BERROR ->
throw(BERROR)
end,
case socket:listen(Socket, 10) of
ok ->
{ok, Socket};
{error, _} = LERROR ->
throw(LERROR)
end
end
catch
throw:ERROR ->
ERROR
end.
</code>
<code type="none">
connect(Addr, Port) ->
try
begin
Socket = case socket:open(inet, stream, tcp) of
{ok, Socket} ->
Socket;
{error, _} = OERROR ->
throw(OERROR)
end,
BSockAddr = #in4_sockaddr{port = 0,
addr = any},
ok = case socket:bind(Socket, BSockAddr) of
ok ->
ok;
{error, _} = BERROR ->
throw(BERROR)
end,
CSockAddr = #in4_sockaddr{port = Port,
addr = Addr},
Socket = case socket:connect(Socket, CSockAddr) of
{ok, Sock} ->
Sock;
{error, _} = CERROR ->
throw(CERROR)
end,
case start_receiver(Socket) of
{ok, Pid} ->
ok = socket:ts_add(Socket, receiver, Pid),
{ok, Socket};
{error, _} = RERROR ->
socket:close(Socket),
throw(RERROR)
end
end
catch
throw:ERROR ->
ERROR
end.
</code>
<code type="none">
accept(LSocket) ->
case socket:accept(LSocket) of
{ok, Socket} ->
case start_receiver(Socket) of
{ok, Pid} ->
ok = socket:ts_add(Socket, receiver, Pid),
{ok, Socket};
{error, _} = RERROR ->
socket:close(Socket),
throw(RERROR)
end,
{error, _} = AERROR ->
AERROR
end.
</code>
<code type="none">
send(Socket, Data) ->
socket:send(Socket, Data).
</code>
<code type="none">
setopt(Socket, controlling_process = Item, Pid) when is_pid(Pid) ->
case socket:setopt(Socket, otp, Item, Pid) of
ok ->
{ok, Receiver} = socket:ts_get(Socket, receiver),
update_receiver(Receiver, {controlling_process, Pid),
ok;
{error, _} = ERROR ->
ERROR
end;
setopt(Socket, active, Active) ->
{ok, Receiver} = socket:ts_get(Socket, receiver),
receiver_active(Receiver, Active),
ok;
%% This is just to indicate that there will be more options
%% that needs to be handled
setopt(Socket, Item, Pid) when is_pid(Pid) ->
socket:setopt(Socket, which_level(Item), Item, Pid).
</code>
<p>The receiver process: </p>
<code type="none">
start_receiver(Socket) ->
CtrlProc = self(),
Ref = make_ref(),
Receiver = proc_lib:spawn_link(fun() -> receiver_init(CtrlProc, Ref) end),
receive
{?MODULE, started, Ref} ->
Receiver ! {?MODULE, receiver, Ref, Sock, true},
unlink(Receiver),
{ok, Receiver};
{'EXIT', Receiver, Reason} ->
{error, Reason}
end.
receiver_active(Receiver, Active)
when (Active =:= false) orelse
(Active =:= true) orelse
(Active =:= once) orelse
is_integer(Active) ->
Receiver ! {?MODULE, active, What}.
receiver_update(Receiver, What) ->
Ref = make_ref(),
Receiver ! {?MODULE, receiver_update, Ref, What},
receive
{?MODULE, receiver_upadate, Ref, Result} ->
Result
end.
-record(receiver, {sock :: socket:socket(),
ctrl :: pid(),
num_packages :: infinity | non_neg_ineteger()}).
receiver_init(Pid, Ref) ->
receive
{?MODULE, receiver, Ref, stop} ->
i("received stop"),
exit(normal);
{?MODULE, receiver, Ref, Sock, InitialMode} ->
i("received socket: ~p", [Sock]),
NumPackages = mode2pkgcount(InitialMode),
receiver(#receiver{sock = Sock, ctrl = Pid})
end.
mode2pkgcount(true) ->
infinity;
mode2pkgcount(false) ->
0;
mode2pkgcount(N) when is_integer(N) andalso (N >= 0) ->
N.
receiver(#receiver{num_packages = 0} = State) ->
receive
{?MODULE, active, false} ->
receiver(State);
{?MODULE, active, true} ->
receiver(State#receiver{num_packages = infinity});
{?MODULE, active, once} ->
receiver(State#receiver{num_packages = 1});
{?MODULE, active, N} when (N > 0) ->
receiver(State#receiver{num_packages = N})
end;
receiver(#receiver{num_packages = N, sock = Sock, ctrl = Pid} = State) ->
case socket:recv(Sock, 0) of
{ok, Package} when size(Package) > 0 ->
Pid ! {tcp, Sock, Packege},
case next_active(N) of
0 ->
Pid ! {tcp_passive, Sock},
receiver(State#{num_packages = 0});
NextN ->
receiver(State#{num_packages = NextN})
end;
{ok, Package} when size(Package) =:= 0 ->
receiver(State);
{error, closed} ->
i("closed"),
Pid ! {tcp_closed, Sock},
exit(normal);
{error, Reason} ->
i("error: ~p", [Reason]),
Pid ! {tcp_error, Sock, Reason},
exit(normal)
end.
</code>
</section>
</erlref>
|