--- chan_pjsip.c 2016-07-07 13:41:14.042564655 +0200 +++ chan_pjsip_modified.c 2016-07-07 13:44:07.418564655 +0200 @@ -1690,25 +1690,83 @@ ast_channel_queue_connected_line_update(session->channel, &connected, NULL); } +/*! + * \brief Set a custom SIP header + * + * This sets a custom header and adds it to an outgoing SIP request. If the header already exists on the message, + * then no action is taken. + * + * \pre chan is locked. + * + * \param header_name The name of the SIP header to add to the outgoing message. + * \param tdata The outgoing SIP message on which to add the header + */ +static void add_header_custom(const char *header_name, const char *header_value, pjsip_tx_data *tdata) +{ + pj_str_t pj_header_name; + pjsip_hdr *header; + + pj_cstr(&pj_header_name, header_name); + header = pjsip_msg_find_hdr_by_name(tdata->msg, &pj_header_name, NULL); + if (header) { + return; + } + ast_sip_add_header(tdata, header_name, header_value); +} + static int call(void *data) { - struct ast_sip_channel_pvt *channel = data; - struct ast_sip_session *session = channel->session; - struct chan_pjsip_pvt *pvt = channel->pvt; - pjsip_tx_data *tdata; - - int res = ast_sip_session_create_invite(session, &tdata); - - if (res) { - ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0); - ast_queue_hangup(session->channel); - } else { - set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(session->channel)); - update_initial_connected_line(session); - ast_sip_session_send_request(session, tdata); - } - ao2_ref(channel, -1); - return res; + struct ast_sip_channel_pvt *channel = data; + struct ast_sip_session *session = channel->session; + struct chan_pjsip_pvt *pvt = channel->pvt; + pjsip_tx_data *tdata; + + struct varshead *headp; + struct ast_var_t *current; + + int res = ast_sip_session_create_invite(session, &tdata); + + if (res) { + ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0); + ast_queue_hangup(session->channel); + } else { + //Check if there are any variables that need to be set before initiating the call + ast_channel_lock(session->channel); + + headp = ast_channel_varshead(session->channel); + AST_LIST_TRAVERSE(headp, current, entries) { + /* PJSIPADDHEADER: Add SIP header to outgoing call */ + if (!strncmp(ast_var_name(current), "PJSIPADDHEADER", strlen("PJSIPADDHEADER"))) { + char *content, *end; + const char *header = ast_var_value(current); + char *headdup = ast_strdupa(header); + /* Strip off the starting " (if it's there) */ + if (*headdup == '"') { + headdup++; + } + if ((content = strchr(headdup, ':'))) { + *content++ = '\0'; + content = ast_skip_blanks(content); /* Skip white space */ + /* Strip the ending " (if it's there) */ + end = content + strlen(content) -1; + if (*end == '"') { + *end = '\0'; + } + add_header_custom(headdup, content, tdata); + ast_debug(1, "Adding SIP Header \"%s\" with content %s \n", headdup, content); + } + } + } + + ast_channel_unlock(session->channel); + + + set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(session->channel)); + update_initial_connected_line(session); + ast_sip_session_send_request(session, tdata); + } + ao2_ref(channel, -1); + return res; } /*! \brief Function called by core to actually start calling a remote party */