Asterisk - The Open Source Telephony Project GIT-master-f36a736
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_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. More...
 
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. More...
 
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. More...
 
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. More...
 
static int func_write_header (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 Implements PJSIP_HEADER function 'write' callback. More...
 
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 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 unload_module (void)
 
static int update_header (void *obj)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Header Functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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 const struct ast_datastore_info header_datastore
 Datastore for saving headers. More...
 
static struct ast_sip_session_supplement header_funcs_supplement
 
static struct ast_custom_function pjsip_header_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. More...
 

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 1047 of file res_pjsip_header_funcs.c.

1047 {
1050};
@ PARAMETER_HEADER

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 1295 of file res_pjsip_header_funcs.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 1295 of file res_pjsip_header_funcs.c.

◆ add_header()

static int add_header ( void *  obj)
static

Definition at line 627 of file res_pjsip_header_funcs.c.

628{
629 struct header_data *data = obj;
630 struct ast_sip_session *session = data->channel->session;
631 pj_pool_t *pool = session->inv_session->dlg->pool;
632 pj_str_t pj_header_name;
633 pj_str_t pj_header_value;
634 struct hdr_list_entry *le;
635 struct hdr_list *list;
636
637 RAII_VAR(struct ast_datastore *, datastore,
639
640 if (!datastore) {
642 data->header_datastore->type))
643 || !(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list)))
644 || ast_sip_session_add_datastore(session, datastore)) {
645 ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
646 return -1;
647 }
648 AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
649 }
650
651 ast_debug(1, "Adding header %s with value %s\n", data->header_name,
652 data->header_value);
653
654 pj_cstr(&pj_header_name, data->header_name);
655 pj_cstr(&pj_header_value, data->header_value);
656 le = pj_pool_zalloc(pool, sizeof(struct hdr_list_entry));
657 le->hdr = (pjsip_hdr *) pjsip_generic_string_hdr_create(pool, &pj_header_name,
658 &pj_header_value);
659 list = datastore->data;
660
661 AST_LIST_INSERT_TAIL(list, le, nextptr);
662
663 return 0;
664}
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.
Definition: linkedlists.h:681
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
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 struct ast_datastore_info * header_datastore
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:941

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_datastore, header_data::header_name, header_data::header_value, RAII_VAR, ast_sip_channel_pvt::session, session, and ast_datastore_info::type.

Referenced by func_write_header().

◆ add_param()

static int add_param ( void *  obj)
static

Definition at line 1115 of file res_pjsip_header_funcs.c.

1116{
1117 struct param_data *data = obj;
1118 struct ast_sip_session *session = data->channel->session;
1119 pj_pool_t *pool = session->inv_session->dlg->pool;
1120
1121 pjsip_fromto_hdr *dlg_info;
1122 pjsip_name_addr *dlg_info_name_addr;
1123 pjsip_sip_uri *dlg_info_uri;
1124
1125 dlg_info = session->inv_session->dlg->local.info; /* Local for outgoing */
1126 dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
1127 dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
1128 if (!PJSIP_URI_SCHEME_IS_SIP(dlg_info_uri) && !PJSIP_URI_SCHEME_IS_SIPS(dlg_info_uri)) {
1129 ast_log(LOG_WARNING, "Non SIP/SIPS URI\n");
1130 return -1;
1131 }
1132
1133 ast_debug(1, "Adding custom %s param %s = %s\n",
1134 data->paramtype == PARAMETER_URI ? "URI" : "header", data->param_name, data->param_value);
1135
1136 /* This works the same as doing this in set_from_header in res_pjsip_session.c
1137 * The way that this maps to pjproject is a little confusing.
1138 * Say we have <sip:foo@bar.com;p1=abc;p2=def?h1=qrs&h2=tuv>;o1=foo;o2=bar
1139 * p1 and p2 are URI parameters.
1140 * (h1 and h2 are URI headers)
1141 * o1 and o2 are header parameters (and don't have anything to do with the URI)
1142 * In pjproject, other_param is used for adding all custom parameters.
1143 * We use the URI for URI stuff, including URI parameters, and the header directly for header parameters.
1144 */
1145
1146#define param_add(pool, list, pname, pvalue) { \
1147 pjsip_param *param; \
1148 param = PJ_POOL_ALLOC_T(pool, pjsip_param); \
1149 pj_strdup2(pool, &param->name, pname); \
1150 pj_strdup2(pool, &param->value, pvalue); \
1151 pj_list_insert_before(list, param); \
1152}
1153
1154 if (data->paramtype == PARAMETER_URI) { /* URI parameter */
1155 param_add(pool, &dlg_info_uri->other_param, data->param_name, S_OR(data->param_value, ""));
1156 } else { /* Header parameter */
1157 param_add(pool, &dlg_info->other_param, data->param_name, S_OR(data->param_value, ""));
1158 }
1159
1160 return 0;
1161}
#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
enum param_type paramtype
struct ast_sip_channel_pvt * channel
const char * param_value

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 1295 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 422 of file res_pjsip_header_funcs.c.

