Blame lib/system_override.c

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