Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues when using ICE Trickle #4

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
3 changes: 2 additions & 1 deletion src/ice/ice_candidate.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ int nr_ice_candidate_create(nr_ice_ctx *ctx,nr_ice_component *comp,nr_ice_socket
break;

case PEER_REFLEXIVE:
snprintf(label, sizeof(label), "prflx");
snprintf(label, sizeof(label), "prflx(%s)", cand->addr.as_string);
break;

default:
Expand Down Expand Up @@ -244,6 +244,7 @@ int nr_ice_peer_peer_rflx_candidate_create(nr_ice_ctx *ctx,char *label, nr_ice_c
cand->component_id=comp->component_id;
cand->component=comp;
cand->stream=comp->stream;
// nr_concat_strings(&cand->label, r_strdup(label), "(", addr->as_string, ")");


r_log(LOG_ICE,LOG_DEBUG,"ICE(%s)/CAND(%s): creating candidate with type %s",
Expand Down
23 changes: 22 additions & 1 deletion src/ice/ice_candidate_pair.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,8 @@ void nr_ice_candidate_pair_set_state(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pa

void nr_ice_candidate_pair_dump_state(nr_ice_cand_pair *pair, int log_level)
{
r_log(LOG_ICE,log_level,"CAND-PAIR(%s): pair %s: state=%s, priority=0x%llx\n",pair->codeword,pair->as_string,nr_ice_cand_pair_states[pair->state],pair->priority);
r_log(LOG_ICE,log_level,"CAND-PAIR(%s): pair %s %s (%s-%s): state=%s, priority=0x%llx\n",
pair->pctx->label, pair->codeword,pair->as_string,pair->local->codeword,pair->remote->codeword,nr_ice_cand_pair_states[pair->state],pair->priority);
}


Expand Down Expand Up @@ -621,6 +622,26 @@ int nr_ice_candidate_pair_insert(nr_ice_cand_pair_head *head,nr_ice_cand_pair *p
return(0);
}

void nr_ice_candidate_pair_check_redundancy(nr_ice_cand_pair_head *head,nr_ice_cand_pair *pair)
{
nr_ice_cand_pair *c1;

c1=TAILQ_FIRST(head);
while(c1){
if (pair->remote->type != PEER_REFLEXIVE && c1->remote->type == PEER_REFLEXIVE &&
!nr_transport_addr_cmp(&c1->local->addr,&pair->local->addr,NR_TRANSPORT_ADDR_CMP_MODE_ALL) &&
!nr_transport_addr_cmp(&c1->remote->addr,&pair->remote->addr,NR_TRANSPORT_ADDR_CMP_MODE_ALL)) {
// We found a redundant pair with a remote Peer Reflexive.
c1->priority = pair->priority;
nr_ice_candidate_pair_cancel(pair->pctx, pair, 0);
r_log(LOG_ICE,LOG_WARNING,"ICE-PEER(%s)/STREAM(%s)/CAND-PAIR(%s)/COMP(%d): Redundancy with: %s, %s == %s",pair->pctx->label,pair->local->stream->label,pair->codeword,pair->remote->component->component_id,c1->codeword, pair->remote->addr.as_string, c1->remote->addr.as_string);
}

c1=TAILQ_NEXT(c1,check_queue_entry);
}
pair=TAILQ_NEXT(pair,check_queue_entry);
}

void nr_ice_candidate_pair_restart_stun_nominated_cb(NR_SOCKET s, int how, void *cb_arg)
{
nr_ice_cand_pair *pair=cb_arg;
Expand Down
1 change: 1 addition & 0 deletions src/ice/ice_candidate_pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void nr_ice_candidate_pair_cancel(nr_ice_peer_ctx *pctx,nr_ice_cand_pair *pair,
int nr_ice_candidate_pair_select(nr_ice_cand_pair *pair);
int nr_ice_candidate_pair_do_triggered_check(nr_ice_peer_ctx *pctx, nr_ice_cand_pair *pair);
int nr_ice_candidate_pair_insert(nr_ice_cand_pair_head *head,nr_ice_cand_pair *pair);
void nr_ice_candidate_pair_check_redundancy(nr_ice_cand_pair_head *head,nr_ice_cand_pair *pair);
int nr_ice_candidate_pair_trigger_check_append(nr_ice_cand_pair_head *head,nr_ice_cand_pair *pair);
void nr_ice_candidate_pair_restart_stun_nominated_cb(NR_SOCKET s, int how, void *cb_arg);
int nr_ice_candidate_pair_destroy(nr_ice_cand_pair **pairp);
Expand Down
10 changes: 7 additions & 3 deletions src/ice/ice_component.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static int nr_ice_component_get_port_from_range(struct nr_ice_ctx_ *ctx, uint16_
uint16_t min_port = ctx->min_port ? ctx->min_port : 49152;
uint16_t max_port = ctx->max_port ? ctx->max_port : 65535;
if (max_port < min_port)
ABORT(r);
ABORT(-1);
const unsigned int range = 1 + max_port - min_port;
const unsigned int buckets = RAND_MAX / range;
const unsigned int limit = buckets * range;
Expand Down Expand Up @@ -985,7 +985,9 @@ static int nr_ice_component_process_incoming_check(nr_ice_component *comp, nr_tr
}

/* Now make a peer reflexive (remote) candidate */
if(r=nr_ice_peer_peer_rflx_candidate_create(comp->stream->pctx->ctx,"prflx",comp,&req->src_addr,&pcand)) {
char label[256];
snprintf(label, sizeof(label), "prflx(%s)", req->src_addr.as_string);
if(r=nr_ice_peer_peer_rflx_candidate_create(comp->stream->pctx->ctx,label,comp,&req->src_addr,&pcand)) {
*error=(r==R_NO_MEMORY)?500:400;
ABORT(r);
}
Expand Down Expand Up @@ -1740,6 +1742,8 @@ int nr_ice_component_insert_pair(nr_ice_component *pcomp, nr_ice_cand_pair *pair
if(r=nr_ice_candidate_pair_insert(&pair->remote->stream->check_list,pair))
ABORT(r);

nr_ice_candidate_pair_check_redundancy(&pair->remote->stream->check_list,pair);

pair_inserted=1;

/* Make sure the check timer is running, if the stream was previously
Expand Down Expand Up @@ -1818,7 +1822,7 @@ void nr_ice_component_dump_state(nr_ice_component *comp, int log_level)

cand=TAILQ_FIRST(&comp->candidates);
while(cand){
r_log(LOG_ICE,log_level,"ICE(%s)/ICE-STREAM(%s)/CAND(%s): %s",comp->ctx->label,comp->stream->label,cand->codeword,cand->label);
r_log(LOG_ICE,log_level,"ICE(%s)/ICE-STREAM(%s)/CAND(%s): %s, type: %s, priority:0x%llx",comp->ctx->label,comp->stream->label,cand->codeword,cand->label,nr_ice_candidate_type_names[cand->type],cand->priority);
cand=TAILQ_NEXT(cand,entry_comp);
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/ice/ice_media_stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ int nr_ice_media_stream_pair_candidates(nr_ice_peer_ctx *pctx,nr_ice_media_strea
pcomp=STAILQ_NEXT(pcomp,entry);
};

// We resort candidate pairs in case of redundancy.
nr_ice_media_stream_resort_check_list(pstream);

if (pstream->ice_state == NR_ICE_MEDIA_STREAM_UNPAIRED) {
nr_ice_media_stream_set_state(pstream, NR_ICE_MEDIA_STREAM_CHECKS_FROZEN);
}
Expand Down Expand Up @@ -1050,6 +1053,25 @@ void nr_ice_media_stream_role_change(nr_ice_media_stream *stream)
}
}

void nr_ice_media_stream_resort_check_list(nr_ice_media_stream *stream)
{
nr_ice_cand_pair *pair,*temp_pair;
nr_ice_cand_pair_head old_checklist;

/* Move check_list to old_checklist (not POD, have to do the hard way) */
TAILQ_INIT(&old_checklist);
TAILQ_FOREACH_SAFE(pair,&stream->check_list,check_queue_entry,temp_pair) {
TAILQ_REMOVE(&stream->check_list,pair,check_queue_entry);
TAILQ_INSERT_TAIL(&old_checklist,pair,check_queue_entry);
}

/* Re-insert into the check list */
TAILQ_FOREACH_SAFE(pair,&old_checklist,check_queue_entry,temp_pair) {
TAILQ_REMOVE(&old_checklist,pair,check_queue_entry);
nr_ice_candidate_pair_insert(&stream->check_list,pair);
}
}

int nr_ice_media_stream_find_pair(nr_ice_media_stream *str, nr_ice_candidate *lcand, nr_ice_candidate *rcand, nr_ice_cand_pair **pair)
{
nr_ice_cand_pair_head *head = &str->check_list;
Expand Down
1 change: 1 addition & 0 deletions src/ice/ice_media_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ int nr_ice_media_stream_get_consent_status(nr_ice_media_stream *stream, int comp
int nr_ice_media_stream_disable_component(nr_ice_media_stream *stream, int component_id);
int nr_ice_media_stream_pair_new_trickle_candidate(nr_ice_peer_ctx *pctx, nr_ice_media_stream *pstream, nr_ice_candidate *cand);
void nr_ice_media_stream_role_change(nr_ice_media_stream *stream);
void nr_ice_media_stream_resort_check_list(nr_ice_media_stream *stream);

#ifdef __cplusplus
}
Expand Down
3 changes: 3 additions & 0 deletions src/ice/ice_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "ice_ctx.h"
#include "ice_candidate.h"
#include "ice_reg.h"
#include "ice_util.h"

static void
skip_whitespace(char **str)
Expand Down Expand Up @@ -347,6 +348,8 @@ nr_ice_peer_candidate_from_attribute(nr_ice_ctx *ctx,char *orig,nr_ice_media_str

nr_ice_candidate_compute_codeword(cand);

// nr_concat_strings(&cand->label, nr_ice_candidate_type_names[cand->type], "(", r_strdup(orig), ")");

*candp=cand;

_status=0;
Expand Down
1 change: 1 addition & 0 deletions src/ice/ice_peer_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ void nr_ice_peer_ctx_dump_state(nr_ice_peer_ctx *pctx, int log_level)
stream=STAILQ_FIRST(&pctx->peer_streams);
while(stream){
nr_ice_media_stream_dump_state(pctx,stream,log_level);
stream=STAILQ_NEXT(stream,entry);
}
r_log(LOG_ICE,log_level,"==========================================");
}
Expand Down