Blame src/socket/ntapi_sc_socket_v1.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dde53a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_object.h>
dd89bb
#include <ntapi/nt_file.h>
dd89bb
#include <ntapi/nt_socket.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
typedef struct _nt_afd_socket_ea {
dd89bb
	uint32_t	next_entry_offset;
dd89bb
	unsigned char	ea_flags;
dd89bb
	unsigned char	ea_name_length;
dd89bb
	uint16_t	ea_value_length;
dd89bb
	char		afd_open_packet[0x10];
dd89bb
	uint32_t	value_1st;
dd89bb
	uint32_t	value_2nd;
dd89bb
	uint32_t	device_name_length;
dd89bb
	wchar16_t	device_name[0x0b];
dd89bb
	uint32_t	ea_ext[4];
dd89bb
} nt_afd_socket_ea;
dd89bb
dd89bb
int32_t __cdecl __ntapi_sc_socket_v1(
dd89bb
	__out	nt_socket *		hssocket,
9c3b7e
	__in	int32_t			domain,
9c3b7e
	__in	int32_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
	int32_t			status;
dd89bb
	nt_object_attributes	oa;
dd89bb
	nt_io_status_block	siosb;
dd89bb
	nt_sqos			ssqos;
dd89bb
	nt_unicode_string	nt_afdep;
dd89bb
	uint32_t		ea_length;
49a598
	uint16_t		sdomain;
49a598
	uint16_t		stype;
dd89bb
	void *			_hsocket;
dd89bb
dd89bb
	wchar16_t afd_end_point[] = {
dd89bb
		'\\','D','e','v','i','c','e',
dd89bb
		'\\','A','f','d',
dd89bb
		'\\','E','n','d','P','o','i','n','t',
dd89bb
		0};
dd89bb
49a598
	if (domain >= 0x10000)
49a598
		return NT_STATUS_INVALID_PARAMETER_2;
49a598
	else if (type > 0x10000)
49a598
		return NT_STATUS_INVALID_PARAMETER_3;
49a598
dd89bb
	/* tcp as default extended attribute */
dd89bb
	nt_afd_socket_ea afd_ea = {
dd89bb
		0,
dd89bb
		0,
dd89bb
		0x0f,
dd89bb
		0x28,
dd89bb
		{'A','f','d','O','p','e','n','P','a','c','k','e','t','X','X',0},
dd89bb
		0,0,
dd89bb
		0x16,
dd89bb
		{'\\','D','e','v','i','c','e','\\','T','c','p'},
dd89bb
		{0}};
dd89bb
dd89bb
	ea_length = 0x43;
dd89bb
dd89bb
	__ntapi->rtl_init_unicode_string(&nt_afdep,afd_end_point);
dd89bb
dd89bb
	if (!desired_access)
dd89bb
		desired_access = NT_GENERIC_READ \
dd89bb
				| NT_GENERIC_WRITE \
dd89bb
				| NT_SEC_SYNCHRONIZE \
dd89bb
				| NT_SEC_WRITE_DAC;
dd89bb
dd89bb
	if (!sqos) {
dd89bb
		ssqos.length 			= sizeof(ssqos);
dd89bb
		ssqos.impersonation_level	= NT_SECURITY_IMPERSONATION;
dd89bb
		ssqos.context_tracking_mode	= NT_SECURITY_TRACKING_DYNAMIC;
dd89bb
		ssqos.effective_only		= 1;
dd89bb
		sqos 				= &ssqos;
dd89bb
	}
dd89bb
dd89bb
	oa.len		= sizeof(oa);
dd89bb
	oa.root_dir	= (void *)0;
dd89bb
	oa.obj_name	= &nt_afdep;
dd89bb
	oa.obj_attr	= NT_OBJ_CASE_INSENSITIVE | NT_OBJ_INHERIT;
dd89bb
	oa.sec_desc	= (nt_security_descriptor *)0;
dd89bb
	oa.sec_qos	= sqos;
dd89bb
dd89bb
	iosb = iosb ? iosb : &siosb;
dd89bb
dd89bb
	if ((status = __ntapi->zw_create_file(
dd89bb
			&_hsocket,
dd89bb
			desired_access,
dd89bb
			&oa,
dd89bb
			iosb,
dd89bb
			0,
dd89bb
			0,
dd89bb
			NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
dd89bb
			NT_FILE_OPEN_IF,
dd89bb
			0,
dd89bb
			&afd_ea,
dd89bb
			ea_length)))
dd89bb
		return status;
dd89bb
dd89bb
	oa.obj_name = 0;
dd89bb
	oa.obj_attr = 0;
dd89bb
49a598
	sdomain = (uint16_t)domain;
49a598
	stype   = (uint16_t)type;
49a598
dd89bb
	if (status == NT_STATUS_SUCCESS) {
dd89bb
		hssocket->hsocket	= _hsocket;
dd89bb
		hssocket->ntflags	= 0;
49a598
		hssocket->domain 	= sdomain;
49a598
		hssocket->type		= stype;
dd89bb
		hssocket->protocol	= protocol;
dd89bb
		hssocket->timeout.quad	= 0;
dd89bb
		hssocket->iostatus	= NT_STATUS_SUCCESS;
dd89bb
		hssocket->waitstatus	= NT_STATUS_SUCCESS;
dd89bb
	}
dd89bb
dd89bb
	return status;
dd89bb
}