Blame src/ipc/ntapi_ipc_create_pipe_v1.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
64e606
/*  Copyright (C) 2013--2021  SysDeer Technologies, LLC */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_file.h>
dd89bb
#include <ntapi/nt_string.h>
dd89bb
#include <ntapi/nt_atomic.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
dd89bb
typedef struct __attr_ptr_size_aligned__ _nt_tty_pipe_name {
dd89bb
	wchar16_t	pipe_dir[8];
dd89bb
	wchar16_t	back_slash;
dd89bb
	wchar16_t	key_1st[8];
dd89bb
	wchar16_t	uscore_1st;
dd89bb
	wchar16_t	key_2nd[8];
dd89bb
	wchar16_t	uscore_2nd;
dd89bb
	wchar16_t	key_3rd[8];
dd89bb
	wchar16_t	uscore_3rd;
dd89bb
	wchar16_t	key_4th[8];
dd89bb
	wchar16_t	uscore_4th;
dd89bb
	wchar16_t	key_5th[8];
dd89bb
	wchar16_t	uscore_5th;
dd89bb
	wchar16_t	key_6th[8];
dd89bb
	wchar16_t	null_termination;
3e609e
	void *		pad;
dd89bb
} nt_tty_pipe_name;
dd89bb
dd89bb
dd89bb
int32_t __stdcall	__ntapi_ipc_create_pipe_v1(
dd89bb
	__out		void **			hpipe_read,
dd89bb
	__out		void **			hpipe_write,
842d0f
	__in		uint32_t		advisory_buffer_size)
dd89bb
{
dd89bb
	int32_t			status;
dd89bb
dd89bb
	void *				hread;
dd89bb
	void *				hwrite;
dd89bb
dd89bb
	nt_object_attributes		oa;
dd89bb
	nt_io_status_block		iosb;
dd89bb
	nt_unicode_string		nt_name;
dd89bb
	nt_security_quality_of_service	sqos;
dd89bb
	nt_large_integer		timeout;
dd89bb
	intptr_t *			counter;
dd89bb
dd89bb
	nt_tty_pipe_name pipe_name =  {
dd89bb
		{'\\','?','?','\\','p','i','p','e'},
dd89bb
		'\\',
dd89bb
		{0},'_',
dd89bb
		{0},'_',
dd89bb
		{0},'_',
dd89bb
		{0},'_',
dd89bb
		{0},'_',
dd89bb
		{0},
3e609e
		0,0
dd89bb
	};
dd89bb
c713d8
	(void)advisory_buffer_size;
c713d8
dd89bb
	/* pipe_count  */
dd89bb
	counter = (intptr_t *)&__ntapi_internals()->v1_pipe_counter;
dd89bb
	at_locked_inc(counter);
dd89bb
dd89bb
	/* get system time */
dd89bb
	status = __ntapi->zw_query_system_time(&timeout);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS)
dd89bb
		return status;
dd89bb
dd89bb
	/* pipe name (no anonymous pipe prior to vista) */
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(	pe_get_current_process_id(),pipe_name.key_1st);
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(	pe_get_current_thread_id(),pipe_name.key_2nd);
dd89bb
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(	timeout.ihigh + (uint32_t)*counter,pipe_name.key_3rd);
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(timeout.ulow + (uint32_t)*counter,pipe_name.key_4th);
dd89bb
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(
dd89bb
		__ntapi->tt_buffer_crc32(0,(char *)&pipe_name,sizeof(pipe_name)),
dd89bb
		pipe_name.key_5th);
dd89bb
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(
dd89bb
		__ntapi->tt_buffer_crc32(0,(char *)&pipe_name,sizeof(pipe_name)),
dd89bb
		pipe_name.key_6th);
dd89bb
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(
dd89bb
		__ntapi->tt_buffer_crc32(0,(char *)&pipe_name,sizeof(pipe_name)),
dd89bb
		pipe_name.key_1st);