424{
425 struct hdr_list_entry *le;
426 pjsip_hdr *hdr = NULL;
427 int i = 1;
428
429 if (!list || ast_strlen_zero(header_name) || header_number < 1) {
430 return NULL;
431 }
432
433 AST_LIST_TRAVERSE(list, le, nextptr) {
434 if (pj_stricmp2(&le->hdr->name, header_name) == 0 && i++ == header_number) {
435 hdr = le->hdr;
436 break;
437 }
438 }
439
440 return hdr;
441}
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
struct hdr_list_entry::@457 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 816 of file res_pjsip_header_funcs.c.

817{
818 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
820 int number;
822 AST_APP_ARG(action);
825
826 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
827 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
828 return -1;
829 }
830
831 if (ast_strlen_zero(args.action)) {
832 ast_log(AST_LOG_ERROR, "This function requires an action.\n");
833 return -1;
834 }
835 if (ast_strlen_zero(args.header_name)) {
836 ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
837 return -1;
838 }
839 if (!args.header_number) {
840 number = 1;
841 } else {
842 sscanf(args.header_number, "%30d", &number);
843 if (number < 1) {
844 number = 1;
845 }
846 }
847
849 header_data.header_name = args.header_name;
855
856 if (!strcasecmp(args.action, "read")) {
858 } else if (!strcasecmp(args.action, "remove")) {
861 } else {
863 "Unknown action '%s' is not valid, must be 'read' or 'remove'.\n",
864 args.action);
865 return -1;
866 }
867}
const char * ast_channel_name(const struct ast_channel *chan)
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)
int ast_sip_push_task_wait_serializer(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Push a task to the serializer and wait for it to complete.
Definition: res_pjsip.c:2179
#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.
#define LOG_ERROR
static int remove_header(void *obj)
static const struct ast_datastore_info header_datastore
Datastore for saving headers.
static int read_header(void *obj)
A structure which contains a channel implementation and session.
struct ast_taskprocessor * serializer
Number structure.
Definition: app_followme.c:154
const char * args

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_datastore, 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, and ast_sip_channel_pvt::session.

◆ 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 1163 of file res_pjsip_header_funcs.c.

1164{
1165 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
1166 struct param_data param_data;
1167
1172 );
1173
1175
1177
1178 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
1179 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
1180 return -1;
1181 }
1182 if (ast_strlen_zero(args.param_type)) {
1183 ast_log(AST_LOG_ERROR, "This function requires a parameter type.\n");
1184 return -1;
1185 }
1186 if (ast_strlen_zero(args.param_name)) {
1187 ast_log(AST_LOG_ERROR, "This function requires a parameter name.\n");
1188 return -1;
1189 }
1190
1191 /* Currently, only From is supported, but this could be extended in the future. */
1192 if (ast_strlen_zero(args.header_name) || strcasecmp(args.header_name, "From")) {
1193 ast_log(LOG_WARNING, "Only the From header is currently supported\n");
1194 return -1;
1195 }
1196
1197 param_data.param_name = args.param_name;
1198 if (!strcasecmp(args.param_type, "header")) {
1200 } else if (!strcasecmp(args.param_type, "uri")) {
1202 } else {
1203 ast_log(LOG_WARNING, "Parameter type '%s' is invalid: must be 'header' or 'uri'\n", args.param_type);
1204 return -1;
1205 }
1206
1207 param_data.buf = buf;
1208 param_data.len = len;
1209
1211}
static int read_param(void *obj)

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 874 of file res_pjsip_header_funcs.c.

