1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 1997-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%----------------------------------------------------------------------------
22%% Interface constants.
23%%
24%% This section must be "identical" to the corresponding in inet_drv.c
25%%
26
27%% family codes to open
28-define(INET_AF_UNSPEC,       0).
29-define(INET_AF_INET,         1).
30-define(INET_AF_INET6,        2).
31-define(INET_AF_ANY,          3). % Fake for ANY in any address family
32-define(INET_AF_LOOPBACK,     4). % Fake for LOOPBACK in any address family
33-define(INET_AF_LOCAL,        5). % For Unix Domain address family
34-define(INET_AF_UNDEFINED,    6). % For any unknown address family
35
36%% type codes to open and gettype - INET_REQ_GETTYPE
37-define(INET_TYPE_STREAM,     1).
38-define(INET_TYPE_DGRAM,      2).
39-define(INET_TYPE_SEQPACKET,  3).
40
41%% socket modes, INET_LOPT_MODE
42-define(INET_MODE_LIST,	      0).
43-define(INET_MODE_BINARY,     1).
44
45%% deliver mode, INET_LOPT_DELIVER
46-define(INET_DELIVER_PORT,    0).
47-define(INET_DELIVER_TERM,    1).
48
49%% active socket, INET_LOPT_ACTIVE
50-define(INET_PASSIVE, 0).
51-define(INET_ACTIVE,  1).
52-define(INET_ONCE,    2). % Active once then passive
53-define(INET_MULTI,   3). % Active N then passive
54
55%% state codes (getstatus, INET_REQ_GETSTATUS)
56-define(INET_F_OPEN,         16#0001).
57-define(INET_F_BOUND,        16#0002).
58-define(INET_F_ACTIVE,       16#0004).
59-define(INET_F_LISTEN,       16#0008).
60-define(INET_F_CON,          16#0010).
61-define(INET_F_ACC,          16#0020).
62-define(INET_F_LST,          16#0040).
63-define(INET_F_BUSY,         16#0080).
64
65%% request codes (erlang:port_control/3)
66-define(INET_REQ_OPEN,          1).
67-define(INET_REQ_CLOSE,         2).
68-define(INET_REQ_CONNECT,       3).
69-define(INET_REQ_PEER,          4).
70-define(INET_REQ_NAME,          5).
71-define(INET_REQ_BIND,          6).
72-define(INET_REQ_SETOPTS,       7).
73-define(INET_REQ_GETOPTS,       8).
74-define(INET_REQ_GETIX,         9).
75%% -define(INET_REQ_GETIF,      10). OBSOLETE
76-define(INET_REQ_GETSTAT,       11).
77-define(INET_REQ_GETHOSTNAME,   12).
78-define(INET_REQ_FDOPEN,        13).
79-define(INET_REQ_GETFD,         14).
80-define(INET_REQ_GETTYPE,       15).
81-define(INET_REQ_GETSTATUS,     16).
82-define(INET_REQ_GETSERVBYNAME, 17).
83-define(INET_REQ_GETSERVBYPORT, 18).
84-define(INET_REQ_SETNAME,       19).
85-define(INET_REQ_SETPEER,       20).
86-define(INET_REQ_GETIFLIST,     21).
87-define(INET_REQ_IFGET,         22).
88-define(INET_REQ_IFSET,         23).
89-define(INET_REQ_SUBSCRIBE,     24).
90-define(INET_REQ_GETIFADDRS,    25).
91-define(INET_REQ_ACCEPT,        26).
92-define(INET_REQ_LISTEN,        27).
93-define(INET_REQ_IGNOREFD,      28).
94-define(INET_REQ_GETLADDRS,     29).
95-define(INET_REQ_GETPADDRS,     30).
96
97%% TCP requests
98%%-define(TCP_REQ_ACCEPT,         40). MOVED
99%%-define(TCP_REQ_LISTEN,         41). MERGED
100-define(TCP_REQ_RECV,           42).
101-define(TCP_REQ_UNRECV,         43).
102-define(TCP_REQ_SHUTDOWN,       44).
103-define(TCP_REQ_SENDFILE,       45).
104
105%% UDP and SCTP requests
106-define(PACKET_REQ_RECV,        60).
107%%-define(SCTP_REQ_LISTEN,        61). MERGED
108-define(SCTP_REQ_BINDX,	        62). %% Multi-home SCTP bind
109-define(SCTP_REQ_PEELOFF,       63).
110
111%% subscribe codes, INET_REQ_SUBSCRIBE
112-define(INET_SUBS_EMPTY_OUT_Q,  1).
113
114%% reply codes for *_REQ_*
115-define(INET_REP_ERROR,    0).
116-define(INET_REP_OK,       1).
117-define(INET_REP,          2).
118
119%% INET, TCP and UDP options:
120-define(INET_OPT_REUSEADDR,      0).
121-define(INET_OPT_KEEPALIVE,      1).
122-define(INET_OPT_DONTROUTE,      2).
123-define(INET_OPT_LINGER,         3).
124-define(INET_OPT_BROADCAST,      4).
125-define(INET_OPT_OOBINLINE,      5).
126-define(INET_OPT_SNDBUF,         6).
127-define(INET_OPT_RCVBUF,         7).
128-define(INET_OPT_PRIORITY,       8).
129-define(INET_OPT_TOS,            9).
130-define(TCP_OPT_NODELAY,         10).
131-define(UDP_OPT_MULTICAST_IF,    11).
132-define(UDP_OPT_MULTICAST_TTL,   12).
133-define(UDP_OPT_MULTICAST_LOOP,  13).
134-define(UDP_OPT_ADD_MEMBERSHIP,  14).
135-define(UDP_OPT_DROP_MEMBERSHIP, 15).
136-define(INET_OPT_IPV6_V6ONLY,    16).
137% "Local" options: codes start from 20:
138-define(INET_LOPT_BUFFER,        20).
139-define(INET_LOPT_HEADER,        21).
140-define(INET_LOPT_ACTIVE,        22).
141-define(INET_LOPT_PACKET,        23).
142-define(INET_LOPT_MODE,          24).
143-define(INET_LOPT_DELIVER,       25).
144-define(INET_LOPT_EXITONCLOSE,   26).
145-define(INET_LOPT_TCP_HIWTRMRK,  27).
146-define(INET_LOPT_TCP_LOWTRMRK,  28).
147-define(INET_LOPT_TCP_SEND_TIMEOUT, 30).
148-define(INET_LOPT_TCP_DELAY_SEND,   31).
149-define(INET_LOPT_PACKET_SIZE,   32).
150-define(INET_LOPT_READ_PACKETS,  33).
151-define(INET_OPT_RAW,            34).
152-define(INET_LOPT_TCP_SEND_TIMEOUT_CLOSE, 35).
153-define(INET_LOPT_MSGQ_HIWTRMRK,  36).
154-define(INET_LOPT_MSGQ_LOWTRMRK,  37).
155-define(INET_LOPT_NETNS,          38).
156-define(INET_LOPT_TCP_SHOW_ECONNRESET, 39).
157-define(INET_LOPT_LINE_DELIM,     40).
158-define(INET_OPT_TCLASS,          41).
159-define(INET_OPT_BIND_TO_DEVICE,  42).
160-define(INET_OPT_RECVTOS,         43).
161-define(INET_OPT_RECVTCLASS,      44).
162-define(INET_OPT_PKTOPTIONS,      45).
163-define(INET_OPT_TTL,             46).
164-define(INET_OPT_RECVTTL,         47).
165-define(TCP_OPT_NOPUSH,           48).
166% Specific SCTP options: separate range:
167-define(SCTP_OPT_RTOINFO,	 	100).
168-define(SCTP_OPT_ASSOCINFO,	 	101).
169-define(SCTP_OPT_INITMSG,	 	102).
170-define(SCTP_OPT_AUTOCLOSE,	 	103).
171-define(SCTP_OPT_NODELAY,		104).
172-define(SCTP_OPT_DISABLE_FRAGMENTS,	105).
173-define(SCTP_OPT_I_WANT_MAPPED_V4_ADDR, 106).
174-define(SCTP_OPT_MAXSEG,		107).
175-define(SCTP_OPT_SET_PEER_PRIMARY_ADDR, 108).
176-define(SCTP_OPT_PRIMARY_ADDR,		109).
177-define(SCTP_OPT_ADAPTATION_LAYER,	110).
178-define(SCTP_OPT_PEER_ADDR_PARAMS,	111).
179-define(SCTP_OPT_DEFAULT_SEND_PARAM,	112).
180-define(SCTP_OPT_EVENTS,		113).
181-define(SCTP_OPT_DELAYED_ACK_TIME,	114).
182-define(SCTP_OPT_STATUS,		115).
183-define(SCTP_OPT_GET_PEER_ADDR_INFO,	116).
184
185%% interface options, INET_REQ_IFGET and INET_REQ_IFSET
186-define(INET_IFOPT_ADDR,      1).
187-define(INET_IFOPT_BROADADDR, 2).
188-define(INET_IFOPT_DSTADDR,   3).
189-define(INET_IFOPT_MTU,       4).
190-define(INET_IFOPT_NETMASK,   5).
191-define(INET_IFOPT_FLAGS,     6).
192-define(INET_IFOPT_HWADDR,    7). %% where support (e.g linux)
193
194%% packet byte values, INET_LOPT_PACKET
195-define(TCP_PB_RAW,     0).
196-define(TCP_PB_1,       1).
197-define(TCP_PB_2,       2).
198-define(TCP_PB_4,       3).
199-define(TCP_PB_ASN1,    4).
200-define(TCP_PB_RM,      5).
201-define(TCP_PB_CDR,     6).
202-define(TCP_PB_FCGI,    7).
203-define(TCP_PB_LINE_LF, 8).
204-define(TCP_PB_TPKT,    9).
205-define(TCP_PB_HTTP,    10).
206-define(TCP_PB_HTTPH,   11).
207-define(TCP_PB_SSL_TLS, 12).
208-define(TCP_PB_HTTP_BIN,13).
209-define(TCP_PB_HTTPH_BIN,14).
210
211
212%% getstat, INET_REQ_GETSTAT
213-define(INET_STAT_RECV_CNT,  1).
214-define(INET_STAT_RECV_MAX,  2).
215-define(INET_STAT_RECV_AVG,  3).
216-define(INET_STAT_RECV_DVI,  4).
217-define(INET_STAT_SEND_CNT,  5).
218-define(INET_STAT_SEND_MAX,  6).
219-define(INET_STAT_SEND_AVG,  7).
220-define(INET_STAT_SEND_PEND, 8).
221-define(INET_STAT_RECV_OCT,  9).
222-define(INET_STAT_SEND_OCT,  10).
223
224%% interface stuff, INET_IFOPT_FLAGS
225-define(INET_IFNAMSIZ,          16).
226-define(INET_IFF_UP,            16#0001).
227-define(INET_IFF_BROADCAST,     16#0002).
228-define(INET_IFF_LOOPBACK,      16#0004).
229-define(INET_IFF_POINTTOPOINT,  16#0008).
230-define(INET_IFF_RUNNING,       16#0010).
231-define(INET_IFF_MULTICAST,     16#0020).
232%%
233-define(INET_IFF_DOWN,          16#0100).
234-define(INET_IFF_NBROADCAST,    16#0200).
235-define(INET_IFF_NPOINTTOPOINT, 16#0800).
236
237%% SCTP Flags for "sctp_sndrcvinfo":
238%% INET_REQ_SETOPTS:SCTP_OPT_DEFAULT_SEND_PARAM
239-define(SCTP_FLAG_UNORDERED, 1). 	% sctp_unordered
240-define(SCTP_FLAG_ADDR_OVER, 2). 	% sctp_addr_over
241-define(SCTP_FLAG_ABORT,     4). 	% sctp_abort
242-define(SCTP_FLAG_EOF,	     8). 	% sctp_eof
243-define(SCTP_FLAG_SNDALL,   16). 	% sctp_sndall, NOT YET IMPLEMENTED.
244
245%% SCTP Flags for "sctp_paddrparams", and the corresp Atoms:
246-define(SCTP_FLAG_HB_ENABLE,		1).	% sctp_hb_enable
247-define(SCTP_FLAG_HB_DISABLE,		2).	% sctp_hb_disable
248-define(SCTP_FLAG_HB_DEMAND,		4).	% sctp_hb_demand
249-define(SCTP_FLAG_PMTUD_ENABLE,		8).	% sctp_pmtud_enable
250-define(SCTP_FLAG_PMTUD_DISABLE,       16).	% sctp_pmtud_disable
251-define(SCTP_FLAG_SACKDELAY_ENABLE,    32).	% sctp_sackdelay_enable
252-define(SCTP_FLAG_SACKDELAY_DISABLE,   64).	% sctp_sackdelay_disable
253
254%%
255%% End of interface constants.
256%%----------------------------------------------------------------------------
257
258-define(LISTEN_BACKLOG, 5).     %% default backlog
259
260%% 5 secs need more ???
261-define(INET_CLOSE_TIMEOUT, 5000).
262
263%%
264%% Port/socket numbers: network standard functions
265%%
266-define(IPPORT_ECHO,             7).
267-define(IPPORT_DISCARD,          9).
268-define(IPPORT_SYSTAT,           11).
269-define(IPPORT_DAYTIME,          13).
270-define(IPPORT_NETSTAT,          15).
271-define(IPPORT_FTP,              21).
272-define(IPPORT_TELNET,           23).
273-define(IPPORT_SMTP,             25).
274-define(IPPORT_TIMESERVER,       37).
275-define(IPPORT_NAMESERVER,       42).
276-define(IPPORT_WHOIS,            43).
277-define(IPPORT_MTP,              57).
278
279%%
280%% Port/socket numbers: host specific functions
281%%
282-define(IPPORT_TFTP,             69).
283-define(IPPORT_RJE,              77).
284-define(IPPORT_FINGER,           79).
285-define(IPPORT_TTYLINK,          87).
286-define(IPPORT_SUPDUP,           95).
287
288%%
289%% UNIX TCP sockets
290%%
291-define(IPPORT_EXECSERVER,       512).
292-define(IPPORT_LOGINSERVER,      513).
293-define(IPPORT_CMDSERVER,        514).
294-define(IPPORT_EFSSERVER,        520).
295
296%%
297%% UNIX UDP sockets
298%%
299-define(IPPORT_BIFFUDP,          512).
300-define(IPPORT_WHOSERVER,        513).
301-define(IPPORT_ROUTESERVER,      520). %% 520+1 also used
302
303
304%%
305%% Ports < IPPORT_RESERVED are reserved for
306%% privileged processes (e.g. root).
307%% Ports > IPPORT_USERRESERVED are reserved
308%% for servers, not necessarily privileged.
309%%
310-define(IPPORT_RESERVED,         1024).
311-define(IPPORT_USERRESERVED,     5000).
312
313%% standard port for socks
314-define(IPPORT_SOCKS,           1080).
315
316%%
317%% Int to bytes
318%%
319-define(int8(X), [(X) band 16#ff]).
320
321-define(int16(X), [((X) bsr 8) band 16#ff, (X) band 16#ff]).
322
323-define(int24(X), [((X) bsr 16) band 16#ff,
324		   ((X) bsr 8) band 16#ff, (X) band 16#ff]).
325
326-define(int32(X),
327	[((X) bsr 24) band 16#ff, ((X) bsr 16) band 16#ff,
328	 ((X) bsr 8) band 16#ff, (X) band 16#ff]).
329
330-define(int64(X),
331	[((X) bsr 56) band 16#ff, ((X) bsr 48) band 16#ff,
332	 ((X) bsr 40) band 16#ff, ((X) bsr 32) band 16#ff,
333	 ((X) bsr 24) band 16#ff, ((X) bsr 16) band 16#ff,
334	 ((X) bsr 8) band 16#ff, (X) band 16#ff]).
335
336-define(intAID(X), % For SCTP AssocID
337        ?int32(X)).
338
339%% Bytes to unsigned
340-define(u64(X7,X6,X5,X4,X3,X2,X1,X0),
341	( ((X7) bsl 56) bor ((X6) bsl 48) bor ((X5) bsl 40) bor
342	  ((X4) bsl 32) bor ((X3) bsl 24) bor ((X2) bsl 16) bor
343	  ((X1) bsl 8) bor (X0)  )).
344
345-define(u32(X3,X2,X1,X0),
346	(((X3) bsl 24) bor ((X2) bsl 16) bor ((X1) bsl 8) bor (X0))).
347
348-define(u24(X2,X1,X0),
349	(((X2) bsl 16) bor ((X1) bsl 8) bor (X0))).
350
351-define(u16(X1,X0),
352	(((X1) bsl 8) bor (X0))).
353
354-define(u8(X0), (X0)).
355
356%% Bytes to signed
357-define(i32(X3,X2,X1,X0),
358        (?u32(X3,X2,X1,X0) -
359         (if (X3) > 127 -> 16#100000000; true -> 0 end))).
360
361-define(i24(X2,X1,X0),
362        (?u24(X2,X1,X0) -
363         (if (X2) > 127 -> 16#1000000; true -> 0 end))).
364
365-define(i16(X1,X0),
366        (?u16(X1,X0) -
367         (if (X1) > 127 -> 16#10000; true -> 0 end))).
368
369-define(i8(X0),
370	(?u8(X0) -
371	 (if (X0) > 127 -> 16#100; true -> 0 end))).
372
373%% macro for use in guard for checking ip address {A,B,C,D}
374-define(ip(A,B,C,D),
375	(((A) bor (B) bor (C) bor (D)) band (bnot 16#ff)) =:= 0).
376
377-define(ip6(A,B,C,D,E,F,G,H),
378	(((A) bor (B) bor (C) bor (D) bor (E) bor (F) bor (G) bor (H))
379	 band (bnot 16#ffff)) =:= 0).
380
381-define(ether(A,B,C,D,E,F),
382	(((A) bor (B) bor (C) bor (D) bor (E) bor (F))
383	 band (bnot 16#ff)) =:= 0).
384
385-define(port(P), (((P) band bnot 16#ffff) =:= 0)).
386
387%% default options (when inet_drv port is started)
388%%
389%% bufsz   = INET_MIN_BUFFER (8K)
390%% header  = 0
391%% packet  = 0 (raw)
392%% mode    = list
393%% deliver = term
394%% active  = false
395%%
396-record(connect_opts,
397	{
398	  ifaddr = any,     %% bind to interface address
399	  port   = 0,       %% bind to port (default is dynamic port)
400	  fd     = -1,      %% fd >= 0 => already bound
401	  opts   = []       %% [{active,true}] added in inet:connect_options
402	 }).
403
404-record(listen_opts,
405	{
406	  ifaddr = any,              %% bind to interface address
407	  port   = 0,                %% bind to port (default is dynamic port)
408	  backlog = ?LISTEN_BACKLOG, %% backlog
409	  fd      = -1,              %% %% fd >= 0 => already bound
410	  opts   = []                %% [{active,true}] added in
411	                             %% inet:listen_options
412	 }).
413
414-record(udp_opts,
415	{
416	  ifaddr = any,
417	  port   = 0,
418	  fd     = -1,
419	  opts   = [{active,true}]
420	 }).
421
422-define(SCTP_DEF_BUFSZ, 65536).
423-define(SCTP_DEF_IFADDR, any).
424-record(sctp_opts,
425	{
426	  ifaddr,
427	  port   = 0,
428	  fd	 = -1,
429	  type   = seqpacket,
430	  opts   = [{mode,	  binary},
431		    {buffer,	  ?SCTP_DEF_BUFSZ},
432		    {sndbuf,	  ?SCTP_DEF_BUFSZ},
433		    {recbuf,	  1024},
434		    {sctp_events, undefined}%,
435		    %%{active,      true}
436		   ]
437        }).
438
439%% The following Tags are purely internal, used for marking items in the
440%% send buffer:
441-define(SCTP_TAG_SEND_ANC_INITMSG,	0).
442-define(SCTP_TAG_SEND_ANC_PARAMS,	1).
443-define(SCTP_TAG_SEND_DATA,		2).
444