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