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