dd89bb
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(
dd89bb
		__ntapi->tt_buffer_crc32(0,(char *)&pipe_name,sizeof(pipe_name)),
dd89bb
		pipe_name.key_2nd);
dd89bb
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(
dd89bb
		__ntapi->tt_buffer_crc32(0,(char *)&pipe_name,sizeof(pipe_name)),
dd89bb
		pipe_name.key_3rd);
dd89bb
dd89bb
	__ntapi->tt_uint32_to_hex_utf16(
dd89bb
		__ntapi->tt_buffer_crc32(0,(char *)&pipe_name,sizeof(pipe_name)),
dd89bb
		pipe_name.key_4th);
dd89bb
dd89bb
	/* nt_name */
dd89bb
	nt_name.strlen = (uint16_t)(sizeof(pipe_name) - sizeof(wchar16_t));
dd89bb
	nt_name.maxlen = (uint16_t)(sizeof(pipe_name));
dd89bb
	nt_name.buffer = (uint16_t *)&pipe_name;
dd89bb
dd89bb
	/* init security structure */
dd89bb
	sqos.length 			= sizeof(sqos);
842d0f
	sqos.impersonation_level	= NT_SECURITY_ANONYMOUS;
842d0f
	sqos.context_tracking_mode	= NT_SECURITY_TRACKING_STATIC;
dd89bb
	sqos.effective_only		= 1;
dd89bb
dd89bb
	/* oa */
dd89bb
	oa.len		= sizeof(oa);
dd89bb
	oa.root_dir	= (void *)0;
dd89bb
	oa.obj_name	= &nt_name;
dd89bb
	oa.obj_attr	= 0x0;
dd89bb
	oa.sec_desc	= (nt_security_descriptor *)0;
dd89bb
	oa.sec_qos	= &sqo;;
dd89bb
dd89bb
	timeout.ihigh = 0xffffffff;
dd89bb
	timeout.ulow  = 0x0;
dd89bb
dd89bb
	/* the reading end */
dd89bb
	status = __ntapi->zw_create_named_pipe_file(
dd89bb
		&hread,
dd89bb
		NT_GENERIC_READ | NT_SEC_SYNCHRONIZE | NT_FILE_WRITE_ATTRIBUTES,
dd89bb
		&oa,
dd89bb
		&iosb,
dd89bb
		NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
dd89bb
		NT_FILE_CREATE,
dd89bb
		NT_FILE_ASYNCHRONOUS_IO,
842d0f
		NT_FILE_PIPE_BYTE_STREAM_TYPE,
842d0f
		NT_FILE_PIPE_BYTE_STREAM_MODE,
842d0f
		NT_FILE_PIPE_QUEUE_OPERATION,
dd89bb
		1,
842d0f
		advisory_buffer_size,
842d0f
		advisory_buffer_size,
dd89bb
		&timeout);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS) {
dd89bb
		return status;
dd89bb
	}
dd89bb
dd89bb
	/* the writing end(s) */
dd89bb
	status = __ntapi->zw_open_file(
dd89bb
		&hwrite,
dd89bb
		NT_GENERIC_WRITE | NT_SEC_SYNCHRONIZE | NT_FILE_READ_ATTRIBUTES,
dd89bb
		&oa,
dd89bb
		&iosb,
dd89bb
		NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
842d0f
		NT_FILE_WRITE_THROUGH | NT_FILE_SYNCHRONOUS_IO_ALERT | NT_FILE_NON_DIRECTORY_FILE);
dd89bb
dd89bb
	if (status != NT_STATUS_SUCCESS) {
dd89bb
		__ntapi->zw_close(hread);
dd89bb
		return status;
dd89bb
	}
dd89bb
dd89bb
	*hpipe_read  = hread;
dd89bb
	*hpipe_write = hwrite;
dd89bb
dd89bb
	return status;
dd89bb
}