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_USERAGENT_PREFIX "Asterisk PBX"
31 #define DEFAULT_OUTBOUND_ENDPOINT "default_outbound_endpoint"
32
33 static char default_useragent[128];
34
35 struct global_config {
36         SORCERY_OBJECT(details);
37         AST_DECLARE_STRING_FIELDS(
38                 AST_STRING_FIELD(useragent);
39                 AST_STRING_FIELD(default_outbound_endpoint);
40                 /*! Debug logging yes|no|host */
41                 AST_STRING_FIELD(debug);
42                 /*! Order by which endpoint identifiers are checked (comma separated list) */
43                 AST_STRING_FIELD(endpoint_identifier_order);
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 = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
61
62         if (!cfg || ast_string_field_init(cfg, 100)) {
63                 return NULL;
64         }
65
66         return cfg;
67 }
68
69 static int global_apply(const struct ast_sorcery *sorcery, void *obj)
70 {
71         struct global_config *cfg = obj;
72         char max_forwards[10];
73
74         snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
75
76         ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
77         ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
78         ast_sip_add_global_response_header("Server", cfg->useragent, 1);
79         return 0;
80 }
81
82 static struct global_config *get_global_cfg(void)
83 {
84         RAII_VAR(struct ao2_container *, globals, ast_sorcery_retrieve_by_fields(
85                          ast_sip_get_sorcery(), "global", AST_RETRIEVE_FLAG_MULTIPLE,
86                          NULL), ao2_cleanup);
87
88         if (!globals) {
89                 return NULL;
90         }
91
92         return ao2_find(globals, NULL, 0);
93 }
94
95 char *ast_sip_global_default_outbound_endpoint(void)
96 {
97         RAII_VAR(struct global_config *, cfg, get_global_cfg(), ao2_cleanup);
98
99         if (!cfg) {
100                 return NULL;
101         }
102
103         return ast_strdup(cfg->default_outbound_endpoint);
104 }
105
106 char *ast_sip_get_debug(void)
107 {
108         char *res;
109         struct global_config *cfg = get_global_cfg();
110
111         if (!cfg) {
112                 return ast_strdup("no");
113         }
114
115         res = ast_strdup(cfg->debug);
116         ao2_ref(cfg, -1);
117
118         return res;
119 }
120
121 char *ast_sip_get_endpoint_identifier_order(void)
122 {
123         char *res;
124         struct global_config *cfg = get_global_cfg();
125
126         if (!cfg) {
127                 return ast_strdup("ip,username,anonymous");
128         }
129
130         res = ast_strdup(cfg->endpoint_identifier_order);
131         ao2_ref(cfg, -1);
132
133         return res;
134 }
135
136 unsigned int ast_sip_get_keep_alive_interval(void)
137 {
138         unsigned int interval;
139         struct global_config *cfg = get_global_cfg();
140
141         if (!cfg) {
142                 return 0;
143         }
144
145         interval = cfg->keep_alive_interval;
146         ao2_ref(cfg, -1);
147
148         return interval;
149 }
150
151 int ast_sip_initialize_sorcery_global(void)
152 {
153         struct ast_sorcery *sorcery = ast_sip_get_sorcery();
154
155         snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version());
156
157         ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
158
159         if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
160                 return -1;
161         }
162
163         ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
164         ast_sorcery_object_field_register(sorcery, "global", "max_forwards", __stringify(DEFAULT_MAX_FORWARDS),
165                         OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
166         ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
167                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
168         ast_sorcery_object_field_register(sorcery, "global", "default_outbound_endpoint", DEFAULT_OUTBOUND_ENDPOINT,
169                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
170         ast_sorcery_object_field_register(sorcery, "global", "debug", "no",
171                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, debug));
172         ast_sorcery_object_field_register(sorcery, "global", "endpoint_identifier_order", "ip,username,anonymous",
173                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, endpoint_identifier_order));
174         ast_sorcery_object_field_register(sorcery, "global", "keep_alive_interval", "",
175                         OPT_UINT_T, 0, FLDSET(struct global_config, keep_alive_interval));
176
177         return 0;
178 }