mirror of https://github.com/asterisk/asterisk
For a complete list of the options added, see the review linked at the bottom of this commit message. (closes issue ASTERISK-21506) reported by Matt Jordan Review: https://reviewboard.asterisk.org/r/2671 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@394759 65c4cc65-6c06-0410-ace0-fbb531ad65f3changes/78/78/1
parent
3c86832f9f
commit
c47787feab
@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* Mark Michelson <mmichelson@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/res_sip.h"
|
||||
#include "asterisk/sorcery.h"
|
||||
#include "asterisk/ast_version.h"
|
||||
|
||||
#define DEFAULT_MAX_FORWARDS 70
|
||||
#define DEFAULT_USERAGENT_PREFIX "Asterisk PBX"
|
||||
|
||||
static char default_useragent[128];
|
||||
|
||||
struct global_config {
|
||||
SORCERY_OBJECT(details);
|
||||
AST_DECLARE_STRING_FIELDS(
|
||||
AST_STRING_FIELD(useragent);
|
||||
);
|
||||
/* Value to put in Max-Forwards header */
|
||||
unsigned int max_forwards;
|
||||
};
|
||||
|
||||
static void global_destructor(void *obj)
|
||||
{
|
||||
struct global_config *cfg = obj;
|
||||
|
||||
ast_string_field_free_memory(cfg);
|
||||
}
|
||||
|
||||
static void *global_alloc(const char *name)
|
||||
{
|
||||
struct global_config *cfg = ast_sorcery_generic_alloc(sizeof(*cfg), global_destructor);
|
||||
|
||||
if (!cfg || ast_string_field_init(cfg, 64)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
||||
static int global_apply(const struct ast_sorcery *sorcery, void *obj)
|
||||
{
|
||||
struct global_config *cfg = obj;
|
||||
char max_forwards[10];
|
||||
|
||||
snprintf(max_forwards, sizeof(max_forwards), "%u", cfg->max_forwards);
|
||||
|
||||
ast_sip_add_global_request_header("Max-Forwards", max_forwards, 1);
|
||||
ast_sip_add_global_request_header("User-Agent", cfg->useragent, 1);
|
||||
ast_sip_add_global_response_header("Server", cfg->useragent, 1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ast_sip_initialize_sorcery_global(struct ast_sorcery *sorcery)
|
||||
{
|
||||
snprintf(default_useragent, sizeof(default_useragent), "%s %s", DEFAULT_USERAGENT_PREFIX, ast_get_version());
|
||||
|
||||
ast_sorcery_apply_default(sorcery, "global", "config", "res_sip.conf,criteria=type=global");
|
||||
|
||||
if (ast_sorcery_object_register(sorcery, "global", global_alloc, NULL, global_apply)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ast_sorcery_object_field_register(sorcery, "global", "type", "", OPT_NOOP_T, 0, 0);
|
||||
ast_sorcery_object_field_register(sorcery, "global", "maxforwards", __stringify(DEFAULT_MAX_FORWARDS),
|
||||
OPT_UINT_T, 0, FLDSET(struct global_config, max_forwards));
|
||||
ast_sorcery_object_field_register(sorcery, "global", "useragent", default_useragent,
|
||||
OPT_STRINGFIELD_T, 0, STRFLDSET(struct global_config, useragent));
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* Mark Michelson <mmichelson@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/res_sip.h"
|
||||
#include "asterisk/sorcery.h"
|
||||
#include "include/res_sip_private.h"
|
||||
|
||||
#define TIMER_T1_MIN 100
|
||||
#define DEFAULT_TIMER_T1 500
|
||||
#define DEFAULT_TIMER_B 32000
|
||||
|
||||
struct system_config {
|
||||
SORCERY_OBJECT(details);
|
||||
/*! Transaction Timer T1 value */
|
||||
unsigned int timert1;
|
||||
/*! Transaction Timer B value */
|
||||
unsigned int timerb;
|
||||
/*! Should we use short forms for headers? */
|
||||
unsigned int compactheaders;
|
||||
};
|
||||
|
||||
static struct ast_sorcery *system_sorcery;
|
||||
|
||||
static void *system_alloc(const char *name)
|
||||
{
|
||||
struct system_config *system = ast_sorcery_generic_alloc(sizeof(*system), NULL);
|
||||
|
||||
if (!system) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return system;
|
||||
}
|
||||
|
||||
static int system_apply(const struct ast_sorcery *system_sorcery, void *obj)
|
||||
{
|
||||
struct system_config *system = obj;
|
||||
int min_timerb;
|
||||
|
||||
if (system->timert1 < TIMER_T1_MIN) {
|
||||
ast_log(LOG_WARNING, "Timer T1 setting is too low. Setting to %d\n", TIMER_T1_MIN);
|
||||
system->timert1 = TIMER_T1_MIN;
|
||||
}
|
||||
|
||||
min_timerb = 64 * system->timert1;
|
||||
|
||||
if (system->timerb < min_timerb) {
|
||||
ast_log(LOG_WARNING, "Timer B setting is too low. Setting to %d\n", min_timerb);
|
||||
system->timerb = min_timerb;
|
||||
}
|
||||
|
||||
pjsip_cfg()->tsx.t1 = system->timert1;
|
||||
pjsip_cfg()->tsx.td = system->timerb;
|
||||
|
||||
if (system->compactheaders) {
|
||||
extern pj_bool_t pjsip_use_compact_form;
|
||||
pjsip_use_compact_form = PJ_TRUE;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ast_sip_initialize_system(void)
|
||||
{
|
||||
system_sorcery = ast_sorcery_open();
|
||||
if (!system_sorcery) {
|
||||
ast_log(LOG_ERROR, "Failed to open SIP system sorcery\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ast_sorcery_apply_config(system_sorcery, "res_sip");
|
||||
|
||||
ast_sorcery_apply_default(system_sorcery, "system", "config", "res_sip.conf,criteria=type=system");
|
||||
|
||||
if (ast_sorcery_object_register(system_sorcery, "system", system_alloc, NULL, system_apply)) {
|
||||
ast_sorcery_unref(system_sorcery);
|
||||
system_sorcery = NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
ast_sorcery_object_field_register(system_sorcery, "system", "type", "", OPT_NOOP_T, 0, 0);
|
||||
ast_sorcery_object_field_register(system_sorcery, "system", "timert1", __stringify(DEFAULT_TIMER_T1),
|
||||
OPT_UINT_T, 0, FLDSET(struct system_config, timert1));
|
||||
ast_sorcery_object_field_register(system_sorcery, "system", "timerb", __stringify(DEFAULT_TIMER_B),
|
||||
OPT_UINT_T, 0, FLDSET(struct system_config, timerb));
|
||||
ast_sorcery_object_field_register(system_sorcery, "system", "compactheaders", "no",
|
||||
OPT_BOOL_T, 1, FLDSET(struct system_config, compactheaders));
|
||||
|
||||
ast_sorcery_load(system_sorcery);
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 2013, Digium, Inc.
|
||||
*
|
||||
* Mark Michelson <mmichelson@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include "asterisk/res_sip.h"
|
||||
#include "asterisk/linkedlists.h"
|
||||
|
||||
static pj_status_t add_request_headers(pjsip_tx_data *tdata);
|
||||
static pj_status_t add_response_headers(pjsip_tx_data *tdata);
|
||||
|
||||
/*!
|
||||
* \brief Indicator we've already handled a specific request/response
|
||||
*
|
||||
* PJSIP tends to reuse requests and responses. If we already have added
|
||||
* headers to a request or response, we mark the message with this value
|
||||
* so that we know not to re-add the headers again.
|
||||
*/
|
||||
static unsigned int handled_id = 0xB000B1E5;
|
||||
|
||||
static pjsip_module global_header_mod = {
|
||||
.name = {"Global headers", 13},
|
||||
.priority = PJSIP_MOD_PRIORITY_APPLICATION,
|
||||
.on_tx_request = add_request_headers,
|
||||
.on_tx_response = add_response_headers,
|
||||
};
|
||||
|
||||
struct header {
|
||||
AST_DECLARE_STRING_FIELDS(
|
||||
AST_STRING_FIELD(name);
|
||||
AST_STRING_FIELD(value);
|
||||
);
|
||||
AST_LIST_ENTRY(header) next;
|
||||
};
|
||||
|
||||
static struct header *alloc_header(const char *name, const char *value)
|
||||
{
|
||||
struct header *alloc;
|
||||
|
||||
alloc = ast_calloc_with_stringfields(1, struct header, 32);
|
||||
|
||||
if (!alloc) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ast_string_field_set(alloc, name, name);
|
||||
ast_string_field_set(alloc, value, value);
|
||||
|
||||
return alloc;
|
||||
}
|
||||
|
||||
static void destroy_header(struct header *to_destroy)
|
||||
{
|
||||
ast_string_field_free_memory(to_destroy);
|
||||
ast_free(to_destroy);
|
||||
}
|
||||
|
||||
AST_RWLIST_HEAD(header_list, header);
|
||||
|
||||
static struct header_list request_headers;
|
||||
static struct header_list response_headers;
|
||||
|
||||
static void add_headers_to_message(struct header_list *headers, pjsip_tx_data *tdata)
|
||||
{
|
||||
struct header *iter;
|
||||
SCOPED_LOCK(lock, headers, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK);
|
||||
if (tdata->mod_data[global_header_mod.id] == &handled_id) {
|
||||
return;
|
||||
}
|
||||
AST_LIST_TRAVERSE(headers, iter, next) {
|
||||
ast_sip_add_header(tdata, iter->name, iter->value);
|
||||
};
|
||||
tdata->mod_data[global_header_mod.id] = &handled_id;
|
||||
}
|
||||
|
||||
static pj_status_t add_request_headers(pjsip_tx_data *tdata)
|
||||
{
|
||||
add_headers_to_message(&request_headers, tdata);
|
||||
|
||||
return PJ_SUCCESS;
|
||||
}
|
||||
|
||||
static pj_status_t add_response_headers(pjsip_tx_data *tdata)
|
||||
{
|
||||
add_headers_to_message(&response_headers, tdata);
|
||||
|
||||
return PJ_SUCCESS;
|
||||
}
|
||||
|
||||
static void remove_header(struct header_list *headers, const char *to_remove)
|
||||
{
|
||||
struct header *iter;
|
||||
AST_LIST_TRAVERSE_SAFE_BEGIN(headers, iter, next) {
|
||||
if (!strcasecmp(iter->name, to_remove)) {
|
||||
AST_LIST_REMOVE_CURRENT(next);
|
||||
break;
|
||||
}
|
||||
}
|
||||
AST_LIST_TRAVERSE_SAFE_END;
|
||||
}
|
||||
|
||||
static int add_header(struct header_list *headers, const char *name, const char *value, int replace)
|
||||
{
|
||||
struct header *to_add;
|
||||
|
||||
to_add = alloc_header(name, value);
|
||||
if (!to_add) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
AST_RWLIST_WRLOCK(headers);
|
||||
if (replace) {
|
||||
remove_header(headers, name);
|
||||
}
|
||||
AST_LIST_INSERT_TAIL(headers, to_add, next);
|
||||
AST_RWLIST_UNLOCK(headers);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ast_sip_add_global_request_header(const char *name, const char *value, int replace)
|
||||
{
|
||||
return add_header(&request_headers, name, value, replace);
|
||||
}
|
||||
|
||||
int ast_sip_add_global_response_header(const char *name, const char *value, int replace)
|
||||
{
|
||||
return add_header(&response_headers, name, value, replace);
|
||||
}
|
||||
|
||||
void ast_sip_initialize_global_headers(void)
|
||||
{
|
||||
AST_RWLIST_HEAD_INIT(&request_headers);
|
||||
AST_RWLIST_HEAD_INIT(&response_headers);
|
||||
|
||||
ast_sip_register_service(&global_header_mod);
|
||||
}
|
||||
|
||||
static void destroy_headers(struct header_list *headers)
|
||||
{
|
||||
struct header *iter;
|
||||
|
||||
while ((iter = AST_RWLIST_REMOVE_HEAD(headers, next))) {
|
||||
destroy_header(iter);
|
||||
}
|
||||
AST_RWLIST_HEAD_DESTROY(headers);
|
||||
}
|
||||
|
||||
void ast_sip_destroy_global_headers(void)
|
||||
{
|
||||
destroy_headers(&request_headers);
|
||||
destroy_headers(&response_headers);
|
||||
}
|
Loading…
Reference in new issue