Asterisk - The Open Source Telephony Project GIT-master-4f2b068
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions | Variables
res_pjsip_header_funcs.c File Reference
#include "asterisk.h"
#include <pjsip.h>
#include <pjsip_ua.h>
#include "asterisk/res_pjsip.h"
#include "asterisk/res_pjsip_session.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
Include dependency graph for res_pjsip_header_funcs.c:

Go to the source code of this file.

Data Structures

struct  hdr_list
 
struct  hdr_list_entry
 Linked list for accumulating headers. More...
 
struct  header_data
 Data structure used for ast_sip_push_task_wait_serializer
More...
 
struct  param_data
 

Macros

#define param_add(pool, list, pname, pvalue)
 

Enumerations

enum  param_type { PARAMETER_HEADER , PARAMETER_URI }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int add_header (void *obj)
 
static int add_inheritable_header (struct ast_channel *channel, const char *header_name, const char *header_value)
 
static int add_param (void *obj)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static pjsip_hdr * find_header (struct hdr_list *list, const char *header_name, int header_number)
 
static int func_read_header (struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
 Implements PJSIP_HEADER function 'read' callback.
 
static int func_read_headers (struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
 Read list of unique SIP headers.
 
static int func_read_param (struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
 
static int func_response_read_header (struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
 Implements PJSIP_RESPONSE_HEADER function 'read' callback.
 
static int func_response_read_headers (struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
 Read list of unique SIP response headers.
 
static int func_write_header (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 Implements PJSIP_HEADER function 'write' callback.
 
static int func_write_inheritable_header (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 Implements PJSIP_INHERITABLE_HEADER function 'write' callback.
 
static int func_write_param (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static int incoming_request (struct ast_sip_session *session, pjsip_rx_data *rdata)
 
static void incoming_response (struct ast_sip_session *session, pjsip_rx_data *rdata)
 
static void inheritable_headers_destroy (void *data)
 
static void * inheritable_headers_duplicate (void *data)
 
static int insert_headers (pj_pool_t *pool, struct hdr_list *list, pjsip_msg *msg)
 
static int load_module (void)
 
static void outgoing_request (struct ast_sip_session *session, pjsip_tx_data *tdata)
 
static int read_header (void *obj)
 
static int read_headers (void *obj)
 
static int read_param (void *obj)
 
static int remove_header (void *obj)
 
static int remove_inheritable_header (struct ast_channel *channel, const char *header_name)
 
static int unload_module (void)
 
static int update_header (void *obj)
 
static int update_inheritable_header (struct ast_channel *channel, const char *header_name, const char *header_value, int header_number)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Header Functions" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, .requires = "res_pjsip,res_pjsip_session", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_sip_session_supplement header_funcs_supplement
 
static const struct ast_datastore_info inheritable_header_datastore
 Datastore for saving inheritable headers (contains hdr_list with ast_malloc'd entries)
 
static struct ast_custom_function pjsip_header_function
 
static struct ast_custom_function pjsip_header_inherit_function
 
static struct ast_custom_function pjsip_header_param_function
 
static struct ast_custom_function pjsip_headers_function
 
static struct ast_custom_function pjsip_response_header_function
 
static struct ast_custom_function pjsip_response_headers_function
 
static const struct ast_datastore_info response_header_datastore
 Datastore for saving response headers.
 
static const struct ast_datastore_info session_header_datastore
 Datastore for saving headers in session (contains hdr_list, no destroy needed)
 

Macro Definition Documentation

◆ param_add

#define param_add (   pool,
  list,
  pname,
  pvalue 
)

Enumeration Type Documentation

◆ param_type

enum param_type
Enumerator
PARAMETER_HEADER 
PARAMETER_URI 

Definition at line 1597 of file res_pjsip_header_funcs.c.

1597 {
1600};

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 1847 of file res_pjsip_header_funcs.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 1847 of file res_pjsip_header_funcs.c.

◆ add_header()

static int add_header ( void *  obj)
static

Definition at line 760 of file res_pjsip_header_funcs.c.

761{
762 struct header_data *data = obj;
763 struct ast_sip_session *session = data->channel->session;
764 pj_pool_t *pool = session->inv_session->dlg->pool;
765 pj_str_t pj_header_name;
766 pj_str_t pj_header_value;
767 struct hdr_list_entry *le;
768 struct hdr_list *list;
769
770 RAII_VAR(struct ast_datastore *, datastore,
772
773 if (!datastore) {
776 || !(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list)))
777 || ast_sip_session_add_datastore(session, datastore)) {
778 ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
779 return -1;
780 }
781 AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
782 }
783
784 ast_debug(1, "Adding header %s with value %s\n", data->header_name,
785 data->header_value);
786
787 pj_cstr(&pj_header_name, data->header_name);
788 pj_cstr(&pj_header_value, data->header_value);
789 le = pj_pool_zalloc(pool, sizeof(struct hdr_list_entry));
790 le->hdr = (pjsip_hdr *) pjsip_generic_string_hdr_create(pool, &pj_header_name,
791 &pj_header_value);
792 list = datastore->data;
793
794 AST_LIST_INSERT_TAIL(list, le, nextptr);
795
796 return 0;
797}
static struct ast_mansession session
#define ast_log
Definition astobj2.c:42
#define ao2_cleanup(obj)
Definition astobj2.h:1934
#define AST_LOG_ERROR
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
static const struct ast_datastore_info session_header_datastore
Datastore for saving headers in session (contains hdr_list, no destroy needed)
struct ast_datastore * ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
Retrieve a session datastore.
int ast_sip_session_add_datastore(struct ast_sip_session *session, struct ast_datastore *datastore)
Add a datastore to a SIP session.
struct ast_datastore * ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
Alternative for ast_datastore_alloc()
const char * type
Definition datastore.h:32
Structure for a data store object.
Definition datastore.h:64
struct ast_sip_session * session
Pointer to session.
A structure describing a SIP session.
Linked list for accumulating headers.
pjsip_hdr * hdr
Data structure used for ast_sip_push_task_wait_serializer
struct ast_sip_channel_pvt * channel
const char * header_value
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:981

References ao2_cleanup, ast_debug, AST_LIST_HEAD_INIT_NOLOCK, AST_LIST_INSERT_TAIL, ast_log, AST_LOG_ERROR, ast_sip_session_add_datastore(), ast_sip_session_alloc_datastore(), ast_sip_session_get_datastore(), header_data::channel, hdr_list_entry::hdr, header_data::header_name, header_data::header_value, RAII_VAR, ast_sip_channel_pvt::session, session, session_header_datastore, and ast_datastore_info::type.

Referenced by func_write_header().

◆ add_inheritable_header()

static int add_inheritable_header ( struct ast_channel channel,
const char *  header_name,
const char *  header_value 
)
static

Definition at line 811 of file res_pjsip_header_funcs.c.

812{
813 struct ast_variable *headers = NULL;
814
815 /* Add to channel datastore to be inherited by bridged channel(s) */
816 struct ast_datastore *chan_datastore;
817 struct ast_variable *new_var;
818
819 ast_channel_lock(channel);
821 ast_debug(3, "Adding inheritable header %s with value %s to channel %s\n", header_name,
822 header_value, ast_channel_name(channel));
823
824 if (!chan_datastore) {
825 /* Create new channel datastore */
828 if (chan_datastore) {
829 chan_datastore->data = NULL;
830 chan_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
831 ast_channel_datastore_add(channel, chan_datastore);
832 ast_debug(3, "Created new inheritable SIP header channel datastore for channel %s\n",
833 ast_channel_name(channel));
834 } else {
835 ast_log(LOG_ERROR, "Failed to allocate channel datastore for channel %s to store inheritable SIP headers\n",
836 ast_channel_name(channel));
837 ast_channel_unlock(channel);
838 return -1;
839 }
840 } else {
841 /* Ensure it's marked as inheritable */
842 if (!chan_datastore->inheritance) {
843 chan_datastore->inheritance = DATASTORE_INHERIT_FOREVER;
844 ast_debug(3, "Upgraded SIP header channel datastore to inheritable for channel %s\n",
845 ast_channel_name(channel));
846 }
847 }
848
849 /* Add THIS header to the channel datastore */
850 headers = chan_datastore->data;
851 new_var = ast_variable_new(header_name, header_value, "");
852 if (!new_var) {
853 ast_log(LOG_ERROR, "Failed to allocate variable for channel %s to store inheritable SIP headers\n",
854 ast_channel_name(channel));
855 ast_channel_unlock(channel);
856 return -1;
857 }
858
859 /* Prepend to list */
860 new_var->next = headers;
861 chan_datastore->data = new_var;
862
863 ast_debug(1, "Inherited Header %s added with value %s for channel %s\n",
864 header_name, header_value, ast_channel_name(channel));
865
866 ast_channel_unlock(channel);
867
868 return 0;
869}
const char * ast_channel_name(const struct ast_channel *chan)
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition channel.c:2375
#define DATASTORE_INHERIT_FOREVER
Definition channel.h:194
#define ast_channel_lock(chan)
Definition channel.h:2982
#define ast_channel_unlock(chan)
Definition channel.h:2983
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition channel.c:2389
#define ast_datastore_alloc(info, uid)
Definition datastore.h:85
#define ast_variable_new(name, value, filename)
#define LOG_ERROR
static const struct ast_datastore_info inheritable_header_datastore
Datastore for saving inheritable headers (contains hdr_list with ast_malloc'd entries)
#define NULL
Definition resample.c:96
void * data
Definition datastore.h:66
unsigned int inheritance
Definition datastore.h:69
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next

References ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_datastore_alloc, ast_debug, ast_log, ast_variable_new, ast_datastore::data, DATASTORE_INHERIT_FOREVER, inheritable_header_datastore, ast_datastore::inheritance, LOG_ERROR, ast_variable::next, NULL, and ast_datastore_info::type.

Referenced by func_write_inheritable_header().

◆ add_param()

static int add_param ( void *  obj)
static

Definition at line 1665 of file res_pjsip_header_funcs.c.

1666{
1667 struct param_data *data = obj;
1668 struct ast_sip_session *session = data->channel->session;
1669 pj_pool_t *pool = session->inv_session->dlg->pool;
1670
1671 pjsip_fromto_hdr *dlg_info;
1672 pjsip_name_addr *dlg_info_name_addr;
1673 pjsip_sip_uri *dlg_info_uri;
1674
1675 dlg_info = session->inv_session->dlg->local.info; /* Local for outgoing */
1676 dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
1677 dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
1678 if (!PJSIP_URI_SCHEME_IS_SIP(dlg_info_uri) && !PJSIP_URI_SCHEME_IS_SIPS(dlg_info_uri)) {
1679 ast_log(LOG_WARNING, "Non SIP/SIPS URI\n");
1680 return -1;
1681 }
1682
1683 ast_debug(1, "Adding custom %s param %s = %s\n",
1684 data->paramtype == PARAMETER_URI ? "URI" : "header", data->param_name, data->param_value);
1685
1686 /* This works the same as doing this in set_from_header in res_pjsip_session.c
1687 * The way that this maps to pjproject is a little confusing.
1688 * Say we have <sip:foo@bar.com;p1=abc;p2=def?h1=qrs&h2=tuv>;o1=foo;o2=bar
1689 * p1 and p2 are URI parameters.
1690 * (h1 and h2 are URI headers)
1691 * o1 and o2 are header parameters (and don't have anything to do with the URI)
1692 * In pjproject, other_param is used for adding all custom parameters.
1693 * We use the URI for URI stuff, including URI parameters, and the header directly for header parameters.
1694 */
1695
1696#define param_add(pool, list, pname, pvalue) { \
1697 pjsip_param *param; \
1698 param = PJ_POOL_ALLOC_T(pool, pjsip_param); \
1699 pj_strdup2(pool, &param->name, pname); \
1700 pj_strdup2(pool, &param->value, pvalue); \
1701 pj_list_insert_before(list, param); \
1702}
1703
1704 if (data->paramtype == PARAMETER_URI) { /* URI parameter */
1705 param_add(pool, &dlg_info_uri->other_param, data->param_name, S_OR(data->param_value, ""));
1706 } else { /* Header parameter */
1707 param_add(pool, &dlg_info->other_param, data->param_name, S_OR(data->param_value, ""));
1708 }
1709
1710 return 0;
1711}
#define LOG_WARNING
#define param_add(pool, list, pname, pvalue)
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition strings.h:80
struct ast_sip_channel_pvt * channel

References ast_debug, ast_log, param_data::channel, LOG_WARNING, param_add, param_data::param_name, param_data::param_value, PARAMETER_URI, param_data::paramtype, S_OR, ast_sip_channel_pvt::session, and session.

Referenced by func_write_param().

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 1847 of file res_pjsip_header_funcs.c.

◆ find_header()

static pjsip_hdr * find_header ( struct hdr_list list,
const char *  header_name,
int  header_number 
)
static

Definition at line 555 of file res_pjsip_header_funcs.c.

557{
558 struct hdr_list_entry *le;
559 pjsip_hdr *hdr = NULL;
560 int i = 1;
561
562 if (!list || ast_strlen_zero(header_name) || header_number < 1) {
563 return NULL;
564 }
565
566 AST_LIST_TRAVERSE(list, le, nextptr) {
567 if (pj_stricmp2(&le->hdr->name, header_name) == 0 && i++ == header_number) {
568 hdr = le->hdr;
569 break;
570 }
571 }
572
573 return hdr;
574}
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
struct hdr_list_entry::@498 nextptr

References AST_LIST_TRAVERSE, ast_strlen_zero(), hdr_list_entry::hdr, hdr_list_entry::nextptr, and NULL.

Referenced by update_header().

◆ func_read_header()

static int func_read_header ( struct ast_channel chan,
const char *  function,
char *  data,
char *  buf,
size_t  len 
)
static

Implements PJSIP_HEADER function 'read' callback.

Valid actions are 'read' and 'remove'.

Definition at line 1237 of file res_pjsip_header_funcs.c.

1238{
1239 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
1240 struct header_data header_data;
1241 int number;
1243 AST_APP_ARG(action);
1246
1247 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
1248 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
1249 return -1;
1250 }
1251
1252 if (ast_strlen_zero(args.action)) {
1253 ast_log(AST_LOG_ERROR, "This function requires an action.\n");
1254 return -1;
1255 }
1256 if (ast_strlen_zero(args.header_name)) {
1257 ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
1258 return -1;
1259 }
1260 if (!args.header_number) {
1261 number = 1;
1262 } else {
1263 sscanf(args.header_number, "%30d", &number);
1264 if (number < 1) {
1265 number = 1;
1266 }
1267 }
1268
1270 header_data.header_name = args.header_name;
1276
1277 if (!strcasecmp(args.action, "read")) {
1279 } else if (!strcasecmp(args.action, "remove")) {
1282 } else {
1284 "Unknown action '%s' is not valid, must be 'read' or 'remove'.\n",
1285 args.action);
1286 return -1;
1287 }
1288}
void * ast_channel_tech_pvt(const struct ast_channel *chan)
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define ast_sip_push_task_wait_serializer(serializer, sip_task, task_data)
Definition res_pjsip.h:2189
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
static int remove_header(void *obj)
static int read_header(void *obj)
static struct @522 args
A structure which contains a channel implementation and session.
struct ast_taskprocessor * serializer
const struct ast_datastore_info * header_datastore
Number structure.

References args, AST_APP_ARG, ast_channel_name(), ast_channel_tech_pvt(), AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, ast_sip_push_task_wait_serializer, AST_STANDARD_APP_ARGS, ast_strlen_zero(), buf, header_data::buf, header_data::channel, header_data::header_datastore, header_data::header_name, header_data::header_number, header_data::header_value, len(), header_data::len, LOG_ERROR, NULL, read_header(), remove_header(), ast_sip_session::serializer, ast_sip_channel_pvt::session, and session_header_datastore.

◆ func_read_headers()

static int func_read_headers ( struct ast_channel chan,
const char *  function,
char *  data,
char *  buf,
size_t  len 
)
static

◆ func_read_param()

static int func_read_param ( struct ast_channel chan,
const char *  function,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 1713 of file res_pjsip_header_funcs.c.

1714{
1715 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
1716 struct param_data param_data;
1717
1722 );
1723
1725
1727
1728 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
1729 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
1730 return -1;
1731 }
1732 if (ast_strlen_zero(args.param_type)) {
1733 ast_log(AST_LOG_ERROR, "This function requires a parameter type.\n");
1734 return -1;
1735 }
1736 if (ast_strlen_zero(args.param_name)) {
1737 ast_log(AST_LOG_ERROR, "This function requires a parameter name.\n");
1738 return -1;
1739 }
1740
1741 /* Currently, only From is supported, but this could be extended in the future. */
1742 if (ast_strlen_zero(args.header_name) || strcasecmp(args.header_name, "From")) {
1743 ast_log(LOG_WARNING, "Only the From header is currently supported\n");
1744 return -1;
1745 }
1746
1747 param_data.param_name = args.param_name;
1748 if (!strcasecmp(args.param_type, "header")) {
1750 } else if (!strcasecmp(args.param_type, "uri")) {
1752 } else {
1753 ast_log(LOG_WARNING, "Parameter type '%s' is invalid: must be 'header' or 'uri'\n", args.param_type);
1754 return -1;
1755 }
1756
1757 param_data.buf = buf;
1758 param_data.len = len;
1759
1761}
static int read_param(void *obj)
enum param_type paramtype

References args, AST_APP_ARG, ast_channel_name(), ast_channel_tech_pvt(), AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, ast_sip_push_task_wait_serializer, AST_STANDARD_APP_ARGS, ast_strlen_zero(), buf, param_data::buf, param_data::channel, param_data::header_name, len(), param_data::len, LOG_ERROR, LOG_WARNING, NULL, param_data::param_name, PARAMETER_HEADER, PARAMETER_URI, param_data::paramtype, read_param(), ast_sip_session::serializer, and ast_sip_channel_pvt::session.

◆ func_response_read_header()

static int func_response_read_header ( struct ast_channel chan,
const char *  function,
char *  data,
char *  buf,
size_t  len 
)
static

Implements PJSIP_RESPONSE_HEADER function 'read' callback.

Valid actions are 'read'

Definition at line 1295 of file res_pjsip_header_funcs.c.

1296{
1297 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
1298 struct header_data header_data;
1299 int number;
1301 AST_APP_ARG(action);
1304
1305 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
1306 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
1307 return -1;
1308 }
1309
1310 if (ast_strlen_zero(args.action)) {
1311 ast_log(AST_LOG_ERROR, "This function requires an action.\n");
1312 return -1;
1313 }
1314 if (ast_strlen_zero(args.header_name)) {
1315 ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
1316 return -1;
1317 }
1318 if (!args.header_number) {
1319 number = 1;
1320 } else {
1321 sscanf(args.header_number, "%30d", &number);
1322 if (number < 1) {
1323 number = 1;
1324 }
1325 }
1326
1328 header_data.header_name = args.header_name;
1334
1335 if (!strcasecmp(args.action, "read")) {
1337 } else {
1339 "Unknown action '%s' is not valid, must be 'read'.\n",
1340 args.action);
1341 return -1;
1342 }
1343}
static const struct ast_datastore_info response_header_datastore
Datastore for saving response headers.

References args, AST_APP_ARG, ast_channel_name(), ast_channel_tech_pvt(), AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, ast_sip_push_task_wait_serializer, AST_STANDARD_APP_ARGS, ast_strlen_zero(), buf, header_data::buf, header_data::channel, header_data::header_datastore, header_data::header_name, header_data::header_number, header_data::header_value, len(), header_data::len, LOG_ERROR, NULL, read_header(), response_header_datastore, ast_sip_session::serializer, and ast_sip_channel_pvt::session.

◆ func_response_read_headers()

static int func_response_read_headers ( struct ast_channel chan,
const char *  function,
char *  data,
char *  buf,
size_t  len 
)
static

Read list of unique SIP response headers.

Definition at line 1202 of file res_pjsip_header_funcs.c.

1203{
1204 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
1205 struct header_data header_data;
1207 AST_APP_ARG(header_pattern);
1208 );
1210
1211 if (!chan || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
1212 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
1213 return -1;
1214 }
1215
1216 if (ast_strlen_zero(args.header_pattern)) {
1217 ast_log(AST_LOG_ERROR, "This function requires a pattern.\n");
1218 return -1;
1219 }
1220
1222 header_data.header_name = args.header_pattern;
1227
1229
1230}

