d883e5d4724c4bb173403324462f7b7b0395b2fb
[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 "asterisk/sorcery.h"
26 #include "asterisk/ast_version.h"
27
28 #define DEFAULT_MAX_FORWARDS 70
29 #define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
30
31 static char default_useragent[128];
32
33 struct global_config {
34         SORCERY_OBJECT(details);
35         AST_DECLARE_STRING_FIELDS(
36                 AST_STRING_FIELD(useragent);
37         );
38         /* Value to put in Max-Forwards header */
39         unsigned int max_forwards;
40 };
41
42 static void global_destructor(void *obj)
43 {
44         struct global_config *cfg = obj;
45
46         ast_string_field_free_memory(cfg);
47 }
48
49 static void *global_alloc(const char *name)
50 {
51         struct global_config *cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
52
53         if (!cfg || ast_string_field_init(cfg, 64)) {
54                 return NULL;
55         }
56
57         return cfg;
58 }
59
60 static int global_apply(const struct ast_sorcery *sorcery, void *obj)
61 {
62         struct global_config *cfg = obj;
63         char max_forwards[10];
64
65         snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
66
67         ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
68         ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
69         ast_sip_add_global_response_header("Server", cfg->useragent, 1);
70         return 0;
71 }
72
73 int ast_sip_initialize_sorcery_global(struct ast_sorcery *sorcery)
74 {
75         snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version());
76
77         ast_sorcery_apply_default(sorcery, "global", "config", "pjsip.conf,criteria=type=global");
78
79         if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
80                 return -1;
81         }
82
83         ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
84         ast_sorcery_object_field_register(sorcery, "global", "max_forwards", __stringify(DEFAULT_MAX_FORWARDS),
85                         OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
86         ast_sorcery_object_field_register(sorcery, "global", "user_agent", default_useragent,
87                         OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
88
89         return 0;
90 }