Blame src/socket/ntapi_sc_bind_v1.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dde53a
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <psxtypes/psxtypes.h>
dd89bb
#include <ntapi/nt_object.h>
dd89bb
#include <ntapi/nt_file.h>
dd89bb
#include <ntapi/nt_socket.h>
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
typedef struct _nt_afd_bind_msg {
dd89bb
	uint32_t	domain;
dd89bb
	uint32_t	type;
dd89bb
	uint32_t	service_flags;
dd89bb
	char		sa_data[14];
dd89bb
} nt_afd_bind_msg;
dd89bb
dd89bb
dd89bb
typedef struct __addr_memcpy {
dd89bb
	uint16_t	d0;
dd89bb
	uint16_t	d1;
dd89bb
	uint16_t	d2;
dd89bb
	uint16_t	d3;
dd89bb
	uint16_t	d4;
dd89bb
	uint16_t	d5;
dd89bb
	uint16_t	d6;
dd89bb
	uint16_t	d7;
dd89bb
} _addr_memcpy;
dd89bb
dd89bb
dd89bb
int32_t __cdecl __ntapi_sc_bind_v1(
dd89bb
	__in		nt_socket *		hssocket,
dd89bb
	__in		const nt_sockaddr *	addr,
dd89bb
	__in		uintptr_t		addrlen,
3260d1
	__in		uint32_t		afdflags	__optional,
3260d1
	__in		uint32_t		srvflags	__optional,
dd89bb
	__out		nt_sockaddr *		sockaddr	__optional,
dd89bb
	__out		nt_io_status_block *	iosb		__optional)
dd89bb
{
dd89bb
	nt_io_status_block	siosb;
dd89bb
	nt_afd_bind_msg		afd_bind_req;
dd89bb
	nt_afd_bind_msg		afd_bind_rep;
dd89bb
dd89bb
	_addr_memcpy *		src;
dd89bb
	_addr_memcpy *		dst;
dd89bb
3260d1
	(void)afdflags;
c713d8
	(void)addrlen;
c713d8
dd89bb
	iosb = iosb ? iosb : &siosb;
dd89bb
dd89bb
	/* service_flags */
3260d1
	if (!srvflags)
3260d1
		srvflags = 0x2000E;
dd89bb
dd89bb
	/* afd_bind_req */
dd89bb
	afd_bind_req.domain = hssocket->domain;
dd89bb
	afd_bind_req.type   = hssocket->type;
3260d1
	afd_bind_req.service_flags = srvflags;
dd89bb
dd89bb
	src = (_addr_memcpy *)addr;
dd89bb
	dst = (_addr_memcpy *)&(afd_bind_req.sa_data);
dd89bb
dd89bb
	dst->d0 = src->d1;
dd89bb
	dst->d1 = src->d2;
dd89bb
	dst->d2 = src->d3;
dd89bb
	dst->d3 = src->d4;
dd89bb
	dst->d4 = src->d5;
dd89bb
	dst->d5 = src->d6;
dd89bb
	dst->d6 = src->d7;
dd89bb
dd89bb
	hssocket->iostatus = __ntapi->zw_device_io_control_file(
dd89bb
			hssocket->hsocket,
dd89bb
			hssocket->hevent,
dd89bb
			0,
dd89bb
			0,
dd89bb
			iosb,
dd89bb
			NT_AFD_IOCTL_BIND,
dd89bb
			&afd_bind_req,
dd89bb
			sizeof(afd_bind_req),
dd89bb
			&afd_bind_rep,
dd89bb
			sizeof(afd_bind_rep));
dd89bb
dd89bb
	__ntapi->sc_wait(hssocket,iosb,0);
dd89bb
dd89bb
	if (!hssocket->iostatus && sockaddr) {
dd89bb
		src = (_addr_memcpy *)&(afd_bind_rep.sa_data);
dd89bb
		dst = (_addr_memcpy *)sockaddr;
dd89bb
dd89bb
		dst->d1 = src->d0;
dd89bb
		dst->d2 = src->d1;
dd89bb
		dst->d3 = src->d2;
dd89bb
		dst->d4 = src->d3;
dd89bb
		dst->d5 = src->d4;
dd89bb
		dst->d6 = src->d5;
dd89bb
		dst->d7 = src->d6;
dd89bb
dd89bb
		sockaddr->sa_addr_in4.sa_family = hssocket->domain;
dd89bb
	}
dd89bb
dd89bb
	return hssocket->iostatus;
dd89bb
}