diff options
34 files changed, 1080 insertions, 339 deletions
@@ -41,6 +41,22 @@ app_voicemail * Added 'fromstring' field to the voicemail boxes. If set, it will override the global 'fromstring' field on a per-mailbox basis. +res_pjsip +------------------ + * A new transport parameter 'symmetric_transport' has been added. + When a request from a dynamic contact comes in on a transport with this + option set to 'yes', the transport name will be saved and used for + subsequent outgoing requests like OPTIONS, NOTIFY and INVITE. It's + saved as a contact uri parameter named 'x-ast-txp' and will display with + the contact uri in CLI, AMI, and ARI output. On the outgoing request, + if a transport wasn't explicitly set on the endpoint AND the request URI + is not a hostname, the saved transport will be used and the 'x-ast-txp' + parameter stripped from the outgoing packet. To facilitate recreation of + subscriptions on asterisk restart, a new column 'contact_uri' needed to be + added to the ps_subcsription_persistence table. Since new columns were + added to both transport and subscription_persistence, an alembic upgrade + should be run to bring the database tables up to date. + res_pjsip_transport_websocket ------------------ * Removed non-secure websocket support. Firefox and Chrome have not allowed @@ -63,6 +79,13 @@ res_pjsip_endpoint_identifier_ip appropriate, as it now matches inbound requests on more than just IP address. +res_rtp_asterisk +----------------- + * The RTP layer of Asterisk now has support for RFC 5761: "Multiplexing RTP + Data and Control Packets on a Single Port." So far, the only channel driver + that supports this feature is chan_pjsip. You can set "rtcp_mux = yes" on + a PJSIP endpoint in pjsip.conf to enable the feature. + ------------------------------------------------------------------------------ --- Functionality changes from Asterisk 13.13.0 to Asterisk 13.14.0 ---------- ------------------------------------------------------------------------------ diff --git a/UPGRADE.txt b/UPGRADE.txt index 4f40b2b9d..63a1885d5 100644 --- a/UPGRADE.txt +++ b/UPGRADE.txt @@ -21,6 +21,15 @@ === UPGRADE-12.txt -- Upgrade info for 11 to 12 =========================================================== +From 13.14.0 to 13.15.0: + +res_rtp_asterisk: + - The RTP layer of Asterisk now has support for RFC 5761: "Multiplexing RTP + Data and Control Packets on a Single Port." For the PJSIP channel driver, + chan_pjsip, you can set "rtcp_mux = yes" on a PJSIP endpoint in pjsip.conf + to enable the feature. For chan_sip you can set "rtcp_mux = yes" either + globally or on a per-peer basis in sip.conf. + From 13.8.0 to 13.9.0: res_parking: diff --git a/apps/app_queue.c b/apps/app_queue.c index f7cee6a76..ddb62d2e0 100644 --- a/apps/app_queue.c +++ b/apps/app_queue.c @@ -5884,6 +5884,7 @@ static void handle_bridge_enter(void *userdata, struct stasis_subscription *sub, { struct queue_stasis_data *queue_data = userdata; struct ast_bridge_blob *enter_blob = stasis_message_data(msg); + SCOPED_AO2LOCK(lock, queue_data); if (queue_data->dying) { return; @@ -5902,6 +5903,67 @@ static void handle_bridge_enter(void *userdata, struct stasis_subscription *sub, } /*! + * \internal + * \brief Handle a stasis bridge leave event. + * + * We track this event to determine if the caller has left the bridge + * as the result of a redirect. Transfers and hangups are handled in + * separate functions. + * + * \param userdata Data pertaining to the particular call in the queue. + * \param sub The stasis subscription on which the message occurred. + * \param msg The stasis message for the bridge leave event + */ +static void handle_bridge_left(void *userdata, struct stasis_subscription *sub, + struct stasis_message *msg) +{ + struct queue_stasis_data *queue_data = userdata; + struct ast_bridge_blob *left_blob = stasis_message_data(msg); + struct ast_channel_snapshot *caller_snapshot, *member_snapshot; + + ao2_lock(queue_data); + + if (queue_data->dying) { + ao2_unlock(queue_data); + return; + } + + if (ast_strlen_zero(queue_data->bridge_uniqueid)) { + ao2_unlock(queue_data); + return; + } + + /* Correct channel, correct bridge? */ + if (strcmp(left_blob->channel->uniqueid, queue_data->caller_uniqueid) + || strcmp(left_blob->bridge->uniqueid, queue_data->bridge_uniqueid)) { + ao2_unlock(queue_data); + return; + } + + caller_snapshot = ast_channel_snapshot_get_latest(queue_data->caller_uniqueid); + member_snapshot = ast_channel_snapshot_get_latest(queue_data->member_uniqueid); + + ao2_unlock(queue_data); + + ast_debug(3, "Detected redirect of queue caller channel %s\n", + caller_snapshot->name); + + ast_queue_log(queue_data->queue->name, caller_snapshot->uniqueid, queue_data->member->membername, + "COMPLETECALLER", "%ld|%ld|%d", + (long) (queue_data->starttime - queue_data->holdstart), + (long) (time(NULL) - queue_data->starttime), queue_data->caller_pos); + + send_agent_complete(queue_data->queue->name, caller_snapshot, member_snapshot, queue_data->member, + queue_data->holdstart, queue_data->starttime, CALLER); + update_queue(queue_data->queue, queue_data->member, queue_data->callcompletedinsl, + time(NULL) - queue_data->starttime); + remove_stasis_subscriptions(queue_data); + + ao2_cleanup(member_snapshot); + ao2_cleanup(caller_snapshot); +} + +/*! * \brief Handle a blind transfer event * * This event is important in order to be able to log the end of the @@ -5922,16 +5984,17 @@ static void handle_blind_transfer(void *userdata, struct stasis_subscription *su RAII_VAR(struct ast_channel_snapshot *, caller_snapshot, NULL, ao2_cleanup); RAII_VAR(struct ast_channel_snapshot *, member_snapshot, NULL, ao2_cleanup); - if (queue_data->dying) { - return; - } - if (transfer_msg->result != AST_BRIDGE_TRANSFER_SUCCESS) { return; } ao2_lock(queue_data); + if (queue_data->dying) { + ao2_unlock(queue_data); + return; + } + if (ast_strlen_zero(queue_data->bridge_uniqueid) || strcmp(queue_data->bridge_uniqueid, transfer_msg->bridge->uniqueid)) { ao2_unlock(queue_data); @@ -5979,10 +6042,6 @@ static void handle_attended_transfer(void *userdata, struct stasis_subscription RAII_VAR(struct ast_channel_snapshot *, caller_snapshot, NULL, ao2_cleanup); RAII_VAR(struct ast_channel_snapshot *, member_snapshot, NULL, ao2_cleanup); - if (queue_data->dying) { - return; - } - if (atxfer_msg->result != AST_BRIDGE_TRANSFER_SUCCESS || atxfer_msg->dest_type == AST_ATTENDED_TRANSFER_DEST_THREEWAY) { return; @@ -5990,6 +6049,11 @@ static void handle_attended_transfer(void *userdata, struct stasis_subscription ao2_lock(queue_data); + if (queue_data->dying) { + ao2_unlock(queue_data); + return; + } + if (ast_strlen_zero(queue_data->bridge_uniqueid)) { ao2_unlock(queue_data); return; @@ -6173,12 +6237,13 @@ static void handle_hangup(void *userdata, struct stasis_subscription *sub, RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup); enum agent_complete_reason reason; + ao2_lock(queue_data); + if (queue_data->dying) { + ao2_unlock(queue_data); return; } - ao2_lock(queue_data); - if (!strcmp(channel_blob->snapshot->uniqueid, queue_data->caller_uniqueid)) { reason = CALLER; } else if (!strcmp(channel_blob->snapshot->uniqueid, queue_data->member_uniqueid)) { @@ -6207,7 +6272,7 @@ static void handle_hangup(void *userdata, struct stasis_subscription *sub, ast_debug(3, "Detected hangup of queue %s channel %s\n", reason == CALLER ? "caller" : "member", channel_blob->snapshot->name); - ast_queue_log(queue_data->queue->name, queue_data->caller_uniqueid, queue_data->member->membername, + ast_queue_log(queue_data->queue->name, caller_snapshot->uniqueid, queue_data->member->membername, reason == CALLER ? "COMPLETECALLER" : "COMPLETEAGENT", "%ld|%ld|%d", (long) (queue_data->starttime - queue_data->holdstart), (long) (time(NULL) - queue_data->starttime), queue_data->caller_pos); @@ -6268,6 +6333,8 @@ static int setup_stasis_subs(struct queue_ent *qe, struct ast_channel *peer, str stasis_message_router_add(queue_data->bridge_router, ast_channel_entered_bridge_type(), handle_bridge_enter, queue_data); + stasis_message_router_add(queue_data->bridge_router, ast_channel_left_bridge_type(), + handle_bridge_left, queue_data); stasis_message_router_add(queue_data->bridge_router, ast_blind_transfer_type(), handle_blind_transfer, queue_data); stasis_message_router_add(queue_data->bridge_router, ast_attended_transfer_type(), diff --git a/apps/confbridge/confbridge_manager.c b/apps/confbridge/confbridge_manager.c index eb1b58e15..bca854ed9 100644 --- a/apps/confbridge/confbridge_manager.c +++ b/apps/confbridge/confbridge_manager.c @@ -191,7 +191,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") </managerEvent> <managerEvent language="en_US" name="ConfbridgeTalking"> <managerEventInstance class="EVENT_FLAG_CALL"> - <synopsis>Raised when a confbridge participant unmutes.</synopsis> + <synopsis>Raised when a confbridge participant begins or ends talking.</synopsis> <syntax> <parameter name="Conference"> <para>The name of the Confbridge conference.</para> diff --git a/bridges/bridge_softmix.c b/bridges/bridge_softmix.c index 436fab7af..486330af0 100644 --- a/bridges/bridge_softmix.c +++ b/bridges/bridge_softmix.c @@ -306,7 +306,8 @@ static void softmix_process_write_audio(struct softmix_translate_helper *trans_h if (entry->trans_pvt && !entry->out_frame) { entry->out_frame = ast_translate(entry->trans_pvt, &sc->write_frame, 0); } - if (entry->out_frame && (entry->out_frame->datalen < MAX_DATALEN)) { + if (entry->out_frame && entry->out_frame->frametype == AST_FRAME_VOICE + && entry->out_frame->datalen < MAX_DATALEN) { ao2_replace(sc->write_frame.subclass.format, entry->out_frame->subclass.format); memcpy(sc->final_buf, entry->out_frame->data.ptr, entry->out_frame->datalen); sc->write_frame.datalen = entry->out_frame->datalen; diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index db5e4fa4d..e8c47ce9f 100644 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -12928,7 +12928,13 @@ static struct iax2_peer *build_peer(const char *name, struct ast_variable *v, st /* Non-dynamic. Make sure we become that way if we're not */ AST_SCHED_DEL(sched, peer->expire); ast_clear_flag64(peer, IAX_DYNAMIC); - peer->addr.ss.ss_family = AST_AF_UNSPEC; + if (peer->dnsmgr) { + // Make sure we refresh dnsmgr if we're using it + ast_dnsmgr_refresh(peer->dnsmgr); + } else { + // Or just invalidate the address + peer->addr.ss.ss_family = AST_AF_UNSPEC; + } if (ast_dnsmgr_lookup(v->value, &peer->addr, &peer->dnsmgr, srvlookup ? "_iax._udp" : NULL)) { return peer_unref(peer); } diff --git a/channels/chan_sip.c b/channels/chan_sip.c index 85796a073..a67ad634c 100644 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -1211,6 +1211,7 @@ static int process_sdp_o(const char *o, struct sip_pvt *p); static int process_sdp_c(const char *c, struct ast_sockaddr *addr); static int process_sdp_a_sendonly(const char *a, int *sendonly); static int process_sdp_a_ice(const char *a, struct sip_pvt *p, struct ast_rtp_instance *instance); +static int process_sdp_a_rtcp_mux(const char *a, struct sip_pvt *p, int *requested); static int process_sdp_a_dtls(const char *a, struct sip_pvt *p, struct ast_rtp_instance *instance); static int process_sdp_a_audio(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newaudiortp, int *last_rtpmap_codec); static int process_sdp_a_video(const char *a, struct sip_pvt *p, struct ast_rtp_codecs *newvideortp, int *last_rtpmap_codec); @@ -6008,7 +6009,7 @@ static int dialog_initialize_rtp(struct sip_pvt *dialog) ast_rtp_instance_set_hold_timeout(dialog->vrtp, dialog->rtpholdtimeout); ast_rtp_instance_set_keepalive(dialog->vrtp, dialog->rtpkeepalive); - ast_rtp_instance_set_prop(dialog->vrtp, AST_RTP_PROPERTY_RTCP, 1); + ast_rtp_instance_set_prop(dialog->vrtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_STANDARD); ast_rtp_instance_set_qos(dialog->vrtp, global_tos_video, global_cos_video, "SIP VIDEO"); } @@ -6028,14 +6029,14 @@ static int dialog_initialize_rtp(struct sip_pvt *dialog) /* Do not timeout text as its not constant*/ ast_rtp_instance_set_keepalive(dialog->trtp, dialog->rtpkeepalive); - ast_rtp_instance_set_prop(dialog->trtp, AST_RTP_PROPERTY_RTCP, 1); + ast_rtp_instance_set_prop(dialog->trtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_STANDARD); } ast_rtp_instance_set_timeout(dialog->rtp, dialog->rtptimeout); ast_rtp_instance_set_hold_timeout(dialog->rtp, dialog->rtpholdtimeout); ast_rtp_instance_set_keepalive(dialog->rtp, dialog->rtpkeepalive); - ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_RTCP, 1); + ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_STANDARD); ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF, ast_test_flag(&dialog->flags[0], SIP_DTMF) == SIP_DTMF_RFC2833); ast_rtp_instance_set_prop(dialog->rtp, AST_RTP_PROPERTY_DTMF_COMPENSATE, ast_test_flag(&dialog->flags[1], SIP_PAGE2_RFC2833_COMPENSATE)); @@ -7754,6 +7755,15 @@ static int interpret_t38_parameters(struct sip_pvt *p, const struct ast_control_ return res; } +enum sip_media_fds { + SIP_AUDIO_RTP_FD, + SIP_AUDIO_RTCP_FD, + SIP_VIDEO_RTP_FD, + SIP_VIDEO_RTCP_FD, + SIP_TEXT_RTP_FD, + SIP_UDPTL_FD, +}; + /*! * \internal * \brief Create and initialize UDPTL for the specified dialog @@ -7782,7 +7792,7 @@ static int initialize_udptl(struct sip_pvt *p) /* T38 can be supported by this dialog, create it and set the derived properties */ if ((p->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, &bindaddr))) { if (p->owner) { - ast_channel_set_fd(p->owner, 5, ast_udptl_fd(p->udptl)); + ast_channel_set_fd(p->owner, SIP_UDPTL_FD, ast_udptl_fd(p->udptl)); } ast_udptl_setqos(p->udptl, global_tos_audio, global_cos_audio); @@ -8208,20 +8218,28 @@ static struct ast_channel *sip_new(struct sip_pvt *i, int state, const char *tit * UDPTL is created as needed in the lifetime of a dialog, its file * descriptor is set in initialize_udptl */ if (i->rtp) { - ast_channel_set_fd(tmp, 0, ast_rtp_instance_fd(i->rtp, 0)); - ast_channel_set_fd(tmp, 1, ast_rtp_instance_fd(i->rtp, 1)); + ast_channel_set_fd(tmp, SIP_AUDIO_RTP_FD, ast_rtp_instance_fd(i->rtp, 0)); + if (ast_test_flag(&i->flags[2], SIP_PAGE3_RTCP_MUX)) { + ast_channel_set_fd(tmp, SIP_AUDIO_RTCP_FD, -1); + } else { + ast_channel_set_fd(tmp, SIP_AUDIO_RTCP_FD, ast_rtp_instance_fd(i->rtp, 1)); + } ast_rtp_instance_set_write_format(i->rtp, fmt); ast_rtp_instance_set_read_format(i->rtp, fmt); } if (needvideo && i->vrtp) { - ast_channel_set_fd(tmp, 2, ast_rtp_instance_fd(i->vrtp, 0)); - ast_channel_set_fd(tmp, 3, ast_rtp_instance_fd(i->vrtp, 1)); + ast_channel_set_fd(tmp, SIP_VIDEO_RTP_FD, ast_rtp_instance_fd(i->vrtp, 0)); + if (ast_test_flag(&i->flags[2], SIP_PAGE3_RTCP_MUX)) { + ast_channel_set_fd(tmp, SIP_VIDEO_RTCP_FD, -1); + } else { + ast_channel_set_fd(tmp, SIP_VIDEO_RTCP_FD, ast_rtp_instance_fd(i->vrtp, 1)); + } } if (needtext && i->trtp) { - ast_channel_set_fd(tmp, 4, ast_rtp_instance_fd(i->trtp, 0)); + ast_channel_set_fd(tmp, SIP_TEXT_RTP_FD, ast_rtp_instance_fd(i->trtp, 0)); } if (i->udptl) { - ast_channel_set_fd(tmp, 5, ast_udptl_fd(i->udptl)); + ast_channel_set_fd(tmp, SIP_UDPTL_FD, ast_udptl_fd(i->udptl)); } if (state == AST_STATE_RING) { @@ -10090,6 +10108,42 @@ static int has_media_stream(struct sip_pvt *p, enum media_type m) return 0; } +static void configure_rtcp(struct sip_pvt *p, struct ast_rtp_instance *instance, int which, int remote_rtcp_mux) +{ + int local_rtcp_mux = ast_test_flag(&p->flags[2], SIP_PAGE3_RTCP_MUX); + int fd = -1; + + if (local_rtcp_mux && remote_rtcp_mux) { + ast_rtp_instance_set_prop(instance, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_MUX); + } else { + ast_rtp_instance_set_prop(instance, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_STANDARD); + fd = ast_rtp_instance_fd(instance, 1); + } + + if (p->owner) { + ast_channel_set_fd(p->owner, which, fd); + } +} + +static void set_ice_components(struct sip_pvt *p, struct ast_rtp_instance *instance, int remote_rtcp_mux) +{ + struct ast_rtp_engine_ice *ice; + int local_rtcp_mux = ast_test_flag(&p->flags[2], SIP_PAGE3_RTCP_MUX); + + ice = ast_rtp_instance_get_ice(instance); + if (!ice) { + return; + } + + if (local_rtcp_mux && remote_rtcp_mux) { + /* We both support RTCP mux. Only one ICE component necessary */ + ice->change_components(instance, 1); + } else { + /* They either don't support RTCP mux or we don't know if they do yet. */ + ice->change_components(instance, 2); + } +} + /*! \brief Process SIP SDP offer, select formats and activate media channels If offer is rejected, we will not change any properties of the call Return 0 on success, a negative value on errors. @@ -10148,6 +10202,10 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action int secure_audio = FALSE; int secure_video = FALSE; + /* RTCP Multiplexing */ + int remote_rtcp_mux_audio = FALSE; + int remote_rtcp_mux_video = FALSE; + /* Others */ int sendonly = -1; unsigned int numberofports; @@ -10674,6 +10732,8 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action processed = TRUE; } else if (process_sdp_a_audio(value, p, &newaudiortp, &last_rtpmap_codec)) { processed = TRUE; + } else if (process_sdp_a_rtcp_mux(value, p, &remote_rtcp_mux_audio)) { + processed = TRUE; } } /* Video specific scanning */ @@ -10691,6 +10751,8 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action processed = TRUE; } else if (process_sdp_a_video(value, p, &newvideortp, &last_rtpmap_codec)) { processed = TRUE; + } else if (process_sdp_a_rtcp_mux(value, p, &remote_rtcp_mux_video)) { + processed = TRUE; } } /* Text (T.140) specific scanning */ @@ -10855,6 +10917,7 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action if (sa && portno > 0) { /* Start ICE negotiation here, only when it is response, and setting that we are conrolling agent, as we are offerer */ + set_ice_components(p, p->rtp, remote_rtcp_mux_audio); if (req->method == SIP_RESPONSE) { start_ice(p->rtp, 1); } @@ -10868,11 +10931,7 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action ast_rtp_codecs_payloads_copy(&newaudiortp, ast_rtp_instance_get_codecs(p->rtp), p->rtp); /* Ensure RTCP is enabled since it may be inactive if we're coming back from a T.38 session */ - ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 1); - /* Ensure audio RTCP reads are enabled */ - if (p->owner) { - ast_channel_set_fd(p->owner, 1, ast_rtp_instance_fd(p->rtp, 1)); - } + configure_rtcp(p, p->rtp, SIP_AUDIO_RTCP_FD, remote_rtcp_mux_audio); if (ast_test_flag(&p->flags[0], SIP_DTMF) == SIP_DTMF_AUTO) { ast_clear_flag(&p->flags[0], SIP_DTMF); @@ -10895,10 +10954,10 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action /* Prevent audio RTCP reads */ if (p->owner) { - ast_channel_set_fd(p->owner, 1, -1); + ast_channel_set_fd(p->owner, SIP_AUDIO_RTCP_FD, -1); } /* Silence RTCP while audio RTP is inactive */ - ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 0); + ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_DISABLED); } else { ast_rtp_instance_stop(p->rtp); if (debug) @@ -10909,6 +10968,7 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action /* Setup video address and port */ if (p->vrtp) { if (vsa && vportno > 0) { + set_ice_components(p, p->vrtp, remote_rtcp_mux_video); start_ice(p->vrtp, (req->method != SIP_RESPONSE) ? 0 : 1); ast_sockaddr_set_port(vsa, vportno); ast_rtp_instance_set_remote_address(p->vrtp, vsa); @@ -10917,6 +10977,7 @@ static int process_sdp(struct sip_pvt *p, struct sip_request *req, int t38action ast_sockaddr_stringify(vsa)); } ast_rtp_codecs_payloads_copy(&newvideortp, ast_rtp_instance_get_codecs(p->vrtp), p->vrtp); + configure_rtcp(p, p->vrtp, SIP_VIDEO_RTCP_FD, remote_rtcp_mux_video); } else { ast_rtp_instance_stop(p->vrtp); if (debug) @@ -11263,6 +11324,18 @@ static int process_sdp_a_ice(const char *a, struct sip_pvt *p, struct ast_rtp_in return found; } +static int process_sdp_a_rtcp_mux(const char *a, struct sip_pvt *p, int *requested) +{ + int found = FALSE; + + if (!strncasecmp(a, "rtcp-mux", 8)) { + *requested = TRUE; + found = TRUE; + } + + return found; +} + static int process_sdp_a_dtls(const char *a, struct sip_pvt *p, struct ast_rtp_instance *instance) { struct ast_rtp_engine_dtls *dtls; @@ -13617,6 +13690,12 @@ static enum sip_result add_sdp(struct sip_request *resp, struct sip_pvt *p, int add_dtls_to_sdp(p->rtp, &a_audio); } + + /* If we've got rtcp-mux enabled, just unconditionally offer it in all SDPs */ + if (ast_test_flag(&p->flags[2], SIP_PAGE3_RTCP_MUX)) { + ast_str_append(&a_audio, 0, "a=rtcp-mux\r\n"); + ast_str_append(&a_video, 0, "a=rtcp-mux\r\n"); + } } if (add_t38) { @@ -13984,18 +14063,18 @@ static int transmit_reinvite_with_sdp(struct sip_pvt *p, int t38version, int old if (p->rtp) { if (t38version) { /* Silence RTCP while audio RTP is inactive */ - ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 0); + ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_DISABLED); if (p->owner) { /* Prevent audio RTCP reads */ - ast_channel_set_fd(p->owner, 1, -1); + ast_channel_set_fd(p->owner, SIP_AUDIO_RTCP_FD, -1); } } else if (ast_sockaddr_isnull(&p->redirip)) { /* Enable RTCP since it will be inactive if we're coming back * with this reinvite */ - ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 1); + ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_STANDARD); if (p->owner) { /* Enable audio RTCP reads */ - ast_channel_set_fd(p->owner, 1, ast_rtp_instance_fd(p->rtp, 1)); + ast_channel_set_fd(p->owner, SIP_AUDIO_RTCP_FD, ast_rtp_instance_fd(p->rtp, 1)); } } } @@ -20963,6 +21042,7 @@ static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct ast_cli(fd, " Parkinglot : %s\n", peer->parkinglot); ast_cli(fd, " Use Reason : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_Q850_REASON))); ast_cli(fd, " Encryption : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP))); + ast_cli(fd, " RTCP Mux : %s\n", AST_CLI_YESNO(ast_test_flag(&peer->flags[2], SIP_PAGE3_RTCP_MUX))); ast_cli(fd, "\n"); peer = sip_unref_peer(peer, "sip_show_peer: sip_unref_peer: done with peer ptr"); } else if (peer && type == 1) { /* manager listing */ @@ -21033,6 +21113,7 @@ static char *_sip_show_peer(int type, int fd, struct mansession *s, const struct astman_append(s, "SIP-Sess-Min: %d\r\n", peer->stimer.st_min_se); astman_append(s, "SIP-RTP-Engine: %s\r\n", peer->engine); astman_append(s, "SIP-Encryption: %s\r\n", ast_test_flag(&peer->flags[1], SIP_PAGE2_USE_SRTP) ? "Y" : "N"); + astman_append(s, "SIP-RTCP-Mux: %s\r\n", ast_test_flag(&peer->flags[2], SIP_PAGE3_RTCP_MUX) ? "Y" : "N"); /* - is enumerated */ astman_append(s, "SIP-DTMFmode: %s\r\n", dtmfmode2str(ast_test_flag(&peer->flags[0], SIP_DTMF))); @@ -21657,6 +21738,7 @@ static char *sip_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_ ast_cli(a->fd, " MOH Interpret: %s\n", default_mohinterpret); ast_cli(a->fd, " MOH Suggest: %s\n", default_mohsuggest); ast_cli(a->fd, " Voice Mail Extension: %s\n", default_vmexten); + ast_cli(a->fd, " RTCP Multiplexing: %s\n", AST_CLI_YESNO(ast_test_flag(&global_flags[2], SIP_PAGE3_RTCP_MUX))); if (realtimepeers || realtimeregs) { @@ -30710,6 +30792,9 @@ static int handle_common_options(struct ast_flags *flags, struct ast_flags *mask } else if (!strcasecmp(v->name, "buggymwi")) { ast_set_flag(&mask[1], SIP_PAGE2_BUGGY_MWI); ast_set2_flag(&flags[1], ast_true(v->value), SIP_PAGE2_BUGGY_MWI); + } else if (!strcasecmp(v->name, "rtcp_mux")) { + ast_set_flag(&mask[2], SIP_PAGE3_RTCP_MUX); + ast_set2_flag(&flags[2], ast_true(v->value), SIP_PAGE3_RTCP_MUX); } else res = 0; @@ -33311,9 +33396,9 @@ static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *i if (p->rtp) { /* Prevent audio RTCP reads */ - ast_channel_set_fd(chan, 1, -1); + ast_channel_set_fd(chan, SIP_AUDIO_RTCP_FD, -1); /* Silence RTCP while audio RTP is inactive */ - ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, 0); + ast_rtp_instance_set_prop(p->rtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_DISABLED); } } else if (!ast_sockaddr_isnull(&p->redirip)) { memset(&p->redirip, 0, sizeof(p->redirip)); @@ -33325,9 +33410,9 @@ static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *i if (p->vrtp) { /* Prevent video RTCP reads */ - ast_channel_set_fd(chan, 3, -1); + ast_channel_set_fd(chan, SIP_VIDEO_RTCP_FD, -1); /* Silence RTCP while video RTP is inactive */ - ast_rtp_instance_set_prop(p->vrtp, AST_RTP_PROPERTY_RTCP, 0); + ast_rtp_instance_set_prop(p->vrtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_DISABLED); } } else if (!ast_sockaddr_isnull(&p->vredirip)) { memset(&p->vredirip, 0, sizeof(p->vredirip)); @@ -33336,9 +33421,9 @@ static int sip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *i if (p->vrtp) { /* Enable RTCP since it will be inactive if we're coming back * from a reinvite */ - ast_rtp_instance_set_prop(p->vrtp, AST_RTP_PROPERTY_RTCP, 1); + ast_rtp_instance_set_prop(p->vrtp, AST_RTP_PROPERTY_RTCP, AST_RTP_INSTANCE_RTCP_STANDARD); /* Enable video RTCP reads */ - ast_channel_set_fd(chan, 3, ast_rtp_instance_fd(p->vrtp, 1)); + ast_channel_set_fd(chan, SIP_VIDEO_RTCP_FD, ast_rtp_instance_fd(p->vrtp, 1)); } } diff --git a/channels/chan_skinny.c b/channels/chan_skinny.c index 9cde77540..9c84eec30 100644 --- a/channels/chan_skinny.c +++ b/channels/chan_skinny.c @@ -7642,7 +7642,6 @@ static void *accept_thread(void *ignore) struct sockaddr_in sin; socklen_t sinlen; struct skinnysession *s; - struct protoent *p; int arg = 1; for (;;) { @@ -7659,12 +7658,10 @@ static void *accept_thread(void *ignore) continue; } - p = getprotobyname("tcp"); - if(p) { - if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) { - ast_log(LOG_WARNING, "Failed to set Skinny tcp connection to TCP_NODELAY mode: %s\n", strerror(errno)); - } + if (setsockopt(as, IPPROTO_TCP, TCP_NODELAY, (char *) &arg, sizeof(arg)) < 0) { + ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on Skinny TCP connection: %s\n", strerror(errno)); } + if (!(s = ast_calloc(1, sizeof(struct skinnysession)))) { close(as); ast_atomic_fetchadd_int(&unauth_sessions, -1); diff --git a/channels/sip/include/sip.h b/channels/sip/include/sip.h index 44c8ddf35..850370cd7 100644 --- a/channels/sip/include/sip.h +++ b/channels/sip/include/sip.h @@ -384,11 +384,12 @@ #define SIP_PAGE3_IGNORE_PREFCAPS (1 << 7) /*!< DP: Ignore prefcaps when setting up an outgoing call leg */ #define SIP_PAGE3_DISCARD_REMOTE_HOLD_RETRIEVAL (1 << 8) /*!< DGP: Stop telling the peer to start music on hold */ #define SIP_PAGE3_FORCE_AVP (1 << 9) /*!< DGP: Force 'RTP/AVP' for all streams, even DTLS */ +#define SIP_PAGE3_RTCP_MUX (1 << 10) /*!< DGP: Attempt to negotiate RFC 5761 RTCP multiplexing */ #define SIP_PAGE3_FLAGS_TO_COPY \ (SIP_PAGE3_SNOM_AOC | SIP_PAGE3_SRTP_TAG_32 | SIP_PAGE3_NAT_AUTO_RPORT | SIP_PAGE3_NAT_AUTO_COMEDIA | \ SIP_PAGE3_DIRECT_MEDIA_OUTGOING | SIP_PAGE3_USE_AVPF | SIP_PAGE3_ICE_SUPPORT | SIP_PAGE3_IGNORE_PREFCAPS | \ - SIP_PAGE3_DISCARD_REMOTE_HOLD_RETRIEVAL | SIP_PAGE3_FORCE_AVP) + SIP_PAGE3_DISCARD_REMOTE_HOLD_RETRIEVAL | SIP_PAGE3_FORCE_AVP | SIP_PAGE3_RTCP_MUX) #define CHECK_AUTH_BUF_INITLEN 256 diff --git a/configs/samples/pjsip.conf.sample b/configs/samples/pjsip.conf.sample index f66161329..82da311a0 100644 --- a/configs/samples/pjsip.conf.sample +++ b/configs/samples/pjsip.conf.sample @@ -841,6 +841,17 @@ ; this option is set to 'no' (the default) changes to the ; particular transport will be ignored. If set to 'yes', ; changes (if any) will be applied. +;symmetric_transport=no ; When a request from a dynamic contact comes in on a + ; transport with this option set to 'yes', the transport + ; name will be saved and used for subsequent outgoing + ; requests like OPTIONS, NOTIFY and INVITE. It's saved + ; as a contact uri parameter named 'x-ast-txp' and will + ; display with the contact uri in CLI, AMI, and ARI + ; output. On the outgoing request, if a transport + ; wasn't explicitly set on the endpoint AND the request + ; URI is not a hostname, the saved transport will be + ; used and the 'x-ast-txp' parameter stripped from the + ; outgoing packet. ;==========================AOR SECTION OPTIONS========================= ;[aor] diff --git a/configs/samples/sip.conf.sample b/configs/samples/sip.conf.sample index c5ffdcccd..2ef997036 100644 --- a/configs/samples/sip.conf.sample +++ b/configs/samples/sip.conf.sample @@ -1063,6 +1063,8 @@ srvlookup=yes ; Enable DNS SRV lookups on outbound calls ; option may be specified at the global or peer scope. ;force_avp=yes ; Force 'RTP/AVP', 'RTP/AVPF', 'RTP/SAVP', and 'RTP/SAVPF' to be used for ; media streams when appropriate, even if a DTLS stream is present. +;rtcp_mux=yes ; Enable support for RFC 5761 RTCP multiplexing which is required for + ; WebRTC support ; ---------------------------------------- REALTIME SUPPORT ------------------------ ; For additional information on ARA, the Asterisk Realtime Architecture, ; please read https://wiki.asterisk.org/wiki/display/AST/Realtime+Database+Configuration diff --git a/contrib/ast-db-manage/config/versions/15db7b91a97a_add_rtcp_mux.py b/contrib/ast-db-manage/config/versions/15db7b91a97a_add_rtcp_mux.py new file mode 100644 index 000000000..8b0214a17 --- /dev/null +++ b/contrib/ast-db-manage/config/versions/15db7b91a97a_add_rtcp_mux.py @@ -0,0 +1,31 @@ +"""empty message + +Revision ID: 15db7b91a97a +Revises: 465e70e8c337 +Create Date: 2017-03-08 16:56:38.108162 + +""" + +# revision identifiers, used by Alembic. +revision = '15db7b91a97a' +down_revision = '465e70e8c337' + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects.postgresql import ENUM + +YESNO_NAME = 'yesno_values' +YESNO_VALUES = ['yes', 'no'] + +def upgrade(): + ############################# Enums ############################## + + # yesno_values have already been created, so use postgres enum object + # type to get around "already created" issue - works okay with mysql + yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False) + + op.add_column('ps_endpoints', sa.Column('rtcp_mux', yesno_values)) + + +def downgrade(): + op.drop_column('ps_endpoints', 'rtcp_mux') diff --git a/contrib/ast-db-manage/config/versions/f638dbe2eb23_symmetric_transport.py b/contrib/ast-db-manage/config/versions/f638dbe2eb23_symmetric_transport.py new file mode 100644 index 000000000..51b5066f5 --- /dev/null +++ b/contrib/ast-db-manage/config/versions/f638dbe2eb23_symmetric_transport.py @@ -0,0 +1,32 @@ +"""symmetric_transport + +Revision ID: f638dbe2eb23 +Revises: 15db7b91a97a +Create Date: 2017-03-09 09:38:59.513479 + +""" + +# revision identifiers, used by Alembic. +revision = 'f638dbe2eb23' +down_revision = '15db7b91a97a' + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects.postgresql import ENUM + +YESNO_NAME = 'yesno_values' +YESNO_VALUES = ['yes', 'no'] + +def upgrade(): + ############################# Enums ############################## + + # yesno_values have already been created, so use postgres enum object + # type to get around "already created" issue - works okay with mysql + yesno_values = ENUM(*YESNO_VALUES, name=YESNO_NAME, create_type=False) + + op.add_column('ps_transports', sa.Column('symmetric_transport', yesno_values)) + op.add_column('ps_subscription_persistence', sa.Column('contact_uri', sa.String(256))) + +def downgrade(): + op.drop_column('ps_subscription_persistence', 'contact_uri') + op.drop_column('ps_transports', 'symmetric_transport') diff --git a/include/asterisk/network.h b/include/asterisk/network.h index 3371e5895..5216f4c61 100644 --- a/include/asterisk/network.h +++ b/include/asterisk/network.h @@ -86,6 +86,11 @@ const char *ast_inet_ntoa(struct in_addr ia); #endif #define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__ +#ifdef getprotobyname +#undef getprotobyname +#endif +#define getprotobyname __getprotobyname_is_not_threadsafe__do_not_use__ + /*! \brief Compares the source address and port of two sockaddr_in */ static force_inline int inaddrcmp(const struct sockaddr_in *sin1, const struct sockaddr_in *sin2) { diff --git a/include/asterisk/res_hep.h b/include/asterisk/res_hep.h index cfd213ad7..dba86e88b 100644 --- a/include/asterisk/res_hep.h +++ b/include/asterisk/res_hep.h @@ -72,6 +72,8 @@ struct hepv3_capture_info { size_t len; /*! If non-zero, the payload accompanying this capture info will be compressed */ unsigned int zipped:1; + /*! The IPPROTO_* protocol where we captured the packet */ + int protocol_id; }; /*! diff --git a/include/asterisk/res_pjsip.h b/include/asterisk/res_pjsip.h index d8e172fc5..05a3eea44 100644 --- a/include/asterisk/res_pjsip.h +++ b/include/asterisk/res_pjsip.h @@ -194,6 +194,8 @@ struct ast_sip_transport { int write_timeout; /*! Allow reload */ int allow_reload; + /*! Automatically send requests out the same transport requests have come in on */ + int symmetric_transport; }; #define SIP_SORCERY_DOMAIN_ALIAS_TYPE "domain_alias" @@ -755,8 +757,14 @@ struct ast_sip_endpoint { char *contact_user; /*! Do we allow an asymmetric RTP codec? */ unsigned int asymmetric_rtp_codec; + /*! Use RTCP-MUX */ + unsigned int rtcp_mux; }; +/*! URI parameter for symmetric transport */ +#define AST_SIP_X_AST_TXP "x-ast-txp" +#define AST_SIP_X_AST_TXP_LEN 9 + /*! * \brief Initialize an auth vector with the configured values. * @@ -1700,6 +1708,26 @@ pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint, /*! * \brief General purpose method for creating an rdata structure using specific information + * \since 13.15.0 + * + * \param rdata[out] The rdata structure that will be populated + * \param packet A SIP message + * \param src_name The source IP address of the message + * \param src_port The source port of the message + * \param transport_type The type of transport the message was received on + * \param local_name The local IP address the message was received on + * \param local_port The local port the message was received on + * \param contact_uri The contact URI of the message + * + * \retval 0 success + * \retval -1 failure + */ +int ast_sip_create_rdata_with_contact(pjsip_rx_data *rdata, char *packet, + const char *src_name, int src_port, char *transport_type, const char *local_name, + int local_port, const char *contact_uri); + +/*! + * \brief General purpose method for creating an rdata structure using specific information * * \param rdata[out] The rdata structure that will be populated * \param packet A SIP message @@ -1712,8 +1740,8 @@ pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint, * \retval 0 success * \retval -1 failure */ -int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port, char *transport_type, - const char *local_name, int local_port); +int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, + int src_port, char *transport_type, const char *local_name, int local_port); /*! * \brief General purpose method for creating a SIP request @@ -2752,4 +2780,54 @@ void ast_sip_modify_id_header(pj_pool_t *pool, pjsip_fromto_hdr *id_hdr, void ast_sip_get_unidentified_request_thresholds(unsigned int *count, unsigned int *period, unsigned int *prune_interval); +/*! + * \brief Get the transport name from an endpoint or request uri + * \since 13.15.0 + * + * \param endpoint + * \param sip_uri + * \param buf Buffer to receive transport name + * \param buf_len Buffer length + * + * \retval 0 Success + * \retval -1 Failure + * + * \note + * If endpoint->transport is not NULL, it is returned in buf. + * Otherwise if sip_uri has an 'x-ast-txp' parameter AND the sip_uri host is + * an ip4 or ip6 address, its value is returned, + */ +int ast_sip_get_transport_name(const struct ast_sip_endpoint *endpoint, + pjsip_sip_uri *sip_uri, char *buf, size_t buf_len); + +/*! + * \brief Sets pjsip_tpselector from an endpoint or uri + * \since 13.15.0 + * + * \param endpoint If endpoint->transport is set, it's used + * \param sip_uri If sip_uri contains a x-ast-txp parameter, it's used + * \param selector The selector to be populated + * + * \retval 0 success + * \retval -1 failure + */ +int ast_sip_set_tpselector_from_ep_or_uri(const struct ast_sip_endpoint *endpoint, + pjsip_sip_uri *sip_uri, pjsip_tpselector *selector); + +/*! + * \brief Set the transport on a dialog + * \since 13.15.0 + * + * \param endpoint + * \param dlg + * \param selector (optional) + * + * \note + * This API calls ast_sip_get_transport_name(endpoint, dlg->target) and if the result is + * non-NULL, calls pjsip_dlg_set_transport. If 'selector' is non-NULL, it is updated with + * the selector used. + */ +int ast_sip_dlg_set_transport(const struct ast_sip_endpoint *endpoint, pjsip_dialog *dlg, + pjsip_tpselector *selector); + #endif /* _RES_PJSIP_H */ diff --git a/include/asterisk/res_pjsip_session.h b/include/asterisk/res_pjsip_session.h index 7e65e6d7c..c41cc3ab9 100644 --- a/include/asterisk/res_pjsip_session.h +++ b/include/asterisk/res_pjsip_session.h @@ -83,6 +83,8 @@ struct ast_sip_session_media { int timeout_sched_id; /*! \brief Stream is on hold */ unsigned int held:1; + /*! \brief Does remote support rtcp_mux */ + unsigned int remote_rtcp_mux:1; /*! \brief Stream type this session media handles */ char stream_type[1]; }; diff --git a/include/asterisk/rtp_engine.h b/include/asterisk/rtp_engine.h index c4a5b6b41..e8f3d78b4 100644 --- a/include/asterisk/rtp_engine.h +++ b/include/asterisk/rtp_engine.h @@ -237,6 +237,15 @@ enum ast_rtp_instance_stat { AST_RTP_INSTANCE_STAT_RXOCTETCOUNT, }; +enum ast_rtp_instance_rtcp { + /*! RTCP should not be sent/received */ + AST_RTP_INSTANCE_RTCP_DISABLED = 0, + /*! RTCP should be sent/received based on standard port rules */ + AST_RTP_INSTANCE_RTCP_STANDARD, + /*! RTCP should be sent/received on the same port as RTP */ + AST_RTP_INSTANCE_RTCP_MUX, +}; + /* Codes for RTP-specific data - not defined by our AST_FORMAT codes */ /*! DTMF (RFC2833) */ #define AST_RTP_DTMF (1 << 0) @@ -443,6 +452,8 @@ struct ast_rtp_engine_ice { void (*turn_request)(struct ast_rtp_instance *instance, enum ast_rtp_ice_component_type component, enum ast_transport transport, const char *server, unsigned int port, const char *username, const char *password); + /*! Callback to alter the number of ICE components on a session */ + void (*change_components)(struct ast_rtp_instance *instance, int num_components); }; /*! \brief DTLS setup types */ diff --git a/main/http.c b/main/http.c index 155b04b78..80c7b3cb4 100644 --- a/main/http.c +++ b/main/http.c @@ -1915,8 +1915,7 @@ static int httpd_process_request(struct ast_tcptls_session_instance *ser) static void *httpd_helper_thread(void *data) { struct ast_tcptls_session_instance *ser = data; - struct protoent *p; - int flags; + int flags = 1; int timeout; if (!ser || !ser->f) { @@ -1936,17 +1935,8 @@ static void *httpd_helper_thread(void *data) * This is necessary to prevent delays (caused by buffering) as we * write to the socket in bits and pieces. */ - p = getprotobyname("tcp"); - if (p) { - int arg = 1; - - if (setsockopt(ser->fd, p->p_proto, TCP_NODELAY, (char *) &arg, sizeof(arg) ) < 0) { - ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on HTTP connection: %s\n", strerror(errno)); - ast_log(LOG_WARNING, "Some HTTP requests may be slow to respond.\n"); - } - } else { - ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on HTTP connection, getprotobyname(\"tcp\") failed\n"); - ast_log(LOG_WARNING, "Some HTTP requests may be slow to respond.\n"); + if (setsockopt(ser->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flags, sizeof(flags)) < 0) { + ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on HTTP connection: %s\n", strerror(errno)); } /* make sure socket is non-blocking */ diff --git a/main/manager.c b/main/manager.c index b8dbb1a04..0f7adf0c8 100644 --- a/main/manager.c +++ b/main/manager.c @@ -6591,10 +6591,9 @@ static void *session_do(void *data) struct mansession s = { .tcptls_session = data, }; - int flags; + int flags = 1; int res; struct ast_sockaddr ser_remote_address_tmp; - struct protoent *p; if (ast_atomic_fetchadd_int(&unauth_sessions, +1) >= authlimit) { fclose(ser->f); @@ -6614,14 +6613,8 @@ static void *session_do(void *data) /* here we set TCP_NODELAY on the socket to disable Nagle's algorithm. * This is necessary to prevent delays (caused by buffering) as we * write to the socket in bits and pieces. */ - p = getprotobyname("tcp"); - if (p) { - int arg = 1; - if( setsockopt(ser->fd, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) { - ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\nSome manager actions may be slow to respond.\n", strerror(errno)); - } - } else { - ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY, getprotobyname(\"tcp\") failed\nSome manager actions may be slow to respond.\n"); + if (setsockopt(ser->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flags, sizeof(flags)) < 0) { + ast_log(LOG_WARNING, "Failed to set TCP_NODELAY on manager connection: %s\n", strerror(errno)); } /* make sure socket is non-blocking */ diff --git a/main/pbx.c b/main/pbx.c index d648084d7..df99940ba 100644 --- a/main/pbx.c +++ b/main/pbx.c @@ -616,7 +616,7 @@ static int ast_add_extension2_lockopt(struct ast_context *con, static struct ast_context *find_context_locked(const char *context); static struct ast_context *find_context(const char *context); static void get_device_state_causing_channels(struct ao2_container *c); -static int ext_strncpy(char *dst, const char *src, int len, int nofluff); +static unsigned int ext_strncpy(char *dst, const char *src, size_t dst_size, int nofluff); /*! * \internal @@ -6907,32 +6907,51 @@ int ast_async_goto_by_name(const char *channame, const char *context, const char return res; } -/*! \brief copy a string skipping whitespace and dashes */ -static int ext_strncpy(char *dst, const char *src, int len, int nofluff) +/*! + * \internal + * \brief Copy a string skipping whitespace and optionally dashes. + * + * \param dst Destination buffer to copy src string. + * \param src Null terminated string to copy. + * \param dst_size Number of bytes in the dst buffer. + * \param nofluf Nonzero if '-' chars are not copied. + * + * \return Number of bytes written to dst including null terminator. + */ +static unsigned int ext_strncpy(char *dst, const char *src, size_t dst_size, int nofluff) { - int count = 0; - int insquares = 0; + unsigned int count; + unsigned int insquares; + unsigned int is_pattern; - while (*src && (count < len - 1)) { + if (!dst_size--) { + /* There really is no dst buffer */ + return 0; + } + + count = 0; + insquares = 0; + is_pattern = *src == '_'; + while (*src && count < dst_size) { if (*src == '[') { - insquares = 1; + if (is_pattern) { + insquares = 1; + } } else if (*src == ']') { insquares = 0; } else if (*src == ' ' && !insquares) { - src++; + ++src; continue; } else if (*src == '-' && !insquares && nofluff) { - src++; + ++src; continue; } - *dst = *src; - dst++; - src++; - count++; + *dst++ = *src++; + ++count; } *dst = '\0'; - return count; + return count + 1; } /*! @@ -7246,10 +7265,10 @@ static int ast_add_extension2_lockopt(struct ast_context *con, p += strlen(label) + 1; } tmp->name = p; - p += ext_strncpy(p, extension, strlen(extension) + 1, 0) + 1; + p += ext_strncpy(p, extension, strlen(extension) + 1, 0); if (exten_fluff) { tmp->exten = p; - p += ext_strncpy(p, extension, strlen(extension) + 1, 1) + 1; + p += ext_strncpy(p, extension, strlen(extension) + 1 - exten_fluff, 1); } else { /* no fluff, we don't need a copy. */ tmp->exten = tmp->name; @@ -7259,10 +7278,10 @@ static int ast_add_extension2_lockopt(struct ast_context *con, /* Blank callerid and NULL callerid are two SEPARATE things. Do NOT confuse the two!!! */ if (callerid) { - p += ext_strncpy(p, callerid, strlen(callerid) + 1, 0) + 1; + p += ext_strncpy(p, callerid, strlen(callerid) + 1, 0); if (callerid_fluff) { tmp->cidmatch = p; - p += ext_strncpy(p, callerid, strlen(callerid) + 1, 1) + 1; + p += ext_strncpy(p, callerid, strlen(callerid) + 1 - callerid_fluff, 1); } tmp->matchcid = AST_EXT_MATCHCID_ON; } else { diff --git a/res/res_hep.c b/res/res_hep.c index 15e779012..8d4987c03 100644 --- a/res/res_hep.c +++ b/res/res_hep.c @@ -441,6 +441,9 @@ struct hepv3_capture_info *hepv3_create_capture_info(const void *payload, size_t memcpy(info->payload, payload, len); info->len = len; + /* Set a reasonable default */ + info->protocol_id = IPPROTO_UDP; + return info; } @@ -472,7 +475,7 @@ static int hep_queue_cb(void *data) /* Build HEPv3 header, capture info, and calculate the total packet size */ memcpy(hg_pkt.header.id, "\x48\x45\x50\x33", 4); - INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.ip_proto, CHUNK_TYPE_IP_PROTOCOL_ID, 0x11); + INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.ip_proto, CHUNK_TYPE_IP_PROTOCOL_ID, capture_info->protocol_id); INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.src_port, CHUNK_TYPE_SRC_PORT, htons(ast_sockaddr_port(&capture_info->src_addr))); INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.dst_port, CHUNK_TYPE_DST_PORT, htons(ast_sockaddr_port(&capture_info->dst_addr))); INITIALIZE_GENERIC_HEP_CHUNK_DATA(&hg_pkt.time_sec, CHUNK_TYPE_TIMESTAMP_SEC, htonl(capture_info->capture_time.tv_sec)); diff --git a/res/res_hep_pjsip.c b/res/res_hep_pjsip.c index 8f5baa2cb..1614b4319 100644 --- a/res/res_hep_pjsip.c +++ b/res/res_hep_pjsip.c @@ -73,6 +73,15 @@ static char *assign_uuid(const pj_str_t *call_id, const pj_str_t *local_tag, con return uuid; } +static int transport_to_protocol_id(pjsip_transport *tp) +{ + /* XXX If we ever add SCTP support, we'll need to revisit */ + if (tp->flag & PJSIP_TRANSPORT_RELIABLE) { + return IPPROTO_TCP; + } + return IPPROTO_UDP; +} + static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata) { char local_buf[256]; @@ -126,6 +135,7 @@ static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata) ast_sockaddr_parse(&capture_info->src_addr, local_buf, PARSE_PORT_REQUIRE); ast_sockaddr_parse(&capture_info->dst_addr, remote_buf, PARSE_PORT_REQUIRE); + capture_info->protocol_id = transport_to_protocol_id(tdata->tp_info.transport); capture_info->capture_time = ast_tvnow(); capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP; capture_info->uuid = uuid; @@ -185,6 +195,8 @@ static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata) ast_sockaddr_parse(&capture_info->src_addr, remote_buf, PARSE_PORT_REQUIRE); ast_sockaddr_parse(&capture_info->dst_addr, local_buf, PARSE_PORT_REQUIRE); + + capture_info->protocol_id = transport_to_protocol_id(rdata->tp_info.transport); capture_info->capture_time.tv_sec = rdata->pkt_info.timestamp.sec; capture_info->capture_time.tv_usec = rdata->pkt_info.timestamp.msec * 1000; capture_info->capture_type = HEPV3_CAPTURE_TYPE_SIP; diff --git a/res/res_pjsip.c b/res/res_pjsip.c index 347658f9c..7b10f47f6 100644 --- a/res/res_pjsip.c +++ b/res/res_pjsip.c @@ -944,6 +944,16 @@ to the receiving one. </para></description> </configOption> + <configOption name="rtcp_mux" default="no"> + <synopsis>Enable RFC 5761 RTCP multiplexing on the RTP port</synopsis> + <description><para> + With this option enabled, Asterisk will attempt to negotiate the use of the "rtcp-mux" + attribute on all media streams. This will result in RTP and RTCP being sent and received + on the same port. This shifts the demultiplexing logic to the application rather than + the transport layer. This option is useful when interoperating with WebRTC endpoints + since they mandate this option's use. + </para></description> + </configOption> </configObject> <configObject name="auth"> <synopsis>Authentication type</synopsis> @@ -1177,6 +1187,22 @@ in-progress calls.</para> </description> </configOption> + <configOption name="symmetric_transport" default="no"> + <synopsis>Use the same transport for outgoing reqests as incoming ones.</synopsis> + <description> + <para>When a request from a dynamic contact + comes in on a transport with this option set to 'yes', + the transport name will be saved and used for subsequent + outgoing requests like OPTIONS, NOTIFY and INVITE. It's + saved as a contact uri parameter named 'x-ast-txp' and will + display with the contact uri in CLI, AMI, and ARI output. + On the outgoing request, if a transport wasn't explicitly + set on the endpoint AND the request URI is not a hostname, + the saved transport will be used and the 'x-ast-txp' + parameter stripped from the outgoing packet. + </para> + </description> + </configOption> </configObject> <configObject name="contact"> <synopsis>A way of creating an aliased name to a SIP URI</synopsis> @@ -2750,7 +2776,54 @@ pjsip_endpoint *ast_sip_get_pjsip_endpoint(void) return ast_pjsip_endpoint; } -static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *user, const char *domain, const pj_str_t *target, pjsip_tpselector *selector) +int ast_sip_get_transport_name(const struct ast_sip_endpoint *endpoint, + pjsip_sip_uri *sip_uri, char *buf, size_t buf_len) +{ + char *host = NULL; + static const pj_str_t x_name = { AST_SIP_X_AST_TXP, AST_SIP_X_AST_TXP_LEN }; + pjsip_param *x_transport; + + if (!ast_strlen_zero(endpoint->transport)) { + ast_copy_string(buf, endpoint->transport, buf_len); + return 0; + } + + x_transport = pjsip_param_find(&sip_uri->other_param, &x_name); + if (!x_transport) { + return -1; + } + + /* Only use x_transport if the uri host is an ip (4 or 6) address */ + host = ast_alloca(sip_uri->host.slen + 1); + ast_copy_pj_str(host, &sip_uri->host, sip_uri->host.slen + 1); + if (!ast_sockaddr_parse(NULL, host, PARSE_PORT_FORBID)) { + return -1; + } + + ast_copy_pj_str(buf, &x_transport->value, buf_len); + + return 0; +} + +int ast_sip_dlg_set_transport(const struct ast_sip_endpoint *endpoint, pjsip_dialog *dlg, + pjsip_tpselector *selector) +{ + pjsip_sip_uri *uri; + pjsip_tpselector sel = { .type = PJSIP_TPSELECTOR_NONE, }; + + uri = pjsip_uri_get_uri(dlg->target); + if (!selector) { + selector = &sel; + } + + ast_sip_set_tpselector_from_ep_or_uri(endpoint, uri, selector); + pjsip_dlg_set_transport(dlg, selector); + + return 0; +} + +static int sip_dialog_create_from(pj_pool_t *pool, pj_str_t *from, const char *user, + const char *domain, const pj_str_t *target, pjsip_tpselector *selector) { pj_str_t tmp, local_addr; pjsip_uri *uri; @@ -2880,15 +2953,16 @@ int ast_sip_set_tpselector_from_transport_name(const char *transport_name, pjsip return ast_sip_set_tpselector_from_transport(transport, selector); } -static int sip_get_tpselector_from_endpoint(const struct ast_sip_endpoint *endpoint, pjsip_tpselector *selector) +int ast_sip_set_tpselector_from_ep_or_uri(const struct ast_sip_endpoint *endpoint, + pjsip_sip_uri *sip_uri, pjsip_tpselector *selector) { - const char *transport_name = endpoint->transport; + char transport_name[128]; - if (ast_strlen_zero(transport_name)) { + if (ast_sip_get_transport_name(endpoint, sip_uri, transport_name, sizeof(transport_name))) { return 0; } - return ast_sip_set_tpselector_from_transport_name(endpoint->transport, selector); + return ast_sip_set_tpselector_from_transport_name(transport_name, selector); } void ast_sip_add_usereqphone(const struct ast_sip_endpoint *endpoint, pj_pool_t *pool, pjsip_uri *uri) @@ -2896,8 +2970,8 @@ void ast_sip_add_usereqphone(const struct ast_sip_endpoint *endpoint, pj_pool_t pjsip_sip_uri *sip_uri; int i = 0; pjsip_param *param; - const pj_str_t STR_USER = { "user", 4 }; - const pj_str_t STR_PHONE = { "phone", 5 }; + static const pj_str_t STR_USER = { "user", 4 }; + static const pj_str_t STR_PHONE = { "phone", 5 }; if (!endpoint || !endpoint->usereqphone || (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri))) { return; @@ -2930,7 +3004,8 @@ void ast_sip_add_usereqphone(const struct ast_sip_endpoint *endpoint, pj_pool_t pj_list_insert_before(&sip_uri->other_param, param); } -pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint, const char *uri, const char *request_user) +pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint, + const char *uri, const char *request_user) { char enclosed_uri[PJSIP_MAX_URL_SIZE]; pj_str_t local_uri = { "sip:temp@temp", 13 }, remote_uri, target_uri; @@ -2955,12 +3030,13 @@ pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint, return NULL; } - if (sip_get_tpselector_from_endpoint(endpoint, &selector)) { - pjsip_dlg_terminate(dlg); - return NULL; - } + /* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */ + dlg->sess_count++; + + ast_sip_dlg_set_transport(endpoint, dlg, &selector); if (sip_dialog_create_from(dlg->pool, &local_uri, endpoint->fromuser, endpoint->fromdomain, &remote_uri, &selector)) { + dlg->sess_count--; pjsip_dlg_terminate(dlg); return NULL; } @@ -2996,11 +3072,6 @@ pjsip_dialog *ast_sip_create_dialog_uac(const struct ast_sip_endpoint *endpoint, ast_sip_add_usereqphone(endpoint, dlg->pool, dlg->target); ast_sip_add_usereqphone(endpoint, dlg->pool, dlg->remote.info->uri); - /* We have to temporarily bump up the sess_count here so the dialog is not prematurely destroyed */ - dlg->sess_count++; - - pjsip_dlg_set_transport(dlg, &selector); - if (!ast_strlen_zero(outbound_proxy)) { pjsip_route_hdr route_set, *route; static const pj_str_t ROUTE_HNAME = { "Route", 5 }; @@ -3069,10 +3140,13 @@ pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint, pjsip_transport_type_e type = rdata->tp_info.transport->key.type; pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, }; pjsip_transport *transport; + pjsip_contact_hdr *contact_hdr; ast_assert(status != NULL); - if (sip_get_tpselector_from_endpoint(endpoint, &selector)) { + contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL); + if (ast_sip_set_tpselector_from_ep_or_uri(endpoint, pjsip_uri_get_uri(contact_hdr->uri), + &selector)) { return NULL; } @@ -3118,8 +3192,8 @@ pjsip_dialog *ast_sip_create_dialog_uas(const struct ast_sip_endpoint *endpoint, return dlg; } -int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port, - char *transport_type, const char *local_name, int local_port) +int ast_sip_create_rdata_with_contact(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port, + char *transport_type, const char *local_name, int local_port, const char *contact) { pj_str_t tmp; @@ -3143,6 +3217,16 @@ int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_nam return -1; } + if (!ast_strlen_zero(contact)) { + pjsip_contact_hdr *contact_hdr; + + contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL); + if (contact_hdr) { + contact_hdr->uri = pjsip_parse_uri(rdata->tp_info.pool, (char *)contact, + strlen(contact), PJSIP_PARSE_URI_AS_NAMEADDR); + } + } + pj_strdup2(rdata->tp_info.pool, &rdata->msg_info.via->recvd_param, rdata->pkt_info.src_name); rdata->msg_info.via->rport_param = -1; @@ -3154,6 +3238,13 @@ int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_nam return 0; } +int ast_sip_create_rdata(pjsip_rx_data *rdata, char *packet, const char *src_name, int src_port, + char *transport_type, const char *local_name, int local_port) +{ + return ast_sip_create_rdata_with_contact(rdata, packet, src_name, src_port, transport_type, + local_name, local_port, NULL); +} + /* PJSIP doesn't know about the INFO method, so we have to define it ourselves */ static const pjsip_method info_method = {PJSIP_OTHER_METHOD, {"INFO", 4} }; static const pjsip_method message_method = {PJSIP_OTHER_METHOD, {"MESSAGE", 7} }; @@ -3235,14 +3326,6 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s pj_cstr(&remote_uri, uri); } - if (endpoint) { - if (sip_get_tpselector_from_endpoint(endpoint, &selector)) { - ast_log(LOG_ERROR, "Unable to retrieve PJSIP transport selector for endpoint %s\n", - ast_sorcery_object_get_id(endpoint)); - return -1; - } - } - pool = pjsip_endpt_create_pool(ast_sip_get_pjsip_endpoint(), "Outbound request", 256, 256); if (!pool) { @@ -3260,6 +3343,8 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s return -1; } + ast_sip_set_tpselector_from_ep_or_uri(endpoint, pjsip_uri_get_uri(sip_uri), &selector); + fromuser = endpoint ? (!ast_strlen_zero(endpoint->fromuser) ? endpoint->fromuser : ast_sorcery_object_get_id(endpoint)) : NULL; if (sip_dialog_create_from(pool, &from, fromuser, endpoint ? endpoint->fromdomain : NULL, &remote_uri, &selector)) { @@ -3279,6 +3364,8 @@ static int create_out_of_dialog_request(const pjsip_method *method, struct ast_s return -1; } + pjsip_tx_data_set_transport(*tdata, &selector); + if (endpoint && !ast_strlen_zero(endpoint->contact_user)){ pjsip_contact_hdr *contact_hdr; pjsip_sip_uri *contact_uri; @@ -3320,6 +3407,8 @@ int ast_sip_create_request(const char *method, struct pjsip_dialog *dlg, { const pjsip_method *pmethod = get_pjsip_method(method); + ast_assert(endpoint != NULL); + if (!pmethod) { ast_log(LOG_WARNING, "Unknown method '%s'. Cannot send request\n", method); return -1; @@ -3584,7 +3673,6 @@ static pj_status_t endpt_send_request(struct ast_sip_endpoint *endpoint, struct send_request_wrapper *req_wrapper; pj_status_t ret_val; pjsip_endpoint *endpt = ast_sip_get_pjsip_endpoint(); - pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, }; if (!cb && token) { /* Silly. Without a callback we cannot do anything with token. */ @@ -3609,11 +3697,6 @@ static pj_status_t endpt_send_request(struct ast_sip_endpoint *endpoint, /* Add a reference to tdata. The wrapper destructor cleans it up. */ pjsip_tx_data_add_ref(tdata); - if (endpoint) { - sip_get_tpselector_from_endpoint(endpoint, &selector); - pjsip_tx_data_set_transport(tdata, &selector); - } - if (timeout > 0) { pj_time_val timeout_timer_val = { timeout / 1000, timeout % 1000 }; diff --git a/res/res_pjsip/config_transport.c b/res/res_pjsip/config_transport.c index 60b4507cd..3c41f175a 100644 --- a/res/res_pjsip/config_transport.c +++ b/res/res_pjsip/config_transport.c @@ -552,13 +552,20 @@ static int transport_apply(const struct ast_sorcery *sorcery, void *obj) } } - if (res == PJ_SUCCESS && (transport->tos || transport->cos)) { - pj_sock_t sock; - pj_qos_params qos_params; - sock = pjsip_udp_transport_get_socket(temp_state->state->transport); - pj_sock_get_qos_params(sock, &qos_params); - set_qos(transport, &qos_params); - pj_sock_set_qos_params(sock, &qos_params); + if (res == PJ_SUCCESS) { + temp_state->state->transport->info = pj_pool_alloc(temp_state->state->transport->pool, + (AST_SIP_X_AST_TXP_LEN + strlen(transport_id) + 2)); + + sprintf(temp_state->state->transport->info, "%s:%s", AST_SIP_X_AST_TXP, transport_id); + + if (transport->tos || transport->cos) { + pj_sock_t sock; + pj_qos_params qos_params; + sock = pjsip_udp_transport_get_socket(temp_state->state->transport); + pj_sock_get_qos_params(sock, &qos_params); + set_qos(transport, &qos_params); + pj_sock_set_qos_params(sock, &qos_params); + } } } else if (transport->type == AST_TRANSPORT_TCP) { pjsip_tcp_transport_cfg cfg; @@ -1375,6 +1382,7 @@ int ast_sip_initialize_sorcery_transport(void) ast_sorcery_object_field_register(sorcery, "transport", "cos", "0", OPT_UINT_T, 0, FLDSET(struct ast_sip_transport, cos)); ast_sorcery_object_field_register(sorcery, "transport", "websocket_write_timeout", AST_DEFAULT_WEBSOCKET_WRITE_TIMEOUT_STR, OPT_INT_T, PARSE_IN_RANGE, FLDSET(struct ast_sip_transport, write_timeout), 1, INT_MAX); ast_sorcery_object_field_register(sorcery, "transport", "allow_reload", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_transport, allow_reload)); + ast_sorcery_object_field_register(sorcery, "transport", "symmetric_transport", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_transport, symmetric_transport)); internal_sip_register_endpoint_formatter(&endpoint_transport_formatter); diff --git a/res/res_pjsip/pjsip_configuration.c b/res/res_pjsip/pjsip_configuration.c index bfaf750d4..eb8e19712 100644 --- a/res/res_pjsip/pjsip_configuration.c +++ b/res/res_pjsip/pjsip_configuration.c @@ -1938,6 +1938,7 @@ int ast_res_pjsip_initialize_configuration(const struct ast_module_info *ast_mod ast_sorcery_object_field_register(sip_sorcery, "endpoint", "subscribe_context", "", OPT_CHAR_ARRAY_T, 0, CHARFLDSET(struct ast_sip_endpoint, subscription.context)); ast_sorcery_object_field_register_custom(sip_sorcery, "endpoint", "contact_user", "", contact_user_handler, contact_user_to_str, NULL, 0, 0); ast_sorcery_object_field_register(sip_sorcery, "endpoint", "asymmetric_rtp_codec", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, asymmetric_rtp_codec)); + ast_sorcery_object_field_register(sip_sorcery, "endpoint", "rtcp_mux", "no", OPT_BOOL_T, 1, FLDSET(struct ast_sip_endpoint, rtcp_mux)); if (ast_sip_initialize_sorcery_transport()) { ast_log(LOG_ERROR, "Failed to register SIP transport support with sorcery\n"); diff --git a/res/res_pjsip/pjsip_message_ip_updater.c b/res/res_pjsip/pjsip_message_ip_updater.c index 7671ad0a7..864d898b3 100644 --- a/res/res_pjsip/pjsip_message_ip_updater.c +++ b/res/res_pjsip/pjsip_message_ip_updater.c @@ -28,6 +28,7 @@ #define MOD_DATA_RESTRICTIONS "restrictions" static pj_status_t multihomed_on_tx_message(pjsip_tx_data *tdata); +static pj_bool_t multihomed_on_rx_message(pjsip_rx_data *rdata); /*! \brief Outgoing message modification restrictions */ struct multihomed_message_restrictions { @@ -41,6 +42,7 @@ static pjsip_module multihomed_module = { .priority = PJSIP_MOD_PRIORITY_TSX_LAYER - 1, .on_tx_request = multihomed_on_tx_message, .on_tx_response = multihomed_on_tx_message, + .on_rx_request = multihomed_on_rx_message, }; /*! \brief Helper function to get (or allocate if not already present) restrictions on a message */ @@ -151,6 +153,44 @@ static int multihomed_rewrite_sdp(struct pjmedia_sdp_session *sdp) return 0; } +static void sanitize_tdata(pjsip_tx_data *tdata) +{ + static const pj_str_t x_name = { AST_SIP_X_AST_TXP, AST_SIP_X_AST_TXP_LEN }; + pjsip_param *x_transport; + pjsip_sip_uri *uri; + pjsip_fromto_hdr *fromto; + pjsip_contact_hdr *contact; + pjsip_hdr *hdr; + + if (tdata->msg->type == PJSIP_REQUEST_MSG) { + uri = pjsip_uri_get_uri(tdata->msg->line.req.uri); + x_transport = pjsip_param_find(&uri->other_param, &x_name); + if (x_transport) { + pj_list_erase(x_transport); + } + } + + for (hdr = tdata->msg->hdr.next; hdr != &tdata->msg->hdr; hdr = hdr->next) { + if (hdr->type == PJSIP_H_TO || hdr->type == PJSIP_H_FROM) { + fromto = (pjsip_fromto_hdr *) hdr; + uri = pjsip_uri_get_uri(fromto->uri); + x_transport = pjsip_param_find(&uri->other_param, &x_name); + if (x_transport) { + pj_list_erase(x_transport); + } + } else if (hdr->type == PJSIP_H_CONTACT) { + contact = (pjsip_contact_hdr *) hdr; + uri = pjsip_uri_get_uri(contact->uri); + x_transport = pjsip_param_find(&uri->other_param, &x_name); + if (x_transport) { + pj_list_erase(x_transport); + } + } + } + + pjsip_tx_data_invalidate_msg(tdata); +} + static pj_status_t multihomed_on_tx_message(pjsip_tx_data *tdata) { struct multihomed_message_restrictions *restrictions = ast_sip_mod_data_get(tdata->mod_data, multihomed_module.id, MOD_DATA_RESTRICTIONS); @@ -159,6 +199,8 @@ static pj_status_t multihomed_on_tx_message(pjsip_tx_data *tdata) pjsip_via_hdr *via; pjsip_fromto_hdr *from; + sanitize_tdata(tdata); + /* Use the destination information to determine what local interface this message will go out on */ pjsip_tpmgr_fla2_param_default(&prm); prm.tp_type = tdata->tp_info.transport->key.type; @@ -273,6 +315,47 @@ static pj_status_t multihomed_on_tx_message(pjsip_tx_data *tdata) return PJ_SUCCESS; } +static pj_bool_t multihomed_on_rx_message(pjsip_rx_data *rdata) +{ + pjsip_contact_hdr *contact; + pjsip_sip_uri *uri; + const char *transport_id; + struct ast_sip_transport *transport; + pjsip_param *x_transport; + + if (rdata->msg_info.msg->type != PJSIP_REQUEST_MSG) { + return PJ_FALSE; + } + + contact = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL); + if (!(contact && contact->uri + && ast_begins_with(rdata->tp_info.transport->info, AST_SIP_X_AST_TXP ":"))) { + return PJ_FALSE; + } + + uri = pjsip_uri_get_uri(contact->uri); + + transport_id = rdata->tp_info.transport->info + AST_SIP_X_AST_TXP_LEN + 1; + transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", transport_id); + + if (!(transport && transport->symmetric_transport)) { + return PJ_FALSE; + } + + x_transport = PJ_POOL_ALLOC_T(rdata->tp_info.pool, pjsip_param); + x_transport->name = pj_strdup3(rdata->tp_info.pool, AST_SIP_X_AST_TXP); + x_transport->value = pj_strdup3(rdata->tp_info.pool, transport_id); + + pj_list_insert_before(&uri->other_param, x_transport); + + ast_debug(1, "Set transport '%s' on %.*s from %.*s:%d\n", transport_id, + (int)rdata->msg_info.msg->line.req.method.name.slen, + rdata->msg_info.msg->line.req.method.name.ptr, + (int)uri->host.slen, uri->host.ptr, uri->port); + + return PJ_FALSE; +} + void ast_res_pjsip_cleanup_message_ip_updater(void) { ast_sip_unregister_service(&multihomed_module); diff --git a/res/res_pjsip_nat.c b/res/res_pjsip_nat.c index 7404ef5f0..5fcab6378 100644 --- a/res/res_pjsip_nat.c +++ b/res/res_pjsip_nat.c @@ -262,32 +262,33 @@ static pj_status_t nat_on_tx_message(pjsip_tx_data *tdata) return PJ_SUCCESS; } - if ( !transport_state->localnet || ast_sockaddr_isnull(&transport_state->external_address)) { - return PJ_SUCCESS; - } - - ast_sockaddr_parse(&addr, tdata->tp_info.dst_name, PARSE_PORT_FORBID); - ast_sockaddr_set_port(&addr, tdata->tp_info.dst_port); + if (transport_state->localnet) { + ast_sockaddr_parse(&addr, tdata->tp_info.dst_name, PARSE_PORT_FORBID); + ast_sockaddr_set_port(&addr, tdata->tp_info.dst_port); - /* See if where we are sending this request is local or not, and if not that we can get a Contact URI to modify */ - if (ast_apply_ha(transport_state->localnet, &addr) != AST_SENSE_ALLOW) { - return PJ_SUCCESS; + /* See if where we are sending this request is local or not, and if not that we can get a Contact URI to modify */ + if (ast_apply_ha(transport_state->localnet, &addr) != AST_SENSE_ALLOW) { + ast_debug(5, "Request is being sent to local address, skipping NAT manipulation\n"); + return PJ_SUCCESS; + } } - /* Update the contact header with the external address */ - if (uri || (uri = nat_get_contact_sip_uri(tdata))) { - pj_strdup2(tdata->pool, &uri->host, ast_sockaddr_stringify_host(&transport_state->external_address)); - if (transport->external_signaling_port) { - uri->port = transport->external_signaling_port; - ast_debug(4, "Re-wrote Contact URI port to %d\n", uri->port); + if (!ast_sockaddr_isnull(&transport_state->external_address)) { + /* Update the contact header with the external address */ + if (uri || (uri = nat_get_contact_sip_uri(tdata))) { + pj_strdup2(tdata->pool, &uri->host, ast_sockaddr_stringify_host(&transport_state->external_address)); + if (transport->external_signaling_port) { + uri->port = transport->external_signaling_port; + ast_debug(4, "Re-wrote Contact URI port to %d\n", uri->port); + } } - } - /* Update the via header if relevant */ - if ((tdata->msg->type == PJSIP_REQUEST_MSG) && (via || (via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL)))) { - pj_strdup2(tdata->pool, &via->sent_by.host, ast_sockaddr_stringify_host(&transport_state->external_address)); - if (transport->external_signaling_port) { - via->sent_by.port = transport->external_signaling_port; + /* Update the via header if relevant */ + if ((tdata->msg->type == PJSIP_REQUEST_MSG) && (via || (via = pjsip_msg_find_hdr(tdata->msg, PJSIP_H_VIA, NULL)))) { + pj_strdup2(tdata->pool, &via->sent_by.host, ast_sockaddr_stringify_host(&transport_state->external_address)); + if (transport->external_signaling_port) { + via->sent_by.port = transport->external_signaling_port; + } } } diff --git a/res/res_pjsip_pubsub.c b/res/res_pjsip_pubsub.c index 1892a20e9..79a4a8c3e 100644 --- a/res/res_pjsip_pubsub.c +++ b/res/res_pjsip_pubsub.c @@ -123,6 +123,9 @@ <configOption name="expires"> <synopsis>The time at which the subscription expires</synopsis> </configOption> + <configOption name="contact_uri"> + <synopsis>The Contact URI of the dialog for the subscription</synopsis> + </configOption> </configObject> <configObject name="resource_list"> <synopsis>Resource list configuration parameters.</synopsis> @@ -376,6 +379,8 @@ struct subscription_persistence { char *tag; /*! When this subscription expires */ struct timeval expires; + /*! Contact URI */ + char contact_uri[PJSIP_MAX_URL_SIZE]; }; /*! @@ -591,8 +596,8 @@ static void subscription_persistence_update(struct sip_subscription_tree *sub_tr return; } - ast_debug(3, "Updating persistence for '%s->%s'\n", - ast_sorcery_object_get_id(sub_tree->endpoint), sub_tree->root->resource); + ast_debug(3, "Updating persistence for '%s->%s'\n", sub_tree->persistence->endpoint, + sub_tree->root->resource); dlg = sub_tree->dlg; sub_tree->persistence->cseq = dlg->local.cseq; @@ -600,10 +605,14 @@ static void subscription_persistence_update(struct sip_subscription_tree *sub_tr if (rdata) { int expires; pjsip_expires_hdr *expires_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_EXPIRES, NULL); + pjsip_contact_hdr *contact_hdr = pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT, NULL); expires = expires_hdr ? expires_hdr->ivalue : DEFAULT_PUBLISH_EXPIRES; sub_tree->persistence->expires = ast_tvadd(ast_tvnow(), ast_samp2tv(expires, 1)); + pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, contact_hdr->uri, + sub_tree->persistence->contact_uri, sizeof(sub_tree->persistence->contact_uri)); + /* When receiving a packet on an streaming transport, it's possible to receive more than one SIP * message at a time into the rdata->pkt_info.packet buffer. However, the rdata->msg_info.msg_buf * will always point to the proper SIP message that is to be processed. When updating subscription @@ -1572,8 +1581,9 @@ static int subscription_persistence_recreate(void *obj, void *arg, int flags) pj_pool_reset(pool); rdata.tp_info.pool = pool; - if (ast_sip_create_rdata(&rdata, persistence->packet, persistence->src_name, persistence->src_port, - persistence->transport_key, persistence->local_name, persistence->local_port)) { + if (ast_sip_create_rdata_with_contact(&rdata, persistence->packet, persistence->src_name, + persistence->src_port, persistence->transport_key, persistence->local_name, + persistence->local_port, persistence->contact_uri)) { ast_log(LOG_WARNING, "Failed recreating '%s' subscription: The message could not be parsed\n", persistence->endpoint); ast_sorcery_delete(ast_sip_get_sorcery(), persistence); @@ -1725,28 +1735,6 @@ void *ast_sip_subscription_get_header(const struct ast_sip_subscription *sub, co return pjsip_msg_find_hdr_by_name(msg, &name, NULL); } -/*! - * \internal - * \brief Wrapper for pjsip_evsub_send_request - * - * This function (re)sets the transport before sending to catch cases - * where the transport might have changed. - * - * If pjproject gives us the ability to resend, we'll only reset the transport - * if PJSIP_ETPNOTAVAIL is returned from send. - * - * \returns pj_status_t - */ -static pj_status_t internal_pjsip_evsub_send_request(struct sip_subscription_tree *sub_tree, pjsip_tx_data *tdata) -{ - pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, }; - - ast_sip_set_tpselector_from_transport_name(sub_tree->endpoint->transport, &selector); - pjsip_dlg_set_transport(sub_tree->dlg, &selector); - - return pjsip_evsub_send_request(sub_tree->evsub, tdata); -} - /* XXX This function is not used. */ struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_subscription_handler *handler, struct ast_sip_endpoint *endpoint, const char *resource) @@ -1794,7 +1782,7 @@ struct ast_sip_subscription *ast_sip_create_subscription(const struct ast_sip_su evsub = sub_tree->evsub; if (pjsip_evsub_initiate(evsub, NULL, -1, &tdata) == PJ_SUCCESS) { - internal_pjsip_evsub_send_request(sub_tree, tdata); + pjsip_evsub_send_request(sub_tree->evsub, tdata); } else { /* pjsip_evsub_terminate will result in pubsub_on_evsub_state, * being called and terminating the subscription. Therefore, we don't @@ -1891,7 +1879,7 @@ static int sip_subscription_send_request(struct sip_subscription_tree *sub_tree, return -1; } - res = internal_pjsip_evsub_send_request(sub_tree, tdata); + res = pjsip_evsub_send_request(sub_tree->evsub, tdata); subscription_persistence_update(sub_tree, NULL, SUBSCRIPTION_PERSISTENCE_SEND_REQUEST); @@ -5343,6 +5331,8 @@ static int load_module(void) persistence_tag_str2struct, persistence_tag_struct2str, NULL, 0, 0); ast_sorcery_object_field_register_custom(sorcery, "subscription_persistence", "expires", "", persistence_expires_str2struct, persistence_expires_struct2str, NULL, 0, 0); + ast_sorcery_object_field_register(sorcery, "subscription_persistence", "contact_uri", "", OPT_CHAR_ARRAY_T, 0, + CHARFLDSET(struct subscription_persistence, contact_uri)); if (apply_list_configuration(sorcery)) { ast_sip_unregister_service(&pubsub_module); diff --git a/res/res_pjsip_refer.c b/res/res_pjsip_refer.c index d52a922fd..db5061249 100644 --- a/res/res_pjsip_refer.c +++ b/res/res_pjsip_refer.c @@ -822,6 +822,13 @@ static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_r */ AST_SIP_USER_OPTIONS_TRUNCATE_CHECK(exten); + /* Uri without exten */ + if (ast_strlen_zero(exten)) { + ast_copy_string(exten, "s", sizeof(exten)); + ast_debug(3, "Channel '%s' from endpoint '%s' attempted blind transfer to a target without extension. Target was set to 's@%s'\n", + ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), context); + } + if (!ast_exists_extension(NULL, context, exten, 1, NULL)) { ast_log(LOG_ERROR, "Channel '%s' from endpoint '%s' attempted blind transfer to '%s@%s' but target does not exist\n", ast_channel_name(session->channel), ast_sorcery_object_get_id(session->endpoint), exten, context); diff --git a/res/res_pjsip_sdp_rtp.c b/res/res_pjsip_sdp_rtp.c index b27050ed8..d44171cf8 100644 --- a/res/res_pjsip_sdp_rtp.c +++ b/res/res_pjsip_sdp_rtp.c @@ -169,6 +169,23 @@ static int rtp_check_timeout(const void *data) return 0; } +/*! + * \brief Enable RTCP on an RTP session. + */ +static void enable_rtcp(struct ast_sip_session *session, struct ast_sip_session_media *session_media, + const struct pjmedia_sdp_media *remote_media) +{ + enum ast_rtp_instance_rtcp rtcp_type; + + if (session->endpoint->rtcp_mux && session_media->remote_rtcp_mux) { + rtcp_type = AST_RTP_INSTANCE_RTCP_MUX; + } else { + rtcp_type = AST_RTP_INSTANCE_RTCP_STANDARD; + } + + ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, rtcp_type); +} + /*! \brief Internal function which creates an RTP instance */ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media) { @@ -179,6 +196,20 @@ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_me if (session->endpoint->media.bind_rtp_to_media_address && !ast_strlen_zero(session->endpoint->media.address)) { ast_sockaddr_parse(&temp_media_address, session->endpoint->media.address, 0); media_address = &temp_media_address; + } else { + struct ast_sip_transport *transport = + ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport", + session->endpoint->transport); + + if (transport && transport->state) { + char hoststr[PJ_INET6_ADDRSTRLEN]; + + pj_sockaddr_print(&transport->state->host, hoststr, sizeof(hoststr), 0); + ast_debug(1, "Transport: %s bound to host: %s, using this for media.\n", + session->endpoint->transport, hoststr); + ast_sockaddr_parse(media_address, hoststr, 0); + } + ao2_cleanup(transport); } if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, media_address, NULL))) { @@ -186,7 +217,6 @@ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_me return -1; } - ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, 1); ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric); if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) { @@ -201,7 +231,7 @@ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_me } if (!strcmp(session_media->stream_type, STR_AUDIO) && - (session->endpoint->media.tos_audio || session->endpoint->media.cos_video)) { + (session->endpoint->media.tos_audio || session->endpoint->media.cos_audio)) { ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio, session->endpoint->media.cos_audio, "SIP RTP Audio"); } else if (!strcmp(session_media->stream_type, STR_VIDEO) && @@ -555,6 +585,13 @@ static void process_ice_attributes(struct ast_sip_session *session, struct ast_s continue; } + if (session->endpoint->rtcp_mux && session_media->remote_rtcp_mux && candidate.id > 1) { + /* Remote side may have offered RTP and RTCP candidates. However, if we're using RTCP MUX, + * then we should ignore RTCP candidates. + */ + continue; + } + candidate.foundation = foundation; candidate.transport = transport; @@ -851,6 +888,26 @@ static int setup_media_encryption(struct ast_sip_session *session, return 0; } +static void set_ice_components(struct ast_sip_session *session, struct ast_sip_session_media *session_media) +{ + struct ast_rtp_engine_ice *ice; + + ast_assert(session_media->rtp != NULL); + + ice = ast_rtp_instance_get_ice(session_media->rtp); + if (!session->endpoint->media.rtp.ice_support || !ice) { + return; + } + + if (session->endpoint->rtcp_mux && session_media->remote_rtcp_mux) { + /* We both support RTCP mux. Only one ICE component necessary */ + ice->change_components(session_media->rtp, 1); + } else { + /* They either don't support RTCP mux or we don't know if they do yet. */ + ice->change_components(session_media->rtp, 2); + } +} + /*! \brief Function which negotiates an incoming media stream */ static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media, const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream) @@ -895,6 +952,11 @@ static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct return -1; } + session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(stream, "rtcp-mux", NULL) != NULL); + set_ice_components(session, session_media); + + enable_rtcp(session, session_media, stream); + res = setup_media_encryption(session, session_media, sdp, stream); if (res) { if (!session->endpoint->media.rtp.encryption_optimistic || @@ -1065,6 +1127,9 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as return -1; } + set_ice_components(session, session_media); + enable_rtcp(session, session_media, NULL); + if (!(media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media))) || !(media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)))) { return -1; @@ -1228,6 +1293,12 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as attr->name = STR_SENDRECV; media->attr[media->attr_count++] = attr; + /* If we've got rtcp-mux enabled, just unconditionally offer it in all SDPs */ + if (session->endpoint->rtcp_mux) { + attr = pjmedia_sdp_attr_create(pool, "rtcp-mux", NULL); + pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr); + } + /* Add the media stream to the SDP */ sdp->media[sdp->media_count++] = media; @@ -1262,6 +1333,11 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a return -1; } + session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(remote_stream, "rtcp-mux", NULL) != NULL); + set_ice_components(session, session_media); + + enable_rtcp(session, session_media, remote_stream); + res = setup_media_encryption(session, session_media, remote, remote_stream); if (!session->endpoint->media.rtp.encryption_optimistic && res) { /* If optimistic encryption is disabled and crypto should have been enabled but was not @@ -1293,7 +1369,9 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a return -1; } ast_channel_set_fd(session->channel, fdno, ast_rtp_instance_fd(session_media->rtp, 0)); - ast_channel_set_fd(session->channel, fdno + 1, ast_rtp_instance_fd(session_media->rtp, 1)); + if (!session->endpoint->rtcp_mux || !session_media->remote_rtcp_mux) { + ast_channel_set_fd(session->channel, fdno + 1, ast_rtp_instance_fd(session_media->rtp, 1)); + } /* If ICE support is enabled find all the needed attributes */ process_ice_attributes(session, session_media, remote, remote_stream); @@ -1387,10 +1465,11 @@ static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struc ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID); /* Is the address within the SDP inside the same network? */ - if (ast_apply_ha(transport_state->localnet, &addr) == AST_SENSE_ALLOW) { + if (transport_state->localnet + && ast_apply_ha(transport_state->localnet, &addr) == AST_SENSE_ALLOW) { return; } - + ast_debug(5, "Setting media address to %s\n", transport->external_media_address); pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address); } diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c index 3c4f102f8..efdce8aa2 100644 --- a/res/res_pjsip_session.c +++ b/res/res_pjsip_session.c @@ -973,32 +973,10 @@ int ast_sip_session_refresh(struct ast_sip_session *session, return 0; } -/*! - * \internal - * \brief Wrapper for pjsip_inv_send_msg - * - * This function (re)sets the transport before sending to catch cases - * where the transport might have changed. - * - * If pjproject gives us the ability to resend, we'll only reset the transport - * if PJSIP_ETPNOTAVAIL is returned from send. - * - * \returns pj_status_t - */ -static pj_status_t internal_pjsip_inv_send_msg(pjsip_inv_session *inv, const char *transport_name, pjsip_tx_data *tdata) -{ - pjsip_tpselector selector = { .type = PJSIP_TPSELECTOR_NONE, }; - - ast_sip_set_tpselector_from_transport_name(transport_name, &selector); - pjsip_dlg_set_transport(inv->dlg, &selector); - - return pjsip_inv_send_msg(inv, tdata); -} - void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata) { handle_outgoing_response(session, tdata); - internal_pjsip_inv_send_msg(session->inv_session, session->endpoint->transport, tdata); + pjsip_inv_send_msg(session->inv_session, tdata); return; } @@ -1229,7 +1207,7 @@ void ast_sip_session_send_request_with_cb(struct ast_sip_session *session, pjsip MOD_DATA_ON_RESPONSE, on_response); handle_outgoing_request(session, tdata); - internal_pjsip_inv_send_msg(session->inv_session, session->endpoint->transport, tdata); + pjsip_inv_send_msg(session->inv_session, tdata); return; } @@ -2049,7 +2027,7 @@ static pjsip_inv_session *pre_session_setup(pjsip_rx_data *rdata, const struct a if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) != PJ_SUCCESS) { pjsip_inv_terminate(inv_session, 500, PJ_FALSE); } - internal_pjsip_inv_send_msg(inv_session, endpoint->transport, tdata); + pjsip_inv_send_msg(inv_session, tdata); return NULL; } return inv_session; @@ -2218,7 +2196,7 @@ static void handle_new_invite_request(pjsip_rx_data *rdata) if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) { pjsip_inv_terminate(inv_session, 500, PJ_FALSE); } else { - internal_pjsip_inv_send_msg(inv_session, endpoint->transport, tdata); + pjsip_inv_send_msg(inv_session, tdata); } } return; @@ -2230,7 +2208,7 @@ static void handle_new_invite_request(pjsip_rx_data *rdata) if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) { pjsip_inv_terminate(inv_session, 500, PJ_FALSE); } else { - internal_pjsip_inv_send_msg(inv_session, endpoint->transport, tdata); + pjsip_inv_send_msg(inv_session, tdata); } #ifdef HAVE_PJSIP_INV_SESSION_REF pjsip_inv_dec_ref(inv_session); @@ -2243,7 +2221,7 @@ static void handle_new_invite_request(pjsip_rx_data *rdata) if (pjsip_inv_initial_answer(inv_session, rdata, 500, NULL, NULL, &tdata) == PJ_SUCCESS) { pjsip_inv_terminate(inv_session, 500, PJ_FALSE); } else { - internal_pjsip_inv_send_msg(inv_session, endpoint->transport, tdata); + pjsip_inv_send_msg(inv_session, tdata); } #ifdef HAVE_PJSIP_INV_SESSION_REF pjsip_inv_dec_ref(inv_session); @@ -3112,7 +3090,10 @@ static void session_outgoing_nat_hook(pjsip_tx_data *tdata, struct ast_sip_trans ast_copy_pj_str(host, &sdp->conn->addr, sizeof(host)); ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID); - if (ast_apply_ha(transport_state->localnet, &addr) != AST_SENSE_ALLOW) { + if (!transport_state->localnet + || (transport_state->localnet + && ast_apply_ha(transport_state->localnet, &addr) == AST_SENSE_ALLOW)) { + ast_debug(5, "Setting external media address to %s\n", transport->external_media_address); pj_strdup2(tdata->pool, &sdp->conn->addr, transport->external_media_address); } } diff --git a/res/res_pjsip_t38.c b/res/res_pjsip_t38.c index 0787f0763..16d50cd27 100644 --- a/res/res_pjsip_t38.c +++ b/res/res_pjsip_t38.c @@ -869,10 +869,11 @@ static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struc ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID); /* Is the address within the SDP inside the same network? */ - if (ast_apply_ha(transport_state->localnet, &addr) == AST_SENSE_ALLOW) { + if (transport_state->localnet + && ast_apply_ha(transport_state->localnet, &addr) == AST_SENSE_ALLOW) { return; } - + ast_debug(5, "Setting media address to %s\n", transport->external_media_address); pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address); } diff --git a/res/res_rtp_asterisk.c b/res/res_rtp_asterisk.c index 346db604c..d681fea02 100644 --- a/res/res_rtp_asterisk.c +++ b/res/res_rtp_asterisk.c @@ -331,6 +331,7 @@ struct ast_rtp { struct ao2_container *ice_active_remote_candidates; /*!< The remote ICE candidates */ struct ao2_container *ice_proposed_remote_candidates; /*!< Incoming remote ICE candidates for new session */ struct ast_sockaddr ice_original_rtp_addr; /*!< rtp address that ICE started on first session */ + unsigned int ice_num_components; /*!< The number of ICE components */ #endif #ifdef HAVE_OPENSSL_SRTP @@ -419,6 +420,7 @@ struct ast_rtcp { * own address every time */ char *local_addr_str; + enum ast_rtp_instance_rtcp type; }; struct rtp_red { @@ -660,6 +662,22 @@ static int ice_reset_session(struct ast_rtp_instance *instance) pj_ice_sess_change_role(rtp->ice, role); } + /* If we only have one component now, and we previously set up TURN for RTCP, + * we need to destroy that TURN socket. + */ + if (rtp->ice_num_components == 1 && rtp->turn_rtcp) { + struct timeval wait = ast_tvadd(ast_tvnow(), ast_samp2tv(TURN_STATE_WAIT_TIME, 1000)); + struct timespec ts = { .tv_sec = wait.tv_sec, .tv_nsec = wait.tv_usec * 1000, }; + + ast_mutex_lock(&rtp->lock); + pj_turn_sock_destroy(rtp->turn_rtcp); + rtp->turn_state = PJ_TURN_STATE_NULL; + while (rtp->turn_state != PJ_TURN_STATE_DESTROYING) { + ast_cond_timedwait(&rtp->cond, &rtp->lock, &ts); + } + ast_mutex_unlock(&rtp->lock); + } + return res; } @@ -775,11 +793,12 @@ static void ast_rtp_ice_start(struct ast_rtp_instance *instance) ast_log(LOG_WARNING, "No RTP candidates; skipping ICE checklist (%p)\n", instance); } - if (!has_rtcp) { + /* If we're only dealing with one ICE component, then we don't care about the lack of RTCP candidates */ + if (!has_rtcp && rtp->ice_num_components > 1) { ast_log(LOG_WARNING, "No RTCP candidates; skipping ICE checklist (%p)\n", instance); } - if (has_rtp && has_rtcp) { + if (has_rtp && (has_rtcp || rtp->ice_num_components == 1)) { pj_status_t res = pj_ice_sess_create_check_list(rtp->ice, &ufrag, &passwd, cand_cnt, &candidates[0]); char reason[80]; @@ -1271,6 +1290,21 @@ static char *generate_random_string(char *buf, size_t size) return buf; } +static void ast_rtp_ice_change_components(struct ast_rtp_instance *instance, int num_components) +{ + struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); + + /* Don't do anything if ICE is unsupported or if we're not changing the + * number of components + */ + if (!icesupport || !rtp->ice || rtp->ice_num_components == num_components) { + return; + } + + rtp->ice_num_components = num_components; + ice_reset_session(instance); +} + /* ICE RTP Engine interface declaration */ static struct ast_rtp_engine_ice ast_rtp_ice = { .set_authentication = ast_rtp_ice_set_authentication, @@ -1283,6 +1317,7 @@ static struct ast_rtp_engine_ice ast_rtp_ice = { .ice_lite = ast_rtp_ice_lite, .set_role = ast_rtp_ice_set_role, .turn_request = ast_rtp_ice_turn_request, + .change_components = ast_rtp_ice_change_components, }; #endif @@ -1542,6 +1577,7 @@ static int ast_rtp_dtls_active(struct ast_rtp_instance *instance) static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance) { struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); + SSL *ssl = rtp->dtls.ssl; dtls_srtp_stop_timeout_timer(instance, rtp, 0); @@ -1559,7 +1595,7 @@ static void ast_rtp_dtls_stop(struct ast_rtp_instance *instance) if (rtp->rtcp) { dtls_srtp_stop_timeout_timer(instance, rtp, 1); - if (rtp->rtcp->dtls.ssl) { + if (rtp->rtcp->dtls.ssl && (rtp->rtcp->dtls.ssl != ssl)) { SSL_free(rtp->rtcp->dtls.ssl); rtp->rtcp->dtls.ssl = NULL; ast_mutex_destroy(&rtp->rtcp->dtls.lock); @@ -1787,7 +1823,7 @@ static void ast_rtp_on_ice_complete(pj_ice_sess *ice, pj_status_t status) #ifdef HAVE_OPENSSL_SRTP dtls_perform_handshake(instance, &rtp->dtls, 0); - if (rtp->rtcp) { + if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { dtls_perform_handshake(instance, &rtp->rtcp->dtls, 1); } #endif @@ -2027,7 +2063,7 @@ static int dtls_srtp_renegotiate(const void *data) SSL_do_handshake(rtp->dtls.ssl); dtls_srtp_check_pending(instance, rtp, 0); - if (rtp->rtcp && rtp->rtcp->dtls.ssl) { + if (rtp->rtcp && rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) { SSL_renegotiate(rtp->rtcp->dtls.ssl); SSL_do_handshake(rtp->rtcp->dtls.ssl); dtls_srtp_check_pending(instance, rtp, 1); @@ -2618,7 +2654,7 @@ static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *ad passwd = pj_str(rtp->local_passwd); /* Create an ICE session for ICE negotiation */ - if (pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, 2, + if (pj_ice_sess_create(&stun_config, NULL, PJ_ICE_SESS_ROLE_UNKNOWN, rtp->ice_num_components, &ast_rtp_ice_sess_cb, &ufrag, &passwd, NULL, &rtp->ice) == PJ_SUCCESS) { /* Make this available for the callbacks */ rtp->ice->user_data = instance; @@ -2627,9 +2663,10 @@ static int ice_create(struct ast_rtp_instance *instance, struct ast_sockaddr *ad rtp_add_candidates_to_ice(instance, rtp, addr, port, AST_RTP_ICE_COMPONENT_RTP, TRANSPORT_SOCKET_RTP); - /* Only add the RTCP candidates to ICE when replacing the session. New sessions + /* Only add the RTCP candidates to ICE when replacing the session and if + * the ICE session contains more than just an RTP component. New sessions * handle this in a separate part of the setup phase */ - if (replace && rtp->rtcp) { + if (replace && rtp->rtcp && rtp->ice_num_components > 1) { rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us, ast_sockaddr_port(&rtp->rtcp->us), AST_RTP_ICE_COMPONENT_RTCP, TRANSPORT_SOCKET_RTCP); @@ -2714,6 +2751,7 @@ static int ast_rtp_new(struct ast_rtp_instance *instance, #ifdef HAVE_PJPROJECT /* Create an ICE session for ICE negotiation */ if (icesupport) { + rtp->ice_num_components = 2; ast_debug(3, "Creating ICE session %s (%d) for RTP instance '%p'\n", ast_sockaddr_stringify(addr), x, instance); if (ice_create(instance, addr, x, 0)) { ast_log(LOG_NOTICE, "Failed to start ICE session\n"); @@ -2723,7 +2761,6 @@ static int ast_rtp_new(struct ast_rtp_instance *instance, } } #endif - /* Record any information we may need */ rtp->sched = sched; @@ -4154,63 +4191,21 @@ static void update_lost_stats(struct ast_rtp *rtp, unsigned int lost_packets) rtp->rtcp->reported_normdev_lost = reported_normdev_lost_current; } -static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) +static struct ast_frame *ast_rtcp_interpret(struct ast_rtp_instance *instance, const unsigned char *rtcpdata, size_t size, struct ast_sockaddr *addr) { struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); - struct ast_sockaddr addr; - unsigned char rtcpdata[8192 + AST_FRIENDLY_OFFSET]; - unsigned int *rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET); - int res, packetwords, position = 0; + unsigned int *rtcpheader = (unsigned int *)(rtcpdata); + int packetwords, position = 0; int report_counter = 0; struct ast_rtp_rtcp_report_block *report_block; struct ast_frame *f = &ast_null_frame; - /* Read in RTCP data from the socket */ - if ((res = rtcp_recvfrom(instance, rtcpdata + AST_FRIENDLY_OFFSET, - sizeof(rtcpdata) - AST_FRIENDLY_OFFSET, - 0, &addr)) < 0) { - ast_assert(errno != EBADF); - if (errno != EAGAIN) { - ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n", - (errno) ? strerror(errno) : "Unspecified"); - return NULL; - } - return &ast_null_frame; - } - - /* If this was handled by the ICE session don't do anything further */ - if (!res) { - return &ast_null_frame; - } - - if (!*(rtcpdata + AST_FRIENDLY_OFFSET)) { - struct sockaddr_in addr_tmp; - struct ast_sockaddr addr_v4; - - if (ast_sockaddr_is_ipv4(&addr)) { - ast_sockaddr_to_sin(&addr, &addr_tmp); - } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) { - ast_debug(1, "Using IPv6 mapped address %s for STUN\n", - ast_sockaddr_stringify(&addr)); - ast_sockaddr_to_sin(&addr_v4, &addr_tmp); - } else { - ast_debug(1, "Cannot do STUN for non IPv4 address %s\n", - ast_sockaddr_stringify(&addr)); - return &ast_null_frame; - } - if ((ast_stun_handle_packet(rtp->rtcp->s, &addr_tmp, rtcpdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT)) { - ast_sockaddr_from_sin(&addr, &addr_tmp); - ast_sockaddr_copy(&rtp->rtcp->them, &addr); - } - return &ast_null_frame; - } - - packetwords = res / 4; + packetwords = size / 4; if (ast_rtp_instance_get_prop(instance, AST_RTP_PROPERTY_NAT)) { /* Send to whoever sent to us */ - if (ast_sockaddr_cmp(&rtp->rtcp->them, &addr)) { - ast_sockaddr_copy(&rtp->rtcp->them, &addr); + if (ast_sockaddr_cmp(&rtp->rtcp->them, addr)) { + ast_sockaddr_copy(&rtp->rtcp->them, addr); if (rtpdebug) { ast_debug(0, "RTCP NAT: Got RTCP from other end. Now sending to address %s\n", ast_sockaddr_stringify(&rtp->rtcp->them)); @@ -4218,7 +4213,7 @@ static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) } } - ast_debug(1, "Got RTCP report of %d bytes\n", res); + ast_debug(1, "Got RTCP report of %zu bytes\n", size); while (position < packetwords) { int i, pt, rc; @@ -4246,9 +4241,9 @@ static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) return &ast_null_frame; } - if (rtcp_debug_test_addr(&addr)) { + if (rtcp_debug_test_addr(addr)) { ast_verbose("\n\nGot RTCP from %s\n", - ast_sockaddr_stringify(&addr)); + ast_sockaddr_stringify(addr)); ast_verbose("PT: %d(%s)\n", pt, (pt == RTCP_PT_SR) ? "Sender Report" : (pt == RTCP_PT_RR) ? "Receiver Report" : (pt == RTCP_PT_FUR) ? "H.261 FUR" : "Unknown"); @@ -4271,7 +4266,7 @@ static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) (unsigned int)ntohl(rtcpheader[i + 1]), &rtcp_report->sender_information.ntp_timestamp); rtcp_report->sender_information.rtp_timestamp = ntohl(rtcpheader[i + 2]); - if (rtcp_debug_test_addr(&addr)) { + if (rtcp_debug_test_addr(addr)) { ast_verbose("NTP timestamp: %u.%06u\n", (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_sec, (unsigned int)rtcp_report->sender_information.ntp_timestamp.tv_usec); @@ -4303,7 +4298,7 @@ static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) report_block->dlsr = ntohl(rtcpheader[i + 5]); if (report_block->lsr && update_rtt_stats(rtp, report_block->lsr, report_block->dlsr) - && rtcp_debug_test_addr(&addr)) { + && rtcp_debug_test_addr(addr)) { struct timeval now; unsigned int lsr_now, lsw, msw; gettimeofday(&now, NULL); @@ -4320,7 +4315,7 @@ static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) update_lost_stats(rtp, report_block->lost_count.packets); rtp->rtcp->reported_jitter_count++; - if (rtcp_debug_test_addr(&addr)) { + if (rtcp_debug_test_addr(addr)) { ast_verbose(" Fraction lost: %d\n", report_block->lost_count.fraction); ast_verbose(" Packets lost so far: %u\n", report_block->lost_count.packets); ast_verbose(" Highest sequence number: %u\n", report_block->highest_seq_no & 0x0000ffff); @@ -4348,7 +4343,7 @@ static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) case RTCP_PT_FUR: /* Handle RTCP FIR as FUR */ case RTCP_PT_PSFB: - if (rtcp_debug_test_addr(&addr)) { + if (rtcp_debug_test_addr(addr)) { ast_verbose("Received an RTCP Fast Update Request\n"); } rtp->f.frametype = AST_FRAME_CONTROL; @@ -4360,13 +4355,13 @@ static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) f = &rtp->f; break; case RTCP_PT_SDES: - if (rtcp_debug_test_addr(&addr)) { + if (rtcp_debug_test_addr(addr)) { ast_verbose("Received an SDES from %s\n", ast_sockaddr_stringify(&rtp->rtcp->them)); } break; case RTCP_PT_BYE: - if (rtcp_debug_test_addr(&addr)) { + if (rtcp_debug_test_addr(addr)) { ast_verbose("Received a BYE from %s\n", ast_sockaddr_stringify(&rtp->rtcp->them)); } @@ -4381,6 +4376,58 @@ static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) rtp->rtcp->rtcp_info = 1; return f; + +} + +static struct ast_frame *ast_rtcp_read(struct ast_rtp_instance *instance) +{ + struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); + struct ast_sockaddr addr; + unsigned char rtcpdata[8192 + AST_FRIENDLY_OFFSET]; + unsigned char *read_area = rtcpdata + AST_FRIENDLY_OFFSET; + size_t read_area_size = sizeof(rtcpdata) - AST_FRIENDLY_OFFSET; + int res; + + /* Read in RTCP data from the socket */ + if ((res = rtcp_recvfrom(instance, read_area, read_area_size, + 0, &addr)) < 0) { + ast_assert(errno != EBADF); + if (errno != EAGAIN) { + ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n", + (errno) ? strerror(errno) : "Unspecified"); + return NULL; + } + return &ast_null_frame; + } + + /* If this was handled by the ICE session don't do anything further */ + if (!res) { + return &ast_null_frame; + } + + if (!*read_area) { + struct sockaddr_in addr_tmp; + struct ast_sockaddr addr_v4; + + if (ast_sockaddr_is_ipv4(&addr)) { + ast_sockaddr_to_sin(&addr, &addr_tmp); + } else if (ast_sockaddr_ipv4_mapped(&addr, &addr_v4)) { + ast_debug(1, "Using IPv6 mapped address %s for STUN\n", + ast_sockaddr_stringify(&addr)); + ast_sockaddr_to_sin(&addr_v4, &addr_tmp); + } else { + ast_debug(1, "Cannot do STUN for non IPv4 address %s\n", + ast_sockaddr_stringify(&addr)); + return &ast_null_frame; + } + if ((ast_stun_handle_packet(rtp->rtcp->s, &addr_tmp, read_area, res, NULL, NULL) == AST_STUN_ACCEPT)) { + ast_sockaddr_from_sin(&addr, &addr_tmp); + ast_sockaddr_copy(&rtp->rtcp->them, &addr); + } + return &ast_null_frame; + } + + return ast_rtcp_interpret(instance, read_area, res, &addr); } static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int *rtpheader, int len, int hdrlen) @@ -4487,19 +4534,54 @@ static int bridge_p2p_rtp_write(struct ast_rtp_instance *instance, unsigned int return 0; } +static int rtcp_mux(struct ast_rtp *rtp, const unsigned char *packet) +{ + uint8_t version; + uint8_t pt; + uint8_t m; + + if (!rtp->rtcp || rtp->rtcp->type != AST_RTP_INSTANCE_RTCP_MUX) { + return 0; + } + + version = (packet[0] & 0XC0) >> 6; + if (version == 0) { + /* version 0 indicates this is a STUN packet and shouldn't + * be interpreted as a possible RTCP packet + */ + return 0; + } + + /* The second octet of a packet will be one of the following: + * For RTP: The marker bit (1 bit) and the RTP payload type (7 bits) + * For RTCP: The payload type (8) + * + * RTP has a forbidden range of payload types (64-95) since these + * will conflict with RTCP payload numbers if the marker bit is set. + */ + m = packet[1] & 0x80; + pt = packet[1] & 0x7F; + if (m && pt >= 64 && pt <= 95) { + return 1; + } + return 0; +} + static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtcp) { struct ast_rtp *rtp = ast_rtp_instance_get_data(instance); struct ast_sockaddr addr; int res, hdrlen = 12, version, payloadtype, padding, mark, ext, cc, prev_seqno; - unsigned int *rtpheader = (unsigned int*)(rtp->rawdata + AST_FRIENDLY_OFFSET), seqno, ssrc, timestamp; + unsigned char *read_area = rtp->rawdata + AST_FRIENDLY_OFFSET; + size_t read_area_size = sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET; + unsigned int *rtpheader = (unsigned int*)(read_area), seqno, ssrc, timestamp; RAII_VAR(struct ast_rtp_payload_type *, payload, NULL, ao2_cleanup); struct ast_sockaddr remote_address = { {0,} }; struct frame_list frames; /* If this is actually RTCP let's hop on over and handle it */ if (rtcp) { - if (rtp->rtcp) { + if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { return ast_rtcp_read(instance); } return &ast_null_frame; @@ -4511,8 +4593,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc } /* Actually read in the data from the socket */ - if ((res = rtp_recvfrom(instance, rtp->rawdata + AST_FRIENDLY_OFFSET, - sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET, 0, + if ((res = rtp_recvfrom(instance, read_area, read_area_size, 0, &addr)) < 0) { ast_assert(errno != EBADF); if (errno != EAGAIN) { @@ -4528,12 +4609,17 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc return &ast_null_frame; } + /* This could be a multiplexed RTCP packet. If so, be sure to interpret it correctly */ + if (rtcp_mux(rtp, read_area)) { + return ast_rtcp_interpret(instance, read_area, res, &addr); + } + /* Make sure the data that was read in is actually enough to make up an RTP packet */ if (res < hdrlen) { /* If this is a keepalive containing only nulls, don't bother with a warning */ int i; for (i = 0; i < res; ++i) { - if (rtp->rawdata[AST_FRIENDLY_OFFSET + i] != '\0') { + if (read_area[i] != '\0') { ast_log(LOG_WARNING, "RTP Read too short\n"); return &ast_null_frame; } @@ -4560,7 +4646,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc ast_sockaddr_stringify(&addr)); return &ast_null_frame; } - if ((ast_stun_handle_packet(rtp->s, &addr_tmp, rtp->rawdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == AST_STUN_ACCEPT) && + if ((ast_stun_handle_packet(rtp->s, &addr_tmp, read_area, res, NULL, NULL) == AST_STUN_ACCEPT) && ast_sockaddr_isnull(&remote_address)) { ast_sockaddr_from_sin(&addr, &addr_tmp); ast_rtp_instance_set_remote_address(instance, &addr); @@ -4609,7 +4695,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc /* do not update the originally given address, but only the remote */ ast_rtp_instance_set_incoming_source_address(instance, &addr); ast_sockaddr_copy(&remote_address, &addr); - if (rtp->rtcp) { + if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { ast_sockaddr_copy(&rtp->rtcp->them, &addr); ast_sockaddr_set_port(&rtp->rtcp->them, ast_sockaddr_port(&addr) + 1); } @@ -4676,7 +4762,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc /* Remove any padding bytes that may be present */ if (padding) { - res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1]; + res -= read_area[res - 1]; } /* Skip over any CSRC fields */ @@ -4750,11 +4836,11 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc * by passing the pointer to the frame list to it so that the method * can append frames to the list as needed. */ - process_dtmf_rfc2833(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark, &frames); + process_dtmf_rfc2833(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark, &frames); } else if (payload->rtp_code == AST_RTP_CISCO_DTMF) { - f = process_dtmf_cisco(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark); + f = process_dtmf_cisco(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark); } else if (payload->rtp_code == AST_RTP_CN) { - f = process_cn_rfc3389(instance, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark); + f = process_cn_rfc3389(instance, read_area + hdrlen, res - hdrlen, seqno, timestamp, &addr, payloadtype, mark); } else { ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, @@ -4810,7 +4896,7 @@ static struct ast_frame *ast_rtp_read(struct ast_rtp_instance *instance, int rtc rtp->f.src = "RTP"; rtp->f.mallocd = 0; rtp->f.datalen = res - hdrlen; - rtp->f.data.ptr = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET; + rtp->f.data.ptr = read_area + hdrlen; rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET; rtp->f.seqno = seqno; @@ -4921,19 +5007,31 @@ static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_pro if (value) { struct ast_sockaddr local_addr; - if (rtp->rtcp) { + if (rtp->rtcp && rtp->rtcp->type == value) { ast_debug(1, "Ignoring duplicate RTCP property on RTP instance '%p'\n", instance); return; } - /* Setup RTCP to be activated on the next RTP write */ - if (!(rtp->rtcp = ast_calloc(1, sizeof(*rtp->rtcp)))) { - return; + + if (!rtp->rtcp) { + rtp->rtcp = ast_calloc(1, sizeof(*rtp->rtcp)); + if (!rtp->rtcp) { + return; + } + rtp->rtcp->s = -1; +#ifdef HAVE_OPENSSL_SRTP + rtp->rtcp->dtls.timeout_timer = -1; +#endif + rtp->rtcp->schedid = -1; } + rtp->rtcp->type = value; + /* Grab the IP address and port we are going to use */ ast_rtp_instance_get_local_address(instance, &rtp->rtcp->us); - ast_sockaddr_set_port(&rtp->rtcp->us, - ast_sockaddr_port(&rtp->rtcp->us) + 1); + if (value == AST_RTP_INSTANCE_RTCP_STANDARD) { + ast_sockaddr_set_port(&rtp->rtcp->us, + ast_sockaddr_port(&rtp->rtcp->us) + 1); + } ast_sockaddr_copy(&local_addr, &rtp->rtcp->us); if (!ast_find_ourip(&local_addr, &rtp->rtcp->us, 0)) { @@ -4943,6 +5041,7 @@ static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_pro ast_sockaddr_copy(&local_addr, &rtp->rtcp->us); } + ast_free(rtp->rtcp->local_addr_str); rtp->rtcp->local_addr_str = ast_strdup(ast_sockaddr_stringify(&local_addr)); if (!rtp->rtcp->local_addr_str) { ast_free(rtp->rtcp); @@ -4950,43 +5049,67 @@ static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_pro return; } - if ((rtp->rtcp->s = - create_new_socket("RTCP", - ast_sockaddr_is_ipv4(&rtp->rtcp->us) ? - AF_INET : - ast_sockaddr_is_ipv6(&rtp->rtcp->us) ? - AF_INET6 : -1)) < 0) { - ast_debug(1, "Failed to create a new socket for RTCP on instance '%p'\n", instance); - ast_free(rtp->rtcp->local_addr_str); - ast_free(rtp->rtcp); - rtp->rtcp = NULL; - return; - } - - /* Try to actually bind to the IP address and port we are going to use for RTCP, if this fails we have to bail out */ - if (ast_bind(rtp->rtcp->s, &rtp->rtcp->us)) { - ast_debug(1, "Failed to setup RTCP on RTP instance '%p'\n", instance); - close(rtp->rtcp->s); - ast_free(rtp->rtcp->local_addr_str); - ast_free(rtp->rtcp); - rtp->rtcp = NULL; - return; - } - - ast_debug(1, "Setup RTCP on RTP instance '%p'\n", instance); - rtp->rtcp->schedid = -1; + if (value == AST_RTP_INSTANCE_RTCP_STANDARD) { + /* We're either setting up RTCP from scratch or + * switching from MUX. Either way, we won't have + * a socket set up, and we need to set it up + */ + if ((rtp->rtcp->s = + create_new_socket("RTCP", + ast_sockaddr_is_ipv4(&rtp->rtcp->us) ? + AF_INET : + ast_sockaddr_is_ipv6(&rtp->rtcp->us) ? + AF_INET6 : -1)) < 0) { + ast_debug(1, "Failed to create a new socket for RTCP on instance '%p'\n", instance); + ast_free(rtp->rtcp->local_addr_str); + ast_free(rtp->rtcp); + rtp->rtcp = NULL; + return; + } + /* Try to actually bind to the IP address and port we are going to use for RTCP, if this fails we have to bail out */ + if (ast_bind(rtp->rtcp->s, &rtp->rtcp->us)) { + ast_debug(1, "Failed to setup RTCP on RTP instance '%p'\n", instance); + close(rtp->rtcp->s); + ast_free(rtp->rtcp->local_addr_str); + ast_free(rtp->rtcp); + rtp->rtcp = NULL; + return; + } #ifdef HAVE_PJPROJECT - if (rtp->ice) { - rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us, ast_sockaddr_port(&rtp->rtcp->us), AST_RTP_ICE_COMPONENT_RTCP, TRANSPORT_SOCKET_RTCP); - } + if (rtp->ice) { + rtp_add_candidates_to_ice(instance, rtp, &rtp->rtcp->us, ast_sockaddr_port(&rtp->rtcp->us), AST_RTP_ICE_COMPONENT_RTCP, TRANSPORT_SOCKET_RTCP); + } #endif - #ifdef HAVE_OPENSSL_SRTP - rtp->rtcp->dtls.timeout_timer = -1; - dtls_setup_rtcp(instance); + dtls_setup_rtcp(instance); #endif + } else { + struct ast_sockaddr addr; + /* RTCPMUX uses the same socket as RTP. If we were previously using standard RTCP + * then close the socket we previously created. + * + * It may seem as though there is a possible race condition here where we might try + * to close the RTCP socket while it is being used to send data. However, this is not + * a problem in practice since setting and adjusting of RTCP properties happens prior + * to activating RTP. It is not until RTP is activated that timers start for RTCP + * transmission + */ + if (rtp->rtcp->s > -1) { + close(rtp->rtcp->s); + } + rtp->rtcp->s = rtp->s; + ast_rtp_instance_get_remote_address(instance, &addr); + ast_sockaddr_copy(&rtp->rtcp->them, &addr); +#ifdef HAVE_OPENSSL_SRTP + if (rtp->rtcp->dtls.ssl) { + SSL_free(rtp->rtcp->dtls.ssl); + } + rtp->rtcp->dtls.ssl = rtp->dtls.ssl; +#endif + } + ast_debug(1, "Setup RTCP on RTP instance '%p'\n", instance); return; } else { if (rtp->rtcp) { @@ -5001,9 +5124,11 @@ static void ast_rtp_prop_set(struct ast_rtp_instance *instance, enum ast_rtp_pro } rtp->rtcp->schedid = -1; } - close(rtp->rtcp->s); + if (rtp->rtcp->s > -1 && rtp->rtcp->s != rtp->s) { + close(rtp->rtcp->s); + } #ifdef HAVE_OPENSSL_SRTP - if (rtp->rtcp->dtls.ssl) { + if (rtp->rtcp->dtls.ssl && rtp->rtcp->dtls.ssl != rtp->dtls.ssl) { SSL_free(rtp->rtcp->dtls.ssl); } #endif @@ -5045,10 +5170,12 @@ static void ast_rtp_remote_address_set(struct ast_rtp_instance *instance, struct ast_debug(1, "Setting RTCP address on RTP instance '%p'\n", instance); ast_sockaddr_copy(&rtp->rtcp->them, addr); if (!ast_sockaddr_isnull(addr)) { - ast_sockaddr_set_port(&rtp->rtcp->them, ast_sockaddr_port(addr) + 1); + if (rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { + ast_sockaddr_set_port(&rtp->rtcp->them, ast_sockaddr_port(addr) + 1); - /* Update the local RTCP address with what is being used */ - ast_sockaddr_set_port(&local, ast_sockaddr_port(&local) + 1); + /* Update the local RTCP address with what is being used */ + ast_sockaddr_set_port(&local, ast_sockaddr_port(&local) + 1); + } ast_sockaddr_copy(&rtp->rtcp->us, &local); ast_free(rtp->rtcp->local_addr_str); @@ -5336,7 +5463,7 @@ static int ast_rtp_activate(struct ast_rtp_instance *instance) dtls_perform_handshake(instance, &rtp->dtls, 0); - if (rtp->rtcp) { + if (rtp->rtcp && rtp->rtcp->type == AST_RTP_INSTANCE_RTCP_STANDARD) { dtls_perform_handshake(instance, &rtp->rtcp->dtls, 1); } |