Review Board 1.7.16


Support text messages outside of a call

Review Request #1042 - Created Dec. 1, 2010 and submitted

Russell Bryant
trunk
Reviewers
asterisk-dev
Asterisk
This branch contains a proposal for adding protocol independent support for processing text messages into and out of the dialplan, outside of a call.  The file doc/asterisk-messaging.txt contains more details on the proposal.  The introduction of the document is quoted here:

"    Asterisk has some limited support today for messaging.  The support that
exists primarily includes passing text messages in the context of a call.  The
SIP and IAX2 protocols have support for this, but that's it.

    There are a couple of other messaging protocols that are supported: Skype
and XMPP (Jabber).  The support of these is very minimal and not very integrated
into the architecture of Asterisk since these messages are not in the context of
a phone call.  They provide a combination of dialplan and manager interface
interfaces that are specific to each protocol.  There just is no current
architectural concept of dealing with text messages.

    The purpose of this proposal is to introduce text messaging into the
architecture of Asterisk.  For messaging support to exist in the true spirit of
Asterisk architecture, the design needs to achieve the following two goals:

    a) Protocol Independence
    b) Scriptable message routing

    The rest of this document goes through some details about how these goals
will be achieved in a way that is both architecturally compatible with Asterisk
as well as practical to implement."

----------

In addition to the documented proposal, I have made some good progress on implementation.  While the document includes some ideas for future enhancements, what is there so far should be usable.

 - core modifications to allow sending incoming messages through the dialplan
 - core modifications to allow outbound messages from the dialplan
 - modifications to res_jabber to allow inbound and outbound messages in the new architecture
 - changes to chan_sip to support inbound and outbound MESSAGE outside of a call
svn/testsuite/asterisk/team/russell/messaging:
  - This branch of the testsuite contains my tests for this branch, which include:
    - tests/sip/message_disabled
      - Ensure MESSAGE outside of a call is rejected when disabled.
    - tests/sip/message_unauth
      - When enabled, test sending a MESSAGE to Asterisk and send another back out from the dialplan.
    - tests/sip/message_auth
      - Same as the last test, but authenticate MESSAGE both inbound and outbound.
    - tests/sip/message_from_call
      - Set up a normal SIP call and send an out of call MESSAGE from the dialplan processing the call

I have also written some simple apps using the pjsua Python module from pjsip that can send and receive messages sent through Asterisk.

Lastly, I have done some manual testing of XMPP messages in and out of Asterisk using this code.
/trunk/channels/chan_sip.c
Diff Revision 3 Diff Revision 4
[20] 427 lines
[+20] [+] ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
428
						<para>Preferred codec index number <replaceable>x</replaceable> (beginning with zero).</para>
428
						<para>Preferred codec index number <replaceable>x</replaceable> (beginning with zero).</para>
429
					</enum>
429
					</enum>
430
				</enumlist>
430
				</enumlist>
431
			</parameter>
431
			</parameter>
432
		</syntax>
432
		</syntax>
433
		<description />
433
		<description></description>
434
	</function>
434
	</function>
435
	<function name="SIPCHANINFO" language="en_US">
435
	<function name="SIPCHANINFO" language="en_US">
436
		<synopsis>
436
		<synopsis>
437
			Gets the specified SIP parameter from the current channel.
437
			Gets the specified SIP parameter from the current channel.
438
		</synopsis>
438
		</synopsis>
[+20] [20] 23 lines
[+20] ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
462
						otherwise <literal>0</literal>.</para>
462
						otherwise <literal>0</literal>.</para>
463
					</enum>
463
					</enum>
464
				</enumlist>
464
				</enumlist>
465
			</parameter>
465
			</parameter>
466
		</syntax>
466
		</syntax>
467
		<description />
467
		<description></description>
468
	</function>
468
	</function>
469
	<function name="CHECKSIPDOMAIN" language="en_US">
469
	<function name="CHECKSIPDOMAIN" language="en_US">
470
		<synopsis>
470
		<synopsis>
471
			Checks if domain is a local domain.
471
			Checks if domain is a local domain.
472
		</synopsis>
472
		</synopsis>
[+20] [20] 221 lines
[+20] [+] static unsigned int default_primary_transport; /*!< Default primary Transport (enum sip_transport) for outbound connections to devices */
694
					\note in the future we could have multiple of these (per domain, per device group etc) */
694
					\note in the future we could have multiple of these (per domain, per device group etc) */
695

    
   
695

   
696
/*!< use this macro when ast_uri_decode is dependent on pedantic checking to be on. */
696
/*!< use this macro when ast_uri_decode is dependent on pedantic checking to be on. */
697
#define SIP_PEDANTIC_DECODE(str)	\
697
#define SIP_PEDANTIC_DECODE(str)	\
698
	if (sip_cfg.pedanticsipchecking && !ast_strlen_zero(str)) {	\
698
	if (sip_cfg.pedanticsipchecking && !ast_strlen_zero(str)) {	\
699
		ast_uri_decode(str);	\
699
		ast_uri_decode(str, ast_uri_sip_user);	\
700
	}	\
700
	}	\
701

    
   
701

   
702
static unsigned int chan_idx;       /*!< used in naming sip channel */
702
static unsigned int chan_idx;       /*!< used in naming sip channel */
703
static int global_match_auth_username;    /*!< Match auth username if available instead of From: Default off. */
703
static int global_match_auth_username;    /*!< Match auth username if available instead of From: Default off. */
704

    
   
704

   
[+20] [20] 493 lines
[+20] [+] static struct ast_config *notify_types = NULL; /*!< The list of manual NOTIFY types we know how to send */
1198
/*---------------------------- Forward declarations of functions in chan_sip.c */
1198
/*---------------------------- Forward declarations of functions in chan_sip.c */
1199
/* Note: This is added to help splitting up chan_sip.c into several files
1199
/* Note: This is added to help splitting up chan_sip.c into several files
1200
	in coming releases. */
1200
	in coming releases. */
1201

    
   
1201

   
1202
/*--- PBX interface functions */
1202
/*--- PBX interface functions */
1203
static struct ast_channel *sip_request_call(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause);
1203
static struct ast_channel *sip_request_call(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause);
1204
static int sip_devicestate(void *data);
1204
static int sip_devicestate(void *data);
1205
static int sip_sendtext(struct ast_channel *ast, const char *text);
1205
static int sip_sendtext(struct ast_channel *ast, const char *text);
1206
static int sip_call(struct ast_channel *ast, char *dest, int timeout);
1206
static int sip_call(struct ast_channel *ast, char *dest, int timeout);
1207
static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen);
1207
static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen);
1208
static int sip_hangup(struct ast_channel *ast);
1208
static int sip_hangup(struct ast_channel *ast);
[+20] [20] 81 lines
[+20] [+] static int process_sdp_o(const char *o, struct sip_pvt *p);
1290
static int process_sdp_a_sendonly(const char *a, int *sendonly);
1290
static int process_sdp_a_sendonly(const char *a, int *sendonly);
1291
static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec);
1291
static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec);
1292
static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newvideortp, int *last_rtpmap_codec);
1292
static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newvideortp, int *last_rtpmap_codec);
1293
static int process_sdp_a_text(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newtextrtp, char *red_fmtp, int *red_num_gen, int *red_data_pt, int *last_rtpmap_codec);
1293
static int process_sdp_a_text(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newtextrtp, char *red_fmtp, int *red_num_gen, int *red_data_pt, int *last_rtpmap_codec);
1294
static int process_sdp_a_image(const char *a, struct sip_pvt *p);
1294
static int process_sdp_a_image(const char *a, struct sip_pvt *p);
1295
static void add_codec_to_sdp(const struct sip_pvt *p, format_t codec,
1295
static void add_codec_to_sdp(const struct sip_pvt *p, struct ast_format *codec,
1296
			     struct ast_str **m_buf, struct ast_str **a_buf,
1296
			     struct ast_str **m_buf, struct ast_str **a_buf,
1297
			     int debug, int *min_packet_size);
1297
			     int debug, int *min_packet_size);
1298
static void add_noncodec_to_sdp(const struct sip_pvt *p, int format,
1298
static void add_noncodec_to_sdp(const struct sip_pvt *p, int format,
1299
				struct ast_str **m_buf, struct ast_str **a_buf,
1299
				struct ast_str **m_buf, struct ast_str **a_buf,
1300
				int debug);
1300
				int debug);
[+20] [20] 264 lines
[+20] [+] static int parse_minse(const char *p_hdrval, int *const p_interval);
1565
static enum st_refresher st_get_refresher(struct sip_pvt *);
1565
static enum st_refresher st_get_refresher(struct sip_pvt *);
1566
static enum st_mode st_get_mode(struct sip_pvt *);
1566
static enum st_mode st_get_mode(struct sip_pvt *);
1567
static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p);
1567
static struct sip_st_dlg* sip_st_alloc(struct sip_pvt *const p);
1568

    
   
1568

   
1569
/*------- RTP Glue functions -------- */
1569
/*------- RTP Glue functions -------- */
1570
static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, format_t codecs, int nat_active);
1570
static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, const struct ast_format_cap *cap, int nat_active);
1571

    
   
1571

   
1572
/*!--- SIP MWI Subscription support */
1572
/*!--- SIP MWI Subscription support */
1573
static int sip_subscribe_mwi(const char *value, int lineno);
1573
static int sip_subscribe_mwi(const char *value, int lineno);
1574
static void sip_subscribe_mwi_destroy(struct sip_subscription_mwi *mwi);
1574
static void sip_subscribe_mwi_destroy(struct sip_subscription_mwi *mwi);
1575
static void sip_send_all_mwi_subscriptions(void);
1575
static void sip_send_all_mwi_subscriptions(void);
1576
static int sip_subscribe_mwi_do(const void *data);
1576
static int sip_subscribe_mwi_do(const void *data);
1577
static int __sip_subscribe_mwi_do(struct sip_subscription_mwi *mwi);
1577
static int __sip_subscribe_mwi_do(struct sip_subscription_mwi *mwi);
1578

    
   
1578

   
1579
/*! \brief Definition of this channel for PBX channel registration */
1579
/*! \brief Definition of this channel for PBX channel registration */
1580
const struct ast_channel_tech sip_tech = {
1580
struct ast_channel_tech sip_tech = {
1581
	.type = "SIP",
1581
	.type = "SIP",
1582
	.description = "Session Initiation Protocol (SIP)",
1582
	.description = "Session Initiation Protocol (SIP)",
1583
	.capabilities = AST_FORMAT_AUDIO_MASK,	/* all audio formats */

   
1584
	.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
1583
	.properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
1585
	.requester = sip_request_call,			/* called with chan unlocked */
1584
	.requester = sip_request_call,			/* called with chan unlocked */
1586
	.devicestate = sip_devicestate,			/* called with chan unlocked (not chan-specific) */
1585
	.devicestate = sip_devicestate,			/* called with chan unlocked (not chan-specific) */
1587
	.call = sip_call,			/* called with chan locked */
1586
	.call = sip_call,			/* called with chan locked */
1588
	.send_html = sip_sendhtml,
1587
	.send_html = sip_sendhtml,
[+20] [20] 26 lines
[+20] const struct ast_channel_tech sip_tech = { struct ast_channel_tech sip_tech = {
1615
struct ast_channel_tech sip_tech_info;
1614
struct ast_channel_tech sip_tech_info;
1616

    
   
1615

   
1617
static int sip_cc_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
1616
static int sip_cc_agent_init(struct ast_cc_agent *agent, struct ast_channel *chan);
1618
static int sip_cc_agent_start_offer_timer(struct ast_cc_agent *agent);
1617
static int sip_cc_agent_start_offer_timer(struct ast_cc_agent *agent);
1619
static int sip_cc_agent_stop_offer_timer(struct ast_cc_agent *agent);
1618
static int sip_cc_agent_stop_offer_timer(struct ast_cc_agent *agent);
1620
static void sip_cc_agent_ack(struct ast_cc_agent *agent);
1619
static void sip_cc_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason);
1621
static int sip_cc_agent_status_request(struct ast_cc_agent *agent);
1620
static int sip_cc_agent_status_request(struct ast_cc_agent *agent);
1622
static int sip_cc_agent_start_monitoring(struct ast_cc_agent *agent);
1621
static int sip_cc_agent_start_monitoring(struct ast_cc_agent *agent);
1623
static int sip_cc_agent_recall(struct ast_cc_agent *agent);
1622
static int sip_cc_agent_recall(struct ast_cc_agent *agent);
1624
static void sip_cc_agent_destructor(struct ast_cc_agent *agent);
1623
static void sip_cc_agent_destructor(struct ast_cc_agent *agent);
1625

    
   
1624

   
1626
static struct ast_cc_agent_callbacks sip_cc_agent_callbacks = {
1625
static struct ast_cc_agent_callbacks sip_cc_agent_callbacks = {
1627
	.type = "SIP",
1626
	.type = "SIP",
1628
	.init = sip_cc_agent_init,
1627
	.init = sip_cc_agent_init,
1629
	.start_offer_timer = sip_cc_agent_start_offer_timer,
1628
	.start_offer_timer = sip_cc_agent_start_offer_timer,
1630
	.stop_offer_timer = sip_cc_agent_stop_offer_timer,
1629
	.stop_offer_timer = sip_cc_agent_stop_offer_timer,
1631
	.ack = sip_cc_agent_ack,
1630
	.respond = sip_cc_agent_respond,
1632
	.status_request = sip_cc_agent_status_request,
1631
	.status_request = sip_cc_agent_status_request,
1633
	.start_monitoring = sip_cc_agent_start_monitoring,
1632
	.start_monitoring = sip_cc_agent_start_monitoring,
1634
	.callee_available = sip_cc_agent_recall,
1633
	.callee_available = sip_cc_agent_recall,
1635
	.destructor = sip_cc_agent_destructor,
1634
	.destructor = sip_cc_agent_destructor,
1636
};
1635
};
1637

    
   
1636

   
1638
static int find_by_notify_uri_helper(void *obj, void *arg, int flags)
1637
static int find_by_notify_uri_helper(void *obj, void *arg, int flags)
1639
{
1638
{
1640
	struct ast_cc_agent *agent = obj;
1639
	struct ast_cc_agent *agent = obj;
1641
	struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
1640
	struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
1642
	const char *uri = arg;
1641
	const char *uri = arg;
1643

    
   
1642

   
1644
	return !strcmp(agent_pvt->notify_uri, uri) ? CMP_MATCH | CMP_STOP : 0;
1643
	return !sip_uri_cmp(agent_pvt->notify_uri, uri) ? CMP_MATCH | CMP_STOP : 0;
1645
}
1644
}
1646

    
   
1645

   
1647
static struct ast_cc_agent *find_sip_cc_agent_by_notify_uri(const char * const uri)
1646
static struct ast_cc_agent *find_sip_cc_agent_by_notify_uri(const char * const uri)
1648
{
1647
{
1649
	struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_notify_uri_helper, (char *)uri, "SIP");
1648
	struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_notify_uri_helper, (char *)uri, "SIP");
[+20] [20] 4 lines
[+20] static struct ast_cc_agent *find_sip_cc_agent_by_notify_uri(const char * const uri)
1654
{
1653
{
1655
	struct ast_cc_agent *agent = obj;
1654
	struct ast_cc_agent *agent = obj;
1656
	struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
1655
	struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
1657
	const char *uri = arg;
1656
	const char *uri = arg;
1658

    
   
1657

   
1659
	return !strcmp(agent_pvt->subscribe_uri, uri) ? CMP_MATCH | CMP_STOP : 0;
1658
	return !sip_uri_cmp(agent_pvt->subscribe_uri, uri) ? CMP_MATCH | CMP_STOP : 0;
1660
}
1659
}
1661

    
   
1660

   
1662
static struct ast_cc_agent *find_sip_cc_agent_by_subscribe_uri(const char * const uri)
1661
static struct ast_cc_agent *find_sip_cc_agent_by_subscribe_uri(const char * const uri)
1663
{
1662
{
1664
	struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_subscribe_uri_helper, (char *)uri, "SIP");
1663
	struct ast_cc_agent *agent = ast_cc_agent_callback(0, find_by_subscribe_uri_helper, (char *)uri, "SIP");
[+20] [20] 62 lines
[+20] [+] static int sip_cc_agent_stop_offer_timer(struct ast_cc_agent *agent)
1727

    
   
1726

   
1728
	AST_SCHED_DEL(sched, agent_pvt->offer_timer_id);
1727
	AST_SCHED_DEL(sched, agent_pvt->offer_timer_id);
1729
	return 0;
1728
	return 0;
1730
}
1729
}
1731

    
   
1730

   
1732
static void sip_cc_agent_ack(struct ast_cc_agent *agent)
1731
static void sip_cc_agent_respond(struct ast_cc_agent *agent, enum ast_cc_agent_response_reason reason)
1733
{
1732
{
1734
	struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
1733
	struct sip_cc_agent_pvt *agent_pvt = agent->private_data;
1735

    
   
1734

   
1736
	sip_pvt_lock(agent_pvt->subscribe_pvt);
1735
	sip_pvt_lock(agent_pvt->subscribe_pvt);
1737
	ast_set_flag(&agent_pvt->subscribe_pvt->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);
1736
	ast_set_flag(&agent_pvt->subscribe_pvt->flags[1], SIP_PAGE2_DIALOG_ESTABLISHED);

    
   
1737
	if (reason == AST_CC_AGENT_RESPONSE_SUCCESS || !ast_strlen_zero(agent_pvt->notify_uri)) {

    
   
1738
		/* The second half of this if statement may be a bit hard to grasp,

    
   
1739
		 * so here's an explanation. When a subscription comes into

    
   
1740
		 * chan_sip, as long as it is not malformed, it will be passed

    
   
1741
		 * to the CC core. If the core senses an out-of-order state transition,

    
   
1742
		 * then the core will call this callback with the "reason" set to a

    
   
1743
		 * failure condition.

    
   
1744
		 * However, an out-of-order state transition will occur during a resubscription

    
   
1745
		 * for CC. In such a case, we can see that we have already generated a notify_uri

    
   
1746
		 * and so we can detect that this isn't a *real* failure. Rather, it is just

    
   
1747
		 * something the core doesn't recognize as a legitimate SIP state transition.

    
   
1748
		 * Thus we respond with happiness and flowers.

    
   
1749
		 */
1738
	transmit_response(agent_pvt->subscribe_pvt, "200 OK", &agent_pvt->subscribe_pvt->initreq);
1750
		transmit_response(agent_pvt->subscribe_pvt, "200 OK", &agent_pvt->subscribe_pvt->initreq);
1739
	transmit_cc_notify(agent, agent_pvt->subscribe_pvt, CC_QUEUED);
1751
		transmit_cc_notify(agent, agent_pvt->subscribe_pvt, CC_QUEUED);

    
   
1752
	} else {

    
   
1753
		transmit_response(agent_pvt->subscribe_pvt, "500 Internal Error", &agent_pvt->subscribe_pvt->initreq);

    
   
1754
	}
1740
	sip_pvt_unlock(agent_pvt->subscribe_pvt);
1755
	sip_pvt_unlock(agent_pvt->subscribe_pvt);
1741
	agent_pvt->is_available = TRUE;
1756
	agent_pvt->is_available = TRUE;
1742
}
1757
}
1743

    
   
1758

   
1744
static int sip_cc_agent_status_request(struct ast_cc_agent *agent)
1759
static int sip_cc_agent_status_request(struct ast_cc_agent *agent)
[+20] [20] 847 lines
[+20] [+] static void *_sip_tcp_helper_thread(struct sip_pvt *pvt, struct ast_tcptls_session_instance *tcptls_session)
2592
				goto cleanup;
2607
				goto cleanup;
2593
			case TCPTLS_ALERT_DATA:
2608
			case TCPTLS_ALERT_DATA:
2594
				ao2_lock(me);
2609
				ao2_lock(me);
2595
				if (!(packet = AST_LIST_REMOVE_HEAD(&me->packet_q, entry))) {
2610
				if (!(packet = AST_LIST_REMOVE_HEAD(&me->packet_q, entry))) {
2596
					ast_log(LOG_WARNING, "TCPTLS thread alert_pipe indicated packet should be sent, but frame_q is empty");
2611
					ast_log(LOG_WARNING, "TCPTLS thread alert_pipe indicated packet should be sent, but frame_q is empty");
2597
				} else if (ast_tcptls_server_write(tcptls_session, ast_str_buffer(packet->data), packet->len) == -1) {

   
2598
					ast_log(LOG_WARNING, "Failure to write to tcp/tls socket\n");

   
2599
				}
2612
				}

    
   
2613
				ao2_unlock(me);
2600

    
   
2614

   
2601
				if (packet) {
2615
				if (packet) {

    
   
2616
					if (ast_tcptls_server_write(tcptls_session, ast_str_buffer(packet->data), packet->len) == -1) {

    
   
2617
						ast_log(LOG_WARNING, "Failure to write to tcp/tls socket\n");

    
   
2618
					}
2602
					ao2_t_ref(packet, -1, "tcptls packet sent, this is no longer needed");
2619
					ao2_t_ref(packet, -1, "tcptls packet sent, this is no longer needed");
2603
				}
2620
				}
2604
				ao2_unlock(me);

   
2605
				break;
2621
				break;
2606
			default:
2622
			default:
2607
				ast_log(LOG_ERROR, "Unknown tcptls thread alert '%d'\n", alert);
2623
				ast_log(LOG_ERROR, "Unknown tcptls thread alert '%d'\n", alert);
2608
			}
2624
			}
2609
		}
2625
		}
[+20] [20] 527 lines
[+20] [+] static int __sip_xmit(struct sip_pvt *p, struct ast_str *data, int len)
3137
		return XMIT_ERROR;
3153
		return XMIT_ERROR;
3138
	}
3154
	}
3139

    
   
3155

   
3140
	if (res == -1) {
3156
	if (res == -1) {
3141
		switch (errno) {
3157
		switch (errno) {
3142
		case EBADF: 		/* Bad file descriptor - seems like this is generated when the host exist, but doesn't accept the UDP packet */
3158
		case EBADF:		/* Bad file descriptor - seems like this is generated when the host exist, but doesn't accept the UDP packet */
3143
		case EHOSTUNREACH: 	/* Host can't be reached */
3159
		case EHOSTUNREACH:	/* Host can't be reached */
3144
		case ENETDOWN: 		/* Interface down */
3160
		case ENETDOWN:		/* Interface down */
3145
		case ENETUNREACH:	/* Network failure */
3161
		case ENETUNREACH:	/* Network failure */
3146
		case ECONNREFUSED:      /* ICMP port unreachable */
3162
		case ECONNREFUSED:      /* ICMP port unreachable */
3147
			res = XMIT_ERROR;	/* Don't bother with trying to transmit again */
3163
			res = XMIT_ERROR;	/* Don't bother with trying to transmit again */
3148
		}
3164
		}
3149
	}
3165
	}
[+20] [20] 267 lines
[+20] [+] static int retrans_pkt(const void *data)
3417

    
   
3433

   
3418
	pkt->retransid = -1; /* Kill this scheduler item */
3434
	pkt->retransid = -1; /* Kill this scheduler item */
3419

    
   
3435

   
3420
	if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
3436
	if (pkt->owner && pkt->method != SIP_OPTIONS && xmitres == 0) {
3421
		if (pkt->is_fatal || sipdebug) { /* Tell us if it's critical or if we're debugging */
3437
		if (pkt->is_fatal || sipdebug) { /* Tell us if it's critical or if we're debugging */
3422
			ast_log(LOG_WARNING, "Retransmission timeout reached on transmission %s for seqno %d (%s %s) -- See doc/sip-retransmit.txt.\n"
3438
			ast_log(LOG_WARNING, "Retransmission timeout reached on transmission %s for seqno %d (%s %s) -- See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions\n"
3423
				"Packet timed out after %dms with no response\n",
3439
				"Packet timed out after %dms with no response\n",
3424
				pkt->owner->callid,
3440
				pkt->owner->callid,
3425
				pkt->seqno,
3441
				pkt->seqno,
3426
				pkt->is_fatal ? "Critical" : "Non-critical",
3442
				pkt->is_fatal ? "Critical" : "Non-critical",
3427
				pkt->is_resp ? "Response" : "Request",
3443
				pkt->is_resp ? "Response" : "Request",
3428
				(int) ast_tvdiff_ms(ast_tvnow(), pkt->time_sent));
3444
				(int) ast_tvdiff_ms(ast_tvnow(), pkt->time_sent));
3429
		}
3445
		}
3430
	} else if (pkt->method == SIP_OPTIONS && sipdebug) {
3446
	} else if (pkt->method == SIP_OPTIONS && sipdebug) {
3431
		ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s)  -- See doc/sip-retransmit.txt.\n", pkt->owner->callid);
3447
		ast_log(LOG_WARNING, "Cancelling retransmit of OPTIONs (call id %s)  -- See https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions\n", pkt->owner->callid);
3432
	}
3448
	}
3433

    
   
3449

   
3434
	if (xmitres == XMIT_ERROR) {
3450
	if (xmitres == XMIT_ERROR) {
3435
		ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission on Call ID %s\n", pkt->owner->callid);
3451
		ast_log(LOG_WARNING, "Transmit error :: Cancelling transmission on Call ID %s\n", pkt->owner->callid);
3436
		append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
3452
		append_history(pkt->owner, "XmitErr", "%s", pkt->is_fatal ? "(Critical)" : "(Non-critical)");
[+20] [20] 9 lines
[+20] static int retrans_pkt(const void *data)
3446
		}
3462
		}
3447
		if (pkt->owner->owner && !pkt->owner->owner->hangupcause) {
3463
		if (pkt->owner->owner && !pkt->owner->owner->hangupcause) {
3448
			pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
3464
			pkt->owner->owner->hangupcause = AST_CAUSE_NO_USER_RESPONSE;
3449
		}
3465
		}
3450
		if (pkt->owner->owner) {
3466
		if (pkt->owner->owner) {
3451
			ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet (see doc/sip-retransmit.txt).\n", pkt->owner->callid);
3467
			ast_log(LOG_WARNING, "Hanging up call %s - no reply to our critical packet (see https://wiki.asterisk.org/wiki/display/AST/SIP+Retransmissions).\n", pkt->owner->callid);
3452

    
   
3468

   
3453
			if (pkt->is_resp &&
3469
			if (pkt->is_resp &&
3454
				(pkt->response_code >= 200) &&
3470
				(pkt->response_code >= 200) &&
3455
				(pkt->response_code < 300) &&
3471
				(pkt->response_code < 300) &&
3456
				pkt->owner->pendinginvite &&
3472
				pkt->owner->pendinginvite &&
[+20] [20] 25 lines
[+20] static int retrans_pkt(const void *data)
3482
		}
3498
		}
3483
	}
3499
	}
3484

    
   
3500

   
3485
	if (pkt->method == SIP_BYE) {
3501
	if (pkt->method == SIP_BYE) {
3486
		/* We're not getting answers on SIP BYE's.  Tear down the call anyway. */
3502
		/* We're not getting answers on SIP BYE's.  Tear down the call anyway. */

    
   
3503
		sip_alreadygone(pkt->owner);
3487
		if (pkt->owner->owner) {
3504
		if (pkt->owner->owner) {
3488
			ast_channel_unlock(pkt->owner->owner);
3505
			ast_channel_unlock(pkt->owner->owner);
3489
		}
3506
		}
3490
		append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
3507
		append_history(pkt->owner, "ByeFailure", "Remote peer doesn't respond to bye. Destroying call anyway.");
3491
		pvt_set_needdestroy(pkt->owner, "no response to BYE");
3508
		pvt_set_needdestroy(pkt->owner, "no response to BYE");
[+20] [20] 545 lines
[+20] [+] static int sip_setoption(struct ast_channel *chan, int option, void *data, int datalen)
4037
	int res = -1;
4054
	int res = -1;
4038
	struct sip_pvt *p = chan->tech_pvt;
4055
	struct sip_pvt *p = chan->tech_pvt;
4039

    
   
4056

   
4040
	switch (option) {
4057
	switch (option) {
4041
	case AST_OPTION_FORMAT_READ:
4058
	case AST_OPTION_FORMAT_READ:
4042
		res = ast_rtp_instance_set_read_format(p->rtp, *(int *) data);
4059
		res = ast_rtp_instance_set_read_format(p->rtp, (struct ast_format *) data);
4043
		break;
4060
		break;
4044
	case AST_OPTION_FORMAT_WRITE:
4061
	case AST_OPTION_FORMAT_WRITE:
4045
		res = ast_rtp_instance_set_write_format(p->rtp, *(int *) data);
4062
		res = ast_rtp_instance_set_write_format(p->rtp, (struct ast_format *) data);
4046
		break;
4063
		break;
4047
	case AST_OPTION_MAKE_COMPATIBLE:
4064
	case AST_OPTION_MAKE_COMPATIBLE:
4048
		res = ast_rtp_instance_make_compatible(chan, p->rtp, (struct ast_channel *) data);
4065
		res = ast_rtp_instance_make_compatible(chan, p->rtp, (struct ast_channel *) data);
4049
		break;
4066
		break;
4050
	case AST_OPTION_DIGIT_DETECT:
4067
	case AST_OPTION_DIGIT_DETECT:
[+20] [20] 131 lines
[+20] [+] static int sip_sendhtml(struct ast_channel *chan, int subclass, const char *data, int datalen)
4182
		break;
4199
		break;
4183
	case AST_STATE_UP:
4200
	case AST_STATE_UP:
4184
		if (!p->pendinginvite) {		/* We are up, and have no outstanding invite */
4201
		if (!p->pendinginvite) {		/* We are up, and have no outstanding invite */
4185
			transmit_reinvite_with_sdp(p, FALSE, FALSE);
4202
			transmit_reinvite_with_sdp(p, FALSE, FALSE);
4186
		} else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
4203
		} else if (!ast_test_flag(&p->flags[0], SIP_PENDINGBYE)) {
4187
			ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);	
4204
			ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
4188
		}	
4205
		}
4189
		break;
4206
		break;
4190
	default:
4207
	default:
4191
		ast_log(LOG_WARNING, "Don't know how to send URI when state is %d!\n", chan->_state);
4208
		ast_log(LOG_WARNING, "Don't know how to send URI when state is %d!\n", chan->_state);
4192
	}
4209
	}
4193

    
   
4210

   
[+20] [20] 24 lines
[+20] [+] static int sip_sendtext(struct ast_channel *ast, const char *text)
4218
		return(0);
4235
		return(0);
4219
	}
4236
	}
4220
	if (debug)
4237
	if (debug)
4221
		ast_verbose("Sending text %s on %s\n", text, ast->name);
4238
		ast_verbose("Sending text %s on %s\n", text, ast->name);
4222
	transmit_message_with_text(dialog, text, 0, 0);
4239
	transmit_message_with_text(dialog, text, 0, 0);
4223
	return 0;	
4240
	return 0;
4224
}
4241
}
4225

    
   
4242

   
4226
/*! \brief Update peer object in realtime storage
4243
/*! \brief Update peer object in realtime storage
4227
	If the Asterisk system name is set in asterisk.conf, we will use
4244
	If the Asterisk system name is set in asterisk.conf, we will use
4228
	that name and store that in the "regserver" field in the sippeers
4245
	that name and store that in the "regserver" field in the sippeers
[+20] [20] 119 lines
[+20] [+] static void sip_destroy_peer(struct sip_peer *peer)
4348
	/* Delete it, it needs to disappear */
4365
	/* Delete it, it needs to disappear */
4349
	if (peer->call) {
4366
	if (peer->call) {
4350
		dialog_unlink_all(peer->call, TRUE, TRUE);
4367
		dialog_unlink_all(peer->call, TRUE, TRUE);
4351
		peer->call = dialog_unref(peer->call, "peer->call is being unset");
4368
		peer->call = dialog_unref(peer->call, "peer->call is being unset");
4352
	}
4369
	}
4353
	

   
4354

    
   
4370

   
4355
	if (peer->mwipvt) {	/* We have an active subscription, delete it */
4371
	if (peer->mwipvt) {	/* We have an active subscription, delete it */
4356
		dialog_unlink_all(peer->mwipvt, TRUE, TRUE);
4372
		dialog_unlink_all(peer->mwipvt, TRUE, TRUE);
4357
		peer->mwipvt = dialog_unref(peer->mwipvt, "unreffing peer->mwipvt");
4373
		peer->mwipvt = dialog_unref(peer->mwipvt, "unreffing peer->mwipvt");
4358
	}
4374
	}
4359
	
