/* Read data from a message. If we're trying to read more data that remains, puts an error message into errmsgbuf and returns -2. Otherwise, tries to read the data and, if that succeeds, subtracts the amount read from the number of bytes of data that remains. Returns 0 on success, logs a message and returns -1 on a network error.  */
static int rpcapd_recv(SOCKET sock, char *buffer, size_t toread, uint32 *plen, char *errmsgbuf)
/* Read data from a message. If we're trying to read more data that remains, puts an error message into errmsgbuf and returns -2. Otherwise, tries to read the data and, if that succeeds, subtracts the amount read from the number of bytes of data that remains. Returns 0 on success, logs a message and returns -1 on a network error.  */
static int rpcapd_recv(SOCKET sock, char *buffer, size_t toread, uint32 *plen, char *errmsgbuf)
{
	int nread;
	char errbuf[PCAP_ERRBUF_SIZE];
	if (toread > *plen)
	{
		pcap_snprintf(errmsgbuf, PCAP_ERRBUF_SIZE, "Message payload is too short");
		return -2;
	}
	nread = sock_recv(sock, buffer, toread,
	    SOCK_RECEIVEALL_YES|SOCK_EOF_IS_ERROR, errbuf, PCAP_ERRBUF_SIZE);
	if (nread == -1)
	{
		rpcapd_log(LOGPRIO_ERROR, "Read from client failed: %s", errbuf);
		return -1;
	}
	*plen -= nread;
	return 0;
}