Coverage Report

Created: 2024-06-20 06:28

/src/gnutls/lib/system_override.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3
 *
4
 * Author: Nikos Mavrogiannopoulos
5
 *
6
 * This file is part of GnuTLS.
7
 *
8
 * The GnuTLS is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public License
10
 * as published by the Free Software Foundation; either version 2.1 of
11
 * the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
20
 *
21
 */
22
23
/* This file contains function that will override the 
24
 * default berkeley sockets API per session.
25
 */
26
27
#include "gnutls_int.h"
28
#include "errors.h"
29
#include "num.h"
30
#include "record.h"
31
#include "buffers.h"
32
#include "mbuffers.h"
33
#include "state.h"
34
#include "dtls.h"
35
#include "system.h"
36
37
#include <errno.h>
38
#ifdef _WIN32
39
#include <windows.h>
40
#endif
41
42
/**
43
 * gnutls_transport_set_errno:
44
 * @session: is a #gnutls_session_t type.
45
 * @err: error value to store in session-specific errno variable.
46
 *
47
 * Store @err in the session-specific errno variable.  Useful values
48
 * for @err are EINTR, EAGAIN and EMSGSIZE, other values are treated will be
49
 * treated as real errors in the push/pull function.
50
 *
51
 * This function is useful in replacement push and pull functions set by
52
 * gnutls_transport_set_push_function() and
53
 * gnutls_transport_set_pull_function() under Windows, where the
54
 * replacements may not have access to the same @errno
55
 * variable that is used by GnuTLS (e.g., the application is linked to
56
 * msvcr71.dll and gnutls is linked to msvcrt.dll).
57
 *
58
 * This function is unreliable if you are using the same
59
 * @session in different threads for sending and receiving.
60
 *
61
 **/
62
void gnutls_transport_set_errno(gnutls_session_t session, int err)
63
0
{
64
0
  session->internals.errnum = err;
65
0
}
66
67
/**
68
 * gnutls_transport_set_pull_function:
69
 * @session: is a #gnutls_session_t type.
70
 * @pull_func: a callback function similar to read()
71
 *
72
 * This is the function where you set a function for gnutls to receive
73
 * data.  Normally, if you use berkeley style sockets, do not need to
74
 * use this function since the default recv(2) will probably be ok.
75
 * The callback should return 0 on connection termination, a positive
76
 * number indicating the number of bytes received, and -1 on error.
77
 *
78
 * @gnutls_pull_func is of the form,
79
 * ssize_t (*gnutls_pull_func)(gnutls_transport_ptr_t, void*, size_t);
80
 **/
81
void gnutls_transport_set_pull_function(gnutls_session_t session,
82
          gnutls_pull_func pull_func)
83
0
{
84
0
  session->internals.pull_func = pull_func;
85
0
}
86
87
/**
88
 * gnutls_transport_set_pull_timeout_function:
89
 * @session: is a #gnutls_session_t type.
90
 * @func: a callback function
91
 *
92
 * This is the function where you set a function for gnutls to know
93
 * whether data are ready to be received. It should wait for data a
94
 * given time frame in milliseconds. The callback should return 0 on 
95
 * timeout, a positive number if data can be received, and -1 on error.
96
 * You'll need to override this function if select() is not suitable
97
 * for the provided transport calls.
98
 *
99
 * As with select(), if the timeout value is zero the callback should return
100
 * zero if no data are immediately available. The special value
101
 * %GNUTLS_INDEFINITE_TIMEOUT indicates that the callback should wait indefinitely
102
 * for data.
103
 *
104
 * @gnutls_pull_timeout_func is of the form,
105
 * int (*gnutls_pull_timeout_func)(gnutls_transport_ptr_t, unsigned int ms);
106
 *
107
 * This callback is necessary when gnutls_handshake_set_timeout() or 
108
 * gnutls_record_set_timeout() are set, under TLS1.3 and for enforcing the DTLS
109
 * mode timeouts when in blocking mode.
110
 *
111
 * For compatibility with future GnuTLS versions this callback must be set when
112
 * a custom pull function is registered. The callback will not be used when the
113
 * session is in TLS mode with non-blocking sockets. That is, when %GNUTLS_NONBLOCK
114
 * is specified for a TLS session in gnutls_init().
115
 *
116
 * The helper function gnutls_system_recv_timeout() is provided to
117
 * simplify writing callbacks. 
118
 *
119
 * Since: 3.0
120
 **/
121
void gnutls_transport_set_pull_timeout_function(gnutls_session_t session,
122
            gnutls_pull_timeout_func func)
123
0
{
124
0
  session->internals.pull_timeout_func = func;
125
0
}
126
127
/**
128
 * gnutls_transport_set_push_function:
129
 * @session: is a #gnutls_session_t type.
130
 * @push_func: a callback function similar to write()
131
 *
132
 * This is the function where you set a push function for gnutls to
133
 * use in order to send data.  If you are going to use berkeley style
134
 * sockets, you do not need to use this function since the default
135
 * send(2) will probably be ok.  Otherwise you should specify this
136
 * function for gnutls to be able to send data.
137
 * The callback should return a positive number indicating the
138
 * bytes sent, and -1 on error.
139
 *
140
 * @push_func is of the form,
141
 * ssize_t (*gnutls_push_func)(gnutls_transport_ptr_t, const void*, size_t);
142
 *
143
 **/
144
void gnutls_transport_set_push_function(gnutls_session_t session,
145
          gnutls_push_func push_func)
146
0
{
147
0
  session->internals.push_func = push_func;
148
0
  session->internals.vec_push_func = NULL;
149
0
}
150
151
/**
152
 * gnutls_transport_set_vec_push_function:
153
 * @session: is a #gnutls_session_t type.
154
 * @vec_func: a callback function similar to writev()
155
 *
156
 * Using this function you can override the default writev(2)
157
 * function for gnutls to send data. Setting this callback 
158
 * instead of gnutls_transport_set_push_function() is recommended
159
 * since it introduces less overhead in the TLS handshake process.
160
 *
161
 * @vec_func is of the form,
162
 * ssize_t (*gnutls_vec_push_func) (gnutls_transport_ptr_t, const giovec_t * iov, int iovcnt);
163
 *
164
 * Since: 2.12.0
165
 **/
166
void gnutls_transport_set_vec_push_function(gnutls_session_t session,
167
              gnutls_vec_push_func vec_func)
168
0
{
169
0
  session->internals.push_func = NULL;
170
0
  session->internals.vec_push_func = vec_func;
171
0
}
172
173
/**
174
 * gnutls_transport_set_errno_function:
175
 * @session: is a #gnutls_session_t type.
176
 * @errno_func: a callback function similar to write()
177
 *
178
 * This is the function where you set a function to retrieve errno
179
 * after a failed push or pull operation.
180
 *
181
 * @errno_func is of the form,
182
 * int (*gnutls_errno_func)(gnutls_transport_ptr_t);
183
 * and should return the errno.
184
 *
185
 * Since: 2.12.0
186
 **/
187
void gnutls_transport_set_errno_function(gnutls_session_t session,
188
           gnutls_errno_func errno_func)
189
0
{
190
0
  session->internals.errno_func = errno_func;
191
0
}