|
|
30d28d |
/********************************************************/
|
|
|
30d28d |
/* ntapi: Native API core library */
|
|
|
30d28d |
/* Copyright (C) 2013--2017 Z. Gilboa */
|
|
|
30d28d |
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
|
|
|
30d28d |
/********************************************************/
|
|
|
30d28d |
|
|
|
30d28d |
#include <psxtypes/psxtypes.h>
|
|
|
30d28d |
#include <ntapi/nt_file.h>
|
|
|
30d28d |
#include <ntapi/nt_string.h>
|
|
|
30d28d |
#include <ntapi/nt_atomic.h>
|
|
|
30d28d |
#include <ntapi/ntapi.h>
|
|
|
30d28d |
#include "ntapi_impl.h"
|
|
|
30d28d |
|
|
|
30d28d |
static int32_t __ipc_connect_return(
|
|
|
30d28d |
intptr_t * hlock,
|
|
|
30d28d |
int32_t status)
|
|
|
30d28d |
{
|
|
|
30d28d |
at_store(hlock,0);
|
|
|
30d28d |
return status;
|
|
|
30d28d |
}
|
|
|
30d28d |
|
|
|
30d28d |
|
|
|
30d28d |
static int32_t __ipc_connect_by_attr(
|
|
|
30d28d |
void ** hport,
|
|
|
30d28d |
const nt_port_attr * attr,
|
|
|
30d28d |
nt_unicode_string * str,
|
|
|
30d28d |
void * hconn,
|
|
|
30d28d |
__out void ** hsection,
|
|
|
30d28d |
__out void ** secaddr,
|
|
|
30d28d |
__out size_t * secsize)
|
|
|
30d28d |
{
|
|
|
30d28d |
int32_t status;
|
|
|
30d28d |
struct dalist_node_ex * node;
|
|
|
30d28d |
const nt_port_attr * conn;
|
|
|
30d28d |
nt_port_attr * nconn;
|
|
|
30d28d |
nt_ipc_conn * ipc;
|
|
|
30d28d |
intptr_t * hlock;
|
|
|
30d28d |
ntapi_internals * __internals;
|
|
|
30d28d |
|
|
|
30d28d |
/* init */
|
|
|
30d28d |
__internals = __ntapi_internals();
|
|
|
30d28d |
|
|
|
30d28d |
/* lock */
|
|
|
30d28d |
hlock = &(__internals->hlock);
|
|
|
30d28d |
|
|
|
30d28d |
if (at_locked_cas(hlock,0,1))
|
|
|
30d28d |
return NT_STATUS_RESOURCE_NOT_OWNED;
|
|
|
30d28d |
|
|
|
30d28d |
/* already connected? */
|
|
|
30d28d |
node = (struct dalist_node_ex *)__internals->ipc_conns.head;
|
|
|
30d28d |
|
|
|
30d28d |
for (; node; node=node->next) {
|
|
|
30d28d |
ipc = (nt_ipc_conn *)&node->dblock;
|
|
|
30d28d |
conn = &ipc->attr;
|
|
|
30d28d |
|
|
|
30d28d |
if ((attr->keys.key[0] == conn->keys.key[0])
|
|
|
30d28d |
&& (attr->keys.key[1] == conn->keys.key[1])
|
|
|
30d28d |
&& (attr->keys.key[2] == conn->keys.key[2])
|
|
|
30d28d |
&& (attr->keys.key[3] == conn->keys.key[3])
|
|
|
30d28d |
&& (attr->keys.key[4] == conn->keys.key[4])
|
|
|
30d28d |
&& (attr->keys.key[5] == conn->keys.key[5])
|
|
|
30d28d |
&& !__ntapi->tt_guid_compare(
|
|
|
30d28d |
&attr->guid,
|
|
|
30d28d |
&conn->guid)) {
|
|
|
30d28d |
/* already connected */
|
|
|
30d28d |
if (hconn)
|
|
|
30d28d |
return __ipc_connect_return(
|
|
|
30d28d |
hlock,
|
|
|
30d28d |
((uintptr_t)hconn == node->key)
|
|
|
30d28d |
? NT_STATUS_SUCCESS
|
|
|
30d28d |
: NT_STATUS_CONTEXT_MISMATCH);
|
|
|
30d28d |
|
|
|
30d28d |
*hport = (void *)node->key;
|
|
|
30d28d |
*hsection = ipc->hsection;
|
|
|
30d28d |
*secaddr = ipc->secaddr;
|
|
|
30d28d |
*secsize = ipc->secsize;
|
|
|
30d28d |
|
|
|
30d28d |
return __ipc_connect_return(hlock,NT_STATUS_SUCCESS);
|
|
|
30d28d |
}
|
|
|
30d28d |
}
|
|
|
30d28d |
|
|
|
30d28d |
/* allocate list node */
|
|
|
30d28d |
if ((status = dalist_get_free_node(
|
|
|
30d28d |
&__internals->ipc_conns,
|
|
|
30d28d |
(void **)&node)))
|
|
|
30d28d |
return __ipc_connect_return(hlock,status);
|
|
|
30d28d |
|
|
|
30d28d |
/* connect as needed */
|
|
|
30d28d |
if (!hconn) {
|
|
|
30d28d |
status = __ntapi->zw_connect_port(
|
|
|
30d28d |
&hconn,str,0,0,0,0,0,0);
|
|
|
30d28d |
|
|
|
30d28d |
if (status) {
|
|
|
30d28d |
dalist_deposit_free_node(
|
|
|
30d28d |
&__internals->ipc_conns,
|
|
|
30d28d |
node);
|
|
|
30d28d |
|
|
|
30d28d |
return __ipc_connect_return(hlock,status);
|
|
|
30d28d |
}
|
|
|
30d28d |
}
|
|
|
30d28d |
|
|
|
30d28d |
/* add connection */
|
|
|
30d28d |
node->key = (uintptr_t)hconn;
|
|
|
30d28d |
ipc = (nt_ipc_conn *)&node->dblock;
|
|
|
30d28d |
nconn = &ipc->attr;
|
|
|
30d28d |
|
|
|
30d28d |
__ntapi->tt_aligned_block_memcpy(
|
|
|
30d28d |
(uintptr_t *)nconn,
|
|
|
30d28d |
(uintptr_t *)attr,
|
|
|
30d28d |
sizeof(nt_port_attr));
|
|
|
30d28d |
|
|
|
30d28d |
ipc->hsection = 0;
|
|
|
30d28d |
ipc->secaddr = 0;
|
|
|
30d28d |
ipc->secsize = 0;
|
|
|
30d28d |
|
|
|
30d28d |
dalist_insert_node_by_key(
|
|
|
30d28d |
&__internals->ipc_conns,
|
|
|
30d28d |
node);
|
|
|
30d28d |
|
|
|
30d28d |
/* all done */
|
|
|
30d28d |
*hport = hconn;
|
|
|
30d28d |
*hsection = 0;
|
|
|
30d28d |
*secaddr = 0;
|
|
|
30d28d |
*secsize = 0;
|
|
|
30d28d |
|
|
|
30d28d |
return __ipc_connect_return(hlock,NT_STATUS_SUCCESS);
|
|
|
30d28d |
}
|
|
|
30d28d |
|
|
|
30d28d |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_connect_section_by_attr(
|
|
|
30d28d |
__out void ** hport,
|
|
|
1b6aec |
__in nt_port_attr * attr,
|
|
|
1b6aec |
__out void ** hsection,
|
|
|
1b6aec |
__out void ** secaddr,
|
|
|
1b6aec |
__out size_t * secsize)
|
|
|
30d28d |
{
|
|
|
30d28d |
nt_port_name name;
|
|
|
30d28d |
nt_unicode_string str;
|
|
|
30d28d |
|
|
|
30d28d |
__ntapi->tt_port_name_from_attr(
|
|
|
30d28d |
&name,attr);
|
|
|
30d28d |
|
|
|
30d28d |
str.strlen = ((size_t)&(((nt_port_name *)0)->null_termination));
|
|
|
30d28d |
str.maxlen = 0;
|
|
|
30d28d |
str.buffer = &name.base_named_objects[0];
|
|
|
30d28d |
|
|
|
30d28d |
return __ipc_connect_by_attr(
|
|
|
30d28d |
hport,attr,&str,0,
|
|
|
1b6aec |
hsection,secaddr,secsize);
|
|
|
30d28d |
}
|
|
|
30d28d |
|
|
|
30d28d |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_connect_section_by_name(
|
|
|
30d28d |
__out void ** hport,
|
|
|
1b6aec |
__in nt_port_name * name,
|
|
|
1b6aec |
__out void ** hsection,
|
|
|
1b6aec |
__out void ** secaddr,
|
|
|
1b6aec |
__out size_t * secsize)
|
|
|
30d28d |
{
|
|
|
30d28d |
int32_t status;
|
|
|
30d28d |
nt_port_attr attr;
|
|
|
30d28d |
nt_unicode_string str;
|
|
|
30d28d |
|
|
|
30d28d |
if ((status = __ntapi->tt_port_attr_from_name(&attr,name)))
|
|
|
30d28d |
return status;
|
|
|
30d28d |
|
|
|
30d28d |
str.strlen = ((size_t)&(((nt_port_name *)0)->null_termination));
|
|
|
30d28d |
str.maxlen = 0;
|
|
|
30d28d |
str.buffer = &name->base_named_objects[0];
|
|
|
30d28d |
|
|
|
30d28d |
return __ipc_connect_by_attr(
|
|
|
30d28d |
hport,&attr,&str,0,
|
|
|
1b6aec |
hsection,secaddr,secsize);
|
|
|
30d28d |
}
|
|
|
30d28d |
|
|
|
30d28d |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_connect_section_by_symlink(
|
|
|
30d28d |
__out void ** hport,
|
|
|
1b6aec |
__in void * hsymlink,
|
|
|
1b6aec |
__out void ** hsection,
|
|
|
1b6aec |
__out void ** secaddr,
|
|
|
1b6aec |
__out size_t * secsize)
|
|
|
30d28d |
{
|
|
|
30d28d |
int32_t status;
|
|
|
30d28d |
nt_port_attr attr;
|
|
|
30d28d |
nt_port_name * name;
|
|
|
30d28d |
size_t namelen;
|
|
|
30d28d |
uintptr_t buffer[512/sizeof(uintptr_t)];
|
|
|
30d28d |
nt_unicode_string * str;
|
|
|
30d28d |
|
|
|
30d28d |
str = (nt_unicode_string *)buffer;
|
|
|
30d28d |
str->strlen = 0;
|
|
|
30d28d |
str->maxlen = sizeof(buffer) - sizeof(nt_unicode_string);
|
|
|
30d28d |
str->buffer = (wchar16_t *)&str[1];
|
|
|
30d28d |
|
|
|
30d28d |
if ((status = __ntapi->zw_query_symbolic_link_object(
|
|
|
30d28d |
hsymlink,str,&namelen)))
|
|
|
30d28d |
return status;
|
|
|
30d28d |
|
|
|
30d28d |
if (str->strlen != ((size_t)&(((nt_port_name *)0)->null_termination)))
|
|
|
30d28d |
return NT_STATUS_INVALID_PORT_ATTRIBUTES;
|
|
|
30d28d |
|
|
|
30d28d |
name = (nt_port_name *)str->buffer;
|
|
|
30d28d |
|
|
|
30d28d |
if ((status = __ntapi->tt_port_attr_from_name(&attr,name)))
|
|
|
30d28d |
return status;
|
|
|
30d28d |
|
|
|
30d28d |
return __ipc_connect_by_attr(
|
|
|
30d28d |
hport,&attr,str,0,
|
|
|
1b6aec |
hsection,secaddr,secsize);
|
|
|
1b6aec |
}
|
|
|
1b6aec |
|
|
|
1b6aec |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_connect_section_by_port(
|
|
|
1b6aec |
__in void * hconn,
|
|
|
1b6aec |
__in nt_port_attr * attr,
|
|
|
1b6aec |
__out void ** hsection,
|
|
|
1b6aec |
__out void ** secaddr,
|
|
|
1b6aec |
__out size_t * secsize)
|
|
|
1b6aec |
{
|
|
|
1b6aec |
return __ipc_connect_by_attr(
|
|
|
1b6aec |
&(void *){0},attr,0,hconn,
|
|
|
1b6aec |
hsection,secaddr,secsize);
|
|
|
1b6aec |
}
|
|
|
1b6aec |
|
|
|
1b6aec |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_connect_by_attr(
|
|
|
1b6aec |
__out void ** hport,
|
|
|
1b6aec |
__in nt_port_attr * attr)
|
|
|
1b6aec |
{
|
|
|
1b6aec |
return __ntapi_ipc_connect_section_by_attr(
|
|
|
1b6aec |
hport,attr,
|
|
|
1b6aec |
&(void *){0},
|
|
|
1b6aec |
&(void *){0},
|
|
|
1b6aec |
&(size_t){0});
|
|
|
1b6aec |
}
|
|
|
1b6aec |
|
|
|
1b6aec |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_connect_by_name(
|
|
|
1b6aec |
__out void ** hport,
|
|
|
1b6aec |
__in nt_port_name * name)
|
|
|
1b6aec |
{
|
|
|
1b6aec |
return __ntapi_ipc_connect_section_by_name(
|
|
|
1b6aec |
hport,name,
|
|
|
1b6aec |
&(void *){0},
|
|
|
1b6aec |
&(void *){0},
|
|
|
1b6aec |
&(size_t){0});
|
|
|
1b6aec |
}
|
|
|
1b6aec |
|
|
|
1b6aec |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_connect_by_symlink(
|
|
|
1b6aec |
__out void ** hport,
|
|
|
1b6aec |
__in void * hsymlink)
|
|
|
1b6aec |
{
|
|
|
1b6aec |
return __ntapi_ipc_connect_section_by_symlink(
|
|
|
1b6aec |
hport,hsymlink,
|
|
|
30d28d |
&(void *){0},
|
|
|
30d28d |
&(void *){0},
|
|
|
30d28d |
&(size_t){0});
|
|
|
30d28d |
}
|
|
|
30d28d |
|
|
|
30d28d |
|
|
|
30d28d |
int32_t __stdcall __ntapi_ipc_connect_by_port(
|
|
|
30d28d |
__in void * hconn,
|
|
|
30d28d |
__in nt_port_attr * attr)
|
|
|
30d28d |
{
|
|
|
1b6aec |
return __ntapi_ipc_connect_section_by_port(
|
|
|
1b6aec |
hconn,attr,
|
|
|
30d28d |
&(void *){0},
|
|
|
30d28d |
&(void *){0},
|
|
|
30d28d |
&(size_t){0});
|
|
|
30d28d |
}
|
|
|
30d28d |
|
|
|
30d28d |
|
|
|
30d28d |
int __ntapi_ipc_page_alloc(
|
|
|
30d28d |
struct dalist_ex * dlist,
|
|
|
30d28d |
void ** addr,
|
|
|
30d28d |
size_t * alloc_size)
|
|
|
30d28d |
{
|
|
|
30d28d |
int32_t status;
|
|
|
30d28d |
ntapi_internals * __internals;
|
|
|
30d28d |
|
|
|
30d28d |
__internals = __ntapi_internals();
|
|
|
30d28d |
|
|
|
30d28d |
if (__internals->ipc_page == __NT_IPC_PAGES)
|
|
|
30d28d |
return NT_STATUS_QUOTA_EXCEEDED;
|
|
|
30d28d |
|
|
|
30d28d |
if ((status = __ntapi->zw_allocate_virtual_memory(
|
|
|
30d28d |
NT_CURRENT_PROCESS_HANDLE,
|
|
|
30d28d |
addr,0,alloc_size,
|
|
|
30d28d |
NT_MEM_COMMIT,
|
|
|
30d28d |
NT_PAGE_READWRITE)))
|
|
|
30d28d |
return status;
|
|
|
30d28d |
|
|
|
30d28d |
dalist_deposit_memory_block(
|
|
|
30d28d |
dlist,*addr,*alloc_size);
|
|
|
30d28d |
|
|
|
30d28d |
__internals->ipc_pages[__internals->ipc_page++] = *addr;
|
|
|
30d28d |
|
|
|
30d28d |
return 0;
|
|
|
30d28d |
}
|
|
|
1b6aec |
|
|
|
1b6aec |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_init_section_by_port(
|
|
|
1b6aec |
__in void * hconn,
|
|
|
1b6aec |
__out void ** hsection,
|
|
|
1b6aec |
__out void ** secaddr,
|
|
|
1b6aec |
__out size_t * secsize)
|
|
|
1b6aec |
{
|
|
|
1b6aec |
int32_t status;
|
|
|
1b6aec |
nt_tty_section_info secinfo;
|
|
|
1b6aec |
nt_iosb iosb;
|
|
|
1b6aec |
struct dalist_node_ex * node;
|
|
|
1b6aec |
nt_ipc_conn * ipc;
|
|
|
1b6aec |
void * addr;
|
|
|
1b6aec |
size_t size;
|
|
|
1b6aec |
ntapi_internals * __internals;
|
|
|
1b6aec |
|
|
|
1b6aec |
/* init */
|
|
|
1b6aec |
__internals = __ntapi_internals();
|
|
|
1b6aec |
|
|
|
1b6aec |
/* lock */
|
|
|
1b6aec |
if (at_locked_cas(&__internals->hlock,0,1))
|
|
|
1b6aec |
return NT_STATUS_RESOURCE_NOT_OWNED;
|
|
|
1b6aec |
|
|
|
1b6aec |
/* connection node */
|
|
|
1b6aec |
if ((status = dalist_get_node_by_key(
|
|
|
1b6aec |
&__internals->ipc_conns,
|
|
|
1b6aec |
&node,(uintptr_t)hconn,
|
|
|
1b6aec |
DALIST_NODE_TYPE_EXISTING,
|
|
|
1b6aec |
&(uintptr_t){0})))
|
|
|
1b6aec |
return __ipc_connect_return(
|
|
|
1b6aec |
&__internals->hlock,
|
|
|
1b6aec |
NT_STATUS_INTERNAL_ERROR);
|
|
|
1b6aec |
|
|
|
1b6aec |
else if (!node)
|
|
|
1b6aec |
return NT_STATUS_NOT_FOUND;
|
|
|
1b6aec |
|
|
|
1b6aec |
else
|
|
|
1b6aec |
ipc = (nt_ipc_conn *)&node->dblock;
|
|
|
1b6aec |
|
|
|
1b6aec |
/* already mapped? */
|
|
|
1b6aec |
if (ipc->secaddr)
|
|
|
1b6aec |
return __ipc_connect_return(
|
|
|
1b6aec |
&__internals->hlock,
|
|
|
1b6aec |
NT_STATUS_SUCCESS);
|
|
|
1b6aec |
|
|
|
1b6aec |
/* section info */
|
|
|
1b6aec |
if ((status = __ntapi->tty_query_information_section(
|
|
|
1b6aec |
hconn,&iosb,&secinfo,0)))
|
|
|
1b6aec |
return __ipc_connect_return(
|
|
|
1b6aec |
&__internals->hlock,
|
|
|
1b6aec |
status);
|
|
|
1b6aec |
|
|
|
1b6aec |
/* map section */
|
|
|
1b6aec |
addr = 0;
|
|
|
1b6aec |
size = 0;
|
|
|
1b6aec |
|
|
|
1b6aec |
if ((status = __ntapi->zw_map_view_of_section(
|
|
|
1b6aec |
secinfo.section,
|
|
|
1b6aec |
NT_CURRENT_PROCESS_HANDLE,
|
|
|
1b6aec |
&addr,0,
|
|
|
1b6aec |
secinfo.section_size,0,&size,
|
|
|
1b6aec |
NT_VIEW_UNMAP,0,
|
|
|
1b6aec |
NT_PAGE_READWRITE)))
|
|
|
1b6aec |
return __ipc_connect_return(
|
|
|
1b6aec |
&__internals->hlock,
|
|
|
1b6aec |
status);
|
|
|
1b6aec |
|
|
|
1b6aec |
/* update */
|
|
|
1b6aec |
*hsection = secinfo.section;
|
|
|
1b6aec |
*secaddr = addr;
|
|
|
1b6aec |
*secsize = size;
|
|
|
1b6aec |
|
|
|
1b6aec |
/* all done */
|
|
|
1b6aec |
return __ipc_connect_return(
|
|
|
1b6aec |
&__internals->hlock,
|
|
|
1b6aec |
NT_STATUS_SUCCESS);
|
|
|
1b6aec |
}
|
|
|
1b6aec |
|
|
|
1b6aec |
|
|
|
1b6aec |
int32_t __stdcall __ntapi_ipc_disconnect_unmap_section_by_port(
|
|
|
1b6aec |
__in void * hconn)
|
|
|
1b6aec |
{
|
|
|
1b6aec |
int32_t status;
|
|
|
1b6aec |
struct dalist_node_ex * node;
|
|
|
1b6aec |
nt_ipc_conn * ipc;
|
|
|
1b6aec |
ntapi_internals * __internals;
|
|
|
1b6aec |
|
|
|
1b6aec |
/* init */
|
|
|
1b6aec |
__internals = __ntapi_internals();
|
|
|
1b6aec |
|
|
|
1b6aec |
/* lock */
|
|
|
1b6aec |
if (at_locked_cas(&__internals->hlock,0,1))
|
|
|
1b6aec |
return NT_STATUS_RESOURCE_NOT_OWNED;
|
|
|
1b6aec |
|
|
|
1b6aec |
/* connection node */
|
|
|
1b6aec |
if ((status = dalist_get_node_by_key(
|
|
|
1b6aec |
&__internals->ipc_conns,
|
|
|
1b6aec |
&node,(uintptr_t)hconn,
|
|
|
1b6aec |
DALIST_NODE_TYPE_EXISTING,
|
|
|
1b6aec |
&(uintptr_t){0})))
|
|
|
1b6aec |
return __ipc_connect_return(
|
|
|
1b6aec |
&__internals->hlock,
|
|
|
1b6aec |
NT_STATUS_INTERNAL_ERROR);
|
|
|
1b6aec |
|
|
|
1b6aec |
else if (!node)
|
|
|
1b6aec |
return __ipc_connect_return(
|
|
|
1b6aec |
&__internals->hlock,
|
|
|
1b6aec |
NT_STATUS_NOT_FOUND);
|
|
|
1b6aec |
|
|
|
1b6aec |
else
|
|
|
1b6aec |
ipc = (nt_ipc_conn *)&node->dblock;
|
|
|
1b6aec |
|
|
|
1b6aec |
/* unmap section */
|
|
|
1b6aec |
if (ipc->secaddr)
|
|
|
1b6aec |
__ntapi->zw_unmap_view_of_section(
|
|
|
1b6aec |
NT_CURRENT_PROCESS_HANDLE,
|
|
|
1b6aec |
ipc->secaddr);
|
|
|
1b6aec |
|
|
|
1b6aec |
/* close section */
|
|
|
1b6aec |
if (ipc->hsection)
|
|
|
1b6aec |
__ntapi->zw_close(ipc->hsection);
|
|
|
1b6aec |
|
|
|
1b6aec |
/* disconnect */
|
|
|
1b6aec |
__ntapi->zw_close(hconn);
|
|
|
1b6aec |
|
|
|
1b6aec |
/* remove node */
|
|
|
1b6aec |
dalist_discard_node(&__internals->ipc_conns,node);
|
|
|
1b6aec |
|
|
|
1b6aec |
return __ipc_connect_return(
|
|
|
1b6aec |
&__internals->hlock,
|
|
|
1b6aec |
NT_STATUS_SUCCESS);
|
|
|
1b6aec |
}
|