summaryrefslogtreecommitdiff
path: root/lib/kernel/doc/src/gen_udp.xml
blob: 042a73823da9b00d8487374400af7af795a98fd2 (plain)
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
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1997</year><year>2021</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>gen_udp</title>
    <prepared>tony@erix.ericsson.se</prepared>
    <docno></docno>
    <date>1997-12-03</date>
    <rev>A</rev>
  </header>
  <module since="">gen_udp</module>
  <modulesummary>Interface to UDP sockets.</modulesummary>
  <description>
    <p>This module provides functions for communicating
      with sockets using the UDP protocol.</p>

    <note>
      <p>
        Functions that create sockets can take an optional option;
        <c>{inet_backend,&nbsp;Backend}</c> that, if specified,
        has to be the first option.  This selects
        the implementation backend towards the platform's socket API.
      </p>
      <p>
        This is a <em>temporary</em> option
        that will be ignored in a future release.
      </p>
      <p>
        The default is <c>Backend&nbsp;=&nbsp;inet</c>
        that selects the traditional <c>inet_drv.c</c> driver.
        The other choice is <c>Backend&nbsp;=&nbsp;socket</c>
        that selects the new <seeerl marker="socket"><c>socket</c></seeerl>
        module and its NIF implementation.
      </p>
      <p>
        The system default can be changed when the node is started
        with the application <c>kernel</c>'s configuration variable
        <c>inet_backend</c>.
      </p>
      <p>For <c>gen_udp</c> with <c>inet_backend = socket</c> we have tried
      to be as "compatible" as possible which has sometimes been impossible.
      Here is a list of cases when the behaviour of inet-backend
      <c>inet</c> (default) and <c>socket</c> are different: </p>
      <list type="bulleted">
	<item>
	  <p>
	    The option
	    <seeerl marker="inet#option-read_packets">read_packets</seeerl>
	    is currently <em>ignored</em>.
	  </p>
	</item>
      </list>
    </note>

  </description>

  <datatypes>
    <datatype>
      <name name="option"/>
    </datatype>
    <datatype>
      <name name="option_name"/>
    </datatype>
    <datatype>
      <name name="open_option"/>
    </datatype>
    <datatype>
      <name>socket()</name>
      <desc>
        <p>As returned by
          <seemfa marker="#open/1"><c>open/1,2</c></seemfa>.</p>
      </desc>
    </datatype>
  </datatypes>

  <funcs>
    <func>
      <name name="close" arity="1" since=""/>
      <fsummary>Close a UDP socket.</fsummary>
      <desc>
        <p>Closes a UDP socket.</p>
      </desc>
    </func>

    <func>
      <name name="controlling_process" arity="2" since=""/>
      <fsummary>Change controlling process of a socket.</fsummary>
      <desc>
        <p>Assigns a new controlling process <c><anno>Pid</anno></c> to
          <c><anno>Socket</anno></c>. The controlling process is the process
          that receives messages from the socket. If called by any other
          process than the current controlling process,
          <c>{error, not_owner}</c> is returned. If the process identified
          by <c><anno>Pid</anno></c> is not an existing local pid,
          <c>{error, badarg}</c> is returned. <c>{error, badarg}</c> may also
          be returned in some cases when <c><anno>Socket</anno></c> is closed
          during the execution of this function.</p>
      </desc>
    </func>

    <func>
      <name name="connect" arity="2" since="OTP 24.3" anchor="connect-sockaddr"/>
      <fsummary>Connect a UDP port.</fsummary>
      <desc> 
	<p>Connecting a UDP socket only means storing the specified
	(destination) socket address, as specified by
	<c><anno>SockAddr</anno></c>, so that the
	system knows where to send data. </p>

	<p>This means that it is not necessary to specify the destination
	address when sending a datagram. That is, we can use
	<seeerl marker="#send-2"><c>send/2</c></seeerl>. </p>

	<p>It also means that the socket will only received data
	from this address. </p>

	<!--
	<p>Depending on the system implementation of connect and send,
	this could mean that sending becomes much faster. </p>
	-->
      </desc>
    </func>

    <func>
      <name name="connect" arity="3" since="OTP 24.3" anchor="connect-addr-port"/>
      <fsummary>Connect a UDP port.</fsummary>
      <desc>
	<p>Connecting a UDP socket only means storing the specified
	(destination) socket address, as specified by
	<c><anno>Address</anno></c> and <c><anno>Port</anno></c>,
	so that the system knows where to send data. </p>

	<p>This means that it is not necessary to specify the destination
	address when sending a datagram. That is, we can use
	<seeerl marker="#send-2"><c>send/2</c></seeerl>. </p>

	<p>It also means that the socket will only received data
	from this address. </p>

	<!--
	<p>Depending on the system implementation of connect and send,
	this could mean that sending becomes much faster. </p>
	-->
      </desc>
    </func>

    <func>
      <name name="open" arity="1" since=""/>
      <name name="open" arity="2" since=""/>
      <fsummary>Associate a UDP port number with the process calling it.</fsummary>
      <desc>
        <p>Associates a UDP port number (<c><anno>Port</anno></c>) with the
          calling process.</p>
        <p>The following options are available:</p>
        <taglist>
          <tag><c>list</c></tag>
	  <item>
	    <p>Received <c>Packet</c> is delivered as a list.</p>
	  </item>
          <tag><c>binary</c></tag>
	  <item>
	    <p>Received <c>Packet</c> is delivered as a binary.</p>
	  </item>
          <tag><c>{ip, Address}</c></tag>
	  <item>
	    <p>If the host has many network interfaces, this option
	    specifies which one to use.</p>
	  </item>
          <tag><c>{ifaddr, Address}</c></tag>
	  <item>
	    <p>Same as <c>{ip, Address}</c>. If the host has many
            network interfaces, this option specifies which one to
	    use.</p>
	    <p>However, if this instead is an
	    <seetype marker="socket#sockaddr_in">
	    <c>socket:sockaddr_in()</c></seetype>
	    or
	    <seetype marker="socket#sockaddr_in6">
	    <c>socket:sockaddr_in6()</c></seetype>
	    this takes precedence over any value previously set with the
	    <c>ip</c> options.
	    If the <c>ip</c> option comes <em>after</em> the <c>ifaddr</c> option,
	    it may be used to <em>update</em> its corresponding field of the
	    <c>ifaddr</c> option (the <c>addr</c> field). </p>
	  </item>
          <tag><c>{fd, integer() >= 0}</c></tag>
	  <item>
	    <p>If a socket has somehow been opened without using
            <c>gen_udp</c>, use this option to pass the file descriptor
            for it. If <c><anno>Port</anno></c> is not set to <c>0</c> and/or
            <c>{ip, ip_address()}</c> is combined with this option, the
            <c>fd</c> is bound to the specified interface and port after it is
            being opened. If these options are not specified, it is assumed that
	    the <c>fd</c> is already bound appropriately.</p>
	  </item>
          <tag><c>inet6</c></tag>
	  <item>
	    <p>Sets up the socket for IPv6.</p>
	  </item>
          <tag><c>inet</c></tag>
	  <item>
	    <p>Sets up the socket for IPv4.</p>
	  </item>
	  <tag><c>local</c></tag>
	  <item>
	    <p>Sets up a Unix Domain Socket. See
	    <seetype marker="inet#local_address">
	      <c>inet:local_address()</c>
	    </seetype>
	    </p>
          </item>
          <tag><c>{udp_module, module()}</c></tag>
	  <item>
	    <p>Overrides which callback module is used. Defaults to
	    <c>inet_udp</c> for IPv4 and <c>inet6_udp</c> for IPv6.</p>
	  </item>
          <tag><c>{multicast_if, Address}</c></tag>
	  <item>
	    <p>Sets the local device for a multicast socket.</p>
	  </item>
          <tag><c>{multicast_loop, true | false}</c></tag>
	  <item>
	    <p>When <c>true</c>, sent multicast packets are looped back to
	    the local sockets.</p>
	  </item>
          <tag><c>{multicast_ttl, Integer}</c></tag>
	  <item>
	    <p>Option <c>multicast_ttl</c> changes the time-to-live (TTL)
            for outgoing multicast datagrams to control the scope of the
            multicasts.</p>
            <p>Datagrams with a TTL of 1 are not forwarded beyond the local
	    network. Defaults to <c>1</c>.</p>
	  </item>
          <tag><c>{add_membership, {MultiAddress, InterfaceAddress}}</c></tag>
	  <item>
	    <p>Joins a multicast group.</p>
	  </item>
          <tag><c>{drop_membership, {MultiAddress, InterfaceAddress}}</c></tag>
	  <item>
	    <p>Leaves a multicast group.</p>
	  </item>
          <tag><c>Opt</c></tag>
	  <item>
	    <p>See
            <seemfa marker="inet#setopts/2"><c>inet:setopts/2</c></seemfa>.
	    </p>
	  </item>
        </taglist>
        <p>The returned socket <c><anno>Socket</anno></c> is used to send
          packets from this port with
          <seemfa marker="#send/4"><c>send/4</c></seemfa>.
          When UDP packets arrive
          at the opened port, if the socket is in an active mode, the packets
          are delivered as messages to the controlling process:</p>
        <code type="none">
{udp, Socket, IP, InPortNo, Packet} % Without ancillary data
{udp, Socket, IP, InPortNo, AncData, Packet} % With ancillary data
	</code>
	<p>
	  The message contains an <c>AncData</c> field
	  if any of the socket
	  <seetype marker="#option">options</seetype>
	  <seeerl marker="inet#option-recvtos"><c>recvtos</c></seeerl>,
	  <seeerl marker="inet#option-recvtclass"><c>recvtclass</c></seeerl>
	  or
	  <seeerl marker="inet#option-recvttl"><c>recvttl</c></seeerl>
	  are active, otherwise it does not.
	</p>
	<p>
	</p>
        <p>If the socket is not in an active mode, data can be
          retrieved through the
          <seemfa marker="#recv/2"><c>recv/2,3</c></seemfa> calls.
          Notice that arriving UDP packets that are longer than
          the receive buffer option specifies can be truncated
          without warning.</p>
        <p>When a socket in <c>{active, N}</c> mode (see
          <seemfa marker="inet#setopts/2"><c>inet:setopts/2</c></seemfa>
          for details), transitions to passive (<c>{active, false}</c>) mode,
          the controlling process is notified by a message of the following
          form:</p>
        <code type="none">
{udp_passive, Socket}</code>
        <p><c>IP</c> and <c>InPortNo</c> define the address from which
          <c>Packet</c> comes. <c>Packet</c> is a list of bytes if
          option <c>list</c> is specified. <c>Packet</c> is a
          binary if option <c>binary</c> is specified.</p>
        <p>Default value for the receive buffer option is
          <c>{recbuf, 8192}</c>.</p>
        <p>If <c><anno>Port</anno> == 0</c>, the underlying OS assigns a free
          UDP port, use
          <seemfa marker="inet#port/1"><c>inet:port/1</c></seemfa>
          to retrieve it.</p>
      </desc>
    </func>

    <func>
      <name name="recv" arity="2" since=""/>
      <name name="recv" arity="3" since=""/>
      <fsummary>Receive a packet from a passive socket.</fsummary>
      <desc>
        <p>
	  Receives a packet from a socket in passive mode. Optional parameter
          <c><anno>Timeout</anno></c> specifies a time-out in milliseconds.
          Defaults to <c>infinity</c>.
	</p>
	<p>
	  If any of the socket
	  <seetype marker="#option">options</seetype>
	  <seeerl marker="inet#option-recvtos"><c>recvtos</c></seeerl>,
	  <seeerl marker="inet#option-recvtclass"><c>recvtclass</c></seeerl>
	  or
	  <seeerl marker="inet#option-recvttl"><c>recvttl</c></seeerl>
	  are active, the <c><anno>RecvData</anno></c> tuple contains an
	  <c><anno>AncData</anno></c> field,
	  otherwise it does not.
	</p>
      </desc>
    </func>

    <func>
      <name name="send" arity="2" since="OTP 24.3" anchor="send-2"/>
      <fsummary>Send a packet on a connected socket.</fsummary>
      <desc>
        <p>
	  Sends a packet on a connected socket
	  (see
	  <seeerl marker="#connect-sockaddr"><c>connect/2</c></seeerl>
	  and
	  <seeerl marker="#connect-addr-port"><c>connect/3</c></seeerl>). </p>
      </desc>
    </func>

    <func>
      <name name="send" arity="3" clause_i="1" anchor="send-3" since="OTP 22.1"/>
      <fsummary>Send a packet.</fsummary>
      <desc>
        <p>
	  Sends a packet to the specified <c><anno>Destination</anno></c>.
	</p>
	<p>
	  This function is equivalent to
	  <seeerl marker="#send-4-AncData"><c>send(<anno>Socket</anno>, <anno>Destination</anno>, [], <anno>Packet</anno>)</c></seeerl>.
	</p>
      </desc>
    </func>

    <func>
      <name name="send" arity="4" clause_i="1" anchor="send-4-1" since=""/>
      <fsummary>Send a packet.</fsummary>
      <desc>
        <p>
	  Sends a packet to the specified <c><anno>Host</anno></c>
	  and <c><anno>Port</anno></c>.
	</p>
	<p>
	  This clause is equivalent to
	  <seemfa marker="#send/5"><c>send(<anno>Socket</anno>, <anno>Host</anno>, <anno>Port</anno>, [], <anno>Packet</anno>)</c></seemfa>.
	</p>
      </desc>
    </func>

    <func>
      <name name="send" arity="4" clause_i="2" anchor="send-4-AncData" since="OTP 22.1"/>
      <fsummary>Send a packet.</fsummary>
      <desc>
        <p>
	  Sends a packet to the specified <c><anno>Destination</anno></c>
	  with ancillary data <c><anno>AncData</anno></c>.
	</p>
	<note>
	  <p>
	    The ancillary data <c><anno>AncData</anno></c>
	    contains options that for this single message
	    override the default options for the socket,
	    an operation that may not be supported on all platforms,
	    and if so return <c>{error, einval}</c>.
	    Using more than one of an ancillary data item type
	    may also not be supported.
	    <c><anno>AncData</anno> =:= []</c> is always supported.
	  </p>
	</note>
      </desc>
    </func>

    <func>
      <name name="send" arity="4" clause_i="3" anchor="send-4-3" since="OTP 22.1"/>
      <fsummary>Send a packet.</fsummary>
      <desc>
        <p>
	  Sends a packet to the specified <c><anno>Destination</anno></c>.
	  Since <c><anno>Destination</anno></c> is complete,
	  <c><anno>PortZero</anno></c> is redundant and has to be <c>0</c>.
	</p>
	<p>
	  This is a legacy clause mostly for
	  <c><anno>Destination</anno> = {local, Binary}</c>
	  where <c><anno>PortZero</anno></c> is superfluous.
	  It is equivalent to
	  <seeerl marker="#send-4-AncData"><c>send(<anno>Socket</anno>, <anno>Destination</anno>, [], <anno>Packet</anno>)</c></seeerl>, the clause right above here.
	</p>
      </desc>
    </func>

    <func>
      <name name="send" arity="5" since="OTP 22.1"/>
      <fsummary>Send a packet.</fsummary>
      <desc>
        <p>
	  Sends a packet to the specified <c><anno>Host</anno></c>
	  and <c><anno>Port</anno></c>,
	  with ancillary data <c><anno>AncData</anno></c>.
	</p>
	<p>
	  Argument <c><anno>Host</anno></c> can be
	  a hostname or a socket address,
	  and <c><anno>Port</anno></c> can be a port number
	  or a service name atom.
	  These are resolved into a <c>Destination</c> and after that
	  this function is equivalent to
	  <seeerl marker="#send-4-AncData"><c>send(<anno>Socket</anno>, Destination, <anno>AncData</anno>, <anno>Packet</anno>)</c></seeerl>, read there about ancillary data.
	</p>
      </desc>
    </func>
  </funcs>
</erlref>