4375

   
4360
	if (peer->chanvars) {
4376
	if (peer->chanvars) {
4361
		ast_variables_destroy(peer->chanvars);
4377
		ast_variables_destroy(peer->chanvars);
4362
		peer->chanvars = NULL;
4378
		peer->chanvars = NULL;
4363
	}
4379
	}
4364
	
4380

   
4365
	register_peer_exten(peer, FALSE);
4381
	register_peer_exten(peer, FALSE);
4366
	ast_free_ha(peer->ha);
4382
	ast_free_ha(peer->ha);
4367
	ast_free_ha(peer->directmediaha);
4383
	ast_free_ha(peer->directmediaha);
4368
	if (peer->selfdestruct)
4384
	if (peer->selfdestruct)
4369
		ast_atomic_fetchadd_int(&apeerobjs, -1);
4385
		ast_atomic_fetchadd_int(&apeerobjs, -1);
[+20] [20] 14 lines
[+20] static void sip_destroy_peer(struct sip_peer *peer)
4384
	}
4400
	}
4385

    
   
4401

   
4386
	ast_cc_config_params_destroy(peer->cc_params);
4402
	ast_cc_config_params_destroy(peer->cc_params);
4387

    
   
4403

   
4388
	ast_string_field_free_memory(peer);
4404
	ast_string_field_free_memory(peer);

    
   
4405

   

    
   
4406
	peer->caps = ast_format_cap_destroy(peer->caps);
4389
}
4407
}
4390

    
   
4408

   
4391
/*! \brief Update peer data in database (if used) */
4409
/*! \brief Update peer data in database (if used) */
4392
static void update_peer(struct sip_peer *p, int expire)
4410
static void update_peer(struct sip_peer *p, int expire)
4393
{
4411
{
[+20] [20] 431 lines
[+20] [+] static int dialog_initialize_rtp(struct sip_pvt *dialog)
4825
	if (!(dialog->rtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
4843
	if (!(dialog->rtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
4826
		return -1;
4844
		return -1;
4827
	}
4845
	}
4828

    
   
4846

   
4829
	if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS) ||
4847
	if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT_ALWAYS) ||
4830
			(ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) && (dialog->capability & AST_FORMAT_VIDEO_MASK))) {
4848
			(ast_test_flag(&dialog->flags[1], SIP_PAGE2_VIDEOSUPPORT) && (ast_format_cap_has_type(dialog->caps, AST_FORMAT_TYPE_VIDEO)))) {
4831
		if (!(dialog->vrtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
4849
		if (!(dialog->vrtp = ast_rtp_instance_new(dialog->engine, sched, &bindaddr_tmp, NULL))) {
4832
			return -1;
4850
			return -1;
4833
		}
4851
		}
4834
		ast_rtp_instance_set_timeout(dialog->vrtp, global_rtptimeout);
4852
		ast_rtp_instance_set_timeout(dialog->vrtp, global_rtptimeout);
4835
		ast_rtp_instance_set_hold_timeout(dialog->vrtp, global_rtpholdtimeout);
4853
		ast_rtp_instance_set_hold_timeout(dialog->vrtp, global_rtpholdtimeout);
[+20] [20] 50 lines
[+20] [+] static int create_addr_from_peer(struct sip_pvt *dialog, struct sip_peer *peer)
4886

    
   
4904

   
4887
	/* XXX TODO: get flags directly from peer only as they are needed using dialog->relatedpeer */
4905
	/* XXX TODO: get flags directly from peer only as they are needed using dialog->relatedpeer */
4888
	ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
4906
	ast_copy_flags(&dialog->flags[0], &peer->flags[0], SIP_FLAGS_TO_COPY);
4889
	ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
4907
	ast_copy_flags(&dialog->flags[1], &peer->flags[1], SIP_PAGE2_FLAGS_TO_COPY);
4890
	ast_copy_flags(&dialog->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
4908
	ast_copy_flags(&dialog->flags[2], &peer->flags[2], SIP_PAGE3_FLAGS_TO_COPY);
4891
	dialog->capability = peer->capability;
4909
	ast_format_cap_copy(dialog->caps, peer->caps);
4892
	dialog->prefs = peer->prefs;
4910
	dialog->prefs = peer->prefs;
4893
	if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
4911
	if (ast_test_flag(&dialog->flags[1], SIP_PAGE2_T38SUPPORT)) {
4894
		/* t38pt_udptl was enabled in the peer and not in [general] */
4912
		/* t38pt_udptl was enabled in the peer and not in [general] */
4895
		if (dialog->udptl || (!dialog->udptl && (dialog->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, &bindaddr)))) {
4913
		if (dialog->udptl || (!dialog->udptl && (dialog->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, &bindaddr)))) {
4896
			dialog->t38_maxdatagram = peer->t38_maxdatagram;
4914
			dialog->t38_maxdatagram = peer->t38_maxdatagram;
[+20] [20] 355 lines
[+20] [+] static int sip_call(struct ast_channel *ast, char *dest, int timeout)
5252
	if (res == -1) {
5270
	if (res == -1) {
5253
		ast->hangupcause = AST_CAUSE_USER_BUSY;
5271
		ast->hangupcause = AST_CAUSE_USER_BUSY;
5254
		return res;
5272
		return res;
5255
	}
5273
	}
5256
	p->callingpres = ast_party_id_presentation(&ast->caller.id);
5274
	p->callingpres = ast_party_id_presentation(&ast->caller.id);
5257
	p->jointcapability = ast_rtp_instance_available_formats(p->rtp, p->capability, p->prefcodec);
5275
	ast_rtp_instance_available_formats(p->rtp, p->caps, p->prefcaps, p->jointcaps);
5258
	p->jointnoncodeccapability = p->noncodeccapability;
5276
	p->jointnoncodeccapability = p->noncodeccapability;
5259

    
   
5277

   
5260
	/* If there are no audio formats left to offer, punt */
5278
	/* If there are no audio formats left to offer, punt */
5261
	if (!(p->jointcapability & AST_FORMAT_AUDIO_MASK)) {
5279
	if (!(ast_format_cap_has_type(p->jointcaps, AST_FORMAT_TYPE_AUDIO))) {
5262
		ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
5280
		ast_log(LOG_WARNING, "No audio format found to offer. Cancelling call to %s\n", p->username);
5263
		res = -1;
5281
		res = -1;
5264
	} else {
5282
	} else {
5265
		int xmitres;
5283
		int xmitres;
5266

    
   
5284

   
[+20] [20] 27 lines
[+20] [+] static void sip_registry_destroy(struct sip_registry *reg)
5294
		ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
5312
		ast_debug(3, "Destroying active SIP dialog for registry %s@%s\n", reg->username, reg->hostname);
5295
		dialog_unlink_all(reg->call, TRUE, TRUE);
5313
		dialog_unlink_all(reg->call, TRUE, TRUE);
5296
		reg->call = dialog_unref(reg->call, "unref reg->call");
5314
		reg->call = dialog_unref(reg->call, "unref reg->call");
5297
		/* reg->call = sip_destroy(reg->call); */
5315
		/* reg->call = sip_destroy(reg->call); */
5298
	}
5316
	}
5299
	AST_SCHED_DEL(sched, reg->expire);	
5317
	AST_SCHED_DEL(sched, reg->expire);
5300
	AST_SCHED_DEL(sched, reg->timeout);
5318
	AST_SCHED_DEL(sched, reg->timeout);
5301
	
5319

   
5302
	ast_string_field_free_memory(reg);
5320
	ast_string_field_free_memory(reg);
5303
	ast_atomic_fetchadd_int(&regobjs, -1);
5321
	ast_atomic_fetchadd_int(&regobjs, -1);
5304
	ast_dnsmgr_release(reg->dnsmgr);
5322
	ast_dnsmgr_release(reg->dnsmgr);
5305
	ast_free(reg);
5323
	ast_free(reg);
5306
}
5324
}
[+20] [20] 155 lines
[+20] [+] void __sip_destroy(struct sip_pvt *p, int lockowner, int lockdialoglist)
5462

    
   
5480

   
5463
	if (p->socket.tcptls_session) {
5481
	if (p->socket.tcptls_session) {
5464
		ao2_ref(p->socket.tcptls_session, -1);
5482
		ao2_ref(p->socket.tcptls_session, -1);
5465
		p->socket.tcptls_session = NULL;
5483
		p->socket.tcptls_session = NULL;
5466
	}
5484
	}

    
   
5485
	p->caps = ast_format_cap_destroy(p->caps);

    
   
5486
	p->jointcaps = ast_format_cap_destroy(p->jointcaps);

    
   
5487
	p->peercaps = ast_format_cap_destroy(p->peercaps);

    
   
5488
	p->redircaps = ast_format_cap_destroy(p->redircaps);

    
   
5489
	p->prefcaps = ast_format_cap_destroy(p->prefcaps);
5467
}
5490
}
5468

    
   
5491

   
5469
/*! \brief  update_call_counter: Handle call_limit for SIP devices
5492
/*! \brief  update_call_counter: Handle call_limit for SIP devices
5470
 * Setting a call-limit will cause calls above the limit not to be accepted.
5493
 * Setting a call-limit will cause calls above the limit not to be accepted.
5471
 *
5494
 *
[+20] [20] 327 lines
[+20] [+] const char *hangup_cause2sip(int cause)
5799
			return "502 Bad Gateway";
5822
			return "502 Bad Gateway";
5800
		case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:	/* Can't find codec to connect to host */
5823
		case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL:	/* Can't find codec to connect to host */
5801
			return "488 Not Acceptable Here";
5824
			return "488 Not Acceptable Here";
5802
		case AST_CAUSE_INTERWORKING:	/* Unspecified Interworking issues */
5825
		case AST_CAUSE_INTERWORKING:	/* Unspecified Interworking issues */
5803
			return "500 Network error";
5826
			return "500 Network error";
5804
			
5827

   
5805
		case AST_CAUSE_NOTDEFINED:
5828
		case AST_CAUSE_NOTDEFINED:
5806
		default:
5829
		default:
5807
			ast_debug(1, "AST hangup cause %d (no match found in SIP)\n", cause);
5830
			ast_debug(1, "AST hangup cause %d (no match found in SIP)\n", cause);
5808
			return NULL;
5831
			return NULL;
5809
	}
5832
	}
[+20] [20] 214 lines
[+20] [+] static int sip_hangup(struct ast_channel *ast)
6024
}
6047
}
6025

    
   