References args, AST_APP_ARG, ast_channel_name(), ast_channel_tech_pvt(), AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, ast_sip_push_task_wait_serializer, AST_STANDARD_APP_ARGS, ast_strlen_zero(), buf, header_data::buf, header_data::channel, header_data::header_datastore, header_data::header_name, header_data::header_value, len(), header_data::len, LOG_ERROR, NULL, read_headers(), response_header_datastore, ast_sip_session::serializer, and ast_sip_channel_pvt::session.

◆ func_write_header()

static int func_write_header ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Implements PJSIP_HEADER function 'write' callback.

Valid actions are 'add', 'update' and 'remove'.

Definition at line 1350 of file res_pjsip_header_funcs.c.

1352{
1353 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
1354 struct header_data header_data;
1355 int header_number;
1357 AST_APP_ARG(action);
1361
1362 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
1363 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
1364 return -1;
1365 }
1366
1367 if (ast_strlen_zero(args.action)) {
1368 ast_log(AST_LOG_ERROR, "This function requires an action.\n");
1369 return -1;
1370 }
1371 if (ast_strlen_zero(args.header_name)) {
1372 ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
1373 return -1;
1374 }
1375 if (!args.header_number) {
1376 header_number = 1;
1377 } else {
1378 sscanf(args.header_number, "%30d", &header_number);
1379 if (header_number < 1) {
1380 header_number = 1;
1381 }
1382 }
1383
1385 header_data.header_name = args.header_name;
1389 header_data.len = 0;
1391
1392 if (!strcasecmp(args.action, "add")) {
1395 } else if (!strcasecmp(args.action, "update")) {
1398 } else if (!strcasecmp(args.action, "remove")) {
1401 } else {
1403 "Unknown action '%s' is not valid, must be 'add', 'update', or 'remove'.\n",
1404 args.action);
1405 return -1;
1406 }
1407}
static int add_header(void *obj)
static int update_header(void *obj)
int value
Definition syslog.c:37

