Blame include/ntapi/nt_socket.h

dd89bb
#ifndef _NT_SOCKET_H_
dd89bb
#define _NT_SOCKET_H_
dd89bb
dd89bb
/**
dd89bb
 *  socket api:
dd89bb
 *  -----------
dd89bb
 *  here we provide native libraries and applications
dd89bb
 *  with a minimal socket abstraction layer;
dd89bb
 *  if you are interested in posix socket semantics
dd89bb
 *  then this header is REALLY NOT what you
dd89bb
 *  are looking for, as the most portable scenario
dd89bb
 *  (psxscl+libc) neither requires nor allows for
dd89bb
 *  direct interaction with the below interfaces.
dd89bb
 *
dd89bb
 *  (additional information for the yet curious...)
dd89bb
 *
dd89bb
 *  client libraries and applications are responsible
dd89bb
 *  for the entire bookkeeping, which should be
dd89bb
 *  easy using the nt_socket structure.
dd89bb
 *
dd89bb
 *  kernel sockets are created using ZwCreateFile,
dd89bb
 *  and are then manipulated (bind, connect, send,
dd89bb
 *  recv, listen, accept, getsockname, etc.) using
dd89bb
 *  ZwDeviceIoControlFile.  accordingly, the main
dd89bb
 *  objective of the below interfaces is to provide
dd89bb
 *  thin wrappers around the above NT system calls,
dd89bb
 *  thereby releasing you from the tedious task
dd89bb
 *  of formatting control messages according to
dd89bb
 *  individual IOCTL codes, protocol types, or
dd89bb
 *  different versions of the operating system.
dd89bb
 *
dd89bb
 *  another noteworthy objective is the direct
dd89bb
 *  translation between posix socket semantics on the
dd89bb
 *  one hand, and the interfaces that this module
dd89bb
 *  provides on the other.  functions in this module
dd89bb
 *  normally take the same arguments as their
dd89bb
 *  posix equivalents, yet require a pointer
dd89bb
 *  to an nt_socket structure in place of an integer
dd89bb
 *  file descriptor (in the posix library, the task
dd89bb
 *  of converting an integer file descriptor to the
dd89bb
 *  above pointer is a trivial one).  for functions
dd89bb
 *  such as send() and recv(), the return value is
dd89bb
 *  the system's native NTSTATUS; the number of bytes
dd89bb
 *  sent or received is then returned in a separate
dd89bb
 *  argument, and can also be obtained, along with
dd89bb
 *  extended status, from the info member of the iosb
dd89bb
 *  argument.  last but not least, each function in
dd89bb
 *  this module accepts one or more optional arguments
dd89bb
 *  of questionable relevance:-)
dd89bb
**/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include "nt_status.h"
dd89bb
#include "nt_object.h"
dd89bb
#include "nt_tty.h"
dd89bb
dd89bb
/* afd socket domains */
dd89bb
#define NT_AF_UNSPEC		(0x0000u)
dd89bb
#define NT_AF_UNIX		(0x0001u)
dd89bb
#define NT_AF_INET		(0x0002u)
dd89bb
#define NT_AF_IMPLINK		(0x0003u)
dd89bb
#define NT_AF_PUP		(0x0004u)
dd89bb
#define NT_AF_CHAOS		(0x0005u)
dd89bb
#define NT_AF_NS		(0x0006u)
dd89bb
#define NT_AF_IPX		(0x0006u) /* synonym */
dd89bb
#define NT_AF_ISO		(0x0007u)
dd89bb
#define NT_AF_OSI		(0x0007u) /* synonym */
dd89bb
#define NT_AF_ECMA		(0x0008u)
dd89bb
#define NT_AF_DATAKIT		(0x0009u)
dd89bb
#define NT_AF_CCITT		(0x000Au)
dd89bb
#define NT_AF_SNA		(0x000Bu)
dd89bb
#define NT_AF_DECnet		(0x000Cu)
dd89bb
#define NT_AF_DLI		(0x000Du)
dd89bb
#define NT_AF_LAT		(0x000Eu)
dd89bb
#define NT_AF_HYLINK		(0x000Fu)
dd89bb
#define NT_AF_APPLETALK		(0x0010u)
dd89bb
#define NT_AF_NETBIOS		(0x0011u)
dd89bb
#define NT_AF_VOICEVIEW		(0x0012u)
dd89bb
#define NT_AF_FIREFOX		(0x0013u)
dd89bb
#define NT_AF_UNKNOWN_1ST	(0x0014u)
dd89bb
#define NT_AF_BAN		(0x0015u)
dd89bb
#define NT_AF_ATM		(0x0016u)
dd89bb
#define NT_AF_INET6		(0x0017u)
dd89bb
#define NT_AF_CLUSTER		(0x0018u)
dd89bb
#define NT_AF_12844		(0x0019u)
dd89bb
#define NT_AF_IRDA		(0x001Au)
dd89bb
#define NT_AF_NETDES		(0x001Cu)
dd89bb
#define NT_AF_TCNPROCESS	(0x001Du)
dd89bb
#define NT_AF_TCNMESSAGE	(0x001Eu)
dd89bb
#define NT_AF_ICLFXBM		(0x001Fu)
dd89bb
#define NT_AF_BTH		(0x0020u)
dd89bb
#define NT_AF_LINK		(0x0021u)
dd89bb
dd89bb
dd89bb
/* afd socket types */
dd89bb
#define NT_SOCK_STREAM		(0x0001u)
dd89bb
#define NT_SOCK_DGRAM		(0x0002u)
dd89bb
#define NT_SOCK_RAW		(0x0003u)
dd89bb
#define NT_SOCK_RDM		(0x0004u)
dd89bb
#define NT_SOCK_SEQPACKET	(0x0005u)
dd89bb
dd89bb
dd89bb
/* afd socket protocols */
dd89bb
#define NT_IPPROTO_IP				0
dd89bb
#define NT_IPPROTO_HOPOPTS			0
dd89bb
#define NT_IPPROTO_ICMP				1
dd89bb
#define NT_IPPROTO_IGMP				2
dd89bb
#define NT_IPPROTO_GGP				3
dd89bb
#define NT_IPPROTO_IPV4				4
dd89bb
#define NT_IPPROTO_ST				5
dd89bb
#define NT_IPPROTO_TCP				6
dd89bb
#define NT_IPPROTO_CBT				7
dd89bb
#define NT_IPPROTO_EGP				8
dd89bb
#define NT_IPPROTO_IGP				9
dd89bb
#define NT_IPPROTO_PUP				12
dd89bb
#define NT_IPPROTO_UDP				17
dd89bb
#define NT_IPPROTO_IDP				22
dd89bb
#define NT_IPPROTO_RDP				27
dd89bb
#define NT_IPPROTO_IPV6				41
dd89bb
#define NT_IPPROTO_ROUTING			43
dd89bb
#define NT_IPPROTO_FRAGMENT			44
dd89bb
#define NT_IPPROTO_ESP				50
dd89bb
#define NT_IPPROTO_AH				51
dd89bb
#define NT_IPPROTO_ICMPV6			58
dd89bb
#define NT_IPPROTO_NONE				59
dd89bb
#define NT_IPPROTO_DSTOPTS			60
dd89bb
#define NT_IPPROTO_ND				77
dd89bb
#define NT_IPPROTO_ICLFXBM			78
dd89bb
#define NT_IPPROTO_PIM				103
dd89bb
#define NT_IPPROTO_PGM				113
dd89bb
#define NT_IPPROTO_L2TP				115
dd89bb
#define NT_IPPROTO_SCTP				132
dd89bb
#define NT_IPPROTO_RAW				255
dd89bb
#define NT_IPPROTO_MAX				256
dd89bb
#define NT_IPPROTO_RESERVED_RAW			257
dd89bb
#define NT_IPPROTO_RESERVED_IPSEC		258
dd89bb
#define NT_IPPROTO_RESERVED_IPSECOFFLOAD	259
dd89bb
#define NT_IPPROTO_RESERVED_WNV			260
dd89bb
#define NT_IPPROTO_RESERVED_MAX			261
dd89bb
dd89bb
dd89bb
dd89bb
/* tdi receive modes */
dd89bb
#define NT_TDI_RECEIVE_BROADCAST		(0x0004u)
dd89bb
#define NT_TDI_RECEIVE_MULTICAST		(0x0008u)
dd89bb
#define NT_TDI_RECEIVE_PARTIAL			(0x0010u)
dd89bb
#define NT_TDI_RECEIVE_NORMAL			(0x0020u)
dd89bb
#define NT_TDI_RECEIVE_EXPEDITED		(0x0040u)
dd89bb
#define NT_TDI_RECEIVE_PEEK			(0x0080u)
dd89bb
#define NT_TDI_RECEIVE_NO_RESPONSE_EXP		(0x0100u)
dd89bb
#define NT_TDI_RECEIVE_COPY_LOOKAHEAD		(0x0200u)
dd89bb
#define NT_TDI_RECEIVE_ENTIRE_MESSAGE		(0x0400u)
dd89bb
#define NT_TDI_RECEIVE_AT_DISPATCH_LEVEL	(0x0800u)
dd89bb
#define NT_TDI_RECEIVE_CONTROL_INFO		(0x1000u)
dd89bb
#define NT_TDI_RECEIVE_FORCE_INDICATION		(0x2000u)
dd89bb
#define NT_TDI_RECEIVE_NO_PUSH			(0x4000u)
dd89bb
dd89bb
/* tdi send modes */
dd89bb
#define NT_TDI_SEND_EXPEDITED			(0x0020u)
dd89bb
#define NT_TDI_SEND_PARTIAL			(0x0040u)
dd89bb
#define NT_TDI_SEND_NO_RESPONSE_EXPECTED	(0x0080u)
dd89bb
#define NT_TDI_SEND_NON_BLOCKING		(0x0100u)
dd89bb
#define NT_TDI_SEND_AND_DISCONNECT		(0x0200u)
dd89bb
dd89bb
/* tdi listen modes */
dd89bb
#define NT_TDI_QUERY_ACCEPT			(0x0001u)
dd89bb
dd89bb
/* tdi disconnect modes */
dd89bb
#define NT_TDI_DISCONNECT_WAIT			(0x0001u)
dd89bb
#define NT_TDI_DISCONNECT_ABORT			(0x0002u)
dd89bb
#define NT_TDI_DISCONNECT_RELEASE		(0x0004u)
dd89bb
dd89bb
/* afd ioctl codes */
dd89bb
#define NT_AFD_IOCTL_BIND			(0x12003u)
dd89bb
#define NT_AFD_IOCTL_CONNECT			(0x12007u)
dd89bb
#define NT_AFD_IOCTL_LISTEN			(0x1200Bu)
dd89bb
#define NT_AFD_IOCTL_ACCEPT			(0x1200Cu)
dd89bb
#define NT_AFD_IOCTL_DUPLICATE			(0x12010u)
dd89bb
#define NT_AFD_IOCTL_SEND			(0x1201Fu)
dd89bb
#define NT_AFD_IOCTL_UDP_SEND			(0x12023u)
dd89bb
#define NT_AFD_IOCTL_RECV			(0x12017u)
dd89bb
#define NT_AFD_IOCTL_DISCONNECT			(0x1202Bu)
dd89bb
#define NT_AFD_IOCTL_SELECT			(0x12024u)
dd89bb
#define NT_AFD_IOCTL_GET_SOCK_NAME		(0x1202Fu)
dd89bb
#define NT_AFD_IOCTL_GET_PEER_NAME		(0x1202Fu)
dd89bb
#define NT_AFD_IOCTL_SET_CONTEXT		(0x1202Fu)
dd89bb
dd89bb
/* afd socket shutdown bits */
dd89bb
#define NT_AFD_DISCONNECT_WR	(0x01u)
dd89bb
#define NT_AFD_DISCONNECT_RD	(0x02u)
dd89bb
dd89bb
/* socket portable shutdown options */
dd89bb
#define NT_SHUT_RD		(0x00u)
dd89bb
#define NT_SHUT_WR		(0x01u)
dd89bb
#define NT_SHUT_RDWR		(0x02u)
dd89bb
dd89bb
/* send, sendto, sendmsg: *nix flags and bit place-holders */
dd89bb
#define NT_MSG_OOB		(0x00000001u)
dd89bb
#define NT_MSG_PEEK		(0x00000002u)
dd89bb
#define NT_MSG_DONTROUTE	(0x00000004u)
dd89bb
#define NT_MSG_CTRUNC		(0x00000008u)
dd89bb
#define NT_MSG_PROXY		(0x00000010u)
dd89bb
#define NT_MSG_TRUNC		(0x00000020u)
dd89bb
#define NT_MSG_DONTWAIT		(0x00000040u)
dd89bb
#define NT_MSG_EOR		(0x00000080u)
dd89bb
#define NT_MSG_WAITALL		(0x00000100u)
dd89bb
#define NT_MSG_FIN		(0x00000200u)
dd89bb
#define NT_MSG_SYN		(0x00000400u)
dd89bb
#define NT_MSG_CONFIRM		(0x00000800u)
dd89bb
#define NT_MSG_RST		(0x00001000u)
dd89bb
#define NT_MSG_ERRQUEUE		(0x00002000u)
dd89bb
#define NT_MSG_NOSIGNAL		(0x00004000u)
dd89bb
#define NT_MSG_MORE		(0x00008000u)
dd89bb
#define NT_MSG_WAITFORONE	(0x00010000u)
dd89bb
#define NT_MSG_CMSG_CLOEXEC	(0x40000000u)
dd89bb
dd89bb
dd89bb
/* socket structures */
dd89bb
typedef struct _nt_socket {
dd89bb
	union {
dd89bb
		void *		hsocket;
dd89bb
		void *		hfile;
dd89bb
		void *		hpipe;
dd89bb
		nt_pty *	hpty;
dd89bb
	};
dd89bb
dd89bb
	void *			hevent;
dd89bb
	int32_t			refcnt;		/* reserved for client */
dd89bb
	uint16_t		fdtype;		/* reserved for client */
dd89bb
	uint16_t		sctype;		/* reserved for client */
dd89bb
	uint32_t		ctxid;		/* reserved for client */
dd89bb
	uint32_t		reserved;	/* reserved for client */
dd89bb
	uint32_t		psxflags;	/* reserved for client */
dd89bb
	uint32_t		ntflags;	/* sc_wait alert flags */
dd89bb
	nt_large_integer	timeout;
dd89bb
	int32_t			iostatus;
dd89bb
	int32_t			waitstatus;
dd89bb
dd89bb
	union {
dd89bb
		struct {
dd89bb
			uint16_t	domain;
dd89bb
			uint16_t	type;
dd89bb
			uint32_t	protocol;
dd89bb
		};
dd89bb
dd89bb
		void *	vfd;
dd89bb
		void *	vmount;
dd89bb
		void *	hpair;
dd89bb
		void *	dirctx;
dd89bb
	};
dd89bb
} nt_socket;
dd89bb
dd89bb
dd89bb
typedef struct _nt_scope_id{
dd89bb
	union {
dd89bb
		struct {
dd89bb
			uint32_t	zone  : 28;
dd89bb
			uint32_t	level : 4;
dd89bb
		};
dd89bb
dd89bb
		uint32_t	value;
dd89bb
	};
dd89bb
} nt_scope_id;
dd89bb
dd89bb
dd89bb
typedef struct _nt_sockaddr_in4 {
dd89bb
	uint16_t	sa_family;
dd89bb
	char		sa_data[14];
dd89bb
} nt_sockaddr_in4;
dd89bb
dd89bb
dd89bb
typedef struct _nt_sockaddr_in6 {
dd89bb
	uint16_t	sa_family;
dd89bb
	uint16_t	sa_port;
dd89bb
	uint32_t	sa_flow;
dd89bb
	unsigned char	sa_data[16];
dd89bb
	nt_scope_id	sa_scope;
dd89bb
} nt_sockaddr_in6;
dd89bb
dd89bb
dd89bb
typedef union _nt_sockaddr {
dd89bb
	nt_sockaddr_in4	sa_addr_in4;
dd89bb
	nt_sockaddr_in6 sa_addr_in6;
dd89bb
} nt_sockaddr;
dd89bb
dd89bb
dd89bb
typedef struct _nt_afd_buffer {
dd89bb
	size_t		length;
dd89bb
	char *		buffer;
dd89bb
} nt_afd_buffer;
dd89bb
dd89bb
dd89bb
typedef struct _nt_afd_listen_info {
dd89bb
	void *		unknown_1st;
dd89bb
	uint32_t	backlog;
dd89bb
	void *		unknown_2nd;
dd89bb
} nt_afd_listen_info;
dd89bb
dd89bb
dd89bb
typedef struct _nt_afd_accept_info {
dd89bb
	uint32_t	sequence;
dd89bb
	nt_sockaddr	addr;
dd89bb
	uint32_t	legacy[2];
dd89bb
} nt_afd_accept_info;
dd89bb
dd89bb
dd89bb
typedef struct _nt_afd_duplicate_info {
dd89bb
	uint32_t	unknown;
dd89bb
	uint32_t	sequence;
dd89bb
	void *		hsocket_dedicated;
dd89bb
} nt_afd_duplicate_info;
dd89bb
dd89bb
dd89bb
typedef struct _nt_afd_send_info {
dd89bb
	nt_afd_buffer *		afd_buffer_array;
dd89bb
	uint32_t		buffer_count;
dd89bb
	uint32_t		afd_flags;
dd89bb
	uint32_t		tdi_flags;
dd89bb
} nt_afd_send_info;
dd89bb
dd89bb
dd89bb
typedef struct _nt_afd_recv_info {
dd89bb
	nt_afd_buffer *		afd_buffer_array;
dd89bb
	uint32_t		buffer_count;
dd89bb
	uint32_t		afd_flags;
dd89bb
	uint32_t		tdi_flags;
dd89bb
} nt_afd_recv_info;
dd89bb
dd89bb
dd89bb
typedef struct _nt_afd_disconnect_info {
dd89bb
	uint32_t	shutdown_flags;
dd89bb
	uint32_t	unknown[3];
dd89bb
} nt_afd_disconnect_info;
dd89bb
dd89bb
dd89bb
/* socket functions */
dd89bb
typedef int32_t __cdecl ntapi_sc_socket(
dd89bb
	__out	nt_socket *		hssocket,
dd89bb
	__in	uint16_t		domain,
dd89bb
	__in	uint16_t		type,
dd89bb
	__in	uint32_t		protocol,
dd89bb
	__in	uint32_t		desired_access	__optional,
dd89bb
	__in	nt_sqos *		sqos		__optional,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_bind(
dd89bb
	__in	nt_socket *		hssocket,
dd89bb
	__in	const nt_sockaddr *	addr,
dd89bb
	__in	uintptr_t		addrlen,
dd89bb
	__in	uintptr_t		service_flags	__optional,
dd89bb
	__out	nt_sockaddr *		sockaddr	__optional,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_listen(
dd89bb
	__in	nt_socket *		hssocket,
dd89bb
	__in	uintptr_t		backlog,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_accept(
dd89bb
	__in	nt_socket *		hssock_listen,
dd89bb
	__in	nt_sockaddr *		addr,
dd89bb
	__in	uint16_t *		addrlen,
dd89bb
	__out	nt_socket *		hssock_dedicated,
dd89bb
	__in	uintptr_t		afdflags	__optional,
dd89bb
	__in	uintptr_t		tdiflags	__optional,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_connect(
dd89bb
	__in	nt_socket *		hssocket,
dd89bb
	__in	nt_sockaddr *		addr,
dd89bb
	__in	uintptr_t		addrlen,
dd89bb
	__in	uintptr_t		service_flags	__optional,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_send(
dd89bb
	__in	nt_socket *		hssocket,
dd89bb
	__in	const void *		buffer,
dd89bb
	__in	size_t			len,
dd89bb
	__out	ssize_t *		bytes_sent	__optional,
dd89bb
	__in	uintptr_t		afdflags	__optional,
dd89bb
	__in	uintptr_t		tdiflags	__optional,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_recv(
dd89bb
	__in	nt_socket *		hssocket,
dd89bb
	__in	const void *		buffer,
dd89bb
	__in	size_t			len,
dd89bb
	__out	ssize_t *		bytes_received	__optional,
dd89bb
	__in	uintptr_t		afdflags	__optional,
dd89bb
	__in	uintptr_t		tdiflags	__optional,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_shutdown(
dd89bb
	__in	nt_socket *		hssocket,
dd89bb
	__in	uintptr_t		psxhow,
dd89bb
	__in	uintptr_t		afdhow,
dd89bb
	__out	nt_io_status_block *	iosb	__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_getsockname(
dd89bb
	__in	nt_socket *		hssocket,
dd89bb
	__in	nt_sockaddr *		addr,
dd89bb
	__in	uint16_t *		addrlen,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_server_accept_connection(
dd89bb
	__in	nt_socket *		hssocket,
dd89bb
	__out	nt_afd_accept_info *	accept_info,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_server_duplicate_socket(
dd89bb
	__in	nt_socket *		hssock_listen,
dd89bb
	__in	nt_socket *		hssock_dedicated,
dd89bb
	__in	nt_afd_accept_info *	accept_info,
dd89bb
	__out	nt_io_status_block *	iosb		__optional);
dd89bb
dd89bb
dd89bb
typedef int32_t __cdecl ntapi_sc_wait(nt_socket * hssocket, nt_iosb * iosb, nt_timeout * timeout);
dd89bb
dd89bb
#endif