6.5.1 Asynchronous operation

GnuTLS can be used with asynchronous socket or event-driven programming.
The approach is similar to using Berkeley sockets under such an environment.
The blocking, due to network interaction, calls such as
gnutls_handshake, gnutls_record_recv,
can be set to non-blocking by setting the underlying sockets to non-blocking.
If other push and pull functions are setup, then they should behave the same
way as recv and send when used in a non-blocking
way, i.e., return -1 and set errno to EAGAIN. Since, during a TLS protocol session
GnuTLS does not block except for network interaction, the non blocking
EAGAIN errno will be propagated and GnuTLS functions
will return the GNUTLS_E_AGAIN error code. Such calls can be resumed the
same way as a system call would.
The only exception is gnutls_record_send,
which if interrupted subsequent calls need not to include the data to be
sent (can be called with NULL argument).

6.5.1.2 Datagram TLS protocol

In contrast with the TLS protocol, the pull timeout function is required,
but will only be called with a timeout of zero. In that case it should indicate
whether there are data to be received or not. When not using the default pull function,
then gnutls_transport_set_pull_timeout_function should be called.

Although in the TLS protocol implementation each call to receive or send
function implies to restoring the same function that was interrupted, in
the DTLS protocol this requirement isn’t true.
There are cases where a retransmission is required, which are indicated by
a received message and thus gnutls_record_get_direction must be called
to decide which direction to check prior to restoring a function call.

Function: intgnutls_record_get_direction(gnutls_session_t session)

session: is a gnutls_session_t type.

This function provides information about the internals of the
record protocol and is only useful if a prior gnutls function call,
e.g. gnutls_handshake() , was interrupted for some reason. That
is, if a function returned GNUTLS_E_INTERRUPTED or
GNUTLS_E_AGAIN . In such a case, you might want to call select()
or poll() before restoring the interrupted gnutls function.

This function’s output is unreliable if you are using the same
session in different threads, for sending and receiving.

Returns: 0 if interrupted while trying to read data, or 1 while trying to write data.