2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
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.
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.
24 #include "asterisk/res_pjsip.h"
25 #include "include/res_pjsip_private.h"
26 #include "asterisk/sorcery.h"
27 #include "asterisk/ast_version.h"
29 #define DEFAULT_MAX_FORWARDS 70
30 #define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
31 #define DEFAULT_OUTBOUND_ENDPOINT "default_outbound_endpoint"
33 static char default_useragent[128];
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);
43 /* Value to put in Max-Forwards header */
44 unsigned int max_forwards;
47 static void global_destructor(void *obj)
49 struct global_config *cfg = obj;
51 ast_string_field_free_memory(cfg);
54 static void *global_alloc(const char *name)
56 struct global_config *cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
58 if (!cfg || ast_string_field_init(cfg, 80)) {
65 static int global_apply(const struct ast_sorcery *sorcery, void *obj)
67 struct global_config *cfg = obj;
68 char max_forwards[10];
70 snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
72 ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
73 ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
74 ast_sip_add_global_response_header("Server", cfg->useragent, 1);
78 static struct global_config *get_global_cfg(void)
80 RAII_VAR(struct ao2_container *, globals, ast_sorcery_retrieve_by_fields(
81 ast_sip_get_sorcery(), "global", AST_RETRIEVE_FLAG_MULTIPLE,
88 return ao2_find(globals, NULL, 0);
91 char *ast_sip_global_default_outbound_endpoint(void)
93 RAII_VAR(struct global_config *, cfg, get_global_cfg(), ao2_cleanup);
99 return ast_strdup(cfg->default_outbound_endpoint);
102 char *ast_sip_get_debug(void)
105 struct global_config *cfg = get_global_cfg();
108 return ast_strdup("no");
111 res = ast_strdup(cfg->debug);
117 int ast_sip_initialize_sorcery_global(void)
119 struct ast_sorcery *sorcery = ast_sip_get_sorcery();
121 snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version());
123 ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
125 if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
129 ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
130 ast_sorcery_object_field_register(sorcery, "global", "max_forwards", __stringify(DEFAULT_MAX_FORWARDS),
131 OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
132 ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
133 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
134 ast_sorcery_object_field_register(sorcery, "global", "default_outbound_endpoint", DEFAULT_OUTBOUND_ENDPOINT,
135 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
136 ast_sorcery_object_field_register(sorcery, "global", "debug", "no",
137 OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, debug));