6048

   
6026
/*! \brief Try setting codec suggested by the SIP_CODEC channel variable */
6049
/*! \brief Try setting codec suggested by the SIP_CODEC channel variable */
6027
static void try_suggested_sip_codec(struct sip_pvt *p)
6050
static void try_suggested_sip_codec(struct sip_pvt *p)
6028
{
6051
{
6029
	format_t fmt;
6052
	struct ast_format fmt;
6030
	const char *codec;
6053
	const char *codec;
6031

    
   
6054

   

    
   
6055
	ast_format_clear(&fmt);

    
   
6056

   
6032
	if (p->outgoing_call) {
6057
	if (p->outgoing_call) {
6033
		codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_OUTBOUND");
6058
		codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_OUTBOUND");
6034
	} else if (!(codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_INBOUND"))) {
6059
	} else if (!(codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC_INBOUND"))) {
6035
		codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
6060
		codec = pbx_builtin_getvar_helper(p->owner, "SIP_CODEC");
6036
	}
6061
	}
6037

    
   
6062

   
6038
	if (!codec) 
6063
	if (!codec) 
6039
		return;
6064
		return;
6040

    
   
6065

   
6041
	fmt = ast_getformatbyname(codec);
6066
	ast_getformatbyname(codec, &fmt);
6042
	if (fmt) {
6067
	if (fmt.id) {
6043
		ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
6068
		ast_log(LOG_NOTICE, "Changing codec to '%s' for this call because of ${SIP_CODEC} variable\n", codec);
6044
		if (p->jointcapability & fmt) {
6069
		if (ast_format_cap_iscompatible(p->jointcaps, &fmt)) {
6045
			p->jointcapability &= fmt;
6070
			ast_format_cap_set(p->jointcaps, &fmt);
6046
			p->capability &= fmt;
6071
			ast_format_cap_set(p->caps, &fmt);
6047
		} else
6072
		} else
6048
			ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
6073
			ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because it is not shared by both ends.\n");
6049
	} else
6074
	} else
6050
		ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
6075
		ast_log(LOG_NOTICE, "Ignoring ${SIP_CODEC} variable because of unrecognized/not configured codec (check allow/disallow in sip.conf): %s\n", codec);
6051
	return;	
6076
	return;
6052
}
6077
}
6053

    
   
6078

   
6054
/*! \brief  sip_answer: Answer SIP call , send 200 OK on Invite
6079
/*! \brief  sip_answer: Answer SIP call , send 200 OK on Invite
6055
 * Part of PBX interface */
6080
 * Part of PBX interface */
6056
static int sip_answer(struct ast_channel *ast)
6081
static int sip_answer(struct ast_channel *ast)
6057
{
6082
{
6058
	int res = 0;
6083
	int res = 0;
6059
	struct sip_pvt *p = ast->tech_pvt;
6084
	struct sip_pvt *p = ast->tech_pvt;
6060

    
   
6085

   
6061
	sip_pvt_lock(p);
6086
	sip_pvt_lock(p);
6062
	if (ast->_state != AST_STATE_UP) {
6087
	if (ast->_state != AST_STATE_UP) {
6063
		try_suggested_sip_codec(p);	
6088
		try_suggested_sip_codec(p);
6064

    
   
6089

   
6065
		ast_setstate(ast, AST_STATE_UP);
6090
		ast_setstate(ast, AST_STATE_UP);
6066
		ast_debug(1, "SIP answering channel: %s\n", ast->name);
6091
		ast_debug(1, "SIP answering channel: %s\n", ast->name);
6067
		ast_rtp_instance_update_source(p->rtp);
6092
		ast_rtp_instance_update_source(p->rtp);
6068
		res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL, FALSE, TRUE);
6093
		res = transmit_response_with_sdp(p, "200 OK", &p->initreq, XMIT_CRITICAL, FALSE, TRUE);
[+20] [20] 9 lines
[+20] [+] static int sip_write(struct ast_channel *ast, struct ast_frame *frame)
6078
	struct sip_pvt *p = ast->tech_pvt;
6103
	struct sip_pvt *p = ast->tech_pvt;
6079
	int res = 0;
6104
	int res = 0;
6080

    
   
6105

   
6081
	switch (frame->frametype) {
6106
	switch (frame->frametype) {
6082
	case AST_FRAME_VOICE:
6107
	case AST_FRAME_VOICE:
6083
		if (!(frame->subclass.codec & ast->nativeformats)) {
6108
		if (!(ast_format_cap_iscompatible(ast->nativeformats, &frame->subclass.format))) {
6084
			char s1[512], s2[512], s3[512];
6109
			char s1[512];
6085
			ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s read/write = %s/%s\n",
6110
			ast_log(LOG_WARNING, "Asked to transmit frame type %s, while native formats is %s read/write = %s/%s\n",
6086
				ast_getformatname(frame->subclass.codec),
6111
				ast_getformatname(&frame->subclass.format),
6087
				ast_getformatname_multiple(s1, sizeof(s1), ast->nativeformats & AST_FORMAT_AUDIO_MASK),
6112
				ast_getformatname_multiple(s1, sizeof(s1), ast->nativeformats),
6088
				ast_getformatname_multiple(s2, sizeof(s2), ast->readformat),
6113
				ast_getformatname(&ast->readformat),
6089
				ast_getformatname_multiple(s3, sizeof(s3), ast->writeformat));
6114
				ast_getformatname(&ast->writeformat));
6090
			return 0;
6115
			return 0;
6091
		}
6116
		}
6092
		if (p) {
6117
		if (p) {
6093
			sip_pvt_lock(p);
6118
			sip_pvt_lock(p);
6094
			if (p->rtp) {
6119
			if (p->rtp) {
[+20] [20] 442 lines
[+20] [+] static int sip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
6537
*/
6562
*/
6538
static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title, const char *linkedid)
6563
static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title, const char *linkedid)
6539
{
6564
{
6540
	struct ast_channel *tmp;
6565
	struct ast_channel *tmp;
6541
	struct ast_variable *v = NULL;
6566
	struct ast_variable *v = NULL;
6542
	format_t fmt;
6567
	struct ast_format fmt;
6543
	format_t what;
6568
	struct ast_format_cap *what = NULL; /* SHALLOW COPY DO NOT DESTROY! */
6544
	format_t video;
6569
	int needvideo = 0;
6545
	format_t text;

   
6546
	format_t needvideo = 0;

   
6547
	int needtext = 0;
6570
	int needtext = 0;
6548
	char buf[SIPBUFSIZE];
6571
	char buf[SIPBUFSIZE];
6549
	char *decoded_exten;
6572
	char *decoded_exten;
6550

    
   
6573

   
6551
	{
6574
	{
[+20] [20] 22 lines
[+20] static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title, const char *linkedid)
6574

    
   
6597

   
6575
	tmp->tech = ( ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INFO || ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_SHORTINFO) ?  &sip_tech_info : &sip_tech;
6598
	tmp->tech = ( ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_INFO || ast_test_flag(&i->flags[0], SIP_DTMF) == SIP_DTMF_SHORTINFO) ?  &sip_tech_info : &sip_tech;
6576

    
   
6599

   
6577
	/* Select our native format based on codec preference until we receive
6600
	/* Select our native format based on codec preference until we receive
6578
	   something from another device to the contrary. */
6601
	   something from another device to the contrary. */
6579
	if (i->jointcapability) { 	/* The joint capabilities of us and peer */
6602
	if (!(ast_format_cap_is_empty(i->jointcaps))) {	/* The joint capabilities of us and peer */
6580
		what = i->jointcapability;
6603
		what = i->jointcaps;
6581
		video = i->jointcapability & AST_FORMAT_VIDEO_MASK;
6604
	} else if (!(ast_format_cap_is_empty(i->caps))) {		/* Our configured capability for this peer */
6582
		text = i->jointcapability & AST_FORMAT_TEXT_MASK;
6605
		what = i->caps;
6583
	} else if (i->capability) {		/* Our configured capability for this peer */
6606
	} else {
6584
		what = i->capability;
6607
		what = sip_cfg.caps;
6585
		video = i->capability & AST_FORMAT_VIDEO_MASK;

   
6586
		text = i->capability & AST_FORMAT_TEXT_MASK;

   
6587
	} else {

   
6588
		what = sip_cfg.capability;	/* Global codec support */

   
6589
		video = sip_cfg.capability & AST_FORMAT_VIDEO_MASK;

   
6590
		text = sip_cfg.capability & AST_FORMAT_TEXT_MASK;

   
6591
	}
6608
	}
6592

    
   
6609

   
6593
	/* Set the native formats for audio  and merge in video */
6610
	/* Set the native formats */
6594
	tmp->nativeformats = ast_codec_choose(&i->prefs, what, 1) | video | text;
6611
	ast_format_cap_copy(tmp->nativeformats, what);

    
   
6612
	/* choose and use only the best audio format for our native formats */

    
   
6613
	ast_codec_choose(&i->prefs, tmp->nativeformats, 1, &fmt); /* get the best audio format */

    
   
6614
	ast_format_cap_remove_bytype(tmp->nativeformats, AST_FORMAT_TYPE_AUDIO); /* remove only the other audio formats */

    
   
6615
	ast_format_cap_add(tmp->nativeformats, &fmt); /* add our best choice back */

    
   
6616

   
6595
	ast_debug(3, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
6617
	ast_debug(3, "*** Our native formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmp->nativeformats));
6596
	ast_debug(3, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcapability));
6618
	ast_debug(3, "*** Joint capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->jointcaps));
6597
	ast_debug(3, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->capability));
6619
	ast_debug(3, "*** Our capabilities are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->caps));
6598
	ast_debug(3, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, ast_codec_choose(&i->prefs, what, 1)));
6620
	ast_debug(3, "*** AST_CODEC_CHOOSE formats are %s \n", ast_getformatname(&fmt));
6599
	if (i->prefcodec) {
6621
	if (!ast_format_cap_is_empty(i->prefcaps)) {
6600
		ast_debug(3, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcodec));
6622
		ast_debug(3, "*** Our preferred formats from the incoming channel are %s \n", ast_getformatname_multiple(buf, SIPBUFSIZE, i->prefcaps));
6601
	}
6623
	}
6602

    
   
6624

   
6603
	/* XXX Why are we choosing a codec from the native formats?? */

   
6604
	fmt = ast_best_codec(tmp->nativeformats);

   
6605

    
   

   
6606
	/* If we have a prefcodec setting, we have an inbound channel that set a
6625
	/* If we have a prefcodec setting, we have an inbound channel that set a
6607
	   preferred format for this call. Otherwise, we check the jointcapability
6626
	   preferred format for this call. Otherwise, we check the jointcapability
6608
	   We also check for vrtp. If it's not there, we are not allowed do any video anyway.
6627
	   We also check for vrtp. If it's not there, we are not allowed do any video anyway.
6609
	 */
6628
	 */
6610
	if (i->vrtp) {
6629
	if (i->vrtp) {
6611
		if (ast_test_flag(&i->flags[1], SIP_PAGE2_VIDEOSUPPORT))
6630
		if (ast_test_flag(&i->flags[1], SIP_PAGE2_VIDEOSUPPORT))
6612
			needvideo = AST_FORMAT_VIDEO_MASK;
6631
			needvideo = 1;
6613
		else if (i->prefcodec)
6632
		else if (!ast_format_cap_is_empty(i->prefcaps))
6614
			needvideo = i->prefcodec & AST_FORMAT_VIDEO_MASK;	/* Outbound call */
6633
			needvideo = ast_format_cap_has_type(i->prefcaps, AST_FORMAT_TYPE_VIDEO);	/* Outbound call */
6615
 		else
6634
		else
6616
			needvideo = i->jointcapability & AST_FORMAT_VIDEO_MASK;	/* Inbound call */
6635
			needvideo = ast_format_cap_has_type(i->jointcaps, AST_FORMAT_TYPE_VIDEO);	/* Inbound call */
6617
	}
6636
	}
6618

    
   
6637

   
6619
	if (i->trtp) {
6638
	if (i->trtp) {
6620
		if (i->prefcodec)
6639
		if (!ast_format_cap_is_empty(i->prefcaps))
6621
			needtext = i->prefcodec & AST_FORMAT_TEXT_MASK;	/* Outbound call */
6640
			needtext = ast_format_cap_has_type(i->prefcaps, AST_FORMAT_TYPE_TEXT);	/* Outbound call */
6622
 		else
6641
		else
6623
			needtext = i->jointcapability & AST_FORMAT_TEXT_MASK;	/* Inbound call */
6642
			needtext = ast_format_cap_has_type(i->jointcaps, AST_FORMAT_TYPE_TEXT);	/* Inbound call */
6624
	}
6643
	}
6625

    
   
6644

   
6626
	if (needvideo) {
6645
	if (needvideo) {
6627
		ast_debug(3, "This channel can handle video! HOLLYWOOD next!\n");
6646
		ast_debug(3, "This channel can handle video! HOLLYWOOD next!\n");
6628
	} else {
6647
	} else {
[+20] [20] 32 lines
[+20] static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title, const char *linkedid)
6661
	if (state == AST_STATE_RING) {
6680
	if (state == AST_STATE_RING) {
6662
		tmp->rings = 1;
6681
		tmp->rings = 1;
6663
	}
6682
	}
6664
	tmp->adsicpe = AST_ADSI_UNAVAILABLE;
6683
	tmp->adsicpe = AST_ADSI_UNAVAILABLE;
6665

    
   
6684

   
6666
	tmp->writeformat = fmt;
6685
	ast_format_copy(&tmp->writeformat, &fmt);
6667
	tmp->rawwriteformat = fmt;
6686
	ast_format_copy(&tmp->rawwriteformat, &fmt);
6668
	ast_rtp_instance_set_write_format(i->rtp, fmt);
6687
	ast_rtp_instance_set_write_format(i->rtp, &fmt);
6669

    
   
6688

   
6670
	tmp->readformat = fmt;
6689
	ast_format_copy(&tmp->readformat, &fmt);
6671
	tmp->rawreadformat = fmt;
6690
	ast_format_copy(&tmp->rawreadformat, &fmt);
6672
	ast_rtp_instance_set_read_format(i->rtp, fmt);
6691
	ast_rtp_instance_set_read_format(i->rtp, &fmt);
6673

    
   
6692

   
6674
	tmp->tech_pvt = dialog_ref(i, "sip_new: set chan->tech_pvt to i");
6693
	tmp->tech_pvt = dialog_ref(i, "sip_new: set chan->tech_pvt to i");
6675

    
   
6694

   
6676
	tmp->callgroup = i->callgroup;
6695
	tmp->callgroup = i->callgroup;
6677
	tmp->pickupgroup = i->pickupgroup;
6696
	tmp->pickupgroup = i->pickupgroup;
[+20] [20] 21 lines
[+20] static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *title, const char *linkedid)
6699
	if (ast_exists_extension(NULL, i->context, i->exten, 1, i->cid_num)) {
6718
	if (ast_exists_extension(NULL, i->context, i->exten, 1, i->cid_num)) {
6700
		/* encoded in dialplan, so keep extension encoded */
6719
		/* encoded in dialplan, so keep extension encoded */
6701
		ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
6720
		ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
6702
	} else {
6721
	} else {
6703
		decoded_exten = ast_strdupa(i->exten);
6722
		decoded_exten = ast_strdupa(i->exten);
6704
		ast_uri_decode(decoded_exten);
6723
		ast_uri_decode(decoded_exten, ast_uri_sip_user);
6705
		ast_copy_string(tmp->exten, decoded_exten, sizeof(tmp->exten));
6724
		ast_copy_string(tmp->exten, decoded_exten, sizeof(tmp->exten));
6706
	}
6725
	}
6707

    
   
6726

   
6708
	/* Don't use ast_set_callerid() here because it will
6727
	/* Don't use ast_set_callerid() here because it will
6709
	 * generate an unnecessary NewCallerID event  */
6728
	 * generate an unnecessary NewCallerID event  */
[+20] [20] 265 lines
[+20] [+] static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
6975
	/* We already hold the channel lock */
6994
	/* We already hold the channel lock */
6976
	if (!p->owner || (f && f->frametype != AST_FRAME_VOICE)) {
6995
	if (!p->owner || (f && f->frametype != AST_FRAME_VOICE)) {
6977
		return f;
6996
		return f;
6978
	}
6997
	}
6979

    
   
6998

   
6980
	if (f && f->subclass.codec != (p->owner->nativeformats & AST_FORMAT_AUDIO_MASK)) {
6999
	if (f && !ast_format_cap_iscompatible(p->owner->nativeformats, &f->subclass.format)) {
6981
		if (!(f->subclass.codec & p->jointcapability)) {
7000
		if (!ast_format_cap_iscompatible(p->jointcaps, &f->subclass.format)) {
6982
			ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
7001
			ast_debug(1, "Bogus frame of format '%s' received from '%s'!\n",
6983
				ast_getformatname(f->subclass.codec), p->owner->name);
7002
				ast_getformatname(&f->subclass.format), p->owner->name);
6984
			return &ast_null_frame;
7003
			return &ast_null_frame;
6985
		}
7004
		}
6986
		ast_debug(1, "Oooh, format changed to %s\n",
7005
		ast_debug(1, "Oooh, format changed to %s\n",
6987
			ast_getformatname(f->subclass.codec));
7006
			ast_getformatname(&f->subclass.format));
6988
		p->owner->nativeformats = (p->owner->nativeformats & (AST_FORMAT_VIDEO_MASK | AST_FORMAT_TEXT_MASK)) | f->subclass.codec;
7007
		ast_format_cap_remove_bytype(p->owner->nativeformats, AST_FORMAT_TYPE_AUDIO);
6989
		ast_set_read_format(p->owner, p->owner->readformat);
7008
		ast_format_cap_add(p->owner->nativeformats, &f->subclass.format);
6990
		ast_set_write_format(p->owner, p->owner->writeformat);
7009
		ast_set_read_format(p->owner, &p->owner->readformat);

    
   
7010
		ast_set_write_format(p->owner, &p->owner->writeformat);
6991
	}
7011
	}
6992

    
   
7012

   
6993
	if (f && p->dsp) {
7013
	if (f && p->dsp) {
6994
		f = ast_dsp_process(p->owner, p->dsp, f);
7014
		f = ast_dsp_process(p->owner, p->dsp, f);
6995
		if (f && f->frametype == AST_FRAME_DTMF) {
7015
		if (f && f->frametype == AST_FRAME_DTMF) {
[+20] [20] 10 lines
[+20] static struct ast_frame *sip_rtp_read(struct ast_channel *ast, struct sip_pvt *p, int *faxdetect)
7006
			} else {
7026
			} else {
7007
				ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass.integer);
7027
				ast_debug(1, "* Detected inband DTMF '%c'\n", f->subclass.integer);
7008
			}
7028
			}
7009
		}
7029
		}
7010
	}
7030
	}
7011
	
7031

   
7012
	return f;
7032
	return f;
7013
}
7033
}
7014

    
   
7034

   
7015
/*! \brief Read SIP RTP from channel */
7035
/*! \brief Read SIP RTP from channel */
7016
static struct ast_frame *sip_read(struct ast_channel *ast)
7036
static struct ast_frame *sip_read(struct ast_channel *ast)
[+20] [20] 131 lines
[+20] [+] struct sip_pvt *sip_alloc(ast_string_field callid, struct ast_sockaddr *addr,
7148

    
   
7168

   
7149
	if (!(p->cc_params = ast_cc_config_params_init())) {
7169
	if (!(p->cc_params = ast_cc_config_params_init())) {
7150
		ao2_t_ref(p, -1, "Yuck, couldn't allocate cc_params struct. Get rid o' p");
7170
		ao2_t_ref(p, -1, "Yuck, couldn't allocate cc_params struct. Get rid o' p");
7151
		return NULL;
7171
		return NULL;
7152
	}
7172
	}

    
   
7173
	p->caps = ast_format_cap_alloc_nolock();

    
   
7174
	p->jointcaps = ast_format_cap_alloc_nolock();

    
   
7175
	p->peercaps = ast_format_cap_alloc_nolock();

    
   
7176
	p->redircaps = ast_format_cap_alloc_nolock();

    
   
7177
	p->prefcaps = ast_format_cap_alloc_nolock();

    
   
7178

   

    
   
7179
	if (!p->caps|| !p->jointcaps || !p->peercaps || !p->redircaps) {

    
   
7180
		p->caps = ast_format_cap_destroy(p->caps);

    
   
7181
		p->jointcaps = ast_format_cap_destroy(p->jointcaps);

    
   
7182
		p->peercaps = ast_format_cap_destroy(p->peercaps);

    
   
7183
		p->redircaps = ast_format_cap_destroy(p->redircaps);

    
   
7184
		p->prefcaps = ast_format_cap_destroy(p->prefcaps);

    
   
7185
		ao2_t_ref(p, -1, "Yuck, couldn't allocate cc_params struct. Get rid o' p");

    
   
7186
		return NULL;

    
   
7187
	}

    
   
7188

   
7153

    
   
7189

   
7154
	/* If this dialog is created as a result of a request or response, lets store
7190
	/* If this dialog is created as a result of a request or response, lets store
7155
	 * some information about it in the dialog. */
7191
	 * some information about it in the dialog. */
7156
	if (req) {
7192
	if (req) {
7157
		char *sent_by, *branch;
7193
		struct sip_via *via;
7158
		const char *cseq = get_header(req, "Cseq");
7194
		const char *cseq = get_header(req, "Cseq");
7159
		unsigned int seqno;
7195
		unsigned int seqno;
7160

    
   
7196

   
7161
		/* get branch parameter from initial Request that started this dialog */
7197
		/* get branch parameter from initial Request that started this dialog */
7162
		get_viabranch(ast_strdupa(get_header(req, "Via")), &sent_by, &branch);
7198
		via = parse_via(get_header(req, "Via"));

    
   
7199
		if (via) {
7163
		/* only store the branch if it begins with the magic prefix "z9hG4bK", otherwise
7200
			/* only store the branch if it begins with the magic prefix "z9hG4bK", otherwise
7164
		 * it is not useful to us to have it */
7201
			 * it is not useful to us to have it */
7165
		if (!ast_strlen_zero(branch) && !strncasecmp(branch, "z9hG4bK", 7)) {
7202
			if (!ast_strlen_zero(via->branch) && !strncasecmp(via->branch, "z9hG4bK", 7)) {
7166
			ast_string_field_set(p, initviabranch, branch);
7203
				ast_string_field_set(p, initviabranch, via->branch);
7167
			ast_string_field_set(p, initviasentby, sent_by);
7204
				ast_string_field_set(p, initviasentby, via->sent_by);

    
   
7205
			}

    
   
7206
			free_via(via);
7168
		}
7207
		}
7169

    
   
7208

   
7170
		/* Store initial incoming cseq. An error in sscanf here is ignored.  There is no approperiate
7209
		/* Store initial incoming cseq. An error in sscanf here is ignored.  There is no approperiate
7171
		 * except not storing the number.  CSeq validation must take place before dialog creation in find_call */
7210
		 * except not storing the number.  CSeq validation must take place before dialog creation in find_call */
7172
		if (!ast_strlen_zero(cseq) && (sscanf(cseq, "%30u", &seqno) == 1)) {
7211
		if (!ast_strlen_zero(cseq) && (sscanf(cseq, "%30u", &seqno) == 1)) {
[+20] [20] 78 lines
[+20] struct sip_pvt *sip_alloc(ast_string_field callid, struct ast_sockaddr *addr,
7251
	else
7290
	else
7252
		ast_string_field_set(p, callid, callid);
7291
		ast_string_field_set(p, callid, callid);
7253
	/* Assign default music on hold class */
7292
	/* Assign default music on hold class */
7254
	ast_string_field_set(p, mohinterpret, default_mohinterpret);
7293
	ast_string_field_set(p, mohinterpret, default_mohinterpret);
7255
	ast_string_field_set(p, mohsuggest, default_mohsuggest);
7294
	ast_string_field_set(p, mohsuggest, default_mohsuggest);
7256
	p->capability = sip_cfg.capability;
7295
	ast_format_cap_copy(p->caps, sip_cfg.caps);
7257
	p->allowtransfer = sip_cfg.allowtransfer;
7296
	p->allowtransfer = sip_cfg.allowtransfer;
7258
	if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
7297
	if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
7259
	    (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
7298
	    (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
7260
		p->noncodeccapability |= AST_RTP_DTMF;
7299
		p->noncodeccapability |= AST_RTP_DTMF;
7261
	if (p->udptl) {
7300
	if (p->udptl) {
[+20] [20] 7 lines
[+20] struct sip_pvt *sip_alloc(ast_string_field callid, struct ast_sockaddr *addr,
7269
	AST_LIST_HEAD_INIT_NOLOCK(&p->request_queue);
7308
	AST_LIST_HEAD_INIT_NOLOCK(&p->request_queue);
7270

    
   
7309

   
7271
	/* Add to active dialog list */
7310
	/* Add to active dialog list */
7272

    
   
7311

   
7273
	ao2_t_link(dialogs, p, "link pvt into dialogs table");
7312
	ao2_t_link(dialogs, p, "link pvt into dialogs table");
7274
	
7313

   
7275
	ast_debug(1, "Allocating new SIP dialog for %s - %s (%s)\n", callid ? callid : p->callid, sip_methods[intended_method].text, p->rtp ? "With RTP" : "No RTP");
7314
	ast_debug(1, "Allocating new SIP dialog for %s - %s (%s)\n", callid ? callid : p->callid, sip_methods[intended_method].text, p->rtp ? "With RTP" : "No RTP");
7276
	return p;
7315
	return p;
7277
}
7316
}
7278

    
   
7317

   

    
   
7318
static int process_via(struct sip_pvt *p, const struct sip_request *req)

    
   
7319
{

    
   
7320
	struct sip_via *via = parse_via(get_header(req, "Via"));

    
   
7321

   

    
   
7322
	if (!via) {

    
   
7323
		ast_log(LOG_ERROR, "error processing via header\n");

    
   
7324
		return -1;

    
   
7325
	}

    
   
7326

   

    
   
7327
	if (via->maddr) {

    
   
7328
		if (ast_sockaddr_resolve_first(&p->sa, via->maddr, PARSE_PORT_FORBID)) {

    
   
7329
			ast_log(LOG_WARNING, "Can't find address for maddr '%s'\n", via->maddr);

    
   
7330
			ast_log(LOG_ERROR, "error processing via header\n");

    
   
7331
			free_via(via);

    
   
7332
			return -1;

    
   
7333
		}

    
   
7334

   

    
   
7335
		if (via->port) {

    
   
7336
			ast_sockaddr_set_port(&p->sa, via->port);

    
   
7337
		} else {

    
   
7338
			ast_sockaddr_set_port(&p->sa, STANDARD_SIP_PORT);

    
   
7339
		}

    
   
7340

   

    
   
7341
		if (ast_sockaddr_is_ipv4_multicast(&p->sa)) {

    
   
7342
			setsockopt(sipsock, IPPROTO_IP, IP_MULTICAST_TTL, &via->ttl, sizeof(via->ttl));

    
   
7343
		}

    
   
7344
	}

    
   
7345

   

    
   
7346
	free_via(via);
Moved from 20547

    
   
7347
	return 0;
Moved from 20548

    
   
7348
}

    
   
7349

   
7279
/* \brief arguments used for Request/Response to matching */
7350
/* \brief arguments used for Request/Response to matching */
7280
struct match_req_args {
7351
struct match_req_args {
7281
	int method;
7352
	int method;
7282
	const char *callid;
7353
	const char *callid;
7283
	const char *totag;
7354
	const char *totag;
[+20] [20] 287 lines
[+20] [+] static struct sip_pvt *find_call(struct sip_request *req, struct ast_sockaddr *addr, const int intended_method)
7571
		struct ao2_iterator *iterator = ao2_t_callback(dialogs,
7642
		struct ao2_iterator *iterator = ao2_t_callback(dialogs,
7572
			OBJ_POINTER | OBJ_MULTIPLE,
7643
			OBJ_POINTER | OBJ_MULTIPLE,
7573
			dialog_find_multiple,
7644
			dialog_find_multiple,
7574
			&tmp_dialog,
7645
			&tmp_dialog,
7575
			"pedantic ao2_find in dialogs");
7646
			"pedantic ao2_find in dialogs");

    
   
7647
		struct sip_via *via = NULL;
7576

    
   
7648

   
7577
		args.method = req->method;
7649
		args.method = req->method;
7578
		args.callid = NULL; /* we already matched this. */
7650
		args.callid = NULL; /* we already matched this. */
7579
		args.totag = totag;
7651
		args.totag = totag;
7580
		args.fromtag = fromtag;
7652
		args.fromtag = fromtag;
7581
		args.seqno = seqno;
7653
		args.seqno = seqno;
7582
		/* get via header information. */
7654
		/* get via header information. */
7583
		args.ruri = REQ_OFFSET_TO_STR(req, rlPart2);
7655
		args.ruri = REQ_OFFSET_TO_STR(req, rlPart2);
7584
		get_viabranch(ast_strdupa(get_header(req, "Via")), (char **) &args.viasentby, (char **) &args.viabranch);
7656
		via = parse_via(get_header(req, "Via"));

    
   
7657
		if (via) {

    
   
7658
			args.viasentby = via->sent_by;

    
   
7659
			args.viabranch = via->branch;

    
   
7660
		}
7585
		/* determine if this is a Request with authentication credentials. */
7661
		/* determine if this is a Request with authentication credentials. */
7586
		if (!ast_strlen_zero(get_header(req, "Authorization")) ||
7662
		if (!ast_strlen_zero(get_header(req, "Authorization")) ||
7587
			!ast_strlen_zero(get_header(req, "Proxy-Authorization"))) {
7663
			!ast_strlen_zero(get_header(req, "Proxy-Authorization"))) {
7588
			args.authentication_present = 1;
7664
			args.authentication_present = 1;
7589
		}
7665
		}
[+20] [20] 13 lines
[+20] static struct sip_pvt *find_call(struct sip_request *req, struct ast_sockaddr *addr, const int intended_method)
7603
			switch (found) {
7679
			switch (found) {
7604
			case SIP_REQ_MATCH:
7680
			case SIP_REQ_MATCH:
7605
				sip_pvt_lock(sip_pvt_ptr);
7681
				sip_pvt_lock(sip_pvt_ptr);
7606
				ao2_iterator_destroy(iterator);
7682
				ao2_iterator_destroy(iterator);
7607
				dialog_unref(fork_pvt, "unref fork_pvt");
7683
				dialog_unref(fork_pvt, "unref fork_pvt");

    
   
7684
				free_via(via);
7608
				return sip_pvt_ptr; /* return pvt with ref */
7685
				return sip_pvt_ptr; /* return pvt with ref */
7609
			case SIP_REQ_LOOP_DETECTED:
7686
			case SIP_REQ_LOOP_DETECTED:
7610
				/* This is likely a forked Request that somehow resulted in us receiving multiple parts of the fork.
7687
				/* This is likely a forked Request that somehow resulted in us receiving multiple parts of the fork.
7611
			 	* RFC 3261 section 8.2.2.2, Indicate that we want to merge requests by sending a 482 response. */
7688
			 	* RFC 3261 section 8.2.2.2, Indicate that we want to merge requests by sending a 482 response. */
7612
				transmit_response_using_temp(callid, addr, 1, intended_method, req, "482 (Loop Detected)");
7689
				transmit_response_using_temp(callid, addr, 1, intended_method, req, "482 (Loop Detected)");
7613
				dialog_unref(sip_pvt_ptr, "pvt did not match incoming SIP msg, unref from search.");
7690
				dialog_unref(sip_pvt_ptr, "pvt did not match incoming SIP msg, unref from search.");
7614
				ao2_iterator_destroy(iterator);
7691
				ao2_iterator_destroy(iterator);
7615
				dialog_unref(fork_pvt, "unref fork_pvt");
7692
				dialog_unref(fork_pvt, "unref fork_pvt");

    
   
7693
				free_via(via);
7616
				return NULL;
7694
				return NULL;
7617
			case SIP_REQ_FORKED:
7695
			case SIP_REQ_FORKED:
7618
				dialog_unref(fork_pvt, "throwing way pvt to fork off of.");
7696
				dialog_unref(fork_pvt, "throwing way pvt to fork off of.");
7619
				fork_pvt = dialog_ref(sip_pvt_ptr, "this pvt has a forked request, save this off to copy information into new dialog\n");
7697
				fork_pvt = dialog_ref(sip_pvt_ptr, "this pvt has a forked request, save this off to copy information into new dialog\n");
7620
				/* fall through */
7698
				/* fall through */
[+20] [20] 11 lines
[+20] static struct sip_pvt *find_call(struct sip_request *req, struct ast_sockaddr *addr, const int intended_method)
7632
			/* XXX right now we only support handling forked INVITE Requests. Any other
7710
			/* XXX right now we only support handling forked INVITE Requests. Any other
7633
			 * forked request type must be added here. */
7711
			 * forked request type must be added here. */
7634
			if (fork_pvt->method == SIP_INVITE) {
7712
			if (fork_pvt->method == SIP_INVITE) {
7635
				forked_invite_init(req, args.totag, fork_pvt, addr);
7713
				forked_invite_init(req, args.totag, fork_pvt, addr);
7636
				dialog_unref(fork_pvt, "throwing way old forked pvt");
7714
				dialog_unref(fork_pvt, "throwing way old forked pvt");

    
   
7715
				free_via(via);
7637
				return NULL;
7716
				return NULL;
7638
			}
7717
			}
7639
			fork_pvt = dialog_unref(fork_pvt, "throwing way pvt to fork off of");
7718
			fork_pvt = dialog_unref(fork_pvt, "throwing way pvt to fork off of");
7640
		}
7719
		}

    
   
7720

   

    
   
7721
		free_via(via);
7641
	} /* end of pedantic mode Request/Reponse to Dialog matching */
7722
	} /* end of pedantic mode Request/Reponse to Dialog matching */
7642

    
   
7723

   
7643
	/* See if the method is capable of creating a dialog */
7724
	/* See if the method is capable of creating a dialog */
7644
	if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
7725
	if (sip_methods[intended_method].can_create == CAN_CREATE_DIALOG) {
7645
		struct sip_pvt *p = NULL;
7726
		struct sip_pvt *p = NULL;
[+20] [20] 570 lines
[+20] [+] static int get_ip_and_port_from_sdp(struct sip_request *req, const enum media_type media, struct ast_sockaddr *addr)
8216
 	Return 0 on success, a negative value on errors.
8297
 	Return 0 on success, a negative value on errors.
8217
	Must be called after find_sdp().
8298
	Must be called after find_sdp().
8218
*/
8299
*/
8219
static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8300
static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8220
{
8301
{

    
   
8302
	int res = 0;

    
   
8303

   
8221
	/* Iterators for SDP parsing */
8304
	/* Iterators for SDP parsing */
8222
	int start = req->sdp_start;
8305
	int start = req->sdp_start;
8223
	int next = start;
8306
	int next = start;
8224
	int iterator = start;
8307
	int iterator = start;
8225

    
   
8308

   
[+20] [20] 18 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8244
 	int vportno = -1;		/*!< RTP Video port number */
8327
	int vportno = -1;		/*!< RTP Video port number */
8245
	int tportno = -1;		/*!< RTP Text port number */
8328
	int tportno = -1;		/*!< RTP Text port number */
8246
	int udptlportno = -1;		/*!< UDPTL Image port number */
8329
	int udptlportno = -1;		/*!< UDPTL Image port number */
8247

    
   
8330

   
8248
	/* Peer capability is the capability in the SDP, non codec is RFC2833 DTMF (101) */	
8331
	/* Peer capability is the capability in the SDP, non codec is RFC2833 DTMF (101) */	
8249
	format_t peercapability = 0, vpeercapability = 0, tpeercapability = 0;
8332
	struct ast_format_cap *peercapability = ast_format_cap_alloc_nolock();

    
   
8333
	struct ast_format_cap *vpeercapability = ast_format_cap_alloc_nolock();

    
   
8334
	struct ast_format_cap *tpeercapability = ast_format_cap_alloc_nolock();

    
   
8335

   
8250
	int peernoncodeccapability = 0, vpeernoncodeccapability = 0, tpeernoncodeccapability = 0;
8336
	int peernoncodeccapability = 0, vpeernoncodeccapability = 0, tpeernoncodeccapability = 0;
8251

    
   
8337

   
8252
	struct ast_rtp_codecs newaudiortp, newvideortp, newtextrtp;
8338
	struct ast_rtp_codecs newaudiortp, newvideortp, newtextrtp;
8253
	format_t newjointcapability;				/* Negotiated capability */
8339
	struct ast_format_cap *newjointcapability = ast_format_cap_alloc_nolock(); /* Negotiated capability */
8254
	format_t newpeercapability;
8340
	struct ast_format_cap *newpeercapability = ast_format_cap_alloc_nolock();
8255
	int newnoncodeccapability;
8341
	int newnoncodeccapability;
8256

    
   
8342

   
8257
	const char *codecs;
8343
	const char *codecs;
8258
	int codec;
8344
	int codec;
8259

    
   
8345

   
[+20] [20] 12 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8272
	char red_fmtp[100] = "empty";	/* For T.140 red */
8358
	char red_fmtp[100] = "empty";	/* For T.140 red */
8273
	int debug = sip_debug_test_pvt(p);
8359
	int debug = sip_debug_test_pvt(p);
8274

    
   
8360

   
8275
	/* START UNKNOWN */
8361
	/* START UNKNOWN */
8276
	char buf[SIPBUFSIZE];
8362
	char buf[SIPBUFSIZE];

    
   
8363
	struct ast_format tmp_fmt;
8277
	/* END UNKNOWN */
8364
	/* END UNKNOWN */
8278

    
   
8365

   
8279
	/* Initial check */
8366
	/* Initial check */
8280
	if (!p->rtp) {
8367
	if (!p->rtp) {
8281
		ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
8368
		ast_log(LOG_ERROR, "Got SDP but have no RTP session allocated.\n");
8282
		return -1;
8369
		res = -1;

    
   
8370
		goto process_sdp_cleanup;

    
   
8371
	}

    
   
8372
	if (!peercapability || !vpeercapability || !tpeercapability || !newpeercapability || !newjointcapability) {

    
   
8373
		res = -1;

    
   
8374
		goto process_sdp_cleanup;
8283
	}
8375
	}
8284

    
   
8376

   
8285
	/* Make sure that the codec structures are all cleared out */
8377
	/* Make sure that the codec structures are all cleared out */
8286
	ast_rtp_codecs_payloads_clear(&newaudiortp, NULL);
8378
	ast_rtp_codecs_payloads_clear(&newaudiortp, NULL);
8287
	ast_rtp_codecs_payloads_clear(&newvideortp, NULL);
8379
	ast_rtp_codecs_payloads_clear(&newvideortp, NULL);
[+20] [20] 19 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8307

    
   
8399

   
8308
	/* Scan for the first media stream (m=) line to limit scanning of globals */
8400
	/* Scan for the first media stream (m=) line to limit scanning of globals */
8309
	nextm = get_sdp_iterate(&next, req, "m");
8401
	nextm = get_sdp_iterate(&next, req, "m");
8310
	if (ast_strlen_zero(nextm)) {
8402
	if (ast_strlen_zero(nextm)) {
8311
		ast_log(LOG_WARNING, "Insufficient information for SDP (m= not found)\n");
8403
		ast_log(LOG_WARNING, "Insufficient information for SDP (m= not found)\n");
8312
 		return -1;
8404
		res = -1;

    
   
8405
		goto process_sdp_cleanup;
8313
 	}
8406
 	}
8314

    
   
8407

   
8315
	/* Scan session level SDP parameters (lines before first media stream) */
8408
	/* Scan session level SDP parameters (lines before first media stream) */
8316
	while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
8409
	while ((type = get_sdp_line(&iterator, next - 1, req, &value)) != '\0') {
8317
		int processed = FALSE;
8410
		int processed = FALSE;
8318
		switch (type) {
8411
		switch (type) {
8319
		case 'o':
8412
		case 'o':
8320
			/* If we end up receiving SDP that doesn't actually modify the session we don't want to treat this as a fatal
8413
			/* If we end up receiving SDP that doesn't actually modify the session we don't want to treat this as a fatal
8321
			 * error. We just want to ignore the SDP and let the rest of the packet be handled as normal.
8414
			 * error. We just want to ignore the SDP and let the rest of the packet be handled as normal.
8322
			 */
8415
			 */
8323
			if (!process_sdp_o(value, p))
8416
			if (!process_sdp_o(value, p)) {
8324
				return (p->session_modify == FALSE) ? 0 : -1;
8417
				res = (p->session_modify == FALSE) ? 0 : -1;

    
   
8418
				goto process_sdp_cleanup;

    
   
8419
			}
8325
			break;
8420
			break;
8326
		case 'c':
8421
		case 'c':
8327
			if (process_sdp_c(value, &sessionsa)) {
8422
			if (process_sdp_c(value, &sessionsa)) {
8328
				processed = TRUE;
8423
				processed = TRUE;
8329
				sa = &sessionsa;
8424
				sa = &sessionsa;
[+20] [20] 57 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8387
			codecs = m + len;
8482
			codecs = m + len;
8388
			ast_copy_string(p->offered_media[SDP_AUDIO].codecs, codecs, sizeof(p->offered_media[SDP_AUDIO].codecs));
8483
			ast_copy_string(p->offered_media[SDP_AUDIO].codecs, codecs, sizeof(p->offered_media[SDP_AUDIO].codecs));
8389
			for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
8484
			for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
8390
				if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
8485
				if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
8391
					ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
8486
					ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
8392
					return -1;
8487
					res = -1;

    
   
8488
					goto process_sdp_cleanup;
8393
				}
8489
				}
8394
				if (debug)
8490
				if (debug)
8395
					ast_verbose("Found RTP audio format %d\n", codec);
8491
					ast_verbose("Found RTP audio format %d\n", codec);
8396
				
8492
				
8397
				ast_rtp_codecs_payloads_set_m_type(&newaudiortp, NULL, codec);
8493
				ast_rtp_codecs_payloads_set_m_type(&newaudiortp, NULL, codec);
[+20] [20] 17 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8415
			codecs = m + len;
8511
			codecs = m + len;
8416
			ast_copy_string(p->offered_media[SDP_VIDEO].codecs, codecs, sizeof(p->offered_media[SDP_VIDEO].codecs));
8512
			ast_copy_string(p->offered_media[SDP_VIDEO].codecs, codecs, sizeof(p->offered_media[SDP_VIDEO].codecs));
8417
			for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
8513
			for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
8418
				if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
8514
				if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
8419
					ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
8515
					ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
8420
					return -1;
8516
					res = -1;

    
   
8517
					goto process_sdp_cleanup;
8421
				}
8518
				}
8422
				if (debug)
8519
				if (debug)
8423
					ast_verbose("Found RTP video format %d\n", codec);
8520
					ast_verbose("Found RTP video format %d\n", codec);
8424
				ast_rtp_codecs_payloads_set_m_type(&newvideortp, NULL, codec);
8521
				ast_rtp_codecs_payloads_set_m_type(&newvideortp, NULL, codec);
8425
			}
8522
			}
[+20] [20] 10 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8436
			codecs = m + len;
8533
			codecs = m + len;
8437
			ast_copy_string(p->offered_media[SDP_TEXT].codecs, codecs, sizeof(p->offered_media[SDP_TEXT].codecs));
8534
			ast_copy_string(p->offered_media[SDP_TEXT].codecs, codecs, sizeof(p->offered_media[SDP_TEXT].codecs));
8438
			for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
8535
			for (; !ast_strlen_zero(codecs); codecs = ast_skip_blanks(codecs + len)) {
8439
				if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
8536
				if (sscanf(codecs, "%30u%n", &codec, &len) != 1) {
8440
					ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
8537
					ast_log(LOG_WARNING, "Error in codec string '%s'\n", codecs);
8441
					return -1;
8538
					res = -1;

    
   
8539
					goto process_sdp_cleanup;
8442
				}
8540
				}
8443
				if (debug)
8541
				if (debug)
8444
					ast_verbose("Found RTP text format %d\n", codec);
8542
					ast_verbose("Found RTP text format %d\n", codec);
8445
				ast_rtp_codecs_payloads_set_m_type(&newtextrtp, NULL, codec);
8543
				ast_rtp_codecs_payloads_set_m_type(&newtextrtp, NULL, codec);
8446
			}
8544
			}
[+20] [20] 94 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8541

    
   
8639

   
8542

    
   
8640

   
8543
	/* Sanity checks */
8641
	/* Sanity checks */
8544
	if (!sa && !vsa && !tsa && !isa) {
8642
	if (!sa && !vsa && !tsa && !isa) {
8545
		ast_log(LOG_WARNING, "Insufficient information in SDP (c=)...\n");
8643
		ast_log(LOG_WARNING, "Insufficient information in SDP (c=)...\n");
8546
		return -1;
8644
		res = -1;

    
   
8645
		goto process_sdp_cleanup;
8547
	}
8646
	}
8548

    
   
8647

   
8549
	if (portno == -1 && vportno == -1 && udptlportno == -1  && tportno == -1) {
8648
	if (portno == -1 && vportno == -1 && udptlportno == -1  && tportno == -1) {
8550
		/* No acceptable offer found in SDP  - we have no ports */
8649
		/* No acceptable offer found in SDP  - we have no ports */
8551
		/* Do not change RTP or VRTP if this is a re-invite */
8650
		/* Do not change RTP or VRTP if this is a re-invite */
8552
		ast_log(LOG_WARNING, "Failing due to no acceptable offer found\n");
8651
		ast_log(LOG_WARNING, "Failing due to no acceptable offer found\n");
8553
		return -2;
8652
		res = -2;

    
   
8653
		goto process_sdp_cleanup;
8554
	}
8654
	}
8555

    
   
8655

   
8556
	if (numberofmediastreams > 3) {
8656
	if (numberofmediastreams > 3) {
8557
		/* We have too many fax, audio and/or video and/or text media streams, fail this offer */
8657
		/* We have too many fax, audio and/or video and/or text media streams, fail this offer */
8558
		ast_log(LOG_WARNING, "Faling due to too many media streams\n");
8658
		ast_log(LOG_WARNING, "Faling due to too many media streams\n");
8559
		return -3;
8659
		res = -3;

    
   
8660
		goto process_sdp_cleanup;
8560
	}
8661
	}
8561

    
   
8662

   
8562
	if (secure_audio && !(p->srtp && (ast_test_flag(p->srtp, SRTP_CRYPTO_OFFER_OK)))) {
8663
	if (secure_audio && !(p->srtp && (ast_test_flag(p->srtp, SRTP_CRYPTO_OFFER_OK)))) {
8563
		ast_log(LOG_WARNING, "Can't provide secure audio requested in SDP offer\n");
8664
		ast_log(LOG_WARNING, "Can't provide secure audio requested in SDP offer\n");
8564
		return -4;
8665
		res = -4;

    
   
8666
		goto process_sdp_cleanup;
8565
	}
8667
	}
8566

    
   
8668

   
8567
	if (!secure_audio && p->srtp) {
8669
	if (!secure_audio && p->srtp) {
8568
		ast_log(LOG_WARNING, "We are requesting SRTP, but they responded without it!\n");
8670
		ast_log(LOG_WARNING, "We are requesting SRTP, but they responded without it!\n");
8569
		return -4;
8671
		res = -4;

    
   
8672
		goto process_sdp_cleanup;
8570
	}
8673
	}
8571

    
   
8674

   
8572
	if (secure_video && !(p->vsrtp && (ast_test_flag(p->vsrtp, SRTP_CRYPTO_OFFER_OK)))) {
8675
	if (secure_video && !(p->vsrtp && (ast_test_flag(p->vsrtp, SRTP_CRYPTO_OFFER_OK)))) {
8573
		ast_log(LOG_WARNING, "Can't provide secure video requested in SDP offer\n");
8676
		ast_log(LOG_WARNING, "Can't provide secure video requested in SDP offer\n");
8574
		return -4;
8677
		res = -4;

    
   
8678
		goto process_sdp_cleanup;
8575
	}
8679
	}
8576

    
   
8680

   
8577
	if (!p->novideo && !secure_video && p->vsrtp) {
8681
	if (!p->novideo && !secure_video && p->vsrtp) {
8578
		ast_log(LOG_WARNING, "We are requesting SRTP, but they responded without it!\n");
8682
		ast_log(LOG_WARNING, "We are requesting SRTP, but they responded without it!\n");
8579
		return -4;
8683
		res = -4;

    
   
8684
		goto process_sdp_cleanup;
8580
	}
8685
	}
8581

    
   
8686

   
8582
	if (!(secure_audio || secure_video) && ast_test_flag(&p->flags[1], SIP_PAGE2_USE_SRTP)) {
8687
	if (!(secure_audio || secure_video) && ast_test_flag(&p->flags[1], SIP_PAGE2_USE_SRTP)) {
8583
		ast_log(LOG_WARNING, "Matched device setup to use SRTP, but request was not!\n");
8688
		ast_log(LOG_WARNING, "Matched device setup to use SRTP, but request was not!\n");
8584
		return -4;
8689
		res = -4;

    
   
8690
		goto process_sdp_cleanup;
8585
	}
8691
	}
8586

    
   
8692

   
8587
	if (udptlportno == -1) {
8693
	if (udptlportno == -1) {
8588
		change_t38_state(p, T38_DISABLED);
8694
		change_t38_state(p, T38_DISABLED);
8589
	}
8695
	}
8590

    
   
8696

   
8591
	/* Now gather all of the codecs that we are asked for: */
8697
	/* Now gather all of the codecs that we are asked for: */
8592
	ast_rtp_codecs_payload_formats(&newaudiortp, &peercapability, &peernoncodeccapability);
8698
	ast_rtp_codecs_payload_formats(&newaudiortp, peercapability, &peernoncodeccapability);
8593
	ast_rtp_codecs_payload_formats(&newvideortp, &vpeercapability, &vpeernoncodeccapability);
8699
	ast_rtp_codecs_payload_formats(&newvideortp, vpeercapability, &vpeernoncodeccapability);
8594
	ast_rtp_codecs_payload_formats(&newtextrtp, &tpeercapability, &tpeernoncodeccapability);
8700
	ast_rtp_codecs_payload_formats(&newtextrtp, tpeercapability, &tpeernoncodeccapability);

    
   
8701

   

    
   
8702
	ast_format_cap_append(newpeercapability, peercapability);

    
   
8703
	ast_format_cap_append(newpeercapability, vpeercapability);

    
   
8704
	ast_format_cap_append(newpeercapability, tpeercapability);

    
   
8705

   

    
   
8706
	ast_format_cap_joint_copy(p->caps, newpeercapability, newjointcapability);

    
   
8707
	if (ast_format_cap_is_empty(newjointcapability) && (portno != -1)) {
Moved from 8622

    
   
8708
		ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
Moved from 8623

    
   
8709
		/* Do NOT Change current setting */

    
   
8710
		res = -1;

    
   
8711
		goto process_sdp_cleanup;

    
   
8712
	}
8595

    
   
8713

   
8596
	newjointcapability = p->capability & (peercapability | vpeercapability | tpeercapability);

   
8597
	newpeercapability = (peercapability | vpeercapability | tpeercapability);

   
8598
	newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
8714
	newnoncodeccapability = p->noncodeccapability & peernoncodeccapability;
8599

    
   
8715

   
8600
	if (debug) {
8716
	if (debug) {
8601
		/* shame on whoever coded this.... */
8717
		/* shame on whoever coded this.... */
8602
		char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE], s5[SIPBUFSIZE];
8718
		char s1[SIPBUFSIZE], s2[SIPBUFSIZE], s3[SIPBUFSIZE], s4[SIPBUFSIZE], s5[SIPBUFSIZE];
8603

    
   
8719

   
8604
		ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s/text=%s, combined - %s\n",
8720
		ast_verbose("Capabilities: us - %s, peer - audio=%s/video=%s/text=%s, combined - %s\n",
8605
			    ast_getformatname_multiple(s1, SIPBUFSIZE, p->capability),
8721
			    ast_getformatname_multiple(s1, SIPBUFSIZE, p->caps),
8606
			    ast_getformatname_multiple(s2, SIPBUFSIZE, peercapability),
8722
			    ast_getformatname_multiple(s2, SIPBUFSIZE, peercapability),
8607
			    ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
8723
			    ast_getformatname_multiple(s3, SIPBUFSIZE, vpeercapability),
8608
			    ast_getformatname_multiple(s4, SIPBUFSIZE, tpeercapability),
8724
			    ast_getformatname_multiple(s4, SIPBUFSIZE, tpeercapability),
8609
			    ast_getformatname_multiple(s5, SIPBUFSIZE, newjointcapability));
8725
			    ast_getformatname_multiple(s5, SIPBUFSIZE, newjointcapability));
8610
	}
8726
	}
8611
	if (debug) {
8727
	if (debug) {
8612
		struct ast_str *s1 = ast_str_alloca(SIPBUFSIZE);
8728
		struct ast_str *s1 = ast_str_alloca(SIPBUFSIZE);
8613
		struct ast_str *s2 = ast_str_alloca(SIPBUFSIZE);
8729
		struct ast_str *s2 = ast_str_alloca(SIPBUFSIZE);
8614
		struct ast_str *s3 = ast_str_alloca(SIPBUFSIZE);
8730
		struct ast_str *s3 = ast_str_alloca(SIPBUFSIZE);
8615

    
   
8731

   
8616
		ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
8732
		ast_verbose("Non-codec capabilities (dtmf): us - %s, peer - %s, combined - %s\n",
8617
			    ast_rtp_lookup_mime_multiple2(s1, p->noncodeccapability, 0, 0),
8733
			    ast_rtp_lookup_mime_multiple2(s1, NULL, p->noncodeccapability, 0, 0),
8618
			    ast_rtp_lookup_mime_multiple2(s2, peernoncodeccapability, 0, 0),
8734
			    ast_rtp_lookup_mime_multiple2(s2, NULL, peernoncodeccapability, 0, 0),
8619
			    ast_rtp_lookup_mime_multiple2(s3, newnoncodeccapability, 0, 0));
8735
			    ast_rtp_lookup_mime_multiple2(s3, NULL, newnoncodeccapability, 0, 0));
8620
	}

   
8621
	if (!newjointcapability && (portno != -1)) {

   
8622
		ast_log(LOG_NOTICE, "No compatible codecs, not accepting this offer!\n");
Moved to 8708

   
8623
		/* Do NOT Change current setting */
Moved to 8709

   
8624
		return -1;

   
8625
	}
8736
	}
8626

    
   
8737

   
8627
	/* Setup audio address and port */
8738
	/* Setup audio address and port */
8628
	if (p->rtp) {
8739
	if (p->rtp) {
8629
		if (portno > 0) {
8740
		if (portno > 0) {
8630
			ast_sockaddr_set_port(sa, portno);
8741
			ast_sockaddr_set_port(sa, portno);
8631
			ast_rtp_instance_set_remote_address(p->rtp, sa);
8742
			ast_rtp_instance_set_remote_address(p->rtp, sa);
8632
			if (debug) {
8743
			if (debug) {
8633
				ast_verbose("Peer audio RTP is at port %s\n",
8744
				ast_verbose("Peer audio RTP is at port %s\n",
8634
					    ast_sockaddr_stringify(sa));
8745
					    ast_sockaddr_stringify(sa));
8635
			}
8746
			}
8636
			/* We are now ready to change the sip session and p->rtp and p->vrtp with the offered codecs, since
8747
			/* We are now ready to change the sip session and p->rtp and p->vrtp with the offered codecs, since
8637
			   they are acceptable */
8748
			   they are acceptable */
8638
			p->jointcapability = newjointcapability;                /* Our joint codec profile for this call */
8749
			ast_format_cap_copy(p->jointcaps, newjointcapability);                /* Our joint codec profile for this call */
8639
			p->peercapability = newpeercapability;                  /* The other sides capability in latest offer */
8750
			ast_format_cap_copy(p->peercaps, newpeercapability);                  /* The other sides capability in latest offer */
8640
			p->jointnoncodeccapability = newnoncodeccapability;     /* DTMF capabilities */
8751
			p->jointnoncodeccapability = newnoncodeccapability;     /* DTMF capabilities */
8641

    
   
8752

   
8642
			if (ast_test_flag(&p->flags[1], SIP_PAGE2_PREFERRED_CODEC)) { /* respond with single most preferred joint codec, limiting the other side's choice */
8753
			if (ast_test_flag(&p->flags[1], SIP_PAGE2_PREFERRED_CODEC)) { /* respond with single most preferred joint codec, limiting the other side's choice */
8643
				p->jointcapability = ast_codec_choose(&p->prefs, p->jointcapability, 1);
8754
				ast_codec_choose(&p->prefs, p->jointcaps, 1, &tmp_fmt);

    
   
8755
				ast_format_cap_set(p->jointcaps, &tmp_fmt);
8644
			}
8756
			}
8645

    
   
8757

   
8646
			ast_rtp_codecs_payloads_copy(&newaudiortp, ast_rtp_instance_get_codecs(p->rtp), p->rtp);
8758
			ast_rtp_codecs_payloads_copy(&newaudiortp, ast_rtp_instance_get_codecs(p->rtp), p->rtp);
8647

    
   
8759

   
8648
			if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
8760
			if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) {
[+20] [20] 42 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8691
			ast_rtp_instance_set_remote_address(p->trtp, tsa);
8803
			ast_rtp_instance_set_remote_address(p->trtp, tsa);
8692
			if (debug) {
8804
			if (debug) {
8693
				ast_verbose("Peer T.140 RTP is at port %s\n",
8805
				ast_verbose("Peer T.140 RTP is at port %s\n",
8694
					    ast_sockaddr_stringify(tsa));
8806
					    ast_sockaddr_stringify(tsa));
8695
			}
8807
			}
8696
			if ((p->jointcapability & AST_FORMAT_T140RED)) {
8808
			if (ast_format_cap_iscompatible(p->jointcaps, ast_format_set(&tmp_fmt, AST_FORMAT_T140RED, 0))) {
8697
				p->red = 1;
8809
				p->red = 1;
8698
				ast_rtp_red_init(p->trtp, 300, red_data_pt, 2);
8810
				ast_rtp_red_init(p->trtp, 300, red_data_pt, 2);
8699
			} else {
8811
			} else {
8700
				p->red = 0;
8812
				p->red = 0;
8701
			}
8813
			}
[+20] [20] 59 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8761
		}
8873
		}
8762
	}
8874
	}
8763

    
   
8875

   
8764
	if ((portno == -1) && (p->t38.state != T38_DISABLED)) {
8876
	if ((portno == -1) && (p->t38.state != T38_DISABLED)) {
8765
		ast_debug(3, "Have T.38 but no audio, accepting offer anyway\n");
8877
		ast_debug(3, "Have T.38 but no audio, accepting offer anyway\n");
8766
		return 0;
8878
		res = 0;

    
   
8879
		goto process_sdp_cleanup;
8767
        }
8880
	}
8768

    
   
8881

   
8769
	/* Ok, we're going with this offer */
8882
	/* Ok, we're going with this offer */
8770
	ast_debug(2, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcapability));
8883
	ast_debug(2, "We're settling with these formats: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, p->jointcaps));
8771

    
   
8884

   
8772
	if (!p->owner) 	/* There's no open channel owning us so we can return here. For a re-invite or so, we proceed */
8885
	if (!p->owner) { /* There's no open channel owning us so we can return here. For a re-invite or so, we proceed */
8773
		return 0;
8886
		res = 0;

    
   
8887
		goto process_sdp_cleanup;

    
   
8888
	}
8774

    
   
8889

   
8775
	ast_debug(4, "We have an owner, now see if we need to change this call\n");
8890
	ast_debug(4, "We have an owner, now see if we need to change this call\n");
8776

    
   
8891
	if (ast_format_cap_has_type(p->jointcaps, AST_FORMAT_TYPE_AUDIO)) {
8777
	if (!(p->owner->nativeformats & p->jointcapability) && (p->jointcapability & AST_FORMAT_AUDIO_MASK)) {

   
8778
		if (debug) {
8892
		if (debug) {
8779
			char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
8893
			char s1[SIPBUFSIZE], s2[SIPBUFSIZE];
8780
			ast_debug(1, "Oooh, we need to change our audio formats since our peer supports only %s and not %s\n",
8894
			ast_debug(1, "Setting native formats after processing SDP. peer joint formats %s, old nativeformats %s\n",
8781
				ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcapability),
8895
				ast_getformatname_multiple(s1, SIPBUFSIZE, p->jointcaps),
8782
				ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
8896
				ast_getformatname_multiple(s2, SIPBUFSIZE, p->owner->nativeformats));
8783
		}
8897
		}
8784
		p->owner->nativeformats = ast_codec_choose(&p->prefs, p->jointcapability, 1) | (p->capability & vpeercapability) | (p->capability & tpeercapability);
8898

   
8785
		ast_set_read_format(p->owner, p->owner->readformat);
8899
		ast_codec_choose(&p->prefs, p->jointcaps, 1, &tmp_fmt);
8786
		ast_set_write_format(p->owner, p->owner->writeformat);
8900

   

    
   
8901
		ast_format_cap_set(p->owner->nativeformats, &tmp_fmt);

    
   
8902
		ast_format_cap_joint_append(p->caps, vpeercapability, p->owner->nativeformats);

    
   
8903
		ast_format_cap_joint_append(p->caps, tpeercapability, p->owner->nativeformats);

    
   
8904

   

    
   
8905
		ast_set_read_format(p->owner, &p->owner->readformat);

    
   
8906
		ast_set_write_format(p->owner, &p->owner->writeformat);
8787
	}
8907
	}
8788
	
8908

   
8789
	if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && (!ast_sockaddr_isnull(sa) || !ast_sockaddr_isnull(vsa) || !ast_sockaddr_isnull(tsa) || !ast_sockaddr_isnull(isa)) && (!sendonly || sendonly == -1)) {
8909
	if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) && (!ast_sockaddr_isnull(sa) || !ast_sockaddr_isnull(vsa) || !ast_sockaddr_isnull(tsa) || !ast_sockaddr_isnull(isa)) && (!sendonly || sendonly == -1)) {
8790
		ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
8910
		ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
8791
		/* Activate a re-invite */
8911
		/* Activate a re-invite */
8792
		ast_queue_frame(p->owner, &ast_null_frame);
8912
		ast_queue_frame(p->owner, &ast_null_frame);
8793
		change_hold_state(p, req, FALSE, sendonly);
8913
		change_hold_state(p, req, FALSE, sendonly);
[+20] [20] 6 lines
[+20] static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action)
8800
		/* RTCP needs to go ahead, even if we're on hold!!! */
8920
		/* RTCP needs to go ahead, even if we're on hold!!! */
8801
		/* Activate a re-invite */
8921
		/* Activate a re-invite */
8802
		ast_queue_frame(p->owner, &ast_null_frame);
8922
		ast_queue_frame(p->owner, &ast_null_frame);
8803
		change_hold_state(p, req, TRUE, sendonly);
8923
		change_hold_state(p, req, TRUE, sendonly);
8804
	}
8924
	}
8805
	
8925

   
8806
	return 0;
8926
process_sdp_cleanup:

    
   
8927
	ast_format_cap_destroy(peercapability);

    
   
8928
	ast_format_cap_destroy(vpeercapability);

    
   
8929
	ast_format_cap_destroy(tpeercapability);

    
   
8930
	ast_format_cap_destroy(newjointcapability);

    
   
8931
	ast_format_cap_destroy(newpeercapability);

    
   
8932
	return res;
8807
}
8933
}
8808

    
   
8934

   
8809
static int process_sdp_o(const char *o, struct sip_pvt *p)
8935
static int process_sdp_o(const char *o, struct sip_pvt *p)
8810
{
8936
{
8811
	char *o_copy;
8937
	char *o_copy;
[+20] [20] 143 lines
[+20] [+] static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec)
8955
		if (framing && p->autoframing) {
9081
		if (framing && p->autoframing) {
8956
			struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
9082
			struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
8957
			int codec_n;
9083
			int codec_n;
8958
			for (codec_n = 0; codec_n < AST_RTP_MAX_PT; codec_n++) {
9084
			for (codec_n = 0; codec_n < AST_RTP_MAX_PT; codec_n++) {
8959
				struct ast_rtp_payload_type format = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(p->rtp), codec_n);
9085
				struct ast_rtp_payload_type format = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(p->rtp), codec_n);
8960
				if (!format.asterisk_format || !format.code)	/* non-codec or not found */
9086
				if (!format.asterisk_format)	/* non-codec or not found */
8961
					continue;
9087
					continue;
8962
				ast_debug(1, "Setting framing for %s to %ld\n", ast_getformatname(format.code), framing);
9088
				ast_debug(1, "Setting framing for %s to %ld\n", ast_getformatname(&format.format), framing);
8963
				ast_codec_pref_setsize(pref, format.code, framing);
9089
				ast_codec_pref_setsize(pref, &format.format, framing);
8964
			}
9090
			}
8965
			ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, pref);
9091
			ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, pref);
8966
		}
9092
		}
8967
		found = TRUE;
9093
		found = TRUE;
8968
	} else if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
9094
	} else if (sscanf(a, "rtpmap: %30u %127[^/]/%30u", &codec, mimeSubtype, &sample_rate) == 3) {
[+20] [20] 14 lines
[+20] static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec)
8983
		} else {
9109
		} else {
8984
			if (debug)
9110
			if (debug)
8985
				ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
9111
				ast_verbose("Discarded description format %s for ID %d\n", mimeSubtype, codec);
8986
		}
