24 #include "asterisk/res_pjsip.h"
26 #include "include/res_pjsip_private.h"
28 static pj_status_t add_request_headers(pjsip_tx_data *tdata);
29 static pj_status_t add_response_headers(pjsip_tx_data *tdata);
38 static unsigned int handled_id = 0xCA115785;
40 static pjsip_module global_header_mod = {
41 .name = {
"Global headers", 13},
42 .priority = PJSIP_MOD_PRIORITY_APPLICATION,
43 .on_tx_request = add_request_headers,
44 .on_tx_response = add_response_headers,
55 static struct header *alloc_header(
const char *name,
const char *value)
71 static void destroy_header(
struct header *to_destroy)
82 static void add_headers_to_message(
struct header_list *headers, pjsip_tx_data *tdata)
86 if (tdata->mod_data[global_header_mod.id] == &handled_id) {
91 pjsip_generic_string_hdr *hdr;
93 hdr = pjsip_msg_find_hdr_by_name(tdata->msg, pj_cstr(&name, iter->name), NULL);
98 ast_sip_add_header(tdata, iter->name, iter->value);
100 tdata->mod_data[global_header_mod.id] = &handled_id;
103 static pj_status_t add_request_headers(pjsip_tx_data *tdata)
105 add_headers_to_message(&request_headers, tdata);
110 static pj_status_t add_response_headers(pjsip_tx_data *tdata)
112 add_headers_to_message(&response_headers, tdata);
117 static void remove_header(
struct header_list *headers,
const char *to_remove)
121 if (!strcasecmp(iter->name, to_remove)) {
123 destroy_header(iter);
130 static int add_header(
struct header_list *headers,
const char *name,
const char *value,
int replace)
132 struct header *to_add = NULL;
134 if (!ast_strlen_zero(value)) {
135 to_add = alloc_header(name, value);
143 remove_header(headers, name);
153 int ast_sip_add_global_request_header(
const char *name,
const char *value,
int replace)
155 return add_header(&request_headers, name, value, replace);
158 int ast_sip_add_global_response_header(
const char *name,
const char *value,
int replace)
160 return add_header(&response_headers, name, value, replace);
163 void ast_sip_initialize_global_headers(
void)
168 ast_sip_register_service(&global_header_mod);
171 static void destroy_headers(
struct header_list *headers)
175 while ((iter = AST_RWLIST_REMOVE_HEAD(headers, next))) {
176 destroy_header(iter);
181 void ast_sip_destroy_global_headers(
void)
183 destroy_headers(&request_headers);
184 destroy_headers(&response_headers);
186 ast_sip_unregister_service(&global_header_mod);
#define AST_RWLIST_HEAD_DESTROY(head)
Destroys an rwlist head structure.
Asterisk main include file. File version handling, generic pbx functions.
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
#define AST_STRING_FIELD(name)
Declare a string field.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
#define SCOPED_LOCK(varname, lock, lockfunc, unlockfunc)
Scoped Locks.
A set of macros to manage forward-linked lists.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
#define AST_RWLIST_HEAD(name, type)
Defines a structure to be used to hold a read/write list of specified type.
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.