summaryrefslogtreecommitdiff
path: root/lib/system_override.c
blob: 326c6adbeff1356e9a12eb72be0b38cecdcbb8c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

/* This file contains function that will override the 
 * default berkeley sockets API per session.
 */

#include "gnutls_int.h"
#include "errors.h"
#include <num.h>
#include <record.h>
#include <buffers.h>
#include <mbuffers.h>
#include <state.h>
#include <dtls.h>
#include <system.h>

#include <errno.h>
#ifdef _WIN32
#include <windows.h>
#endif

/**
 * gnutls_transport_set_errno:
 * @session: is a #gnutls_session_t type.
 * @err: error value to store in session-specific errno variable.
 *
 * Store @err in the session-specific errno variable.  Useful values
 * for @err are EINTR, EAGAIN and EMSGSIZE, other values are treated will be
 * treated as real errors in the push/pull function.
 *
 * This function is useful in replacement push and pull functions set by
 * gnutls_transport_set_push_function() and
 * gnutls_transport_set_pull_function() under Windows, where the
 * replacements may not have access to the same @errno
 * variable that is used by GnuTLS (e.g., the application is linked to
 * msvcr71.dll and gnutls is linked to msvcrt.dll).
 *
 * This function is unreliable if you are using the same
 * @session in different threads for sending and receiving.
 *
 **/
void gnutls_transport_set_errno(gnutls_session_t session, int err)
{
	session->internals.errnum = err;
}

/**
 * gnutls_transport_set_pull_function:
 * @session: is a #gnutls_session_t type.
 * @pull_func: a callback function similar to read()
 *
 * This is the function where you set a function for gnutls to receive
 * data.  Normally, if you use berkeley style sockets, do not need to
 * use this function since the default recv(2) will probably be ok.
 * The callback should return 0 on connection termination, a positive
 * number indicating the number of bytes received, and -1 on error.
 *
 * @gnutls_pull_func is of the form,
 * ssize_t (*gnutls_pull_func)(gnutls_transport_ptr_t, void*, size_t);
 **/
void gnutls_transport_set_pull_function(gnutls_session_t session,
					gnutls_pull_func pull_func)
{
	session->internals.pull_func = pull_func;
}

/**
 * gnutls_transport_set_pull_timeout_function:
 * @session: is a #gnutls_session_t type.
 * @func: a callback function
 *
 * This is the function where you set a function for gnutls to know
 * whether data are ready to be received. It should wait for data a
 * given time frame in milliseconds. The callback should return 0 on 
 * timeout, a positive number if data can be received, and -1 on error.
 * You'll need to override this function if select() is not suitable
 * for the provided transport calls.
 *
 * As with select(), if the timeout value is zero the callback should return
 * zero if no data are immediately available. The special value
 * %GNUTLS_INDEFINITE_TIMEOUT indicates that the callback should wait indefinitely
 * for data.
 *
 * @gnutls_pull_timeout_func is of the form,
 * int (*gnutls_pull_timeout_func)(gnutls_transport_ptr_t, unsigned int ms);
 *
 * This callback is necessary when gnutls_handshake_set_timeout() or 
 * gnutls_record_set_timeout() are set, under TLS1.3 and for enforcing the DTLS
 * mode timeouts when in blocking mode.
 *
 * For compatibility with future GnuTLS versions this callback must be set when
 * a custom pull function is registered. The callback will not be used when the
 * session is in TLS mode with non-blocking sockets. That is, when %GNUTLS_NONBLOCK
 * is specified for a TLS session in gnutls_init().
 *
 * The helper function gnutls_system_recv_timeout() is provided to
 * simplify writing callbacks. 
 *
 * Since: 3.0
 **/
void gnutls_transport_set_pull_timeout_function(gnutls_session_t session,
						gnutls_pull_timeout_func func)
{
	session->internals.pull_timeout_func = func;
}

/**
 * gnutls_transport_set_push_function:
 * @session: is a #gnutls_session_t type.
 * @push_func: a callback function similar to write()
 *
 * This is the function where you set a push function for gnutls to
 * use in order to send data.  If you are going to use berkeley style
 * sockets, you do not need to use this function since the default
 * send(2) will probably be ok.  Otherwise you should specify this
 * function for gnutls to be able to send data.
 * The callback should return a positive number indicating the
 * bytes sent, and -1 on error.
 *
 * @push_func is of the form,
 * ssize_t (*gnutls_push_func)(gnutls_transport_ptr_t, const void*, size_t);
 *
 **/
void gnutls_transport_set_push_function(gnutls_session_t session,
					gnutls_push_func push_func)
{
	session->internals.push_func = push_func;
	session->internals.vec_push_func = NULL;
}

/**
 * gnutls_transport_set_vec_push_function:
 * @session: is a #gnutls_session_t type.
 * @vec_func: a callback function similar to writev()
 *
 * Using this function you can override the default writev(2)
 * function for gnutls to send data. Setting this callback 
 * instead of gnutls_transport_set_push_function() is recommended
 * since it introduces less overhead in the TLS handshake process.
 *
 * @vec_func is of the form,
 * ssize_t (*gnutls_vec_push_func) (gnutls_transport_ptr_t, const giovec_t * iov, int iovcnt);
 *
 * Since: 2.12.0
 **/
void gnutls_transport_set_vec_push_function(gnutls_session_t session,
					    gnutls_vec_push_func vec_func)
{
	session->internals.push_func = NULL;
	session->internals.vec_push_func = vec_func;
}

/**
 * gnutls_transport_set_errno_function:
 * @session: is a #gnutls_session_t type.
 * @errno_func: a callback function similar to write()
 *
 * This is the function where you set a function to retrieve errno
 * after a failed push or pull operation.
 *
 * @errno_func is of the form,
 * int (*gnutls_errno_func)(gnutls_transport_ptr_t);
 * and should return the errno.
 *
 * Since: 2.12.0
 **/
void gnutls_transport_set_errno_function(gnutls_session_t session,
					 gnutls_errno_func errno_func)
{
	session->internals.errno_func = errno_func;
}