9112
		}
8987
	} else if (sscanf(a, "fmtp: %30u %63s", &codec, fmtp_string) == 2) {
9113
	} else if (sscanf(a, "fmtp: %30u %63s", &codec, fmtp_string) == 2) {
8988
		struct ast_rtp_payload_type payload;
9114
		struct ast_format *format;
8989

    
   
9115

   
8990
		payload = ast_rtp_codecs_payload_lookup(newaudiortp, codec);
9116
		if ((format = ast_rtp_codecs_get_payload_format(newaudiortp, codec))) {
8991
		if (payload.code && payload.asterisk_format) {

   
8992
			unsigned int bit_rate;
9117
			unsigned int bit_rate;
8993

    
   
9118

   
8994
			switch (payload.code) {
9119
			switch ((int) format->id) {
8995
			case AST_FORMAT_SIREN7:
9120
			case AST_FORMAT_SIREN7:
8996
				if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
9121
				if (sscanf(fmtp_string, "bitrate=%30u", &bit_rate) == 1) {
8997
					if (bit_rate != 32000) {
9122
					if (bit_rate != 32000) {
8998
						ast_log(LOG_WARNING, "Got Siren7 offer at %d bps, but only 32000 bps supported; ignoring.\n", bit_rate);
9123
						ast_log(LOG_WARNING, "Got Siren7 offer at %d bps, but only 32000 bps supported; ignoring.\n", bit_rate);
8999
						ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
9124
						ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
[+20] [20] 19 lines
[+20] static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec)
9019
						ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
9144
						ast_rtp_codecs_payloads_unset(newaudiortp, NULL, codec);
9020
					} else {
9145
					} else {
9021
						found = TRUE;
9146
						found = TRUE;
9022
					}
9147
					}
9023
				}
9148
				}

    
   
9149
				break;

    
   
9150
			case AST_FORMAT_SILK:

    
   
9151
				{

    
   
9152
					int val = 0;

    
   
9153
					if (sscanf(fmtp_string, "maxaveragebitrate=%30u", &val) == 1) {

    
   
9154
						ast_format_append(format, SILK_ATTR_KEY_MAX_BITRATE, val, AST_FORMAT_ATTR_END);

    
   
9155
					}

    
   
9156
					if (sscanf(fmtp_string, "usedtx=%30u", &val) == 1) {

    
   
9157
						ast_format_append(format, SILK_ATTR_KEY_DTX, val ? 1 : 0, AST_FORMAT_ATTR_END);

    
   
9158
					}

    
   
9159
					if (sscanf(fmtp_string, "useinbandfec=%30u", &val) == 1) {

    
   
9160
						ast_format_append(format, SILK_ATTR_KEY_FEC, val ? 1 : 0, AST_FORMAT_ATTR_END);

    
   
9161
					}

    
   
9162
					break;

    
   
9163
				}
9024
			}
9164
			}
9025
		}
9165
		}
9026
	}
9166
	}
9027

    
   
9167

   
9028
	return found;
9168
	return found;
[+20] [20] 656 lines
[+20] [+] static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
9685
	if (!ast_strlen_zero(p->url)) {
9825
	if (!ast_strlen_zero(p->url)) {
9686
		add_header(resp, "Access-URL", p->url);
9826
		add_header(resp, "Access-URL", p->url);
9687
		ast_string_field_set(p, url, NULL);
9827
		ast_string_field_set(p, url, NULL);
9688
	}
9828
	}
9689

    
   
9829

   

    
   
9830
	/* default to routing the response to the address where the request

    
   
9831
	 * came from.  Since we don't have a transport layer, we do this here.

    
   
9832
	 */

    
   
9833
	p->sa = p->recv;

    
   
9834

   

    
   
9835
	if (process_via(p, req)) {

    
   
9836
		ast_log(LOG_WARNING, "error processing via header, will send response to originating address\n");

    
   
9837
	}

    
   
9838

   
9690
	return 0;
9839
	return 0;
9691
}
9840
}
9692

    
   
9841

   
9693
/*! \brief Initialize a SIP request message (not the initial one in a dialog) */
9842
/*! \brief Initialize a SIP request message (not the initial one in a dialog) */
9694
static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
9843
static int reqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, int seqno, int newbranch)
[+20] [20] 6 lines
[+20] static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
9701
	const char *ot, *of;
9850
	const char *ot, *of;
9702
	int is_strict = FALSE;		/*!< Strict routing flag */
9851
	int is_strict = FALSE;		/*!< Strict routing flag */
9703
	int is_outbound = ast_test_flag(&p->flags[0], SIP_OUTGOING);	/* Session direction */
9852
	int is_outbound = ast_test_flag(&p->flags[0], SIP_OUTGOING);	/* Session direction */
9704

    
   
9853

   
9705
	memset(req, 0, sizeof(struct sip_request));
9854
	memset(req, 0, sizeof(struct sip_request));
9706
	
9855

   
9707
	snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
9856
	snprintf(p->lastmsg, sizeof(p->lastmsg), "Tx: %s", sip_methods[sipmethod].text);
9708
	
9857

   
9709
	if (!seqno) {
9858
	if (!seqno) {
9710
		p->ocseq++;
9859
		p->ocseq++;
9711
		seqno = p->ocseq;
9860
		seqno = p->ocseq;
9712
	}
9861
	}
9713
	
9862

   
9714
	/* A CANCEL must have the same branch as the INVITE that it is canceling. */
9863
	/* A CANCEL must have the same branch as the INVITE that it is canceling. */
9715
	if (sipmethod == SIP_CANCEL) {
9864
	if (sipmethod == SIP_CANCEL) {
9716
		p->branch = p->invite_branch;
9865
		p->branch = p->invite_branch;
9717
		build_via(p);
9866
		build_via(p);
9718
	} else if (newbranch && (sipmethod == SIP_INVITE)) {
9867
	} else if (newbranch && (sipmethod == SIP_INVITE)) {
[+20] [20] 9 lines
[+20] static int respprep(struct sip_request *resp, struct sip_pvt *p, const char *msg, const struct sip_request *req)
9728
	if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop, ";lr") == NULL) {
9877
	if (p->route && !ast_strlen_zero(p->route->hop) && strstr(p->route->hop, ";lr") == NULL) {
9729
		is_strict = TRUE;
9878
		is_strict = TRUE;
9730
		if (sipdebug)
9879
		if (sipdebug)
9731
			ast_debug(1, "Strict routing enforced for session %s\n", p->callid);
9880
			ast_debug(1, "Strict routing enforced for session %s\n", p->callid);
9732
	}
9881
	}
9733
	
9882

   
9734
	if (sipmethod == SIP_CANCEL)
9883
	if (sipmethod == SIP_CANCEL)
9735
		c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);	/* Use original URI */
9884
		c = REQ_OFFSET_TO_STR(&p->initreq, rlPart2);	/* Use original URI */
9736
	else if (sipmethod == SIP_ACK) {
9885
	else if (sipmethod == SIP_ACK) {
9737
		/* Use URI from Contact: in 200 OK (if INVITE)
9886
		/* Use URI from Contact: in 200 OK (if INVITE)
9738
		(we only have the contacturi on INVITEs) */
9887
		(we only have the contacturi on INVITEs) */
[+20] [20] 505 lines
[+20] [+] static int add_rpid(struct sip_request *req, struct sip_pvt *p)
10244
		return 0;
10393
		return 0;
10245
	if (ast_strlen_zero(lid_name))
10394
	if (ast_strlen_zero(lid_name))
10246
		lid_name = lid_num;
10395
		lid_name = lid_num;
10247
	fromdomain = S_OR(p->fromdomain, ast_sockaddr_stringify_host(&p->ourip));
10396
	fromdomain = S_OR(p->fromdomain, ast_sockaddr_stringify_host(&p->ourip));
10248

    
   
10397

   
10249
	lid_num = ast_uri_encode(lid_num, tmp2, sizeof(tmp2), 1);
10398
	lid_num = ast_uri_encode(lid_num, tmp2, sizeof(tmp2), ast_uri_sip_user);
10250

    
   
10399

   
10251
	if (ast_test_flag(&p->flags[0], SIP_SENDRPID_PAI)) {
10400
	if (ast_test_flag(&p->flags[0], SIP_SENDRPID_PAI)) {
10252
		if ((lid_pres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
10401
		if ((lid_pres & AST_PRES_RESTRICTION) != AST_PRES_ALLOWED) {
10253
			ast_str_set(&tmp, -1, "%s", anonymous_string);
10402
			ast_str_set(&tmp, -1, "%s", anonymous_string);
10254
		} else {
10403
		} else {
[+20] [20] 63 lines
[+20] [+] static int add_vidupdate(struct sip_request *req)
10318
	add_content(req, xml_is_a_huge_waste_of_space);
10467
	add_content(req, xml_is_a_huge_waste_of_space);
10319
	return 0;
10468
	return 0;
10320
}
10469
}
10321

    
   
10470

   
10322
/*! \brief Add codec offer to SDP offer/answer body in INVITE or 200 OK */
10471
/*! \brief Add codec offer to SDP offer/answer body in INVITE or 200 OK */
10323
static void add_codec_to_sdp(const struct sip_pvt *p, format_t codec,
10472
static void add_codec_to_sdp(const struct sip_pvt *p,
10324
			     struct ast_str **m_buf, struct ast_str **a_buf,
10473
	struct ast_format *format,
10325
			     int debug, int *min_packet_size)
10474
	struct ast_str **m_buf,

    
   
10475
	struct ast_str **a_buf,

    
   
10476
	int debug,

    
   
10477
	int *min_packet_size)
10326
{
10478
{
10327
	int rtp_code;
10479
	int rtp_code;
10328
	struct ast_format_list fmt;
10480
	struct ast_format_list fmt;
10329

    
   
10481

   
10330

    
   
10482

   
10331
	if (debug)
10483
	if (debug)
10332
		ast_verbose("Adding codec 0x%" PRIx64 " (%s) to SDP\n", codec, ast_getformatname(codec));
10484
		ast_verbose("Adding codec %d (%s) to SDP\n", format->id, ast_getformatname(format));
10333
	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 1, codec)) == -1)
10485
	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 1, format, 0)) == -1)
10334
		return;
10486
		return;
10335

    
   
10487

   
10336
	if (p->rtp) {
10488
	if (p->rtp) {
10337
		struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
10489
		struct ast_codec_pref *pref = &ast_rtp_instance_get_codecs(p->rtp)->pref;
10338
		fmt = ast_codec_pref_getsize(pref, codec);
10490
		fmt = ast_codec_pref_getsize(pref, format);
10339
	} else /* I don't see how you couldn't have p->rtp, but good to check for and error out if not there like earlier code */
10491
	} else /* I don't see how you couldn't have p->rtp, but good to check for and error out if not there like earlier code */
10340
		return;
10492
		return;
10341
	ast_str_append(m_buf, 0, " %d", rtp_code);
10493
	ast_str_append(m_buf, 0, " %d", rtp_code);
10342
	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10494
	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n",
10343
		       ast_rtp_lookup_mime_subtype2(1, codec,
10495
		rtp_code,
10344
						   ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
10496
		ast_rtp_lookup_mime_subtype2(1, format, 0, ast_test_flag(&p->flags[0], SIP_G726_NONSTANDARD) ? AST_RTP_OPT_G726_NONSTANDARD : 0),
10345
		       ast_rtp_lookup_sample_rate2(1, codec));
10497
		ast_rtp_lookup_sample_rate2(1, format, 0));
10346

    
   
10498

   
10347
	switch (codec) {
10499
	switch ((int) format->id) {
10348
	case AST_FORMAT_G729A:
10500
	case AST_FORMAT_G729A:
10349
		/* Indicate that we don't support VAD (G.729 annex B) */
10501
		/* Indicate that we don't support VAD (G.729 annex B) */
10350
		ast_str_append(a_buf, 0, "a=fmtp:%d annexb=no\r\n", rtp_code);
10502
		ast_str_append(a_buf, 0, "a=fmtp:%d annexb=no\r\n", rtp_code);
10351
		break;
10503
		break;
10352
	case AST_FORMAT_G723_1:
10504
	case AST_FORMAT_G723_1:
[+20] [20] 14 lines
[+20] static void add_codec_to_sdp(const struct sip_pvt *p, format_t codec, [+] static void add_codec_to_sdp(const struct sip_pvt *p,
10367
		break;
10519
		break;
10368
	case AST_FORMAT_G719:
10520
	case AST_FORMAT_G719:
10369
		/* Indicate that we only expect 64Kbps */
10521
		/* Indicate that we only expect 64Kbps */
10370
		ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=64000\r\n", rtp_code);
10522
		ast_str_append(a_buf, 0, "a=fmtp:%d bitrate=64000\r\n", rtp_code);
10371
		break;
10523
		break;

    
   
10524
	case AST_FORMAT_SILK:

    
   
10525
		{

    
   
10526
			int val = 0;

    
   
10527
			if (!ast_format_get_value(format, SILK_ATTR_KEY_MAX_BITRATE, &val) && val > 5000 && val < 40000) {

    
   
10528
				ast_str_append(a_buf, 0, "a=fmtp:%d maxaveragebitrate=%u\r\n", rtp_code, val);

    
   
10529
			}

    
   
10530
			if (!ast_format_get_value(format, SILK_ATTR_KEY_DTX, &val)) {

    
   
10531
				ast_str_append(a_buf, 0, "a=fmtp:%d usedtx=%u\r\n", rtp_code, val ? 1 : 0);

    
   
10532
			}

    
   
10533
			if (!ast_format_get_value(format, SILK_ATTR_KEY_FEC, &val)) {

    
   
10534
				ast_str_append(a_buf, 0, "a=fmtp:%d useinbandfec=%u\r\n", rtp_code, val ? 1 : 0);

    
   
10535
			}

    
   
10536
			break;

    
   
10537
		}
10372
	}
10538
	}
10373

    
   
10539

   
10374
	if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
10540
	if (fmt.cur_ms && (fmt.cur_ms < *min_packet_size))
10375
		*min_packet_size = fmt.cur_ms;
10541
		*min_packet_size = fmt.cur_ms;
10376

    
   
10542

   
10377
	/* Our first codec packetization processed cannot be zero */
10543
	/* Our first codec packetization processed cannot be zero */
10378
	if ((*min_packet_size)==0 && fmt.cur_ms)
10544
	if ((*min_packet_size)==0 && fmt.cur_ms)
10379
		*min_packet_size = fmt.cur_ms;
10545
		*min_packet_size = fmt.cur_ms;
10380
}
10546
}
10381

    
   
10547

   
10382
/*! \brief Add video codec offer to SDP offer/answer body in INVITE or 200 OK */
10548
/*! \brief Add video codec offer to SDP offer/answer body in INVITE or 200 OK */
10383
/* This is different to the audio one now so we can add more caps later */
10549
/* This is different to the audio one now so we can add more caps later */
10384
static void add_vcodec_to_sdp(const struct sip_pvt *p, format_t codec,
10550
static void add_vcodec_to_sdp(const struct sip_pvt *p, struct ast_format *format,
10385
			     struct ast_str **m_buf, struct ast_str **a_buf,
10551
			     struct ast_str **m_buf, struct ast_str **a_buf,
10386
			     int debug, int *min_packet_size)
10552
			     int debug, int *min_packet_size)
10387
{
10553
{
10388
	int rtp_code;
10554
	int rtp_code;
10389

    
   
10555

   
10390
	if (!p->vrtp)
10556
	if (!p->vrtp)
10391
		return;
10557
		return;
10392

    
   
10558

   
10393
	if (debug)
10559
	if (debug)
10394
		ast_verbose("Adding video codec 0x%" PRIx64 " (%s) to SDP\n", codec, ast_getformatname(codec));
10560
		ast_verbose("Adding video codec %d (%s) to SDP\n", format->id, ast_getformatname(format));
10395

    
   
10561

   
10396
	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->vrtp), 1, codec)) == -1)
10562
	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->vrtp), 1, format, 0)) == -1)
10397
		return;
10563
		return;
10398

    
   
10564

   
10399
	ast_str_append(m_buf, 0, " %d", rtp_code);
10565
	ast_str_append(m_buf, 0, " %d", rtp_code);
10400
	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10566
	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10401
		       ast_rtp_lookup_mime_subtype2(1, codec, 0),
10567
		       ast_rtp_lookup_mime_subtype2(1, format, 0, 0),
10402
		       ast_rtp_lookup_sample_rate2(1, codec));
10568
		       ast_rtp_lookup_sample_rate2(1, format, 0));
10403
	/* Add fmtp code here */
10569
	/* Add fmtp code here */
10404
}
10570
}
10405

    
   
10571

   
10406
/*! \brief Add text codec offer to SDP offer/answer body in INVITE or 200 OK */
10572
/*! \brief Add text codec offer to SDP offer/answer body in INVITE or 200 OK */
10407
static void add_tcodec_to_sdp(const struct sip_pvt *p, int codec,
10573
static void add_tcodec_to_sdp(const struct sip_pvt *p, struct ast_format *format,
10408
			     struct ast_str **m_buf, struct ast_str **a_buf,
10574
			     struct ast_str **m_buf, struct ast_str **a_buf,
10409
			     int debug, int *min_packet_size)
10575
			     int debug, int *min_packet_size)
10410
{
10576
{
10411
	int rtp_code;
10577
	int rtp_code;
10412

    
   
10578

   
10413
	if (!p->trtp)
10579
	if (!p->trtp)
10414
		return;
10580
		return;
10415

    
   
10581

   
10416
	if (debug)
10582
	if (debug)
10417
		ast_verbose("Adding text codec 0x%x (%s) to SDP\n", codec, ast_getformatname(codec));
10583
		ast_verbose("Adding text codec %d (%s) to SDP\n", format->id, ast_getformatname(format));
10418

    
   
10584

   
10419
	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, codec)) == -1)
10585
	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, format, 0)) == -1)
10420
		return;
10586
		return;
10421

    
   
10587

   
10422
	ast_str_append(m_buf, 0, " %d", rtp_code);
10588
	ast_str_append(m_buf, 0, " %d", rtp_code);
10423
	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10589
	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10424
		       ast_rtp_lookup_mime_subtype2(1, codec, 0),
10590
		       ast_rtp_lookup_mime_subtype2(1, format, 0, 0),
10425
		       ast_rtp_lookup_sample_rate2(1, codec));
10591
		       ast_rtp_lookup_sample_rate2(1, format, 0));
10426
	/* Add fmtp code here */
10592
	/* Add fmtp code here */
10427

    
   
10593

   
10428
	if (codec == AST_FORMAT_T140RED) {
10594
	if (format->id == AST_FORMAT_T140RED) {
10429
		int t140code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, AST_FORMAT_T140);
10595
		struct ast_format tmp_fmt;

    
   
10596
		int t140code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->trtp), 1, ast_format_set(&tmp_fmt, AST_FORMAT_T140, 0), 0);
10430
		ast_str_append(a_buf, 0, "a=fmtp:%d %d/%d/%d\r\n", rtp_code,
10597
		ast_str_append(a_buf, 0, "a=fmtp:%d %d/%d/%d\r\n", rtp_code,
10431
			 t140code,
10598
			 t140code,
10432
			 t140code,
10599
			 t140code,
10433
			 t140code);
10600
			 t140code);
10434

    
   
10601

   
[+20] [20] 28 lines
[+20] [+] static void add_noncodec_to_sdp(const struct sip_pvt *p, int format,
10463
				int debug)
10630
				int debug)
10464
{
10631
{
10465
	int rtp_code;
10632
	int rtp_code;
10466

    
   
10633

   
10467
	if (debug)
10634
	if (debug)
10468
		ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype2(0, format, 0));
10635
		ast_verbose("Adding non-codec 0x%x (%s) to SDP\n", format, ast_rtp_lookup_mime_subtype2(0, NULL, format, 0));
10469
	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 0, format)) == -1)
10636
	if ((rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(p->rtp), 0, NULL, format)) == -1)
10470
		return;
10637
		return;
10471

    
   
10638

   
10472
	ast_str_append(m_buf, 0, " %d", rtp_code);
10639
	ast_str_append(m_buf, 0, " %d", rtp_code);
10473
	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10640
	ast_str_append(a_buf, 0, "a=rtpmap:%d %s/%d\r\n", rtp_code,
10474
		       ast_rtp_lookup_mime_subtype2(0, format, 0),
10641
		       ast_rtp_lookup_mime_subtype2(0, NULL, format, 0),
10475
		       ast_rtp_lookup_sample_rate2(0, format));
10642
		       ast_rtp_lookup_sample_rate2(0, NULL, format));
10476
	if (format == AST_RTP_DTMF)	/* Indicate we support DTMF and FLASH... */
10643
	if (format == AST_RTP_DTMF)	/* Indicate we support DTMF and FLASH... */
10477
		ast_str_append(a_buf, 0, "a=fmtp:%d 0-16\r\n", rtp_code);
10644
		ast_str_append(a_buf, 0, "a=fmtp:%d 0-16\r\n", rtp_code);
10478
}
10645
}
10479

    
   
