/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 |
82 | | gnutls_transport_set_pull_function(gnutls_session_t session, |
83 | | gnutls_pull_func pull_func) |
84 | 0 | { |
85 | 0 | session->internals.pull_func = pull_func; |
86 | 0 | } |
87 | | |
88 | | /** |
89 | | * gnutls_transport_set_pull_timeout_function: |
90 | | * @session: is a #gnutls_session_t type. |
91 | | * @func: a callback function |
92 | | * |
93 | | * This is the function where you set a function for gnutls to know |
94 | | * whether data are ready to be received. It should wait for data a |
95 | | * given time frame in milliseconds. The callback should return 0 on |
96 | | * timeout, a positive number if data can be received, and -1 on error. |
97 | | * You'll need to override this function if select() is not suitable |
98 | | * for the provided transport calls. |
99 | | * |
100 | | * As with select(), if the timeout value is zero the callback should return |
101 | | * zero if no data are immediately available. The special value |
102 | | * %GNUTLS_INDEFINITE_TIMEOUT indicates that the callback should wait indefinitely |
103 | | * for data. |
104 | | * |
105 | | * @gnutls_pull_timeout_func is of the form, |
106 | | * int (*gnutls_pull_timeout_func)(gnutls_transport_ptr_t, unsigned int ms); |
107 | | * |
108 | | * This callback is necessary when gnutls_handshake_set_timeout() or |
109 | | * gnutls_record_set_timeout() are set, under TLS1.3 and for enforcing the DTLS |
110 | | * mode timeouts when in blocking mode. |
111 | | * |
112 | | * For compatibility with future GnuTLS versions this callback must be set when |
113 | | * a custom pull function is registered. The callback will not be used when the |
114 | | * session is in TLS mode with non-blocking sockets. That is, when %GNUTLS_NONBLOCK |
115 | | * is specified for a TLS session in gnutls_init(). |
116 | | * |
117 | | * The helper function gnutls_system_recv_timeout() is provided to |
118 | | * simplify writing callbacks. |
119 | | * |
120 | | * Since: 3.0 |
121 | | **/ |
122 | | void |
123 | | gnutls_transport_set_pull_timeout_function(gnutls_session_t session, |
124 | | gnutls_pull_timeout_func func) |
125 | 0 | { |
126 | 0 | session->internals.pull_timeout_func = func; |
127 | 0 | } |
128 | | |
129 | | /** |
130 | | * gnutls_transport_set_push_function: |
131 | | * @session: is a #gnutls_session_t type. |
132 | | * @push_func: a callback function similar to write() |
133 | | * |
134 | | * This is the function where you set a push function for gnutls to |
135 | | * use in order to send data. If you are going to use berkeley style |
136 | | * sockets, you do not need to use this function since the default |
137 | | * send(2) will probably be ok. Otherwise you should specify this |
138 | | * function for gnutls to be able to send data. |
139 | | * The callback should return a positive number indicating the |
140 | | * bytes sent, and -1 on error. |
141 | | * |
142 | | * @push_func is of the form, |
143 | | * ssize_t (*gnutls_push_func)(gnutls_transport_ptr_t, const void*, size_t); |
144 | | * |
145 | | **/ |
146 | | void |
147 | | gnutls_transport_set_push_function(gnutls_session_t session, |
148 | | gnutls_push_func push_func) |
149 | 0 | { |
150 | 0 | session->internals.push_func = push_func; |
151 | 0 | session->internals.vec_push_func = NULL; |
152 | 0 | } |
153 | | |
154 | | /** |
155 | | * gnutls_transport_set_vec_push_function: |
156 | | * @session: is a #gnutls_session_t type. |
157 | | * @vec_func: a callback function similar to writev() |
158 | | * |
159 | | * Using this function you can override the default writev(2) |
160 | | * function for gnutls to send data. Setting this callback |
161 | | * instead of gnutls_transport_set_push_function() is recommended |
162 | | * since it introduces less overhead in the TLS handshake process. |
163 | | * |
164 | | * @vec_func is of the form, |
165 | | * ssize_t (*gnutls_vec_push_func) (gnutls_transport_ptr_t, const giovec_t * iov, int iovcnt); |
166 | | * |
167 | | * Since: 2.12.0 |
168 | | **/ |
169 | | void |
170 | | gnutls_transport_set_vec_push_function(gnutls_session_t session, |
171 | | gnutls_vec_push_func vec_func) |
172 | 0 | { |
173 | 0 | session->internals.push_func = NULL; |
174 | 0 | session->internals.vec_push_func = vec_func; |
175 | 0 | } |
176 | | |
177 | | /** |
178 | | * gnutls_transport_set_errno_function: |
179 | | * @session: is a #gnutls_session_t type. |
180 | | * @errno_func: a callback function similar to write() |
181 | | * |
182 | | * This is the function where you set a function to retrieve errno |
183 | | * after a failed push or pull operation. |
184 | | * |
185 | | * @errno_func is of the form, |
186 | | * int (*gnutls_errno_func)(gnutls_transport_ptr_t); |
187 | | * and should return the errno. |
188 | | * |
189 | | * Since: 2.12.0 |
190 | | **/ |
191 | | void |
192 | | gnutls_transport_set_errno_function(gnutls_session_t session, |
193 | | gnutls_errno_func errno_func) |
194 | 0 | { |
195 | 0 | session->internals.errno_func = errno_func; |
196 | 0 | } |