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"
30 #define TIMER_T1_MIN 100
31 #define DEFAULT_TIMER_T1 500
32 #define DEFAULT_TIMER_B 32000
34 struct system_config {
35 SORCERY_OBJECT(details);
36 /*! Transaction Timer T1 value */
38 /*! Transaction Timer B value */
40 /*! Should we use short forms for headers? */
41 unsigned int compactheaders;
43 /*! Initial number of threads in the threadpool */
45 /*! The amount by which the number of threads is incremented when necessary */
47 /*! Thread idle timeout in seconds */
49 /*! Maxumum number of threads in the threadpool */
54 static struct ast_threadpool_options sip_threadpool_options = {
55 .version = AST_THREADPOOL_OPTIONS_VERSION,
58 void sip_get_threadpool_options(struct ast_threadpool_options *threadpool_options)
60 *threadpool_options = sip_threadpool_options;
63 static struct ast_sorcery *system_sorcery;
65 static void *system_alloc(const char *name)
67 struct system_config *system = ast_sorcery_generic_alloc(sizeof(*system), NULL);
76 static int system_apply(const struct ast_sorcery *system_sorcery, void *obj)
78 struct system_config *system = obj;
81 if (system->timert1 < TIMER_T1_MIN) {
82 ast_log(LOG_WARNING, "Timer T1 setting is too low. Setting to %d\n", TIMER_T1_MIN);
83 system->timert1 = TIMER_T1_MIN;
86 min_timerb = 64 * system->timert1;
88 if (system->timerb < min_timerb) {
89 ast_log(LOG_WARNING, "Timer B setting is too low. Setting to %d\n", min_timerb);
90 system->timerb = min_timerb;
93 pjsip_cfg()->tsx.t1 = system->timert1;
94 pjsip_cfg()->tsx.td = system->timerb;
96 if (system->compactheaders) {
97 extern pj_bool_t pjsip_use_compact_form;
98 pjsip_use_compact_form = PJ_TRUE;
101 sip_threadpool_options.initial_size = system->threadpool.initial_size;
102 sip_threadpool_options.auto_increment = system->threadpool.auto_increment;
103 sip_threadpool_options.idle_timeout = system->threadpool.idle_timeout;
104 sip_threadpool_options.max_size = system->threadpool.max_size;
109 int ast_sip_initialize_system(void)
111 RAII_VAR(struct ao2_container *, system_configs, NULL, ao2_cleanup);
112 RAII_VAR(struct system_config *, system, NULL, ao2_cleanup);
114 system_sorcery = ast_sorcery_open();
115 if (!system_sorcery) {
116 ast_log(LOG_ERROR, "Failed to open SIP system sorcery\n");
120 ast_sorcery_apply_config(system_sorcery, "res_pjsip");
122 ast_sorcery_apply_default(system_sorcery, "system", "config", "pjsip.conf,criteria=type=system");
124 if (ast_sorcery_object_register_no_reload(system_sorcery, "system", system_alloc, NULL, system_apply)) {
125 ast_log(LOG_ERROR, "Failed to register with sorcery (is res_sorcery_config loaded?)\n");
126 ast_sorcery_unref(system_sorcery);
127 system_sorcery = NULL;
131 ast_sorcery_object_field_register(system_sorcery, "system", "type", "", OPT_NOOP_T, 0, 0);
132 ast_sorcery_object_field_register(system_sorcery, "system", "timer_t1", __stringify(DEFAULT_TIMER_T1),
133 OPT_UINT_T, 0, FLDSET(struct system_config, timert1));
134 ast_sorcery_object_field_register(system_sorcery, "system", "timer_b", __stringify(DEFAULT_TIMER_B),
135 OPT_UINT_T, 0, FLDSET(struct system_config, timerb));
136 ast_sorcery_object_field_register(system_sorcery, "system", "compact_headers", "no",
137 OPT_BOOL_T, 1, FLDSET(struct system_config, compactheaders));
138 ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_initial_size", "0",
139 OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.initial_size));
140 ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_auto_increment", "5",
141 OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.auto_increment));
142 ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_idle_timeout", "60",
143 OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.idle_timeout));
144 ast_sorcery_object_field_register(system_sorcery, "system", "threadpool_max_size", "0",
145 OPT_UINT_T, 0, FLDSET(struct system_config, threadpool.max_size));
147 ast_sorcery_load(system_sorcery);
149 system_configs = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
150 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
152 if (ao2_container_count(system_configs)) {
156 /* No config present, allocate one and apply defaults */
157 system = ast_sorcery_alloc(system_sorcery, "system", NULL);
159 ast_log(LOG_ERROR, "Unable to allocate default system config.\n");
160 ast_sorcery_unref(system_sorcery);
164 if (system_apply(system_sorcery, system)) {
165 ast_log(LOG_ERROR, "Failed to apply default system config.\n");
166 ast_sorcery_unref(system_sorcery);
173 void ast_sip_destroy_system(void)
175 ast_sorcery_unref(system_sorcery);
178 static int system_create_resolver_and_set_nameservers(void *data)
180 struct ao2_container *discovered_nameservers;
181 struct ao2_iterator it_nameservers;
184 pj_dns_resolver *resolver;
185 pj_str_t nameservers[PJ_DNS_RESOLVER_MAX_NS];
186 unsigned int count = 0;
188 discovered_nameservers = ast_dns_get_nameservers();
189 if (!discovered_nameservers) {
190 ast_log(LOG_ERROR, "Could not retrieve local system nameservers, resorting to system resolution\n");
194 if (!ao2_container_count(discovered_nameservers)) {
195 ast_log(LOG_ERROR, "There are no local system nameservers configured, resorting to system resolution\n");
196 ao2_ref(discovered_nameservers, -1);
200 if (!(resolver = pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint()))) {
201 status = pjsip_endpt_create_resolver(ast_sip_get_pjsip_endpoint(), &resolver);
202 if (status != PJ_SUCCESS) {
203 ast_log(LOG_ERROR, "Could not create DNS resolver(%d), resorting to system resolution\n", status);
208 it_nameservers = ao2_iterator_init(discovered_nameservers, 0);
209 while ((nameserver = ao2_iterator_next(&it_nameservers))) {
210 pj_strset2(&nameservers[count++], nameserver);
211 ao2_ref(nameserver, -1);
213 if (count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
217 ao2_iterator_destroy(&it_nameservers);
219 status = pj_dns_resolver_set_ns(resolver, count, nameservers, NULL);
221 /* Since we no longer need the nameservers we can drop the list of them */
222 ao2_ref(discovered_nameservers, -1);
224 if (status != PJ_SUCCESS) {
225 ast_log(LOG_ERROR, "Could not set nameservers on DNS resolver in PJSIP(%d), resorting to system resolution\n",
230 if (!pjsip_endpt_get_resolver(ast_sip_get_pjsip_endpoint())) {
231 status = pjsip_endpt_set_resolver(ast_sip_get_pjsip_endpoint(), resolver);
232 if (status != PJ_SUCCESS) {
233 ast_log(LOG_ERROR, "Could not set DNS resolver in PJSIP(%d), resorting to system resolution\n", status);
241 void ast_sip_initialize_dns(void)
243 ast_sip_push_task_synchronous(NULL, system_create_resolver_and_set_nameservers, NULL);