10646

   
10480
/*! \brief Set all IP media addresses for this call
10647
/*! \brief Set all IP media addresses for this call
[+20] [20] 115 lines
[+20] [+] static void get_crypto_attrib(struct sip_srtp *srtp, const char **a_crypto)
10596
    is used in Session-Timers where RE-INVITEs are used for refreshing SIP sessions
10763
    is used in Session-Timers where RE-INVITEs are used for refreshing SIP sessions
10597
    without modifying the media session in any way.
10764
    without modifying the media session in any way.
10598
*/
10765
*/
10599
static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10766
static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10600
{
10767
{
10601
	format_t alreadysent = 0;
10768
	struct ast_format_cap *alreadysent = ast_format_cap_alloc_nolock();
10602

    
   
10769
	struct ast_format_cap *tmpcap = ast_format_cap_alloc_nolock();

    
   
10770
	int res = AST_SUCCESS;

    
   
10771
	int doing_directmedia = FALSE;
10603
	struct ast_sockaddr addr = { {0,} };
10772
	struct ast_sockaddr addr = { {0,} };
10604
	struct ast_sockaddr vaddr = { {0,} };
10773
	struct ast_sockaddr vaddr = { {0,} };
10605
	struct ast_sockaddr taddr = { {0,} };
10774
	struct ast_sockaddr taddr = { {0,} };
10606
	struct ast_sockaddr udptladdr = { {0,} };
10775
	struct ast_sockaddr udptladdr = { {0,} };
10607
	struct ast_sockaddr dest = { {0,} };
10776
	struct ast_sockaddr dest = { {0,} };
[+20] [20] 19 lines
[+20] static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10627
	struct ast_str *a_modem = ast_str_alloca(1024); /* Attributes for modem */
10796
	struct ast_str *a_modem = ast_str_alloca(1024); /* Attributes for modem */
10628
	const char *a_crypto = NULL;
10797
	const char *a_crypto = NULL;
10629
	const char *v_a_crypto = NULL;
10798
	const char *v_a_crypto = NULL;
10630
	const char *t_a_crypto = NULL;
10799
	const char *t_a_crypto = NULL;
10631

    
   
10800

   
10632
	format_t x;
10801
	int x;
10633
	format_t capability = 0;
10802
	struct ast_format tmp_fmt;
10634
	int needaudio = FALSE;
10803
	int needaudio = FALSE;
10635
	int needvideo = FALSE;
10804
	int needvideo = FALSE;
10636
	int needtext = FALSE;
10805
	int needtext = FALSE;
10637
	int debug = sip_debug_test_pvt(p);
10806
	int debug = sip_debug_test_pvt(p);
10638
	int min_audio_packet_size = 0;
10807
	int min_audio_packet_size = 0;
[+20] [20] 5 lines
[+20] static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10644
	char dummy_answer[256];
10813
	char dummy_answer[256];
10645

    
   
10814

   
10646
	/* Set the SDP session name */
10815
	/* Set the SDP session name */
10647
	snprintf(subject, sizeof(subject), "s=%s\r\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
10816
	snprintf(subject, sizeof(subject), "s=%s\r\n", ast_strlen_zero(global_sdpsession) ? "-" : global_sdpsession);
10648

    
   
10817

   

    
   
10818
	if (!alreadysent || !tmpcap) {

    
   
10819
		res = AST_FAILURE;

    
   
10820
		goto add_sdp_cleanup;

    
   
10821
	}
10649
	if (!p->rtp) {
10822
	if (!p->rtp) {
10650
		ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
10823
		ast_log(LOG_WARNING, "No way to add SDP without an RTP structure\n");
10651
		return AST_FAILURE;
10824
		res = AST_FAILURE;

    
   
10825
		goto add_sdp_cleanup;

    
   
10826

   
10652
	}
10827
	}
10653
	/* XXX We should not change properties in the SIP dialog until
10828
	/* XXX We should not change properties in the SIP dialog until
10654
		we have acceptance of the offer if this is a re-invite */
10829
		we have acceptance of the offer if this is a re-invite */
10655

    
   
10830

   
10656
	/* Set RTP Session ID and version */
10831
	/* Set RTP Session ID and version */
[+20] [20] 4 lines
[+20] static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10661
		if (oldsdp == FALSE)
10836
		if (oldsdp == FALSE)
10662
			p->sessionversion++;
10837
			p->sessionversion++;
10663
	}
10838
	}
10664

    
   
10839

   
10665
	if (add_audio) {
10840
	if (add_audio) {

    
   
10841
		doing_directmedia = (!ast_sockaddr_isnull(&p->redirip) && !(ast_format_cap_is_empty(p->redircaps))) ? TRUE : FALSE;
10666
		/* Check if we need video in this call */
10842
		/* Check if we need video in this call */
10667
		if ((p->jointcapability & AST_FORMAT_VIDEO_MASK) && !p->novideo) {
10843
		if ((ast_format_cap_has_type(p->jointcaps, AST_FORMAT_TYPE_VIDEO)) && !p->novideo) {
10668
			if (p->vrtp) {
10844
			if (p->vrtp) {
10669
				needvideo = TRUE;
10845
				needvideo = TRUE;
10670
				ast_debug(2, "This call needs video offers!\n");
10846
				ast_debug(2, "This call needs video offers!\n");
10671
			} else
10847
			} else
10672
				ast_debug(2, "This call needs video offers, but there's no video support enabled!\n");
10848
				ast_debug(2, "This call needs video offers, but there's no video support enabled!\n");
10673
		}
10849
		}
10674
		/* Check if we need text in this call */
10850
		/* Check if we need text in this call */
10675
		if ((p->jointcapability & AST_FORMAT_TEXT_MASK) && !p->notext) {
10851
		if ((ast_format_cap_has_type(p->jointcaps, AST_FORMAT_TYPE_TEXT)) && !p->notext) {
10676
			if (sipdebug_text)
10852
			if (sipdebug_text)
10677
				ast_verbose("We think we can do text\n");
10853
				ast_verbose("We think we can do text\n");
10678
			if (p->trtp) {
10854
			if (p->trtp) {
10679
				if (sipdebug_text) {
10855
				if (sipdebug_text) {
10680
					ast_verbose("And we have a text rtp object\n");
10856
					ast_verbose("And we have a text rtp object\n");
[+20] [20] 19 lines
[+20] static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10700
		 (ast_sockaddr_is_ipv6(&dest) && !ast_sockaddr_is_ipv4_mapped(&dest)) ?
10876
		 (ast_sockaddr_is_ipv6(&dest) && !ast_sockaddr_is_ipv4_mapped(&dest)) ?
10701
			"IP6" : "IP4",
10877
			"IP6" : "IP4",
10702
		 ast_sockaddr_stringify_addr(&dest));
10878
		 ast_sockaddr_stringify_addr(&dest));
10703

    
   
10879

   
10704
	if (add_audio) {
10880
	if (add_audio) {
10705
		capability = p->jointcapability;
10881
		if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR) {

    
   
10882
			hold = "a=recvonly\r\n";

    
   
10883
			doing_directmedia = FALSE;

    
   
10884
		} else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE) {

    
   
10885
			hold = "a=inactive\r\n";

    
   
10886
			doing_directmedia = FALSE;

    
   
10887
		} else {

    
   
10888
			hold = "a=sendrecv\r\n";

    
   
10889
		}

    
   
10890

   

    
   
10891
		ast_format_cap_copy(tmpcap, p->jointcaps);
10706

    
   
10892

   
10707
		/* XXX note, Video and Text are negated - 'true' means 'no' */
10893
		/* XXX note, Video and Text are negated - 'true' means 'no' */
10708
		ast_debug(1, "** Our capability: %s Video flag: %s Text flag: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), capability),
10894
		ast_debug(1, "** Our capability: %s Video flag: %s Text flag: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), tmpcap),
10709
			  p->novideo ? "True" : "False", p->notext ? "True" : "False");
10895
			  p->novideo ? "True" : "False", p->notext ? "True" : "False");
10710
		ast_debug(1, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcodec));
10896
		ast_debug(1, "** Our prefcodec: %s \n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), p->prefcaps));

    
   
10897

   

    
   
10898
		if (doing_directmedia) {

    
   
10899
			ast_format_cap_joint_copy(p->jointcaps, p->redircaps, tmpcap);

    
   
10900
			ast_debug(1, "** Our native-bridge filtered capablity: %s\n", ast_getformatname_multiple(codecbuf, sizeof(codecbuf), tmpcap));

    
   
10901
		}
10711

    
   
10902

   
10712
		/* Check if we need audio */
10903
		/* Check if we need audio */
10713
		if (capability & AST_FORMAT_AUDIO_MASK)
10904
		if (ast_format_cap_has_type(tmpcap, AST_FORMAT_TYPE_AUDIO))
10714
			needaudio = TRUE;
10905
			needaudio = TRUE;
10715

    
   
10906

   
10716
		if (debug) {
10907
		if (debug) {
10717
			ast_verbose("Audio is at %s\n", ast_sockaddr_stringify_port(&p->ourip));
10908
			ast_verbose("Audio is at %s\n", ast_sockaddr_stringify_port(&p->ourip));
10718
		}
10909
		}
[+20] [20] 33 lines
[+20] static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10752

    
   
10943

   
10753
		get_crypto_attrib(p->srtp, &a_crypto);
10944
		get_crypto_attrib(p->srtp, &a_crypto);
10754
		ast_str_append(&m_audio, 0, "m=audio %d RTP/%s", ast_sockaddr_port(&dest),
10945
		ast_str_append(&m_audio, 0, "m=audio %d RTP/%s", ast_sockaddr_port(&dest),
10755
			a_crypto ? "SAVP" : "AVP");
10946
			a_crypto ? "SAVP" : "AVP");
10756

    
   
10947

   
10757
		if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_ONEDIR)

   
10758
			hold = "a=recvonly\r\n";

   
10759
		else if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD) == SIP_PAGE2_CALL_ONHOLD_INACTIVE)

   
10760
			hold = "a=inactive\r\n";

   
10761
		else

   
10762
			hold = "a=sendrecv\r\n";

   
10763

    
   

   
10764
		/* Now, start adding audio codecs. These are added in this order:
10948
		/* Now, start adding audio codecs. These are added in this order:
10765
		   - First what was requested by the calling channel
10949
		   - First what was requested by the calling channel
10766
		   - Then preferences in order from sip.conf device config for this peer/user
10950
		   - Then preferences in order from sip.conf device config for this peer/user
10767
		   - Then other codecs in capabilities, including video
10951
		   - Then other codecs in capabilities, including video
10768
		*/
10952
		*/
10769

    
   
10953

   
10770
		/* Prefer the audio codec we were requested to use, first, no matter what
10954
		/* Prefer the audio codec we were requested to use, first, no matter what
10771
		   Note that p->prefcodec can include video codecs, so mask them out
10955
		   Note that p->prefcodec can include video codecs, so mask them out
10772
		*/
10956
		*/
10773
		if (capability & p->prefcodec) {
10957
		if (ast_format_cap_has_joint(tmpcap, p->prefcaps)) {
10774
			format_t codec = p->prefcodec & AST_FORMAT_AUDIO_MASK;
10958
			ast_format_cap_iter_start(p->prefcaps);
10775

    
   
10959
			while (!(ast_format_cap_iter_next(p->prefcaps, &tmp_fmt))) {
10776
			add_codec_to_sdp(p, codec, &m_audio, &a_audio, debug, &min_audio_packet_size);
10960
				if (AST_FORMAT_GET_TYPE(tmp_fmt.id) != AST_FORMAT_TYPE_AUDIO) {
10777
			alreadysent |= codec;
10961
					continue;

    
   
10962
				}

    
   
10963
				add_codec_to_sdp(p, &tmp_fmt, &m_audio, &a_audio, debug, &min_audio_packet_size);

    
   
10964
				ast_format_cap_add(alreadysent, &tmp_fmt);

    
   
10965
			}

    
   
10966
			ast_format_cap_iter_end(p->prefcaps);
10778
		}
10967
		}
10779

    
   
10968

   
10780
		/* Start by sending our preferred audio/video codecs */
10969
		/* Start by sending our preferred audio/video codecs */
10781
		for (x = 0; x < 64; x++) {
10970
		for (x = 0; x < AST_CODEC_PREF_SIZE; x++) {
10782
			format_t codec;
10971
			if (!(ast_codec_pref_index(&p->prefs, x, &tmp_fmt)))
10783

    
   

   
10784
			if (!(codec = ast_codec_pref_index(&p->prefs, x)))

   
10785
				break;
10972
				break;
10786

    
   
10973

   
10787
			if (!(capability & codec))
10974
			if (!(ast_format_cap_iscompatible(tmpcap, &tmp_fmt)))
10788
				continue;
10975
				continue;
10789

    
   
10976

   
10790
			if (alreadysent & codec)
10977
			if (ast_format_cap_iscompatible(alreadysent, &tmp_fmt))
10791
				continue;
10978
				continue;
10792

    
   
10979

   
10793
			add_codec_to_sdp(p, codec, &m_audio, &a_audio, debug, &min_audio_packet_size);
10980
			if (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_AUDIO) {
10794
			alreadysent |= codec;
10981
				add_codec_to_sdp(p, &tmp_fmt, &m_audio, &a_audio, debug, &min_audio_packet_size);

    
   
10982
			} else if (needvideo && (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_VIDEO)) {

    
   
10983
				add_vcodec_to_sdp(p, &tmp_fmt, &m_video, &a_video, debug, &min_video_packet_size);

    
   
10984
			} else if (needtext && (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_TEXT)) {

    
   
10985
				add_tcodec_to_sdp(p, &tmp_fmt, &m_text, &a_text, debug, &min_text_packet_size);
10795
		}
10986
			}
10796

    
   
10987

   
10797
		/* Now send any other common audio and video codecs, and non-codec formats: */
10988
			ast_format_cap_add(alreadysent, &tmp_fmt);
10798
		for (x = 1ULL; x <= (needtext ? AST_FORMAT_TEXT_MASK : (needvideo ? AST_FORMAT_VIDEO_MASK : AST_FORMAT_AUDIO_MASK)); x <<= 1) {
10989
		}
10799
			if (!(capability & x))	/* Codec not requested */

   
10800
				continue;

   
10801

    
   
10990

   
10802
			if (alreadysent & x)	/* Already added to SDP */
10991
		/* Now send any other common audio and video codecs, and non-codec formats: */

    
   
10992
		ast_format_cap_iter_start(tmpcap);

    
   
10993
		while (!(ast_format_cap_iter_next(tmpcap, &tmp_fmt))) {

    
   
10994
			if (ast_format_cap_iscompatible(alreadysent, &tmp_fmt))
10803
				continue;
10995
				continue;
10804

    
   
10996

   
10805
			if (x & AST_FORMAT_AUDIO_MASK)
10997
			if (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_AUDIO) {
10806
				add_codec_to_sdp(p, x, &m_audio, &a_audio, debug, &min_audio_packet_size);
10998
				add_codec_to_sdp(p, &tmp_fmt, &m_audio, &a_audio, debug, &min_audio_packet_size);
10807
			else if (x & AST_FORMAT_VIDEO_MASK)
10999
			} else if (needvideo && (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_VIDEO)) {
10808
				add_vcodec_to_sdp(p, x, &m_video, &a_video, debug, &min_video_packet_size);
11000
				add_vcodec_to_sdp(p, &tmp_fmt, &m_video, &a_video, debug, &min_video_packet_size);
10809
			else if (x & AST_FORMAT_TEXT_MASK)
11001
			} else if (needtext && (AST_FORMAT_GET_TYPE(tmp_fmt.id) == AST_FORMAT_TYPE_TEXT)) {
10810
				add_tcodec_to_sdp(p, x, &m_text, &a_text, debug, &min_text_packet_size);
11002
				add_tcodec_to_sdp(p, &tmp_fmt, &m_text, &a_text, debug, &min_text_packet_size);

    
   
11003
			}
10811
		}
11004
		}

    
   
11005
		ast_format_cap_iter_end(tmpcap);
10812

    
   
11006

   
10813
		/* Now add DTMF RFC2833 telephony-event as a codec */
11007
		/* Now add DTMF RFC2833 telephony-event as a codec */
10814
		for (x = 1LL; x <= AST_RTP_MAX; x <<= 1) {
11008
		for (x = 1LL; x <= AST_RTP_MAX; x <<= 1) {
10815
			if (!(p->jointnoncodeccapability & x))
11009
			if (!(p->jointnoncodeccapability & x))
10816
				continue;
11010
				continue;
[+20] [20] 142 lines
[+20] static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int oldsdp, int add_audio, int add_t38)
10959

    
   
11153

   
10960
	/* we unlink this dialog and link again into the dialogs_rtpcheck container to doesnt add it twice */
11154
	/* we unlink this dialog and link again into the dialogs_rtpcheck container to doesnt add it twice */
10961
	ao2_t_unlink(dialogs_rtpcheck, p, "unlink pvt into dialogs_rtpcheck container");
11155
	ao2_t_unlink(dialogs_rtpcheck, p, "unlink pvt into dialogs_rtpcheck container");
10962
	ao2_t_link(dialogs_rtpcheck, p, "link pvt into dialogs_rtpcheck container");
11156
	ao2_t_link(dialogs_rtpcheck, p, "link pvt into dialogs_rtpcheck container");
10963

    
   
11157

   
10964
	ast_debug(3, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, capability));
11158
	ast_debug(3, "Done building SDP. Settling with this capability: %s\n", ast_getformatname_multiple(buf, SIPBUFSIZE, tmpcap));
10965

    
   
11159

   
10966
	return AST_SUCCESS;
11160
add_sdp_cleanup:

    
   
11161
	alreadysent = ast_format_cap_destroy(alreadysent);

    
   
11162
	tmpcap = ast_format_cap_destroy(tmpcap);

    
   
11163

   

    
   
11164
	return res;
10967
}
11165
}
10968

    
   
11166

   
10969
/*! \brief Used for 200 OK and 183 early media */
11167
/*! \brief Used for 200 OK and 183 early media */
10970
static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
11168
static int transmit_response_with_t38_sdp(struct sip_pvt *p, char *msg, struct sip_request *req, int retrans)
10971
{
11169
{
10972
	struct sip_request resp;
11170
	struct sip_request resp;
10973
	int seqno;
11171
	int seqno;
10974
	
11172

   
10975
	if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
11173
	if (sscanf(get_header(req, "CSeq"), "%30d ", &seqno) != 1) {
10976
		ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
11174
		ast_log(LOG_WARNING, "Unable to get seqno from '%s'\n", get_header(req, "CSeq"));
10977
		return -1;
11175
		return -1;
10978
	}
11176
	}
10979
	respprep(&resp, p, msg, req);
11177
	respprep(&resp, p, msg, req);
[+20] [20] 228 lines
[+20] [+] static void extract_uri(struct sip_pvt *p, struct sip_request *req)
11208
}
11406
}
11209

    
   
11407

   
11210
/*! \brief Build contact header - the contact header we send out */
11408
/*! \brief Build contact header - the contact header we send out */
11211
static void build_contact(struct sip_pvt *p)
11409
static void build_contact(struct sip_pvt *p)
11212
{
11410
{

    
   
11411
	char tmp[SIPBUFSIZE];

    
   
11412
	char *user = ast_uri_encode(p->exten, tmp, sizeof(tmp), ast_uri_sip_user);

    
   
11413

   
11213
	if (p->socket.type == SIP_TRANSPORT_UDP) {
11414
	if (p->socket.type == SIP_TRANSPORT_UDP) {
11214
		ast_string_field_build(p, our_contact, "<sip:%s%s%s>", p->exten,
11415
		ast_string_field_build(p, our_contact, "<sip:%s%s%s>", user,
11215
			ast_strlen_zero(p->exten) ? "" : "@", ast_sockaddr_stringify(&p->ourip));
11416
			ast_strlen_zero(user) ? "" : "@", ast_sockaddr_stringify(&p->ourip));
11216
	} else {
11417
	} else {
11217
		ast_string_field_build(p, our_contact, "<sip:%s%s%s;transport=%s>", p->exten,
11418
		ast_string_field_build(p, our_contact, "<sip:%s%s%s;transport=%s>", user,
11218
			ast_strlen_zero(p->exten) ? "" : "@", ast_sockaddr_stringify(&p->ourip),
11419
			ast_strlen_zero(user) ? "" : "@", ast_sockaddr_stringify(&p->ourip),
11219
			get_transport(p->socket.type));
11420
			get_transport(p->socket.type));
11220
	}
11421
	}
11221
}
11422
}
11222

    
   
11423

   
11223
/*! \brief Initiate new SIP request to peer/user */
11424
/*! \brief Initiate new SIP request to peer/user */
[+20] [20] 70 lines
[+20] [+] static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, const char * const explicit_uri)
11294
		n = p->fromname;
11495
		n = p->fromname;
11295
	else /* Save for any further attempts */
11496
	else /* Save for any further attempts */
11296
		ast_string_field_set(p, fromname, n);
11497
		ast_string_field_set(p, fromname, n);
11297

    
   
11498

   
11298
	if (sip_cfg.pedanticsipchecking) {
11499
	if (sip_cfg.pedanticsipchecking) {
11299
		ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
11500
		ast_escape_quoted(n, tmp_n, sizeof(tmp_n));
11300
		n = tmp_n;
11501
		n = tmp_n;
11301
		ast_uri_encode(l, tmp_l, sizeof(tmp_l), 0);
11502
		ast_uri_encode(l, tmp_l, sizeof(tmp_l), ast_uri_sip_user);
11302
		l = tmp_l;
11503
		l = tmp_l;
11303
	}
11504
	}
11304

    
   
11505

   
11305
	ourport = (p->fromdomainport) ? p->fromdomainport : ast_sockaddr_port(&p->ourip);
11506
	ourport = (p->fromdomainport) ? p->fromdomainport : ast_sockaddr_port(&p->ourip);
11306
	if (!sip_standard_port(p->socket.type, ourport)) {
11507
	if (!sip_standard_port(p->socket.type, ourport)) {
[+20] [20] 13 lines
[+20] static void initreqprep(struct sip_request *req, struct sip_pvt *p, int sipmethod, const char * const explicit_uri)
11320
			/* Otherwise, use the username while waiting for registration */
11521
			/* Otherwise, use the username while waiting for registration */
11321
			ast_str_append(&invite, 0, "sip:");
11522
			ast_str_append(&invite, 0, "sip:");
11322
			if (!ast_strlen_zero(p->username)) {
11523
			if (!ast_strlen_zero(p->username)) {
11323
				n = p->username;
11524
				n = p->username;
11324
				if (sip_cfg.pedanticsipchecking) {
11525
				if (sip_cfg.pedanticsipchecking) {
11325
					ast_uri_encode(n, tmp_n, sizeof(tmp_n), 0);
11526
					ast_uri_encode(n, tmp_n, sizeof(tmp_n), ast_uri_sip_user);
11326
					n = tmp_n;
11527
					n = tmp_n;
11327
				}
11528
				}
11328
				ast_str_append(&invite, 0, "%s@", n);
11529
				ast_str_append(&invite, 0, "%s@", n);
11329
			}
11530
			}
11330
			ast_str_append(&invite, 0, "%s", p->tohost);
11531
			ast_str_append(&invite, 0, "%s", p->tohost);
[+20] [20] 240 lines
[+20] [+] static int transmit_invite(struct sip_pvt *p, int sipmethod, int sdp, int init, const char * const explicit_uri)
11571
					}
11772
					}
