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 #ifndef GNUTLS_LIB_BUFFERS_H
24 #define GNUTLS_LIB_BUFFERS_H
25 
26 #include "mbuffers.h"
27 
28 #define MBUFFER_FLUSH 1
29 
30 void
31 _gnutls_record_buffer_put(gnutls_session_t session,
32 			  content_type_t type, uint64_t seq,
33 			  mbuffer_st * bufel);
34 
_gnutls_record_buffer_get_size(gnutls_session_t session)35 inline static int _gnutls_record_buffer_get_size(gnutls_session_t session)
36 {
37 	return session->internals.record_buffer.byte_length;
38 }
39 
40 #define NO_TIMEOUT_FUNC_SET(session) unlikely(session->internals.pull_timeout_func == gnutls_system_recv_timeout \
41 	     && session->internals.pull_func != system_read)
42 
43 /*-
44  * record_check_unprocessed:
45  * @session: is a #gnutls_session_t structure.
46  *
47  * This function checks if there are unprocessed data
48  * in the gnutls record buffers. Those data might not
49  * be complete records.
50  *
51  * Returns: Returns the size of the data or zero.
52  -*/
record_check_unprocessed(gnutls_session_t session)53 inline static size_t record_check_unprocessed(gnutls_session_t session)
54 {
55 	return session->internals.record_recv_buffer.byte_length;
56 }
57 
58 int _gnutls_record_buffer_get(content_type_t type,
59 			      gnutls_session_t session, uint8_t * data,
60 			      size_t length, uint8_t seq[8]);
61 int _gnutls_record_buffer_get_packet(content_type_t type,
62 				     gnutls_session_t session,
63 				     gnutls_packet_t *packet);
64 ssize_t _gnutls_io_read_buffered(gnutls_session_t, size_t n,
65 				 content_type_t, unsigned int *ms);
66 int _gnutls_io_clear_peeked_data(gnutls_session_t session);
67 
68 ssize_t _gnutls_io_write_buffered(gnutls_session_t session,
69 				  mbuffer_st * bufel, unsigned int mflag);
70 
71 int _gnutls_handshake_io_cache_int(gnutls_session_t,
72 				   gnutls_handshake_description_t,
73 				   mbuffer_st * bufel);
74 
75 ssize_t
76 _gnutls_handshake_io_recv_int(gnutls_session_t session,
77 			      gnutls_handshake_description_t htype,
78 			      handshake_buffer_st * hsk,
79 			      unsigned int optional);
80 
81 ssize_t _gnutls_io_write_flush(gnutls_session_t session);
82 int _gnutls_io_check_recv(gnutls_session_t session, unsigned int ms);
83 ssize_t _gnutls_handshake_io_write_flush(gnutls_session_t session);
84 
_gnutls_handshake_buffer_clear(handshake_buffer_st * hsk)85 inline static void _gnutls_handshake_buffer_clear(handshake_buffer_st *
86 						  hsk)
87 {
88 	_gnutls_buffer_clear(&hsk->data);
89 	hsk->htype = -1;
90 }
91 
_gnutls_handshake_buffer_init(handshake_buffer_st * hsk)92 inline static void _gnutls_handshake_buffer_init(handshake_buffer_st * hsk)
93 {
94 	memset(hsk, 0, sizeof(*hsk));
95 	_gnutls_buffer_init(&hsk->data);
96 	hsk->htype = -1;
97 }
98 
_gnutls_handshake_recv_buffer_clear(gnutls_session_t session)99 inline static void _gnutls_handshake_recv_buffer_clear(gnutls_session_t
100 						       session)
101 {
102 	int i;
103 	for (i = 0; i < session->internals.handshake_recv_buffer_size; i++)
104 		_gnutls_handshake_buffer_clear(&session->internals.
105 					       handshake_recv_buffer[i]);
106 	session->internals.handshake_recv_buffer_size = 0;
107 	_mbuffer_head_clear(&session->internals.handshake_header_recv_buffer);
108 }
109 
_gnutls_handshake_recv_buffer_init(gnutls_session_t session)110 inline static void _gnutls_handshake_recv_buffer_init(gnutls_session_t
111 						      session)
112 {
113 	int i;
114 	for (i = 0; i < MAX_HANDSHAKE_MSGS; i++) {
115 		_gnutls_handshake_buffer_init(&session->internals.
116 					      handshake_recv_buffer[i]);
117 	}
118 	session->internals.handshake_recv_buffer_size = 0;
119 	_mbuffer_head_init(&session->internals.handshake_header_recv_buffer);
120 }
121 
122 int _gnutls_parse_record_buffered_msgs(gnutls_session_t session);
123 
124 ssize_t
125 _gnutls_recv_in_buffers(gnutls_session_t session, content_type_t type,
126 			gnutls_handshake_description_t htype,
127 			unsigned int ms);
128 
129 #define _gnutls_handshake_io_buffer_clear( session) \
130 	_mbuffer_head_clear( &session->internals.handshake_send_buffer); \
131 	_gnutls_handshake_recv_buffer_clear( session);
132 
133 #endif /* GNUTLS_LIB_BUFFERS_H */
134