summaryrefslogtreecommitdiffhomepage
path: root/src/socket/ntapi_sc_accept.c
blob: f4fa0d8884be29beb4ed6016ffd923f7d1faac4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  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;
	uint64_t	d2;
	uint32_t	d3;
} _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;

	(void)afdflags;
	(void)tdiflags;

	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;

		if ((size_t)iosb->info >= sizeof(accept_info.sequence)
					+ sizeof(nt_sockaddr_in6)) {
			dst->d2 = src->d2;
			dst->d3 = src->d3;
		} else {
			dst->d2 = 0;
			dst->d3 = 0;
		}
	}

	/* return address length information */
	if (addrlen)
		*addrlen = sizeof(nt_sockaddr);

	return status;
}