MODULE LinNet ["libc.so.70.0"];

	(*
		A. V. Shiryaev, 2012.11, 2013.08

		OpenBSD 5.4
		32-bit
	*)

	IMPORT Libc := LinLibc;

	CONST
		INVALID_SOCKET* = -1;
		SOCKET_ERROR* = -1;

		(* /usr/include/sys/socket.h *)
%%defs-socket%%
		(* /usr/include/netinet/in.h *)
			INADDR_NONE* = -1;
%%defs-in%%
		(* /usr/include/sys/param.h *)
			MAXHOSTNAMELEN* = 256; (* max hostname size *)

		(* /usr/include/netdb.h *)
%%defs-netdb%%
		(* /usr/include/sys/select.h *)
			FD_SETSIZE = 1024;
			__NBBY = 8;

	TYPE
		SOCKET* = INTEGER;

		(* /usr/include/sys/_types.h *)
			socklen_t* = INTEGER;
			sa_family_t* = SHORTCHAR;
			in_addr_t* = INTEGER;
			in_port_t* = SHORTINT;

		(* /usr/include/sys/socket.h *)
			sockaddr* = RECORD [untagged]
				sa_len*: SHORTCHAR; (* total length *)
				sa_family*: sa_family_t; (* address family *)
				sa_data*: ARRAY [untagged] 14 OF SHORTCHAR; (* actually longer; address value *)
			END;
			sockaddr_storage* = RECORD [untagged]
				ss_len*: SHORTCHAR; (* total length *)
				ss_family*: sa_family_t; (* address family *)
				__ss_pad1: ARRAY [untagged] 6 OF SHORTCHAR; (* align to quad *)
				__ss_pad2: LONGINT; (* force alignment for stupid compilers *)
				__ss_pad3: ARRAY [untagged] 240 OF SHORTCHAR; (* pad to a total of 256 bytes *)
			END;

		(* /usr/include/netinet/in.h *)
(*
			in_addr* = RECORD [untagged]
				s_addr*: in_addr_t;
			END;
*)
			in_addr* = RECORD [untagged]
				S_un*: RECORD [union]
					S_un_b*: RECORD [untagged]
						s_b1*: SHORTCHAR;
						s_b2*: SHORTCHAR;
						s_b3*: SHORTCHAR;
						s_b4*: SHORTCHAR;
					END;
					S_un_w*: RECORD [untagged]
						s_w1*: SHORTINT;
						s_w2*: SHORTINT;
					END;
					S_addr*: in_addr_t;
				END;
			END;

			sockaddr_in* = RECORD [untagged]
				sin_len*: SHORTCHAR;
				sin_family*: sa_family_t;
				sin_port*: in_port_t;
				sin_addr*: in_addr;
				sin_zero*: ARRAY [untagged] 8 OF SHORTCHAR;
			END;

		(* /usr/include/netdb.h *)
			Ptrhostent* = POINTER TO hostent;
			hostent* = RECORD [untagged]
				h_name*: Libc.PtrSTR; (* official name of host *)
				h_aliases*: POINTER TO ARRAY [untagged] OF Libc.PtrSTR; (* alias list *)
				h_addrtype*: INTEGER; (* host address type *)
				h_length*: INTEGER; (* length of address *)
				h_addr_list*: POINTER TO ARRAY [untagged] OF POINTER TO ARRAY [untagged] OF in_addr; (* list of addresses from name server *)
			END;

		(* /usr/include/sys/time.h *)
			timeval* = RECORD [untagged]
				tv_sec*: INTEGER; (* seconds *)
				tv_usec*: INTEGER; (* and microseconds *)
			END;

		(* /usr/include/sys/select.h *)
			__fd_mask = SET;
			fd_set* = ARRAY [untagged] (FD_SETSIZE + (SIZE(__fd_mask) * __NBBY - 1)) DIV (SIZE(__fd_mask) * __NBBY) OF __fd_mask;

	VAR
		h_errno*: INTEGER;

	PROCEDURE [ccall] socket* (domain: INTEGER; type: INTEGER; protocol: INTEGER): SOCKET;
	PROCEDURE [ccall] accept* (s: SOCKET; VAR addr: sockaddr; VAR addrlen: socklen_t): SOCKET;
	PROCEDURE [ccall] bind* (s: SOCKET; VAR name: sockaddr; namelen: socklen_t): INTEGER;
	PROCEDURE [ccall] connect* (s: SOCKET; VAR name: sockaddr; namelen: socklen_t): INTEGER;
	PROCEDURE [ccall] listen* (s: SOCKET; backlog: INTEGER): INTEGER;
	PROCEDURE [ccall] recv* (s: SOCKET; buf: Libc.PtrVoid; len: Libc.size_t; flags: SET): Libc.ssize_t;
	PROCEDURE [ccall] send* (s: SOCKET; msg: Libc.PtrVoid; len: Libc.size_t; flags: SET): Libc.ssize_t;
	PROCEDURE [ccall] shutdown* (s: SOCKET; how: INTEGER): INTEGER;
	PROCEDURE [ccall] getsockopt* (s: SOCKET; level: INTEGER; optname: SET; optval: Libc.PtrVoid; VAR optlen: socklen_t): INTEGER;
	PROCEDURE [ccall] setsockopt* (s: SOCKET; level: INTEGER; optname: SET; optval: Libc.PtrVoid; optlen: socklen_t): INTEGER;

	PROCEDURE [ccall] htons* (host16: SHORTINT): SHORTINT;

	PROCEDURE [ccall] gethostbyname* (name: Libc.PtrSTR): Ptrhostent;
	PROCEDURE [ccall] inet_addr* (cp: Libc.PtrSTR): in_addr_t;

	PROCEDURE [ccall] getsockname* (s: SOCKET; VAR name: sockaddr; VAR namelen: socklen_t): INTEGER;

	PROCEDURE [ccall] hstrerror* (err: INTEGER): Libc.PtrSTR;

(*
	PROCEDURE FD_ZERO (VAR set: Net.fd_set);
		VAR i: INTEGER;
	BEGIN
		i := LEN(set); REPEAT DEC(i); set[i] := {} UNTIL i = 0
	END FD_ZERO;

	PROCEDURE FD_SET (fd: Net.SOCKET; VAR set: Net.fd_set);
	BEGIN
		INCL(set[fd DIV 32], fd MOD 32)
	END FD_SET;
*)

	PROCEDURE [ccall] select* (nfds: INTEGER; VAR [nil] readfds: fd_set; VAR [nil] writefds: fd_set; VAR [nil] exceptfds: fd_set; VAR timeout: timeval): INTEGER;

END LinNet.
