Asterisk - The Open Source Telephony Project
18.5.0
|
#include "asterisk.h"
#include "asterisk/iostream.h"
#include <fcntl.h>
#include <openssl/err.h>
#include <openssl/opensslv.h>
#include <openssl/ssl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include "asterisk/astobj2.h"
#include "asterisk/logger.h"
#include "asterisk/strings.h"
#include "asterisk/threadstorage.h"
#include "asterisk/time.h"
#include "asterisk/utils.h"
Go to the source code of this file.
Data Structures | |
struct | ast_iostream |
Macros | |
#define | ERR2STR_BUFSIZE 128 |
Functions | |
static void | __init_err2str_threadbuf (void) |
int | ast_iostream_close (struct ast_iostream *stream) |
Close an iostream. More... | |
ssize_t | ast_iostream_discard (struct ast_iostream *stream, size_t size) |
Discard the specified number of bytes from an iostream. More... | |
struct ast_iostream * | ast_iostream_from_fd (int *fd) |
Create an iostream from a file descriptor. More... | |
int | ast_iostream_get_fd (struct ast_iostream *stream) |
Get an iostream's file descriptor. More... | |
SSL * | ast_iostream_get_ssl (struct ast_iostream *stream) |
Get a pointer to an iostream's OpenSSL SSL structure. More... | |
ssize_t | ast_iostream_gets (struct ast_iostream *stream, char *buffer, size_t size) |
Read a LF-terminated string from an iostream. More... | |
void | ast_iostream_nonblock (struct ast_iostream *stream) |
Make an iostream non-blocking. More... | |
ssize_t | ast_iostream_printf (struct ast_iostream *stream, const char *format,...) |
Write a formatted string to an iostream. More... | |
ssize_t | ast_iostream_read (struct ast_iostream *stream, void *buffer, size_t count) |
Read data from an iostream. More... | |
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. More... | |
void | ast_iostream_set_timeout_disable (struct ast_iostream *stream) |
Disable the iostream timeout timer. More... | |
void | ast_iostream_set_timeout_idle_inactivity (struct ast_iostream *stream, int timeout, int timeout_reset) |
Set the iostream inactivity & idle timeout timers. More... | |
void | ast_iostream_set_timeout_inactivity (struct ast_iostream *stream, int timeout) |
Set the iostream inactivity timeout timer. More... | |
void | ast_iostream_set_timeout_sequence (struct ast_iostream *stream, struct timeval start, int timeout) |
Set the iostream I/O sequence timeout timer. More... | |
int | ast_iostream_start_tls (struct ast_iostream **pstream, SSL_CTX *ssl_ctx, int client) |
Begin TLS on an iostream. More... | |
int | ast_iostream_wait_for_input (struct ast_iostream *stream, int timeout) |
Wait for input on the iostream's file descriptor. More... | |
ssize_t | ast_iostream_write (struct ast_iostream *stream, const void *buffer, size_t size) |
Write data to an iostream. More... | |
static void | iostream_dtor (void *cookie) |
static ssize_t | iostream_read (struct ast_iostream *stream, void *buf, size_t size) |
static const char * | ssl_error_to_string (int sslerr, int ret) |
Variables | |
static struct ast_threadstorage | err2str_threadbuf = { .once = PTHREAD_ONCE_INIT , .key_init = __init_err2str_threadbuf , .custom_init = NULL , } |
#define ERR2STR_BUFSIZE 128 |
Definition at line 53 of file iostream.c.
Referenced by ssl_error_to_string().
|
static |
Definition at line 52 of file iostream.c.
int ast_iostream_close | ( | struct ast_iostream * | stream | ) |
Close an iostream.
stream | A pointer to an iostream |
0 | success |
-1 | failure |
errno
may be set providing additional information on why the failure occurred. Definition at line 528 of file iostream.c.
References ao2_t_ref, ast_log, errno, ast_iostream::fd, LOG_ERROR, NULL, ast_iostream::ssl, and ssl_error_to_string().
Referenced by app_exec(), ast_tcptls_close_session_file(), ast_websocket_close(), auth_http_callback(), close_mansession_file(), generic_http_callback(), session_destroy_fn(), and session_instance_destructor().
ssize_t ast_iostream_discard | ( | struct ast_iostream * | stream, |
size_t | count | ||
) |
Discard the specified number of bytes from an iostream.
stream | A pointer to an iostream |
count | The number of bytes to discard. |
-1
is returned and errno
may be set indicating the error. Definition at line 357 of file iostream.c.
References ast_iostream_read(), and buf.
Referenced by http_body_discard_contents().
struct ast_iostream* ast_iostream_from_fd | ( | int * | fd | ) |
Create an iostream from a file descriptor.
fd | A pointer to an open file descriptor |
NULL
if allocation fails. Definition at line 604 of file iostream.c.
References AO2_ALLOC_OPT_LOCK_NOLOCK, ao2_alloc_options, ast_iostream::fd, iostream_dtor(), ast_iostream::timeout, and ast_iostream::timeout_reset.
Referenced by app_exec(), ast_tcptls_client_create(), ast_tcptls_server_root(), auth_http_callback(), and generic_http_callback().
int ast_iostream_get_fd | ( | struct ast_iostream * | stream | ) |
Get an iostream's file descriptor.
stream | A pointer to an iostream |
Definition at line 84 of file iostream.c.
References ast_iostream::fd.
Referenced by _sip_tcp_helper_thread(), action_waitevent(), ast_websocket_fd(), ast_websocket_uri_cb(), auth_http_callback(), eivr_comm(), get_input(), handle_showmanconn(), httpd_helper_thread(), process_output(), session_do(), sip_prepare_socket(), and sip_tcptls_read().
SSL* ast_iostream_get_ssl | ( | struct ast_iostream * | stream | ) |
Get a pointer to an iostream's OpenSSL SSL
structure.
stream | A pointer to an iostream |
SSL
structure for the given iostream, or NULL
if TLS has not been initiated.NULL
. Definition at line 108 of file iostream.c.
References ast_iostream::ssl.
Referenced by _sip_tcp_helper_thread(), ast_websocket_uri_cb(), handle_tcptls_connection(), sip_threadinfo_create(), and websocket_client_connect().
ssize_t ast_iostream_gets | ( | struct ast_iostream * | stream, |
char * | buffer, | ||
size_t | size | ||
) |
Read a LF-terminated string from an iostream.
stream | A pointer to an iostream |
buffer | Pointer to a buffer to store the read bytes. |
size | The total size of buffer in bytes. |
-1
will be returned and errno
may be set indicating the error. Definition at line 300 of file iostream.c.
References iostream_read(), len(), ast_iostream::rbuf, ast_iostream::rbufhead, and ast_iostream::rbuflen.
Referenced by eivr_comm(), http_body_discard_chunk_trailer_headers(), http_body_get_chunk_length(), http_request_headers_get(), httpd_process_request(), and websocket_client_handshake_get_response().
void ast_iostream_nonblock | ( | struct ast_iostream * | stream | ) |
Make an iostream non-blocking.
stream | A pointer to an iostream |
Definition at line 103 of file iostream.c.
References ast_fd_set_flags, and ast_iostream::fd.
Referenced by _sip_tcp_helper_thread(), ast_websocket_set_nonblock(), httpd_helper_thread(), and session_do().
ssize_t ast_iostream_printf | ( | struct ast_iostream * | stream, |
const char * | format, | ||
... | |||
) |
Write a formatted string to an iostream.
stream | A pointer to an iostream |
format | A format string, as documented by printf(3) |
... | Arguments for the provided format string |
-1
if an error occurs. Note that if -1
is returned, the number of bytes written to the iostream is unspecified. Definition at line 491 of file iostream.c.
References ast_free, ast_iostream_write(), ast_malloc, buf, error(), and len().
Referenced by ast_http_send(), ast_websocket_uri_cb(), and websocket_client_handshake().
ssize_t ast_iostream_read | ( | struct ast_iostream * | stream, |
void * | buffer, | ||
size_t | count | ||
) |
Read data from an iostream.
stream | A pointer to an iostream |
buffer | Pointer to a buffer to store the read bytes. |
count | The number of bytes to read. |
Definition at line 273 of file iostream.c.
References errno, ast_iostream::fd, iostream_read(), ast_iostream::rbufhead, and ast_iostream::rbuflen.
Referenced by ast_iostream_discard(), get_input(), http_body_check_chunk_sync(), http_body_read_contents(), readmimefile(), sip_tcptls_read(), and ws_safe_read().
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.
stream | A pointer to an iostream |
exclusive_input | TRUE if stream can exclusively wait for fd input. Otherwise, the stream will not wait for fd input. It will wait while trying to send data. |
Definition at line 148 of file iostream.c.
References ast_assert, ast_iostream::exclusive_input, and NULL.
Referenced by _sip_tcp_helper_thread(), ast_websocket_set_nonblock(), ast_websocket_uri_cb(), httpd_helper_thread(), and session_do().
void ast_iostream_set_timeout_disable | ( | struct ast_iostream * | stream | ) |
Disable the iostream timeout timer.
stream | A pointer to an iostream |
Definition at line 113 of file iostream.c.
References ast_assert, NULL, ast_iostream::timeout, and ast_iostream::timeout_reset.
Referenced by _sip_tcp_helper_thread(), ast_websocket_close(), ast_websocket_write(), send_string(), and session_do().
void ast_iostream_set_timeout_idle_inactivity | ( | struct ast_iostream * | stream, |
int | timeout, | ||
int | timeout_reset | ||
) |
Set the iostream inactivity & idle timeout timers.
stream | A pointer to an iostream |
timeout | Number of milliseconds to wait for initial data transfer with the peer. |
timeout_reset | Number of milliseconds to wait for subsequent data transfer with the peer. |
As an example, if you want to timeout a peer if they do not send an initial message within 5 seconds or if they do not send a message at least every 30 seconds, you would set timeout to 5000
and timeout_reset to 30000
.
Definition at line 130 of file iostream.c.
References ast_assert, NULL, ast_iostream::start, ast_iostream::timeout, and ast_iostream::timeout_reset.
Referenced by httpd_helper_thread().
void ast_iostream_set_timeout_inactivity | ( | struct ast_iostream * | stream, |
int | timeout | ||
) |
Set the iostream inactivity timeout timer.
stream | A pointer to an iostream |
timeout | Number of milliseconds to wait for data transfer with the peer. |
This is basically how much time we are willing to spend in an I/O call before we declare the peer unresponsive.
Definition at line 121 of file iostream.c.
References ast_assert, NULL, ast_iostream::start, ast_iostream::timeout, and ast_iostream::timeout_reset.
Referenced by ast_websocket_close(), and send_string().
void ast_iostream_set_timeout_sequence | ( | struct ast_iostream * | stream, |
struct timeval | start, | ||
int | timeout | ||
) |
Set the iostream I/O sequence timeout timer.
stream | A pointer to an iostream |
start | Time the I/O sequence timer starts. |
timeout | Number of milliseconds from the start time before timeout. |
This is how much time are we willing to allow the peer to complete an operation that can take several I/O calls. The main use is as an authentication timer with us.
Definition at line 139 of file iostream.c.
References ast_assert, NULL, ast_iostream::start, ast_iostream::timeout, and ast_iostream::timeout_reset.
Referenced by _sip_tcp_helper_thread(), ast_websocket_write(), and session_do().
int ast_iostream_start_tls | ( | struct ast_iostream ** | stream, |
SSL_CTX * | ctx, | ||
int | client | ||
) |
Begin TLS on an iostream.
stream | A pointer to an iostream pointer |
ctx | A pointer to an SSL_CTX which will be passed to SSL_new() |
client | Non-zero to indicate that we are the client, zero to indicate that we are the server. |
0 | success |
-1 | failure |
errno
may be set providing additional information on why the failure occurred. Definition at line 620 of file iostream.c.
References ast_log, errno, ast_iostream::fd, LOG_ERROR, ast_iostream::ssl, and ssl_error_to_string().
Referenced by handle_tcptls_connection().
int ast_iostream_wait_for_input | ( | struct ast_iostream * | stream, |
int | timeout | ||
) |
Wait for input on the iostream's file descriptor.
stream | A pointer to an iostream |
timeout | the number of milliseconds to wait |
-1 | if error occurred |
0 | if the timeout expired |
1 | if the stream is ready for reading |
Definition at line 89 of file iostream.c.
References ast_wait_for_input(), ast_iostream::fd, and ast_iostream::ssl.
Referenced by ast_websocket_wait_for_input(), and ws_safe_read().
ssize_t ast_iostream_write | ( | struct ast_iostream * | stream, |
const void * | buffer, | ||
size_t | count | ||
) |
Write data to an iostream.
stream | A pointer to an iostream |
buffer | Pointer to a buffer from which to read bytes. |
count | The number of bytes from buffer to write. |
-1
and may set errno
to indicate the error. Definition at line 374 of file iostream.c.
References ast_debug, ast_remaining_ms(), ast_tvnow(), ast_wait_for_input(), ast_wait_for_output(), errno, ast_iostream::fd, ast_iostream::ssl, ssl_error_to_string(), ast_iostream::start, and ast_iostream::timeout.
Referenced by _sip_tcp_helper_thread(), ast_http_send(), ast_iostream_printf(), ast_websocket_close(), ast_websocket_write(), process_output(), send_eivr_event(), and send_string().
|
static |
Definition at line 592 of file iostream.c.
References ast_assert, and ast_iostream::fd.
Referenced by ast_iostream_from_fd().
|
static |
Definition at line 155 of file iostream.c.
References ast_debug, ast_remaining_ms(), ast_tvnow(), ast_wait_for_input(), ast_wait_for_output(), errno, ast_iostream::exclusive_input, ast_iostream::fd, ast_iostream::ssl, ssl_error_to_string(), ast_iostream::start, ast_iostream::timeout, and ast_iostream::timeout_reset.
Referenced by ast_iostream_gets(), and ast_iostream_read().
|
static |
Definition at line 55 of file iostream.c.
References ast_threadstorage_get(), buf, ERR2STR_BUFSIZE, err2str_threadbuf, and errno.
Referenced by ast_iostream_close(), ast_iostream_start_tls(), ast_iostream_write(), and iostream_read().
|
static |
Definition at line 52 of file iostream.c.
Referenced by ssl_error_to_string().