summaryrefslogtreecommitdiffhomepage
path: root/src/socket
diff options
context:
space:
mode:
Diffstat (limited to 'src/socket')
-rw-r--r--src/socket/ntapi_sc_accept.c79
-rw-r--r--src/socket/ntapi_sc_bind_v1.c101
-rw-r--r--src/socket/ntapi_sc_bind_v2.c85
-rw-r--r--src/socket/ntapi_sc_connect_v1.c93
-rw-r--r--src/socket/ntapi_sc_connect_v2.c69
-rw-r--r--src/socket/ntapi_sc_getsockname_v1.c80
-rw-r--r--src/socket/ntapi_sc_getsockname_v2.c42
-rw-r--r--src/socket/ntapi_sc_listen.c44
-rw-r--r--src/socket/ntapi_sc_recv.c63
-rw-r--r--src/socket/ntapi_sc_send.c59
-rw-r--r--src/socket/ntapi_sc_server_accept_connection_v1.c78
-rw-r--r--src/socket/ntapi_sc_server_accept_connection_v2.c44
-rw-r--r--src/socket/ntapi_sc_server_duplicate_socket.c45
-rw-r--r--src/socket/ntapi_sc_shutdown.c65
-rw-r--r--src/socket/ntapi_sc_socket_v1.c118
-rw-r--r--src/socket/ntapi_sc_socket_v2.c124
-rw-r--r--src/socket/ntapi_sc_wait.c42
17 files changed, 1231 insertions, 0 deletions
diff --git a/src/socket/ntapi_sc_accept.c b/src/socket/ntapi_sc_accept.c
new file mode 100644
index 0000000..a9f0a4e
--- /dev/null
+++ b/src/socket/ntapi_sc_accept.c
@@ -0,0 +1,79 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct __addr_memcpy {
+ uint64_t d0;
+ uint64_t d1;
+} _addr_memcpy;
+
+
+int32_t __cdecl __ntapi_sc_accept(
+ __in nt_socket * hssock_listen,
+ __out nt_sockaddr * addr,
+ __out uint16_t * addrlen,
+ __out nt_socket * hssock_dedicated,
+ __in uintptr_t afdflags __optional,
+ __in uintptr_t tdiflags __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ int32_t status;
+
+ nt_afd_accept_info accept_info;
+ nt_io_status_block siosb;
+
+ _addr_memcpy * src;
+ _addr_memcpy * dst;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* establish kernel connection */
+ if ((status = __ntapi->sc_server_accept_connection(
+ hssock_listen,
+ &accept_info,
+ iosb)))
+ return status;
+
+ /* create connection-dedicated socket handle */
+ if ((status = __ntapi->sc_socket(
+ hssock_dedicated,
+ hssock_listen->domain,
+ hssock_listen->type,
+ hssock_listen->protocol,
+ 0,
+ 0,
+ 0)))
+ return status;
+
+ /* associate the dedicated handle with the connection */
+ if ((status = __ntapi->sc_server_duplicate_socket(
+ hssock_listen,
+ hssock_dedicated,
+ &accept_info,
+ 0)))
+ return status;
+
+ /* return address information */
+ if (addr) {
+ src = (_addr_memcpy *)&(accept_info.addr);
+ dst = (_addr_memcpy *)addr;
+
+ dst->d0 = src->d0;
+ dst->d1 = src->d1;
+ }
+
+ /* return address length information */
+ if (addrlen)
+ *addrlen = sizeof(nt_sockaddr);
+
+ return status;
+}
diff --git a/src/socket/ntapi_sc_bind_v1.c b/src/socket/ntapi_sc_bind_v1.c
new file mode 100644
index 0000000..df66656
--- /dev/null
+++ b/src/socket/ntapi_sc_bind_v1.c
@@ -0,0 +1,101 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_afd_bind_msg {
+ uint32_t domain;
+ uint32_t type;
+ uint32_t service_flags;
+ char sa_data[14];
+} nt_afd_bind_msg;
+
+
+typedef struct __addr_memcpy {
+ uint16_t d0;
+ uint16_t d1;
+ uint16_t d2;
+ uint16_t d3;
+ uint16_t d4;
+ uint16_t d5;
+ uint16_t d6;
+ uint16_t d7;
+} _addr_memcpy;
+
+
+int32_t __cdecl __ntapi_sc_bind_v1(
+ __in nt_socket * hssocket,
+ __in const nt_sockaddr * addr,
+ __in uintptr_t addrlen,
+ __in uintptr_t service_flags __optional,
+ __out nt_sockaddr * sockaddr __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_io_status_block siosb;
+ nt_afd_bind_msg afd_bind_req;
+ nt_afd_bind_msg afd_bind_rep;
+
+ _addr_memcpy * src;
+ _addr_memcpy * dst;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* service_flags */
+ if (!service_flags)
+ service_flags = 0x2000E;
+
+ /* afd_bind_req */
+ afd_bind_req.domain = hssocket->domain;
+ afd_bind_req.type = hssocket->type;
+ afd_bind_req.service_flags = (uint32_t)service_flags;
+
+ src = (_addr_memcpy *)addr;
+ dst = (_addr_memcpy *)&(afd_bind_req.sa_data);
+
+ dst->d0 = src->d1;
+ dst->d1 = src->d2;
+ dst->d2 = src->d3;
+ dst->d3 = src->d4;
+ dst->d4 = src->d5;
+ dst->d5 = src->d6;
+ dst->d6 = src->d7;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_BIND,
+ &afd_bind_req,
+ sizeof(afd_bind_req),
+ &afd_bind_rep,
+ sizeof(afd_bind_rep));
+
+ __ntapi->sc_wait(hssocket,iosb,0);
+
+ if (!hssocket->iostatus && sockaddr) {
+ src = (_addr_memcpy *)&(afd_bind_rep.sa_data);
+ dst = (_addr_memcpy *)sockaddr;
+
+ dst->d1 = src->d0;
+ dst->d2 = src->d1;
+ dst->d3 = src->d2;
+ dst->d4 = src->d3;
+ dst->d5 = src->d4;
+ dst->d6 = src->d5;
+ dst->d7 = src->d6;
+
+ sockaddr->sa_addr_in4.sa_family = hssocket->domain;
+ }
+
+ return hssocket->iostatus;
+}
diff --git a/src/socket/ntapi_sc_bind_v2.c b/src/socket/ntapi_sc_bind_v2.c
new file mode 100644
index 0000000..f9b503b
--- /dev/null
+++ b/src/socket/ntapi_sc_bind_v2.c
@@ -0,0 +1,85 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_afd_bind_request {
+ uint32_t unknown;
+ nt_sockaddr addr;
+} nt_afd_bind_request;
+
+typedef struct _nt_afd_bind_reply {
+ nt_sockaddr addr;
+} nt_afd_bind_reply;
+
+typedef struct __addr_memcpy {
+ uint32_t d0;
+ uint32_t d1;
+ uint32_t d2;
+ uint32_t d3;
+} _addr_memcpy;
+
+
+int32_t __cdecl __ntapi_sc_bind_v2(
+ __in nt_socket * hssocket,
+ __in const nt_sockaddr * addr,
+ __in uintptr_t addrlen,
+ __in uintptr_t service_flags __optional,
+ __out nt_sockaddr * sockaddr __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_io_status_block siosb;
+ nt_afd_bind_request afd_bind_req;
+ nt_afd_bind_reply afd_bind_rep;
+
+ _addr_memcpy * src;
+ _addr_memcpy * dst;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* request */
+ afd_bind_req.unknown = hssocket->domain;
+
+ src = (_addr_memcpy *)addr;
+ dst = (_addr_memcpy *)&(afd_bind_req.addr);
+
+ dst->d0 = src->d0;
+ dst->d1 = src->d1;
+ dst->d2 = src->d2;
+ dst->d3 = src->d3;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_BIND,
+ &afd_bind_req,
+ sizeof(afd_bind_req),
+ &afd_bind_rep,
+ sizeof(afd_bind_rep));
+
+ __ntapi->sc_wait(hssocket,iosb,0);
+
+ if (!hssocket->iostatus && sockaddr) {
+ /* return updated address information */
+ src = (_addr_memcpy *)&(afd_bind_rep);
+ dst = (_addr_memcpy *)sockaddr;
+
+ dst->d0 = src->d0;
+ dst->d1 = src->d1;
+ dst->d2 = src->d2;
+ dst->d3 = src->d3;
+ }
+
+ return hssocket->iostatus;
+}
diff --git a/src/socket/ntapi_sc_connect_v1.c b/src/socket/ntapi_sc_connect_v1.c
new file mode 100644
index 0000000..380dbc9
--- /dev/null
+++ b/src/socket/ntapi_sc_connect_v1.c
@@ -0,0 +1,93 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+/* weed in Redmond during the 1990's anyone? */
+typedef struct _nt_afd_connect_request {
+ uintptr_t unknown;
+ void * paddr;
+ void * hasync;
+ uint32_t type;
+ uint32_t service_flags;
+ char sa_data[14];
+ uint16_t hangover;
+ uint32_t unused;
+} nt_afd_connect_request;
+
+typedef struct __addr_memcpy {
+ uint16_t d0;
+ uint16_t d1;
+ uint16_t d2;
+ uint16_t d3;
+ uint16_t d4;
+ uint16_t d5;
+ uint16_t d6;
+ uint16_t d7;
+} _addr_memcpy;
+
+int32_t __cdecl __ntapi_sc_connect_v1(
+ __in nt_socket * hssocket,
+ __in nt_sockaddr * addr,
+ __in uintptr_t addrlen,
+ __in uintptr_t service_flags __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_io_status_block siosb;
+ nt_afd_connect_request afd_connect_req;
+
+ _addr_memcpy * src;
+ _addr_memcpy * dst;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* service_flags */
+ if (!service_flags)
+ service_flags = 0x2000E;
+
+ /* afd_connect_req */
+ afd_connect_req.type = hssocket->type;
+ afd_connect_req.service_flags = (uint32_t)service_flags;
+
+ afd_connect_req.paddr = (void *)0;
+ afd_connect_req.hasync = (void *)0;
+
+ afd_connect_req.unknown = 0;
+ afd_connect_req.unused = 0;
+ afd_connect_req.hangover = 0;
+
+ src = (_addr_memcpy *)addr;
+ dst = (_addr_memcpy *)&(afd_connect_req.sa_data);
+
+ dst->d0 = src->d1;
+ dst->d1 = src->d2;
+ dst->d2 = src->d3;
+ dst->d3 = src->d4;
+ dst->d4 = src->d5;
+ dst->d5 = src->d6;
+ dst->d6 = src->d7;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_CONNECT,
+ &afd_connect_req,
+ sizeof(afd_connect_req),
+ (void *)0,
+ 0);
+
+ return hssocket->iostatus
+ ? __ntapi->sc_wait(hssocket,iosb,0)
+ : NT_STATUS_SUCCESS;
+}
diff --git a/src/socket/ntapi_sc_connect_v2.c b/src/socket/ntapi_sc_connect_v2.c
new file mode 100644
index 0000000..3857f6f
--- /dev/null
+++ b/src/socket/ntapi_sc_connect_v2.c
@@ -0,0 +1,69 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_afd_connect_request {
+ uintptr_t unknown[2];
+ void * paddr;
+ nt_sockaddr addr;
+} nt_afd_connect_request;
+
+typedef struct __addr_memcpy {
+ uint64_t d0;
+ uint64_t d1;
+} _addr_memcpy;
+
+
+int32_t __cdecl __ntapi_sc_connect_v2(
+ __in nt_socket * hssocket,
+ __in nt_sockaddr * addr,
+ __in uintptr_t addrlen,
+ __in uintptr_t service_flags __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_io_status_block siosb;
+ nt_afd_connect_request afd_connect_req;
+
+ _addr_memcpy * src;
+ _addr_memcpy * dst;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* afd_connect_req */
+ afd_connect_req.unknown[0] = 0;
+ afd_connect_req.unknown[1] = 0;
+
+ src = (_addr_memcpy *)addr;
+ dst = (_addr_memcpy *)&(afd_connect_req.addr);
+
+ dst->d0 = src->d0;
+ dst->d1 = src->d1;
+
+ afd_connect_req.paddr = &(afd_connect_req.addr);
+ afd_connect_req.addr.sa_addr_in4.sa_family = hssocket->domain;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_CONNECT,
+ &afd_connect_req,
+ sizeof(afd_connect_req),
+ (void *)0,
+ 0);
+
+ return hssocket->iostatus
+ ? __ntapi->sc_wait(hssocket,iosb,0)
+ : NT_STATUS_SUCCESS;
+}
diff --git a/src/socket/ntapi_sc_getsockname_v1.c b/src/socket/ntapi_sc_getsockname_v1.c
new file mode 100644
index 0000000..85a9357
--- /dev/null
+++ b/src/socket/ntapi_sc_getsockname_v1.c
@@ -0,0 +1,80 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_afd_server_socket_name_info {
+ uint32_t unknown;
+ uint32_t type;
+ uint32_t service_flags;
+ char sa_data[14];
+} nt_afd_server_socket_name_info;
+
+
+struct __addr_memcpy {
+ uint16_t d0;
+ uint16_t d1;
+ uint16_t d2;
+ uint16_t d3;
+ uint16_t d4;
+ uint16_t d5;
+ uint16_t d6;
+ uint16_t d7;
+};
+
+
+int32_t __cdecl __ntapi_sc_getsockname_v1(
+ __in nt_socket * hssocket,
+ __in nt_sockaddr * addr,
+ __in uint16_t * addrlen,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_io_status_block siosb;
+ nt_afd_server_socket_name_info sock_name_info;
+
+ struct __addr_memcpy * asrc;
+ struct __addr_memcpy * adst;
+
+ iosb = iosb ? iosb : &siosb;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_GET_SOCK_NAME,
+ 0,
+ 0,
+ &sock_name_info,
+ sizeof(sock_name_info));
+
+ __ntapi->sc_wait(hssocket,iosb,0);
+
+ if (!hssocket->iostatus) {
+ addr->sa_addr_in4.sa_family = hssocket->domain;
+
+ asrc = (struct __addr_memcpy *)&(sock_name_info.sa_data);
+ adst = (struct __addr_memcpy *)addr;
+
+ adst->d1 = asrc->d0;
+ adst->d2 = asrc->d1;
+ adst->d3 = asrc->d2;
+ adst->d4 = asrc->d3;
+ adst->d5 = asrc->d4;
+ adst->d6 = asrc->d5;
+ adst->d7 = asrc->d6;
+
+ *addrlen = (uint16_t)iosb->info;
+ };
+
+ return hssocket->iostatus;
+}
diff --git a/src/socket/ntapi_sc_getsockname_v2.c b/src/socket/ntapi_sc_getsockname_v2.c
new file mode 100644
index 0000000..07313ac
--- /dev/null
+++ b/src/socket/ntapi_sc_getsockname_v2.c
@@ -0,0 +1,42 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+int32_t __cdecl __ntapi_sc_getsockname_v2(
+ __in nt_socket * hssocket,
+ __in nt_sockaddr * addr,
+ __in uint16_t * addrlen,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_iosb siosb;
+
+ iosb = iosb ? iosb : &siosb;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_GET_SOCK_NAME,
+ 0,
+ 0,
+ addr,
+ sizeof(*addr));
+
+ __ntapi->sc_wait(hssocket,iosb,0);
+
+ if (!hssocket->iostatus)
+ *addrlen = (uint16_t)iosb->info;
+
+ return hssocket->iostatus;
+}
diff --git a/src/socket/ntapi_sc_listen.c b/src/socket/ntapi_sc_listen.c
new file mode 100644
index 0000000..cc3e66a
--- /dev/null
+++ b/src/socket/ntapi_sc_listen.c
@@ -0,0 +1,44 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+int32_t __cdecl __ntapi_sc_listen(
+ __in nt_socket * hssocket,
+ __in uintptr_t backlog,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_afd_listen_info afd_listen;
+ nt_io_status_block siosb;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* afd_listen */
+ afd_listen.unknown_1st = 0;
+ afd_listen.unknown_2nd = 0;
+ afd_listen.backlog = (uint32_t)backlog;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_LISTEN,
+ &afd_listen,
+ sizeof(afd_listen),
+ 0,
+ 0);
+
+ return hssocket->iostatus
+ ? __ntapi->sc_wait(hssocket,iosb,0)
+ : NT_STATUS_SUCCESS;
+}
diff --git a/src/socket/ntapi_sc_recv.c b/src/socket/ntapi_sc_recv.c
new file mode 100644
index 0000000..8db3426
--- /dev/null
+++ b/src/socket/ntapi_sc_recv.c
@@ -0,0 +1,63 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+int32_t __cdecl __ntapi_sc_recv(
+ __in nt_socket * hssocket,
+ __in const void * buffer,
+ __in size_t len,
+ __out ssize_t * bytes_received __optional,
+ __in uintptr_t afdflags __optional,
+ __in uintptr_t tdiflags __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_afd_buffer afd_buffer;
+ nt_afd_recv_info afd_recv;
+ nt_io_status_block siosb;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* tdiflags */
+ if (tdiflags == 0)
+ tdiflags = NT_TDI_RECEIVE_NORMAL;
+
+ /* afd_buffer */
+ afd_buffer.length = len;
+ afd_buffer.buffer = (char *)buffer;
+
+ /* afd_recv */
+ afd_recv.afd_buffer_array = &afd_buffer;
+ afd_recv.buffer_count = 1;
+
+ afd_recv.afd_flags = (uint32_t)afdflags;
+ afd_recv.tdi_flags = (uint32_t)tdiflags;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_RECV,
+ &afd_recv,
+ sizeof(afd_recv),
+ 0,
+ 0);
+
+ if (hssocket->iostatus && (hssocket->ntflags & __NT_FILE_SYNC_IO))
+ __ntapi->sc_wait(hssocket,iosb,&hssocket->timeout);
+
+ if (!hssocket->iostatus && bytes_received)
+ *bytes_received = iosb->info;
+
+ return hssocket->iostatus;
+}
diff --git a/src/socket/ntapi_sc_send.c b/src/socket/ntapi_sc_send.c
new file mode 100644
index 0000000..2286d65
--- /dev/null
+++ b/src/socket/ntapi_sc_send.c
@@ -0,0 +1,59 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+int32_t __cdecl __ntapi_sc_send(
+ __in nt_socket * hssocket,
+ __in const void * buffer,
+ __in size_t len,
+ __out ssize_t * bytes_sent __optional,
+ __in uintptr_t afdflags __optional,
+ __in uintptr_t tdiflags __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_afd_buffer afd_buffer;
+ nt_afd_send_info afd_send;
+ nt_io_status_block siosb;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* afd_buffer */
+ afd_buffer.length = len;
+ afd_buffer.buffer = (char *)buffer;
+
+ /* afd_send */
+ afd_send.afd_buffer_array = &afd_buffer;
+ afd_send.buffer_count = 1;
+
+ afd_send.afd_flags = (uint32_t)afdflags;
+ afd_send.tdi_flags = (uint32_t)tdiflags;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_SEND,
+ &afd_send,
+ sizeof(afd_send),
+ 0,
+ 0);
+
+ if (hssocket->iostatus && (hssocket->ntflags & __NT_FILE_SYNC_IO))
+ __ntapi->sc_wait(hssocket,iosb,&hssocket->timeout);
+
+ if (!hssocket->iostatus && bytes_sent)
+ *bytes_sent = iosb->info;
+
+ return hssocket->iostatus;
+}
diff --git a/src/socket/ntapi_sc_server_accept_connection_v1.c b/src/socket/ntapi_sc_server_accept_connection_v1.c
new file mode 100644
index 0000000..0154ef7
--- /dev/null
+++ b/src/socket/ntapi_sc_server_accept_connection_v1.c
@@ -0,0 +1,78 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_afd_server_accept_info {
+ uint32_t sequence;
+ uint32_t unknown;
+ uint32_t service_flags;
+ char sa_data[14];
+} nt_afd_server_accept_info;
+
+typedef struct __addr_memcpy {
+ uint16_t d0;
+ uint16_t d1;
+ uint16_t d2;
+ uint16_t d3;
+ uint16_t d4;
+ uint16_t d5;
+ uint16_t d6;
+ uint16_t d7;
+} _addr_memcpy;
+
+int32_t __cdecl __ntapi_sc_server_accept_connection_v1(
+ __in nt_socket * hssocket,
+ __out nt_afd_accept_info * accept_info,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_io_status_block siosb;
+ nt_afd_server_accept_info accept_info_buffer;
+
+ _addr_memcpy * asrc;
+ _addr_memcpy * adst;
+
+ iosb = iosb ? iosb : &siosb;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_ACCEPT,
+ 0,
+ 0,
+ &accept_info_buffer,
+ sizeof(accept_info_buffer));
+
+ if (hssocket->iostatus && (hssocket->ntflags & __NT_FILE_SYNC_IO))
+ __ntapi->sc_wait(hssocket,iosb,&hssocket->timeout);
+
+ if (hssocket->iostatus)
+ return hssocket->iostatus;
+
+ accept_info->sequence = accept_info_buffer.sequence;
+ accept_info->addr.sa_addr_in4.sa_family = hssocket->domain;
+
+ asrc = (_addr_memcpy *)&(accept_info_buffer.sa_data);
+ adst = (_addr_memcpy *)&(accept_info->addr);
+
+ adst->d1 = asrc->d0;
+ adst->d2 = asrc->d1;
+ adst->d3 = asrc->d2;
+ adst->d4 = asrc->d3;
+ adst->d5 = asrc->d4;
+ adst->d6 = asrc->d5;
+ adst->d7 = asrc->d6;
+
+ return hssocket->iostatus;
+}
diff --git a/src/socket/ntapi_sc_server_accept_connection_v2.c b/src/socket/ntapi_sc_server_accept_connection_v2.c
new file mode 100644
index 0000000..3520c75
--- /dev/null
+++ b/src/socket/ntapi_sc_server_accept_connection_v2.c
@@ -0,0 +1,44 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_afd_server_accept_info {
+ uint32_t sequence;
+ nt_sockaddr addr;
+} nt_afd_server_accept_info;
+
+int32_t __cdecl __ntapi_sc_server_accept_connection_v2(
+ __in nt_socket * hssocket,
+ __out nt_afd_accept_info * accept_info,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_io_status_block siosb;
+
+ iosb = iosb ? iosb : &siosb;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_ACCEPT,
+ 0,
+ 0,
+ accept_info,
+ sizeof(nt_afd_server_accept_info));
+
+ if (hssocket->iostatus && (hssocket->ntflags & __NT_FILE_SYNC_IO))
+ __ntapi->sc_wait(hssocket,iosb,&hssocket->timeout);
+
+ return hssocket->iostatus;
+}
diff --git a/src/socket/ntapi_sc_server_duplicate_socket.c b/src/socket/ntapi_sc_server_duplicate_socket.c
new file mode 100644
index 0000000..4084593
--- /dev/null
+++ b/src/socket/ntapi_sc_server_duplicate_socket.c
@@ -0,0 +1,45 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+int32_t __cdecl __ntapi_sc_server_duplicate_socket(
+ __in nt_socket * hssock_listen,
+ __in nt_socket * hssock_dedicated,
+ __in nt_afd_accept_info * accept_info,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_afd_duplicate_info duplicate_info;
+ nt_io_status_block siosb;
+
+ iosb = iosb ? iosb : &siosb;
+
+ /* duplicate_info */
+ duplicate_info.unknown = 0;
+ duplicate_info.sequence = accept_info->sequence;
+ duplicate_info.hsocket_dedicated = hssock_dedicated->hsocket;
+
+ hssock_dedicated->iostatus = __ntapi->zw_device_io_control_file(
+ hssock_listen->hsocket,
+ hssock_dedicated->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_DUPLICATE,
+ &duplicate_info,
+ sizeof(duplicate_info),
+ 0,
+ 0);
+
+ return hssock_dedicated->iostatus
+ ? __ntapi->sc_wait(hssock_dedicated,iosb,0)
+ : NT_STATUS_SUCCESS;
+}
diff --git a/src/socket/ntapi_sc_shutdown.c b/src/socket/ntapi_sc_shutdown.c
new file mode 100644
index 0000000..115214c
--- /dev/null
+++ b/src/socket/ntapi_sc_shutdown.c
@@ -0,0 +1,65 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+int32_t __cdecl __ntapi_sc_shutdown(
+ __in nt_socket * hssocket,
+ __in uintptr_t psxhow,
+ __in uintptr_t afdhow,
+ __out nt_io_status_block * iosb __optional)
+{
+ nt_afd_disconnect_info afd_disconnect;
+ nt_io_status_block siosb;
+
+ iosb = iosb ? iosb : &siosb;
+
+ if (afdhow == 0) {
+ switch (psxhow) {
+ case NT_SHUT_RD:
+ afdhow = NT_AFD_DISCONNECT_RD;
+ break;
+
+ case NT_SHUT_WR:
+ afdhow = NT_AFD_DISCONNECT_WR;
+ break;
+
+ case NT_SHUT_RDWR:
+ afdhow = NT_AFD_DISCONNECT_RD | NT_AFD_DISCONNECT_WR;
+ break;
+
+ default:
+ return NT_STATUS_INVALID_PARAMETER_2;
+ break;
+ }
+ }
+
+ afd_disconnect.shutdown_flags = (uint32_t)afdhow;
+ afd_disconnect.unknown[0] = 0xff;
+ afd_disconnect.unknown[1] = 0xff;
+ afd_disconnect.unknown[2] = 0xff;
+
+ hssocket->iostatus = __ntapi->zw_device_io_control_file(
+ hssocket->hsocket,
+ hssocket->hevent,
+ 0,
+ 0,
+ iosb,
+ NT_AFD_IOCTL_DISCONNECT,
+ &afd_disconnect,
+ sizeof(afd_disconnect),
+ 0,
+ 0);
+
+ return hssocket->iostatus
+ ? __ntapi->sc_wait(hssocket,iosb,0)
+ : NT_STATUS_SUCCESS;
+}
diff --git a/src/socket/ntapi_sc_socket_v1.c b/src/socket/ntapi_sc_socket_v1.c
new file mode 100644
index 0000000..d57f212
--- /dev/null
+++ b/src/socket/ntapi_sc_socket_v1.c
@@ -0,0 +1,118 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_afd_socket_ea {
+ uint32_t next_entry_offset;
+ unsigned char ea_flags;
+ unsigned char ea_name_length;
+ uint16_t ea_value_length;
+ char afd_open_packet[0x10];
+ uint32_t value_1st;
+ uint32_t value_2nd;
+ uint32_t device_name_length;
+ wchar16_t device_name[0x0b];
+ uint32_t ea_ext[4];
+} nt_afd_socket_ea;
+
+int32_t __cdecl __ntapi_sc_socket_v1(
+ __out nt_socket * hssocket,
+ __in uint16_t domain,
+ __in uint16_t type,
+ __in uint32_t protocol,
+ __in uint32_t desired_access __optional,
+ __in nt_sqos * sqos __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ int32_t status;
+ nt_object_attributes oa;
+ nt_io_status_block siosb;
+ nt_sqos ssqos;
+ nt_unicode_string nt_afdep;
+ uint32_t ea_length;
+ void * _hsocket;
+
+ wchar16_t afd_end_point[] = {
+ '\\','D','e','v','i','c','e',
+ '\\','A','f','d',
+ '\\','E','n','d','P','o','i','n','t',
+ 0};
+
+ /* tcp as default extended attribute */
+ nt_afd_socket_ea afd_ea = {
+ 0,
+ 0,
+ 0x0f,
+ 0x28,
+ {'A','f','d','O','p','e','n','P','a','c','k','e','t','X','X',0},
+ 0,0,
+ 0x16,
+ {'\\','D','e','v','i','c','e','\\','T','c','p'},
+ {0}};
+
+ ea_length = 0x43;
+
+ __ntapi->rtl_init_unicode_string(&nt_afdep,afd_end_point);
+
+ if (!desired_access)
+ desired_access = NT_GENERIC_READ \
+ | NT_GENERIC_WRITE \
+ | NT_SEC_SYNCHRONIZE \
+ | NT_SEC_WRITE_DAC;
+
+ if (!sqos) {
+ ssqos.length = sizeof(ssqos);
+ ssqos.impersonation_level = NT_SECURITY_IMPERSONATION;
+ ssqos.context_tracking_mode = NT_SECURITY_TRACKING_DYNAMIC;
+ ssqos.effective_only = 1;
+ sqos = &ssqos;
+ }
+
+ oa.len = sizeof(oa);
+ oa.root_dir = (void *)0;
+ oa.obj_name = &nt_afdep;
+ oa.obj_attr = NT_OBJ_CASE_INSENSITIVE | NT_OBJ_INHERIT;
+ oa.sec_desc = (nt_security_descriptor *)0;
+ oa.sec_qos = sqos;
+
+ iosb = iosb ? iosb : &siosb;
+
+ if ((status = __ntapi->zw_create_file(
+ &_hsocket,
+ desired_access,
+ &oa,
+ iosb,
+ 0,
+ 0,
+ NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
+ NT_FILE_OPEN_IF,
+ 0,
+ &afd_ea,
+ ea_length)))
+ return status;
+
+ oa.obj_name = 0;
+ oa.obj_attr = 0;
+
+ if (status == NT_STATUS_SUCCESS) {
+ hssocket->hsocket = _hsocket;
+ hssocket->ntflags = 0;
+ hssocket->domain = domain;
+ hssocket->type = type;
+ hssocket->protocol = protocol;
+ hssocket->timeout.quad = 0;
+ hssocket->iostatus = NT_STATUS_SUCCESS;
+ hssocket->waitstatus = NT_STATUS_SUCCESS;
+ }
+
+ return status;
+}
diff --git a/src/socket/ntapi_sc_socket_v2.c b/src/socket/ntapi_sc_socket_v2.c
new file mode 100644
index 0000000..069c596
--- /dev/null
+++ b/src/socket/ntapi_sc_socket_v2.c
@@ -0,0 +1,124 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_object.h>
+#include <ntapi/nt_file.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+typedef struct _nt_socket_attr {
+ uint32_t datagram;
+ uint32_t unknown;
+ uint32_t domain;
+ uint32_t type;
+ uint32_t protocol;
+} nt_socket_attr;
+
+typedef struct _nt_afd_socket_ea {
+ uint32_t next_entry_offset;
+ unsigned char ea_flags;
+ unsigned char ea_name_length;
+ uint16_t ea_value_length;
+ char afd_open_packet[16];
+ nt_socket_attr sattr;
+ uint32_t ea_ext[4];
+} nt_afd_socket_ea;
+
+int32_t __cdecl __ntapi_sc_socket_v2(
+ __out nt_socket * hssocket,
+ __in uint16_t domain,
+ __in uint16_t type,
+ __in uint32_t protocol,
+ __in uint32_t desired_access __optional,
+ __in nt_sqos * sqos __optional,
+ __out nt_io_status_block * iosb __optional)
+{
+ int32_t status;
+ nt_object_attributes oa;
+ nt_io_status_block siosb;
+ nt_sqos ssqos;
+ nt_unicode_string nt_afdep;
+ uint32_t ea_length;
+ void * _hsocket;
+
+ wchar16_t afd_end_point[] = {
+ '\\','D','e','v','i','c','e',
+ '\\','A','f','d',
+ '\\','E','n','d','P','o','i','n','t',
+ 0};
+
+ nt_afd_socket_ea afd_ea = {
+ 0,
+ 0,
+ 0x0f,
+ 0x20,
+ {'A','f','d','O','p','e','n','P','a','c','k','e','t','X','X',0},
+ {0},
+ {0}};
+
+ ea_length = sizeof(afd_ea);
+
+ afd_ea.sattr.domain = domain;
+ afd_ea.sattr.type = type;
+ afd_ea.sattr.protocol = protocol;
+
+ afd_ea.sattr.datagram = (type == NT_SOCK_DGRAM) ? protocol : 0;
+
+ __ntapi->rtl_init_unicode_string(&nt_afdep,afd_end_point);
+
+ if (!desired_access)
+ desired_access = NT_GENERIC_READ \
+ | NT_GENERIC_WRITE \
+ | NT_SEC_SYNCHRONIZE \
+ | NT_SEC_WRITE_DAC;
+
+ if (!sqos) {
+ ssqos.length = sizeof(ssqos);
+ ssqos.impersonation_level = NT_SECURITY_IMPERSONATION;
+ ssqos.context_tracking_mode = NT_SECURITY_TRACKING_DYNAMIC;
+ ssqos.effective_only = 1;
+ sqos = &ssqos;
+ }
+
+ oa.len = sizeof(oa);
+ oa.root_dir = (void *)0;
+ oa.obj_name = &nt_afdep;
+ oa.obj_attr = NT_OBJ_CASE_INSENSITIVE | NT_OBJ_INHERIT;
+ oa.sec_desc = (nt_security_descriptor *)0;
+ oa.sec_qos = sqos;
+
+ iosb = iosb ? iosb : &siosb;
+
+ if ((status = __ntapi->zw_create_file(
+ &_hsocket,
+ desired_access,
+ &oa,
+ iosb,
+ 0,
+ 0,
+ NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE,
+ NT_FILE_OPEN_IF,
+ 0,
+ &afd_ea,
+ ea_length)))
+ return status;
+
+ oa.obj_name = 0;
+ oa.obj_attr = 0;
+
+ hssocket->hsocket = _hsocket;
+ hssocket->ntflags = 0;
+ hssocket->domain = domain;
+ hssocket->type = type;
+ hssocket->protocol = protocol;
+ hssocket->timeout.quad = 0;
+ hssocket->iostatus = NT_STATUS_SUCCESS;
+ hssocket->waitstatus = NT_STATUS_SUCCESS;
+
+ return status;
+}
diff --git a/src/socket/ntapi_sc_wait.c b/src/socket/ntapi_sc_wait.c
new file mode 100644
index 0000000..3bfad28
--- /dev/null
+++ b/src/socket/ntapi_sc_wait.c
@@ -0,0 +1,42 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_sync.h>
+#include <ntapi/nt_socket.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+int32_t __cdecl __ntapi_sc_wait(nt_socket * hssocket, nt_iosb * iosb, nt_timeout * timeout)
+{
+ nt_iosb cancel;
+
+ timeout = (timeout && timeout->quad)
+ ? timeout
+ : 0;
+
+ if (hssocket->hevent && (hssocket->iostatus == NT_STATUS_PENDING)) {
+ hssocket->waitstatus = __ntapi->zw_wait_for_single_object(
+ hssocket->hevent,
+ !!(hssocket->ntflags & NT_FILE_SYNCHRONOUS_IO_ALERT),
+ timeout);
+
+ switch (hssocket->waitstatus) {
+ case NT_STATUS_SUCCESS:
+ hssocket->iostatus = NT_STATUS_SUCCESS;
+ break;
+
+ case NT_STATUS_ALERTED:
+ hssocket->iostatus = NT_STATUS_ALERTED;
+ __ntapi->zw_cancel_io_file(
+ hssocket->hsocket,
+ &cancel);
+ break;
+ }
+ }
+
+ return hssocket->iostatus;
+}