875{
876 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
878 int number;
880 AST_APP_ARG(action);
883
884 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
885 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
886 return -1;
887 }
888
889 if (ast_strlen_zero(args.action)) {
890 ast_log(AST_LOG_ERROR, "This function requires an action.\n");
891 return -1;
892 }
893 if (ast_strlen_zero(args.header_name)) {
894 ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
895 return -1;
896 }
897 if (!args.header_number) {
898 number = 1;
899 } else {
900 sscanf(args.header_number, "%30d", &number);
901 if (number < 1) {
902 number = 1;
903 }
904 }
905
907 header_data.header_name = args.header_name;
913
914 if (!strcasecmp(args.action, "read")) {
916 } else {
918 "Unknown action '%s' is not valid, must be 'read'.\n",
919 args.action);
920 return -1;
921 }
922}
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 781 of file res_pjsip_header_funcs.c.

782{
783 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
786 AST_APP_ARG(header_pattern);
787 );
789
790 if (!chan || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
791 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
792 return -1;
793 }
794
795 if (ast_strlen_zero(args.header_pattern)) {
796 ast_log(AST_LOG_ERROR, "This function requires a pattern.\n");
797 return -1;
798 }
799
801 header_data.header_name = args.header_pattern;
806
808
809}

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 929 of file res_pjsip_header_funcs.c.

931{
932 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
934 int header_number;
936 AST_APP_ARG(action);
939
940 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
941 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
942 return -1;
943 }
944
945 if (ast_strlen_zero(args.action)) {
946 ast_log(AST_LOG_ERROR, "This function requires an action.\n");
947 return -1;
948 }
949 if (ast_strlen_zero(args.header_name)) {
950 ast_log(AST_LOG_ERROR, "This function requires a header name.\n");
951 return -1;
952 }
953 if (!args.header_number) {
954 header_number = 1;
955 } else {
956 sscanf(args.header_number, "%30d", &header_number);
957 if (header_number < 1) {
958 header_number = 1;
959 }
960 }
961
963 header_data.header_name = args.header_name;
967 header_data.len = 0;
969
970 if (!strcasecmp(args.action, "add")) {
973 } else if (!strcasecmp(args.action, "update")) {
976 } else if (!strcasecmp(args.action, "remove")) {
979 } else {
981 "Unknown action '%s' is not valid, must be 'add', 'update', or 'remove'.\n",
982 args.action);
983 return -1;
984 }
985}
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_datastore, 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, update_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 1213 of file res_pjsip_header_funcs.c.

1214{
1215 struct ast_sip_channel_pvt *channel = chan ? ast_channel_tech_pvt(chan) : NULL;
1216 struct param_data param_data;
1221 );
1222
1224
1226
1227 if (!channel || strncmp(ast_channel_name(chan), "PJSIP/", 6)) {
1228 ast_log(LOG_ERROR, "This function requires a PJSIP channel.\n");
1229 return -1;
1230 }
1231 if (ast_strlen_zero(args.param_type)) {
1232 ast_log(AST_LOG_ERROR, "This function requires a parameter type.\n");
1233 return -1;
1234 }
1235 if (ast_strlen_zero(args.param_name)) {
1236 ast_log(AST_LOG_ERROR, "This function requires a parameter name.\n");
1237 return -1;
1238 }
1239
1240 /* Currently, only From is supported, but this could be extended in the future. */
1241 if (ast_strlen_zero(args.header_name) || strcasecmp(args.header_name, "From")) {
1242 ast_log(LOG_WARNING, "Only the From header is currently supported\n");
1243 return -1;
1244 }
1245
1246 param_data.param_name = args.param_name;
1247 if (!strcasecmp(args.param_type, "header")) {
1249 } else if (!strcasecmp(args.param_type, "uri")) {
1251 } else {
1252 ast_log(LOG_WARNING, "Parameter type '%s' is invalid: must be 'header' or 'uri'\n", args.param_type);
1253 return -1;
1254 }
1256
1258}
static int add_param(void *obj)

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 360 of file res_pjsip_header_funcs.c.

361{
362 pj_pool_t *pool = session->inv_session->dlg->pool;
363 RAII_VAR(struct ast_datastore *, datastore,
365
366 if (!datastore) {
367 if (!(datastore =
369 ||
370 !(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list))) ||
372 ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
373 return 0;
374 }
375 AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
376 }
377 insert_headers(pool, (struct hdr_list *) datastore->data, rdata->msg_info.msg);
378
379 return 0;
380}
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(), header_datastore, insert_headers(), RAII_VAR, session, and ast_datastore_info::type.