References add_header(), args, AST_APP_ARG, ast_channel_name(), ast_channel_tech_pvt(), AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, ast_sip_push_task_wait_serializer, AST_STANDARD_APP_ARGS, ast_strlen_zero(), header_data::buf, header_data::channel, header_data::header_datastore, header_data::header_name, header_data::header_number, header_data::header_value, header_data::len, LOG_ERROR, NULL, remove_header(), ast_sip_session::serializer, ast_sip_channel_pvt::session, session_header_datastore, update_header(), and value.

◆ func_write_inheritable_header()

static int func_write_inheritable_header ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Implements PJSIP_INHERITABLE_HEADER function 'write' callback.

Valid actions are 'add', 'update' and 'remove'.

Definition at line 1414 of file res_pjsip_header_funcs.c.

1416{
1417 int header_number;
1419 AST_APP_ARG(action);
1420 AST_APP_ARG(header_name);
1421 AST_APP_ARG(header_number););
1423
1424 if (!chan) {
1425 ast_log(LOG_ERROR, "The PJSIP_INHERITABLE_HEADER function requires a channel.\n");
1426 return -1;
1427 }
1428 if (ast_strlen_zero(args.action)) {
1429 ast_log(AST_LOG_ERROR, "The PJSIP_INHERITABLE_HEADER function requires an action.\n");
1430 return -1;
1431 }
1432 if (ast_strlen_zero(args.header_name)) {
1433 ast_log(AST_LOG_ERROR, "The PJSIP_INHERITABLE_HEADER function requires a header name.\n");
1434 return -1;
1435 }
1436 if (!args.header_number) {
1437 header_number = 1;
1438 } else {
1439 sscanf(args.header_number, "%30d", &header_number);
1440 if (header_number < 1) {
1441 header_number = 1;
1442 }
1443 }
1444
1445 /* TODO: we might want to check channel type and use the serializer if PJSIP */
1446 if (!strcasecmp(args.action, "add")) {
1447 return add_inheritable_header(chan, args.header_name, value);
1448 } else if (!strcasecmp(args.action, "update")) {
1449 return update_inheritable_header(chan, args.header_name, value, header_number);
1450 } else if (!strcasecmp(args.action, "remove")) {
1451 return remove_inheritable_header(chan, args.header_name);
1452 } else {
1454 "Unknown action '%s' is not valid, must be 'add', 'update', or 'remove'.\n",
1455 args.action);
1456 return -1;
1457 }
1458}
static int add_inheritable_header(struct ast_channel *channel, const char *header_name, const char *header_value)
static int remove_inheritable_header(struct ast_channel *channel, const char *header_name)
static int update_inheritable_header(struct ast_channel *channel, const char *header_name, const char *header_value, int header_number)

