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

	(*
		GNU/Linux
		i386
	*)

	CONST
		NULL* = 0H;

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

		INADDR_NONE* = -1;
		INADDR_ANY* = 0;

		(* socket domains *)
			PF_UNSPEC* = 0; (* Unspecified. *)
			PF_LOCAL* = 1; (* Local to host (pipes and file-domain). *)
			PF_UNIX* = PF_LOCAL; (* POSIX name for PF_LOCAL. *)
			PF_FILE* = PF_LOCAL; (* Another non-standard name for PF_LOCAL. *)
			PF_INET* = 2; (* IP protocol family. *)
			PF_AX25* = 3; (* Amateur Radio AX.25. *)
			PF_IPX* = 4; (* Novell Internet Protocol. *)
			PF_APPLETALK* = 5; (* Appletalk DDP. *)
			PF_NETROM* = 6; (* Amateur radio NetROM. *)
			PF_BRIDGE* = 7; (* Multiprotocol bridge. *)
			PF_ATMPVC* = 8; (* ATM PVCs. *)
			PF_X25* = 9; (* Reserved for X.25 project. *)
			PF_INET6* = 10; (* IP version 6. *)
			PF_ROSE* = 11; (* Amateur Radio X.25 PLP. *)
			PF_DECnet* = 12; (* Reserved for DECnet project. *)
			PF_NETBEUI* = 13; (* Reserved for 802.2LLC project. *)
			PF_SECURITY* = 14; (* Security callback pseudo AF. *)
			PF_KEY* = 15; (* PF_KEY key management API. *)
			PF_NETLINK* = 16;
			PF_ROUTE* = PF_NETLINK; (* Alias to emulate 4.4BSD. *)
			PF_PACKET* = 17; (* Packet family. *)
			PF_ASH* = 18; (* Ash. *)
			PF_ECONET* = 19; (* Acorn Econet. *)
			PF_ATMSVC* = 20; (* ATM SVCs. *)
			PF_RDS* = 21; (* RDS sockets. *)
			PF_SNA* = 22; (* Linux SNA Project *)
			PF_IRDA* = 23; (* IRDA sockets. *)
			PF_PPPOX* = 24; (* PPPoX sockets. *)
			PF_WANPIPE* = 25; (* Wanpipe API sockets. *)
			PF_LLC* = 26; (* Linux LLC. *)
			PF_CAN* = 29; (* Controller Area Network. *)
			PF_TIPC* = 30; (* TIPC sockets. *)
			PF_BLUETOOTH* = 31; (* Bluetooth sockets. *)
			PF_IUCV* = 32; (* IUCV sockets. *)
			PF_RXRPC* = 33; (* RxRPC sockets. *)
			PF_ISDN* = 34; (* mISDN sockets. *)
			PF_PHONET* = 35; (* Phonet sockets. *)
			PF_IEEE802154* = 36; (* IEEE 802.15.4 sockets. *)
			PF_CAIF* = 37; (* CAIF sockets. *)
			PF_ALG* = 38; (* Algorithm sockets. *)
			PF_NFC* = 39; (* NFC sockets. *)
			PF_VSOCK* = 40; (* vSockets. *)
			PF_MAX* = 41; (* For now.. *)

		(* socket types *)
			(* /usr/include/i386-linux-gnu/bits/socket_type.h *)
				SOCK_STREAM* = 1;
				SOCK_DGRAM* = 2;
				SOCK_RAW* = 3;
				SOCK_RDM* = 4;
				SOCK_SEQPACKET* = 5;
				SOCK_DCCP* = 6;
				SOCK_PACKET* = 10;

		(* socket protocols *)
			IPPROTO_IP = 0; (* Dummy protocol for TCP. *)
			IPPROTO_HOPOPTS* = 0; (* IPv6 Hop-by-Hop options. *)
			IPPROTO_ICMP* = 1; (* Internet Control Message Protocol. *)
			IPPROTO_IGMP* = 2; (* Internet Group Management Protocol. *)
			IPPROTO_IPIP* = 4; (* IPIP tunnels (older KA9Q tunnels use 94). *)
			IPPROTO_TCP* = 6; (* Transmission Control Protocol. *)
			IPPROTO_EGP* = 8; (* Exterior Gateway Protocol. *)
			IPPROTO_PUP* = 12; (* PUP protocol. *)
			IPPROTO_UDP* = 17; (* User Datagram Protocol. *)
			IPPROTO_IDP* = 22; (* XNS IDP protocol. *)
			IPPROTO_TP* = 29; (* SO Transport Protocol Class 4. *)
			IPPROTO_DCCP* = 33; (* Datagram Congestion Control Protocol. *)
			IPPROTO_IPV6* = 41; (* IPv6 header. *)
			IPPROTO_ROUTING* = 43; (* IPv6 routing header. *)
			IPPROTO_FRAGMENT* = 44; (* IPv6 fragmentation header. *)
			IPPROTO_RSVP* = 46; (* Reservation Protocol. *)
			IPPROTO_GRE* = 47; (* General Routing Encapsulation. *)
			IPPROTO_ESP* = 50; (* encapsulating security payload. *)
			IPPROTO_AH* = 51; (* authentication header. *)
			IPPROTO_ICMPV6* = 58; (* ICMPv6. *)
			IPPROTO_NONE* = 59; (* IPv6 no next header. *)
			IPPROTO_DSTOPTS* = 60; (* IPv6 destination options. *)
			IPPROTO_MTP* = 92; (* Multicast Transport Protocol. *)
			IPPROTO_ENCAP* = 98; (* Encapsulation Header. *)
			IPPROTO_PIM* = 103; (* Protocol Independent Multicast. *)
			IPPROTO_COMP* = 108; (* Compression Header Protocol. *)
			IPPROTO_SCTP* = 132; (* Stream Control Transmission Protocol. *)
			IPPROTO_UDPLITE* = 136; (* UDP-Lite protocol. *)
			IPPROTO_RAW* = 255; (* Raw IP packets. *)

		(* socket options *)
			SO_DEBUG* = {0};
			SO_REUSEADDR* = {1};
			SO_TYPE* = {0,1};
			SO_ERROR* = {2};
			SO_DONTROUTE* = {0,2};
			SO_BROADCAST* = {1,2};
			SO_SNDBUF* = {0..2};
			SO_RCVBUF* = {3};
			SO_SNDBUFFORCE* = {5};
			SO_RCVBUFFORCE* = {0,5};
			SO_KEEPALIVE* = {0,3};
			SO_OOBINLINE* = {1,3};
			SO_NO_CHECK* = {0,1,3};
			SO_PRIORITY* = {2,3};
			SO_LINGER* = {0,2,3};
			SO_BSDCOMPAT* = {1..3};
			SO_REUSEPORT* = {0..3};
			SO_SECURITY_AUTHENTICATION* = {1,2,4};
			SO_SECURITY_ENCRYPTION_TRANSPORT* = {0..2,4};
			SO_SECURITY_ENCRYPTION_NETWORK* = {3,4};
			SO_BINDTODEVICE* = {0,3,4};
			SO_ATTACH_FILTER* = {1,3,4};
			SO_DETACH_FILTER* = {0,1,3,4};
			SO_PEERNAME* = {2..4};
			SO_TIMESTAMP* = {0,2..4};
			SO_ACCEPTCONN* = {1..4};
			SO_PEERSEC* = {0..4};
			SO_PASSSEC* = {1,5};
			SO_TIMESTAMPNS* = {0,1,5};
			SO_MARK* = {2,5};
			SO_TIMESTAMPING* = {0,2,5};
			SO_PROTOCOL* = {1,2,5};
			SO_DOMAIN* = {0..2,5};
			SO_RXQ_OVFL* = {3,5};
			SO_WIFI_STATUS* = {0,3,5};
			SO_PEEK_OFF* = {1,3,5};
			SO_NOFCS* = {0,1,3,5};
			SO_LOCK_FILTER* = {2,3,5};
			SO_SELECT_ERR_QUEUE* = {0,2,3,5};
			SO_BUSY_POLL* = {1..3,5};
			SO_MAX_PACING_RATE* = {0..3,5};

		(* /usr/include/i386-linux-gnu/sys/socket.h *)
			SHUT_RD* = 0;
			SHUT_WR* = 1;
			SHUT_RDWR* = 2;

		SOL_SOCKET* = 1;

		(* /usr/include/i386-linux-gnu/bits/socket.h *)
			MSG_OOB* = {0};
			MSG_PEEK* = {1};
			MSG_DONTROUTE* = {2};
			MSG_CTRUNC* = {3};
			MSG_PROXY* = {4};
			MSG_TRUNC* = {5};
			MSG_DONTWAIT* = {6};
			MSG_EOR* = {7};
			MSG_WAITALL* = {8};
			MSG_FIN* = {9};
			MSG_SYN* = {10};
			MSG_CONFIRM* = {11};
			MSG_RST* = {12};
			MSG_ERRQUEUE* = {13};
			MSG_NOSIGNAL* = {14};
			MSG_MORE* = {15};
			MSG_WAITFORONE* = {16};

		SOMAXCONN* = 128;

		(* addrinfo.ai_flags *)
			AI_PASSIVE* = {0}; (* Socket address is intended for `bind'. *)
			AI_CANONNAME* = {1}; (* Request for canonical name. *)
			AI_NUMERICHOST* = {2}; (* Don't use name resolution. *)
			AI_V4MAPPED* = {3}; (* IPv4 mapped addresses are acceptable. *)
			AI_ALL* = {4}; (* Return IPv4 mapped and IPv6 addresses. *)
			AI_CANONIDN* = {7}; (* Translate canonical name from IDN format. *)
			AI_NUMERICSERV* = {10}; (* Don't use name resolution. *)

		(* getaddrinfo/getnameinfo/gai_strerror error codes *)
			EAI_BADFLAGS* = -1; (* Invalid value for `ai_flags' field. *)
			EAI_NONAME* = -2; (* NAME or SERVICE is unknown. *)
			EAI_AGAIN* = -3; (* Temporary failure in name resolution. *)
			EAI_FAIL* = -4; (* Non-recoverable failure in name res. *)
			EAI_FAMILY* = -6; (* `ai_family' not supported. *)
			EAI_SOCKTYPE* = -7; (* `ai_socktype' not supported. *)
			EAI_SERVICE* = -8; (* SERVICE not supported for `ai_socktype'. *)
			EAI_MEMORY* = -10; (* Memory allocation failure. *)
			EAI_SYSTEM* = -11; (* System error returned in `errno'. *)
			EAI_OVERFLOW* = -12; (* Argument buffer overflow. *)
			EAI_NODATA* = -5; (* No address associated with NAME. *)
			EAI_ADDRFAMILY* = -9; (* Address family for NAME not supported. *)
			EAI_INPROGRESS* = -100; (* Processing request in progress. *)
			EAI_CANCELED* = -101; (* Request canceled. *)
			EAI_NOTCANCELED* = -102; (* Request not canceled. *)
			EAI_ALLDONE* = -103; (* All requests done. *)
			EAI_INTR* = -104; (* Interrupted by a signal. *)
			EAI_IDN_ENCODE* = -105; (* IDN encoding failed. *)

		(* /usr/include/i386-linux-gnu/bits/typesizes.h *)
			__FD_SETSIZE = 1024;

	TYPE
		u_int8_t = SHORTCHAR;
		int8_t = SHORTCHAR;
		uint16_t = SHORTINT;
		(* PtrVoid, int, intFlags, short, socklen_t, size_t, ssize_t, in_addr_t, in_port_t, time_t, suseconds_t, nfds_t, sa_family_t *)
		PtrVoid* = INTEGER;
		short* = SHORTINT;
		int* = INTEGER;
		intFlags* = SET;
		size_t* = INTEGER;
		ssize_t* = INTEGER;
		time_t* = INTEGER;
		suseconds_t* = INTEGER;
		socklen_t* = INTEGER;
		in_addr_t* = INTEGER;
		in_port_t* = SHORTINT;
		nfds_t* = INTEGER;
		sa_family_t* = SHORTINT;

		PtrSTR* = POINTER [untagged] TO ARRAY [untagged] OF SHORTCHAR;

		SOCKET* = int;

(*
		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;

		Ptrsockaddr* = POINTER [untagged] TO sockaddr;
		sockaddr* = RECORD [untagged]
			(* /usr/include/i386-linux-gnu/bits/socket.h *)
				sa_family: sa_family_t;
				sa_data: ARRAY [untagged] 14 OF SHORTCHAR;
		END;

		Ptrsockaddr_storage = POINTER [untagged] TO sockaddr_storage;
		sockaddr_storage* = RECORD [untagged]
			(* /usr/include/i386-linux-gnu/bits/socket.h *)
				sa_family: sa_family_t;
				__ss_align: INTEGER;
				__ss_padding: ARRAY [untagged] 128 - 2 * 4 OF SHORTCHAR;
		END;

		Ptrsockaddr_in* = POINTER [untagged] TO sockaddr_in;
		sockaddr_in* = RECORD [untagged]
			(* /usr/include/linux/in.h *)
				sin_family*: sa_family_t; (* address family *)
				sin_port*: SHORTINT; (* port number *)
				sin_addr*: in_addr; (* internet address *)
				__pad: ARRAY [untagged] 8 OF SHORTCHAR;
		END;

		Ptraddrinfo* = POINTER [untagged] TO addrinfo;
		addrinfo* = RECORD [untagged]
			(* /usr/include/netdb.h *)
				ai_flags*: intFlags; (* input flags *)
				ai_family*: int; (* protocol family for socket *)
				ai_socktype*: int; (* socket type *)
				ai_protocol*: int; (* protocol for socket *)
				ai_addrlen*: socklen_t; (* length of socket-address *)
				ai_addr*: Ptrsockaddr; (* socket-address for socket *)
				ai_canonname*: PtrSTR; (* canonical name for service location (iff req) *)
				ai_next*: Ptraddrinfo; (* pointer to next in list *)
		END;

		timeval* = RECORD [untagged]
			(* Linux, FreeBSD, OpenBSD *)
				tv_sec*: time_t; (* seconds *)
				tv_usec*: suseconds_t; (* and microseconds *)
		END;

		pollfd* = RECORD [untagged]
			(* Linux, FreeBSD, OpenBSD *)
				fd*: int;
				events*: short;
				revents*: short;
		END;

		(* /usr/include/i386-linux-gnu/sys/select.h *)
			__fd_mask = SET;
			fd_set* = ARRAY [untagged] __FD_SETSIZE DIV (SIZE(__fd_mask) * 8) OF __fd_mask;

	(* POSIX.1 *)
		PROCEDURE [ccall] socket* (domain: int; type: int; protocol: int): 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): int;
		PROCEDURE [ccall] connect* (s: SOCKET; VAR name: sockaddr; namelen: socklen_t): int;
		PROCEDURE [ccall] getsockname* (s: SOCKET; VAR name: sockaddr; VAR namelen: socklen_t): int;
		PROCEDURE [ccall] listen* (s: SOCKET; backlog: int): int;
		PROCEDURE [ccall] recv* (s: SOCKET; buf: PtrVoid; len: size_t; flags: intFlags): ssize_t;
		PROCEDURE [ccall] send* (s: SOCKET; msg: PtrVoid; len: size_t; flags: intFlags): ssize_t;
		PROCEDURE [ccall] shutdown* (s: SOCKET; how: int): int;
		PROCEDURE [ccall] getsockopt* (s: SOCKET; level: int; optname: intFlags; optval: PtrVoid; VAR [nil] optlen: socklen_t): int;
		PROCEDURE [ccall] setsockopt* (s: SOCKET; level: int; optname: intFlags; optval: PtrVoid; optlen: socklen_t): int;

		PROCEDURE [ccall] inet_addr* (cp: PtrSTR): in_addr_t;
		PROCEDURE [ccall] htons* (host16: uint16_t): uint16_t;

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

		PROCEDURE [ccall] poll* (VAR fds: ARRAY [untagged] OF pollfd; nfds: nfds_t; timeout: int): int;

(*
	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;
*)

	(* POSIX.1g *)
		PROCEDURE [ccall] getaddrinfo* (hostname: PtrSTR; servname: PtrSTR; VAR [nil] hints: addrinfo; VAR res: Ptraddrinfo): int;
		PROCEDURE [ccall] freeaddrinfo* (ai: Ptraddrinfo);

		PROCEDURE [ccall] gai_strerror* (ecode: int): PtrSTR;

END LinNet.