res_pjsip: Allow configuration of endpoint identifier query order
[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
29 #define DEFAULT_MAX_FORWARDS 70
30 #define DEFAULT_KEEPALIVE_INTERVAL 0
31 #define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
32 #define DEFAULT_OUTBOUND_ENDPOINT "default_outbound_endpoint"
33 #define DEFAULT_DEBUG "no"
34 #define DEFAULT_ENDPOINT_IDENTIFIER_ORDER "ip,username,anonymous"
35
36 static char default_useragent[256];
37
38 struct global_config {
39         SORCERY_OBJECT(details);
40         AST_DECLARE_STRING_FIELDS(
41                 AST_STRING_FIELD(useragent);
42                 AST_STRING_FIELD(default_outbound_endpoint);
43                 /*! Debug logging yes|no|host */
44                 AST_STRING_FIELD(debug);
45                 /*! Order by which endpoint identifiers are checked (comma separated list) */
46                 AST_STRING_FIELD(endpoint_identifier_order);
47         );
48         /* Value to put in Max-Forwards header */
49         unsigned int max_forwards;
50         /* The interval at which to send keep alive messages to active connection-oriented transports */
51         unsigned int keep_alive_interval;
52 };
53
54 static void global_destructor(void *obj)
55 {
56         struct global_config *cfg = obj;
57
58         ast_string_field_free_memory(cfg);
59 }
60
61 static void *global_alloc(const char *name)
62 {
63         struct global_config *cfg;
64
65         cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
66         if (!cfg || ast_string_field_init(cfg, 100)) {
67                 ao2_cleanup(cfg);
68                 return NULL;
69         }
70
71         return cfg;
72 }
73
74 static int global_apply(const struct ast_sorcery *sorcery, void *obj)
75 {
76         struct global_config *cfg = obj;
77         char max_forwards[10];
78
79         snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
80
81         ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
82         ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
83         ast_sip_add_global_response_header("Server", cfg->useragent, 1);
84         return 0;
85 }
86
87 static struct global_config *get_global_cfg(void)
88 {
89         struct global_config *cfg;
90         struct ao2_container *globals;
91
92         globals = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), "global",
93                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
94         if (!globals) {
95                 return NULL;
96         }
97
98         cfg = ao2_find(globals, NULL, 0);
99         ao2_ref(globals, -1);
100         return cfg;
101 }
102
103 char *ast_sip_global_default_outbound_endpoint(void)
104 {
105         char *str;
106         struct global_config *cfg;
107
108         cfg = get_global_cfg();
109         if (!cfg) {
110                 return ast_strdup(DEFAULT_OUTBOUND_ENDPOINT);
111         }
112
113         str = ast_strdup(cfg->default_outbound_endpoint);
114         ao2_ref(cfg, -1);
115         return str;
116 }
117
118 char *ast_sip_get_debug(void)
119 {
120         char *res;
121         struct global_config *cfg;
122
123         cfg = get_global_cfg();
124         if (!cfg) {
125                 return ast_strdup(DEFAULT_DEBUG);
126         }
127
128         res = ast_strdup(cfg->debug);
129         ao2_ref(cfg, -1);
130         return res;
131 }
132
133 char *ast_sip_get_endpoint_identifier_order(void)
134 {
135         char *res;
136         struct global_config *cfg;
137
138         cfg = get_global_cfg();
139         if (!cfg) {
140                 return ast_strdup(DEFAULT_ENDPOINT_IDENTIFIER_ORDER);
141         }
142
143         res = ast_strdup(cfg->endpoint_identifier_order);
144         ao2_ref(cfg, -1);
145         return res;
146 }
147
148 unsigned int ast_sip_get_keep_alive_interval(void)
149 {
150         unsigned int interval;
151         struct global_config *cfg;
152
153         cfg = get_global_cfg();
154         if (!cfg) {
155                 return DEFAULT_KEEPALIVE_INTERVAL;
156         }
157
158         interval = cfg->keep_alive_interval;
159         ao2_ref(cfg, -1);
160         return interval;
161 }
162
163 /*!
164  * \internal
165  * \brief Observer to set default global object if none exist.
166  *
167  * \param name Module name owning the sorcery instance.
168  * \param sorcery Instance being observed.
169  * \param object_type Name of object being observed.
170  * \param reloaded Non-zero if the object is being reloaded.
171  *
172  * \return Nothing
173  */
174 static void global_loaded_observer(const char *name, const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
175 {
176         struct ao2_container *globals;
177         struct global_config *cfg;
178
179         if (strcmp(object_type, "global")) {
180                 /* Not interested */
181                 return;
182         }
183
184         globals = ast_sorcery_retrieve_by_fields(sorcery, "global",
185                 AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, NULL);
186         if (globals) {
187                 int count;
188
189                 count = ao2_container_count(globals);
190                 ao2_ref(globals, -1);
191
192                 if (1 < count) {
193                         ast_log(LOG_ERROR,
194                                 "At most one pjsip.conf type=global object can be defined.  You have %d defined.\n",
195                                 count);
196                         return;
197                 }
198                 if (count) {
199                         return;
200                 }
201         }
202
203         ast_debug(1, "No pjsip.conf type=global object exists so applying defaults.\n");
204         cfg = ast_sorcery_alloc(sorcery, "global", NULL);
205         if (!cfg) {
206                 return;
207         }
208         global_apply(sorcery, cfg);
209         ao2_ref(cfg, -1);
210 }
211
212 static const struct ast_sorcery_instance_observer observer_callbacks_global = {
213         .object_type_loaded = global_loaded_observer,
214 };
215
216 int ast_sip_destroy_sorcery_global(void)
217 {
218         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
219
220         ast_sorcery_instance_observer_remove(sorcery, &observer_callbacks_global);
221
222         return 0;
223 }
224
225 int ast_sip_initialize_sorcery_global(void)
226 {
227         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
228
229         snprintf(default_useragent, sizeof(default_useragent), "%s %s",
230                 DEFAULT_USERAGENT_PREFIX, ast_get_version());
231
232         ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
233
234         if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
235                 return -1;
236         }
237
238         ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
239         ast_sorcery_object_field_register(sorcery, "global", "max_forwards",
240                 __stringify(DEFAULT_MAX_FORWARDS),
241                 OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
242         ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
243                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
244         ast_sorcery_object_field_register(sorcery, "global", "default_outbound_endpoint",
245                 DEFAULT_OUTBOUND_ENDPOINT,
246                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
247         ast_sorcery_object_field_register(sorcery, "global", "debug", DEFAULT_DEBUG,
248                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, debug));
249         ast_sorcery_object_field_register(sorcery, "global", "endpoint_identifier_order",
250                 DEFAULT_ENDPOINT_IDENTIFIER_ORDER,
251                 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, endpoint_identifier_order));
252         ast_sorcery_object_field_register(sorcery, "global", "keep_alive_interval",
253                 __stringify(DEFAULT_KEEPALIVE_INTERVAL),
254                 OPT_UINT_T, 0, FLDSET(struct global_config, keep_alive_interval));
255
256         if (ast_sorcery_instance_observer_add(sorcery, &observer_callbacks_global)) {
257                 return -1;
258         }
259
260         return 0;
261 }