summaryrefslogtreecommitdiffhomepage
path: root/src/socket/ntapi_sc_accept.c
blob: fa61603970c4c3e797d57401b6ac7f849796b3e7 (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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2016  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;
}