◆ incoming_response()

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

Definition at line 390 of file res_pjsip_header_funcs.c.

391{
392 pj_pool_t *pool = session->inv_session->dlg->pool;
393 RAII_VAR(struct ast_datastore *, datastore,
395 pjsip_status_line status = rdata->msg_info.msg->line.status;
396
397 /* Skip responses different of 200 OK, when 2xx is received. */
398 if (session->inv_session->state != PJSIP_INV_STATE_CONNECTING || status.code!=200) {
399 return;
400 }
401
402 if (!datastore) {
403 if (!(datastore =
405 ||
406 !(datastore->data = pj_pool_alloc(pool, sizeof(struct hdr_list))) ||
408 ast_log(AST_LOG_ERROR, "Unable to create datastore for header functions.\n");
409 return;
410 }
411 AST_LIST_HEAD_INIT_NOLOCK((struct hdr_list *) datastore->data);
412 }
413 insert_headers(pool, (struct hdr_list *) datastore->data, rdata->msg_info.msg);
414
415 return;
416}
jack_status_t status
Definition: app_jack.c:146

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.

◆ insert_headers()

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

Definition at line 337 of file res_pjsip_header_funcs.c.

338{
339 pjsip_hdr *hdr = msg->hdr.next;
340 struct hdr_list_entry *le;
341
342 while (hdr && hdr != &msg->hdr) {
343 le = pj_pool_zalloc(pool, sizeof(struct hdr_list_entry));
344 le->hdr = pjsip_hdr_clone(pool, hdr);
345 AST_LIST_INSERT_TAIL(list, le, nextptr);
346 hdr = hdr->next;
347 }
348
349 return 0;
350}

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 1266 of file res_pjsip_header_funcs.c.

1267{
1274
1276}
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1558
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_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_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 1020 of file res_pjsip_header_funcs.c.

1021{
1022 struct hdr_list *list;
1023 struct hdr_list_entry *le;
1024 RAII_VAR(struct ast_datastore *, datastore,
1026
1027 if (!datastore || !datastore->data ||
1028 (session->inv_session->state >= PJSIP_INV_STATE_CONFIRMED)) {
1029 return;
1030 }
1031
1032 list = datastore->data;
1033 AST_LIST_TRAVERSE(list, le, nextptr) {
1034 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr *) pjsip_hdr_clone(tdata->pool, le->hdr));
1035 }
1037}
void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
Remove a session datastore from the session.

References ao2_cleanup, AST_LIST_TRAVERSE, ast_sip_session_get_datastore(), ast_sip_session_remove_datastore(), hdr_list_entry::hdr, header_datastore, hdr_list_entry::nextptr, RAII_VAR, session, and ast_datastore_info::type.

◆ read_header()

static int read_header ( void *  obj)
static

Definition at line 544 of file res_pjsip_header_funcs.c.

545{
546 struct header_data *data = obj;
547 size_t len = strlen(data->header_name);
548 pjsip_hdr *hdr = NULL;
549 char *pj_hdr_string;
550 int pj_hdr_string_len;
551 char *p;
552 size_t plen;
553 struct hdr_list_entry *le;
554 struct hdr_list *list;
555 int i = 1;
556 RAII_VAR(struct ast_datastore *, datastore,
559
560 if (!datastore || !datastore->data) {
561 ast_debug(1, "There was no datastore from which to read headers.\n");
562 return -1;
563 }
564
565 list = datastore->data;
566 AST_LIST_TRAVERSE(list, le, nextptr) {
567 if (data->header_name[len - 1] == '*') {
568 if (pj_strnicmp2(&le->hdr->name, data->header_name, len - 1) == 0 && i++ == data->header_number) {
569 hdr = le->hdr;
570 break;
571 }
572 } else {
573 if (pj_stricmp2(&le->hdr->name, data->header_name) == 0 && i++ == data->header_number) {
574 hdr = le->hdr;
575 break;
576 }
577 }
578 }
579
580 if (!hdr) {
581 ast_debug(1, "There was no header named %s.\n", data->header_name);
582 return -1;
583 }
584
585 pj_hdr_string = ast_alloca(data->len);
586 pj_hdr_string_len = pjsip_hdr_print_on(hdr, pj_hdr_string, data->len - 1);
587 if (pj_hdr_string_len == -1) {
589 "Not enough buffer space in pjsip_hdr_print_on\n");
590 return -1;
591 }
592
593 pj_hdr_string[pj_hdr_string_len] = '\0';
594
595 p = strchr(pj_hdr_string, ':');
596 if (!p) {
598 "A malformed header was returned from pjsip_hdr_print_on.\n");
599 return -1;
600 }
601
602 ++p;
603 p = ast_strip(p);
604 plen = strlen(p);
605 if (plen + 1 > data->len) {
607 "Buffer isn't big enough to hold header value. %zu > %zu\n", plen + 1,
608 data->len);
609 return -1;
610 }
611
612 ast_copy_string(data->buf, p, data->len);
613
614 return 0;
615}
#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 453 of file res_pjsip_header_funcs.c.

