Blob Blame History Raw
/********************************************************/
/*  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 _nt_afd_bind_request {
	uint32_t	flags;
	nt_sockaddr	addr;
} nt_afd_bind_request;

typedef struct _nt_afd_bind_reply {
	nt_sockaddr	addr;
} nt_afd_bind_reply;

typedef struct __addr_memcpy {
	uint64_t	d0;
	uint64_t	d1;
	uint64_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		uint32_t		afdflags	__optional,
	__in		uint32_t		srvflags	__optional,
	__out		nt_sockaddr *		sockaddr	__optional,
	__out		nt_iosb volatile *	iosb		__optional)
{
	nt_iosb volatile	siosb;
	nt_afd_bind_request	afd_bind_req;
	nt_afd_bind_reply	afd_bind_rep;

	_addr_memcpy *		src;
	_addr_memcpy *		dst;

	(void)srvflags;

	if ((addrlen != sizeof(nt_sockaddr_in4))
	 && (addrlen != sizeof(nt_sockaddr_in6)))
		return NT_STATUS_INVALID_PARAMETER_3;

	iosb = iosb ? iosb : &siosb;

	/* request */
	afd_bind_req.flags = afdflags;

	src = (_addr_memcpy *)addr;
	dst = (_addr_memcpy *)&(afd_bind_req.addr);

	dst->d0 = src->d0;
	dst->d1 = src->d1;

	if (addrlen == sizeof(nt_sockaddr_in6)) {
		dst->d2 = src->d2;
		dst->d3 = src->d3;
	} else {
		dst->d2 = 0;
		dst->d3 = 0;
	}

	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;

		if (addrlen == sizeof(nt_sockaddr_in6)) {
			dst->d2 = src->d2;
			dst->d3 = src->d3;
		}
	}

	return hssocket->iostatus;
}