Asterisk - The Open Source Telephony Project  18.5.0
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 <[email protected]>
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 iostream.h
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 
34 struct ssl_st; /* forward declaration */
35 struct ssl_ctx_st; /* forward declaration */
36 struct timeval; /* forward declaration */
37 typedef struct ssl_st SSL;
38 typedef struct ssl_ctx_st SSL_CTX;
39 
40 struct ast_iostream; /* forward declaration */
41 
42 /*!
43  * \brief Disable the iostream timeout timer.
44  *
45  * \param stream A pointer to an iostream
46  *
47  * \return Nothing
48  */
50 
51 /*!
52  * \brief Set the iostream inactivity timeout timer.
53  *
54  * \param stream A pointer to an iostream
55  * \param timeout Number of milliseconds to wait for data transfer with the peer.
56  *
57  * \details This is basically how much time we are willing to spend
58  * in an I/O call before we declare the peer unresponsive.
59  *
60  * \note Setting timeout to -1 disables the timeout.
61  * \note Setting this timeout replaces the I/O sequence timeout timer.
62  *
63  * \return Nothing
64  */
66 
67 /*!
68  * \brief Set the iostream inactivity & idle timeout timers.
69  *
70  * \param stream A pointer to an iostream
71  * \param timeout Number of milliseconds to wait for initial data transfer with
72  * the peer.
73  * \param timeout_reset Number of milliseconds to wait for subsequent data
74  * transfer with the peer.
75  *
76  * \details As an example, if you want to timeout a peer if they do not send an
77  * initial message within 5 seconds or if they do not send a message at
78  * least every 30 seconds, you would set \a timeout to \c 5000 and
79  * \a timeout_reset to \c 30000.
80  *
81  * \note Setting either of these timeouts to -1 will disable them.
82  *
83  * \return Nothing
84  */
86 
87 /*!
88  * \brief Set the iostream I/O sequence timeout timer.
89  *
90  * \param stream A pointer to an iostream
91  * \param start Time the I/O sequence timer starts.
92  * \param timeout Number of milliseconds from the start time before timeout.
93  *
94  * \details This is how much time are we willing to allow the peer
95  * to complete an operation that can take several I/O calls. The
96  * main use is as an authentication timer with us.
97  *
98  * \note Setting timeout to -1 disables the timeout.
99  * \note Setting this timeout replaces the inactivity timeout timer.
100  *
101  * \return Nothing
102  */
103 void ast_iostream_set_timeout_sequence(struct ast_iostream *stream, struct timeval start, int timeout);
104 
105 /*!
106  * \brief Set the iostream if it can exclusively depend upon the set timeouts.
107  *
108  * \param stream A pointer to an iostream
109  * \param exclusive_input TRUE if stream can exclusively wait for fd input.
110  * Otherwise, the stream will not wait for fd input. It will wait while
111  * trying to send data.
112  *
113  * \note The stream timeouts still need to be set.
114  *
115  * \return Nothing
116  */
118 
119 /*!
120  * \brief Get an iostream's file descriptor.
121  *
122  * \param stream A pointer to an iostream
123  *
124  * \return The file descriptor for the given iostream, or -1 if the iostream has no open
125  * file descriptor.
126  */
127 int ast_iostream_get_fd(struct ast_iostream *stream);
128 
129 /*!
130  * \brief Wait for input on the iostream's file descriptor
131  * \since 16.8.0
132  * \since 17.2.0
133  *
134  * \param stream A pointer to an iostream
135  * \param timeout the number of milliseconds to wait
136  *
137  * \retval -1 if error occurred
138  * \retval 0 if the timeout expired
139  * \retval 1 if the stream is ready for reading
140  */
141 int ast_iostream_wait_for_input(struct ast_iostream *stream, int timeout);
142 
143 /*!
144  * \brief Make an iostream non-blocking.
145  *
146  * \param stream A pointer to an iostream
147  *
148  * \return Nothing
149  */
150 void ast_iostream_nonblock(struct ast_iostream *stream);
151 
152 /*!
153  * \brief Get a pointer to an iostream's OpenSSL \c SSL structure
154  *
155  * \param stream A pointer to an iostream
156  *
157  * \return A pointer to the OpenSSL \c SSL structure for the given iostream, or
158  * \c NULL if TLS has not been initiated.
159  *
160  * \note If OpenSSL support is not included in the build, this will always return
161  * \c NULL.
162  */
163 SSL *ast_iostream_get_ssl(struct ast_iostream *stream);
164 
165 /*!
166  * \brief Read data from an iostream.
167  *
168  * \param stream A pointer to an iostream
169  * \param buffer Pointer to a buffer to store the read bytes.
170  * \param count The number of bytes to read.
171  *
172  * \return Upon successful completion, returns a non-negative integer indicating
173  * the number of bytes actually read. Otherwise, returns -1 and may set
174  * errno to indicate the error.
175  */
176 ssize_t ast_iostream_read(struct ast_iostream *stream, void *buffer, size_t count);
177 
178 /*!
179  * \brief Read a LF-terminated string from an iostream.
180  *
181  * \param stream A pointer to an iostream
182  * \param buffer Pointer to a buffer to store the read bytes.
183  * \param size The total size of \a buffer in bytes.
184  *
185  * \return The number of bytes stored in \a buffer, excluding the null byte used
186  * to terminate the string. If the size of \a buffer (indicated by the
187  * caller with the \a size argument) is not sufficient to store the
188  * entire line it will be truncated to fit the available space. The
189  * contents of \a buffer will always be terminated with a null byte. In
190  * the case of an error, \c -1 will be returned and \c errno may be set
191  * indicating the error.
192  */
193 ssize_t ast_iostream_gets(struct ast_iostream *stream, char *buffer, size_t size);
194 
195 /*!
196  * \brief Discard the specified number of bytes from an iostream.
197  *
198  * \param stream A pointer to an iostream
199  * \param count The number of bytes to discard.
200  *
201  * \return Upon successful completion, returns the number of bytes discarded.
202  * Otherwise, \c -1 is returned and \c errno may be set indicating the
203  * error.
204  */
205 ssize_t ast_iostream_discard(struct ast_iostream *stream, size_t count);
206 
207 /*!
208  * \brief Write data to an iostream.
209  *
210  * \param stream A pointer to an iostream
211  * \param buffer Pointer to a buffer from which to read bytes.
212  * \param count The number of bytes from \a buffer to write.
213  *
214  * \return Upon successful completion, returns the number of bytes actually
215  * written to the iostream. This number shall never be greater than
216  * \a count. Otherwise, returns \c -1 and may set \c errno to indicate
217  * the error.
218  */
219 ssize_t ast_iostream_write(struct ast_iostream *stream, const void *buffer, size_t count);
220 
221 /*!
222  * \brief Write a formatted string to an iostream.
223  *
224  * \param stream A pointer to an iostream
225  * \param format A format string, as documented by printf(3)
226  * \param ... Arguments for the provided \a format string
227  *
228  * \return The number of bytes written, or \c -1 if an error occurs. Note that if
229  * \c -1 is returned, the number of bytes written to the iostream is
230  * unspecified.
231  */
232 ssize_t __attribute__((format(printf, 2, 3))) ast_iostream_printf(
233  struct ast_iostream *stream, const char *format, ...);
234 
235 /*!
236  * \brief Create an iostream from a file descriptor.
237  *
238  * \param fd A pointer to an open file descriptor
239  *
240  * \return A newly allocated iostream or \c NULL if allocation fails.
241  */
242 struct ast_iostream *ast_iostream_from_fd(int *fd);
243 
244 /*!
245  * \brief Begin TLS on an iostream.
246  *
247  * \param stream A pointer to an iostream pointer
248  * \param ctx A pointer to an \c SSL_CTX which will be passed to \c SSL_new()
249  * \param client Non-zero to indicate that we are the client, zero to indicate
250  * that we are the server.
251  *
252  * \retval 0 success
253  * \retval -1 failure
254  *
255  * \note The iostream that is passed in \a stream may be replaced with a
256  * different one before this function returns.
257  * \note On failure, \c errno may be set providing additional information on why
258  * the failure occurred.
259  */
260 int ast_iostream_start_tls(struct ast_iostream **stream, SSL_CTX *ctx, int client);
261 
262 /*!
263  * \brief Close an iostream.
264  *
265  * \param stream A pointer to an iostream
266  *
267  * \retval 0 success
268  * \retval -1 failure
269  *
270  * \note On failure, \c errno may be set providing additional information on why
271  * the failure occurred.
272  */
273 int ast_iostream_close(struct ast_iostream *stream);
274 
275 #endif /* _ASTERISK_IOSTREAM_H */
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:148
int ast_iostream_wait_for_input(struct ast_iostream *stream, int timeout)
Wait for input on the iostream&#39;s file descriptor.
Definition: iostream.c:89
Asterisk main include file. File version handling, generic pbx functions.
int ast_iostream_start_tls(struct ast_iostream **stream, SSL_CTX *ctx, int client)
Begin TLS on an iostream.
Definition: iostream.c:620
ssize_t ast_iostream_write(struct ast_iostream *stream, const void *buffer, size_t count)
Write data to an iostream.
Definition: iostream.c:374
ssize_t ast_iostream_discard(struct ast_iostream *stream, size_t count)
Discard the specified number of bytes from an iostream.
Definition: iostream.c:357
static int timeout
Definition: cdr_mysql.c:86
void ast_iostream_set_timeout_inactivity(struct ast_iostream *stream, int timeout)
Set the iostream inactivity timeout timer.
Definition: iostream.c:121
struct ast_iostream * ast_iostream_from_fd(int *fd)
Create an iostream from a file descriptor.
Definition: iostream.c:604
int ast_iostream_get_fd(struct ast_iostream *stream)
Get an iostream&#39;s file descriptor.
Definition: iostream.c:84
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:130
struct ssl_st SSL
Definition: iostream.h:37
ssize_t ast_iostream_read(struct ast_iostream *stream, void *buffer, size_t count)
Read data from an iostream.
Definition: iostream.c:273
SSL * ast_iostream_get_ssl(struct ast_iostream *stream)
Get a pointer to an iostream&#39;s OpenSSL SSL structure.
Definition: iostream.c:108
int ast_iostream_close(struct ast_iostream *stream)
Close an iostream.
Definition: iostream.c:528
ssize_t ast_iostream_printf(struct ast_iostream *stream, const char *format,...)
Write a formatted string to an iostream.
Definition: iostream.c:491
void ast_iostream_set_timeout_disable(struct ast_iostream *stream)
Disable the iostream timeout timer.
Definition: iostream.c:113
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:139
struct timeval start
Definition: iostream.c:41
int timeout_reset
Definition: iostream.c:44
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:300
void ast_iostream_nonblock(struct ast_iostream *stream)
Make an iostream non-blocking.
Definition: iostream.c:103
struct ssl_ctx_st SSL_CTX
Definition: iostream.h:38
static snd_pcm_format_t format
Definition: chan_alsa.c:102
int exclusive_input
Definition: iostream.c:45