11572
					if ((content = strchr(headdup, ':'))) {
11773
					if ((content = strchr(headdup, ':'))) {
11573
						*content++ = '\0';
11774
						*content++ = '\0';
11574
						content = ast_skip_blanks(content); /* Skip white space */
11775
						content = ast_skip_blanks(content); /* Skip white space */
11575
						/* Strip the ending " (if it's there) */
11776
						/* Strip the ending " (if it's there) */
11576
				 		end = content + strlen(content) -1;	
11777
						end = content + strlen(content) -1;
11577
						if (*end == '"') {
11778
						if (*end == '"') {
11578
							*end = '\0';
11779
							*end = '\0';
11579
						}
11780
						}
11580
					
11781

   
11581
						add_header(&req, headdup, content);
11782
						add_header(&req, headdup, content);
11582
						if (sipdebug) {
11783
						if (sipdebug) {
11583
							ast_debug(1, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
11784
							ast_debug(1, "Adding SIP Header \"%s\" with content :%s: \n", headdup, content);
11584
						}
11785
						}
11585
					}
11786
					}
[+20] [20] 981 lines
[+20] [+] static int transmit_register(struct sip_registry *r, int sipmethod, const char *auth, const char *authheader)
12567
	add_header(&req, "Call-ID", p->callid);
12768
	add_header(&req, "Call-ID", p->callid);
12568
	add_header(&req, "CSeq", tmp);
12769
	add_header(&req, "CSeq", tmp);
12569
	if (!ast_strlen_zero(global_useragent))
12770
	if (!ast_strlen_zero(global_useragent))
12570
		add_header(&req, "User-Agent", global_useragent);
12771
		add_header(&req, "User-Agent", global_useragent);
12571

    
   
12772

   
12572
	if (auth) { 	/* Add auth header */
12773
	if (auth) {	/* Add auth header */
12573
		add_header(&req, authheader, auth);
12774
		add_header(&req, authheader, auth);
12574
	} else if (!ast_strlen_zero(r->nonce)) {
12775
	} else if (!ast_strlen_zero(r->nonce)) {
12575
		char digest[1024];
12776
		char digest[1024];
12576

    
   
12777

   
12577
		/* We have auth data to reuse, build a digest header.
12778
		/* We have auth data to reuse, build a digest header.
[+20] [20] 78 lines
[+20] [+] static int sip_notify_allocate(struct sip_pvt *p)
12656
	\todo Fix the transfer() dialplan function so that a transfer may fail
12857
	\todo Fix the transfer() dialplan function so that a transfer may fail
12657
*/
12858
*/
12658
static int transmit_refer(struct sip_pvt *p, const char *dest)
12859
static int transmit_refer(struct sip_pvt *p, const char *dest)
12659
{
12860
{
12660
	struct sip_request req = {
12861
	struct sip_request req = {
12661
		.headers = 0,	
12862
		.headers = 0,
12662
	};
12863
	};
12663
	char from[256];
12864
	char from[256];
12664
	const char *of;
12865
	const char *of;
12665
	char *c;
12866
	char *c;
12666
	char referto[256];
12867
	char referto[256];
[+20] [20] 465 lines
[+20] [+] static enum parse_register_result parse_register_contact(struct sip_pvt *pvt, struct sip_peer *peer, struct sip_request *req)
13132
	const char *useragent;
13333
	const char *useragent;
13133
	struct ast_sockaddr oldsin, testsa;
13334
	struct ast_sockaddr oldsin, testsa;
13134
	char *firstcuri = NULL;
13335
	char *firstcuri = NULL;
13135
	int start = 0;
13336
	int start = 0;
13136
	int wildcard_found = 0;
13337
	int wildcard_found = 0;
13137
	int single_binding_found;
13338
	int single_binding_found = 0;
13138

    
   
13339

   
13139
	ast_copy_string(contact, __get_header(req, "Contact", &start), sizeof(contact));
13340
	ast_copy_string(contact, __get_header(req, "Contact", &start), sizeof(contact));
13140

    
   
13341

   
13141
	if (ast_strlen_zero(expires)) {	/* No expires header, try look in Contact: */
13342
	if (ast_strlen_zero(expires)) {	/* No expires header, try look in Contact: */
13142
		char *s = strcasestr(contact, ";expires=");
13343
		char *s = strcasestr(contact, ";expires=");
[+20] [20] 246 lines
[+20] [+] static void build_route(struct sip_pvt *p, struct sip_request *req, int backwards)
13389
		p->route = NULL;
13590
		p->route = NULL;
13390
	}
13591
	}
13391

    
   
13592

   
13392
	/* We only want to create the route set the first time this is called */
13593
	/* We only want to create the route set the first time this is called */
13393
	p->route_persistent = 1;
13594
	p->route_persistent = 1;
13394
	
13595

   
13395
	/* Build a tailq, then assign it to p->route when done.
13596
	/* Build a tailq, then assign it to p->route when done.
13396
	 * If backwards, we add entries from the head so they end up
13597
	 * If backwards, we add entries from the head so they end up
13397
	 * in reverse order. However, we do need to maintain a correct
13598
	 * in reverse order. However, we do need to maintain a correct
13398
	 * tail pointer because the contact is always at the end.
13599
	 * tail pointer because the contact is always at the end.
13399
	 */
13600
	 */
[+20] [20] 1154 lines
[+20] [+] static enum sip_get_dest_result get_destination(struct sip_pvt *p, struct sip_request *oreq, int *cc_recall_core_id)
14554
	}
14755
	}
14555

    
   
14756

   
14556
	/* Since extensions.conf can have unescaped characters, try matching a
14757
	/* Since extensions.conf can have unescaped characters, try matching a
14557
	 * decoded uri in addition to the non-decoded uri. */
14758
	 * decoded uri in addition to the non-decoded uri. */
14558
	decoded_uri = ast_strdupa(uri);
14759
	decoded_uri = ast_strdupa(uri);
14559
	ast_uri_decode(decoded_uri);
14760
	ast_uri_decode(decoded_uri, ast_uri_sip_user);
14560

    
   
14761

   
14561
	/* If this is a subscription we actually just need to see if a hint exists for the extension */
14762
	/* If this is a subscription we actually just need to see if a hint exists for the extension */
14562
	if (req->method == SIP_SUBSCRIBE) {
14763
	if (req->method == SIP_SUBSCRIBE) {
14563
		char hint[AST_MAX_EXTENSION];
14764
		char hint[AST_MAX_EXTENSION];
14564
		int which = 0;
14765
		int which = 0;
[+20] [20] 61 lines
[+20] [+] static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
14626
	if (totag) {
14827
	if (totag) {
14627
		ast_debug(4, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
14828
		ast_debug(4, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
14628
	}
14829
	}
14629

    
   
14830

   
14630
	/* Search dialogs and find the match */
14831
	/* Search dialogs and find the match */
14631
	
14832

   
14632
	sip_pvt_ptr = ao2_t_find(dialogs, &tmp_dialog, OBJ_POINTER, "ao2_find of dialog in dialogs table");
14833
	sip_pvt_ptr = ao2_t_find(dialogs, &tmp_dialog, OBJ_POINTER, "ao2_find of dialog in dialogs table");
14633
	if (sip_pvt_ptr) {
14834
	if (sip_pvt_ptr) {
14634
		/* Go ahead and lock it (and its owner) before returning */
14835
		/* Go ahead and lock it (and its owner) before returning */
14635
		sip_pvt_lock(sip_pvt_ptr);
14836
		sip_pvt_lock(sip_pvt_ptr);
14636
		if (sip_cfg.pedanticsipchecking) {
14837
		if (sip_cfg.pedanticsipchecking) {
[+20] [20] 42 lines
[+20] static struct sip_pvt *get_sip_pvt_byid_locked(const char *callid, const char *totag, const char *fromtag)
14679
						  totag, sip_pvt_ptr->tag);
14880
						  totag, sip_pvt_ptr->tag);
14680
				}
14881
				}
14681
				return NULL;
14882
				return NULL;
14682
			}
14883
			}
14683
		}
14884
		}
14684
		
14885

   
14685
		if (totag)
14886
		if (totag)
14686
			ast_debug(4, "Matched %s call - their tag is %s Our tag is %s\n",
14887
			ast_debug(4, "Matched %s call - their tag is %s Our tag is %s\n",
14687
					  sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING",
14888
					  sip_pvt_ptr->outgoing_call == TRUE ? "OUTGOING": "INCOMING",
14688
					  sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
14889
					  sip_pvt_ptr->theirtag, sip_pvt_ptr->tag);
14689

    
   
14890

   
[+20] [20] 92 lines
[+20] [+] static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
14782
		char *to = NULL, *from = NULL;
14983
		char *to = NULL, *from = NULL;
14783
		
14984
		
14784
		/* This is an attended transfer */
14985
		/* This is an attended transfer */
14785
		referdata->attendedtransfer = 1;
14986
		referdata->attendedtransfer = 1;
14786
		ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
14987
		ast_copy_string(referdata->replaces_callid, ptr+9, sizeof(referdata->replaces_callid));
14787
		ast_uri_decode(referdata->replaces_callid);
14988
		ast_uri_decode(referdata->replaces_callid, ast_uri_sip_user);
14788
		if ((ptr = strchr(referdata->replaces_callid, ';'))) 	/* Find options */ {
14989
		if ((ptr = strchr(referdata->replaces_callid, ';'))) 	/* Find options */ {
14789
			*ptr++ = '\0';
14990
			*ptr++ = '\0';
14790
		}
14991
		}
14791
		
14992
		
14792
		if (ptr) {
14993
		if (ptr) {
[+20] [20] 11 lines
[+20] static int get_refer_info(struct sip_pvt *transferer, struct sip_request *outgoing_req)
14804
			if ((to = strchr(ptr, ';'))) {
15005
			if ((to = strchr(ptr, ';'))) {
14805
				*to = '\0';
15006
				*to = '\0';
14806
			}
15007
			}
14807
			ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
15008
			ast_copy_string(referdata->replaces_callid_totag, ptr, sizeof(referdata->replaces_callid_totag));
14808
		}
15009
		}
14809
		
15010

   
14810
		if (from) {
15011
		if (from) {
14811
			ptr = from + 9;
15012
			ptr = from + 9;
14812
			if ((to = strchr(ptr, '&'))) {
15013
			if ((to = strchr(ptr, '&'))) {
14813
				*to = '\0';
15014
				*to = '\0';
14814
			}
15015
			}
14815
			if ((to = strchr(ptr, ';'))) {
15016
			if ((to = strchr(ptr, ';'))) {
14816
				*to = '\0';
15017
				*to = '\0';
14817
			}
15018
			}
14818
			ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
15019
			ast_copy_string(referdata->replaces_callid_fromtag, ptr, sizeof(referdata->replaces_callid_fromtag));
14819
		}
15020
		}
14820
		
15021

   

    
   
15022
		if (!strcmp(referdata->replaces_callid, transferer->callid) &&

    
   
15023
			(!sip_cfg.pedanticsipchecking ||

    
   
15024
			(!strcmp(referdata->replaces_callid_fromtag, transferer->theirtag) &&

    
   
15025
			!strcmp(referdata->replaces_callid_totag, transferer->tag)))) {

    
   
15026
				ast_log(LOG_WARNING, "Got an attempt to replace own Call-ID on %s\n", transferer->callid);

    
   
15027
				return -4;

    
   
15028
		}

    
   
15029

   
14821
		if (!sip_cfg.pedanticsipchecking) {
15030
		if (!sip_cfg.pedanticsipchecking) {
14822
			ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
15031
			ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s (No check of from/to tags)\n", referdata->replaces_callid );
14823
		} else {
15032
		} else {
14824
			ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s F-tag: %s T-tag: %s\n", referdata->replaces_callid, referdata->replaces_callid_fromtag ? referdata->replaces_callid_fromtag : "<none>", referdata->replaces_callid_totag ? referdata->replaces_callid_totag : "<none>" );
15033
			ast_debug(2, "Attended transfer: Will use Replace-Call-ID : %s F-tag: %s T-tag: %s\n", referdata->replaces_callid, referdata->replaces_callid_fromtag ? referdata->replaces_callid_fromtag : "<none>", referdata->replaces_callid_totag ? referdata->replaces_callid_totag : "<none>" );
14825
		}
15034
		}
14826
	}
15035
	}
14827
	
15036

   
14828
	if ((ptr = strchr(refer_to, '@'))) {	/* Separate domain */
15037
	if ((ptr = strchr(refer_to, '@'))) {	/* Separate domain */
14829
		char *urioption = NULL, *domain;
15038
		char *urioption = NULL, *domain;
14830
		int bracket = 0;
15039
		int bracket = 0;
14831
		*ptr++ = '\0';
15040
		*ptr++ = '\0';
14832

    
   
15041

   
[+20] [20] 370 lines
[+20] [+] static enum check_auth_result check_peer_ok(struct sip_pvt *p, char *of,
15203
		ast_string_field_set(p, language, peer->language);
15412
		ast_string_field_set(p, language, peer->language);
15204
		ast_string_field_set(p, accountcode, peer->accountcode);
15413
		ast_string_field_set(p, accountcode, peer->accountcode);
15205
		p->amaflags = peer->amaflags;
15414
		p->amaflags = peer->amaflags;
15206
		p->callgroup = peer->callgroup;
15415
		p->callgroup = peer->callgroup;
15207
		p->pickupgroup = peer->pickupgroup;
15416
		p->pickupgroup = peer->pickupgroup;
15208
		p->capability = peer->capability;
15417
		ast_format_cap_copy(p->caps, peer->caps);

    
   
15418
		ast_format_cap_copy(p->jointcaps, peer->caps);
15209
		p->prefs = peer->prefs;
15419
		p->prefs = peer->prefs;
15210
		p->jointcapability = peer->capability;

   
15211
 		if (peer->maxforwards > 0) {
15420
 		if (peer->maxforwards > 0) {
15212
			p->maxforwards = peer->maxforwards;
15421
			p->maxforwards = peer->maxforwards;
15213
		}
15422
		}
15214
		if (p->peercapability)
15423
		if (!(ast_format_cap_is_empty(p->peercaps))) {
15215
			p->jointcapability &= p->peercapability;
15424
			struct ast_format_cap *tmp = ast_format_cap_joint(p->jointcaps, p->peercaps);

    
   
15425
			struct ast_format_cap *tmp2;

    
   
15426
			if (tmp) {

    
   
15427
				tmp2 = p->jointcaps;

    
   
15428
				p->jointcaps = tmp;

    
   
15429
				ast_format_cap_destroy(tmp2);

    
   
15430
			}

    
   
15431
		}
15216
		p->maxcallbitrate = peer->maxcallbitrate;
15432
		p->maxcallbitrate = peer->maxcallbitrate;
15217
		if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
15433
		if ((ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833) ||
15218
		    (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
15434
		    (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO))
15219
			p->noncodeccapability |= AST_RTP_DTMF;
15435
			p->noncodeccapability |= AST_RTP_DTMF;
15220
		else
15436
		else
[+20] [20] 228 lines
[+20] [+] static void receive_message(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, const char *e)
15449
		memset(&f, 0, sizeof(f));
15665
		memset(&f, 0, sizeof(f));
15450
		f.frametype = AST_FRAME_TEXT;
15666
		f.frametype = AST_FRAME_TEXT;
15451
		f.subclass.integer = 0;
15667
		f.subclass.integer = 0;
15452
		f.offset = 0;
15668
		f.offset = 0;
15453
		f.data.ptr = ast_str_buffer(buf);
15669
		f.data.ptr = ast_str_buffer(buf);
15454
		f.datalen = ast_str_strlen(buf);
15670
		f.datalen = ast_str_strlen(buf) + 1;
15455
		ast_queue_frame(p->owner, &f);
15671
		ast_queue_frame(p->owner, &f);
15456
		transmit_response(p, "202 Accepted", req); /* We respond 202 accepted, since we relay the message */
15672
		transmit_response(p, "202 Accepted", req); /* We respond 202 accepted, since we relay the message */
15457
		return;
15673
		return;
15458
	}
15674
	}
15459

    
   
15675

   
[+20] [20] 759 lines
[+20] [+] static int dialog_needdestroy(void *dialogobj, void *arg, int flags)
16219
		if (dialog->rtp && ast_rtp_instance_get_bridged(dialog->rtp)) {
16435
		if (dialog->rtp && ast_rtp_instance_get_bridged(dialog->rtp)) {
16220
			ast_debug(2, "Bridge still active.  Delaying destruction of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
16436
			ast_debug(2, "Bridge still active.  Delaying destruction of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
16221
			sip_pvt_unlock(dialog);
16437
			sip_pvt_unlock(dialog);
16222
			return 0;
16438
			return 0;
16223
		}
16439
		}
16224
		
16440

   
16225
		if (dialog->vrtp && ast_rtp_instance_get_bridged(dialog->vrtp)) {
16441
		if (dialog->vrtp && ast_rtp_instance_get_bridged(dialog->vrtp)) {
16226
			ast_debug(2, "Bridge still active.  Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
16442
			ast_debug(2, "Bridge still active.  Delaying destroy of SIP dialog '%s' Method: %s\n", dialog->callid, sip_methods[dialog->method].text);
16227
			sip_pvt_unlock(dialog);
16443
			sip_pvt_unlock(dialog);
16228
			return 0;
16444
			return 0;
16229
		}
16445
		}
[+20] [20] 145 lines
[+20] [+] static char *sip_prune_realtime(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16375

    
   
16591

   
16376
/*! \brief Print codec list from preference to CLI/manager */
16592
/*! \brief Print codec list from preference to CLI/manager */
16377
static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
16593
static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
16378
{
16594
{
16379
	int x;
16595
	int x;
16380
	format_t codec;
16596
	struct ast_format codec;
16381

    
   
16597

   
16382
	for(x = 0; x < 64 ; x++) {
16598
	for(x = 0; x < AST_CODEC_PREF_SIZE; x++) {
16383
		codec = ast_codec_pref_index(pref, x);
16599
		if (!(ast_codec_pref_index(pref, x, &codec))) {
16384
		if (!codec)

   
16385
			break;
16600
			break;
16386
		ast_cli(fd, "%s", ast_getformatname(codec));
16601
		}

    
   
16602
		ast_cli(fd, "%s", ast_getformatname(&codec));
16387
		ast_cli(fd, ":%d", pref->framing[x]);
16603
		ast_cli(fd, ":%d", pref->framing[x]);
16388
		if (x < 31 && ast_codec_pref_index(pref, x + 1))
16604
		if (x < 31 && ast_codec_pref_index(pref, x + 1, &codec))
16389
			ast_cli(fd, ",");
16605
			ast_cli(fd, ",");
16390
	}
16606
	}
16391
	if (!x)
16607
	if (!x)
16392
		ast_cli(fd, "none");
16608
		ast_cli(fd, "none");
16393
}
16609
}
[+20] [20] 177 lines
[+20] [+] static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
16571
	char codec_buf[512];
16787
	char codec_buf[512];
16572
	struct ast_codec_pref *pref;
16788
	struct ast_codec_pref *pref;
16573
	struct ast_variable *v;
16789
	struct ast_variable *v;
16574
	struct sip_auth *auth;
16790
	struct sip_auth *auth;
16575
	int x = 0, load_realtime;
16791
	int x = 0, load_realtime;
16576
	format_t codec = 0;
16792
	struct ast_format codec;
16577
	int realtimepeers;
16793
	int realtimepeers;
16578

    
   
16794

   
16579
	realtimepeers = ast_check_realtime("sippeers");
16795
	realtimepeers = ast_check_realtime("sippeers");
16580

    
   
16796

   
16581
	if (argc < 4)
16797
	if (argc < 4)
[+20] [20] 104 lines
[+20] static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
16686
		} else
16902
		} else
16687
			ast_cli(fd, "(none)");
16903
			ast_cli(fd, "(none)");
16688

    
   
16904

   
16689
		ast_cli(fd, "\n");
16905
		ast_cli(fd, "\n");
16690
		ast_cli(fd, "  Codecs       : ");
16906
		ast_cli(fd, "  Codecs       : ");
16691
		ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
16907
		ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->caps);
16692
		ast_cli(fd, "%s\n", codec_buf);
16908
		ast_cli(fd, "%s\n", codec_buf);
16693
		ast_cli(fd, "  Codec Order  : (");
16909
		ast_cli(fd, "  Codec Order  : (");
16694
		print_codec_to_cli(fd, &peer->prefs);
16910
		print_codec_to_cli(fd, &peer->prefs);
16695
		ast_cli(fd, ")\n");
16911
		ast_cli(fd, ")\n");
16696

    
   
16912

   
[+20] [20] 83 lines
[+20] static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct message *m, int argc, const char *argv[])
16780
		astman_append(s, "Default-addr-IP: %s\r\nDefault-addr-port: %d\r\n", ast_sockaddr_stringify_addr(&peer->defaddr), ast_sockaddr_port(&peer->defaddr));
16996
		astman_append(s, "Default-addr-IP: %s\r\nDefault-addr-port: %d\r\n", ast_sockaddr_stringify_addr(&peer->defaddr), ast_sockaddr_port(&peer->defaddr));
16781
		astman_append(s, "Default-Username: %s\r\n", peer->username);
16997
		astman_append(s, "Default-Username: %s\r\n", peer->username);
16782
		if (!ast_strlen_zero(sip_cfg.regcontext))
16998
		if (!ast_strlen_zero(sip_cfg.regcontext))
16783
			astman_append(s, "RegExtension: %s\r\n", peer->regexten);
16999
			astman_append(s, "RegExtension: %s\r\n", peer->regexten);
16784
		astman_append(s, "Codecs: ");
17000
		astman_append(s, "Codecs: ");
16785
		ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->capability);
17001
		ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, peer->caps);
16786
		astman_append(s, "%s\r\n", codec_buf);
17002
		astman_append(s, "%s\r\n", codec_buf);
16787
		astman_append(s, "CodecOrder: ");
17003
		astman_append(s, "CodecOrder: ");
16788
		pref = &peer->prefs;
17004
		pref = &peer->prefs;
16789
		for(x = 0; x < 64 ; x++) {
17005
		for(x = 0; x < AST_CODEC_PREF_SIZE ; x++) {
16790
			codec = ast_codec_pref_index(pref, x);
17006
			if (!(ast_codec_pref_index(pref, x, &codec))) {
16791
			if (!codec)

   
16792
				break;
17007
				break;
16793
			astman_append(s, "%s", ast_getformatname(codec));
17008
			}
16794
			if (x < 63 && ast_codec_pref_index(pref, x+1))
17009
			astman_append(s, "%s", ast_getformatname(&codec));

    
   
17010
			if ((x < (AST_CODEC_PREF_SIZE - 1)) && ast_codec_pref_index(pref, x+1, &codec))
16795
				astman_append(s, ",");
17011
				astman_append(s, ",");
16796
		}
17012
		}
16797

    
   
17013

   
16798
		astman_append(s, "\r\n");
17014
		astman_append(s, "\r\n");
16799
		astman_append(s, "Status: ");
17015
		astman_append(s, "Status: ");
[+20] [20] 41 lines
[+20] [+] static char *complete_sip_user(const char *word, int state)
16841
		if (!strncasecmp(word, user->name, wordlen) && ++which > state) {
17057
		if (!strncasecmp(word, user->name, wordlen) && ++which > state) {
16842
			result = ast_strdup(user->name);
17058
			result = ast_strdup(user->name);
16843
		}
17059
		}
16844
		ao2_unlock(user);
17060
		ao2_unlock(user);
16845
		unref_peer(user, "complete sip user");
17061
		unref_peer(user, "complete sip user");

    
   
17062
		if (result) {

    
   
17063
			break;

    
   
17064
		}
16846
	}
17065
	}
16847
	ao2_iterator_destroy(&user_iter);
17066
	ao2_iterator_destroy(&user_iter);
16848
	return result;
17067
	return result;
16849
}
17068
}
16850
/*! \brief Support routine for 'sip show user' CLI */
17069
/*! \brief Support routine for 'sip show user' CLI */
[+20] [20] 431 lines
[+20] [+] static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17282
		}
17501
		}
17283
	}
17502
	}
17284
	ast_cli(a->fd, "\nGlobal Signalling Settings:\n");
17503
	ast_cli(a->fd, "\nGlobal Signalling Settings:\n");
17285
	ast_cli(a->fd, "---------------------------\n");
17504
	ast_cli(a->fd, "---------------------------\n");
17286
	ast_cli(a->fd, "  Codecs:                 ");
17505
	ast_cli(a->fd, "  Codecs:                 ");
17287
	ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, sip_cfg.capability);
17506
	ast_getformatname_multiple(codec_buf, sizeof(codec_buf) -1, sip_cfg.caps);
17288
	ast_cli(a->fd, "%s\n", codec_buf);
17507
	ast_cli(a->fd, "%s\n", codec_buf);
17289
	ast_cli(a->fd, "  Codec Order:            ");
17508
	ast_cli(a->fd, "  Codec Order:            ");
17290
	print_codec_to_cli(a->fd, &default_prefs);
17509
	print_codec_to_cli(a->fd, &default_prefs);
17291
	ast_cli(a->fd, "\n");
17510
	ast_cli(a->fd, "\n");
17292
	ast_cli(a->fd, "  Relax DTMF:             %s\n", AST_CLI_YESNO(global_relaxdtmf));
17511
	ast_cli(a->fd, "  Relax DTMF:             %s\n", AST_CLI_YESNO(global_relaxdtmf));
[+20] [20] 146 lines
[+20] [+] static int show_channels_cb(void *__cur, void *__arg, int flags)
17439
		char formatbuf[SIPBUFSIZE/2];
17658
		char formatbuf[SIPBUFSIZE/2];
17440
		
17659
		
17441
		ast_cli(arg->fd, FORMAT, ast_sockaddr_stringify_addr(dst),
17660
		ast_cli(arg->fd, FORMAT, ast_sockaddr_stringify_addr(dst),
17442
				S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
17661
				S_OR(cur->username, S_OR(cur->cid_num, "(None)")),
17443
				cur->callid,
17662
				cur->callid,
17444
				ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0),
17663
				cur->owner ? ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner->nativeformats) : "(nothing)",
17445
				AST_CLI_YESNO(ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD)),
17664
				AST_CLI_YESNO(ast_test_flag(&cur->flags[1], SIP_PAGE2_CALL_ONHOLD)),
17446
				cur->needdestroy ? "(d)" : "",
17665
				cur->needdestroy ? "(d)" : "",
17447
				cur->lastmsg ,
17666
				cur->lastmsg ,
17448
				referstatus,
17667
				referstatus,
17449
				cur->relatedpeer ? cur->relatedpeer->name : "<guest>"
17668
				cur->relatedpeer ? cur->relatedpeer->name : "<guest>"
[+20] [20] 238 lines
[+20] [+] static char *sip_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
17688
				ast_cli(a->fd, "  * SIP Call\n");
17907
				ast_cli(a->fd, "  * SIP Call\n");
17689
			}
17908
			}
17690
			ast_cli(a->fd, "  Curr. trans. direction:  %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
17909
			ast_cli(a->fd, "  Curr. trans. direction:  %s\n", ast_test_flag(&cur->flags[0], SIP_OUTGOING) ? "Outgoing" : "Incoming");
17691
			ast_cli(a->fd, "  Call-ID:                %s\n", cur->callid);
17910
			ast_cli(a->fd, "  Call-ID:                %s\n", cur->callid);
17692
			ast_cli(a->fd, "  Owner channel ID:       %s\n", cur->owner ? cur->owner->name : "<none>");
17911
			ast_cli(a->fd, "  Owner channel ID:       %s\n", cur->owner ? cur->owner->name : "<none>");
17693
			ast_cli(a->fd, "  Our Codec Capability:   %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->capability));
17912
			ast_cli(a->fd, "  Our Codec Capability:   %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->caps));
17694
			ast_cli(a->fd, "  Non-Codec Capability (DTMF):   %d\n", cur->noncodeccapability);
17913
			ast_cli(a->fd, "  Non-Codec Capability (DTMF):   %d\n", cur->noncodeccapability);
17695
			ast_cli(a->fd, "  Their Codec Capability:   %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->peercapability));
17914
			ast_cli(a->fd, "  Their Codec Capability:   %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->peercaps));
17696
			ast_cli(a->fd, "  Joint Codec Capability:   %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->jointcapability));
17915
			ast_cli(a->fd, "  Joint Codec Capability:   %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->jointcaps));
17697
			ast_cli(a->fd, "  Format:                 %s\n", ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner ? cur->owner->nativeformats : 0) );
17916
			ast_cli(a->fd, "  Format:                 %s\n", cur->owner ? ast_getformatname_multiple(formatbuf, sizeof(formatbuf), cur->owner->nativeformats) : "(nothing)" );
17698
			ast_cli(a->fd, "  T.38 support            %s\n", AST_CLI_YESNO(cur->udptl != NULL));
17917
			ast_cli(a->fd, "  T.38 support            %s\n", AST_CLI_YESNO(cur->udptl != NULL));
17699
			ast_cli(a->fd, "  Video support           %s\n", AST_CLI_YESNO(cur->vrtp != NULL));
17918
			ast_cli(a->fd, "  Video support           %s\n", AST_CLI_YESNO(cur->vrtp != NULL));
17700
			ast_cli(a->fd, "  MaxCallBR:              %d kbps\n", cur->maxcallbitrate);
17919
			ast_cli(a->fd, "  MaxCallBR:              %d kbps\n", cur->maxcallbitrate);
17701
			ast_cli(a->fd, "  Theoretical Address:    %s\n", ast_sockaddr_stringify(&cur->sa));
17920
			ast_cli(a->fd, "  Theoretical Address:    %s\n", ast_sockaddr_stringify(&cur->sa));
17702
			ast_cli(a->fd, "  Received Address:       %s\n", ast_sockaddr_stringify(&cur->recv));
17921
			ast_cli(a->fd, "  Received Address:       %s\n", ast_sockaddr_stringify(&cur->recv));
[+20] [20] 718 lines
[+20] [+] static int build_reply_digest(struct sip_pvt *p, int method, char* digest, int digest_len)
18421
		if (sipdebug)
18640
		if (sipdebug)
18422
 			ast_debug(1, "Using realm %s authentication for call %s\n", p->realm, p->callid);
18641
			ast_debug(1, "Using realm %s authentication for call %s\n", p->realm, p->callid);
18423
 	} else {
18642
	} else {
18424
 		/* No authentication, use peer or register= config */
18643
		/* No authentication, use peer or register= config */
18425
 		username = p->authname;
18644
		username = p->authname;
18426
 		secret =  p->peersecret;
18645
 		secret = p->relatedpeer 

    
   
18646
			&& !ast_strlen_zero(p->relatedpeer->remotesecret)

    
   
18647
			? p->relatedpeer->remotesecret : p->peersecret;
18427
 		md5secret = p->peermd5secret;
18648
		md5secret = p->peermd5secret;
18428
 	}
18649
	}
18429
	if (ast_strlen_zero(username))	/* We have no authentication */
18650
	if (ast_strlen_zero(username))	/* We have no authentication */
18430
		return -1;
18651
		return -1;
18431

    
   
18652

   
[+20] [20] 165 lines
[+20] [+] static int function_sippeer(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
18597
	} else  if (!strcasecmp(colname, "callerid_name")) {
18818
	} else  if (!strcasecmp(colname, "callerid_name")) {
18598
		ast_copy_string(buf, peer->cid_name, len);
18819
		ast_copy_string(buf, peer->cid_name, len);
18599
	} else  if (!strcasecmp(colname, "callerid_num")) {
18820
	} else  if (!strcasecmp(colname, "callerid_num")) {
18600
		ast_copy_string(buf, peer->cid_num, len);
18821
		ast_copy_string(buf, peer->cid_num, len);
18601
	} else  if (!strcasecmp(colname, "codecs")) {
18822
	} else  if (!strcasecmp(colname, "codecs")) {
18602
		ast_getformatname_multiple(buf, len -1, peer->capability);
18823
		ast_getformatname_multiple(buf, len -1, peer->caps);
18603
	} else if (!strcasecmp(colname, "encryption")) {
18824
	} else if (!strcasecmp(colname, "encryption")) {
18604
		snprintf(buf, len, "%d", ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP));
18825
		snprintf(buf, len, "%d", ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP));
18605
	} else  if (!strncasecmp(colname, "chanvar[", 8)) {
18826
	} else  if (!strncasecmp(colname, "chanvar[", 8)) {
18606
		char *chanvar=colname + 8;
18827
		char *chanvar=colname + 8;
18607
		struct ast_variable *v;
18828
		struct ast_variable *v;
18608
	
18829

   
18609
		chanvar = strsep(&chanvar, "]");
18830
		chanvar = strsep(&chanvar, "]");
18610
		for (v = peer->chanvars ; v ; v = v->next) {
18831
		for (v = peer->chanvars ; v ; v = v->next) {
18611
			if (!strcasecmp(v->name, chanvar)) {
18832
			if (!strcasecmp(v->name, chanvar)) {
18612
				ast_copy_string(buf, v->value, len);
18833
				ast_copy_string(buf, v->value, len);
18613
			}
18834
			}
18614
		}
18835
		}
18615
	} else  if (!strncasecmp(colname, "codec[", 6)) {
18836
	} else  if (!strncasecmp(colname, "codec[", 6)) {
18616
		char *codecnum;
18837
		char *codecnum;
18617
		format_t codec = 0;
18838
		struct ast_format codec;
18618
		
18839

   
18619
		codecnum = colname + 6;	/* move past the '[' */
18840
		codecnum = colname + 6;	/* move past the '[' */
18620
		codecnum = strsep(&codecnum, "]"); /* trim trailing ']' if any */
18841
		codecnum = strsep(&codecnum, "]"); /* trim trailing ']' if any */
18621
		if((codec = ast_codec_pref_index(&peer->prefs, atoi(codecnum)))) {
18842
		if((ast_codec_pref_index(&peer->prefs, atoi(codecnum), &codec))) {
18622
			ast_copy_string(buf, ast_getformatname(codec), len);
18843
			ast_copy_string(buf, ast_getformatname(&codec), len);
18623
		} else {
18844
		} else {
18624
			buf[0] = '\0';
18845
			buf[0] = '\0';
18625
		}
18846
		}
18626
	} else {
18847
	} else {
18627
		buf[0] = '\0';
18848
		buf[0] = '\0';
[+20] [20] 230 lines
[+20] [+] static void parse_moved_contact(struct sip_pvt *p, struct sip_request *req, char **name, char **number, int set_call_forward)
18858
		else if (!strncasecmp(contact_number, "sips:", 5))
19079
		else if (!strncasecmp(contact_number, "sips:", 5))
18859
			contact_number += 5;
19080
			contact_number += 5;
18860
		separator = strchr(contact_number, ';');	/* And username ; parameters? */
19081
		separator = strchr(contact_number, ';');	/* And username ; parameters? */
18861
		if (separator)
19082
		if (separator)
18862
			*separator = '\0';
19083
			*separator = '\0';
18863
		ast_uri_decode(contact_number);
19084
		ast_uri_decode(contact_number, ast_uri_sip_user);
18864
		if (set_call_forward) {
19085
		if (set_call_forward) {
18865
			ast_debug(2, "Received 302 Redirect to extension '%s' (domain %s)\n", contact_number, domain);
19086
			ast_debug(2, "Received 302 Redirect to extension '%s' (domain %s)\n", contact_number, domain);
18866
			if (p->owner) {
19087
			if (p->owner) {
18867
				pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
19088
				pbx_builtin_setvar_helper(p->owner, "SIPDOMAIN", domain);
18868
				ast_string_field_set(p->owner, call_forward, contact_number);
19089
				ast_string_field_set(p->owner, call_forward, contact_number);
[+20] [20] 43 lines
[+20] [+] static void check_pendings(struct sip_pvt *p)
18912
				ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
19133
				ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_DEV);
18913
			}
19134
			}
18914
			/* Perhaps there is an SD change INVITE outstanding */
19135
			/* Perhaps there is an SD change INVITE outstanding */
18915
			transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
19136
			transmit_request_with_auth(p, SIP_BYE, 0, XMIT_RELIABLE, TRUE);
18916
		}
19137
		}
18917
		ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);	
19138
		ast_clear_flag(&p->flags[0], SIP_PENDINGBYE);
18918
		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19139
		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
18919
	} else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
19140
	} else if (ast_test_flag(&p->flags[0], SIP_NEEDREINVITE)) {
18920
		/* if we can't REINVITE, hold it for later */
19141
		/* if we can't REINVITE, hold it for later */
18921
		if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
19142
		if (p->pendinginvite || p->invitestate == INV_CALLING || p->invitestate == INV_PROCEEDING || p->invitestate == INV_EARLY_MEDIA || p->waitid > 0) {
18922
			ast_debug(2, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
19143
			ast_debug(2, "NOT Sending pending reinvite (yet) on '%s'\n", p->callid);
18923
		} else {
19144
		} else {
18924
			ast_debug(2, "Sending pending reinvite on '%s'\n", p->callid);
19145
			ast_debug(2, "Sending pending reinvite on '%s'\n", p->callid);
18925
			/* Didn't get to reinvite yet, so do it now */
19146
			/* Didn't get to reinvite yet, so do it now */
18926
			transmit_reinvite_with_sdp(p, (p->t38.state == T38_LOCAL_REINVITE ? TRUE : FALSE), FALSE);
19147
			transmit_reinvite_with_sdp(p, (p->t38.state == T38_LOCAL_REINVITE ? TRUE : FALSE), FALSE);
18927
			ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);	
19148
			ast_clear_flag(&p->flags[0], SIP_NEEDREINVITE);
18928
		}
19149
		}
18929
	}
19150
	}
18930
}
19151
}
18931

    
   
19152

   
18932
/*! \brief Reset the NEEDREINVITE flag after waiting when we get 491 on a Re-invite
19153
/*! \brief Reset the NEEDREINVITE flag after waiting when we get 491 on a Re-invite
[+20] [20] 843 lines
[+20] [+] static int handle_response_register(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
19776
		if (!r) {
19997
		if (!r) {
19777
			ast_log(LOG_WARNING, "Got 200 OK on REGISTER, but there isn't a registry entry for '%s' (we probably already got the OK)\n", S_OR(p->peername, p->username));
19998
			ast_log(LOG_WARNING, "Got 200 OK on REGISTER, but there isn't a registry entry for '%s' (we probably already got the OK)\n", S_OR(p->peername, p->username));
19778
			pvt_set_needdestroy(p, "received erroneous 200 response");
19999
			pvt_set_needdestroy(p, "received erroneous 200 response");
19779
			return 0;
20000
			return 0;
19780
		}
20001
		}
19781
		
20002

   
19782
		r->regstate = REG_STATE_REGISTERED;
20003
		r->regstate = REG_STATE_REGISTERED;
19783
		r->regtime = ast_tvnow();		/* Reset time of last successful registration */
20004
		r->regtime = ast_tvnow();		/* Reset time of last successful registration */
19784
		manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
20005
		manager_event(EVENT_FLAG_SYSTEM, "Registry", "ChannelType: SIP\r\nDomain: %s\r\nStatus: %s\r\n", r->hostname, regstate2str(r->regstate));
19785
		r->regattempts = 0;
20006
		r->regattempts = 0;
19786
		ast_debug(1, "Registration successful\n");
20007
		ast_debug(1, "Registration successful\n");
