PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/kernel/doc/src/inet.xml

https://github.com/cobusc/otp
XML | 1439 lines | 1402 code | 37 blank | 0 comment | 0 complexity | e20c312f7505653a4dfb06175d9d1025 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE erlref SYSTEM "erlref.dtd">
  3. <erlref>
  4. <header>
  5. <copyright>
  6. <year>1997</year><year>2018</year>
  7. <holder>Ericsson AB. All Rights Reserved.</holder>
  8. </copyright>
  9. <legalnotice>
  10. Licensed under the Apache License, Version 2.0 (the "License");
  11. you may not use this file except in compliance with the License.
  12. You may obtain a copy of the License at
  13. http://www.apache.org/licenses/LICENSE-2.0
  14. Unless required by applicable law or agreed to in writing, software
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. </legalnotice>
  20. <title>inet</title>
  21. <prepared>bjorn@erix.ericsson.se</prepared>
  22. <docno></docno>
  23. <date>1998-02-04</date>
  24. <rev>A</rev>
  25. </header>
  26. <module>inet</module>
  27. <modulesummary>Access to TCP/IP protocols.</modulesummary>
  28. <description>
  29. <p>This module provides access to TCP/IP protocols.</p>
  30. <p>See also
  31. <seealso marker="erts:inet_cfg">ERTS User's Guide:
  32. Inet Configuration</seealso> for more information about how to
  33. configure an Erlang runtime system for IP communication.</p>
  34. <p>The following two Kernel configuration parameters affect the
  35. behavior of all sockets opened on an Erlang node:</p>
  36. <list type="bulleted">
  37. <item><p><c>inet_default_connect_options</c> can contain a list of
  38. default options used for all sockets returned when doing
  39. <c>connect</c>.</p></item>
  40. <item><p><c>inet_default_listen_options</c> can contain a list of
  41. default options used when issuing a <c>listen</c> call.</p></item>
  42. </list>
  43. <p>When <c>accept</c> is issued, the values of the listening socket options
  44. are inherited. No such application variable is therefore needed for
  45. <c>accept</c>.</p>
  46. <p>Using the Kernel configuration parameters above, one
  47. can set default options for all TCP sockets on a node, but use this
  48. with care. Options such as <c>{delay_send,true}</c> can be
  49. specified in this way. The following is an example of starting an Erlang
  50. node with all sockets using delayed send:</p>
  51. <pre>
  52. $ <input>erl -sname test -kernel \</input>
  53. <input>inet_default_connect_options '[{delay_send,true}]' \</input>
  54. <input>inet_default_listen_options '[{delay_send,true}]'</input></pre>
  55. <p>Notice that default option <c>{active, true}</c>
  56. cannot be changed, for internal reasons.</p>
  57. <p>Addresses as inputs to functions can be either a string or a
  58. tuple. For example, the IP address 150.236.20.73 can be passed to
  59. <c>gethostbyaddr/1</c>, either as string <c>"150.236.20.73"</c>
  60. or as tuple <c>{150, 236, 20, 73}</c>.</p>
  61. <p><em>IPv4 address examples:</em></p>
  62. <code type="none">
  63. Address ip_address()
  64. ------- ------------
  65. 127.0.0.1 {127,0,0,1}
  66. 192.168.42.2 {192,168,42,2}</code>
  67. <p><em>IPv6 address examples:</em></p>
  68. <code type="none">
  69. Address ip_address()
  70. ------- ------------
  71. ::1 {0,0,0,0,0,0,0,1}
  72. ::192.168.42.2 {0,0,0,0,0,0,(192 bsl 8) bor 168,(42 bsl 8) bor 2}
  73. ::FFFF:192.168.42.2
  74. {0,0,0,0,0,16#FFFF,(192 bsl 8) bor 168,(42 bsl 8) bor 2}
  75. 3ffe:b80:1f8d:2:204:acff:fe17:bf38
  76. {16#3ffe,16#b80,16#1f8d,16#2,16#204,16#acff,16#fe17,16#bf38}
  77. fe80::204:acff:fe17:bf38
  78. {16#fe80,0,0,0,0,16#204,16#acff,16#fe17,16#bf38}</code>
  79. <p>Function
  80. <seealso marker="#parse_address/1"><c>parse_address/1</c></seealso>
  81. can be useful:</p>
  82. <pre>
  83. 1> <input>inet:parse_address("192.168.42.2").</input>
  84. {ok,{192,168,42,2}}
  85. 2> <input>inet:parse_address("::FFFF:192.168.42.2").</input>
  86. {ok,{0,0,0,0,0,65535,49320,10754}}</pre>
  87. </description>
  88. <datatypes>
  89. <datatype>
  90. <name name="hostent"/>
  91. <desc>
  92. <p>The record is defined in the Kernel include file
  93. <c>"inet.hrl"</c>.</p>
  94. <p>Add the following directive to the module:</p>
  95. <code>
  96. -include_lib("kernel/include/inet.hrl").</code>
  97. </desc>
  98. </datatype>
  99. <datatype>
  100. <name name="hostname"/>
  101. </datatype>
  102. <datatype>
  103. <name name="ip_address"/>
  104. </datatype>
  105. <datatype>
  106. <name name="ip4_address"/>
  107. </datatype>
  108. <datatype>
  109. <name name="ip6_address"/>
  110. </datatype>
  111. <datatype>
  112. <name name="port_number"/>
  113. </datatype>
  114. <datatype>
  115. <name name="local_address"/>
  116. <desc>
  117. <p>
  118. This address family only works on Unix-like systems.
  119. </p>
  120. <p>
  121. <c><anno>File</anno></c> is normally a file pathname
  122. in a local filesystem. It is limited in length by the
  123. operating system, traditionally to 108 bytes.
  124. </p>
  125. <p>
  126. A <c>binary()</c> is passed as is to the operating system,
  127. but a <c>string()</c> is encoded according to the
  128. <seealso marker="file#native_name_encoding/0">
  129. system filename encoding mode.
  130. </seealso>
  131. </p>
  132. <p>
  133. Other addresses are possible, for example Linux implements
  134. "Abstract Addresses". See the documentation for
  135. Unix Domain Sockets on your system,
  136. normally <c>unix</c> in manual section 7.
  137. </p>
  138. <p>
  139. In most API functions where you can use
  140. this address family the port number must be <c>0</c>.
  141. </p>
  142. </desc>
  143. </datatype>
  144. <datatype>
  145. <name name="socket_address"/>
  146. </datatype>
  147. <datatype>
  148. <name name="socket_getopt"/>
  149. </datatype>
  150. <datatype>
  151. <name name="socket_setopt"/>
  152. </datatype>
  153. <datatype>
  154. <name name="returned_non_ip_address"/>
  155. <desc>
  156. <p>
  157. Addresses besides
  158. <seealso marker="#type-ip_address">
  159. <c>ip_address()</c>
  160. </seealso>
  161. ones that are returned from socket API functions.
  162. See in particular
  163. <seealso marker="#type-local_address">
  164. <c>local_address()</c>.
  165. </seealso>
  166. The <c>unspec</c> family corresponds to AF_UNSPEC and can
  167. occur if the other side has no socket address.
  168. The <c>undefined</c> family can only occur in the unlikely
  169. event of an address family that the VM does not recognize.
  170. </p>
  171. </desc>
  172. </datatype>
  173. <datatype>
  174. <name name="posix"/>
  175. <desc>
  176. <p>An atom that is named from the POSIX error codes used in Unix,
  177. and in the runtime libraries of most C compilers. See section
  178. <seealso marker="#error_codes">POSIX Error Codes</seealso>.</p>
  179. </desc>
  180. </datatype>
  181. <datatype>
  182. <name>socket()</name>
  183. <desc>
  184. <p>See
  185. <seealso marker="gen_tcp#type-socket"><c>gen_tcp:type-socket</c></seealso>
  186. and
  187. <seealso marker="gen_udp#type-socket"><c>gen_udp:type-socket</c></seealso>.
  188. </p>
  189. </desc>
  190. </datatype>
  191. <datatype>
  192. <name name="address_family"/>
  193. </datatype>
  194. <datatype>
  195. <name name="socket_protocol"/>
  196. </datatype>
  197. </datatypes>
  198. <funcs>
  199. <func>
  200. <name name="close" arity="1"/>
  201. <fsummary>Close a socket of any type.</fsummary>
  202. <desc>
  203. <p>Closes a socket of any type.</p>
  204. </desc>
  205. </func>
  206. <func>
  207. <name name="format_error" arity="1"/>
  208. <fsummary>Return a descriptive string for an error reason.</fsummary>
  209. <desc>
  210. <p>Returns a diagnostic error string. For possible POSIX values and
  211. corresponding strings, see section
  212. <seealso marker="#error_codes">POSIX Error Codes</seealso>.</p>
  213. </desc>
  214. </func>
  215. <func>
  216. <name name="get_rc" arity="0"/>
  217. <fsummary>Return a list of IP configuration parameters.</fsummary>
  218. <desc>
  219. <p>
  220. Returns the state of the <c>Inet</c> configuration database in
  221. form of a list of recorded configuration parameters. For more
  222. information, see <seealso marker="erts:inet_cfg">ERTS User's Guide:
  223. Inet Configuration</seealso>.
  224. </p>
  225. <p>
  226. Only actual parameters with other than default values
  227. are returned, for example not directives that specify
  228. other sources for configuration parameters nor
  229. directives that clear parameters.
  230. </p>
  231. </desc>
  232. </func>
  233. <func>
  234. <name name="getaddr" arity="2"/>
  235. <fsummary>Return the IP address for a host.</fsummary>
  236. <desc>
  237. <p>Returns the IP address for <c><anno>Host</anno></c> as a tuple of
  238. integers. <c><anno>Host</anno></c> can be an IP address, a single
  239. hostname, or a fully qualified hostname.</p>
  240. </desc>
  241. </func>
  242. <func>
  243. <name name="getaddrs" arity="2"/>
  244. <fsummary>Return the IP addresses for a host.</fsummary>
  245. <desc>
  246. <p>Returns a list of all IP addresses for <c><anno>Host</anno></c>.
  247. <c><anno>Host</anno></c> can be an IP address, a single hostname, or
  248. a fully qualified hostname.</p>
  249. </desc>
  250. </func>
  251. <func>
  252. <name name="gethostbyaddr" arity="1"/>
  253. <fsummary>Return a hostent record for the host with the specified
  254. address.</fsummary>
  255. <desc>
  256. <p>Returns a <c>hostent</c> record for the host with the specified
  257. address.</p></desc>
  258. </func>
  259. <func>
  260. <name name="gethostbyname" arity="1"/>
  261. <fsummary>Return a hostent record for the host with the specified name.
  262. </fsummary>
  263. <desc>
  264. <p>Returns a <c>hostent</c> record for the host with the specified
  265. hostname.</p>
  266. <p>If resolver option <c>inet6</c> is <c>true</c>,
  267. an IPv6 address is looked up.</p>
  268. </desc>
  269. </func>
  270. <func>
  271. <name name="gethostbyname" arity="2"/>
  272. <fsummary>Return a hostent record for the host with the specified name.
  273. </fsummary>
  274. <desc>
  275. <p>Returns a <c>hostent</c> record for the host with the specified
  276. name, restricted to the specified address family.</p>
  277. </desc>
  278. </func>
  279. <func>
  280. <name name="gethostname" arity="0"/>
  281. <fsummary>Return the local hostname.</fsummary>
  282. <desc>
  283. <p>Returns the local hostname. Never fails.</p>
  284. </desc>
  285. </func>
  286. <func>
  287. <name name="getifaddrs" arity="0"/>
  288. <fsummary>Return a list of interfaces and their addresses.</fsummary>
  289. <desc>
  290. <p>Returns a list of 2-tuples containing interface names and the
  291. interface addresses. <c><anno>Ifname</anno></c> is a Unicode string.
  292. <c><anno>Hwaddr</anno></c> is hardware dependent, for example, on
  293. Ethernet interfaces
  294. it is the 6-byte Ethernet address (MAC address (EUI-48 address)).</p>
  295. <p>The tuples <c>{addr,<anno>Addr</anno>}</c>, <c>{netmask,_}</c>, and
  296. <c>{broadaddr,_}</c> are repeated in the result list if the interface
  297. has multiple addresses. If you come across an interface with
  298. multiple <c>{flag,_}</c> or <c>{hwaddr,_}</c> tuples, you have
  299. a strange interface or possibly a bug in this function. The tuple
  300. <c>{flag,_}</c> is mandatory, all others are optional.</p>
  301. <p>Do not rely too much on the order of <c><anno>Flag</anno></c> atoms
  302. or <c><anno>Ifopt</anno></c> tuples. There are however some rules:</p>
  303. <list type="bulleted">
  304. <item><p>Immediately after
  305. <c>{addr,_}</c> follows <c>{netmask,_}</c>.</p></item>
  306. <item><p>Immediately thereafter follows <c>{broadaddr,_}</c> if flag
  307. <c>broadcast</c> is <em>not</em> set and flag
  308. <c>pointtopoint</c> <em>is</em> set.</p></item>
  309. <item><p>Any <c>{netmask,_}</c>, <c>{broadaddr,_}</c>, or
  310. <c>{dstaddr,_}</c> tuples that follow an <c>{addr,_}</c>
  311. tuple concerns that address.</p></item>
  312. </list>
  313. <p>The tuple <c>{hwaddr,_}</c> is not returned on Solaris, as the
  314. hardware address historically belongs to the link layer and only
  315. the superuser can read such addresses.</p>
  316. <warning>
  317. <p>On Windows, the data is fetched from different OS API functions,
  318. so the <c><anno>Netmask</anno></c> and <c><anno>Broadaddr</anno></c>
  319. values can be calculated, just as some <c><anno>Flag</anno></c>
  320. values. Report flagrant bugs.</p>
  321. </warning>
  322. </desc>
  323. </func>
  324. <func>
  325. <name name="getopts" arity="2"/>
  326. <fsummary>Get one or more options for a socket.</fsummary>
  327. <desc>
  328. <p>Gets one or more options for a socket. For a list of available
  329. options, see
  330. <seealso marker="#setopts/2"><c>setopts/2</c></seealso>.</p>
  331. <p>The number of elements in the returned
  332. <c><anno>OptionValues</anno></c>
  333. list does not necessarily correspond to the number of options
  334. asked for. If the operating system fails to support an option,
  335. it is left out in the returned list. An error tuple is returned
  336. only when getting options for the socket is impossible (that is,
  337. the socket is closed or the buffer size in a raw request
  338. is too large). This behavior is kept for backward
  339. compatibility reasons.</p>
  340. <p>A raw option request
  341. <c>RawOptReq = {raw, Protocol, OptionNum, ValueSpec}</c>
  342. can be used to get information about
  343. socket options not (explicitly) supported by the emulator. The
  344. use of raw socket options makes the code non-portable, but
  345. allows the Erlang programmer to take advantage of unusual features
  346. present on the current platform.</p>
  347. <p><c>RawOptReq</c> consists of tag <c>raw</c> followed
  348. by the protocol level, the option number, and either a binary
  349. or the size, in bytes, of the
  350. buffer in which the option value is to be stored. A binary is to be
  351. used when the underlying <c>getsockopt</c> requires <em>input</em>
  352. in the argument field. In this case, the binary size
  353. is to correspond to the required buffer
  354. size of the return value. The supplied values in a <c>RawOptReq</c>
  355. correspond to the second, third, and fourth/fifth parameters to the
  356. <c>getsockopt</c> call in the C socket API. The value stored
  357. in the buffer is returned as a binary <c>ValueBin</c>,
  358. where all values are coded in the native endianess.</p>
  359. <p>Asking for and inspecting raw socket options require low-level
  360. information about the current operating system and TCP stack.</p>
  361. <p><em>Example:</em></p>
  362. <p>Consider a Linux machine where option
  363. <c>TCP_INFO</c> can be used to collect TCP statistics
  364. for a socket. Assume you are interested in field
  365. <c>tcpi_sacked</c> of <c>struct tcp_info</c>
  366. filled in when asking for <c>TCP_INFO</c>. To be able to access
  367. this information, you need to know the following:</p>
  368. <list type="bulleted">
  369. <item>The numeric value of protocol level <c>IPPROTO_TCP</c></item>
  370. <item>The numeric value of option <c>TCP_INFO</c></item>
  371. <item>The size of <c>struct tcp_info</c></item>
  372. <item>The size and offset of the specific field</item>
  373. </list>
  374. <p>By inspecting the headers or writing a small C program, it is found
  375. that <c>IPPROTO_TCP</c> is 6, <c>TCP_INFO</c> is 11, the structure
  376. size is 92 (bytes), the offset of <c>tcpi_sacked</c> is 28 bytes,
  377. and the value is a 32-bit integer. The following code can be used
  378. to retrieve the value:</p>
  379. <code type="none"><![CDATA[
  380. get_tcpi_sacked(Sock) ->
  381. {ok,[{raw,_,_,Info}]} = inet:getopts(Sock,[{raw,6,11,92}]),
  382. <<_:28/binary,TcpiSacked:32/native,_/binary>> = Info,
  383. TcpiSacked.]]></code>
  384. <p>Preferably, you would check the machine type, the operating system,
  385. and the Kernel version before executing anything similar to
  386. this code.</p>
  387. </desc>
  388. </func>
  389. <func>
  390. <name name="getstat" arity="1"/>
  391. <name name="getstat" arity="2"/>
  392. <fsummary>Get one or more statistic options for a socket.</fsummary>
  393. <type name="stat_option"/>
  394. <desc>
  395. <p>Gets one or more statistic options for a socket.</p>
  396. <p><c>getstat(<anno>Socket</anno>)</c> is equivalent to
  397. <c>getstat(<anno>Socket</anno>, [recv_avg, recv_cnt, recv_dvi,
  398. recv_max, recv_oct, send_avg, send_cnt, send_dvi, send_max,
  399. send_oct])</c>.</p>
  400. <p>The following options are available:</p>
  401. <taglist>
  402. <tag><c>recv_avg</c></tag>
  403. <item>
  404. <p>Average size of packets, in bytes, received by the socket.</p>
  405. </item>
  406. <tag><c>recv_cnt</c></tag>
  407. <item>
  408. <p>Number of packets received by the socket.</p>
  409. </item>
  410. <tag><c>recv_dvi</c></tag>
  411. <item>
  412. <p>Average packet size deviation, in bytes, received by the socket.</p>
  413. </item>
  414. <tag><c>recv_max</c></tag>
  415. <item>
  416. <p>Size of the largest packet, in bytes, received by the socket.</p>
  417. </item>
  418. <tag><c>recv_oct</c></tag>
  419. <item>
  420. <p>Number of bytes received by the socket.</p>
  421. </item>
  422. <tag><c>send_avg</c></tag>
  423. <item>
  424. <p>Average size of packets, in bytes, sent from the socket.</p>
  425. </item>
  426. <tag><c>send_cnt</c></tag>
  427. <item>
  428. <p>Number of packets sent from the socket.</p>
  429. </item>
  430. <tag><c>send_dvi</c></tag>
  431. <item>
  432. <p>Average packet size deviation, in bytes, sent from the socket.</p>
  433. </item>
  434. <tag><c>send_max</c></tag>
  435. <item>
  436. <p>Size of the largest packet, in bytes, sent from the socket.</p>
  437. </item>
  438. <tag><c>send_oct</c></tag>
  439. <item>
  440. <p>Number of bytes sent from the socket.</p>
  441. </item>
  442. </taglist>
  443. </desc>
  444. </func>
  445. <func>
  446. <name name="i" arity="0" />
  447. <name name="i" arity="1" />
  448. <name name="i" arity="2" />
  449. <fsummary>Displays information and statistics about sockets on the terminal</fsummary>
  450. <desc>
  451. <p>
  452. Lists all TCP, UDP and SCTP sockets, including those that the Erlang runtime system uses as well as
  453. those created by the application.
  454. </p>
  455. <p>
  456. The following options are available:
  457. </p>
  458. <taglist>
  459. <tag><c>port</c></tag>
  460. <item>
  461. <p>The internal index of the port.</p>
  462. </item>
  463. <tag><c>module</c></tag>
  464. <item>
  465. <p>The callback module of the socket.</p>
  466. </item>
  467. <tag><c>recv</c></tag>
  468. <item>
  469. <p>Number of bytes received by the socket.</p>
  470. </item>
  471. <tag><c>sent</c></tag>
  472. <item>
  473. <p>Number of bytes sent from the socket.</p>
  474. </item>
  475. <tag><c>owner</c></tag>
  476. <item>
  477. <p>The socket owner process.</p>
  478. </item>
  479. <tag><c>local_address</c></tag>
  480. <item>
  481. <p>The local address of the socket.</p>
  482. </item>
  483. <tag><c>foreign_address</c></tag>
  484. <item>
  485. <p>The address and port of the other end of the connection.</p>
  486. </item>
  487. <tag><c>state</c></tag>
  488. <item>
  489. <p>The connection state.</p>
  490. </item>
  491. <tag><c>type</c></tag>
  492. <item>
  493. <p>STREAM or DGRAM or SEQPACKET.</p>
  494. </item>
  495. </taglist>
  496. </desc>
  497. </func>
  498. <func>
  499. <name name="ntoa" arity="1" />
  500. <fsummary>Convert IPv6/IPV4 address to ASCII.</fsummary>
  501. <desc>
  502. <p>Parses an
  503. <seealso marker="#type-ip_address"><c>ip_address()</c></seealso>
  504. and returns an IPv4 or IPv6 address string.</p>
  505. </desc>
  506. </func>
  507. <func>
  508. <name name="parse_address" arity="1" />
  509. <fsummary>Parse an IPv4 or IPv6 address.</fsummary>
  510. <desc>
  511. <p>Parses an IPv4 or IPv6 address string and returns an
  512. <seealso marker="#type-ip4_address"><c>ip4_address()</c></seealso> or
  513. <seealso marker="#type-ip6_address"><c>ip6_address()</c></seealso>.
  514. Accepts a shortened IPv4 address string.</p>
  515. </desc>
  516. </func>
  517. <func>
  518. <name name="parse_ipv4_address" arity="1" />
  519. <fsummary>Parse an IPv4 address.</fsummary>
  520. <desc>
  521. <p>Parses an IPv4 address string and returns an
  522. <seealso marker="#type-ip4_address"><c>ip4_address()</c></seealso>.
  523. Accepts a shortened IPv4 address string.</p>
  524. </desc>
  525. </func>
  526. <func>
  527. <name name="parse_ipv4strict_address" arity="1" />
  528. <fsummary>Parse an IPv4 address strict.</fsummary>
  529. <desc>
  530. <p>Parses an IPv4 address string containing four fields, that is,
  531. <em>not</em> shortened, and returns an
  532. <seealso marker="#type-ip4_address"><c>ip4_address()</c></seealso>.
  533. </p>
  534. </desc>
  535. </func>
  536. <func>
  537. <name name="parse_ipv6_address" arity="1" />
  538. <fsummary>Parse an IPv6 address.</fsummary>
  539. <desc>
  540. <p>Parses an IPv6 address string and returns an
  541. <seealso marker="#type-ip6_address"><c>ip6_address()</c></seealso>.
  542. If an IPv4 address string is specified, an IPv4-mapped IPv6 address
  543. is returned.</p>
  544. </desc>
  545. </func>
  546. <func>
  547. <name name="parse_ipv6strict_address" arity="1" />
  548. <fsummary>Parse an IPv6 address strict.</fsummary>
  549. <desc>
  550. <p>Parses an IPv6 address string and returns an
  551. <seealso marker="#type-ip6_address"><c>ip6_address()</c></seealso>.
  552. Does <em>not</em> accept IPv4 addresses.</p>
  553. </desc>
  554. </func>
  555. <func>
  556. <name name="ipv4_mapped_ipv6_address" arity="1" />
  557. <fsummary>Convert to and from IPv4-mapped IPv6 address.</fsummary>
  558. <desc>
  559. <p>
  560. Convert an IPv4 address to an IPv4-mapped IPv6 address
  561. or the reverse. When converting from an IPv6 address
  562. all but the 2 low words are ignored so this function also
  563. works on some other types of addresses than IPv4-mapped.
  564. </p>
  565. </desc>
  566. </func>
  567. <func>
  568. <name name="parse_strict_address" arity="1" />
  569. <fsummary>Parse an IPv4 or IPv6 address strict.</fsummary>
  570. <desc>
  571. <p>Parses an IPv4 or IPv6 address string and returns an
  572. <seealso marker="#type-ip4_address"><c>ip4_address()</c></seealso> or
  573. <seealso marker="#type-ip6_address"><c>ip6_address()</c></seealso>.
  574. Does <em>not</em> accept a shortened IPv4 address string.</p>
  575. </desc>
  576. </func>
  577. <func>
  578. <name name="peername" arity="1"/>
  579. <fsummary>Return the address and port for the other end of a connection.
  580. </fsummary>
  581. <desc>
  582. <p>Returns the address and port for the other end of a connection.</p>
  583. <p>Notice that for SCTP sockets, this function returns only
  584. one of the peer addresses of the socket. Function
  585. <seealso marker="#peernames/1"><c>peernames/1,2</c></seealso>
  586. returns all.</p>
  587. </desc>
  588. </func>
  589. <func>
  590. <name name="peernames" arity="1"/>
  591. <fsummary>Return all address/port numbers for the other end of a
  592. connection.</fsummary>
  593. <desc>
  594. <p>Equivalent to
  595. <seealso marker="#peernames/2"><c>peernames(<anno>Socket</anno>, 0)</c></seealso>.
  596. </p>
  597. <p>Notice that the behavior of this function for an SCTP
  598. one-to-many style socket is not defined by the
  599. <url href="http://tools.ietf.org/html/draft-ietf-tsvwg-sctpsocket-13">SCTP Sockets API Extensions</url>.</p>
  600. </desc>
  601. </func>
  602. <func>
  603. <name name="peernames" arity="2"/>
  604. <fsummary>Return all address/port numbers for the other end of a
  605. connection.</fsummary>
  606. <desc>
  607. <p>Returns a list of all address/port number pairs for the other end
  608. of an association <c><anno>Assoc</anno></c> of a socket.</p>
  609. <p>This function can return multiple addresses for multihomed
  610. sockets, such as SCTP sockets. For other sockets it
  611. returns a one-element list.</p>
  612. <p>Notice that parameter <c><anno>Assoc</anno></c> is by the
  613. <url href="http://tools.ietf.org/html/draft-ietf-tsvwg-sctpsocket-13">SCTP Sockets API Extensions</url>
  614. defined to be ignored for
  615. one-to-one style sockets. What the special value <c>0</c>
  616. means, hence its behavior for one-to-many style sockets,
  617. is unfortunately undefined.</p>
  618. </desc>
  619. </func>
  620. <func>
  621. <name name="port" arity="1"/>
  622. <fsummary>Return the local port number for a socket.</fsummary>
  623. <desc>
  624. <p>Returns the local port number for a socket.</p>
  625. </desc>
  626. </func>
  627. <func>
  628. <name name="setopts" arity="2"/>
  629. <fsummary>Set one or more options for a socket.</fsummary>
  630. <desc>
  631. <p>Sets one or more options for a socket.</p>
  632. <p>The following options are available:</p>
  633. <taglist>
  634. <tag><c>{active, true | false | once | N}</c></tag>
  635. <item>
  636. <p>If the value is <c>true</c>, which is the default,
  637. everything received from the socket is sent as
  638. messages to the receiving process.</p>
  639. <p>If the value is <c>false</c> (passive mode), the process must
  640. explicitly receive incoming data by calling
  641. <seealso marker="gen_tcp#recv/2"><c>gen_tcp:recv/2,3</c></seealso>,
  642. <seealso marker="gen_udp#recv/2"><c>gen_udp:recv/2,3</c></seealso>,
  643. or <seealso marker="gen_sctp#recv/1"><c>gen_sctp:recv/1,2</c></seealso>
  644. (depending on the type of socket).</p>
  645. <p>If the value is <c>once</c> (<c>{active, once}</c>),
  646. <em>one</em> data message from the socket is sent
  647. to the process. To receive one more message,
  648. <c>setopts/2</c> must be called again with option
  649. <c>{active, once}</c>.</p>
  650. <p>If the value is an integer <c>N</c> in the range -32768 to 32767
  651. (inclusive), the value is added to the socket's count of data
  652. messages sent to the controlling process. A socket's default
  653. message count is <c>0</c>. If a negative value is specified, and
  654. its magnitude is equal to or greater than the socket's current
  655. message count, the socket's message count is set to <c>0</c>.
  656. Once the socket's message count reaches <c>0</c>, either because
  657. of sending
  658. received data messages to the process or by being explicitly set,
  659. the process is then notified by a special message, specific to
  660. the type of socket, that the socket has entered passive
  661. mode. Once the socket enters passive mode, to receive more
  662. messages <c>setopts/2</c> must be called again to set the
  663. socket back into an active mode.</p>
  664. <p>When using <c>{active, once}</c> or <c>{active, N}</c>, the
  665. socket changes behavior automatically when data is received.
  666. This can be confusing in combination with connection-oriented
  667. sockets (that is, <c>gen_tcp</c>), as a socket
  668. with <c>{active, false}</c> behavior reports closing
  669. differently than a socket with <c>{active, true}</c>
  670. behavior. To simplify programming, a socket where
  671. the peer closed, and this is detected while in
  672. <c>{active, false}</c> mode, still generates message
  673. <c>{tcp_closed,Socket}</c> when set to <c>{active, once}</c>,
  674. <c>{active, true}</c>, or <c>{active, N}</c> mode.
  675. It is therefore safe to assume that message
  676. <c>{tcp_closed,Socket}</c>, possibly followed by socket port
  677. termination (depending on option <c>exit_on_close</c>)
  678. eventually appears when a socket changes
  679. back and forth between <c>{active, true}</c> and
  680. <c>{active, false}</c> mode. However,
  681. <em>when</em> peer closing is detected it is all up to the
  682. underlying TCP/IP stack and protocol.</p>
  683. <p>Notice that <c>{active, true}</c> mode provides no flow
  684. control; a fast sender can easily overflow the
  685. receiver with incoming messages. The same is true for
  686. <c>{active, N}</c> mode, while the message count is greater
  687. than zero.</p>
  688. <p>Use active mode only if
  689. your high-level protocol provides its own flow control
  690. (for example, acknowledging received messages) or the
  691. amount of data exchanged is small. <c>{active, false}</c>
  692. mode, use of the <c>{active, once}</c> mode, or <c>{active, N}</c>
  693. mode with values of <c>N</c> appropriate for the application
  694. provides flow control. The other side cannot send
  695. faster than the receiver can read.</p>
  696. </item>
  697. <tag><c>{broadcast, Boolean}</c> (UDP sockets)</tag>
  698. <item>
  699. <p>Enables/disables permission to send broadcasts.</p>
  700. <marker id="option-buffer"></marker>
  701. </item>
  702. <tag><c>{buffer, Size}</c></tag>
  703. <item>
  704. <p>The size of the user-level software buffer used by
  705. the driver.
  706. Not to be confused with options <c>sndbuf</c>
  707. and <c>recbuf</c>, which correspond to the
  708. Kernel socket buffers. It is recommended
  709. to have <c>val(buffer) &gt;= max(val(sndbuf),val(recbuf))</c> to
  710. avoid performance issues because of unnecessary copying.
  711. <c>val(buffer)</c> is automatically set to the above
  712. maximum when values <c>sndbuf</c> or <c>recbuf</c> are set.
  713. However, as the sizes set for <c>sndbuf</c> and <c>recbuf</c>
  714. usually become larger, you are encouraged to use
  715. <seealso marker="#getopts/2"><c>getopts/2</c></seealso>
  716. to analyze the behavior of your operating system.</p>
  717. <p>Note that this is also the maximum amount of data that can be
  718. received from a single recv call. If you are using higher than
  719. normal MTU consider setting buffer higher.</p>
  720. </item>
  721. <tag><c>{delay_send, Boolean}</c></tag>
  722. <item>
  723. <p>Normally, when an Erlang process sends to a socket,
  724. the driver tries to send the data immediately. If that
  725. fails, the driver uses any means available to queue
  726. up the message to be sent whenever the operating system
  727. says it can handle it. Setting <c>{delay_send, true}</c>
  728. makes <em>all</em> messages queue up. The messages sent
  729. to the network are then larger but fewer.
  730. The option affects the scheduling of send
  731. requests versus Erlang processes instead of changing any
  732. real property of the socket. The option is
  733. implementation-specific. Defaults to <c>false</c>.</p>
  734. </item>
  735. <tag><c>{deliver, port | term}</c></tag>
  736. <item>
  737. <p>When <c>{active, true}</c>, data is delivered on the form
  738. <c>port</c> : <c>{S, {data, [H1,..Hsz | Data]}}</c> or
  739. <c>term</c> : <c>{tcp, S, [H1..Hsz | Data]}</c>.</p>
  740. </item>
  741. <tag><c>{dontroute, Boolean}</c></tag>
  742. <item>
  743. <p>Enables/disables routing bypass for outgoing messages.</p>
  744. </item>
  745. <tag><c>{exit_on_close, Boolean}</c></tag>
  746. <item>
  747. <p>This option is set to <c>true</c> by default.</p>
  748. <p>The only reason to set it to <c>false</c> is if you want
  749. to continue sending data to the socket after a close is
  750. detected, for example, if the peer uses
  751. <seealso marker="gen_tcp#shutdown/2"><c>gen_tcp:shutdown/2</c></seealso>
  752. to shut down the write side.</p>
  753. </item>
  754. <tag><c>{header, Size}</c></tag>
  755. <item>
  756. <p>This option is only meaningful if option <c>binary</c>
  757. was specified when the socket was created. If option
  758. <c>header</c> is specified, the first
  759. <c>Size</c> number bytes of data received from the socket
  760. are elements of a list, and the remaining data is
  761. a binary specified as the tail of the same list. For example,
  762. if <c>Size == 2</c>, the data received matches
  763. <c>[Byte1,Byte2|Binary]</c>.</p>
  764. </item>
  765. <tag><c>{high_msgq_watermark, Size}</c></tag>
  766. <item>
  767. <p>The socket message queue is set to a busy
  768. state when the amount of data on the message
  769. queue reaches this limit. Notice that this limit only
  770. concerns data that has not yet reached the ERTS internal
  771. socket implementation. Defaults to 8 kB.</p>
  772. <p>Senders of data to the socket are suspended if
  773. either the socket message queue is busy or the socket
  774. itself is busy.</p>
  775. <p>For more information, see options <c>low_msgq_watermark</c>,
  776. <c>high_watermark</c>, and <c>low_watermark</c>.</p>
  777. <p>Notice that distribution sockets disable the use of
  778. <c>high_msgq_watermark</c> and <c>low_msgq_watermark</c>.
  779. Instead use the
  780. <seealso marker="erts:erlang#system_info_dist_buf_busy_limit">distribution buffer busy limit</seealso>,
  781. which is a similar feature.</p>
  782. </item>
  783. <tag><c>{high_watermark, Size}</c> (TCP/IP sockets)</tag>
  784. <item>
  785. <p>The socket is set to a busy state when the amount
  786. of data queued internally by the ERTS socket implementation
  787. reaches this limit. Defaults to 8 kB.</p>
  788. <p>Senders of data to the socket are suspended if
  789. either the socket message queue is busy or the socket
  790. itself is busy.</p>
  791. <p>For more information, see options <c>low_watermark</c>,
  792. <c>high_msgq_watermark</c>, and <c>low_msqg_watermark</c>.</p>
  793. </item>
  794. <tag><c>{ipv6_v6only, Boolean}</c></tag>
  795. <item>
  796. <p>Restricts the socket to use only IPv6, prohibiting any
  797. IPv4 connections. This is only applicable for
  798. IPv6 sockets (option <c>inet6</c>).</p>
  799. <p>On most platforms this option must be set on the socket
  800. before associating it to an address. It is therefore only
  801. reasonable to specify it when creating the socket and not
  802. to use it when calling function
  803. (<seealso marker="#setopts/2"><c>setopts/2</c></seealso>)
  804. containing this description.</p>
  805. <p>The behavior of a socket with this option set to
  806. <c>true</c> is the only portable one. The original
  807. idea when IPv6 was new of using IPv6 for all traffic
  808. is now not recommended by FreeBSD (you can use
  809. <c>{ipv6_v6only,false}</c> to override the recommended
  810. system default value),
  811. forbidden by OpenBSD (the supported GENERIC kernel),
  812. and impossible on Windows (which has separate
  813. IPv4 and IPv6 protocol stacks). Most Linux distros
  814. still have a system default value of <c>false</c>.
  815. This policy shift among operating systems to
  816. separate IPv6 from IPv4 traffic has evolved, as
  817. it gradually proved hard and complicated to get
  818. a dual stack implementation correct and secure.</p>
  819. <p>On some platforms, the only allowed value for this option
  820. is <c>true</c>, for example, OpenBSD and Windows. Trying to set
  821. this option to <c>false</c>, when creating the socket, fails
  822. in this case.</p>
  823. <p>Setting this option on platforms where it does not exist
  824. is ignored. Getting this option with
  825. <seealso marker="#getopts/2"><c>getopts/2</c></seealso>
  826. returns no value, that is, the returned list does not contain an
  827. <c>{ipv6_v6only,_}</c> tuple. On Windows, the option
  828. does not exist, but it is emulated as a
  829. read-only option with value <c>true</c>.</p>
  830. <p>Therefore, setting this option to <c>true</c>
  831. when creating a socket never fails, except possibly on a
  832. platform where you
  833. have customized the kernel to only allow <c>false</c>,
  834. which can be doable (but awkward) on, for example, OpenBSD.</p>
  835. <p>If you read back the option value using
  836. <seealso marker="#getopts/2"><c>getopts/2</c></seealso>
  837. and get no value, the option does not exist in the host
  838. operating system. The behavior of both an IPv6 and an IPv4
  839. socket listening on the same port, and for an IPv6 socket
  840. getting IPv4 traffic is then no longer predictable.</p>
  841. </item>
  842. <tag><c>{keepalive, Boolean}</c>(TCP/IP sockets)</tag>
  843. <item>
  844. <p>Enables/disables periodic transmission on a connected
  845. socket when no other data is exchanged. If
  846. the other end does not respond, the connection is
  847. considered broken and an error message is sent to
  848. the controlling process. Defaults to <c>disabled</c>.</p>
  849. <marker id="option-linger"></marker>
  850. </item>
  851. <tag><c>{linger, {true|false, Seconds}}</c></tag>
  852. <item>
  853. <p>Determines the time-out, in seconds, for flushing unsent data
  854. in the <c>close/1</c> socket call. If the first component of
  855. the value tuple is <c>false</c>, the second is ignored. This
  856. means that <c>close/1</c> returns immediately, not waiting
  857. for data to be flushed. Otherwise, the second component is
  858. the flushing time-out, in seconds.</p>
  859. </item>
  860. <tag><c>{low_msgq_watermark, Size}</c></tag>
  861. <item>
  862. <p>If the socket message queue is in a busy state, the
  863. socket message queue is set in a not busy state when
  864. the amount of data queued in the message queue falls
  865. below this limit. Notice that this limit only concerns data
  866. that has not yet reached the ERTS internal socket
  867. implementation. Defaults to 4 kB.</p>
  868. <p>Senders that are suspended because of either a
  869. busy message queue or a busy socket are resumed
  870. when the socket message queue and the socket
  871. are not busy.</p>
  872. <p>For more information, see options <c>high_msgq_watermark</c>,
  873. <c>high_watermark</c>, and <c>low_watermark</c>.</p>
  874. <p>Notice that distribution sockets disable the use of
  875. <c>high_msgq_watermark</c> and <c>low_msgq_watermark</c>.
  876. Instead they use the
  877. <seealso marker="erts:erlang#system_info_dist_buf_busy_limit">distribution
  878. buffer busy limit</seealso>, which is a similar feature.</p>
  879. </item>
  880. <tag><c>{low_watermark, Size}</c> (TCP/IP sockets)</tag>
  881. <item>
  882. <p>If the socket is in a busy state, the socket is
  883. set in a not busy state when the amount of data
  884. queued internally by the ERTS socket implementation
  885. falls below this limit. Defaults to 4 kB.</p>
  886. <p>Senders that are suspended because of a
  887. busy message queue or a busy socket are resumed
  888. when the socket message queue and the socket are not busy.</p>
  889. <p>For more information, see options <c>high_watermark</c>,
  890. <c>high_msgq_watermark</c>, and <c>low_msgq_watermark</c>.</p>
  891. </item>
  892. <tag><c>{mode, Mode :: binary | list}</c></tag>
  893. <item>
  894. <p>Received <c>Packet</c> is delivered as defined by <c>Mode</c>.
  895. </p>
  896. </item>
  897. <tag><c>{netns, Namespace :: file:filename_all()}</c></tag>
  898. <item>
  899. <p>Sets a network namespace for the socket. Parameter
  900. <c>Namespace</c> is a filename defining the namespace, for
  901. example, <c>"/var/run/netns/example"</c>, typically created by
  902. command <c>ip netns add example</c>. This option must be used in
  903. a function call that creates a socket, that is,
  904. <seealso marker="gen_tcp#connect/3"><c>gen_tcp:connect/3,4</c></seealso>,
  905. <seealso marker="gen_tcp#listen/2"><c>gen_tcp:listen/2</c></seealso>,
  906. <seealso marker="gen_udp#open/1"><c>gen_udp:open/1,2</c></seealso>, or
  907. <seealso marker="gen_sctp#open/0"><c>gen_sctp:open/0,1,2</c></seealso>.</p>
  908. <p>This option uses the Linux-specific syscall
  909. <c>setns()</c>, such as in Linux kernel 3.0 or later,
  910. and therefore only exists when the runtime system
  911. is compiled for such an operating system.</p>
  912. <p>The virtual machine also needs elevated privileges, either
  913. running as superuser or (for Linux) having capability
  914. <c>CAP_SYS_ADMIN</c> according to the documentation for
  915. <c>setns(2)</c>.
  916. However, during testing also <c>CAP_SYS_PTRACE</c>
  917. and <c>CAP_DAC_READ_SEARCH</c> have proven to be necessary.</p>
  918. <p><em>Example:</em></p>
  919. <code>
  920. setcap cap_sys_admin,cap_sys_ptrace,cap_dac_read_search+epi beam.smp</code>
  921. <p>Notice that the filesystem containing the virtual machine
  922. executable (<c>beam.smp</c> in the example) must be local,
  923. mounted without flag <c>nosetuid</c>,
  924. support extended attributes, and
  925. the kernel must support file capabilities.
  926. All this runs out of the box on at least Ubuntu 12.04 LTS,
  927. except that SCTP sockets appear to not support
  928. network namespaces.</p>
  929. <p><c>Namespace</c> is a filename and is encoded
  930. and decoded as discussed in module
  931. <seealso marker="file">file</seealso>, with the
  932. following exceptions:</p>
  933. <list type="bulleted">
  934. <item><p>Emulator flag <c>+fnu</c> is ignored.</p></item>
  935. <item><p><seealso marker="#getopts/2"><c>getopts/2</c></seealso>
  936. for this option returns a binary for the filename if the stored
  937. filename cannot be decoded. This is only to occur if you set the
  938. option using a binary that cannot be decoded with the emulator's
  939. filename encoding:
  940. <seealso marker="file#native_name_encoding/0"><c>file:native_name_encoding/0</c></seealso>.</p></item>
  941. </list>
  942. </item>
  943. <tag><c>{bind_to_device, Ifname :: binary()}</c></tag>
  944. <item>
  945. <p>Binds a socket to a specific network interface. This option
  946. must be used in a function call that creates a socket, that is,
  947. <seealso marker="gen_tcp#connect/3"><c>gen_tcp:connect/3,4</c></seealso>,
  948. <seealso marker="gen_tcp#listen/2"><c>gen_tcp:listen/2</c></seealso>,
  949. <seealso marker="gen_udp#open/1"><c>gen_udp:open/1,2</c></seealso>, or
  950. <seealso marker="gen_sctp#open/0"><c>gen_sctp:open/0,1,2</c></seealso>.</p>
  951. <p>Unlike <seealso marker="#getifaddrs/0"><c>getifaddrs/0</c></seealso>, Ifname
  952. is encoded a binary. In the unlikely case that a system is using
  953. non-7-bit-ASCII characters in network device names, special care
  954. has to be taken when encoding this argument.</p>
  955. <p>This option uses the Linux-specific socket option
  956. <c>SO_BINDTODEVICE</c>, such as in Linux kernel 2.0.30 or later,
  957. and therefore only exists when the runtime system
  958. is compiled for such an operating system.</p>
  959. <p>Before Linux 3.8, this socket option could be set, but could not retrieved
  960. with <seealso marker="#getopts/2"><c>getopts/2</c></seealso>. Since Linux 3.8,
  961. it is readable.</p>
  962. <p>The virtual machine also needs elevated privileges, either
  963. running as superuser or (for Linux) having capability
  964. <c>CAP_NET_RAW</c>.</p>
  965. <p>The primary use case for this option is to bind sockets into
  966. <url href="http://www.kernel.org/doc/Documentation/networking/vrf.txt">Linux VRF instances</url>.
  967. </p>
  968. </item>
  969. <tag><c>list</c></tag>
  970. <item>
  971. <p>Received <c>Packet</c> is delivered as a list.</p>
  972. </item>
  973. <tag><c>binary</c></tag>
  974. <item>
  975. <p>Received <c>Packet</c> is delivered as a binary.</p>
  976. </item>
  977. <tag><c>{nodelay, Boolean}</c>(TCP/IP sockets)</tag>
  978. <item>
  979. <p>If <c>Boolean == true</c>, option <c>TCP_NODELAY</c>
  980. is turned on for the socket, which means that also small
  981. amounts of data are sent immediately.</p>
  982. </item>
  983. <tag><c>{packet, PacketType}</c>(TCP/IP sockets)</tag>
  984. <item>
  985. <p><marker id="packet"/>Defines the type of packets to use for a socket.
  986. Possible values:</p>
  987. <taglist>
  988. <tag><c>raw | 0</c></tag>
  989. <item>
  990. <p>No packaging is done.</p>
  991. </item>
  992. <tag><c>1 | 2 | 4</c></tag>
  993. <item>
  994. <p>Packets consist of a header specifying the number of
  995. bytes in the packet, followed by that number of bytes.
  996. The header length can be one, two, or four bytes, and
  997. containing an unsigned integer in big-endian byte order.
  998. Each send operation generates the header, and the header
  999. is stripped off on each receive operation.</p>
  1000. <p>The 4-byte header is limited to 2Gb.</p>
  1001. </item>
  1002. <tag><c>asn1 | cdr | sunrm | fcgi | tpkt | line</c></tag>
  1003. <item>
  1004. <p>These packet types only have effect on receiving.
  1005. When sending a packet, it is the responsibility of
  1006. the application to supply a correct header. On
  1007. receiving, however, one message is sent to
  1008. the controlling process for each complete packet
  1009. received, and, similarly, each call to
  1010. <c>gen_tcp:recv/2,3</c> returns one complete packet.
  1011. The header is <em>not</em> stripped off.</p>
  1012. <p>The meanings of the packet types are as follows:</p>
  1013. <list type="bulleted">
  1014. <item><c>asn1</c> - ASN.1 BER</item>
  1015. <item><c>sunrm</c> - Sun's RPC encoding</item>
  1016. <item><c>cdr</c> - CORBA (GIOP 1.1)</item>
  1017. <item><c>fcgi</c> - Fast CGI</item>
  1018. <item><c>tpkt</c> - TPKT format [RFC1006]</item>
  1019. <item><c>line</c> - Line mode, a packet is a line-terminated
  1020. with newline, lines longer than the receive buffer are
  1021. truncated</item>
  1022. </list>
  1023. </item>
  1024. <tag><c>http | http_bin</c></tag>
  1025. <item>
  1026. <p>The Hypertext Transfer Protocol. The packets
  1027. are returned with the format according to <c>HttpPacket</c>
  1028. described in
  1029. <seealso marker="erts:erlang#decode_packet/3">
  1030. <c>erlang:decode_packet/3</c></seealso> in ERTS.
  1031. A socket in passive
  1032. mode returns <c>{ok, HttpPacket}</c> from <c>gen_tcp:recv</c>
  1033. while an active socket sends messages like
  1034. <c>{http, Socket, HttpPacket}</c>.</p>
  1035. </item>
  1036. <tag><c>httph | httph_bin</c></tag>
  1037. <item>
  1038. <p>These two types are often not needed, as the socket
  1039. automatically switches from <c>http</c>/<c>http_bin</c> to
  1040. <c>httph</c>/<c>httph_bin</c> internally after the first line
  1041. is read. However, there can be occasions when they are
  1042. useful, such as parsing trailers from chunked encoding.</p>
  1043. </item>
  1044. </taglist>
  1045. </item>
  1046. <tag><c>{packet_size, Integer}</c>(TCP/IP sockets)</tag>
  1047. <item>
  1048. <p>Sets the maximum allowed length of the packet body. If
  1049. the packet header indicates that the length of the packet
  1050. is longer than the maximum allowed length, the packet is
  1051. considered invalid. The same occurs if the packet header
  1052. is too large for the socket receive buffer.</p>
  1053. <p>For line-oriented protocols (<c>line</c>, <c>http*</c>),
  1054. option <c>packet_size</c> also guarantees that lines up to the
  1055. indicated length are accepted and not considered invalid
  1056. because of internal buffer limitations.</p>
  1057. </item>
  1058. <tag><c>{line_delimiter, Char}</c>(TCP/IP sockets)</tag>
  1059. <item>
  1060. <p>Sets the line delimiting character for line-oriented protocols
  1061. (<c>line</c>). Defaults to <c>$\n</c>.</p>
  1062. </item>
  1063. <tag><c>{raw, Protocol, OptionNum, ValueBin}</c></tag>
  1064. <item>
  1065. <p>See below.</p>
  1066. </item>
  1067. <tag><c>{read_packets, Integer}</c>(UDP sockets)</tag>
  1068. <item>
  1069. <p>Sets the maximum number of UDP packets to read without
  1070. intervention from the socket when data is available.
  1071. When this many packets have been read and delivered
  1072. to the destination process, new packets are not read
  1073. until a new notification of available data has arrived.
  1074. Defaults to <c>5</c>. If this parameter is set too
  1075. high, the system can become unresponsive because of
  1076. UDP packet flooding.</p>
  1077. </item>
  1078. <tag><c>{recbuf, Size}</c></tag>
  1079. <item>
  1080. <p>The minimum size of the receive buffer to use for
  1081. the socket. You are encouraged to use
  1082. <seealso marker="#getopts/2"><c>getopts/2</c></seealso>
  1083. to retrieve the size set by your operating system.</p>
  1084. </item>
  1085. <tag><c>{reuseaddr, Boolean}</c></tag>
  1086. <item>
  1087. <p>Allows or disallows local reuse of port numbers. By
  1088. default, reuse is disallowed.</p>
  1089. </item>
  1090. <tag><c>{send_timeout, Integer}</c></tag>
  1091. <item>
  1092. <p>Only allowed for connection-oriented sockets.</p>
  1093. <p>Specifies a longest time to wait for a send operation to
  1094. be accepted by the underlying TCP stack. When the limit is
  1095. exceeded, the send operation returns
  1096. <c>{error,timeout}</c>. How much of a packet that
  1097. got sent is unknown; the socket is therefore to be closed
  1098. whenever a time-out has occurred (see <c>send_timeout_close</c>
  1099. below). Defaults to <c>infinity</c>.</p>
  1100. </item>
  1101. <tag><c>{send_timeout_close, Boolean}</c></tag>
  1102. <item>
  1103. <p>Only allowed for connection-oriented sockets.</p>
  1104. <p>Used together with <c>send_timeout</c> to specify whether
  1105. the socket is to be automatically closed when the send operation
  1106. returns <c>{error,timeout}</c>. The recommended setting is
  1107. <c>true</c>, which automatically closes the socket.
  1108. Defaults to <c>false</c> because of backward compatibility.</p>
  1109. <marker id="option-sndbuf"></marker>
  1110. </item>
  1111. <tag><c>{show_econnreset, Boolean}</c>(TCP/IP sockets)</tag>
  1112. <item>
  1113. <p>When this option is set to <c>false</c>, which is
  1114. default, an RST received from the TCP peer is treated
  1115. as a normal close (as though an FIN was sent). A caller to
  1116. <seealso marker="gen_tcp#recv/2"><c>gen_tcp:recv/2</c></seealso>
  1117. gets <c>{error, closed}</c>. In active
  1118. mode, the controlling process receives a
  1119. <c>{tcp_closed, Socket}</c> message, indicating that the
  1120. peer has closed the connection.</p>
  1121. <p>Setting this option to <c>true</c> allows you to
  1122. distinguish between a connection that was closed normally,
  1123. and one that was aborted (intentionally or unintentionally)
  1124. by the TCP peer. A call to
  1125. <seealso marker="gen_tcp#recv/2"><c>gen_tcp:recv/2</c></seealso>
  1126. returns <c>{error, econnreset}</c>. In
  1127. active mode, the controlling process receives a
  1128. <c>{tcp_error, Socket, econnreset}</c> message
  1129. before the usual <c>{tcp_closed, Socket}</c>, as is
  1130. the case for any other socket error. Calls to
  1131. <seealso marker="gen_tcp#send/2"><c>gen_tcp:send/2</c></seealso>
  1132. also returns <c>{error, econnreset}</c> when it
  1133. is detected that a TCP peer has sent an RST.</p>
  1134. <p>A connected socket returned from
  1135. <seealso marker="gen_tcp#accept/1"><c>gen_tcp:accept/1</c></seealso>
  1136. inherits the <c>show_econnreset</c> setting from the
  1137. listening socket.</p>
  1138. <marker id="option-show_econnreset"></marker>
  1139. </item>
  1140. <tag><c>{sndbuf, Size}</c></tag>
  1141. <item>
  1142. <p>The minimum size of the send buffer to use for the socket.
  1143. You are encouraged to use
  1144. <seealso marker="#getopts/2"><c>getopts/2</c></seealso>,
  1145. to retrieve the size set by your operating system.</p>
  1146. </item>
  1147. <tag><c>{priority, Integer}</c></tag>
  1148. <item>
  1149. <p>Sets the <c>SO_PRIORITY</c> socket level option on platforms
  1150. where this is implemented. The behavior and allowed range varies
  1151. between different systems.
  1152. The option is ignored on platforms where it
  1153. is not implemented. Use with caution.</p>
  1154. </item>
  1155. <tag><c>{tos, Integer}</c></tag>
  1156. <item>
  1157. <p>Sets <c>IP_TOS IP</c> level options on platforms where this is
  1158. implemented. The behavior and allowed range varies between
  1159. different systems.
  1160. The option is ignored on platforms where it is not
  1161. implemented. Use with caution.</p>
  1162. </item>
  1163. <tag><c>{tclass, Integer}</c></tag>
  1164. <item>
  1165. <p>
  1166. Sets <c>IPV6_TCLASS IP</c> level options on platforms
  1167. where this is implemented. The behavior and allowed range
  1168. varies between different systems.
  1169. The option is ignored on platforms where it is not
  1170. implemented. Use with caution.</p>
  1171. </item>
  1172. </taglist>
  1173. <p>In addition to these options, <em>raw</em>
  1174. option specifications can be used. The raw options are
  1175. specified as a tuple of arity four, beginning with tag
  1176. <c>raw</c>, followed by the protocol level, the option number,
  1177. and the option value specified as a binary. This
  1178. corresponds to the second, third, and fourth arguments to the
  1179. <c>setsockopt</c> call in the C socket API. The option value
  1180. must be coded in the native endianess of the platform and,
  1181. if a structure is required, must follow the structure
  1182. alignment conventions on the specific platform.</p>
  1183. <p>Using raw socket options requires detailed knowledge about
  1184. the current operating system and TCP stack.</p>
  1185. <p><em>Example:</em></p>
  1186. <p>This example concerns the use of raw options. Consider a Linux
  1187. system where you want to set option <c>TCP_LINGER2</c> on
  1188. protocol level <c>IPPROTO_TCP</c> in the stack. You know
  1189. that on this particular system it defaults to 60 (seconds),
  1190. but you want to lower it to 30 for a particular
  1191. socket. Option <c>TCP_LINGER2</c> is not explicitly
  1192. supported by <c>inet</c>, but you know that the protocol level
  1193. translates to number 6, the option number to number 8,
  1194. and the value is to be specified as a 32-bit integer. You can use
  1195. this code line to set the option for the socket named
  1196. <c>Sock</c>:</p>
  1197. <code type="none"><![CDATA[
  1198. inet:setopts(Sock,[{raw,6,8,<<30:32/native>>}]),]]></code>
  1199. <p>As many options are silently discarded by the stack if they
  1200. are specified out of range; it can be a good idea to check that
  1201. a raw option is accepted. The following code places the value
  1202. in variable <c>TcpLinger2:</c></p>
  1203. <code type="none"><![CDATA[
  1204. {ok,[{raw,6,8,<<TcpLinger2:32/native>>}]}=inet:getopts(Sock,[{raw,6,8,4}]),]]></code>
  1205. <p>Code such as these examples is inherently non-portable,
  1206. even different versions of the same OS on the same platform
  1207. can respond differently to this kind of option
  1208. manipulation. Use with care.</p>
  1209. <p>Notice that the default options for TCP/IP sockets can be
  1210. changed with the Kernel configuration parameters mentioned in
  1211. the beginning of this manual page.</p>
  1212. </desc>
  1213. </func>
  1214. <func>
  1215. <name name="sockname" arity="1"/>
  1216. <fsummary>Return the local address and port number for a socket.
  1217. </fsummary>
  1218. <desc>
  1219. <p>Returns the local address and port number for a socket.</p>
  1220. <p>Notice that for SCTP sockets this function returns only
  1221. one of the socket addresses. Function
  1222. <seealso marker="#socknames/1"><c>socknames/1,2</c></seealso>
  1223. returns all.</p>
  1224. </desc>
  1225. </func>
  1226. <func>
  1227. <name name="socknames" arity="1"/>
  1228. <fsummary>Return all local address/port numbers for a socket.</fsummary>
  1229. <desc>
  1230. <p>Equivalent to
  1231. <seealso marker="#socknames/2"><c>socknames(<anno>Socket</anno>, 0)</c></seealso>.
  1232. </p>
  1233. </desc>
  1234. </func>
  1235. <func>
  1236. <name name="socknames" arity="2"/>
  1237. <fsummary>Return all local address/port numbers for a socket.</fsummary>
  1238. <desc>
  1239. <p>Returns a list of all local address/port number pairs for a socket
  1240. for the specified association <c><anno>Assoc</anno></c>.</p>
  1241. <p>This function can return multiple addresses for multihomed
  1242. sockets, such as SCTP sockets. For other sockets it
  1243. returns a one-element list.</p>
  1244. <p>Notice that parameter <c><anno>Assoc</anno></c> is by the
  1245. <url href="http://tools.ietf.org/html/draft-ietf-tsvwg-sctpsocket-13">SCTP Sockets API Extensions</url>
  1246. defined to be ignored for one-to-one style sockets.
  1247. For one-to-many style sockets, the special value <c>0</c>
  1248. is defined to mean that the returned addresses must be
  1249. without any particular association.
  1250. How different SCTP implementations interpret this varies somewhat.
  1251. </p>
  1252. </desc>
  1253. </func>
  1254. </funcs>
  1255. <section>
  1256. <marker id="error_codes"></marker>
  1257. <title>POSIX Error Codes</title>
  1258. <list type="bulleted">
  1259. <item><c>e2big</c> - Too long argument list</item>
  1260. <item><c>eacces</c> - Permission denied</item>
  1261. <item><c>eaddrinuse</c> - Address already in use</item>
  1262. <item><c>eaddrnotavail</c> - Cannot assign requested address</item>
  1263. <item><c>eadv</c> - Advertise error</item>
  1264. <item><c>eafnosupport</c> - Address family not supported by
  1265. protocol family</item>
  1266. <item><c>eagain</c> - Resource temporarily unavailable</item>
  1267. <item><c>ealign</c> - EALIGN</item>
  1268. <item><c>ealready</c> - Operation already in progress</item>
  1269. <item><c>ebade</c> - Bad exchange descriptor</item>
  1270. <item><c>ebadf</c> - Bad file number</item>
  1271. <item><c>ebadfd</c> - File descriptor in bad state</item>
  1272. <item><c>ebadmsg</c> - Not a data message</item>
  1273. <item><c>ebadr</c> - Bad request descriptor</item>
  1274. <item><c>ebadrpc</c> - Bad RPC structure</item>
  1275. <item><c>ebadrqc</c> - Bad request code</item>
  1276. <item><c>ebadslt</c> - Invalid slot</item>
  1277. <item><c>ebfont</c> - Bad font file format</item>
  1278. <item><c>ebusy</c> - File busy</item>
  1279. <item><c>echild</c> - No children</item>
  1280. <item><c>echrng</c> - Channel number out of range</item>
  1281. <item><c>ecomm</c> - Communication error on send</item>
  1282. <item><c>econnaborted</c> - Software caused connection abort</item>
  1283. <item><c>econnrefused</c> - Connection refused</item>
  1284. <item><c>econnreset</c> - Connection reset by peer</item>
  1285. <item><c>edeadlk</c> - Resource deadlock avoided</item>
  1286. <item><c>edeadlock</c> - Resource deadlock avoided</item>
  1287. <item><c>edestaddrreq</c> - Destination address required</item>
  1288. <item><c>edirty</c> - Mounting a dirty fs without force</item>
  1289. <item><c>edom</c> - Math argument out of range</item>
  1290. <item><c>edotdot</c> - Cross mount point</item>
  1291. <item><c>edquot</c> - Disk quota exceeded</item>
  1292. <item><c>eduppkg</c> - Duplicate package name</item>
  1293. <item><c>eexist</c> - File already exists</item>
  1294. <item><c>efault</c> - Bad address in system call argument</item>
  1295. <item><c>efbig</c> - File too large</item>
  1296. <item><c>ehostdown</c> - Host is down</item>
  1297. <item><c>ehostunreach</c> - Host is unreachable</item>
  1298. <item><c>eidrm</c> - Identifier removed</item>
  1299. <item><c>einit</c> - Initialization error</item>
  1300. <item><c>einprogress</c> - Operation now in progress</item>
  1301. <item><c>eintr</c> - Interrupted system call</item>
  1302. <item><c>einval</c> - Invalid argument</item>
  1303. <item><c>eio</c> - I/O error</item>
  1304. <item><c>eisconn</c> - Socket is already connected</item>
  1305. <item><c>eisdir</c> - Illegal operation on a directory</item>
  1306. <item><c>eisnam</c> - Is a named file</item>
  1307. <item><c>el2hlt</c> - Level 2 halted</item>
  1308. <item><c>el2nsync</c> - Level 2 not synchronized</item>
  1309. <item><c>el3hlt</c> - Level 3 halted</item>
  1310. <item><c>el3rst</c> - Level 3 reset</item>
  1311. <item><c>elbin</c> - ELBIN</item>
  1312. <item><c>elibacc</c> - Cannot access a needed shared library</item>
  1313. <item><c>elibbad</c> - Accessing a corrupted shared library</item>
  1314. <item><c>elibexec</c> - Cannot exec a shared library directly</item>
  1315. <item><c>elibmax</c> - Attempting to link in more shared
  1316. libraries than system limit</item>
  1317. <item><c>elibscn</c> - <c>.lib</c> section in <c>a.out</c>
  1318. corrupted</item>
  1319. <item><c>elnrng</c> - Link number out of range</item>
  1320. <item><c>eloop</c> - Too many levels of symbolic links</item>
  1321. <item><c>emfile</c> - Too many open files</item>
  1322. <item><c>emlink</c> - Too many links</item>
  1323. <item><c>emsgsize</c> - Message too long</item>
  1324. <item><c>emultihop</c> - Multihop attempted</item>
  1325. <item><c>enametoolong</c> - Filename too long</item>
  1326. <item><c>enavail</c> - Unavailable</item>
  1327. <item><c>enet</c> - ENET</item>
  1328. <item><c>enetdown</c> - Network is down</item>
  1329. <item><c>enetreset</c> - Network dropped connection on reset</item>
  1330. <item><c>enetunreach</c> - Network is unreachable</item>
  1331. <item><c>enfile</c> - File table overflow</item>
  1332. <item><c>enoano</c> - Anode table overflow</item>
  1333. <item><c>enobufs</c> - No buffer space available</item>
  1334. <item><c>enocsi</c> - No CSI structure available</item>
  1335. <item><c>enodata</c> - No data available</item>
  1336. <item><c>enodev</c> - No such device</item>
  1337. <item><c>enoent</c> - No such file or directory</item>
  1338. <item><c>enoexec</c> - Exec format error</item>
  1339. <item><c>enolck</c> - No locks available</item>
  1340. <item><c>enolink</c> - Link has been severed</item>
  1341. <item><c>enomem</c> - Not enough memory</item>
  1342. <item><c>enomsg</c> - No message of desired type</item>
  1343. <item><c>enonet</c> - Machine is not on the network</item>
  1344. <item><c>enopkg</c> - Package not installed</item>
  1345. <item><c>enoprotoopt</c> - Bad protocol option</item>
  1346. <item><c>enospc</c> - No space left on device</item>
  1347. <item><c>enosr</c> - Out of stream resources or not a stream device</item>
  1348. <item><c>enosym</c> - Unresolved symbol name</item>
  1349. <item><c>enosys</c> - Function not implemented</item>
  1350. <item><c>enotblk</c> - Block device required</item>
  1351. <item><c>enotconn</c> - Socket is not connected</item>
  1352. <item><c>enotdir</c> - Not a directory</item>
  1353. <item><c>enotempty</c> - Directory not empty</item>
  1354. <item><c>enotnam</c> - Not a named file</item>
  1355. <item><c>enotsock</c> - Socket operation on non-socket</item>
  1356. <item><c>enotsup</c> - Operation not supported</item>
  1357. <item><c>enotty</c> - Inappropriate device for <c>ioctl</c></item>
  1358. <item><c>enotuniq</c> - Name not unique on network</item>
  1359. <item><c>enxio</c> - No such device or address</item>
  1360. <item><c>eopnotsupp</c> - Operation not supported on socket</item>
  1361. <item><c>eperm</c> - Not owner</item>
  1362. <item><c>epfnosupport</c> - Protocol family not supported</item>
  1363. <item><c>epipe</c> - Broken pipe</item>
  1364. <item><c>eproclim</c> - Too many processes</item>
  1365. <item><c>eprocunavail</c> - Bad procedure for program</item>
  1366. <item><c>eprogmismatch</c> - Wrong program version</item>
  1367. <item><c>eprogunavail</c> - RPC program unavailable</item>
  1368. <item><c>eproto</c> - Protocol error</item>
  1369. <item><c>eprotonosupport</c> - Protocol not supported</item>
  1370. <item><c>eprototype</c> - Wrong protocol type for socket</item>
  1371. <item><c>erange</c> - Math result unrepresentable</item>
  1372. <item><c>erefused</c> - EREFUSED</item>
  1373. <item><c>eremchg</c> - Remote address changed</item>
  1374. <item><c>eremdev</c> - Remote device</item>
  1375. <item><c>eremote</c> - Pathname hit remote filesystem</item>
  1376. <item><c>eremoteio</c> - Remote I/O error</item>
  1377. <item><c>eremoterelease</c> - EREMOTERELEASE</item>
  1378. <item><c>erofs</c> - Read-only filesystem</item>
  1379. <item><c>erpcmismatch</c> - Wrong RPC version</item>
  1380. <item><c>erremote</c> - Object is remote</item>
  1381. <item><c>eshutdown</c> - Cannot send after socket shutdown</item>
  1382. <item><c>esocktnosupport</c> - Socket type not supported</item>
  1383. <item><c>espipe</c> - Invalid seek</item>
  1384. <item><c>esrch</c> - No such process</item>
  1385. <item><c>esrmnt</c> - Srmount error</item>
  1386. <item><c>estale</c> - Stale remote file handle</item>
  1387. <item><c>esuccess</c> - Error 0</item>
  1388. <item><c>etime</c> - Timer expired</item>
  1389. <item><c>etimedout</c> - Connection timed out</item>
  1390. <item><c>etoomanyrefs</c> - Too many references</item>
  1391. <item><c>etxtbsy</c> - Text file or pseudo-device busy</item>
  1392. <item><c>euclean</c> - Structure needs cleaning</item>
  1393. <item><c>eunatch</c> - Protocol driver not attached</item>
  1394. <item><c>eusers</c> - Too many users</item>
  1395. <item><c>eversion</c> - Version mismatch</item>
  1396. <item><c>ewouldblock</c> - Operation would block</item>
  1397. <item><c>exdev</c> - Cross-domain link</item>
  1398. <item><c>exfull</c> - Message tables full</item>
  1399. <item><c>nxdomain</c> - Hostname or domain name cannot be found</item>
  1400. </list>
  1401. </section>
  1402. </erlref>