Blame lib/system_override.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
Packit aea12f
 *
Packit aea12f
 * Author: Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS.
Packit aea12f
 *
Packit aea12f
 * The GnuTLS is free software; you can redistribute it and/or
Packit aea12f
 * modify it under the terms of the GNU Lesser General Public License
Packit aea12f
 * as published by the Free Software Foundation; either version 2.1 of
Packit aea12f
 * the License, or (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * This library is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * Lesser General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU Lesser General Public License
Packit aea12f
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit aea12f
 *
Packit aea12f
 */
Packit aea12f
Packit aea12f
/* This file contains function that will override the 
Packit aea12f
 * default berkeley sockets API per session.
Packit aea12f
 */
Packit aea12f
Packit aea12f
#include "gnutls_int.h"
Packit aea12f
#include "errors.h"
Packit aea12f
#include <num.h>
Packit aea12f
#include <record.h>
Packit aea12f
#include <buffers.h>
Packit aea12f
#include <mbuffers.h>
Packit aea12f
#include <state.h>
Packit aea12f
#include <dtls.h>
Packit aea12f
#include <system.h>
Packit aea12f
Packit aea12f
#include <errno.h>
Packit aea12f
#ifdef _WIN32
Packit aea12f
#include <windows.h>
Packit aea12f
#endif
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_transport_set_errno:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @err: error value to store in session-specific errno variable.
Packit aea12f
 *
Packit aea12f
 * Store @err in the session-specific errno variable.  Useful values
Packit aea12f
 * for @err are EINTR, EAGAIN and EMSGSIZE, other values are treated will be
Packit aea12f
 * treated as real errors in the push/pull function.
Packit aea12f
 *
Packit aea12f
 * This function is useful in replacement push and pull functions set by
Packit aea12f
 * gnutls_transport_set_push_function() and
Packit aea12f
 * gnutls_transport_set_pull_function() under Windows, where the
Packit aea12f
 * replacements may not have access to the same @errno
Packit aea12f
 * variable that is used by GnuTLS (e.g., the application is linked to
Packit aea12f
 * msvcr71.dll and gnutls is linked to msvcrt.dll).
Packit aea12f
 *
Packit aea12f
 * This function is unreliable if you are using the same
Packit aea12f
 * @session in different threads for sending and receiving.
Packit aea12f
 *
Packit aea12f
 **/
Packit aea12f
void gnutls_transport_set_errno(gnutls_session_t session, int err)
Packit aea12f
{
Packit aea12f
	session->internals.errnum = err;
Packit aea12f
}
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_transport_set_pull_function:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @pull_func: a callback function similar to read()
Packit aea12f
 *
Packit aea12f
 * This is the function where you set a function for gnutls to receive
Packit aea12f
 * data.  Normally, if you use berkeley style sockets, do not need to
Packit aea12f
 * use this function since the default recv(2) will probably be ok.
Packit aea12f
 * The callback should return 0 on connection termination, a positive
Packit aea12f
 * number indicating the number of bytes received, and -1 on error.
Packit aea12f
 *
Packit aea12f
 * @gnutls_pull_func is of the form,
Packit aea12f
 * ssize_t (*gnutls_pull_func)(gnutls_transport_ptr_t, void*, size_t);
Packit aea12f
 **/
Packit aea12f
void
Packit aea12f
gnutls_transport_set_pull_function(gnutls_session_t session,
Packit aea12f
				   gnutls_pull_func pull_func)
Packit aea12f
{
Packit aea12f
	session->internals.pull_func = pull_func;
Packit aea12f
}
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_transport_set_pull_timeout_function:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @func: a callback function
Packit aea12f
 *
Packit aea12f
 * This is the function where you set a function for gnutls to know
Packit aea12f
 * whether data are ready to be received. It should wait for data a
Packit aea12f
 * given time frame in milliseconds. The callback should return 0 on 
Packit aea12f
 * timeout, a positive number if data can be received, and -1 on error.
Packit aea12f
 * You'll need to override this function if select() is not suitable
Packit aea12f
 * for the provided transport calls.
Packit aea12f
 *
Packit aea12f
 * As with select(), if the timeout value is zero the callback should return
Packit aea12f
 * zero if no data are immediately available. The special value
Packit aea12f
 * %GNUTLS_INDEFINITE_TIMEOUT indicates that the callback should wait indefinitely
Packit aea12f
 * for data.
Packit aea12f
 *
Packit aea12f
 * @gnutls_pull_timeout_func is of the form,
Packit aea12f
 * int (*gnutls_pull_timeout_func)(gnutls_transport_ptr_t, unsigned int ms);
Packit aea12f
 *
Packit aea12f
 * This callback is necessary when gnutls_handshake_set_timeout() or 
Packit Service 991b93
 * gnutls_record_set_timeout() are set, under TLS1.3 and for enforcing the DTLS
Packit Service 991b93
 * mode timeouts when in blocking mode.
Packit Service 991b93
 *
Packit Service 991b93
 * For compatibility with future GnuTLS versions this callback must be set when
Packit Service 991b93
 * a custom pull function is registered. The callback will not be used when the
Packit Service 991b93
 * session is in TLS mode with non-blocking sockets. That is, when %GNUTLS_NONBLOCK
Packit Service 991b93
 * is specified for a TLS session in gnutls_init().
Packit aea12f
 *
Packit aea12f
 * The helper function gnutls_system_recv_timeout() is provided to
Packit aea12f
 * simplify writing callbacks. 
Packit aea12f
 *
Packit aea12f
 * Since: 3.0
Packit aea12f
 **/
Packit aea12f
void
Packit aea12f
gnutls_transport_set_pull_timeout_function(gnutls_session_t session,
Packit aea12f
					   gnutls_pull_timeout_func func)
Packit aea12f
{
Packit aea12f
	session->internals.pull_timeout_func = func;
Packit aea12f
}
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_transport_set_push_function:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @push_func: a callback function similar to write()
Packit aea12f
 *
Packit aea12f
 * This is the function where you set a push function for gnutls to
Packit aea12f
 * use in order to send data.  If you are going to use berkeley style
Packit aea12f
 * sockets, you do not need to use this function since the default
Packit aea12f
 * send(2) will probably be ok.  Otherwise you should specify this
Packit aea12f
 * function for gnutls to be able to send data.
Packit aea12f
 * The callback should return a positive number indicating the
Packit aea12f
 * bytes sent, and -1 on error.
Packit aea12f
 *
Packit aea12f
 * @push_func is of the form,
Packit aea12f
 * ssize_t (*gnutls_push_func)(gnutls_transport_ptr_t, const void*, size_t);
Packit aea12f
 *
Packit aea12f
 **/
Packit aea12f
void
Packit aea12f
gnutls_transport_set_push_function(gnutls_session_t session,
Packit aea12f
				   gnutls_push_func push_func)
Packit aea12f
{
Packit aea12f
	session->internals.push_func = push_func;
Packit aea12f
	session->internals.vec_push_func = NULL;
Packit aea12f
}
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_transport_set_vec_push_function:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @vec_func: a callback function similar to writev()
Packit aea12f
 *
Packit aea12f
 * Using this function you can override the default writev(2)
Packit aea12f
 * function for gnutls to send data. Setting this callback 
Packit aea12f
 * instead of gnutls_transport_set_push_function() is recommended
Packit aea12f
 * since it introduces less overhead in the TLS handshake process.
Packit aea12f
 *
Packit aea12f
 * @vec_func is of the form,
Packit aea12f
 * ssize_t (*gnutls_vec_push_func) (gnutls_transport_ptr_t, const giovec_t * iov, int iovcnt);
Packit aea12f
 *
Packit aea12f
 * Since: 2.12.0
Packit aea12f
 **/
Packit aea12f
void
Packit aea12f
gnutls_transport_set_vec_push_function(gnutls_session_t session,
Packit aea12f
				       gnutls_vec_push_func vec_func)
Packit aea12f
{
Packit aea12f
	session->internals.push_func = NULL;
Packit aea12f
	session->internals.vec_push_func = vec_func;
Packit aea12f
}
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_transport_set_errno_function:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @errno_func: a callback function similar to write()
Packit aea12f
 *
Packit aea12f
 * This is the function where you set a function to retrieve errno
Packit aea12f
 * after a failed push or pull operation.
Packit aea12f
 *
Packit aea12f
 * @errno_func is of the form,
Packit aea12f
 * int (*gnutls_errno_func)(gnutls_transport_ptr_t);
Packit aea12f
 * and should return the errno.
Packit aea12f
 *
Packit aea12f
 * Since: 2.12.0
Packit aea12f
 **/
Packit aea12f
void
Packit aea12f
gnutls_transport_set_errno_function(gnutls_session_t session,
Packit aea12f
				    gnutls_errno_func errno_func)
Packit aea12f
{
Packit aea12f
	session->internals.errno_func = errno_func;
Packit aea12f
}