[+20] [20] 5 lines
[+20] static int handle_response_register(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
19792
			r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 200");
20013
			r->call = dialog_unref(r->call, "unsetting registry->call pointer-- case 200");
19793
		p->registry = registry_unref(p->registry, "unref registry entry p->registry");
20014
		p->registry = registry_unref(p->registry, "unref registry entry p->registry");
19794
		/* Let this one hang around until we have all the responses */
20015
		/* Let this one hang around until we have all the responses */
19795
		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20016
		sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
19796
		/* p->needdestroy = 1; */
20017
		/* p->needdestroy = 1; */
19797
		
20018

   
19798
		/* set us up for re-registering
20019
		/* set us up for re-registering
19799
		 * figure out how long we got registered for
20020
		 * figure out how long we got registered for
19800
		 * according to section 6.13 of RFC, contact headers override
20021
		 * according to section 6.13 of RFC, contact headers override
19801
		 * expires headers, so check those first */
20022
		 * expires headers, so check those first */
19802
		expires = 0;
20023
		expires = 0;
[+20] [20] 549 lines
[+20] [+] static void handle_response(struct sip_pvt *p, int resp, const char *rest, struct sip_request *req, int seqno)
20352
			if (sipmethod == SIP_INVITE)
20573
			if (sipmethod == SIP_INVITE)
20353
				handle_response_invite(p, resp, rest, req, seqno);
20574
				handle_response_invite(p, resp, rest, req, seqno);
20354
			break;
20575
			break;
20355
		default:	/* Errors without handlers */
20576
		default:	/* Errors without handlers */
20356
			if ((resp >= 100) && (resp < 200)) {
20577
			if ((resp >= 100) && (resp < 200)) {
20357
				if (sipmethod == SIP_INVITE) { 	/* re-invite */
20578
				if (sipmethod == SIP_INVITE) {	/* re-invite */
20358
					if (!req->ignore && sip_cancel_destroy(p))
20579
					if (!req->ignore && sip_cancel_destroy(p))
20359
						ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
20580
						ast_log(LOG_WARNING, "Unable to cancel SIP destruction.  Expect bad things.\n");
20360
				}
20581
				}
20361
			} else if ((resp >= 200) && (resp < 300)) { /* on any unrecognized 2XX response do the following */
20582
			} else if ((resp >= 200) && (resp < 300)) { /* on any unrecognized 2XX response do the following */
20362
				if (sipmethod == SIP_INVITE) {
20583
				if (sipmethod == SIP_INVITE) {
[+20] [20] 42 lines
[+20] [+] static void *sip_park_thread(void *stuff)
20405
	copy_request(&req, &d->req);
20626
	copy_request(&req, &d->req);
20406

    
   
20627

   
20407
	if (!transferee || !transferer) {
20628
	if (!transferee || !transferer) {
20408
		ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
20629
		ast_log(LOG_ERROR, "Missing channels for parking! Transferer %s Transferee %s\n", transferer ? "<available>" : "<missing>", transferee ? "<available>" : "<missing>" );
20409
		deinit_req(&d->req);
20630
		deinit_req(&d->req);
20410
		free(d);
20631
		ast_free(d);
20411
		return NULL;
20632
		return NULL;
20412
	}
20633
	}
20413
	ast_debug(4, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
20634
	ast_debug(4, "SIP Park: Transferer channel %s, Transferee %s\n", transferer->name, transferee->name);
20414

    
   
20635

   
20415
	if (ast_do_masquerade(transferee)) {
20636
	if (ast_do_masquerade(transferee)) {
20416
		ast_log(LOG_WARNING, "Masquerade failed.\n");
20637
		ast_log(LOG_WARNING, "Masquerade failed.\n");
20417
		transmit_response(transferer->tech_pvt, "503 Internal error", &req);
20638
		transmit_response(transferer->tech_pvt, "503 Internal error", &req);
20418
		deinit_req(&d->req);
20639
		deinit_req(&d->req);
20419
		free(d);
20640
		ast_free(d);
20420
		return NULL;
20641
		return NULL;
20421
	}
20642
	}
20422

    
   
20643

   
20423
	res = ast_park_call(transferee, transferer, 0, d->parkexten, &ext);
20644
	res = ast_park_call(transferee, transferer, 0, d->parkexten, &ext);
20424
	

   
20425

    
   
20645

   
20426
#ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
20646
#ifdef WHEN_WE_KNOW_THAT_THE_CLIENT_SUPPORTS_MESSAGE
20427
	if (!res) {
20647
	if (!res) {
20428
		transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n", 0, 0);
20648
		transmit_message_with_text(transferer->tech_pvt, "Unable to park call.\n", 0, 0);
20429
	} else {
20649
	} else {
20430
		/* Then tell the transferer what happened */
20650
		/* Then tell the transferer what happened */
20431
		sprintf(buf, "Call parked on extension '%d'", ext);
20651
		sprintf(buf, "Call parked on extension '%d'", ext);
20432
		transmit_message_with_text(transferer->tech_pvt, buf, 0, 0);
20652
		transmit_message_with_text(transferer->tech_pvt, buf, 0, 0);
20433
	}
20653
	}
20434
#endif
20654
#endif
20435

    
   
20655

   
20436
	/* Any way back to the current call??? */
20656
	/* Any way back to the current call??? */
20437
	/* Transmit response to the REFER request */
20657
	/* Transmit response to the REFER request */
20438
	transmit_response(transferer->tech_pvt, "202 Accepted", &req);

   
20439
	if (!res)	{
20658
	if (!res)	{
20440
		/* Transfer succeeded */
20659
		/* Transfer succeeded */
20441
		append_history(transferer->tech_pvt, "SIPpark", "Parked call on %d", ext);
20660
		append_history(transferer->tech_pvt, "SIPpark", "Parked call on %d", ext);
20442
		transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
20661
		transmit_notify_with_sipfrag(transferer->tech_pvt, d->seqno, "200 OK", TRUE);
20443
		transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
20662
		transferer->hangupcause = AST_CAUSE_NORMAL_CLEARING;
[+20] [20] 4 lines
[+20] static void *sip_park_thread(void *stuff)
20448
		append_history(transferer->tech_pvt, "SIPpark", "Parking failed\n");
20667
		append_history(transferer->tech_pvt, "SIPpark", "Parking failed\n");
20449
		ast_debug(1, "SIP Call parked failed \n");
20668
		ast_debug(1, "SIP Call parked failed \n");
20450
		/* Do not hangup call */
20669
		/* Do not hangup call */
20451
	}
20670
	}
20452
	deinit_req(&d->req);
20671
	deinit_req(&d->req);
20453
	free(d);
20672
	ast_free(d);
20454
	return NULL;
20673
	return NULL;
20455
}
20674
}
20456

    
   
20675

   
20457
/*! \brief Park a call using the subsystem in res_features.c
20676
/*! \brief Park a call using the subsystem in res_features.c
20458
	This is executed in a separate thread
20677
	This is executed in a separate thread
[+20] [20] 70 lines
[+20] [+] static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno, char *parkexten)
20529
		if (!transferee) {
20748
		if (!transferee) {
20530
			ast_debug(1, "No transferee channel, giving up parking\n");
20749
			ast_debug(1, "No transferee channel, giving up parking\n");
20531
		}
20750
		}
20532
		return -1;
20751
		return -1;
20533
	}
20752
	}
20534
	if ((d = ast_calloc(1, sizeof(*d)))) {
20753
	if (!(d = ast_calloc(1, sizeof(*d)))) {

    
   
20754
		return -1;

    
   
20755
	}
20535

    
   
20756

   
20536
		/* Save original request for followup */
20757
	/* Save original request for followup */
20537
		copy_request(&d->req, req);
20758
	copy_request(&d->req, req);
20538
		d->chan1 = transferee;	/* Transferee */
20759
	d->chan1 = transferee;	/* Transferee */
20539
		d->chan2 = transferer;	/* Transferer */
20760
	d->chan2 = transferer;	/* Transferer */
20540
		d->seqno = seqno;
20761
	d->seqno = seqno;
20541
		d->parkexten = parkexten;
20762
	d->parkexten = parkexten;
20542
		if (ast_pthread_create_detached_background(&th, NULL, sip_park_thread, d) < 0) {
20763
	if (ast_pthread_create_detached_background(&th, NULL, sip_park_thread, d) < 0) {
20543
			/* Could not start thread */
20764
		/* Could not start thread */
20544
			deinit_req(&d->req);
20765
		deinit_req(&d->req);
20545
			ast_free(d);	/* We don't need it anymore. If thread is created, d will be free'd
20766
		ast_free(d);	/* We don't need it anymore. If thread is created, d will be free'd
20546
					   by sip_park_thread() */
20767
				   by sip_park_thread() */
20547
			return 0;
Moved to 28337

   
20548
		}
Moved to 28338

   
20549
	}

   
20550
	return -1;
20768
		return -1;
20551
}
20769
	}
Moved from 20547

    
   
20770
	return 0;
Moved from 20548

    
   
20771
}
20552

    
   
20772

   
20553
/*! \brief Turn off generator data
20773
/*! \brief Turn off generator data
20554
	XXX Does this function belong in the SIP channel?
20774
	XXX Does this function belong in the SIP channel?
20555
*/
20775
*/
20556
static void ast_quiet_chan(struct ast_channel *chan)
20776
static void ast_quiet_chan(struct ast_channel *chan)
[+20] [20] 9 lines
[+20] static int sip_park(struct ast_channel *chan1, struct ast_channel *chan2, struct sip_request *req, int seqno, char *parkexten)
20566
/*! \brief Attempt transfer of SIP call
20786
/*! \brief Attempt transfer of SIP call
20567
	This fix for attended transfers on a local PBX */
20787
	This fix for attended transfers on a local PBX */
20568
static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
20788
static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
20569
{
20789
{
20570
	int res = 0;
20790
	int res = 0;
20571
	struct ast_channel *peera = NULL,	
20791
	struct ast_channel *peera = NULL,
20572
		*peerb = NULL,
20792
		*peerb = NULL,
20573
		*peerc = NULL,
20793
		*peerc = NULL,
20574
		*peerd = NULL;
20794
		*peerd = NULL;
20575

    
   
20795

   
20576

    
   
20796

   
20577
	/* We will try to connect the transferee with the target and hangup
20797
	/* We will try to connect the transferee with the target and hangup
20578
	   all channels to the transferer */	
20798
	   all channels to the transferer */
20579
	ast_debug(4, "Sip transfer:--------------------\n");
20799
	ast_debug(4, "Sip transfer:--------------------\n");
20580
	if (transferer->chan1)
20800
	if (transferer->chan1)
20581
		ast_debug(4, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
20801
		ast_debug(4, "-- Transferer to PBX channel: %s State %s\n", transferer->chan1->name, ast_state2str(transferer->chan1->_state));
20582
	else
20802
	else
20583
		ast_debug(4, "-- No transferer first channel - odd??? \n");
20803
		ast_debug(4, "-- No transferer first channel - odd??? \n");
[+20] [20] 24 lines
[+20] static int attempt_transfer(struct sip_dual *transferer, struct sip_dual *target)
20608
		ast_debug(3, "SIP transfer: Three channels to handle\n");
20828
		ast_debug(3, "SIP transfer: Three channels to handle\n");
20609
	}
20829
	}
20610

    
   
20830

   
20611
	if (peera && peerb && peerc && (peerb != peerc)) {
20831
	if (peera && peerb && peerc && (peerb != peerc)) {
20612
		ast_quiet_chan(peera);		/* Stop generators */
20832
		ast_quiet_chan(peera);		/* Stop generators */
20613
		ast_quiet_chan(peerb);	
20833
		ast_quiet_chan(peerb);
20614
		ast_quiet_chan(peerc);
20834
		ast_quiet_chan(peerc);
20615
		if (peerd)
20835
		if (peerd)
20616
			ast_quiet_chan(peerd);
20836
			ast_quiet_chan(peerd);
20617

    
   
20837

   
20618
		ast_debug(4, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
20838
		ast_debug(4, "SIP transfer: trying to masquerade %s into %s\n", peerc->name, peerb->name);
[+20] [20] 108 lines
[+20] [+] static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e)
20727

    
   
20947

   
20728
	if( (sep = strchr(event, ';')) ) {	/* XXX bug here - overwriting string ? */
20948
	if( (sep = strchr(event, ';')) ) {	/* XXX bug here - overwriting string ? */
20729
		*sep++ = '\0';
20949
		*sep++ = '\0';
20730
		eventid = sep;
20950
		eventid = sep;
20731
	}
20951
	}
20732
	
20952

   
20733
	if (sipdebug)
20953
	if (sipdebug)
20734
		ast_debug(2, "Got NOTIFY Event: %s\n", event);
20954
		ast_debug(2, "Got NOTIFY Event: %s\n", event);
20735

    
   
20955

   
20736
	if (!strcmp(event, "refer")) {
20956
	if (!strcmp(event, "refer")) {
20737
		/* Save nesting depth for now, since there might be other events we will
20957
		/* Save nesting depth for now, since there might be other events we will
[+20] [20] 9 lines
[+20] static int handle_request_notify(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e)
20747
		/* EventID for each transfer... EventID is basically the REFER cseq
20967
		/* EventID for each transfer... EventID is basically the REFER cseq
20748

    
   
20968

   
20749
		 We are getting notifications on a call that we transfered
20969
		 We are getting notifications on a call that we transfered
20750
		 We should hangup when we are getting a 200 OK in a sipfrag
20970
		 We should hangup when we are getting a 200 OK in a sipfrag
20751
		 Check if we have an owner of this event */
20971
		 Check if we have an owner of this event */
20752
		
20972

   
20753
		/* Check the content type */
20973
		/* Check the content type */
20754
		if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
20974
		if (strncasecmp(get_header(req, "Content-Type"), "message/sipfrag", strlen("message/sipfrag"))) {
20755
			/* We need a sipfrag */
20975
			/* We need a sipfrag */
20756
			transmit_response(p, "400 Bad request", req);
20976
			transmit_response(p, "400 Bad request", req);
20757
			sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
20977
			sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
[+20] [20] 590 lines
[+20] [+] static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock)
21348

    
   
21568

   
21349
		if (sipdebug)
21569
		if (sipdebug)
21350
			ast_debug(3, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
21570
			ast_debug(3, "INVITE part of call transfer. Replaces [%s]\n", p_replaces);
21351
		/* Create a buffer we can manipulate */
21571
		/* Create a buffer we can manipulate */
21352
		replace_id = ast_strdupa(p_replaces);
21572
		replace_id = ast_strdupa(p_replaces);
21353
		ast_uri_decode(replace_id);
21573
		ast_uri_decode(replace_id, ast_uri_sip_user);
21354

    
   
21574

   
21355
		if (!p->refer && !sip_refer_allocate(p)) {
21575
		if (!p->refer && !sip_refer_allocate(p)) {
21356
			transmit_response_reliable(p, "500 Server Internal Error", req);
21576
			transmit_response_reliable(p, "500 Server Internal Error", req);
21357
			append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
21577
			append_history(p, "Xfer", "INVITE/Replace Failed. Out of memory.");
21358
			sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
21578
			sip_scheddestroy(p, DEFAULT_TRANS_TIMEOUT);
[+20] [20] 166 lines
[+20] static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock)
21525
					res = -1;
21745
					res = -1;
21526
					goto request_invite_cleanup;
21746
					goto request_invite_cleanup;
21527
				}
21747
				}
21528
				ast_queue_control(p->owner, AST_CONTROL_SRCUPDATE);
21748
				ast_queue_control(p->owner, AST_CONTROL_SRCUPDATE);
21529
			} else {
21749
			} else {
21530
				p->jointcapability = p->capability;
21750
				ast_format_cap_copy(p->jointcaps, p->caps);
21531
				ast_debug(1, "Hm....  No sdp for the moment\n");
21751
				ast_debug(1, "Hm....  No sdp for the moment\n");
21532
				/* Some devices signal they want to be put off hold by sending a re-invite
21752
				/* Some devices signal they want to be put off hold by sending a re-invite
21533
				   *without* an SDP, which is supposed to mean "Go back to your state"
21753
				   *without* an SDP, which is supposed to mean "Go back to your state"
21534
				   and since they put os on remote hold, we go back to off hold */
21754
				   and since they put os on remote hold, we go back to off hold */
21535
				if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
21755
				if (ast_test_flag(&p->flags[1], SIP_PAGE2_CALL_ONHOLD)) {
[+20] [20] 71 lines
[+20] static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock)
21607
				ast_debug(1, "No compatible codecs for this SIP call.\n");
21827
				ast_debug(1, "No compatible codecs for this SIP call.\n");
21608
				res = -1;
21828
				res = -1;
21609
				goto request_invite_cleanup;
21829
				goto request_invite_cleanup;
21610
			}
21830
			}
21611
		} else {	/* No SDP in invite, call control session */
21831
		} else {	/* No SDP in invite, call control session */
21612
			p->jointcapability = p->capability;
21832
			ast_format_cap_copy(p->jointcaps, p->caps);
21613
			ast_debug(2, "No SDP in Invite, third party call control\n");
21833
			ast_debug(2, "No SDP in Invite, third party call control\n");
21614
		}
21834
		}
21615

    
   
21835

   
21616
		/* Queue NULL frame to prod ast_rtp_bridge if appropriate */
21836
		/* Queue NULL frame to prod ast_rtp_bridge if appropriate */
21617
		/* This seems redundant ... see !p-owner above */
21837
		/* This seems redundant ... see !p-owner above */
[+20] [20] 42 lines
[+20] static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock)
21660
			case SIP_GET_DEST_REFUSED:
21880
			case SIP_GET_DEST_REFUSED:
21661
			default:
21881
			default:
21662
				{
21882
				{
21663
					char *decoded_exten = ast_strdupa(p->exten);
21883
					char *decoded_exten = ast_strdupa(p->exten);
21664
					transmit_response_reliable(p, "404 Not Found", req);
21884
					transmit_response_reliable(p, "404 Not Found", req);
21665
					ast_uri_decode(decoded_exten);
21885
					ast_uri_decode(decoded_exten, ast_uri_sip_user);
21666
					ast_log(LOG_NOTICE, "Call from '%s' to extension"
21886
					ast_log(LOG_NOTICE, "Call from '%s' to extension"
21667
						" '%s' rejected because extension not found in context '%s'.\n",
21887
						" '%s' rejected because extension not found in context '%s'.\n",
21668
						S_OR(p->username, p->peername), decoded_exten, p->context);
21888
						S_OR(p->username, p->peername), decoded_exten, p->context);
21669
				}
21889
				}
21670
			} /* end switch */
21890
			} /* end switch */
[+20] [20] 338 lines
[+20] static int handle_request_invite(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, struct ast_sockaddr *addr, int *recount, const char *e, int *nounlock)
22009
		}
22229
		}
22010
	} else {
22230
	} else {
22011
		if (p && (p->autokillid == -1)) {
22231
		if (p && (p->autokillid == -1)) {
22012
			const char *msg;
22232
			const char *msg;
22013

    
   
22233

   
22014
			if (!p->jointcapability)
22234
			if ((ast_format_cap_is_empty(p->jointcaps)))
22015
				msg = "488 Not Acceptable Here (codec error)";
22235
				msg = "488 Not Acceptable Here (codec error)";
22016
			else {
22236
			else {
22017
				ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
22237
				ast_log(LOG_NOTICE, "Unable to create/find SIP channel for this INVITE\n");
22018
				msg = "503 Unavailable";
22238
				msg = "503 Unavailable";
22019
			}
22239
			}
[+20] [20] 45 lines
[+20] [+] static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno, int *nounlock)
22065
	/* Check if the call ID of the replaces header does exist locally */
22285
	/* Check if the call ID of the replaces header does exist locally */
22066
	if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
22286
	if (!(targetcall_pvt = get_sip_pvt_byid_locked(transferer->refer->replaces_callid, transferer->refer->replaces_callid_totag,
22067
		transferer->refer->replaces_callid_fromtag))) {
22287
		transferer->refer->replaces_callid_fromtag))) {
22068
		if (transferer->refer->localtransfer) {
22288
		if (transferer->refer->localtransfer) {
22069
			/* We did not find the refered call. Sorry, can't accept then */
22289
			/* We did not find the refered call. Sorry, can't accept then */
22070
			transmit_response(transferer, "202 Accepted", req);

   
22071
			/* Let's fake a response from someone else in order
22290
			/* Let's fake a response from someone else in order
22072
		   	to follow the standard */
22291
		   	to follow the standard */
22073
			transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
22292
			transmit_notify_with_sipfrag(transferer, seqno, "481 Call leg/transaction does not exist", TRUE);
22074
			append_history(transferer, "Xfer", "Refer failed");
22293
			append_history(transferer, "Xfer", "Refer failed");
22075
			ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
22294
			ast_clear_flag(&transferer->flags[0], SIP_GOTREFER);
[+20] [20] 4 lines
[+20] static int local_attended_transfer(struct sip_pvt *transferer, struct sip_dual *current, struct sip_request *req, int seqno, int *nounlock)
22080
		ast_debug(3, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
22299
		ast_debug(3, "SIP attended transfer: Not our call - generating INVITE with replaces\n");
22081
		return 0;
22300
		return 0;
22082
	}
22301
	}
22083

    
   
22302

   
22084
	/* Ok, we can accept this transfer */
22303
	/* Ok, we can accept this transfer */
22085
	transmit_response(transferer, "202 Accepted", req);

   
22086
	append_history(transferer, "Xfer", "Refer accepted");
22304
	append_history(transferer, "Xfer", "Refer accepted");
22087
	if (!targetcall_pvt->owner) {	/* No active channel */
22305
	if (!targetcall_pvt->owner) {	/* No active channel */
22088
		ast_debug(4, "SIP attended transfer: Error: No owner of target call\n");
22306
		ast_debug(4, "SIP attended transfer: Error: No owner of target call\n");
22089
		/* Cancel transfer */
22307
		/* Cancel transfer */
22090
		transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
22308
		transmit_notify_with_sipfrag(transferer, seqno, "503 Service Unavailable", TRUE);
[+20] [20] 282 lines
[+20] [+] static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock)
22373
		default:
22591
		default:
22374
			/* Refer-to extension not found, fake a failed transfer */
22592
			/* Refer-to extension not found, fake a failed transfer */
22375
			transmit_response(p, "202 Accepted", req);
22593
			transmit_response(p, "202 Accepted", req);
22376
			append_history(p, "Xfer", "Refer failed. Bad extension.");
22594
			append_history(p, "Xfer", "Refer failed. Bad extension.");
22377
			transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
22595
			transmit_notify_with_sipfrag(p, seqno, "404 Not found", TRUE);
22378
			ast_clear_flag(&p->flags[0], SIP_GOTREFER);	
22596
			ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22379
			if (req->debug)
22597
			if (req->debug)
22380
				ast_debug(1, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
22598
				ast_debug(1, "SIP transfer to bad extension: %s\n", p->refer->refer_to);
22381
			break;
22599
			break;
22382
		}
22600
		}
22383
		return 0;
22601
		return 0;
[+20] [20] 70 lines
[+20] static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock)
22454
		ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
22672
		ast_queue_control(current.chan1, AST_CONTROL_UNHOLD);
22455
	}
22673
	}
22456

    
   
22674

   
22457
	ast_set_flag(&p->flags[0], SIP_GOTREFER);
22675
	ast_set_flag(&p->flags[0], SIP_GOTREFER);
22458

    
   
22676

   

    
   
22677
	/* From here on failures will be indicated with NOTIFY requests */

    
   
22678
	transmit_response(p, "202 Accepted", req);

    
   
22679

   
22459
	/* Attended transfer: Find all call legs and bridge transferee with target*/
22680
	/* Attended transfer: Find all call legs and bridge transferee with target*/
22460
	if (p->refer->attendedtransfer) {
22681
	if (p->refer->attendedtransfer) {
22461
		if ((res = local_attended_transfer(p, &current, req, seqno, nounlock)))
22682
		if ((res = local_attended_transfer(p, &current, req, seqno, nounlock)))
22462
			return res;	/* We're done with the transfer */
22683
			return res;	/* We're done with the transfer */
22463
		/* Fall through for remote transfers that we did not find locally */
22684
		/* Fall through for remote transfers that we did not find locally */
22464
		if (sipdebug)
22685
		if (sipdebug)
22465
			ast_debug(4, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
22686
			ast_debug(4, "SIP attended transfer: Still not our call - generating INVITE with replaces\n");
22466
		/* Fallthrough if we can't find the call leg internally */
22687
		/* Fallthrough if we can't find the call leg internally */
22467
	}
22688
	}
22468

    
   
22689

   
22469
	/* Parking a call */
22690
	/* Must release lock now, because it will not longer
22470
	if (p->refer->localtransfer && ast_parking_ext_valid(p->refer->refer_to, p->owner, p->owner->context)) {
22691
	   be accessible after the transfer! */
22471
		/* Must release c's lock now, because it will not longer be accessible after the transfer! */

   
22472
		*nounlock = 1;
22692
	*nounlock = 1;
Moved from 22531

    
   
22693
	/*
Moved from 22532

    
   
22694
	 * Increase ref count so that we can delay channel destruction until after
Moved from 22533

    
   
22695
	 * we get a chance to fire off some events.
Moved from 22534

    
   
22696
	 */
Moved from 22535

    
   
22697
	ast_channel_ref(current.chan1);

    
   
22698
	sip_pvt_unlock(p);
22473
		ast_channel_unlock(current.chan1);
22699
	ast_channel_unlock(current.chan1);

    
   
22700

   

    
   
22701
	/* Parking a call */

    
   
22702
	if (p->refer->localtransfer && ast_parking_ext_valid(p->refer->refer_to, p->owner, p->owner->context)) {

    
   
22703
		sip_pvt_lock(p);
22474
		copy_request(&current.req, req);
22704
		copy_request(&current.req, req);
22475
		ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22705
		ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22476
		p->refer->status = REFER_200OK;
22706
		p->refer->status = REFER_200OK;
22477
		append_history(p, "Xfer", "REFER to call parking.");
22707
		append_history(p, "Xfer", "REFER to call parking.");
22478
		ast_manager_event_multichan(EVENT_FLAG_CALL, "Transfer", 2, chans,
22708
		ast_manager_event_multichan(EVENT_FLAG_CALL, "Transfer", 2, chans,
[+20] [20] 12 lines
[+20] static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock)
22491
			current.chan2->name,
22721
			current.chan2->name,
22492
			current.chan2->uniqueid,
22722
			current.chan2->uniqueid,
22493
			p->refer->refer_to);
22723
			p->refer->refer_to);
22494
		if (sipdebug)
22724
		if (sipdebug)
22495
			ast_debug(4, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
22725
			ast_debug(4, "SIP transfer to parking: trying to park %s. Parked by %s\n", current.chan2->name, current.chan1->name);
22496
		sip_park(current.chan2, current.chan1, req, seqno, p->refer->refer_to);
22726
		if (sip_park(current.chan2, current.chan1, req, seqno, p->refer->refer_to)) {

    
   
22727
			transmit_notify_with_sipfrag(p, seqno, "500 Internal Server Error", TRUE);

    
   
22728
		}

    
   
22729
		ast_channel_unref(current.chan1);
22497
		return res;
22730
		return res;
22498
	}
22731
	}
22499

    
   
22732

   
22500
	/* Blind transfers and remote attended xfers */
22733
	/* Blind transfers and remote attended xfers */
22501
	transmit_response(p, "202 Accepted", req);

   
22502

    
   

   
22503
	if (current.chan1 && current.chan2) {
22734
	if (current.chan1 && current.chan2) {
22504
		ast_debug(3, "chan1->name: %s\n", current.chan1->name);
22735
		ast_debug(3, "chan1->name: %s\n", current.chan1->name);
22505
		pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
22736
		pbx_builtin_setvar_helper(current.chan1, "BLINDTRANSFER", current.chan2->name);
22506
	}
22737
	}

    
   
22738

   

    
   
22739
	sip_pvt_lock(p);

    
   
22740

   
22507
	if (current.chan2) {
22741
	if (current.chan2) {
22508
		pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
22742
		pbx_builtin_setvar_helper(current.chan2, "BLINDTRANSFER", current.chan1->name);
22509
		pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
22743
		pbx_builtin_setvar_helper(current.chan2, "SIPDOMAIN", p->refer->refer_to_domain);
22510
		pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
22744
		pbx_builtin_setvar_helper(current.chan2, "SIPTRANSFER", "yes");
22511
		/* One for the new channel */
22745
		/* One for the new channel */
[+20] [20] 11 lines
[+20] static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock)
22523
				p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
22757
				p->refer->replaces_callid_fromtag ? ";from-tag=" : "",
22524
				p->refer->replaces_callid_fromtag);
22758
				p->refer->replaces_callid_fromtag);
22525
		if (current.chan2)
22759
		if (current.chan2)
22526
			pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
22760
			pbx_builtin_setvar_helper(current.chan2, "_SIPTRANSFER_REPLACES", tempheader);
22527
	}
22761
	}
22528
	/* Must release lock now, because it will not longer

   
22529
	   be accessible after the transfer! */

   
22530
	*nounlock = 1;

   
22531
	/*
Moved to 22693

   
22532
	 * Increase ref count so that we can delay channel destruction until after
Moved to 22694

   
22533
	 * we get a chance to fire off some events.
Moved to 22695

   
22534
	 */
Moved to 22696

   
22535
	ast_channel_ref(current.chan1);
Moved to 22697

   
22536
	ast_channel_unlock(current.chan1);

   
22537

    
   
22762

   
22538
	/* Connect the call */
22763
	/* Connect the call */
22539

    
   
22764

   
22540
	/* FAKE ringing if not attended transfer */
22765
	/* FAKE ringing if not attended transfer */
22541
	if (!p->refer->attendedtransfer)
22766
	if (!p->refer->attendedtransfer)
[+20] [20] 62 lines
[+20] static int handle_request_refer(struct sip_pvt *p, struct sip_request *req, int debug, int seqno, int *nounlock)
22604
		if (p->refer->localtransfer)
22829
		if (p->refer->localtransfer)
22605
			p->refer->status = REFER_200OK;
22830
			p->refer->status = REFER_200OK;
22606
		if (p->owner)
22831
		if (p->owner)
22607
			p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
22832
			p->owner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
22608
		append_history(p, "Xfer", "Refer succeeded.");
22833
		append_history(p, "Xfer", "Refer succeeded.");
22609
		ast_clear_flag(&p->flags[0], SIP_GOTREFER);	
22834
		ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22610
		/* Do not hangup call, the other side do that when we say 200 OK */
22835
		/* Do not hangup call, the other side do that when we say 200 OK */
22611
		/* We could possibly implement a timer here, auto congestion */
22836
		/* We could possibly implement a timer here, auto congestion */
22612
		res = 0;
22837
		res = 0;
22613
	} else {
22838
	} else {
22614
		ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);	/* Don't delay hangup */
22839
		ast_clear_flag(&p->flags[0], SIP_DEFER_BYE_ON_TRANSFER);	/* Don't delay hangup */
22615
		ast_debug(3, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
22840
		ast_debug(3, "%s transfer failed. Resuming original call.\n", p->refer->attendedtransfer? "Attended" : "Blind");
22616
		append_history(p, "Xfer", "Refer failed.");
22841
		append_history(p, "Xfer", "Refer failed.");
22617
		/* Failure of some kind */
22842
		/* Failure of some kind */
22618
		p->refer->status = REFER_FAILED;
22843
		p->refer->status = REFER_FAILED;
22619
		transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
22844
		transmit_notify_with_sipfrag(p, seqno, "503 Service Unavailable", TRUE);
22620
		ast_clear_flag(&p->flags[0], SIP_GOTREFER);	
22845
		ast_clear_flag(&p->flags[0], SIP_GOTREFER);
22621
		res = -1;
22846
		res = -1;
22622
	}
22847
	}
22623

    
   
22848

   
22624
	ast_channel_unref(current.chan1);
22849
	ast_channel_unref(current.chan1);
22625

    
   
22850

   
[+20] [20] 877 lines
[+20] [+] static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e)
23503
	int gotdest = 0;
23728
	int gotdest = 0;
23504
	int res = 0;
23729
	int res = 0;
23505
	int firststate = AST_EXTENSION_REMOVED;
23730
	int firststate = AST_EXTENSION_REMOVED;
23506
	struct sip_peer *authpeer = NULL;
23731
	struct sip_peer *authpeer = NULL;
23507
	const char *eventheader = get_header(req, "Event");	/* Get Event package name */
23732
	const char *eventheader = get_header(req, "Event");	/* Get Event package name */
23508
	int resubscribe = (p->subscribed != NONE);
23733
	int resubscribe = (p->subscribed != NONE) && !req->ignore;
23509
	char *temp, *event;
23734
	char *temp, *event;
23510

    
   
23735

   
23511
	if (p->initreq.headers) {	
23736
	if (p->initreq.headers) {
23512
		/* We already have a dialog */
23737
		/* We already have a dialog */
23513
		if (p->initreq.method != SIP_SUBSCRIBE) {
23738
		if (p->initreq.method != SIP_SUBSCRIBE) {
23514
			/* This is a SUBSCRIBE within another SIP dialog, which we do not support */
23739
			/* This is a SUBSCRIBE within another SIP dialog, which we do not support */
23515
			/* For transfers, this could happen, but since we haven't seen it happening, let us just refuse this */
23740
			/* For transfers, this could happen, but since we haven't seen it happening, let us just refuse this */
23516
 			transmit_response(p, "403 Forbidden (within dialog)", req);
23741
			transmit_response(p, "403 Forbidden (within dialog)", req);
23517
			/* Do not destroy session, since we will break the call if we do */
23742
			/* Do not destroy session, since we will break the call if we do */
23518
			ast_debug(1, "Got a subscription within the context of another call, can't handle that - %s (Method %s)\n", p->callid, sip_methods[p->initreq.method].text);
23743
			ast_debug(1, "Got a subscription within the context of another call, can't handle that - %s (Method %s)\n", p->callid, sip_methods[p->initreq.method].text);
23519
			return 0;
23744
			return 0;
23520
		} else if (req->debug) {
23745
		} else if (req->debug) {
23521
			if (resubscribe)
23746
			if (resubscribe)
23522
				ast_debug(1, "Got a re-subscribe on existing subscription %s\n", p->callid);
23747
				ast_debug(1, "Got a re-subscribe on existing subscription %s\n", p->callid);
23523
			else
23748
			else
23524
				ast_debug(1, "Got a new subscription %s (possibly with auth)\n", p->callid);
23749
				ast_debug(1, "Got a new subscription %s (possibly with auth) or retransmission\n", p->callid);
23525
		}
23750
		}
23526
	}
23751
	}
23527

    
   
23752

   
23528
	/* Check if we have a global disallow setting on subscriptions.
23753
	/* Check if we have a global disallow setting on subscriptions.
23529
		if so, we don't have to check peer settings after auth, which saves a lot of processing
23754
		if so, we don't have to check peer settings after auth, which saves a lot of processing
[+20] [20] 38 lines
[+20] static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e)
23568
		return 0;
23793
		return 0;
23569
	}
23794
	}
23570

    
   
23795

   
23571
	if ( (strchr(eventheader, ';'))) {
23796
	if ( (strchr(eventheader, ';'))) {
23572
		event = ast_strdupa(eventheader);	/* Since eventheader is a const, we can't change it */
23797
		event = ast_strdupa(eventheader);	/* Since eventheader is a const, we can't change it */
23573
		temp = strchr(event, ';'); 		
23798
		temp = strchr(event, ';');
23574
		*temp = '\0';				/* Remove any options for now */
23799
		*temp = '\0';				/* Remove any options for now */
23575
							/* We might need to use them later :-) */
23800
							/* We might need to use them later :-) */
