Asterisk - The Open Source Telephony Project GIT-master-545c459
Loading...
Searching...
No Matches
iostream.h
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2015, Digium, Inc.
5 *
6 * Timo Teräs <timo.teras@iki.fi>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19#ifndef _ASTERISK_IOSTREAM_H
20#define _ASTERISK_IOSTREAM_H
21
22/*!
23 * \file
24 *
25 * \brief Generic abstraction for input/output streams.
26 */
27
28#include "asterisk.h" /* for size_t, ssize_t, HAVE_OPENSSL */
29
30#if defined(HAVE_OPENSSL)
31#define DO_SSL /* comment in/out if you want to support ssl */
32#endif
33
34struct ssl_st; /* forward declaration */
35struct ssl_ctx_st; /* forward declaration */
36struct timeval; /* forward declaration */
37typedef struct ssl_st SSL;
38typedef struct ssl_ctx_st SSL_CTX;
39
40struct ast_iostream; /* forward declaration */
41
42/*!
43 * \brief Disable the iostream timeout timer.
44 *
45 * \param stream A pointer to an iostream
46 */
48
49/*!
50 * \brief Set the iostream inactivity timeout timer.
51 *
52 * \param stream A pointer to an iostream
53 * \param timeout Number of milliseconds to wait for data transfer with the peer.
54 *
55 * \details This is basically how much time we are willing to spend
56 * in an I/O call before we declare the peer unresponsive.
57 *
58 * \note Setting timeout to -1 disables the timeout.
59 * \note Setting this timeout replaces the I/O sequence timeout timer.
60 */
62
63/*!
64 * \brief Set the iostream inactivity & idle timeout timers.
65 *
66 * \param stream A pointer to an iostream
67 * \param timeout Number of milliseconds to wait for initial data transfer with
68 * the peer.
69 * \param timeout_reset Number of milliseconds to wait for subsequent data
70 * transfer with the peer.
71 *
72 * \details As an example, if you want to timeout a peer if they do not send an
73 * initial message within 5 seconds or if they do not send a message at
74 * least every 30 seconds, you would set \a timeout to \c 5000 and
75 * \a timeout_reset to \c 30000.
76 *
77 * \note Setting either of these timeouts to -1 will disable them.
78 */
80
81/*!
82 * \brief Set the iostream I/O sequence timeout timer.
83 *
84 * \param stream A pointer to an iostream
85 * \param start Time the I/O sequence timer starts.
86 * \param timeout Number of milliseconds from the start time before timeout.
87 *
88 * \details This is how much time are we willing to allow the peer
89 * to complete an operation that can take several I/O calls. The
90 * main use is as an authentication timer with us.
91 *
92 * \warning To be effective, the socket must be in non-blocking mode because the timeout
93 * can only be checked after a read or write operation returns. If the socket is in blocking
94 * mode (the default), those calls may block for longer than the specified timeout, possibly
95 * much longer.
96 *
97 * \note Setting timeout to -1 disables the timeout.
98 * \note Setting this timeout replaces the inactivity timeout timer.
99 */
100void ast_iostream_set_timeout_sequence(struct ast_iostream *stream, struct timeval start, int timeout);
101
102/*!
103 * \brief Set the iostream if it can exclusively depend upon the set timeouts.
104 *
105 * \param stream A pointer to an iostream
106 * \param exclusive_input TRUE if stream can exclusively wait for fd input.
107 * Otherwise, the stream will not wait for fd input. It will wait while
108 * trying to send data.
109 *
110 * \note The stream timeouts still need to be set.
111 */
113
114/*!
115 * \brief Set the iostream's SNI hostname for TLS client connections
116 *
117 * \param stream A pointer to an iostream
118 * \param sni_hostname The hostname to use for SNI when in client mode
119 *
120 * \retval 0 if the hostname was set successfully.
121 * \retval -1 if memory could not be allocated for the hostname.
122 */
123int ast_iostream_set_sni_hostname(struct ast_iostream *stream, const char *sni_hostname);
124
125/*!
126 * \brief Get an iostream's file descriptor.
127 *
128 * \param stream A pointer to an iostream
129 *
130 * \return The file descriptor for the given iostream
131 * \retval -1 if the iostream has no open file descriptor.
132 */
133int ast_iostream_get_fd(struct ast_iostream *stream);
134
135/*!
136 * \brief Wait for input on the iostream's file descriptor
137 * \since 16.8.0
138 * \since 17.2.0
139 *
140 * \param stream A pointer to an iostream
141 * \param timeout the number of milliseconds to wait
142 *
143 * \retval -1 if error occurred
144 * \retval 0 if the timeout expired
145 * \retval 1 if the stream is ready for reading
146 */
147int ast_iostream_wait_for_input(struct ast_iostream *stream, int timeout);
148
149/*!
150 * \brief Make an iostream non-blocking.
151 *
152 * \param stream A pointer to an iostream
153 */
154void ast_iostream_nonblock(struct ast_iostream *stream);
155
156/*!
157 * \brief Make an iostream blocking.
158 * \since 20.21.0
159 * \since 22.11.0
160 * \since 23.5.0
161 *
162 * \param stream A pointer to an iostream
163 */
164void ast_iostream_blocking(struct ast_iostream *stream);
165
166/*!
167 * \brief Get a pointer to an iostream's OpenSSL \c SSL structure
168 *
169 * \param stream A pointer to an iostream
170 *
171 * \return A pointer to the OpenSSL \c SSL structure for the given iostream
172 * \retval NULL if TLS has not been initiated.
173 *
174 * \note If OpenSSL support is not included in the build, this will always return
175 * \c NULL.
176 */
177SSL *ast_iostream_get_ssl(struct ast_iostream *stream);
178
179/*!
180 * \brief Read data from an iostream.
181 *
182 * \param stream A pointer to an iostream
183 * \param buffer Pointer to a buffer to store the read bytes.
184 * \param count The number of bytes to read.
185 *
186 * \return Upon successful completion, returns a non-negative integer indicating
187 * the number of bytes actually read. Otherwise, returns -1 and may set
188 * errno to indicate the error.
189 */
190ssize_t ast_iostream_read(struct ast_iostream *stream, void *buffer, size_t count);
191
192/*!
193 * \brief Read a LF-terminated string from an iostream.
194 *
195 * \param stream A pointer to an iostream
196 * \param buffer Pointer to a buffer to store the read bytes.
197 * \param size The total size of \a buffer in bytes.
198 *
199 * \return The number of bytes stored in \a buffer, excluding the null byte used
200 * to terminate the string. If the size of \a buffer (indicated by the
201 * caller with the \a size argument) is not sufficient to store the
202 * entire line it will be truncated to fit the available space. The
203 * contents of \a buffer will always be terminated with a null byte. In
204 * the case of an error, \c -1 will be returned and \c errno may be set
205 * indicating the error.
206 */
207ssize_t ast_iostream_gets(struct ast_iostream *stream, char *buffer, size_t size);
208
209/*!
210 * \brief Discard the specified number of bytes from an iostream.
211 *
212 * \param stream A pointer to an iostream
213 * \param count The number of bytes to discard.
214 *
215 * \return Upon successful completion, returns the number of bytes discarded.
216 * Otherwise, \c -1 is returned and \c errno may be set indicating the
217 * error.
218 */
219ssize_t ast_iostream_discard(struct ast_iostream *stream, size_t count);
220
221/*!
222 * \brief Write data to an iostream.
223 *
224 * \param stream A pointer to an iostream
225 * \param buffer Pointer to a buffer from which to read bytes.
226 * \param count The number of bytes from \a buffer to write.
227 *
228 * \return Upon successful completion, returns the number of bytes actually
229 * written to the iostream. This number shall never be greater than
230 * \a count. Otherwise, returns \c -1 and may set \c errno to indicate
231 * the error.
232 */
233ssize_t ast_iostream_write(struct ast_iostream *stream, const void *buffer, size_t count);
234
235/*!
236 * \brief Write a formatted string to an iostream.
237 *
238 * \param stream A pointer to an iostream
239 * \param format A format string, as documented by printf(3)
240 * \param ... Arguments for the provided \a format string
241 *
242 * \return The number of bytes written, or \c -1 if an error occurs. Note that if
243 * \c -1 is returned, the number of bytes written to the iostream is
244 * unspecified.
245 */
247 struct ast_iostream *stream, const char *format, ...);
248
249/*!
250 * \brief Create an iostream from a file descriptor.
251 *
252 * \param fd A pointer to an open file descriptor
253 *
254 * \return A newly allocated iostream or \c NULL if allocation fails.
255 */
257
258/*!
259 * \brief Begin TLS on an iostream.
260 *
261 * \param stream A pointer to an iostream pointer
262 * \param ctx A pointer to an \c SSL_CTX which will be passed to \c SSL_new()
263 * \param client Non-zero to indicate that we are the client, zero to indicate
264 * that we are the server.
265 *
266 * \retval 0 success
267 * \retval -1 failure
268 *
269 * \note The iostream that is passed in \a stream may be replaced with a
270 * different one before this function returns.
271 * \note On failure, \c errno may be set providing additional information on why
272 * the failure occurred.
273 */
274int ast_iostream_start_tls(struct ast_iostream **stream, SSL_CTX *ctx, int client);
275
276/*!
277 * \brief Close an iostream.
278 *
279 * \param stream A pointer to an iostream
280 *
281 * \retval 0 success
282 * \retval -1 failure
283 *
284 * \note On failure, \c errno may be set providing additional information on why
285 * the failure occurred.
286 */
288
289#endif /* _ASTERISK_IOSTREAM_H */
Asterisk main include file. File version handling, generic pbx functions.
struct ssl_ctx_st SSL_CTX
Definition iostream.h:38
void ast_iostream_blocking(struct ast_iostream *stream)
Make an iostream blocking.
Definition iostream.c:109
ssize_t ast_iostream_printf(struct ast_iostream *stream, const char *format,...)
Write a formatted string to an iostream.
Definition iostream.c:507
ssize_t ast_iostream_gets(struct ast_iostream *stream, char *buffer, size_t size)
Read a LF-terminated string from an iostream.
Definition iostream.c:316
void ast_iostream_set_timeout_inactivity(struct ast_iostream *stream, int timeout)
Set the iostream inactivity timeout timer.
Definition iostream.c:127
struct ast_iostream * ast_iostream_from_fd(int *fd)
Create an iostream from a file descriptor.
Definition iostream.c:616
void ast_iostream_set_timeout_idle_inactivity(struct ast_iostream *stream, int timeout, int timeout_reset)
Set the iostream inactivity & idle timeout timers.
Definition iostream.c:136
SSL * ast_iostream_get_ssl(struct ast_iostream *stream)
Get a pointer to an iostream's OpenSSL SSL structure.
Definition iostream.c:114
int ast_iostream_start_tls(struct ast_iostream **stream, SSL_CTX *ctx, int client)
Begin TLS on an iostream.
Definition iostream.c:632
ssize_t ast_iostream_write(struct ast_iostream *stream, const void *buffer, size_t count)
Write data to an iostream.
Definition iostream.c:390
int ast_iostream_get_fd(struct ast_iostream *stream)
Get an iostream's file descriptor.
Definition iostream.c:85
struct ssl_st SSL
Definition iostream.h:37
void ast_iostream_set_exclusive_input(struct ast_iostream *stream, int exclusive_input)
Set the iostream if it can exclusively depend upon the set timeouts.
Definition iostream.c:154
int ast_iostream_set_sni_hostname(struct ast_iostream *stream, const char *sni_hostname)
Set the iostream's SNI hostname for TLS client connections.
Definition iostream.c:161
ssize_t ast_iostream_read(struct ast_iostream *stream, void *buffer, size_t count)
Read data from an iostream.
Definition iostream.c:289
void ast_iostream_set_timeout_sequence(struct ast_iostream *stream, struct timeval start, int timeout)
Set the iostream I/O sequence timeout timer.
Definition iostream.c:145
void ast_iostream_nonblock(struct ast_iostream *stream)
Make an iostream non-blocking.
Definition iostream.c:104
ssize_t ast_iostream_discard(struct ast_iostream *stream, size_t count)
Discard the specified number of bytes from an iostream.
Definition iostream.c:373
int ast_iostream_close(struct ast_iostream *stream)
Close an iostream.
Definition iostream.c:544
void ast_iostream_set_timeout_disable(struct ast_iostream *stream)
Disable the iostream timeout timer.
Definition iostream.c:119
int ast_iostream_wait_for_input(struct ast_iostream *stream, int timeout)
Wait for input on the iostream's file descriptor.
Definition iostream.c:90
char * sni_hostname
Definition iostream.c:49
int exclusive_input
Definition iostream.c:45
int timeout_reset
Definition iostream.c:44
struct timeval start
Definition iostream.c:41