[gnutls-devel] Waiting for input data

Tim Ruehsen tim.ruehsen at gmx.de
Fri Feb 8 14:54:24 CET 2013


Maybe this code snippet/example from libmget (License LGPL3+) may help you:
It assumes a non-blocking socket descriptor set by 
	gnutls_transport_set_ptr(session, (void *)(ssize_t)sockfd);


ssize_t mget_ssl_read_timeout(void *session, char *buf, size_t count, int 
timeout)
{
	ssize_t nbytes;
	int rc;

	for (;;) {
		if (gnutls_record_check_pending(session) <= 0 && 
(rc=_ready_2_read(session, timeout)) <= 0)
			return rc;

		nbytes=gnutls_record_recv(session, buf, count);

		if (nbytes >= 0 || nbytes != GNUTLS_E_AGAIN)
			break;
	}

	return nbytes < -1 ? -1 : nbytes;
}

static int _ready_2_read(gnutls_session_t session, int timeout)
{
	return _ready_2_transfer(session, timeout, POLLIN);
}

static int _ready_2_transfer(gnutls_session_t session, int timeout, int mode)
{
	// 0: no timeout / immediate
	// -1: INFINITE timeout
	if (timeout) {
		int sockfd = (int)(ssize_t)gnutls_transport_get_ptr(session);
		int rc;

		// wait for socket to be ready to read
		struct pollfd pollfd[1] = {
			{ sockfd, mode, 0}};

		if ((rc = poll(pollfd, 1, timeout)) <= 0)
			return rc < 0 ? -1 : 0;

		if (!(pollfd[0].revents & mode))
			return -1;
	}

	return 1;
}

Regards,

     Tim



More information about the Gnutls-devel mailing list