pjsip_scheduler.c: Fix some corner cases.
[asterisk/asterisk.git] / res / res_pjsip / config_system.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
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.
13  *
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.
17  */
18
19 #include "asterisk.h"
20
21 #include <pjsip.h>
22 #include <pjlib.h>
23
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"
30
31 #define TIMER_T1_MIN 100
32 #define DEFAULT_TIMER_T1 500
33 #define DEFAULT_TIMER_B 32000
34
35 struct system_config {
36         SORCERY_OBJECT(details);
37         /*! Transaction Timer T1 value */
38         unsigned int timert1;
39         /*! Transaction Timer B value */
40         unsigned int timerb;
41         /*! Should we use short forms for headers? */
42         unsigned int compactheaders;
43         struct {
44                 /*! Initial number of threads in the threadpool */
45                 int initial_size;
46                 /*! The amount by which the number of threads is incremented when necessary */
47                 int auto_increment;
48                 /*! Thread idle timeout in seconds */
49                 int idle_timeout;
50                 /*! Maxumum number of threads in the threadpool */
51                 int max_size;
52         } threadpool;
53         /*! Nonzero to disable switching from UDP to TCP transport */
54         unsigned int disable_tcp_switch;
55 };
56
57 static struct ast_threadpool_options sip_threadpool_options = {
58         .version = AST_THREADPOOL_OPTIONS_VERSION,
59 };
60
61 void sip_get_threadpool_options(struct ast_threadpool_options *threadpool_options)
62 {
63         *threadpool_options = sip_threadpool_options;
64 }
65
66 static struct ast_sorcery *system_sorcery;
67
68 static void *system_alloc(const char *name)
69 {
70         struct system_config *system = ast_sorcery_generic_alloc(sizeof(*system), NULL);
71
72         if (!system) {
73                 return NULL;
74         }
75
76         return system;
77 }
78
79 static int system_apply(const struct ast_sorcery *system_sorcery, void *obj)
80 {
81         struct system_config *system = obj;
82         int min_timerb;
83
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;
87         }
88
89         min_timerb = 64 * system->timert1;
90
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;
94         }
95
96         pjsip_cfg()->tsx.t1 = system->timert1;
97         pjsip_cfg()->tsx.td = system->timerb;
98
99         if (system->compactheaders) {
100                 extern pj_bool_t pjsip_use_compact_form;
101
102                 pjsip_use_compact_form = PJ_TRUE;
103         }
104
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;
109
110         pjsip_cfg()->endpt.disable_tcp_switch =
111                 system->disable_tcp_switch ? PJ_TRUE : PJ_FALSE;
112
113         return 0;
114 }
115
116 static struct system_config *get_system_cfg(void)
117 {
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);
122
123         if (!systems) {
124                 return NULL;
125         }
126
127         cfg = ao2_find(systems, NULL, 0);
128         ao2_ref(systems, -1);
129         return cfg;
130 }
131
132 int sip_cli_print_system(struct ast_sip_cli_context *context)
133 {
134         struct system_config *cfg = get_system_cfg();
135
136         if (!cfg) {
137                 cfg = ast_sorcery_alloc(system_sorcery, "system", NULL);
138                 if (!cfg) {
139                         return -1;
140                 }
141         }
142
143         ast_str_append(&context->output_buffer, 0, "\nSystem Settings:\n\n");
144         ast_sip_cli_print_sorcery_objectset(cfg, context, 0);
145
146         ao2_ref(cfg, -1);
147         return 0;
148 }
149
150 int ast_sip_initialize_system(void)
151 {
152         RAII_VAR(struct ao2_container *, system_configs, NULL, ao2_cleanup);
153         RAII_VAR(struct system_config *, system, NULL, ao2_cleanup);
154
155         system_sorcery = ast_sorcery_open();
156         if (!system_sorcery) {
157                 ast_log(LOG_ERROR, "Failed to open SIP system sorcery\n");
158                 return -1;
159         }
160
161         ast_sorcery_apply_default(system_sorcery, "system", "config", "pjsip.conf,criteria=type=system");
162
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;
167                 return -1;
168         }
169
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", "50",
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));
187
188         ast_sorcery_load(system_sorcery);
189
190         system_configs = ast_sorcery_retrieve_by_fields(system_sorcery, "system",
191                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
192
193         if (ao2_container_count(system_configs)) {
194                 return 0;
195         }
196
197         /* No config present, allocate one and apply defaults */
198         system = ast_sorcery_alloc(system_sorcery, "system", NULL);
199         if (!system) {
200                 ast_log(LOG_ERROR, "Unable to allocate default system config.\n");
201                 ast_sorcery_unref(system_sorcery);
202                 return -1;
203         }
204
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);
208                 return -1;
209         }
210
211         return 0;
212 }
213
214 void ast_sip_destroy_system(void)
215 {
216         ast_sorcery_unref(system_sorcery);
217 }
218
219 static int system_create_resolver_and_set_nameservers(void *data)
220 {
221         struct ao2_container *discovered_nameservers;
222         struct ao2_iterator it_nameservers;
223         char *nameserver;
224         pj_status_t status;
225         pj_dns_resolver *resolver;
226         pj_str_t nameservers[PJ_DNS_RESOLVER_MAX_NS];
227         unsigned int count = 0;
228
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");
232                 return 0;
233         }
234
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);
238                 return -1;
239         }
240
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);
246                         return 0;
247                 }
248         }
249
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);
254
255                 if (count == (PJ_DNS_RESOLVER_MAX_NS - 1)) {
256                         break;
257                 }
258         }
259         ao2_iterator_destroy(&it_nameservers);
260
261         status = pj_dns_resolver_set_ns(resolver, count, nameservers, NULL);
262
263         /* Since we no longer need the nameservers we can drop the list of them */
264         ao2_ref(discovered_nameservers, -1);
265
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",
268                         status);
269                 return 0;
270         }
271
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);
276                         return 0;
277                 }
278         }
279
280         return 0;
281 }
282
283 void ast_sip_initialize_dns(void)
284 {
285         ast_sip_push_task_synchronous(NULL, system_create_resolver_and_set_nameservers, NULL);
286 }