res_pjsip_messaging: send message to a default outbound endpoint
[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         );
41         /* Value to put in Max-Forwards header */
42         unsigned int max_forwards;
43 };
44
45 static void global_destructor(void *obj)
46 {
47         struct global_config *cfg = obj;
48
49         ast_string_field_free_memory(cfg);
50 }
51
52 static void *global_alloc(const char *name)
53 {
54         struct global_config *cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
55
56         if (!cfg || ast_string_field_init(cfg, 64)) {
57                 return NULL;
58         }
59
60         return cfg;
61 }
62
63 static int global_apply(const struct ast_sorcery *sorcery, void *obj)
64 {
65         struct global_config *cfg = obj;
66         char max_forwards[10];
67
68         snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
69
70         ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
71         ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
72         ast_sip_add_global_response_header("Server", cfg->useragent, 1);
73         return 0;
74 }
75
76 static struct global_config *get_global_cfg(void)
77 {
78         RAII_VAR(struct ao2_container *, globals, ast_sorcery_retrieve_by_fields(
79                          ast_sip_get_sorcery(), "global", AST_RETRIEVE_FLAG_MULTIPLE,
80                          NULL), ao2_cleanup);
81
82         if (!globals) {
83                 return NULL;
84         }
85
86         return ao2_find(globals, NULL, 0);
87 }
88
89 char *ast_sip_global_default_outbound_endpoint(void)
90 {
91         RAII_VAR(struct global_config *, cfg, get_global_cfg(), ao2_cleanup);
92
93         if (!cfg) {
94                 return NULL;
95         }
96
97         return ast_strdup(cfg->default_outbound_endpoint);
98 }
99
100 int ast_sip_initialize_sorcery_global(struct ast_sorcery *sorcery)
101 {
102         snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version());
103
104         ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
105
106         if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
107                 return -1;
108         }
109
110         ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
111         ast_sorcery_object_field_register(sorcery, "global", "max_forwards", __stringify(DEFAULT_MAX_FORWARDS),
112                         OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
113         ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
114                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
115         ast_sorcery_object_field_register(sorcery, "global", "default_outbound_endpoint", DEFAULT_OUTBOUND_ENDPOINT,
116                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, default_outbound_endpoint));
117
118         return 0;
119 }