References add_inheritable_header(), args, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, AST_STANDARD_APP_ARGS, ast_strlen_zero(), header_data::header_name, header_data::header_number, LOG_ERROR, remove_inheritable_header(), update_inheritable_header(), and value.

◆ func_write_param()

static int func_write_param ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 1763 of file res_pjsip_header_funcs.c.

1764{
1765 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
1766 struct param_data param_data;
1771 );
1772
1774
1776
1777 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
1778 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
1779 return -1;
1780 }
1781 if (ast_strlen_zero(args.param_type)) {
1782 ast_log(AST_LOG_ERROR, "This function requires a parameter type.\n");
1783 return -1;
1784 }
1785 if (ast_strlen_zero(args.param_name)) {
1786 ast_log(AST_LOG_ERROR, "This function requires a parameter name.\n");
1787 return -1;
1788 }
1789
1790 /* Currently, only From is supported, but this could be extended in the future. */
1791 if (ast_strlen_zero(args.header_name) || strcasecmp(args.header_name, "From")) {
1792 ast_log(LOG_WARNING, "Only the From header is currently supported\n");
1793 return -1;
1794 }
1795
1796 param_data.param_name = args.param_name;
1797 if (!strcasecmp(args.param_type, "header")) {
1799 } else if (!strcasecmp(args.param_type, "uri")) {
1801 } else {
1802 ast_log(LOG_WARNING, "Parameter type '%s' is invalid: must be 'header' or 'uri'\n", args.param_type);
1803 return -1;
1804 }
1806
1808}
static int add_param(void *obj)
const char * param_value

References add_param(), args, AST_APP_ARG, ast_channel_name(), ast_channel_tech_pvt(), AST_DECLARE_APP_ARGS, ast_log, AST_LOG_ERROR, ast_sip_push_task_wait_serializer, AST_STANDARD_APP_ARGS, ast_strlen_zero(), param_data::channel, param_data::header_name, LOG_ERROR, LOG_WARNING, NULL, param_data::param_name, param_data::param_value, PARAMETER_HEADER, PARAMETER_URI, param_data::paramtype, ast_sip_session::serializer, ast_sip_channel_pvt::session, and value.

◆ incoming_request()

static int incoming_request ( struct ast_sip_session session,
pjsip_rx_data *  rdata 
)
static

Definition at line 493 of file res_pjsip_header_funcs.c.

494{
495 pj_pool_t *pool = session->inv_session->dlg->pool;
496 RAII_VAR(struct ast_datastore *, datastore,
498
499 if (!datastore) {
500 if (!(datastore =
502 ||
503 !(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list))) ||
505 ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
506 return 0;
507 }
508 AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
509 }
510 insert_headers(pool, (struct hdr_list *) datastore->data, rdata->msg_info.msg);
511
512 return 0;
513}
static int insert_headers(pj_pool_t *pool, struct hdr_list *list, pjsip_msg *msg)

References ao2_cleanup, AST_LIST_HEAD_INIT_NOLOCK, ast_log, AST_LOG_ERROR, ast_sip_session_add_datastore(), ast_sip_session_alloc_datastore(), ast_sip_session_get_datastore(), insert_headers(), RAII_VAR, session, session_header_datastore, and ast_datastore_info::type.

◆ incoming_response()

static void incoming_response ( struct ast_sip_session session,
pjsip_rx_data *  rdata 
)
static

Definition at line 523 of file res_pjsip_header_funcs.c.

524{
525 pj_pool_t *pool = session->inv_session->dlg->pool;
526 RAII_VAR(struct ast_datastore *, datastore,
528 pjsip_status_line status = rdata->msg_info.msg->line.status;
529
530 /* Skip responses different of 200 OK, when 2xx is received. */
531 if (session->inv_session->state != PJSIP_INV_STATE_CONNECTING || status.code!=200) {
532 return;
533 }
534
535 if (!datastore) {
536 if (!(datastore =
538 ||
539 !(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list))) ||
541 ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
542 return;
543 }
544 AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
545 }
546 insert_headers(pool, (struct hdr_list *) datastore->data, rdata->msg_info.msg);
547
548 return;
549}
jack_status_t status
Definition app_jack.c:149

References ao2_cleanup, AST_LIST_HEAD_INIT_NOLOCK, ast_log, AST_LOG_ERROR, ast_sip_session_add_datastore(), ast_sip_session_alloc_datastore(), ast_sip_session_get_datastore(), insert_headers(), RAII_VAR, response_header_datastore, session, status, and ast_datastore_info::type.

◆ inheritable_headers_destroy()

static void inheritable_headers_destroy ( void *  data)
static

Definition at line 429 of file res_pjsip_header_funcs.c.

430{
432}
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition extconf.c:1260

References ast_variables_destroy().

Referenced by remove_inheritable_header(), and update_inheritable_header().

◆ inheritable_headers_duplicate()

static void * inheritable_headers_duplicate ( void *  data)
static

Definition at line 420 of file res_pjsip_header_funcs.c.

421{
422 return ast_variables_dup(data);
423}
struct ast_variable * ast_variables_dup(struct ast_variable *var)
Duplicate variable list.

References ast_variables_dup().

Referenced by outgoing_request().

◆ insert_headers()

static int insert_headers ( pj_pool_t *  pool,
struct hdr_list list,
pjsip_msg *  msg 
)
static

Definition at line 470 of file res_pjsip_header_funcs.c.

471{
472 pjsip_hdr *hdr = msg->hdr.next;
473 struct hdr_list_entry *le;
474
475 while (hdr && hdr != &msg->hdr) {
476 le = pj_pool_zalloc(pool, sizeof(struct hdr_list_entry));
477 le->hdr = pjsip_hdr_clone(pool, hdr);
478 AST_LIST_INSERT_TAIL(list, le, nextptr);
479 hdr = hdr->next;
480 }
481
482 return 0;
483}

References AST_LIST_INSERT_TAIL, hdr_list_entry::hdr, and hdr_list_entry::nextptr.

Referenced by incoming_request(), and incoming_response().

◆ load_module()

static int load_module ( void  )
static

Definition at line 1816 of file res_pjsip_header_funcs.c.

