2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
22 * \brief Generic support for tcp/tls servers in Asterisk.
23 * \note In order to have TLS/SSL support, we need the openssl libraries.
24 * Still we can decide whether or not to use them by commenting
25 * in or out the DO_SSL macro.
27 * TLS/SSL support is basically implemented by reading from a config file
28 * (currently http.conf and sip.conf) the names of the certificate and cipher to use,
29 * and then run ssl_setup() to create an appropriate SSL_CTX (ssl_ctx)
30 * If we support multiple domains, presumably we need to read multiple
33 * When we are requested to open a TLS socket, we run make_file_from_fd()
34 * on the socket, to do the necessary setup. At the moment the context's name
35 * is hardwired in the function, but we can certainly make it into an extra
36 * parameter to the function.
38 * We declare most of ssl support variables unconditionally,
39 * because their number is small and this simplifies the code.
41 * \note The ssl-support variables (ssl_ctx, do_ssl, certfile, cipher)
42 * and their setup should be moved to a more central place, e.g. asterisk.conf
43 * and the source files that processes it. Similarly, ssl_setup() should
44 * be run earlier in the startup process so modules have it available.
49 #ifndef _ASTERISK_SERVER_H
50 #define _ASTERISK_SERVER_H
52 #include "asterisk/utils.h"
53 #include "asterisk/astobj2.h"
55 #if defined(HAVE_OPENSSL) && (defined(HAVE_FUNOPEN) || defined(HAVE_FOPENCOOKIE))
56 #define DO_SSL /* comment in/out if you want to support ssl */
60 #include <openssl/ssl.h>
61 #include <openssl/err.h>
63 /* declare dummy types so we can define a pointer to them */
64 typedef struct {} SSL;
65 typedef struct {} SSL_CTX;
69 #define AST_CERTFILE "asterisk.pem"
72 /*! Verify certificate when acting as server */
73 AST_SSL_VERIFY_CLIENT = (1 << 0),
74 /*! Don't verify certificate when connecting to a server */
75 AST_SSL_DONT_VERIFY_SERVER = (1 << 1),
76 /*! Don't compare "Common Name" against IP or hostname */
77 AST_SSL_IGNORE_COMMON_NAME = (1 << 2)
80 struct ast_tls_config {
86 struct ast_flags flags;
91 * The following code implements a generic mechanism for starting
92 * services on a TCP or TLS socket.
93 * The service is configured in the struct server_args, and
94 * then started by calling server_start(desc) on the descriptor.
95 * server_start() first verifies if an instance of the service is active,
96 * and in case shuts it down. Then, if the service must be started, creates
97 * a socket and a thread in charge of doing the accept().
99 * The body of the thread is desc->accept_fn(desc), which the user can define
100 * freely. We supply a sample implementation, server_root(), structured as an
101 * infinite loop. At the beginning of each iteration it runs periodic_fn()
102 * if defined (e.g. to perform some cleanup etc.) then issues a poll()
103 * or equivalent with a timeout of 'poll_timeout' milliseconds, and if the
104 * following accept() is successful it creates a thread in charge of
105 * running the session, whose body is desc->worker_fn(). The argument of
106 * worker_fn() is a struct ast_tcptls_session_instance, which contains the address
107 * of the other party, a pointer to desc, the file descriptors (fd) on which
108 * we can do a select/poll (but NOT IO/, and a FILE *on which we can do I/O.
109 * We have both because we want to support plain and SSL sockets, and
110 * going through a FILE *lets us provide the encryption/decryption
111 * on the stream without using an auxiliary thread.
113 * NOTE: in order to let other parts of asterisk use these services,
114 * we need to do the following:
115 * + move struct ast_tcptls_session_instance and struct server_args to
116 * a common header file, together with prototypes for
117 * server_start() and server_root().
121 * describes a server instance
123 struct ast_tcptls_session_instance {
124 FILE *f; /* fopen/funopen result */
125 int fd; /* the socket returned by accept() */
126 SSL *ssl; /* ssl state */
127 /* iint (*ssl_setup)(SSL *); */
129 struct sockaddr_in requestor;
130 struct server_args *parent;
135 * arguments for the accepting thread
138 struct sockaddr_in sin;
139 struct sockaddr_in oldsin;
140 char hostname[MAXHOSTNAMELEN]; /*!< only necessary for SSL clients so we can compare to common name */
141 struct ast_tls_config *tls_cfg; /*!< points to the SSL configuration if any */
145 void *(*accept_fn)(void *); /*!< the function in charge of doing the accept */
146 void (*periodic_fn)(void *);/*!< something we may want to run before after select on the accept socket */
147 void *(*worker_fn)(void *); /*!< the function in charge of doing the actual work */
151 #if defined(HAVE_FUNOPEN)
155 #define HOOK_T ssize_t
159 struct ast_tcptls_session_instance *ast_tcptls_client_start(struct server_args *desc);
161 void *ast_tcptls_server_root(void *);
162 void ast_tcptls_server_start(struct server_args *desc);
163 void ast_tcptls_server_stop(struct server_args *desc);
164 int ast_ssl_setup(struct ast_tls_config *cfg);
166 void *ast_make_file_from_fd(void *data);
168 HOOK_T ast_tcptls_server_read(struct ast_tcptls_session_instance *ser, void *buf, size_t count);
169 HOOK_T ast_tcptls_server_write(struct ast_tcptls_session_instance *ser, void *buf, size_t count);
171 #endif /* _ASTERISK_SERVER_H */