Blame src/socket/ntapi_sc_recv.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
4256e2
/*  Copyright (C) 2013--2016  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
f6c77a
typedef struct _nt_afd_recv_udp_info {
f6c77a
	nt_afd_recv_info	afd_recv;
f6c77a
	nt_sockaddr *		addr;
f6c77a
	nt_sockaddr_reply *	raddr;
f6c77a
} nt_afd_recv_udp_info;
f6c77a
dd89bb
int32_t __cdecl __ntapi_sc_recv(
dd89bb
	__in		nt_socket *		hssocket,
dd89bb
	__in		const void *		buffer,
dd89bb
	__in		size_t			len,
dd89bb
	__out		ssize_t *		bytes_received	__optional,
f6c77a
	__in		nt_sockaddr_reply *	raddr		__optional,
dd89bb
	__in		uintptr_t		afdflags	__optional,
dd89bb
	__in		uintptr_t		tdiflags	__optional,
dd89bb
	__out		nt_io_status_block *	iosb		__optional)
dd89bb
{
dd89bb
	nt_afd_buffer		afd_buffer;
f6c77a
	nt_afd_recv_udp_info	afd_recv;
dd89bb
	nt_io_status_block	siosb;
dd89bb
dd89bb
	iosb = iosb ? iosb : &siosb;
dd89bb
f6c77a
	/* conditional connect */
f6c77a
	if (raddr && (hssocket->protocol == NT_IPPROTO_UDP)) {
f6c77a
		afd_recv.raddr  = raddr;
f6c77a
		raddr->addrlen  = sizeof(raddr->addr);
f6c77a
		afd_recv.addr   = &raddr->addr;
f6c77a
	} else
f6c77a
		raddr = 0;
f6c77a
dd89bb
	/* tdiflags */
dd89bb
	if (tdiflags == 0)
dd89bb
		tdiflags = NT_TDI_RECEIVE_NORMAL;
dd89bb
dd89bb
	/* afd_buffer */
dd89bb
	afd_buffer.length = len;
dd89bb
	afd_buffer.buffer = (char *)buffer;
dd89bb
f6c77a
	/* afd_recv.afd_recv */
f6c77a
	afd_recv.afd_recv.afd_buffer_array = &afd_buffer;
f6c77a
	afd_recv.afd_recv.buffer_count     = 1;
dd89bb
f6c77a
	afd_recv.afd_recv.afd_flags = (uint32_t)afdflags;
f6c77a
	afd_recv.afd_recv.tdi_flags = (uint32_t)tdiflags;
dd89bb
dd89bb
	hssocket->iostatus = __ntapi->zw_device_io_control_file(
dd89bb
			hssocket->hsocket,
dd89bb
			hssocket->hevent,
dd89bb
			0,
dd89bb
			0,
dd89bb
			iosb,
f6c77a
			raddr ? NT_AFD_IOCTL_UDP_RECV : NT_AFD_IOCTL_RECV,
dd89bb
			&afd_recv,
f6c77a
			raddr ? sizeof(afd_recv) : sizeof(afd_recv.afd_recv),
dd89bb
			0,
dd89bb
			0);
dd89bb
dd89bb
	if (hssocket->iostatus && (hssocket->ntflags & __NT_FILE_SYNC_IO))
dd89bb
		__ntapi->sc_wait(hssocket,iosb,&hssocket->timeout);
dd89bb
dd89bb
	if (!hssocket->iostatus && bytes_received)
dd89bb
		*bytes_received = iosb->info;
dd89bb
dd89bb
	return hssocket->iostatus;
dd89bb
}