1817{
1825
1827}
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562
static struct ast_custom_function pjsip_header_function
static struct ast_custom_function pjsip_response_headers_function
static struct ast_sip_session_supplement header_funcs_supplement
static struct ast_custom_function pjsip_header_param_function
static struct ast_custom_function pjsip_response_header_function
static struct ast_custom_function pjsip_header_inherit_function
static struct ast_custom_function pjsip_headers_function
#define ast_sip_session_register_supplement(supplement)

References ast_custom_function_register, AST_MODULE_LOAD_SUCCESS, ast_sip_session_register_supplement, header_funcs_supplement, pjsip_header_function, pjsip_header_inherit_function, pjsip_header_param_function, pjsip_headers_function, pjsip_response_header_function, and pjsip_response_headers_function.

◆ outgoing_request()

static void outgoing_request ( struct ast_sip_session session,
pjsip_tx_data *  tdata 
)
static

Definition at line 1498 of file res_pjsip_header_funcs.c.

1499{
1500 struct hdr_list *hdr_list;
1501 struct hdr_list_entry *le;
1502 int header_count = 0;
1503 struct ast_datastore *inherited_datastore = NULL;
1504 RAII_VAR(struct ast_datastore *, session_datastore,
1506
1507 /* Check if we're past the INVITE stage */
1508 if (session->inv_session->state >= PJSIP_INV_STATE_CONFIRMED) {
1509 ast_debug(3, "Already confirmed (state=%d)\n",
1510 session->inv_session->state);
1511 return;
1512 }
1513
1514 /* If we're UAC and have a channel, check the channel datastore for an inheritable header datastore */
1515 if (session->channel && session->inv_session->role == PJSIP_ROLE_UAC) {
1516 struct ast_datastore *chan_datastore = NULL;
1517
1518 ast_channel_lock(session->channel);
1519
1521
1522 if (chan_datastore && chan_datastore->inheritance && chan_datastore->data) {
1525
1526 if (inherited_datastore) {
1527 /* The data is in ast_variable format from the duplicate callback. We need to make our own copy to
1528 avoid sharing the pointer. Reverse the order so they appear in the INVITE in the order added. */
1529 inherited_datastore->data = ast_variables_reverse((struct ast_variable *)
1530 inheritable_headers_duplicate((struct ast_variable *) chan_datastore->data));
1531 if (inherited_datastore->data) {
1532 inherited_datastore->inheritance = chan_datastore->inheritance;
1533 ast_sip_session_add_datastore(session, inherited_datastore);
1534 } else {
1535 ast_log(LOG_ERROR, "Failed to duplicate ast_variable list for channel %s\n",
1536 session->channel ? ast_channel_name(session->channel) : "(none)");
1537 ao2_ref(inherited_datastore, -1);
1538 inherited_datastore = NULL;
1539 }
1540 } else {
1541 ast_log(LOG_ERROR, "Failed to allocate inherited session datastore for channel %s\n",
1542 session->channel ? ast_channel_name(session->channel) : "(none)");
1543 }
1544 }
1545 ast_channel_unlock(session->channel);
1546 }
1547
1548 /* If we have no datastores at all, nothing to do */
1549 if (!session_datastore && !inherited_datastore) {
1550 return;
1551 }
1552
1553 /* Add headers from regular session datastore (non-inheritable headers added on this channel) */
1554 if (session_datastore && session_datastore->data) {
1555 hdr_list = session_datastore->data;
1556 AST_LIST_TRAVERSE(hdr_list, le, nextptr) {
1557 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) pjsip_hdr_clone(tdata->pool, le->hdr));
1558 header_count++;
1559 }
1560 ast_debug(3, "Added %d regular header(s) to channel %s\n", header_count,
1561 session->channel ? ast_channel_name(session->channel) : "(none)");
1562 /* Remove datastores after use */
1563 ast_sip_session_remove_datastore(session, session_datastore->uid);
1564 }
1565
1566 /* Add headers from inherited datastore (inheritable headers from parent channel) */
1567 if (inherited_datastore && inherited_datastore->data) {
1568 int inherited_count = 0;
1569 struct ast_variable *headers = inherited_datastore->data;
1570 struct ast_variable *var;
1571
1572 for (var = headers; var; var = var->next) {
1573 if (!ast_sip_add_header(tdata, var->name, var->value)) {
1574 inherited_count++;
1575 }
1576 }
1577 ast_debug(3, "Added %d inherited header(s) to channel %s\n", inherited_count,
1578 session->channel ? ast_channel_name(session->channel) : "(none)");
1579 header_count += inherited_count;
1580 /* Remove datastores after use */
1581 ast_sip_session_remove_datastore(session, inherited_datastore->uid);
1582 ao2_ref(inherited_datastore, -1);
1583 }
1584
1585 ast_debug(3, "Added total of %d header(s) to channel %s\n", header_count,
1586 session->channel ? ast_channel_name(session->channel) : "(none)");
1587}
#define var
Definition ast_expr2f.c:605
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
struct ast_variable * ast_variables_reverse(struct ast_variable *var)
Reverse a variable list.
int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
Add a header to an outbound SIP message.
Definition res_pjsip.c:2002
static void * inheritable_headers_duplicate(void *data)
void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
Remove a session datastore from the session.
const char * uid
Definition datastore.h:65

References ao2_cleanup, ao2_ref, ast_channel_datastore_find(), ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_debug, AST_LIST_TRAVERSE, ast_log, ast_sip_add_header(), ast_sip_session_add_datastore(), ast_sip_session_alloc_datastore(), ast_sip_session_get_datastore(), ast_sip_session_remove_datastore(), ast_variables_reverse(), ast_datastore::data, hdr_list_entry::hdr, inheritable_header_datastore, inheritable_headers_duplicate(), ast_datastore::inheritance, LOG_ERROR, ast_variable::next, NULL, RAII_VAR, session, session_header_datastore, ast_datastore_info::type, ast_datastore::uid, and var.

◆ read_header()

static int read_header ( void *  obj)
static

Definition at line 677 of file res_pjsip_header_funcs.c.

678{
679 struct header_data *data = obj;
680 size_t len = strlen(data->header_name);
681 pjsip_hdr *hdr = NULL;
682 char *pj_hdr_string;
683 int pj_hdr_string_len;
684 char *p;
685 size_t plen;
686 struct hdr_list_entry *le;
687 struct hdr_list *list;
688 int i = 1;
689 RAII_VAR(struct ast_datastore *, datastore,
692
693 if (!datastore || !datastore->data) {
694 ast_debug(1, "There was no datastore from which to read headers.\n");
695 return -1;
696 }
697
698 list = datastore->data;
699 AST_LIST_TRAVERSE(list, le, nextptr) {
700 if (len >= 1 && data->header_name[len - 1] == '*') {
701 if (pj_strnicmp2(&le->hdr->name, data->header_name, len - 1) == 0 && i++ == data->header_number) {
702 hdr = le->hdr;
703 break;
704 }
705 } else {
706 if (pj_stricmp2(&le->hdr->name, data->header_name) == 0 && i++ == data->header_number) {
707 hdr = le->hdr;
708 break;
709 }
710 }
711 }
712
713 if (!hdr) {
714 ast_debug(1, "There was no header named %s.\n", data->header_name);
715 return -1;
716 }
717
718 pj_hdr_string = ast_alloca(data->len);
719 pj_hdr_string_len = pjsip_hdr_print_on(hdr, pj_hdr_string, data->len - 1);
720 if (pj_hdr_string_len == -1) {
722 "Not enough buffer space in pjsip_hdr_print_on\n");
723 return -1;
724 }
725
726 pj_hdr_string[pj_hdr_string_len] = '\0';
727
728 p = strchr(pj_hdr_string, ':');
729 if (!p) {
731 "A malformed header was returned from pjsip_hdr_print_on.\n");
732 return -1;
733 }
734
735 ++p;
736 p = ast_strip(p);
737 plen = strlen(p);
738 if (plen + 1 > data->len) {
740 "Buffer isn't big enough to hold header value. %zu > %zu\n", plen + 1,
741 data->len);
742 return -1;
743 }
744
745 ast_copy_string(data->buf, p, data->len);
746
747 return 0;
748}
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition astmm.h:288
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition strings.h:425
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition strings.h:223

