3d88ffc2a9ae8acc9a96a4ff4335b93805e80d1d
[asterisk/asterisk.git] / res / res_pjsip / config_global.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 "include/res_pjsip_private.h"
26 #include "asterisk/sorcery.h"
27 #include "asterisk/ast_version.h"
28 #include "asterisk/res_pjsip_cli.h"
29
30 #define DEFAULT_MAX_FORWARDS 70
31 #define DEFAULT_KEEPALIVE_INTERVAL 0
32 #define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
33 #define DEFAULT_OUTBOUND_ENDPOINT "default_outbound_endpoint"
34 #define DEFAULT_DEBUG "no"
35 #define DEFAULT_ENDPOINT_IDENTIFIER_ORDER "ip,username,anonymous"
36 #define DEFAULT_MAX_INITIAL_QUALIFY_TIME 0
37 #define DEFAULT_FROM_USER "asterisk"
38 #define DEFAULT_REGCONTEXT ""
39
40 static char default_useragent[256];
41
42 struct global_config {
43         SORCERY_OBJECT(details);
44         AST_DECLARE_STRING_FIELDS(
45                 AST_STRING_FIELD(useragent);
46                 AST_STRING_FIELD(regcontext);
47                 AST_STRING_FIELD(default_outbound_endpoint);
48                 /*! Debug logging yes|no|host */
49                 AST_STRING_FIELD(debug);
50                 /*! Order by which endpoint identifiers are checked (comma separated list) */
51                 AST_STRING_FIELD(endpoint_identifier_order);
52                 /*! User name to place in From header if there is no better option */
53                 AST_STRING_FIELD(default_from_user);
54         );
55         /* Value to put in Max-Forwards header */
56         unsigned int max_forwards;
57         /* The interval at which to send keep alive messages to active connection-oriented transports */
58         unsigned int keep_alive_interval;
59         /* The maximum time for all contacts to be qualified at startup */
60         unsigned int max_initial_qualify_time;
61 };
62
63 static void global_destructor(void *obj)
64 {
65         struct global_config *cfg = obj;
66
67         ast_string_field_free_memory(cfg);
68 }
69
70 static void *global_alloc(const char *name)
71 {
72         struct global_config *cfg;
73
74         cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
75         if (!cfg || ast_string_field_init(cfg, 100)) {
76                 ao2_cleanup(cfg);
77                 return NULL;
78         }
79
80         return cfg;
81 }
82
83 static int global_apply(const struct ast_sorcery *sorcery, void *obj)
84 {
85         struct global_config *cfg = obj;
86         char max_forwards[10];
87
88         snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
89
90         ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
91         ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
92         ast_sip_add_global_response_header("Server", cfg->useragent, 1);
93         return 0;
94 }
95
96 static struct global_config *get_global_cfg(void)
97 {
98         struct global_config *cfg;
99         struct ao2_container *globals;
100
101         globals = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "global",
102                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
103         if (!globals) {
104                 return NULL;
105         }
106
107         cfg = ao2_find(globals, NULL, 0);
108         ao2_ref(globals, -1);
109         return cfg;
110 }
111
112 char *ast_sip_global_default_outbound_endpoint(void)
113 {
114         char *str;
115         struct global_config *cfg;
116
117         cfg = get_global_cfg();
118         if (!cfg) {
119                 return ast_strdup(DEFAULT_OUTBOUND_ENDPOINT);
120         }
121
122         str = ast_strdup(cfg->default_outbound_endpoint);
123         ao2_ref(cfg, -1);
124         return str;
125 }
126
127 char *ast_sip_get_debug(void)
128 {
129         char *res;
130         struct global_config *cfg;
131
132         cfg = get_global_cfg();
133         if (!cfg) {
134                 return ast_strdup(DEFAULT_DEBUG);
135         }
136
137         res = ast_strdup(cfg->debug);
138         ao2_ref(cfg, -1);
139         return res;
140 }
141
142 char *ast_sip_get_regcontext(void)
143 {
144         char *res;
145         struct global_config *cfg;
146
147         cfg = get_global_cfg();
148         if (!cfg) {
149                 return ast_strdup(DEFAULT_REGCONTEXT);
150         }
151
152         res = ast_strdup(cfg->regcontext);
153         ao2_ref(cfg, -1);
154
155         return res;
156 }
157
158
159 char *ast_sip_get_endpoint_identifier_order(void)
160 {
161         char *res;
162         struct global_config *cfg;
163
164         cfg = get_global_cfg();
165         if (!cfg) {
166                 return ast_strdup(DEFAULT_ENDPOINT_IDENTIFIER_ORDER);
167         }
168
169         res = ast_strdup(cfg->endpoint_identifier_order);
170         ao2_ref(cfg, -1);
171         return res;
172 }
173
174 unsigned int ast_sip_get_keep_alive_interval(void)
175 {
176         unsigned int interval;
177         struct global_config *cfg;
178
179         cfg = get_global_cfg();
180         if (!cfg) {
181                 return DEFAULT_KEEPALIVE_INTERVAL;
182         }
183
184         interval = cfg->keep_alive_interval;
185         ao2_ref(cfg, -1);
186         return interval;
187 }
188
189 unsigned int ast_sip_get_max_initial_qualify_time(void)
190 {
191         unsigned int time;
192         struct global_config *cfg;
193
194         cfg = get_global_cfg();
195         if (!cfg) {
196                 return DEFAULT_MAX_INITIAL_QUALIFY_TIME;
197         }
198
199         time = cfg->max_initial_qualify_time;
200         ao2_ref(cfg, -1);
201         return time;
202 }
203
204 void ast_sip_get_default_from_user(char *from_user, size_t size)
205 {
206         struct global_config *cfg;
207
208         cfg = get_global_cfg();
209         if (!cfg) {
210                 ast_copy_string(from_user, DEFAULT_FROM_USER, size);
211         } else {
212                 ast_copy_string(from_user, cfg->default_from_user, size);
213                 ao2_ref(cfg, -1);
214         }
215 }
216
217 /*!
218  * \internal
219  * \brief Observer to set default global object if none exist.
220  *
221  * \param name Module name owning the sorcery instance.
222  * \param sorcery Instance being observed.
223  * \param object_type Name of object being observed.
224  * \param reloaded Non-zero if the object is being reloaded.
225  *
226  * \return Nothing
227  */
228 static void global_loaded_observer(const char *name, const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
229 {
230         struct ao2_container *globals;
231         struct global_config *cfg;
232
233         if (strcmp(object_type, "global")) {
234                 /* Not interested */
235                 return;
236         }
237
238         globals = ast_sorcery_retrieve_by_fields(sorcery, "global",
239                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
240         if (globals) {
241                 int count;
242
243                 count = ao2_container_count(globals);
244                 ao2_ref(globals, -1);
245
246                 if (1 < count) {
247                         ast_log(LOG_ERROR,
248                                 "At most one pjsip.conf type=global object can be defined.  You have %d defined.\n",
249                                 count);
250                         return;
251                 }
252                 if (count) {
253                         return;
254                 }
255         }
256
257         ast_debug(1, "No pjsip.conf type=global object exists so applying defaults.\n");
258         cfg = ast_sorcery_alloc(sorcery, "global", NULL);
259         if (!cfg) {
260                 return;
261         }
262         global_apply(sorcery, cfg);
263         ao2_ref(cfg, -1);
264 }
265
266 static const struct ast_sorcery_instance_observer observer_callbacks_global = {
267         .object_type_loaded = global_loaded_observer,
268 };
269
270 int sip_cli_print_global(struct ast_sip_cli_context *context)
271 {
272         struct global_config *cfg = get_global_cfg();
273
274         if (!cfg) {
275                 cfg = ast_sorcery_alloc(ast_sip_get_sorcery(), "global", NULL);
276                 if (!cfg) {
277                         return -1;
278                 }
279         }
280
281         ast_str_append(&context->output_buffer, 0, "\nGlobal Settings:\n\n");
282         ast_sip_cli_print_sorcery_objectset(cfg, context, 0);
283
284         ao2_ref(cfg, -1);
285         return 0;
286 }
287
288 int ast_sip_destroy_sorcery_global(void)
289 {
290         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
291
292         ast_sorcery_instance_observer_remove(sorcery, &observer_callbacks_global);
293
294         return 0;
295 }
296
297 int ast_sip_initialize_sorcery_global(void)
298 {
299         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
300
301         snprintf(default_useragent, sizeof(default_useragent), "%s %s",
302                 DEFAULT_USERAGENT_PREFIX, ast_get_version());
303
304         ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
305
306         if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
307                 return -1;
308         }
309
310         ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
311         ast_sorcery_object_field_register(sorcery, "global", "max_forwards",
312                 __stringify(DEFAULT_MAX_FORWARDS),
313                 OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
314         ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
315                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
316         ast_sorcery_object_field_register(sorcery, "global", "default_outbound_endpoint",
317                 DEFAULT_OUTBOUND_ENDPOINT,
318                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
319         ast_sorcery_object_field_register(sorcery, "global", "debug", DEFAULT_DEBUG,
320                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, debug));
321         ast_sorcery_object_field_register(sorcery, "global", "endpoint_identifier_order",
322                 DEFAULT_ENDPOINT_IDENTIFIER_ORDER,
323                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, endpoint_identifier_order));
324         ast_sorcery_object_field_register(sorcery, "global", "keep_alive_interval",
325                 __stringify(DEFAULT_KEEPALIVE_INTERVAL),
326                 OPT_UINT_T, 0, FLDSET(struct global_config, keep_alive_interval));
327         ast_sorcery_object_field_register(sorcery, "global", "max_initial_qualify_time",
328                 __stringify(DEFAULT_MAX_INITIAL_QUALIFY_TIME),
329                 OPT_UINT_T, 0, FLDSET(struct global_config, max_initial_qualify_time));
330         ast_sorcery_object_field_register(sorcery, "global", "default_from_user", DEFAULT_FROM_USER,
331                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_from_user));
332         ast_sorcery_object_field_register(sorcery, "global", "regcontext", DEFAULT_REGCONTEXT,
333                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, regcontext));
334
335
336         if (ast_sorcery_instance_observer_add(sorcery, &observer_callbacks_global)) {
337                 return -1;
338         }
339
340         return 0;
341 }