2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@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.
24 #include "asterisk/res_pjsip.h"
25 #include "asterisk/sorcery.h"
26 #include "include/res_pjsip_private.h"
27 #include "asterisk/threadpool.h"
28 #include "asterisk/dns.h"
29 #include "asterisk/res_pjsip_cli.h"
31 #define TIMER_T1_MIN 100
32 #define DEFAULT_TIMER_T1 500
33 #define DEFAULT_TIMER_B 32000
35 struct system_config {
36 SORCERY_OBJECT(details);
37 /*! Transaction Timer T1 value */
39 /*! Transaction Timer B value */
41 /*! Should we use short forms for headers? */
42 unsigned int compactheaders;
44 /*! Initial number of threads in the threadpool */
46 /*! The amount by which the number of threads is incremented when necessary */
48 /*! Thread idle timeout in seconds */
50 /*! Maxumum number of threads in the threadpool */
53 /*! Nonzero to disable switching from UDP to TCP transport */
54 unsigned int disable_tcp_switch;
57 static struct ast_threadpool_options sip_threadpool_options = {
58 .version = AST_THREADPOOL_OPTIONS_VERSION,
61 void sip_get_threadpool_options(struct ast_threadpool_options *threadpool_options)
63 *threadpool_options = sip_threadpool_options;
66 static struct ast_sorcery *system_sorcery;
68 static void *system_alloc(const char *name)
70 struct system_config *system = ast_sorcery_generic_alloc(sizeof(*system), NULL);
79 static int system_apply(const struct ast_sorcery *system_sorcery, void *obj)
81 struct system_config *system = obj;
84 if (system->timert1 < TIMER_T1_MIN) {
85 ast_log(LOG_WARNING, "Timer T1 setting is too low. Setting to %d\n", TIMER_T1_MIN);
86 system->timert1 = TIMER_T1_MIN;
89 min_timerb = 64 * system->timert1;
91 if (system->timerb < min_timerb) {
92 ast_log(LOG_WARNING, "Timer B setting is too low. Setting to %d\n", min_timerb);
93 system->timerb = min_timerb;
96 pjsip_cfg()->tsx.t1 = system->timert1;
97 pjsip_cfg()->tsx.td = system->timerb;
99 if (system->compactheaders) {
100 extern pj_bool_t pjsip_use_compact_form;
102 pjsip_use_compact_form = PJ_TRUE;
105 sip_threadpool_options.initial_size = system->threadpool.initial_size;
106 sip_threadpool_options.auto_increment = system->threadpool.auto_increment;
107 sip_threadpool_options.idle_timeout = system->threadpool.idle_timeout;
108 sip_threadpool_options.max_size = system->threadpool.max_size;
110 pjsip_cfg()->endpt.disable_tcp_switch =
111 system->disable_tcp_switch ? PJ_TRUE : PJ_FALSE;
116 static struct system_config *get_system_cfg(void)
118 struct system_config *cfg;
119 struct ao2_container *systems;
120 systems = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
121 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
127 cfg = ao2_find(systems, NULL, 0);
128 ao2_ref(systems, -1);
132 int sip_cli_print_system(struct ast_sip_cli_context *context)
134 struct system_config *cfg = get_system_cfg();
137 cfg = ast_sorcery_alloc(system_sorcery, "system", NULL);
143 ast_str_append(&context->output_buffer, 0, "\nSystem Settings:\n\n");
144 ast_sip_cli_print_sorcery_objectset(cfg, context, 0);
150 int ast_sip_initialize_system(void)
152 RAII_VAR(struct ao2_container *, system_configs, NULL, ao2_cleanup);
153 RAII_VAR(struct system_config *, system, NULL, ao2_cleanup);
155 system_sorcery = ast_sorcery_open();
156 if (!system_sorcery) {
157 ast_log(LOG_ERROR, "Failed to open SIP system sorcery\n");
161 ast_sorcery_apply_default(system_sorcery, "system", "config", "pjsip.conf,criteria=type=system");
163 if (ast_sorcery_object_register_no_reload(system_sorcery, "system", system_alloc, NULL, system_apply)) {
164 ast_log(LOG_ERROR, "Failed to register with sorcery (is res_sorcery_config loaded?)\n");
165 ast_sorcery_unref(system_sorcery);
166 system_sorcery = NULL;
170 ast_sorcery_object_field_register(system_sorcery, "system", "type", "", OPT_NOOP_T, 0, 0);
171 ast_sorcery_object_field_register(system_sorcery, "system", "timer_t1", __stringify(DEFAULT_TIMER_T1),
172 OPT_UINT_T, 0, FLDSET(struct system_config, timert1));
173 ast_sorcery_object_field_register(system_sorcery, "system", "timer_b", __stringify(DEFAULT_TIMER_B),
174 OPT_UINT_T, 0, FLDSET(struct system_config, timerb));
175 ast_sorcery_object_field_register(system_sorcery, "system", "compact_headers", "no",
176 OPT_BOOL_T, 1, FLDSET(struct system_config, compactheaders));
177 ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_initial_size", "0",
178 OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.initial_size));
179 ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_auto_increment", "5",
180 OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.auto_increment));
181 ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_idle_timeout", "60",
182 OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.idle_timeout));
183 ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_max_size", "0",
184 OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.max_size));
185 ast_sorcery_object_field_register(system_sorcery, "system", "disable_tcp_switch", "yes",
186 OPT_BOOL_T, 1, FLDSET(struct system_config, disable_tcp_switch));
188 ast_sorcery_load(system_sorcery);
190 system_configs = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
191 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
193 if (ao2_container_count(system_configs)) {
197 /* No config present, allocate one and apply defaults */
198 system = ast_sorcery_alloc(system_sorcery, "system", NULL);
200 ast_log(LOG_ERROR, "Unable to allocate default system config.\n");
201 ast_sorcery_unref(system_sorcery);
205 if (system_apply(system_sorcery, system)) {
206 ast_log(LOG_ERROR, "Failed to apply default system config.\n");
207 ast_sorcery_unref(system_sorcery);
214 void ast_sip_destroy_system(void)
216 ast_sorcery_unref(system_sorcery);
219 static int system_create_resolver_and_set_nameservers(void *data)
221 struct ao2_container *discovered_nameservers;
222 struct ao2_iterator it_nameservers;
225 pj_dns_resolver *resolver;
226 pj_str_t nameservers[PJ_DNS_RESOLVER_MAX_NS];
227 unsigned int count = 0;
229 discovered_nameservers = ast_dns_get_nameservers();
230 if (!discovered_nameservers) {
231 ast_log(LOG_ERROR, "Could not retrieve local system nameservers, resorting to system resolution\n");
235 if (!ao2_container_count(discovered_nameservers)) {
236 ast_log(LOG_ERROR, "There are no local system nameservers configured, resorting to system resolution\n");
237 ao2_ref(discovered_nameservers, -1);
241 if (!(resolver = pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint()))) {
242 status = pjsip_endpt_create_resolver(ast_sip_get_pjsip_endpoint(), &resolver);
243 if (status != PJ_SUCCESS) {
244 ast_log(LOG_ERROR, "Could not create DNS resolver(%d), resorting to system resolution\n", status);
245 ao2_ref(discovered_nameservers, -1);
250 it_nameservers = ao2_iterator_init(discovered_nameservers, 0);
251 while ((nameserver = ao2_iterator_next(&it_nameservers))) {
252 pj_strset2(&nameservers[count++], nameserver);
253 ao2_ref(nameserver, -1);
255 if (count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
259 ao2_iterator_destroy(&it_nameservers);
261 status = pj_dns_resolver_set_ns(resolver, count, nameservers, NULL);
263 /* Since we no longer need the nameservers we can drop the list of them */
264 ao2_ref(discovered_nameservers, -1);
266 if (status != PJ_SUCCESS) {
267 ast_log(LOG_ERROR, "Could not set nameservers on DNS resolver in PJSIP(%d), resorting to system resolution\n",
272 if (!pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint())) {
273 status = pjsip_endpt_set_resolver(ast_sip_get_pjsip_endpoint(), resolver);
274 if (status != PJ_SUCCESS) {
275 ast_log(LOG_ERROR, "Could not set DNS resolver in PJSIP(%d), resorting to system resolution\n", status);
283 void ast_sip_initialize_dns(void)
285 ast_sip_push_task_synchronous(NULL, system_create_resolver_and_set_nameservers, NULL);