References ao2_cleanup, ast_alloca, ast_copy_string(), ast_debug, AST_LIST_TRAVERSE, ast_log, AST_LOG_ERROR, ast_sip_session_get_datastore(), ast_strip(), header_data::buf, header_data::channel, hdr_list_entry::hdr, header_data::header_datastore, header_data::header_name, header_data::header_number, len(), header_data::len, NULL, RAII_VAR, ast_sip_channel_pvt::session, and ast_datastore_info::type.

Referenced by func_read_header(), and func_response_read_header().

◆ read_headers()

static int read_headers ( void *  obj)
static

Definition at line 586 of file res_pjsip_header_funcs.c.

587{
588 struct header_data *data = obj;
589 size_t len = !ast_strlen_zero(data->header_name) ? strlen(data->header_name) : 0;
590 pjsip_hdr *hdr = NULL;
591 char *pj_hdr_string;
592 int pj_hdr_string_len;
593 char *p;
594 char *pos;
595 size_t plen, wlen = 0;
596 struct hdr_list_entry *le;
597 struct hdr_list *list;
598
599 RAII_VAR(struct ast_datastore *, datastore,
602
603 if (!datastore || !datastore->data) {
604 ast_debug(1, "There was no datastore from which to read headers.\n");
605 return -1;
606 }
607
608 list = datastore->data;
609 pj_hdr_string = ast_alloca(data->len);
610 AST_LIST_TRAVERSE(list, le, nextptr) {
611 if (!len || pj_strnicmp2(&le->hdr->name, data->header_name, len) == 0) {
612 /* Found matched header, append to buf */
613 hdr = le->hdr;
614
615 pj_hdr_string_len = pjsip_hdr_print_on(hdr, pj_hdr_string, data->len - 1);
616 if (pj_hdr_string_len == -1) {
618 "Not enough buffer space in pjsip_hdr_print_on\n");
619 return -1;
620 }
621 pj_hdr_string[pj_hdr_string_len] = '\0';
622 p = strchr(pj_hdr_string, ':');
623 if (!p) {
625 "A malformed header was returned from pjsip_hdr_print_on\n");
626 continue;
627 }
628
629 pj_hdr_string[p - pj_hdr_string] = '\0';
630 p = ast_strip(pj_hdr_string);
631 plen = strlen(p);
632 if (wlen + plen + 1 > data->len) {
634 "Buffer isn't big enough to hold header value. %zu > %zu\n", plen + 1,
635 data->len);
636 return -1;
637 }
638 pos = strstr(data->buf, p);
639 if (pos && pos[1] == ',') {
640 if (pos == data->buf) {
641 continue;
642 } else if (pos[-1] == ',') {
643 continue;
644 }
645 }
646 ast_copy_string(data->buf + wlen, p, data->len - wlen);
647 wlen += plen;
648 ast_copy_string(data->buf + wlen, ",", data->len - wlen);
649 wlen++;
650 }
651 }
652
653 if (wlen == 0) {
654 if (!len) {
655 /* No headers at all on this channel */
656 return 0;
657 } else {
658 ast_debug(1, "There was no header beginning with %s.\n", data->header_name);
659 return -1;
660 }
661 } else {
662 data->buf[wlen-1] = '\0';
663 }
664 return 0;
665}
#define AST_LOG_WARNING

References ao2_cleanup, ast_alloca, ast_copy_string(), ast_debug, AST_LIST_TRAVERSE, ast_log, AST_LOG_ERROR, AST_LOG_WARNING, ast_sip_session_get_datastore(), ast_strip(), ast_strlen_zero(), header_data::buf, header_data::channel, hdr_list_entry::hdr, header_data::header_datastore, header_data::header_name, len(), header_data::len, NULL, RAII_VAR, ast_sip_channel_pvt::session, and ast_datastore_info::type.

Referenced by func_read_headers(), and func_response_read_headers().

◆ read_param()

static int read_param ( void *  obj)
static

Definition at line 1613 of file res_pjsip_header_funcs.c.

1614{
1615 struct param_data *data = obj;
1616 struct ast_sip_session *session = data->channel->session;
1617 pj_str_t param_name;
1618
1619 pjsip_fromto_hdr *dlg_info;
1620 pjsip_name_addr *dlg_info_name_addr;
1621 pjsip_sip_uri *dlg_info_uri;
1622 pjsip_param *param;
1623 size_t param_len;
1624
1625 dlg_info = session->inv_session->dlg->remote.info; /* Remote dialog for incoming */
1626 dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
1627 dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
1628
1629 pj_cstr(&param_name, data->param_name);
1630
1631 if (data->paramtype == PARAMETER_URI) { /* URI parameter */
1632 param = pjsip_param_find(&dlg_info_uri->other_param, &param_name);
1633 } else { /* Header parameter */
1634 param = pjsip_param_find(&dlg_info->other_param, &param_name);
1635 }
1636
1637 if (!param) {
1638 ast_debug(1, "No %s parameter found named %s\n",
1639 data->paramtype == PARAMETER_URI ? "URI" : "header", data->param_name);
1640 return -1;
1641 }
1642
1643 param_len = pj_strlen(&param->value);
1644 if (param_len >= data->len) {
1645 ast_log(LOG_ERROR, "Buffer is too small for parameter value (%zu > %zu)\n", param_len, data->len);
1646 return -1;
1647 }
1648
1649 ast_debug(2, "Successfully read %s parameter %s (length %zu)\n",
1650 data->paramtype == PARAMETER_URI ? "URI" : "header", data->param_name, param_len);
1651 if (param_len) {
1652 ast_copy_string(data->buf, pj_strbuf(&param->value), data->len);
1653 }
1654 data->buf[param_len] = '\0';
1655
1656 return 0;
1657}

References ast_copy_string(), ast_debug, ast_log, param_data::buf, param_data::channel, param_data::len, LOG_ERROR, param_data::param_name, PARAMETER_URI, param_data::paramtype, ast_sip_channel_pvt::session, and session.

Referenced by func_read_param().

◆ remove_header()

static int remove_header ( void *  obj)
static

Definition at line 1015 of file res_pjsip_header_funcs.c.

1016{
1017 struct header_data *data = obj;
1018 size_t len = strlen(data->header_name);
1019 struct hdr_list *list;
1020 struct hdr_list_entry *le;
1021 int removed_count = 0;
1022 RAII_VAR(struct ast_datastore *, datastore,
1024 ao2_cleanup);
1025
1026 ast_debug(3, "Attempting to remove header %s from channel %p\n", data->header_name,
1027 data->channel->session->channel ? ast_channel_name(data->channel->session->channel) : "(none)");
1028
1029 if (!datastore || !datastore->data) {
1030 ast_log(AST_LOG_ERROR, "No headers had been previously added to this session.\n");
1031 return -1;
1032 }
1033
1034 list = datastore->data;
1036 if (len >= 1 && data->header_name[len - 1] == '*') {
1037 if (pj_strnicmp2(&le->hdr->name, data->header_name, len - 1) == 0) {
1038 ast_debug(3, "Found wildcard match, removing header %.*s from channel %p\n",
1039 (int)le->hdr->name.slen, le->hdr->name.ptr,
1040 data->channel->session->channel ? ast_channel_name(data->channel->session->channel) : "(none)");
1042 removed_count++;
1043 }
1044 } else {
1045 if (pj_stricmp2(&le->hdr->name, data->header_name) == 0) {
1046 ast_debug(3, "Found exact match, removing header %.*s from channel %p\n",
1047 (int)le->hdr->name.slen, le->hdr->name.ptr,
1048 data->channel->session->channel ? ast_channel_name(data->channel->session->channel) : "(none)");
1050 removed_count++;
1051 }
1052 }
1053 }
1055
1056 if (removed_count == 0) {
1057 ast_debug(3, "No headers named %s found to remove on channel %s.\n", data->header_name,
1058 data->channel->session->channel ? ast_channel_name(data->channel->session->channel) : "(none)");
1059 }
1060
1061 if (data->buf && data->len) {
1062 snprintf(data->buf, data->len, "%d", removed_count);
1063 ast_debug(3, "Removed %d headers matching %s from channel %s\n", removed_count, data->header_name,
1064 data->channel->session->channel ? ast_channel_name(data->channel->session->channel) : "(none)");
1065 }
1066
1067 return 0;
1068}
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
struct ast_channel * channel

