Asterisk - The Open Source Telephony Project  18.5.0
TLS Implementation Overview

The following code implements a generic mechanism for starting services on a TCP or TLS socket. The service is configured in the struct session_args, and then started by calling server_start(desc) on the descriptor. server_start() first verifies if an instance of the service is active, and in case shuts it down. Then, if the service must be started, creates a socket and a thread in charge of doing the accept().

The body of the thread is desc->accept_fn(desc), which the user can define freely. We supply a sample implementation, server_root(), structured as an infinite loop. At the beginning of each iteration it runs periodic_fn() if defined (e.g. to perform some cleanup etc.) then issues a poll() or equivalent with a timeout of 'poll_timeout' milliseconds, and if the following accept() is successful it creates a thread in charge of running the session, whose body is desc->worker_fn(). The argument of worker_fn() is a struct ast_tcptls_session_instance, which contains the address of the other party, a pointer to desc, the file descriptors (fd) on which we can do a select/poll (but NOT I/O), and a FILE *on which we can do I/O. We have both because we want to support plain and SSL sockets, and going through a FILE * lets us provide the encryption/decryption on the stream without using an auxiliary thread.