diff --git a/src/ice/cand.c b/src/ice/cand.c index 4037dbc..89000e5 100644 --- a/src/ice/cand.c +++ b/src/ice/cand.c @@ -23,12 +23,11 @@ #include -static void cand_destructor(void *data) +static void cand_destructor(void *arg) { - struct cand *cand = data; + struct cand *cand = arg; list_unlink(&cand->le); - mem_deref(cand->foundation); mem_deref(cand->ifname); @@ -120,10 +119,8 @@ int icem_lcand_add(struct icem *icem, struct cand *base, enum cand_type type, struct cand *cand; int err; - if (!base) { - DEBUG_WARNING("icem add local candidate: no base\n"); + if (!base) return EINVAL; - } err = cand_alloc(&cand, icem, type, base->compid, ice_calc_prio(type, 0, base->compid), diff --git a/src/ice/candpair.c b/src/ice/candpair.c index cab4631..04087b1 100644 --- a/src/ice/candpair.c +++ b/src/ice/candpair.c @@ -21,14 +21,12 @@ #include -static void candpair_destructor(void *data) +static void candpair_destructor(void *arg) { - struct candpair *cp = data; + struct candpair *cp = arg; list_unlink(&cp->le); - mem_deref(cp->ct_conn); - mem_deref(cp->lcand); mem_deref(cp->rcand); } @@ -36,9 +34,7 @@ static void candpair_destructor(void *data) static bool sort_handler(struct le *le1, struct le *le2, void *arg) { - const struct candpair *cp1 = le1->data; - const struct candpair *cp2 = le2->data; - + const struct candpair *cp1 = le1->data, *cp2 = le2->data; (void)arg; return cp1->pprio >= cp2->pprio; @@ -138,7 +134,6 @@ int icem_candpair_clone(struct candpair **cpp, struct candpair *cp0, cp->def = cp0->def; cp->valid = cp0->valid; cp->nominated = cp0->nominated; - cp->use_cand = cp0->use_cand; cp->state = cp0->state; cp->pprio = cp0->pprio; cp->usec_sent = cp0->usec_sent; @@ -170,13 +165,6 @@ void icem_candpair_prio_order(struct list *lst) } -void icem_candpair_move(struct candpair *cp, struct list *list) -{ - list_unlink(&cp->le); - list_add_sorted(list, cp); -} - - /* cancel transaction */ void icem_candpair_cancel(struct candpair *cp) { @@ -200,7 +188,9 @@ void icem_candpair_make_valid(struct candpair *cp) cp->ertt = (long)(ice_get_usec() - cp->usec_sent); icem_candpair_set_state(cp, CANDPAIR_SUCCEEDED); - icem_candpair_move(cp, &cp->icem->validl); + + list_unlink(&cp->le); + list_add_sorted(&cp->icem->validl, cp); } @@ -211,6 +201,7 @@ void icem_candpair_failed(struct candpair *cp, int err, uint16_t scode) cp->err = err; cp->scode = scode; + cp->valid = false; icem_candpair_set_state(cp, CANDPAIR_FAILED); } @@ -373,14 +364,12 @@ int icem_candpair_debug(struct re_printf *pf, const struct candpair *cp) if (!cp) return 0; - err = re_hprintf(pf, "{%u} %10s {%c%c%c%c} %22llu %28H <---> %28H", + err = re_hprintf(pf, "{%u} %10s {%c%c%c} %28H <---> %28H", cp->lcand->compid, ice_candpair_state2name(cp->state), cp->def ? 'D' : ' ', cp->valid ? 'V' : ' ', cp->nominated ? 'N' : ' ', - cp->use_cand ? 'U' : ' ', - cp->pprio, icem_cand_print, cp->lcand, icem_cand_print, cp->rcand); diff --git a/src/ice/chklist.c b/src/ice/chklist.c index 115f6a9..eaeb741 100644 --- a/src/ice/chklist.c +++ b/src/ice/chklist.c @@ -147,7 +147,8 @@ int icem_checklist_form(struct icem *icem) return EINVAL; if (ICE_MODE_LITE == icem->ice->lmode) { - DEBUG_WARNING("Checklist form: only valid for full-mode\n"); + DEBUG_WARNING("%s: Checklist: only valid for full-mode\n", + icem->name); return EINVAL; } @@ -202,21 +203,22 @@ static void concluding_ice(struct icem_comp *comp) return; /* pick the best candidate pair, highest priority */ - icem_candpair_prio_order(&comp->icem->validl); cp = icem_candpair_find_st(&comp->icem->validl, comp->id, CANDPAIR_SUCCEEDED); if (!cp) { - DEBUG_WARNING("valid candpair not found for compid %u\n", - comp->id); + DEBUG_WARNING("{%s.%u} conclude: no valid candpair found" + " (validl=%u)\n", + comp->icem->name, comp->id, + list_count(&comp->icem->validl)); return; } icem_comp_set_selected(comp, cp); if (comp->icem->ice->conf.nom == ICE_NOMINATION_REGULAR) { + /* send STUN request with USE_CAND flag via triggered qeueue */ - cp->use_cand = true; - (void)icem_conncheck_send(cp, true); + (void)icem_conncheck_send(cp, true, true); icem_conncheck_schedule_check(comp->icem); } @@ -244,8 +246,10 @@ void icem_checklist_update(struct icem *icem) struct icem_comp *comp = le->data; if (!icem_candpair_find_compid(&icem->validl, comp->id)) { - DEBUG_WARNING("%s: no candidate pair for compid %u\n", - icem->name, comp->id); + DEBUG_WARNING("{%s.%u} no valid candidate pair" + " (validl=%u)\n", + icem->name, comp->id, + list_count(&icem->validl)); err = ENOENT; break; } diff --git a/src/ice/comp.c b/src/ice/comp.c index ed75e57..52d0458 100644 --- a/src/ice/comp.c +++ b/src/ice/comp.c @@ -25,7 +25,6 @@ enum {COMPID_MIN = 1, COMPID_MAX = 255}; -enum {DEFAULT_KEEPALIVE = 15}; #if 0 @@ -85,12 +84,11 @@ static bool helper_recv_handler(struct sa *src, struct mbuf *mb, void *arg) } -static void destructor(void *data) +static void destructor(void *arg) { - struct icem_comp *comp = data; + struct icem_comp *comp = arg; tmr_cancel(&comp->tmr_ka); - mem_deref(comp->ct_gath); mem_deref(comp->turnc); mem_deref(comp->cp_sel); @@ -208,6 +206,9 @@ void icem_comp_set_default_rcand(struct icem_comp *comp, struct cand *rcand) comp->def_rcand = mem_ref(rcand); if (comp->turnc) { + DEBUG_NOTICE("{%s.%u} Default: Add TURN Channel to peer %J\n", + comp->icem->name, comp->id, &rcand->addr); + (void)turnc_add_chan(comp->turnc, &rcand->addr, NULL, NULL); } } @@ -219,7 +220,8 @@ void icem_comp_set_selected(struct icem_comp *comp, struct candpair *cp) return; if (cp->state != CANDPAIR_SUCCEEDED) { - DEBUG_WARNING("set_selected: invalid state %s\n", + DEBUG_WARNING("{%s.%u} set_selected: invalid state %s\n", + comp->icem->name, comp->id, ice_candpair_state2name(cp->state)); } @@ -252,7 +254,7 @@ static void timeout(void *arg) struct icem_comp *comp = arg; struct candpair *cp; - tmr_start(&comp->tmr_ka, DEFAULT_KEEPALIVE * 1000 + rand_u16() % 1000, + tmr_start(&comp->tmr_ka, ICE_DEFAULT_Tr * 1000 + rand_u16() % 1000, timeout, comp); /* find selected candidate-pair */ @@ -272,8 +274,7 @@ void icem_comp_keepalive(struct icem_comp *comp, bool enable) return; if (enable) { - tmr_start(&comp->tmr_ka, DEFAULT_KEEPALIVE * 1000, - timeout, comp); + tmr_start(&comp->tmr_ka, ICE_DEFAULT_Tr * 1000, timeout, comp); } else { tmr_cancel(&comp->tmr_ka); diff --git a/src/ice/connchk.c b/src/ice/connchk.c index b503d18..b8522da 100644 --- a/src/ice/connchk.c +++ b/src/ice/connchk.c @@ -93,10 +93,8 @@ static struct candpair *construct_valid_pair(struct icem *icem, */ cp2 = icem_candpair_find(&icem->validl, lcand, rcand); - if (cp2) { - DEBUG_NOTICE("candpair already in VALID list\n"); + if (cp2) return cp2; - } err = icem_candpair_clone(&cp2, cp, lcand, rcand); if (err) @@ -186,13 +184,12 @@ static void stunc_resp_handler(int err, uint16_t scode, const char *reason, case 487: /* Role Conflict */ ice_switch_local_role(icem->ice); - icem_candpair_set_state(cp, CANDPAIR_WAITING); - (void)icem_conncheck_send(cp, true); + (void)icem_conncheck_send(cp, false, true); break; default: - DEBUG_WARNING("%s: STUN Response: %u %s\n", icem->name, - scode, reason); + DEBUG_WARNING("{%s.%u} STUN Response: %u %s\n", + icem->name, cp->comp->id, scode, reason); icem_candpair_failed(cp, err, scode); break; } @@ -202,14 +199,13 @@ static void stunc_resp_handler(int err, uint16_t scode, const char *reason, } -int icem_conncheck_send(struct candpair *cp, bool trigged) +int icem_conncheck_send(struct candpair *cp, bool use_cand, bool trigged) { struct cand *lcand = cp->lcand; struct icem *icem = cp->icem; struct ice *ice = icem->ice; char username_buf[64]; size_t presz = 0; - int use_cand = 0; uint32_t prio_prflx; uint16_t ctrl_attr; int err = 0; @@ -227,8 +223,8 @@ int icem_conncheck_send(struct candpair *cp, bool trigged) case ROLE_CONTROLLING: ctrl_attr = STUN_ATTR_CONTROLLING; - if (cp->use_cand || ice->conf.nom == ICE_NOMINATION_AGGRESSIVE) - use_cand = 1; + if (ice->conf.nom == ICE_NOMINATION_AGGRESSIVE) + use_cand = true; break; case ROLE_CONTROLLED: @@ -284,7 +280,7 @@ int icem_conncheck_send(struct candpair *cp, bool trigged) STUN_METHOD_BINDING, (uint8_t *)icem->rpwd, str_len(icem->rpwd), true, stunc_resp_handler, cp, - 3 + use_cand, + 3 + !!use_cand, STUN_ATTR_USERNAME, username_buf, STUN_ATTR_PRIORITY, &prio_prflx, ctrl_attr, &ice->tiebrk, @@ -305,7 +301,7 @@ static void do_check(struct candpair *cp) { int err; - err = icem_conncheck_send(cp, false); + err = icem_conncheck_send(cp, false, false); if (err) { icem_candpair_failed(cp, err, 0); return; @@ -374,11 +370,8 @@ int icem_conncheck_start(struct icem *icem) if (!icem) return EINVAL; - if (ICE_MODE_FULL != icem->ice->lmode) { - DEBUG_WARNING("connchk: invalid mode %s\n", - ice_mode2name(icem->ice->lmode)); + if (ICE_MODE_FULL != icem->ice->lmode) return EINVAL; - } err = icem_checklist_form(icem); if (err) @@ -389,9 +382,6 @@ int icem_conncheck_start(struct icem *icem) DEBUG_NOTICE("%s: starting connectivity checks" " with %u candidate pairs\n", icem->name, list_count(&icem->checkl)); -#if 0 - (void)re_printf("%H\n", icem_debug, icem); -#endif /* add some delay, to wait for call to be 'established' */ tmr_start(&icem->tmr_pace, 1000, timeout, icem); diff --git a/src/ice/gather.c b/src/ice/gather.c index 236dba1..25f092d 100644 --- a/src/ice/gather.c +++ b/src/ice/gather.c @@ -128,13 +128,13 @@ static void turnc_handler(int err, uint16_t scode, const char *reason, } if (err) { - DEBUG_WARNING("{%s.%d} TURN Client error: %s\n", + DEBUG_WARNING("{%s.%u} TURN Client error: %s\n", icem->name, comp->id, strerror(err)); goto out; } if (scode) { - DEBUG_WARNING("{%s.%d} TURN Client error: %u %s\n", + DEBUG_WARNING("{%s.%u} TURN Client error: %u %s\n", icem->name, comp->id, scode, reason); err = send_binding_request(icem, comp); if (err) diff --git a/src/ice/ice.h b/src/ice/ice.h index 1382131..68c8937 100644 --- a/src/ice/ice.h +++ b/src/ice/ice.h @@ -45,12 +45,12 @@ enum ice_transp { }; enum { - ICE_DEFAULT_Tr = 15, /* [ms] */ - ICE_DEFAULT_Ta_RTP = 20, /* [ms] */ - ICE_DEFAULT_Ta_NON_RTP = 500, /* [ms] */ - ICE_DEFAULT_RTO_RTP = 100, /* [ms] */ - ICE_DEFAULT_RTO_NONRTP = 500, /* [ms] */ - ICE_DEFAULT_RC = 4 + ICE_DEFAULT_Tr = 15, /**< Keepalive interval [s] */ + ICE_DEFAULT_Ta_RTP = 20, /**< Pacing interval RTP [ms] */ + ICE_DEFAULT_Ta_NON_RTP = 500, /**< Pacing interval [ms] */ + ICE_DEFAULT_RTO_RTP = 100, /**< Retransmission TimeOut RTP [ms] */ + ICE_DEFAULT_RTO_NONRTP = 500, /**< Retransmission TimeOut [ms] */ + ICE_DEFAULT_RC = 7 /**< Retransmission count */ }; @@ -134,7 +134,6 @@ struct candpair { bool def; /**< Default flag */ bool valid; /**< Valid flag */ bool nominated; /**< Nominated flag */ - bool use_cand; /**< Use-candidate flag */ enum candpair_state state; /**< Candidate pair state */ uint64_t pprio; /**< Pair priority */ uint64_t usec_sent; /**< When connectivity request was sent */ @@ -168,7 +167,6 @@ int icem_candpair_alloc(struct candpair **cpp, struct icem *icem, int icem_candpair_clone(struct candpair **cpp, struct candpair *cp0, struct cand *lcand, struct cand *rcand); void icem_candpair_prio_order(struct list *lst); -void icem_candpair_move(struct candpair *cp, struct list *list); void icem_candpair_cancel(struct candpair *cp); void icem_candpair_make_valid(struct candpair *cp); void icem_candpair_failed(struct candpair *cp, int err, uint16_t scode); @@ -218,7 +216,7 @@ void icecomp_printf(struct icem_comp *comp, const char *fmt, ...); void icem_conncheck_schedule_check(struct icem *icem); void icem_conncheck_continue(struct icem *icem); void icem_conncheck_stop(struct icem *icem); -int icem_conncheck_send(struct candpair *cp, bool trigged); +int icem_conncheck_send(struct candpair *cp, bool use_cand, bool trigged); /* icestr */ diff --git a/src/ice/icem.c b/src/ice/icem.c index 1072758..3327431 100644 --- a/src/ice/icem.c +++ b/src/ice/icem.c @@ -27,13 +27,11 @@ static void icem_destructor(void *data) list_unlink(&icem->le); tmr_cancel(&icem->tmr_pace); - list_flush(&icem->compl); list_flush(&icem->validl); list_flush(&icem->checkl); list_flush(&icem->lcandl); list_flush(&icem->rcandl); - mem_deref(icem->stun); mem_deref(icem->rufrag); mem_deref(icem->rpwd); @@ -155,7 +153,8 @@ void icem_cand_redund_elim(struct icem *icem) { uint32_t n = ice_list_unique(&icem->lcandl, unique_handler); if (n > 0) { - DEBUG_NOTICE("redundant candidates eliminated: %u\n", n); + DEBUG_NOTICE("%s: redundant candidates eliminated: %u\n", + icem->name, n); } } @@ -214,8 +213,12 @@ int icem_add_chan(struct icem *icem, uint8_t compid, const struct sa *raddr) if (!comp) return ENOENT; - if (comp->turnc) + if (comp->turnc) { + DEBUG_NOTICE("{%s.%u} Add TURN Channel to peer %J\n", + comp->icem->name, comp->id, raddr); + return turnc_add_chan(comp->turnc, raddr, NULL, NULL); + } return 0; } @@ -223,7 +226,8 @@ int icem_add_chan(struct icem *icem, uint8_t compid, const struct sa *raddr) static void purge_relayed(struct icem *icem, struct icem_comp *comp) { - icecomp_printf(comp, "purge local RELAY candidates\n"); + DEBUG_NOTICE("{%s.%u} purge local RELAY candidates\n", + icem->name, comp->id); /* * Purge all Candidate-Pairs where the Local candidate @@ -249,6 +253,7 @@ void icem_update(struct icem *icem) /* remove TURN client if not used by local "Selected" */ if (comp->cp_sel) { + if (comp->cp_sel->lcand->type != CAND_TYPE_RELAY) purge_relayed(icem, comp); } @@ -287,8 +292,7 @@ int icem_debug(struct re_printf *pf, const struct icem *icem) const struct icem_comp *comp = le->data; if (comp->cp_sel) { - err |= re_hprintf(pf, " Selected id=%u: %H\n", - comp->id, + err |= re_hprintf(pf, " Selected: %H\n", icem_candpair_debug, comp->cp_sel); } } diff --git a/src/ice/stunsrv.c b/src/ice/stunsrv.c index 70844bc..b55709e 100644 --- a/src/ice/stunsrv.c +++ b/src/ice/stunsrv.c @@ -35,9 +35,11 @@ static void triggered_check(struct icem *icem, struct cand *lcand, cp = icem_candpair_find(&icem->checkl, lcand, rcand); if (cp) { - DEBUG_NOTICE("triggered_check: found CANDPAIR on checklist" - " in state: %s\n", - ice_candpair_state2name(cp->state)); + DEBUG_NOTICE("{%s.%u} triggered_check: found CANDPAIR on" + " checklist in state: %s [%H]\n", + icem->name, cp->comp->id, + ice_candpair_state2name(cp->state), + icem_candpair_debug, cp); switch (cp->state) { @@ -59,7 +61,7 @@ static void triggered_check(struct icem *icem, struct cand *lcand, case CANDPAIR_FROZEN: case CANDPAIR_WAITING: - err = icem_conncheck_send(cp, true); + err = icem_conncheck_send(cp, false, true); if (err) { DEBUG_WARNING("triggered check failed\n"); } @@ -83,7 +85,7 @@ static void triggered_check(struct icem *icem, struct cand *lcand, icem_candpair_set_state(cp, CANDPAIR_WAITING); - (void)icem_conncheck_send(cp, true); + (void)icem_conncheck_send(cp, false, true); } } @@ -142,9 +144,10 @@ static void handle_stun(struct ice *ice, struct icem *icem, int err; if (icem->state != CHECKLIST_RUNNING) { - DEBUG_WARNING("%s.%u: Checklist is not running (%s)\n", - icem->name, comp->id, + DEBUG_WARNING("{%s.%u} src=%J Checklist is not running (%s)\n", + icem->name, comp->id, src, ice_checkl_state2name(icem->state)); + return; } /* 7.2.1.3. Learning Peer Reflexive Candidates */ @@ -170,7 +173,7 @@ static void handle_stun(struct ice *ice, struct icem *icem, } if (!lcand) { - DEBUG_WARNING("%s.%u: local candidate not found (checkl=%u)\n", + DEBUG_WARNING("{%s.%u} no local candidate (checkl=%u)\n", icem->name, comp->id, list_count(&icem->checkl)); } @@ -183,8 +186,9 @@ static void handle_stun(struct ice *ice, struct icem *icem, cp = lookup_candpair(icem, rcand); if (!cp) { - DEBUG_WARNING("candidate pair not found:" - " source=%J\n", src); + DEBUG_WARNING("{%s.%u} candidate pair not found:" + " source=%J\n", + icem->name, comp->id, src); } } @@ -202,9 +206,9 @@ static void handle_stun(struct ice *ice, struct icem *icem, if (use_cand) { if (ice->lrole == ROLE_CONTROLLED) { if (cp && cp->state == CANDPAIR_SUCCEEDED) { - DEBUG_NOTICE("{id=%d} setting NOMINATED" + DEBUG_NOTICE("{%s.%u} setting NOMINATED" " flag on candpair [%H]\n", - comp->id, + icem->name, comp->id, icem_candpair_debug, cp); cp->nominated = true; }