res_pjsip: When no global type the debug option defaults to "yes"
[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         );
43         /* Value to put in Max-Forwards header */
44         unsigned int max_forwards;
45 };
46
47 static void global_destructor(void *obj)
48 {
49         struct global_config *cfg = obj;
50
51         ast_string_field_free_memory(cfg);
52 }
53
54 static void *global_alloc(const char *name)
55 {
56         struct global_config *cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
57
58         if (!cfg || ast_string_field_init(cfg, 80)) {
59                 return NULL;
60         }
61
62         return cfg;
63 }
64
65 static int global_apply(const struct ast_sorcery *sorcery, void *obj)
66 {
67         struct global_config *cfg = obj;
68         char max_forwards[10];
69
70         snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
71
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);
75         return 0;
76 }
77
78 static struct global_config *get_global_cfg(void)
79 {
80         RAII_VAR(struct ao2_container *, globals, ast_sorcery_retrieve_by_fields(
81                          ast_sip_get_sorcery(), "global", AST_RETRIEVE_FLAG_MULTIPLE,
82                          NULL), ao2_cleanup);
83
84         if (!globals) {
85                 return NULL;
86         }
87
88         return ao2_find(globals, NULL, 0);
89 }
90
91 char *ast_sip_global_default_outbound_endpoint(void)
92 {
93         RAII_VAR(struct global_config *, cfg, get_global_cfg(), ao2_cleanup);
94
95         if (!cfg) {
96                 return NULL;
97         }
98
99         return ast_strdup(cfg->default_outbound_endpoint);
100 }
101
102 char *ast_sip_get_debug(void)
103 {
104         char *res;
105         struct global_config *cfg = get_global_cfg();
106
107         if (!cfg) {
108                 return ast_strdup("no");
109         }
110
111         res = ast_strdup(cfg->debug);
112         ao2_ref(cfg, -1);
113
114         return res;
115 }
116
117 int ast_sip_initialize_sorcery_global(struct ast_sorcery *sorcery)
118 {
119         snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version());
120
121         ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
122
123         if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
124                 return -1;
125         }
126
127         ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
128         ast_sorcery_object_field_register(sorcery, "global", "max_forwards", __stringify(DEFAULT_MAX_FORWARDS),
129                         OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
130         ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
131                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
132         ast_sorcery_object_field_register(sorcery, "global", "default_outbound_endpoint", DEFAULT_OUTBOUND_ENDPOINT,
133                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
134         ast_sorcery_object_field_register(sorcery, "global", "debug", "no",
135                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, debug));
136
137         return 0;
138 }