References ao2_cleanup, ast_channel_name(), ast_debug, AST_LIST_REMOVE_CURRENT, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, ast_log, AST_LOG_ERROR, ast_sip_session_get_datastore(), header_data::buf, ast_sip_session::channel, header_data::channel, hdr_list_entry::hdr, header_data::header_name, len(), header_data::len, hdr_list_entry::nextptr, RAII_VAR, ast_sip_channel_pvt::session, session_header_datastore, and ast_datastore_info::type.

Referenced by func_read_header(), and func_write_header().

◆ remove_inheritable_header()

static int remove_inheritable_header ( struct ast_channel channel,
const char *  header_name 
)
static

Definition at line 1079 of file res_pjsip_header_funcs.c.

1080{
1081 size_t len = strlen(header_name);
1082 struct ast_variable *headers = NULL;
1083 struct ast_variable *var, *prev = NULL;
1084 int removed_count = 0;
1085 struct ast_datastore *inherited_datastore;
1086
1087 ast_channel_lock(channel);
1088 inherited_datastore = ast_channel_datastore_find(channel, &inheritable_header_datastore, NULL);
1089 ast_debug(3, "Attempting to remove header %s from channel %s\n",
1090 header_name, ast_channel_name(channel));
1091
1092 /* Remove from inherited datastore */
1093 if (!inherited_datastore || !inherited_datastore->data) {
1094 ast_log(AST_LOG_ERROR, "No inheritable headers had been previously added to channel %s.\n",
1095 ast_channel_name(channel));
1096 ast_channel_unlock(channel);
1097 return -1;
1098 }
1099
1100 headers = inherited_datastore->data;
1101
1102 /* If removing all headers */
1103 if (strcmp(header_name, "*") == 0) {
1105 inherited_datastore->data = NULL;
1106 ast_debug(3, "Removed all inheritable headers from channel %s\n",
1107 ast_channel_name(channel));
1108 ast_channel_unlock(channel);
1109 return 0;
1110 }
1111
1112 /* Remove specific headers (exact match or wildcard) */
1113 var = headers;
1114 while (var) {
1115 struct ast_variable *next = var->next;
1116 int match = 0;
1117
1118 if (len >= 1 && header_name[len - 1] == '*') {
1119 /* Wildcard match */
1120 match = (strncasecmp(var->name, header_name, len - 1) == 0);
1121 if (match) {
1122 ast_debug(3, "Found wildcard match, removing header %s with value %s from channel %s\n",
1123 var->name, var->value, ast_channel_name(channel));
1124 }
1125 } else {
1126 /* Exact match */
1127 match = (strcasecmp(var->name, header_name) == 0);
1128 if (match) {
1129 ast_debug(3, "Found exact match, removing header %s with value %s from channel %s\n",
1130 var->name, var->value, ast_channel_name(channel));
1131 }
1132 }
1133
1134 if (match) {
1135 /* Remove this variable */
1136 if (var == headers) {
1137 /* If we're removing the first header in the list, we need to update the head pointer */
1138 headers = next;
1139 inherited_datastore->data = headers;
1140 } else if (prev) {
1141 /* Otherwise, we need to link the previous header to the next header */
1142 prev->next = next;
1143 }
1144
1145 /* destroy the variable */
1146 var->next = NULL;
1148 removed_count++;
1149 var = next;
1150 } else {
1151 prev = var;
1152 var = next;
1153 }
1154 }
1155
1156 /* Update datastore with new list head */
1157 inherited_datastore->data = headers;
1158
1159 if (removed_count == 0) {
1160 ast_debug(3, "No headers matching %s found for channel %s\n",
1161 header_name, ast_channel_name(channel));
1162 }
1163
1164 ast_debug(3, "Removed %d headers matching %s from channel %s\n", removed_count, header_name,
1165 ast_channel_name(channel));
1166
1167 ast_channel_unlock(channel);
1168 return 0;
1169}
static int match(struct ast_sockaddr *addr, unsigned short callno, unsigned short dcallno, const struct chan_iax2_pvt *cur, int check_dcallno)
Definition chan_iax2.c:2388
static void inheritable_headers_destroy(void *data)

References ast_channel_datastore_find(), ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_debug, ast_log, AST_LOG_ERROR, ast_variables_destroy(), ast_datastore::data, inheritable_header_datastore, inheritable_headers_destroy(), len(), match(), ast_variable::next, NULL, and var.

Referenced by func_write_inheritable_header().

◆ unload_module()

static int unload_module ( void  )
static

◆ update_header()

static int update_header ( void *  obj)
static

Definition at line 881 of file res_pjsip_header_funcs.c.