23576
	} else
23801
	} else
23577
		event = (char *) eventheader;		/* XXX is this legal ? */
23802
		event = (char *) eventheader;		/* XXX is this legal ? */
23578

    
   
23803

   
23579
	/* Handle authentication */
23804
	/* Handle authentication if we're new and not a retransmission. We can't just

    
   
23805
	 * use if !req->ignore, because then we'll end up sending

    
   
23806
	 * a 200 OK if someone retransmits without sending auth */

    
   
23807
	if (p->subscribed == NONE || resubscribe) {
23580
	res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, addr, &authpeer);
23808
		res = check_user_full(p, req, SIP_SUBSCRIBE, e, 0, addr, &authpeer);

    
   
23809

   
23581
	/* if an authentication response was sent, we are done here */
23810
		/* if an authentication response was sent, we are done here */
23582
	if (res == AUTH_CHALLENGE_SENT)	/* authpeer = NULL here */
23811
		if (res == AUTH_CHALLENGE_SENT)	/* authpeer = NULL here */
23583
		return 0;
23812
			return 0;
23584
	if (res < 0) {
23813
		if (res < 0) {
23585
		if (res == AUTH_FAKE_AUTH) {
23814
			if (res == AUTH_FAKE_AUTH) {
23586
			ast_log(LOG_NOTICE, "Sending fake auth rejection for device %s\n", get_header(req, "From"));
23815
				ast_log(LOG_NOTICE, "Sending fake auth rejection for device %s\n", get_header(req, "From"));
23587
			transmit_fake_auth_response(p, SIP_SUBSCRIBE, req, XMIT_UNRELIABLE);
23816
				transmit_fake_auth_response(p, SIP_SUBSCRIBE, req, XMIT_UNRELIABLE);
23588
		} else {
23817
			} else {
23589
			ast_log(LOG_NOTICE, "Failed to authenticate device %s for SUBSCRIBE\n", get_header(req, "From"));
23818
				ast_log(LOG_NOTICE, "Failed to authenticate device %s for SUBSCRIBE\n", get_header(req, "From"));
23590
			transmit_response_reliable(p, "403 Forbidden", req);
23819
				transmit_response_reliable(p, "403 Forbidden", req);
23591
		}
23820
			}

    
   
23821

   
23592
		pvt_set_needdestroy(p, "authentication failed");
23822
			pvt_set_needdestroy(p, "authentication failed");
23593
		return 0;
23823
			return 0;
23594
	}
23824
		}

    
   
23825
	}
23595

    
   
23826

   
23596
	/* At this point, authpeer cannot be NULL. Remember we hold a reference,
23827
	/* At this point, authpeer cannot be NULL. Remember we hold a reference,
23597
	 * so we must release it when done.
23828
	 * so we must release it when done.
23598
	 * XXX must remove all the checks for authpeer == NULL.
23829
	 * XXX must remove all the checks for authpeer == NULL.
23599
	 */
23830
	 */
[+20] [20] 114 lines
[+20] static int handle_request_subscribe(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int seqno, const char *e)
23714

    
   
23945

   
23715
		acceptheader = __get_header(req, "Accept", &start);
23946
		acceptheader = __get_header(req, "Accept", &start);
23716
		while (!found_supported && !ast_strlen_zero(acceptheader)) {
23947
		while (!found_supported && !ast_strlen_zero(acceptheader)) {
23717
			found_supported = strcmp(acceptheader, "application/simple-message-summary") ? 0 : 1;
23948
			found_supported = strcmp(acceptheader, "application/simple-message-summary") ? 0 : 1;
23718
			if (!found_supported && (option_debug > 2)) {
23949
			if (!found_supported && (option_debug > 2)) {
23719
				ast_log(LOG_DEBUG, "Received SIP mailbox subscription for unknown format: %s\n", acceptheader);
23950
				ast_debug(1, "Received SIP mailbox subscription for unknown format: %s\n", acceptheader);
23720
			}
23951
			}
23721
			acceptheader = __get_header(req, "Accept", &start);
23952
			acceptheader = __get_header(req, "Accept", &start);
23722
		}
23953
		}
23723
		if (start && !found_supported) {
23954
		if (start && !found_supported) {
23724
			/* Format requested that we do not support */
23955
			/* Format requested that we do not support */
[+20] [20] 300 lines
[+20] [+] static int handle_incoming(struct sip_pvt *p, struct sip_request *req, struct ast_sockaddr *addr, int *recount, int *nounlock)
24025
		return 0;
24256
		return 0;
24026
	}
24257
	}
24027

    
   
24258

   
24028
	/* New SIP request coming in
24259
	/* New SIP request coming in
24029
	   (could be new request in existing SIP dialog as well...)
24260
	   (could be new request in existing SIP dialog as well...)
24030
	 */			
24261
	 */
24031
	

   
24032
	p->method = req->method;	/* Find out which SIP method they are using */
24262
	p->method = req->method;	/* Find out which SIP method they are using */
24033
	ast_debug(4, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
24263
	ast_debug(4, "**** Received %s (%d) - Command in SIP %s\n", sip_methods[p->method].text, sip_methods[p->method].id, cmd);
24034

    
   
24264

   
24035
	if (p->icseq && (p->icseq > seqno) ) {
24265
	if (p->icseq && (p->icseq > seqno) ) {
24036
		if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
24266
		if (p->pendinginvite && seqno == p->pendinginvite && (req->method == SIP_ACK || req->method == SIP_CANCEL)) {
[+20] [20] 384 lines
[+20] [+] static int handle_request_do(struct sip_request *req, struct ast_sockaddr *addr)
24421
	/*! \todo XXX if nounlock is nonzero we do not have the channel lock anymore.  handle_incoming() assumes that it is locked. */
24651
	/*! \todo XXX if nounlock is nonzero we do not have the channel lock anymore.  handle_incoming() assumes that it is locked. */
24422
	if (handle_incoming(p, req, addr, &recount, &nounlock) == -1) {
24652
	if (handle_incoming(p, req, addr, &recount, &nounlock) == -1) {
24423
		/* Request failed */
24653
		/* Request failed */
24424
		ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
24654
		ast_debug(1, "SIP message could not be handled, bad request: %-70.70s\n", p->callid[0] ? p->callid : "<no callid>");
24425
	}
24655
	}
24426
		
24656

   
24427
	if (recount)
24657
	if (recount)
24428
		ast_update_use_count();
24658
		ast_update_use_count();
24429

    
   
24659

   
24430
	if (p->owner && !nounlock)
24660
	if (p->owner && !nounlock)
24431
		ast_channel_unlock(p->owner);
24661
		ast_channel_unlock(p->owner);
[+20] [20] 939 lines
[+20] [+] static int sip_devicestate(void *data)
25371
	 * hosts out of memory.
25601
	 * hosts out of memory.
25372
	 */
25602
	 */
25373
	if ((p = find_peer(host, NULL, FALSE, FINDALLDEVICES, TRUE, 0))) {
25603
	if ((p = find_peer(host, NULL, FALSE, FINDALLDEVICES, TRUE, 0))) {
25374
		if (!(ast_sockaddr_isnull(&p->addr) && ast_sockaddr_isnull(&p->defaddr))) {
25604
		if (!(ast_sockaddr_isnull(&p->addr) && ast_sockaddr_isnull(&p->defaddr))) {
25375
			/* we have an address for the peer */
25605
			/* we have an address for the peer */
25376
		
25606

   
25377
			/* Check status in this order
25607
			/* Check status in this order
25378
				- Hold
25608
				- Hold
25379
				- Ringing
25609
				- Ringing
25380
				- Busy (enforced only by call limit)
25610
				- Busy (enforced only by call limit)
25381
				- Inuse (we have a call)
25611
				- Inuse (we have a call)
[+20] [20] 43 lines
[+20] static int sip_devicestate(void *data)
25425
 *		SIP/exten@host!dnid
25655
 *		SIP/exten@host!dnid
25426
 *	or	SIP/host/exten!dnid
25656
 *	or	SIP/host/exten!dnid
25427
 *	or	SIP/host!dnid
25657
 *	or	SIP/host!dnid
25428
 * \endverbatim
25658
 * \endverbatim
25429
*/
25659
*/
25430
static struct ast_channel *sip_request_call(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause)
25660
static struct ast_channel *sip_request_call(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
25431
{
25661
{
25432
	struct sip_pvt *p;
25662
	struct sip_pvt *p;
25433
	struct ast_channel *tmpc = NULL;
25663
	struct ast_channel *tmpc = NULL;
25434
	char *ext = NULL, *host;
25664
	char *ext = NULL, *host;
25435
	char tmp[256];
25665
	char tmp[256];

    
   
25666
	char tmp2[256];
25436
	char *dest = data;
25667
	char *dest = data;
25437
	char *dnid;
25668
	char *dnid;
25438
	char *secret = NULL;
25669
	char *secret = NULL;
25439
	char *md5secret = NULL;
25670
	char *md5secret = NULL;
25440
	char *authname = NULL;
25671
	char *authname = NULL;
25441
	char *trans = NULL;
25672
	char *trans = NULL;
25442
	char dialstring[256];
25673
	char dialstring[256];
25443
	char *remote_address;
25674
	char *remote_address;
25444
	enum sip_transport transport = 0;
25675
	enum sip_transport transport = 0;
25445
	struct ast_sockaddr remote_address_sa = { {0,} };
25676
	struct ast_sockaddr remote_address_sa = { {0,} };
25446
	format_t oldformat = format;

   
25447
	AST_DECLARE_APP_ARGS(args,
25677
	AST_DECLARE_APP_ARGS(args,
25448
		AST_APP_ARG(peerorhost);
25678
		AST_APP_ARG(peerorhost);
25449
		AST_APP_ARG(exten);
25679
		AST_APP_ARG(exten);
25450
		AST_APP_ARG(remote_address);
25680
		AST_APP_ARG(remote_address);
25451
	);
25681
	);
25452

    
   
25682

   
25453
	/* mask request with some set of allowed formats.
25683
	/* mask request with some set of allowed formats.
25454
	 * XXX this needs to be fixed.
25684
	 * XXX this needs to be fixed.
25455
	 * The original code uses AST_FORMAT_AUDIO_MASK, but it is
25685
	 * The original code uses AST_FORMAT_AUDIO_MASK, but it is
25456
	 * unclear what to use here. We have global_capabilities, which is
25686
	 * unclear what to use here. We have global_capabilities, which is
25457
	 * configured from sip.conf, and sip_tech.capabilities, which is
25687
	 * configured from sip.conf, and sip_tech.capabilities, which is
25458
	 * hardwired to all audio formats.
25688
	 * hardwired to all audio formats.
25459
	 */
25689
	 */
25460
	format &= AST_FORMAT_AUDIO_MASK;
25690
	if (!(ast_format_cap_has_type(cap, AST_FORMAT_TYPE_AUDIO))) {
25461
	if (!format) {
25691
		ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format %s while capability is %s\n",
25462
		ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format %s while capability is %s\n", ast_getformatname(oldformat), ast_getformatname(sip_cfg.capability));
25692
		ast_getformatname_multiple(tmp, sizeof(tmp), cap),

    
   
25693
		ast_getformatname_multiple(tmp2, sizeof(tmp2), sip_cfg.caps));
25463
		*cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;	/* Can't find codec to connect to host */
25694
		*cause = AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;	/* Can't find codec to connect to host */
25464
		return NULL;
25695
		return NULL;
25465
	}
25696
	}
25466
	ast_debug(1, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), oldformat));
25697
	ast_debug(1, "Asked to create a SIP channel with formats: %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), cap));

    
   
25698

   

    
   
25699
	if (ast_strlen_zero(dest)) {

    
   
25700
		ast_log(LOG_ERROR, "Unable to create channel with empty destination.\n");

    
   
25701
		*cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;

    
   
25702
		return NULL;

    
   
25703
	}
25467

    
   
25704

   
25468
	if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE, NULL))) {
25705
	if (!(p = sip_alloc(NULL, NULL, 0, SIP_INVITE, NULL))) {
25469
		ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", dest);
25706
		ast_log(LOG_ERROR, "Unable to build sip pvt data for '%s' (Out of memory or socket error)\n", dest);
25470
		*cause = AST_CAUSE_SWITCH_CONGESTION;
25707
		*cause = AST_CAUSE_SWITCH_CONGESTION;
25471
		return NULL;
25708
		return NULL;
[+20] [20] 105 lines
[+20] static struct ast_channel *sip_request_call(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause) [+] static struct ast_channel *sip_request_call(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
25577
	ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
25814
	ast_sip_ouraddrfor(&p->sa, &p->ourip, p);
25578
	build_via(p);
25815
	build_via(p);
25579
	ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
25816
	ao2_t_unlink(dialogs, p, "About to change the callid -- remove the old name");
25580
	build_callid_pvt(p);
25817
	build_callid_pvt(p);
25581
	ao2_t_link(dialogs, p, "Linking in under new name");
25818
	ao2_t_link(dialogs, p, "Linking in under new name");
25582
	
25819

   
25583
	/* We have an extension to call, don't use the full contact here */
25820
	/* We have an extension to call, don't use the full contact here */
25584
	/* This to enable dialing registered peers with extension dialling,
25821
	/* This to enable dialing registered peers with extension dialling,
25585
	   like SIP/peername/extension 	
25822
	   like SIP/peername/extension 	
25586
	   SIP/peername will still use the full contact
25823
	   SIP/peername will still use the full contact
25587
	 */
25824
	 */
[+20] [20] 10 lines
[+20] static struct ast_channel *sip_request_call(const char *type, format_t format, const struct ast_channel *requestor, void *data, int *cause) [+] static struct ast_channel *sip_request_call(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
25598
	if (authname && !ast_strlen_zero(authname))
25835
	if (authname && !ast_strlen_zero(authname))
25599
		ast_string_field_set(p, authname, authname);
25836
		ast_string_field_set(p, authname, authname);
25600
#if 0
25837
#if 0
25601
	printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
25838
	printf("Setting up to call extension '%s' at '%s'\n", ext ? ext : "<none>", host);
25602
#endif
25839
#endif
25603
	p->prefcodec = oldformat;				/* Format for this call */
25840
	ast_format_cap_append(p->prefcaps, cap);
25604
	p->jointcapability = oldformat & p->capability;
25841
	ast_format_cap_joint_copy(cap, p->caps, p->jointcaps);

    
   
25842

   
25605
	sip_pvt_lock(p);
25843
	sip_pvt_lock(p);
25606
	tmpc = sip_new(p, AST_STATE_DOWN, host, requestor ? requestor->linkedid : NULL);	/* Place the call */
25844
	tmpc = sip_new(p, AST_STATE_DOWN, host, requestor ? requestor->linkedid : NULL);	/* Place the call */
25607
	if (sip_cfg.callevents)
25845
	if (sip_cfg.callevents)
25608
		manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
25846
		manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate",
25609
			"Channel: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
25847
			"Channel: %s\r\nChanneltype: %s\r\nSIPcallid: %s\r\nSIPfullcontact: %s\r\nPeername: %s\r\n",
[+20] [20] 251 lines
[+20] [+] static int add_sip_domain(const char *domain, const enum domain_mode mode, const char *context)
25861

    
   
26099

   
25862
	AST_LIST_LOCK(&domain_list);
26100
	AST_LIST_LOCK(&domain_list);
25863
	AST_LIST_INSERT_TAIL(&domain_list, d, list);
26101
	AST_LIST_INSERT_TAIL(&domain_list, d, list);
25864
	AST_LIST_UNLOCK(&domain_list);
26102
	AST_LIST_UNLOCK(&domain_list);
25865

    
   
26103

   
25866
 	if (sipdebug)	
26104
	if (sipdebug)
25867
		ast_debug(1, "Added local SIP domain '%s'\n", domain);
26105
		ast_debug(1, "Added local SIP domain '%s'\n", domain);
25868

    
   
26106

   
25869
	return 1;
26107
	return 1;
25870
}
26108
}
25871

    
   
26109

   
[+20] [20] 8 lines
[+20] [+] static int check_sip_domain(const char *domain, char *context, size_t len)
25880
		if (strcasecmp(d->domain, domain))
26118
		if (strcasecmp(d->domain, domain))
25881
			continue;
26119
			continue;
25882

    
   
26120

   
25883
		if (len && !ast_strlen_zero(d->context))
26121
		if (len && !ast_strlen_zero(d->context))
25884
			ast_copy_string(context, d->context, len);
26122
			ast_copy_string(context, d->context, len);
25885
		
26123

   
25886
		result = 1;
26124
		result = 1;
25887
		break;
26125
		break;
25888
	}
26126
	}
25889
	AST_LIST_UNLOCK(&domain_list);
26127
	AST_LIST_UNLOCK(&domain_list);
25890

    
   
26128

   
[+20] [20] 134 lines
[+20] [+] static void set_peer_defaults(struct sip_peer *peer)
26025
	ast_string_field_set(peer, mohinterpret, default_mohinterpret);
26263
	ast_string_field_set(peer, mohinterpret, default_mohinterpret);
26026
	ast_string_field_set(peer, mohsuggest, default_mohsuggest);
26264
	ast_string_field_set(peer, mohsuggest, default_mohsuggest);
26027
	ast_string_field_set(peer, engine, default_engine);
26265
	ast_string_field_set(peer, engine, default_engine);
26028
	ast_sockaddr_setnull(&peer->addr);
26266
	ast_sockaddr_setnull(&peer->addr);
26029
	ast_sockaddr_setnull(&peer->defaddr);
26267
	ast_sockaddr_setnull(&peer->defaddr);
26030
	peer->capability = sip_cfg.capability;
26268
	ast_format_cap_copy(peer->caps, sip_cfg.caps);
26031
	peer->maxcallbitrate = default_maxcallbitrate;
26269
	peer->maxcallbitrate = default_maxcallbitrate;
26032
	peer->rtptimeout = global_rtptimeout;
26270
	peer->rtptimeout = global_rtptimeout;
26033
	peer->rtpholdtimeout = global_rtpholdtimeout;
26271
	peer->rtpholdtimeout = global_rtpholdtimeout;
26034
	peer->rtpkeepalive = global_rtpkeepalive;
26272
	peer->rtpkeepalive = global_rtpkeepalive;
26035
	peer->allowtransfer = sip_cfg.allowtransfer;
26273
	peer->allowtransfer = sip_cfg.allowtransfer;
[+20] [20] 38 lines
[+20] [+] static struct sip_peer *temp_peer(const char *name)
26074

    
   
26312

   
26075
	if (ast_string_field_init(peer, 512)) {
26313
	if (ast_string_field_init(peer, 512)) {
26076
		ao2_t_ref(peer, -1, "failed to string_field_init, drop peer");
26314
		ao2_t_ref(peer, -1, "failed to string_field_init, drop peer");
26077
		return NULL;
26315
		return NULL;
26078
	}
26316
	}

    
   
26317
	

    
   
26318
	if (!(peer->cc_params = ast_cc_config_params_init())) {

    
   
26319
		ao2_t_ref(peer, -1, "failed to allocate cc_params for peer");

    
   
26320
		return NULL;

    
   
26321
	}
26079

    
   
26322

   
26080
	ast_atomic_fetchadd_int(&apeerobjs, 1);
26323
	ast_atomic_fetchadd_int(&apeerobjs, 1);
26081
	set_peer_defaults(peer);
26324
	set_peer_defaults(peer);
26082

    
   
26325

   
26083
	ast_copy_string(peer->name, name, sizeof(peer->name));
26326
	ast_copy_string(peer->name, name, sizeof(peer->name));
[+20] [20] 25 lines
[+20] [+] static void add_peer_mailboxes(struct sip_peer *peer, const char *value)
26109

    
   
26352

   
26110
		/* Check whether the mailbox is already in the list */
26353
		/* Check whether the mailbox is already in the list */
26111
		AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
26354
		AST_LIST_TRAVERSE(&peer->mailboxes, mailbox, entry) {
26112
			if (!strcmp(mailbox->mailbox, mbox) && !strcmp(S_OR(mailbox->context, ""), S_OR(context, ""))) {
26355
			if (!strcmp(mailbox->mailbox, mbox) && !strcmp(S_OR(mailbox->context, ""), S_OR(context, ""))) {
26113
				duplicate = 1;
26356
				duplicate = 1;
26114
				mailbox->delme = 0;

   
26115
				break;
26357
				break;
26116
			}
26358
			}
26117
		}
26359
		}
26118
		if (duplicate) {
26360
		if (duplicate) {
26119
			continue;
26361
			continue;
[+20] [20] 48 lines
[+20] [+] static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only)
26168
		/* Already in the list, remove it and it will be added back (or FREE'd)  */
26410
		/* Already in the list, remove it and it will be added back (or FREE'd)  */
26169
		found++;
26411
		found++;
26170
		if (!(peer->the_mark))
26412
		if (!(peer->the_mark))
26171
			firstpass = 0;
26413
			firstpass = 0;
26172
	} else {
26414
	} else {
26173
		if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct")))
26415
		if (!(peer = ao2_t_alloc(sizeof(*peer), sip_destroy_peer_fn, "allocate a peer struct"))) {
26174
			return NULL;
26416
			return NULL;
26175

    
   
26417
		}

    
   
26418
		if (!(peer->caps = ast_format_cap_alloc_nolock())) {

    
   
26419
			ao2_t_ref(peer, -1, "failed to allocate format capabilities, drop peer");

    
   
26420
			return NULL;

    
   
26421
		}
26176
		if (ast_string_field_init(peer, 512)) {
26422
		if (ast_string_field_init(peer, 512)) {
26177
			ao2_t_ref(peer, -1, "failed to string_field_init, drop peer");
26423
			ao2_t_ref(peer, -1, "failed to string_field_init, drop peer");
26178
			return NULL;
26424
			return NULL;
26179
		}
26425
		}
26180

    
   
26426

   
[+20] [20] 280 lines
[+20] static struct sip_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime, int devstate_only)
26461
			} else if (!strcasecmp(v->name, "allowtransfer")) {
26707
			} else if (!strcasecmp(v->name, "allowtransfer")) {
26462
				peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
26708
				peer->allowtransfer = ast_true(v->value) ? TRANSFER_OPENFORALL : TRANSFER_CLOSED;
26463
			} else if (!strcasecmp(v->name, "pickupgroup")) {
26709
			} else if (!strcasecmp(v->name, "pickupgroup")) {
26464
				peer->pickupgroup = ast_get_group(v->value);
26710
				peer->pickupgroup = ast_get_group(v->value);
26465
			} else if (!strcasecmp(v->name, "allow")) {
26711
			} else if (!strcasecmp(v->name, "allow")) {
26466
				int error =  ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, TRUE);
26712
				int error = ast_parse_allow_disallow(&peer->prefs, peer->caps, v->value, TRUE);
26467
				if (error) {
26713
				if (error) {
26468
					ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
26714
					ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
26469
				}
26715
				}
26470
			} else if (!strcasecmp(v->name, "disallow")) {
26716
			} else if (!strcasecmp(v->name, "disallow")) {
26471
				int error =  ast_parse_allow_disallow(&peer->prefs, &peer->capability, v->value, FALSE);
26717
				int error =  ast_parse_allow_disallow(&peer->prefs, peer->caps, v->value, FALSE);
26472
				if (error) {
26718
				if (error) {
26473
					ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
26719
					ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
26474
				}
26720
				}
26475
			} else if (!strcasecmp(v->name, "preferred_codec_only")) {
26721
			} else if (!strcasecmp(v->name, "preferred_codec_only")) {
26476
				ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_PREFERRED_CODEC);
26722
				ast_set2_flag(&peer->flags[1], ast_true(v->value), SIP_PAGE2_PREFERRED_CODEC);
[+20] [20] 325 lines
[+20] [+] static int peer_markall_func(void *device, void *arg, int flags)
26802
	struct sip_peer *peer = device;
27048
	struct sip_peer *peer = device;
26803
	peer->the_mark = 1;
27049
	peer->the_mark = 1;
26804
	return 0;
27050
	return 0;
26805
}
27051
}
26806

    
   
27052

   

    
   
27053
/*!

    
   
27054
 * \internal

    
   
27055
 * \brief If no default formats are set in config, these are used 

    
   
27056
 */

    
   
27057
static void sip_set_default_format_capabilities(struct ast_format_cap *cap)

    
   
27058
{

    
   
27059
	struct ast_format tmp_fmt;

    
   
27060

   

    
   
27061
	ast_format_cap_remove_all(cap);

    
   
27062
	ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_ULAW, 0));

    
   
27063
	ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_TESTLAW, 0));

    
   
27064
	ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_ALAW, 0));

    
   
27065
	ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_GSM, 0));

    
   
27066
	ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_H263, 0));

    
   
27067
}

    
   
27068

   
26807
/*! \brief Re-read SIP.conf config file
27069
/*! \brief Re-read SIP.conf config file
26808
\note	This function reloads all config data, except for
27070
\note	This function reloads all config data, except for
26809
	active peers (with registrations). They will only
27071
	active peers (with registrations). They will only
26810
	change configuration data at restart, not at reload.
27072
	change configuration data at restart, not at reload.
26811
	SIP debug and recordhistory state will not change
27073
	SIP debug and recordhistory state will not change
[+20] [20] 145 lines
[+20] [+] static int reload_config(enum channelreloadreason reason)
26957
	externrefresh = 10;
27219
	externrefresh = 10;
26958

    
   
27220

   
26959
	/* Reset channel settings to default before re-configuring */
27221
	/* Reset channel settings to default before re-configuring */
26960
	sip_cfg.allow_external_domains = DEFAULT_ALLOW_EXT_DOM;				/* Allow external invites */
27222
	sip_cfg.allow_external_domains = DEFAULT_ALLOW_EXT_DOM;				/* Allow external invites */
26961
	sip_cfg.regcontext[0] = '\0';
27223
	sip_cfg.regcontext[0] = '\0';
26962
	sip_cfg.capability = DEFAULT_CAPABILITY;
27224
	sip_set_default_format_capabilities(sip_cfg.caps);
26963
	sip_cfg.regextenonqualify = DEFAULT_REGEXTENONQUALIFY;
27225
	sip_cfg.regextenonqualify = DEFAULT_REGEXTENONQUALIFY;
26964
	sip_cfg.notifyringing = DEFAULT_NOTIFYRINGING;
27226
	sip_cfg.notifyringing = DEFAULT_NOTIFYRINGING;
26965
	sip_cfg.notifycid = DEFAULT_NOTIFYCID;
27227
	sip_cfg.notifycid = DEFAULT_NOTIFYCID;
26966
	sip_cfg.notifyhold = FALSE;		/*!< Keep track of hold status for a peer */
27228
	sip_cfg.notifyhold = FALSE;		/*!< Keep track of hold status for a peer */
26967
	sip_cfg.directrtpsetup = FALSE;		/* Experimental feature, disabled by default */
27229
	sip_cfg.directrtpsetup = FALSE;		/* Experimental feature, disabled by default */
[+20] [20] 401 lines
[+20] static int reload_config(enum channelreloadreason reason)
27369
		} else if (!strcasecmp(v->name, "externtlsport")) {
27631
		} else if (!strcasecmp(v->name, "externtlsport")) {
27370
			if (!(externtlsport = port_str2int(v->value, STANDARD_TLS_PORT))) {
27632
			if (!(externtlsport = port_str2int(v->value, STANDARD_TLS_PORT))) {
27371
				ast_log(LOG_WARNING, "Invalid externtlsport value, must be a positive integer between 1 and 65535 at line %d\n", v->lineno);
27633
				ast_log(LOG_WARNING, "Invalid externtlsport value, must be a positive integer between 1 and 65535 at line %d\n", v->lineno);
27372
			}
27634
			}
27373
		} else if (!strcasecmp(v->name, "allow")) {
27635
		} else if (!strcasecmp(v->name, "allow")) {
27374
			int error =  ast_parse_allow_disallow(&default_prefs, &sip_cfg.capability, v->value, TRUE);
27636
			int error =  ast_parse_allow_disallow(&default_prefs, sip_cfg.caps, v->value, TRUE);
27375
			if (error) {
27637
			if (error) {
27376
				ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
27638
				ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
27377
			}
27639
			}
27378
		} else if (!strcasecmp(v->name, "disallow")) {
27640
		} else if (!strcasecmp(v->name, "disallow")) {
27379
			int error =  ast_parse_allow_disallow(&default_prefs, &sip_cfg.capability, v->value, FALSE);
27641
			int error =  ast_parse_allow_disallow(&default_prefs, sip_cfg.caps, v->value, FALSE);
27380
			if (error) {
27642
			if (error) {
27381
				ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
27643
				ast_log(LOG_WARNING, "Codec configuration errors found in line %d : %s = %s\n", v->lineno, v->name, v->value);
27382
			}
27644
			}
27383
		} else if (!strcasecmp(v->name, "preferred_codec_only")) {
27645
		} else if (!strcasecmp(v->name, "preferred_codec_only")) {
27384
			ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_PREFERRED_CODEC);
27646
			ast_set2_flag(&global_flags[1], ast_true(v->value), SIP_PAGE2_PREFERRED_CODEC);
[+20] [20] 518 lines
[+20] [+] static struct ast_udptl *sip_get_udptl_peer(struct ast_channel *chan)
27903
}
28165
}
27904

    
   
28166

   
27905
static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
28167
static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
27906
{
28168
{
27907
	struct sip_pvt *p;
28169
	struct sip_pvt *p;
27908
	
28170

   
27909
	p = chan->tech_pvt;
28171
	p = chan->tech_pvt;
27910
	if (!p) {
28172
	if (!p) {
27911
		return -1;
28173
		return -1;
27912
	}
28174
	}

    
   
28175
	/*

    
   
28176
	 * Lock both the pvt and it's owner safely.

    
   
28177
	 */
27913
	sip_pvt_lock(p);
28178
	sip_pvt_lock(p);

    
   
28179
	while (p->owner && ast_channel_trylock(p->owner)) {

    
   
28180
		sip_pvt_unlock(p);

    
   
28181
		usleep(1);

    
   
28182
		sip_pvt_lock(p);

    
   
28183
	}

    
   
28184

   

    
   
28185
	if (!p->owner) {

    
   
28186
		sip_pvt_unlock(p);
Moved from 20547

    
   
28187
		return 0;
Moved from 20548

    
   
28188
	}
27914
	if (udptl) {
28189
	if (udptl) {
27915
		ast_udptl_get_peer(udptl, &p->udptlredirip);
28190
		ast_udptl_get_peer(udptl, &p->udptlredirip);
27916
	} else {
28191
	} else {
27917
		memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
28192
		memset(&p->udptlredirip, 0, sizeof(p->udptlredirip));
27918
	}
28193
	}
[+20] [20] 8 lines
[+20] static int sip_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl)
27927
			ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
28202
			ast_set_flag(&p->flags[0], SIP_NEEDREINVITE);
27928
		}
28203
		}
27929
	}
28204
	}
27930
	/* Reset lastrtprx timer */
28205
	/* Reset lastrtprx timer */
27931
	p->lastrtprx = p->lastrtptx = time(NULL);
28206
	p->lastrtprx = p->lastrtptx = time(NULL);

    
   
28207
	ast_channel_unlock(p->owner);
27932
	sip_pvt_unlock(p);
28208
	sip_pvt_unlock(p);
27933
	return 0;
28209
	return 0;
27934
}
28210
}
27935

    
   
28211

   
27936
static enum ast_rtp_glue_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
28212
static enum ast_rtp_glue_result sip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
[+20] [20] 92 lines
[+20] [+] static enum ast_rtp_glue_result sip_get_trtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
28029
	sip_pvt_unlock(p);
28305
	sip_pvt_unlock(p);
28030

    
   
28306

   
28031
	return res;
28307
	return res;
28032
}
28308
}
28033

    
   
28309

   
28034
static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, format_t codecs, int nat_active)
28310
static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, const struct ast_format_cap *cap, int nat_active)
28035
{
28311
{
28036
	struct sip_pvt *p;
28312
	struct sip_pvt *p;
28037
	int changed = 0;
28313
	int changed = 0;
28038

    
   
28314

   
28039
	p = chan->tech_pvt;
28315
	p = chan->tech_pvt;
[+20] [20] 4 lines
[+20] static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, format_t codecs, int nat_active) [+] static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_rtp_instance *vinstance, struct ast_rtp_instance *tinstance, const struct ast_format_cap *cap, int nat_active)
28044
	/* Disable early RTP bridge  */
28320
	/* Disable early RTP bridge  */
28045
	if (!ast_bridged_channel(chan) && !sip_cfg.directrtpsetup) { 	/* We are in early state */
28321
	if (!ast_bridged_channel(chan) && !sip_cfg