454{
455 struct header_data *data = obj;
456 size_t len = !ast_strlen_zero(data->header_name) ? strlen(data->header_name) : 0;
457 pjsip_hdr *hdr = NULL;
458 char *pj_hdr_string;
459 int pj_hdr_string_len;
460 char *p;
461 char *pos;
462 size_t plen, wlen = 0;
463 struct hdr_list_entry *le;
464 struct hdr_list *list;
465
466 RAII_VAR(struct ast_datastore *, datastore,
469
470 if (!datastore || !datastore->data) {
471 ast_debug(1, "There was no datastore from which to read headers.\n");
472 return -1;
473 }
474
475 list = datastore->data;
476 pj_hdr_string = ast_alloca(data->len);
477 AST_LIST_TRAVERSE(list, le, nextptr) {
478 if (!len || pj_strnicmp2(&le->hdr->name, data->header_name, len) == 0) {
479 /* Found matched header, append to buf */
480 hdr = le->hdr;
481
482 pj_hdr_string_len = pjsip_hdr_print_on(hdr, pj_hdr_string, data->len - 1);
483 if (pj_hdr_string_len == -1) {
485 "Not enough buffer space in pjsip_hdr_print_on\n");
486 return -1;
487 }
488 pj_hdr_string[pj_hdr_string_len] = '\0';
489 p = strchr(pj_hdr_string, ':');
490 if (!p) {
492 "A malformed header was returned from pjsip_hdr_print_on\n");
493 continue;
494 }
495
496 pj_hdr_string[p - pj_hdr_string] = '\0';
497 p = ast_strip(pj_hdr_string);
498 plen = strlen(p);
499 if (wlen + plen + 1 > data->len) {
501 "Buffer isn't big enough to hold header value. %zu > %zu\n", plen + 1,
502 data->len);
503 return -1;
504 }
505 pos = strstr(data->buf, p);
506 if (pos && pos[1] == ',') {
507 if (pos == data->buf) {
508 continue;
509 } else if (pos[-1] == ',') {
510 continue;
511 }
512 }
513 ast_copy_string(data->buf + wlen, p, data->len - wlen);
514 wlen += plen;
515 ast_copy_string(data->buf + wlen, ",", data->len - wlen);
516 wlen++;
517 }
518 }
519
520 if (wlen == 0) {
521 if (!len) {
522 /* No headers at all on this channel */
523 return 0;
524 } else {
525 ast_debug(1, "There was no header beginning with %s.\n", data->header_name);
526 return -1;
527 }
528 } else {
529 data->buf[wlen-1] = '\0';
530 }
531 return 0;
532}
#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 1063 of file res_pjsip_header_funcs.c.

