Ignore:
Timestamp:
Mar 26, 2007 1:25:07 PM (17 years ago)
Author:
bennylp
Message:

ICE: implement RTCP component and cancelling check in progress

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjnath/src/pjnath/ice.c

    r1104 r1106  
    5757}; 
    5858 
    59 #define CHECK_NAME_LEN      128 
    60 #define LOG4(expr)          PJ_LOG(4,expr) 
    61 #define LOG5(expr)          PJ_LOG(4,expr) 
    62 #define GET_LCAND_ID(cand)  (cand - ice->lcand) 
    63 #define GET_CHECK_ID(chk)   (chk - ice->clist.checks) 
     59#define CHECK_NAME_LEN          128 
     60#define LOG4(expr)              PJ_LOG(4,expr) 
     61#define LOG5(expr)              PJ_LOG(4,expr) 
     62#define GET_LCAND_ID(cand)      (cand - ice->lcand) 
     63#define GET_CHECK_ID(cl, chk)   (chk - (cl)->checks) 
    6464 
    6565 
     
    131131 
    132132 
     133PJ_DEF(const char*) pj_ice_get_cand_type_name(pj_ice_cand_type type) 
     134{ 
     135    return cand_type_names[type]; 
     136} 
     137 
     138 
    133139/* 
    134140 * Create ICE stream session. 
     
    177183        pj_ice_comp *comp; 
    178184        comp = &ice->comp[i]; 
    179         comp->comp_id = i+1; 
    180         comp->nominated_check_id = -1; 
     185        comp->valid_check = NULL; 
    181186    } 
    182187 
     
    412417                     foundation && addr && base_addr && addr_len, 
    413418                     PJ_EINVAL); 
     419    PJ_ASSERT_RETURN(comp_id <= ice->comp_cnt, PJ_EINVAL); 
    414420 
    415421    pj_mutex_lock(ice->mutex); 
     
    499505 
    500506    PJ_ASSERT_RETURN(ice && comp_id && cand_id, PJ_EINVAL); 
     507    PJ_ASSERT_RETURN(comp_id <= ice->comp_cnt, PJ_EINVAL); 
    501508 
    502509    *cand_id = -1; 
     
    586593 
    587594static const char *dump_check(char *buffer, unsigned bufsize, 
    588                               const pj_ice *ice, 
     595                              const pj_ice_checklist *clist, 
    589596                              const pj_ice_check *check) 
    590597{ 
     
    598605    if (lcand->addr.addr.sa_family == PJ_AF_INET) { 
    599606        len = pj_ansi_snprintf(buffer, bufsize, 
    600                                "%d: %s:%d-->%s:%d", 
    601                                GET_CHECK_ID(check), 
     607                               "%d: [%d] %s:%d-->%s:%d", 
     608                               GET_CHECK_ID(clist, check), 
     609                               check->lcand->comp_id, 
    602610                               laddr, (int)pj_ntohs(lcand->addr.ipv4.sin_port), 
    603611                               pj_inet_ntoa(rcand->addr.ipv4.sin_addr), 
     
    628636        const pj_ice_check *c = &clist->checks[i]; 
    629637        LOG4((ice->obj_name, " %s (%s, state=%s)", 
    630              dump_check(buffer, sizeof(buffer), ice, c), 
     638             dump_check(buffer, sizeof(buffer), clist, c), 
    631639             (c->nominated ? "nominated" : "not nominated"),  
    632640             check_state_name[c->state])); 
     
    647655 
    648656    LOG5((ice->obj_name, "Check %s: state changed from %s to %s", 
    649          dump_check(buf, sizeof(buf), ice, check), 
     657         dump_check(buf, sizeof(buf), &ice->clist, check), 
    650658         check_state_name[check->state], 
    651659         check_state_name[st])); 
     
    760768 
    761769                LOG5((ice->obj_name, "Check %s pruned", 
    762                     dump_check(buf, sizeof(buf), ice, &clist->checks[j]))); 
     770                    dump_check(buf, sizeof(buf), &ice->clist,  
     771                               &clist->checks[j]))); 
    763772 
    764773                pj_array_erase(clist->checks, sizeof(clist->checks[0]), 
     
    813822    if (check->err_code==PJ_SUCCESS && check->nominated) { 
    814823        pj_ice_comp *comp; 
     824        char buf[CHECK_NAME_LEN]; 
    815825 
    816826        LOG5((ice->obj_name, "Check %d is successful and nominated", 
    817              GET_CHECK_ID(check))); 
     827             GET_CHECK_ID(&ice->clist, check))); 
    818828 
    819829        for (i=0; i<ice->clist.count; ++i) { 
    820830            pj_ice_check *c = &ice->clist.checks[i]; 
    821             if (c->lcand->comp_id == check->lcand->comp_id && 
    822                 (c->state==PJ_ICE_CHECK_STATE_FROZEN || 
    823                  c->state==PJ_ICE_CHECK_STATE_WAITING)) 
    824             { 
    825                 LOG5((ice->obj_name,  
    826                      "Check %d to be failed because state is %s", 
    827                      i, check_state_name[c->state])); 
    828                 check_set_state(ice, c, PJ_ICE_CHECK_STATE_FAILED, 
    829                                 PJ_ECANCELLED); 
     831            if (c->lcand->comp_id == check->lcand->comp_id) { 
     832                if (c->state < PJ_ICE_CHECK_STATE_IN_PROGRESS) { 
     833                    /* Just fail Frozen/Waiting check */ 
     834                    LOG5((ice->obj_name,  
     835                         "Check %s to be failed because state is %s", 
     836                         dump_check(buf, sizeof(buf), &ice->clist, c),  
     837                         check_state_name[c->state])); 
     838                    check_set_state(ice, c, PJ_ICE_CHECK_STATE_FAILED, 
     839                                    PJ_ECANCELLED); 
     840 
     841                } else if (c->state == PJ_ICE_CHECK_STATE_IN_PROGRESS) { 
     842                    /* State is IN_PROGRESS, cancel transaction */ 
     843                    if (c->tdata) { 
     844                        LOG5((ice->obj_name,  
     845                             "Cancelling check %s (In Progress)", 
     846                             dump_check(buf, sizeof(buf), &ice->clist, c))); 
     847                        pj_stun_session_cancel_req(c->lcand->stun_sess,  
     848                                                   c->tdata, PJ_FALSE, 0); 
     849                        c->tdata = NULL; 
     850                        check_set_state(ice, c, PJ_ICE_CHECK_STATE_FAILED, 
     851                                        PJ_ECANCELLED); 
     852                    } 
     853                } 
    830854            } 
    831855        } 
     
    833857        /* Update the nominated check for the component */ 
    834858        comp = find_comp(ice, check->lcand->comp_id); 
    835         if (comp->nominated_check_id < 0) { 
    836             comp->nominated_check_id = GET_CHECK_ID(check); 
     859        if (comp->valid_check == NULL) { 
     860            comp->valid_check = check; 
    837861        } else { 
    838             pj_ice_check *nom_check; 
    839             nom_check = &ice->clist.checks[comp->nominated_check_id]; 
    840             if (nom_check->prio < check->prio) 
    841                 comp->nominated_check_id = GET_CHECK_ID(check); 
     862            if (comp->valid_check->prio < check->prio) 
     863                comp->valid_check = check; 
    842864        } 
    843865    } 
     
    878900     */ 
    879901    for (i=0; i<ice->comp_cnt; ++i) { 
    880         if (ice->comp[i].nominated_check_id == -1) 
     902        if (ice->comp[i].valid_check == NULL) 
    881903            break; 
    882904    } 
     
    950972    ice->rcand_cnt = 0; 
    951973    for (i=0; i<rcand_cnt; ++i) { 
    952         pj_ice_cand *cn = &ice->rcand[ice->rcand_cnt++]; 
     974        pj_ice_cand *cn = &ice->rcand[ice->rcand_cnt]; 
     975 
     976        /* Ignore candidate which has no matching component ID */ 
     977        pj_assert(rcand[i].comp_id > 0); 
     978        if (rcand[i].comp_id==0 || rcand[i].comp_id > ice->comp_cnt) { 
     979            continue; 
     980        } 
     981 
    953982        pj_memcpy(cn, &rcand[i], sizeof(pj_ice_cand)); 
    954983        pj_strdup(ice->pool, &cn->foundation, &rcand[i].foundation); 
     984        ice->rcand_cnt++; 
    955985    } 
    956986 
     
    967997                pj_mutex_unlock(ice->mutex); 
    968998                return PJ_ETOOMANY; 
    969             } else { 
    970                 clist->count++; 
    971             } 
     999            }  
    9721000 
    9731001            /* A local candidate is paired with a remote candidate if 
     
    9871015 
    9881016            chk->prio = CALC_CHECK_PRIO(ice, lcand, rcand); 
     1017 
     1018            clist->count++; 
    9891019        } 
    9901020    } 
     
    10271057                                 unsigned check_id) 
    10281058{ 
    1029     pj_stun_tx_data *tdata; 
    10301059    pj_ice_comp *comp; 
    10311060    struct req_data *rd; 
     
    10441073    LOG5((ice->obj_name,  
    10451074         "Sending connectivity check for check %s",  
    1046          dump_check(buffer, sizeof(buffer), ice, check))); 
     1075         dump_check(buffer, sizeof(buffer), clist, check))); 
    10471076 
    10481077    /* Create request */ 
    10491078    status = pj_stun_session_create_req(lcand->stun_sess,  
    1050                                         PJ_STUN_BINDING_REQUEST, &tdata); 
     1079                                        PJ_STUN_BINDING_REQUEST,  
     1080                                        &check->tdata); 
    10511081    if (status != PJ_SUCCESS) 
    10521082        return status; 
     
    10551085     * completes and on_stun_request_complete() callback is called. 
    10561086     */ 
    1057     rd = PJ_POOL_ZALLOC_T(tdata->pool, struct req_data); 
     1087    rd = PJ_POOL_ZALLOC_T(check->tdata->pool, struct req_data); 
    10581088    rd->ice = ice; 
    10591089    rd->clist = clist; 
    10601090    rd->ckid = check_id; 
    1061     tdata->user_data = (void*) rd; 
     1091    check->tdata->user_data = (void*) rd; 
    10621092 
    10631093    /* Add PRIORITY */ 
    10641094    prio = CALC_CAND_PRIO(PJ_ICE_CAND_TYPE_PRFLX, 65535,  
    10651095                          lcand->comp_id); 
    1066     pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg, PJ_STUN_ATTR_PRIORITY, 
    1067                               prio); 
     1096    pj_stun_msg_add_uint_attr(check->tdata->pool, check->tdata->msg,  
     1097                              PJ_STUN_ATTR_PRIORITY, prio); 
    10681098 
    10691099    /* Add USE-CANDIDATE and set this check to nominated */ 
    10701100    if (ice->role == PJ_ICE_ROLE_CONTROLLING) { 
    1071         pj_stun_msg_add_empty_attr(tdata->pool, tdata->msg,  
     1101        pj_stun_msg_add_empty_attr(check->tdata->pool, check->tdata->msg,  
    10721102                                   PJ_STUN_ATTR_USE_CANDIDATE); 
    10731103        check->nominated = PJ_TRUE; 
     
    10811111    status = pj_stun_session_send_msg(lcand->stun_sess, PJ_FALSE,  
    10821112                                      &rcand->addr,  
    1083                                       sizeof(pj_sockaddr_in), tdata); 
    1084     if (status != PJ_SUCCESS) 
     1113                                      sizeof(pj_sockaddr_in), check->tdata); 
     1114    if (status != PJ_SUCCESS) { 
     1115        check->tdata = NULL; 
    10851116        return status; 
     1117    } 
    10861118 
    10871119    check_set_state(ice, check, PJ_ICE_CHECK_STATE_IN_PROGRESS, PJ_SUCCESS); 
     
    12501282    clist = rd->clist; 
    12511283 
     1284    /* Mark STUN transaction as complete */ 
     1285    pj_assert(tdata == check->tdata); 
     1286    check->tdata = NULL; 
     1287 
    12521288    pj_mutex_lock(ice->mutex); 
    12531289 
     
    12591295    LOG4((ice->obj_name,  
    12601296         "Check %s%s: connectivity check %s", 
    1261          dump_check(buffer, sizeof(buffer), ice, check), 
     1297         dump_check(buffer, sizeof(buffer), &ice->clist, check), 
    12621298         (check->nominated ? " (nominated)" : " (not nominated)"), 
    12631299         (status==PJ_SUCCESS ? "SUCCESS" : "FAILED"))); 
     
    15011537    if (i == ice->rcand_cnt) { 
    15021538        rcand = &ice->rcand[ice->rcand_cnt++]; 
    1503         rcand->comp_id = comp->comp_id; 
     1539        rcand->comp_id = lcand->comp_id; 
    15041540        rcand->type = PJ_ICE_CAND_TYPE_PRFLX; 
    15051541        rcand->prio = ap->value; 
     
    16491685    pj_ice_comp *comp; 
    16501686    unsigned cand_id; 
    1651     pj_ice_check *check; 
    1652  
    1653     PJ_ASSERT_RETURN(ice, PJ_EINVAL); 
     1687 
     1688    PJ_ASSERT_RETURN(ice && comp_id && comp_id <= ice->comp_cnt, PJ_EINVAL); 
    16541689     
    16551690    pj_mutex_lock(ice->mutex); 
     
    16611696    } 
    16621697 
    1663     if (comp->nominated_check_id == -1) { 
     1698    if (comp->valid_check == NULL) { 
    16641699        status = PJNATH_EICEINPROGRESS; 
    16651700        goto on_return; 
    16661701    } 
    16671702 
    1668     check = &ice->clist.checks[comp->nominated_check_id]; 
    1669     cand_id = GET_LCAND_ID(check->lcand); 
    1670  
     1703    cand_id = GET_LCAND_ID(comp->valid_check->lcand); 
    16711704    status = (*ice->cb.on_tx_pkt)(ice, comp_id, cand_id, data, data_len,  
    1672                                   &check->rcand->addr,  
     1705                                  &comp->valid_check->rcand->addr,  
    16731706                                  sizeof(pj_sockaddr_in)); 
    16741707 
Note: See TracChangeset for help on using the changeset viewer.