882{
883 struct header_data *data = obj;
884 pj_pool_t *pool = data->channel->session->inv_session->dlg->pool;
885 pjsip_hdr *hdr = NULL;
886 RAII_VAR(struct ast_datastore *, datastore,
889
890 ast_debug(3, "Attempting to update header %s for channel %p\n", data->header_name,
891 data->channel->session->channel ? ast_channel_name(data->channel->session->channel) : "(none)");
892
893 if (!datastore || !datastore->data) {
894 ast_log(AST_LOG_ERROR, "No headers had been previously added to this session.\n");
895 return -1;
896 }
897
898 hdr = find_header((struct hdr_list *) datastore->data, data->header_name,
899 data->header_number);
900
901 if (!hdr) {
902 ast_log(AST_LOG_ERROR, "There was no header named %s.\n", data->header_name);
903 return -1;
904 }
905
906 ast_debug(3, "Found header %s in session datastore, updating value to %s for channel %p\n",
907 data->header_name, data->header_value,
908 data->channel->session->channel ? ast_channel_name(data->channel->session->channel) : "(none)");
909 pj_strdup2(pool, &((pjsip_generic_string_hdr *) hdr)->hvalue, data->header_value);
910
911 return 0;
912}
static pjsip_hdr * find_header(struct hdr_list *list, const char *header_name, int header_number)
struct pjsip_inv_session * inv_session

References ao2_cleanup, ast_channel_name(), ast_debug, ast_log, AST_LOG_ERROR, ast_sip_session_get_datastore(), ast_sip_session::channel, header_data::channel, find_header(), header_data::header_name, header_data::header_number, header_data::header_value, ast_sip_session::inv_session, NULL, RAII_VAR, ast_sip_channel_pvt::session, session_header_datastore, and ast_datastore_info::type.

Referenced by func_write_header().

◆ update_inheritable_header()

static int update_inheritable_header ( struct ast_channel channel,
const char *  header_name,
const char *  header_value,
int  header_number 
)
static

Definition at line 925 of file res_pjsip_header_funcs.c.

926{
927 struct ast_datastore *inherited_datastore;
928 struct ast_variable *headers = NULL;
929 struct ast_variable *var, *prev = NULL;
930 int i = 1, num_matching_headers = 0, target_header_index;
931
932 ast_channel_lock(channel);
933 inherited_datastore = ast_channel_datastore_find(channel, &inheritable_header_datastore, NULL);
934 ast_debug(3, "Attempting to update header %s for channel %s\n", header_name,
935 ast_channel_name(channel));
936
937 if (!inherited_datastore || !inherited_datastore->data) {
938 ast_log(AST_LOG_ERROR, "No inheritable headers had been previously added to this channel.\n");
939 ast_channel_unlock(channel);
940 return -1;
941 }
942
943 headers = inherited_datastore->data;
944
945 /* The headers are in reverse order so we need to find out how many there are to know which one to update */
946 for (var = headers; var; var = var->next) {
947 if (strcasecmp(var->name, header_name) == 0) {
948 num_matching_headers++;
949 }
950 }
951
952 /* As we already counted the matching headers, we can skip walking the list again if the count is 0 or
953 is less than the requested header number */
954 if (num_matching_headers == 0) {
955 ast_log(AST_LOG_ERROR, "There was no header named %s on channel %s.\n", header_name,
956 ast_channel_name(channel));
957 ast_channel_unlock(channel);
958 return -1;
959 } else if (num_matching_headers < header_number) {
960 ast_log(AST_LOG_ERROR, "There were only %d headers named %s on channel %s, cannot update header number %d.\n",
961 num_matching_headers, header_name, ast_channel_name(channel), header_number);
962 ast_channel_unlock(channel);
963 return -1;
964 }
965
966 target_header_index = num_matching_headers - header_number + 1;
967 /* Search for the header in the inherited list */
968 for (var = headers; var; var = var->next) {
969 if (strcasecmp(var->name, header_name) == 0 && i++ == target_header_index) {
970 /* Found the header, create a new one with the new value and free the old */
971 struct ast_variable *new_var = ast_variable_new(header_name, header_value, "");
972 if (!new_var) {
973 ast_log(AST_LOG_ERROR, "Failed to allocate variable for updated header for channel %s\n",
974 ast_channel_name(channel));
975 ast_channel_unlock(channel);
976 return -1;
977 }
978
979 new_var->next = var->next;
980
981 if (var == headers) {
982 /* If we're updating the first header in the list, we need to update the head pointer */
983 inherited_datastore->data = new_var;
984 } else if (prev) {
985 /* Otherwise, we need to link the previous header to the new header */
986 prev->next = new_var;
987 }
988
989 var->next = NULL;
991
992 ast_debug(3, "Updated header %s with new value %s for channel %s\n",
993 header_name, header_value, ast_channel_name(channel));
994 ast_channel_unlock(channel);
995 return 0;
996 }
997 prev = var;
998 }
999
1000 /* We should never get here*/
1001 ast_log(AST_LOG_ERROR, "Unable to update header %s on channel %s.\n", header_name,
1002 ast_channel_name(channel));
1003 ast_channel_unlock(channel);
1004 return -1;
1005}

References ast_channel_datastore_find(), ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_debug, ast_log, AST_LOG_ERROR, ast_variable_new, ast_datastore::data, inheritable_header_datastore, inheritable_headers_destroy(), ast_variable::next, NULL, and var.

Referenced by func_write_inheritable_header().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Header Functions" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_APP_DEPEND, .requires = "res_pjsip,res_pjsip_session", }
static

Definition at line 1847 of file res_pjsip_header_funcs.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 1847 of file res_pjsip_header_funcs.c.

◆ header_funcs_supplement

struct ast_sip_session_supplement header_funcs_supplement
static

Definition at line 1589 of file res_pjsip_header_funcs.c.

1589 {
1590 .method = "INVITE",
1591 .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL - 1000,
1592 .incoming_request = incoming_request,
1593 .outgoing_request = outgoing_request,
1594 .incoming_response = incoming_response,
1595};
@ AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL
Definition res_pjsip.h:3364
static void incoming_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
static int incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
static void outgoing_request(struct ast_sip_session *session, pjsip_tx_data *tdata)

Referenced by load_module(), and unload_module().

◆ inheritable_header_datastore

const struct ast_datastore_info inheritable_header_datastore
static
Initial value:
= {
.type = "inheritable_header_datastore",
}

Datastore for saving inheritable headers (contains hdr_list with ast_malloc'd entries)

Definition at line 441 of file res_pjsip_header_funcs.c.

441 {
442 .type = "inheritable_header_datastore",
445};

Referenced by add_inheritable_header(), outgoing_request(), remove_inheritable_header(), and update_inheritable_header().

◆ pjsip_header_function

struct ast_custom_function pjsip_header_function
static
Initial value:
= {
.name = "PJSIP_HEADER",
}
static int func_read_header(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
Implements PJSIP_HEADER function 'read' callback.
static int func_write_header(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Implements PJSIP_HEADER function 'write' callback.

Definition at line 1460 of file res_pjsip_header_funcs.c.

1460 {
1461 .name = "PJSIP_HEADER",
1462 .read = func_read_header,
1463 .write = func_write_header,
1464};

Referenced by load_module(), and unload_module().

◆ pjsip_header_inherit_function

struct ast_custom_function pjsip_header_inherit_function
static
Initial value:
= {
.name = "PJSIP_INHERITABLE_HEADER",
}
static int func_write_inheritable_header(struct ast_channel *chan, const char *cmd, char *data, const char *value)
Implements PJSIP_INHERITABLE_HEADER function 'write' callback.

Definition at line 1466 of file res_pjsip_header_funcs.c.

1466 {
1467 .name = "PJSIP_INHERITABLE_HEADER",
1469};

Referenced by load_module(), and unload_module().

◆ pjsip_header_param_function

struct ast_custom_function pjsip_header_param_function
static
Initial value:
= {
.name = "PJSIP_HEADER_PARAM",
.read = func_read_param,
.write = func_write_param,
}
static int func_write_param(struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int func_read_param(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)

Definition at line 1810 of file res_pjsip_header_funcs.c.

1810 {
1811 .name = "PJSIP_HEADER_PARAM",
1812 .read = func_read_param,
1813 .write = func_write_param,
1814};

Referenced by load_module(), and unload_module().

◆ pjsip_headers_function

struct ast_custom_function pjsip_headers_function
static
Initial value:
= {
.name = "PJSIP_HEADERS",
}
static int func_read_headers(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
Read list of unique SIP headers.

Definition at line 1471 of file res_pjsip_header_funcs.c.

1471 {
1472 .name = "PJSIP_HEADERS",
1473 .read = func_read_headers
1474};

Referenced by load_module(), and unload_module().

◆ pjsip_response_header_function

struct ast_custom_function pjsip_response_header_function
static
Initial value:
= {
.name = "PJSIP_RESPONSE_HEADER",
}
static int func_response_read_header(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
Implements PJSIP_RESPONSE_HEADER function 'read' callback.

Definition at line 1476 of file res_pjsip_header_funcs.c.

1476 {
1477 .name = "PJSIP_RESPONSE_HEADER",
1479};

Referenced by load_module(), and unload_module().

◆ pjsip_response_headers_function

struct ast_custom_function pjsip_response_headers_function
static
Initial value:
= {
.name = "PJSIP_RESPONSE_HEADERS",
}
static int func_response_read_headers(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
Read list of unique SIP response headers.

Definition at line 1481 of file res_pjsip_header_funcs.c.

1481 {
1482 .name = "PJSIP_RESPONSE_HEADERS",
1484};

Referenced by load_module(), and unload_module().

◆ response_header_datastore

const struct ast_datastore_info response_header_datastore
static
Initial value:
= {
.type = "response_header_datastore",
}

Datastore for saving response headers.

Definition at line 448 of file res_pjsip_header_funcs.c.

448 {
449 .type = "response_header_datastore",
450};

Referenced by func_response_read_header(), func_response_read_headers(), and incoming_response().

◆ session_header_datastore

const struct ast_datastore_info session_header_datastore
static
Initial value:
= {
.type = "session_header_datastore",
}

Datastore for saving headers in session (contains hdr_list, no destroy needed)

Definition at line 435 of file res_pjsip_header_funcs.c.

435 {
436 .type = "session_header_datastore",
437 /* No destroy or duplicate callback - data is pool-allocated hdr_list */
438};

Referenced by add_header(), func_read_header(), func_read_headers(), func_write_header(), incoming_request(), outgoing_request(), remove_header(), and update_header().