1064{
1065 struct param_data *data = obj;
1066 struct ast_sip_session *session = data->channel->session;
1067 pj_str_t param_name;
1068
1069 pjsip_fromto_hdr *dlg_info;
1070 pjsip_name_addr *dlg_info_name_addr;
1071 pjsip_sip_uri *dlg_info_uri;
1072 pjsip_param *param;
1073 size_t param_len;
1074
1075 dlg_info = session->inv_session->dlg->remote.info; /* Remote dialog for incoming */
1076 dlg_info_name_addr = (pjsip_name_addr *) dlg_info->uri;
1077 dlg_info_uri = pjsip_uri_get_uri(dlg_info_name_addr);
1078
1079 pj_cstr(&param_name, data->param_name);
1080
1081 if (data->paramtype == PARAMETER_URI) { /* URI parameter */
1082 param = pjsip_param_find(&dlg_info_uri->other_param, &param_name);
1083 } else { /* Header parameter */
1084 param = pjsip_param_find(&dlg_info->other_param, &param_name);
1085 }
1086
1087 if (!param) {
1088 ast_debug(1, "No %s parameter found named %s\n",
1089 data->paramtype == PARAMETER_URI ? "URI" : "header", data->param_name);
1090 return -1;
1091 }
1092
1093 param_len = pj_strlen(&param->value);
1094 if (param_len >= data->len) {
1095 ast_log(LOG_ERROR, "Buffer is too small for parameter value (%zu > %zu)\n", param_len, data->len);
1096 return -1;
1097 }
1098
1099 ast_debug(2, "Successfully read %s parameter %s (length %zu)\n",
1100 data->paramtype == PARAMETER_URI ? "URI" : "header", data->param_name, param_len);
1101 if (param_len) {
1102 ast_copy_string(data->buf, pj_strbuf(&param->value), data->len);
1103 }
1104 data->buf[param_len] = '\0';
1105
1106 return 0;
1107}

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 711 of file res_pjsip_header_funcs.c.

712{
713 struct header_data *data = obj;
714 size_t len = strlen(data->header_name);
715 struct hdr_list *list;
716 struct hdr_list_entry *le;
717 int removed_count = 0;
718 RAII_VAR(struct ast_datastore *, datastore,
721
722 if (!datastore || !datastore->data) {
723 ast_log(AST_LOG_ERROR, "No headers had been previously added to this session.\n");
724 return -1;
725 }
726
727 list = datastore->data;
729 if (data->header_name[len - 1] == '*') {
730 if (pj_strnicmp2(&le->hdr->name, data->header_name, len - 1) == 0) {
732 removed_count++;
733 }
734 } else {
735 if (pj_stricmp2(&le->hdr->name, data->header_name) == 0) {
737 removed_count++;
738 }
739 }
740 }
742
743 if (data->buf && data->len) {
744 snprintf(data->buf, data->len, "%d", removed_count);
745 }
746
747 return 0;
748}
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557

References ao2_cleanup, 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, header_data::channel, hdr_list_entry::hdr, header_data::header_datastore, header_data::header_name, len(), header_data::len, hdr_list_entry::nextptr, RAII_VAR, ast_sip_channel_pvt::session, and ast_datastore_info::type.

Referenced by func_read_header(), and func_write_header().

◆ unload_module()

static int unload_module ( void  )
static

◆ update_header()

static int update_header ( void *  obj)
static

Definition at line 676 of file res_pjsip_header_funcs.c.

677{
678 struct header_data *data = obj;
679 pj_pool_t *pool = data->channel->session->inv_session->dlg->pool;
680 pjsip_hdr *hdr = NULL;
681 RAII_VAR(struct ast_datastore *, datastore,
684
685 if (!datastore || !datastore->data) {
686 ast_log(AST_LOG_ERROR, "No headers had been previously added to this session.\n");
687 return -1;
688 }
689
690 hdr = find_header((struct hdr_list *) datastore->data, data->header_name,
691 data->header_number);
692
693 if (!hdr) {
694 ast_log(AST_LOG_ERROR, "There was no header named %s.\n", data->header_name);
695 return -1;
696 }
697
698 pj_strdup2(pool, &((pjsip_generic_string_hdr *) hdr)->hvalue, data->header_value);
699
700 return 0;
701}
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_log, AST_LOG_ERROR, ast_sip_session_get_datastore(), header_data::channel, find_header(), header_data::header_datastore, 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, and ast_datastore_info::type.

Referenced by func_write_header().

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "PJSIP Header Functions" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .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 1295 of file res_pjsip_header_funcs.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 1295 of file res_pjsip_header_funcs.c.

◆ header_datastore

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

Datastore for saving headers.

Definition at line 311 of file res_pjsip_header_funcs.c.

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

◆ header_funcs_supplement

struct ast_sip_session_supplement header_funcs_supplement
static

Definition at line 1039 of file res_pjsip_header_funcs.c.

Referenced by load_module(), and unload_module().

◆ 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 987 of file res_pjsip_header_funcs.c.

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 1260 of file res_pjsip_header_funcs.c.

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 993 of file res_pjsip_header_funcs.c.

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 998 of file res_pjsip_header_funcs.c.

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 1003 of file res_pjsip_header_funcs.c.

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 315 of file res_pjsip_header_funcs.c.

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