Changeset 1094


Ignore:
Timestamp:
Mar 22, 2007 1:16:37 AM (17 years ago)
Author:
bennylp
Message:

Completed initial test for ICE

Location:
pjproject/trunk/pjnath/src
Files:
5 edited

Legend:

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

    r1093 r1094  
    2424struct ice_data 
    2525{ 
     26    const char     *obj_name; 
    2627    pj_bool_t       complete; 
    2728    pj_status_t     err_code; 
    2829    unsigned        rx_rtp_cnt; 
    2930    unsigned        rx_rtcp_cnt; 
     31 
     32    char            rx_rtp_data[32]; 
     33    char            rx_rtcp_data[32]; 
    3034}; 
    3135 
     
    3842    id->complete = PJ_TRUE; 
    3943    id->err_code = status; 
     44    PJ_LOG(3,(THIS_FILE, "    ICE %s complete %s", id->obj_name, 
     45              (status==PJ_SUCCESS ? "successfully" : "with failure"))); 
    4046} 
    4147 
     
    4753{ 
    4854    struct ice_data *id = (struct ice_data*) icemt->user_data; 
     55 
    4956    id->rx_rtp_cnt++; 
     57    pj_memcpy(id->rx_rtp_data, pkt, size); 
     58 
     59    PJ_UNUSED_ARG(src_addr); 
     60    PJ_UNUSED_ARG(src_addr_len); 
    5061} 
    5162 
     
    5768{ 
    5869    struct ice_data *id = (struct ice_data*) icemt->user_data; 
     70 
    5971    id->rx_rtcp_cnt++; 
     72    pj_memcpy(id->rx_rtcp_data, pkt, size); 
     73 
     74    PJ_UNUSED_ARG(src_addr); 
     75    PJ_UNUSED_ARG(src_addr_len); 
    6076} 
    6177 
     
    129145 
    130146 
    131 /* Direct agent to agent communication */ 
    132 static int ice_direct_test() 
     147/* Perform ICE test with the following parameters: 
     148 * 
     149 * - title:     The title of the test 
     150 * - ocand_cnt, 
     151 *   ocand      Additional candidates to be added to offerer 
     152 * - acand_cnt, 
     153 *   acand      Additional candidates to be added to answerer 
     154 * 
     155 * The additional candidates are invalid candidates, meaning they 
     156 * won't be reachable by the agents. They are used to "confuse" 
     157 * ICE processing. 
     158 */ 
     159static int perform_ice_test(const char *title, 
     160                            unsigned ocand_cnt, 
     161                            const pj_ice_cand ocand[], 
     162                            unsigned acand_cnt, 
     163                            const pj_ice_cand acand[]) 
    133164{ 
    134165    pj_icemt *im1, *im2; 
    135166    pj_icemt_cb icemt_cb; 
    136167    struct ice_data *id1, *id2; 
     168    pj_timestamp t_start, t_end; 
     169    pj_ice_cand *rcand; 
     170    unsigned i; 
    137171    pj_status_t status; 
    138172 
    139     PJ_LOG(3,(THIS_FILE, "...direct communication")); 
     173    PJ_LOG(3,(THIS_FILE, "...%s", title)); 
    140174 
    141175    pj_bzero(&icemt_cb, sizeof(icemt_cb)); 
     
    145179 
    146180    /* Create first ICE */ 
    147     status = pj_icemt_create(&stun_cfg, NULL, PJ_ICE_ROLE_CONTROLLING, 
     181    status = pj_icemt_create(&stun_cfg, "offerer", PJ_ICE_ROLE_CONTROLLING, 
    148182                             &icemt_cb, 0, PJ_FALSE, PJ_FALSE, NULL, &im1); 
    149183    if (status != PJ_SUCCESS) 
     
    151185 
    152186    id1 = PJ_POOL_ZALLOC_T(im1->pool, struct ice_data); 
     187    id1->obj_name = "offerer"; 
    153188    im1->user_data = id1; 
    154189 
     190    /* Add additional candidates */ 
     191    for (i=0; i<ocand_cnt; ++i) { 
     192        status = pj_ice_add_cand(im1->ice, 1, ocand[i].type, 65535, 
     193                                 &ocand[i].foundation, &ocand[i].addr, 
     194                                 &ocand[i].base_addr, &ocand[i].srv_addr, 
     195                                 sizeof(pj_sockaddr_in), NULL); 
     196        if (status != PJ_SUCCESS) 
     197            return -22; 
     198    } 
     199 
    155200    /* Create second ICE */ 
    156     status = pj_icemt_create(&stun_cfg, NULL, PJ_ICE_ROLE_CONTROLLED, 
     201    status = pj_icemt_create(&stun_cfg, "answerer", PJ_ICE_ROLE_CONTROLLED, 
    157202                             &icemt_cb, 0, PJ_FALSE, PJ_FALSE, NULL, &im2); 
    158203    if (status != PJ_SUCCESS) 
     
    160205 
    161206    id2 = PJ_POOL_ZALLOC_T(im2->pool, struct ice_data); 
     207    id2->obj_name = "answerer"; 
    162208    im2->user_data = id2; 
    163209 
     210    /* Add additional candidates */ 
     211    for (i=0; i<acand_cnt; ++i) { 
     212        status = pj_ice_add_cand(im1->ice, 1, acand[i].type, 65535, 
     213                                 &acand[i].foundation, &acand[i].addr, 
     214                                 &acand[i].base_addr, &acand[i].srv_addr, 
     215                                 sizeof(pj_sockaddr_in), NULL); 
     216        if (status != PJ_SUCCESS) 
     217            return -22; 
     218    } 
     219 
     220    /* Set credentials */ 
    164221    { 
    165         pj_str_t u1 = pj_str("uname1"); 
     222        pj_str_t u1 = pj_str("offerer"); 
    166223        pj_str_t p1 = pj_str("pass1"); 
    167         pj_str_t u2 = pj_str("uname2"); 
     224        pj_str_t u2 = pj_str("answerer"); 
    168225        pj_str_t p2 = pj_str("pass2"); 
    169226 
     
    182239        return -35; 
    183240 
     241    /* Mark start time */ 
     242    pj_get_timestamp(&t_start); 
     243 
    184244    /* Both can start now */ 
    185245    status = pj_ice_start_check(im1->ice); 
     
    187247        return -40; 
    188248 
    189 #if 0 
     249#if 1 
    190250    status = pj_ice_start_check(im2->ice); 
    191251    if (status != PJ_SUCCESS) 
    192         return -40; 
     252        return -45; 
    193253#endif 
    194254 
    195255    /* Just wait until both completes, or timed out */ 
    196     while (!id1->complete || !id2->complete) 
     256    while (!id1->complete || !id2->complete) { 
     257        pj_timestamp t_now; 
     258 
    197259        handle_events(1); 
    198260 
     261        pj_get_timestamp(&t_now); 
     262        if (pj_elapsed_msec(&t_start, &t_now) >= 10000) { 
     263            PJ_LOG(3,(THIS_FILE, "....error: timed-out")); 
     264            return -50; 
     265        } 
     266    } 
     267 
     268    /* Mark end-time */ 
     269    pj_get_timestamp(&t_end); 
     270 
     271    /* Check status */ 
     272    if (id1->err_code != PJ_SUCCESS) 
     273        return -53; 
     274    if (id2->err_code != PJ_SUCCESS) 
     275        return -56; 
     276 
     277    /* Verify that offerer gets answerer's transport address */ 
     278    rcand = im1->ice->clist.checks[im1->ice->comp[0].nominated_check_id].rcand; 
     279    if (pj_memcmp(&rcand->addr, &im2->ice->lcand[0].addr, sizeof(pj_sockaddr_in))!=0) { 
     280        PJ_LOG(3,(THIS_FILE, "....error: address mismatch")); 
     281        return -60; 
     282    } 
     283 
     284    /* And the other way around */ 
     285    rcand = im2->ice->clist.checks[im2->ice->comp[0].nominated_check_id].rcand; 
     286    if (pj_memcmp(&rcand->addr, &im1->ice->lcand[0].addr, sizeof(pj_sockaddr_in))!=0) { 
     287        PJ_LOG(3,(THIS_FILE, "....error: address mismatch")); 
     288        return -70; 
     289    } 
     290 
     291    /* Done */ 
     292    PJ_LOG(3,(THIS_FILE, "....success: ICE completed in %d msec",  
     293              pj_elapsed_msec(&t_start, &t_end))); 
     294 
     295    /* Wait for some more time */ 
     296    PJ_LOG(3,(THIS_FILE, ".....waiting..")); 
     297    for (;;) { 
     298        pj_timestamp t_now; 
     299 
     300        pj_get_timestamp(&t_now); 
     301        if (pj_elapsed_msec(&t_end, &t_now) > 10000) 
     302            break; 
     303 
     304        handle_events(1); 
     305    } 
     306 
     307 
     308    pj_icemt_destroy(im1); 
     309    pj_icemt_destroy(im2); 
    199310    return 0; 
    200  
    201311} 
    202312 
     
    208318    pj_ioqueue_t *ioqueue; 
    209319    pj_timer_heap_t *timer_heap; 
    210      
     320    pj_ice_cand ocand[PJ_ICE_MAX_CAND]; 
     321    pj_ice_cand acand[PJ_ICE_MAX_CAND]; 
     322    pj_str_t s; 
     323 
    211324    pool = pj_pool_create(mem, NULL, 4000, 4000, NULL); 
    212325    pj_ioqueue_create(pool, 12, &ioqueue); 
     
    217330    pj_log_set_level(5); 
    218331 
     332    /* Basic create/destroy */ 
    219333    rc = ice_basic_create_destroy_test(); 
    220334    if (rc != 0) 
    221335        goto on_return; 
    222336 
    223     rc = ice_direct_test(); 
     337    /* Direct communication */ 
     338    rc = perform_ice_test("Direct connection", 0, NULL, 0, NULL); 
    224339    if (rc != 0) 
    225340        goto on_return; 
     341 
     342    /* Direct communication with invalid address */ 
     343    pj_bzero(ocand, sizeof(ocand)); 
     344    pj_sockaddr_in_init(&ocand[0].addr.ipv4, pj_cstr(&s, "127.0.0.127"), 1234); 
     345    pj_sockaddr_in_init(&ocand[0].base_addr.ipv4, pj_cstr(&s, "127.0.0.128"), 1234); 
     346    ocand[0].comp_id = 1; 
     347    ocand[0].foundation = pj_str("H2"); 
     348    ocand[0].type = PJ_ICE_CAND_TYPE_HOST; 
     349 
     350    rc = perform_ice_test("Direct connection with 1 invalid address", 1, ocand, 0, NULL); 
     351    if (rc != 0) 
     352        goto on_return; 
     353 
    226354 
    227355on_return: 
  • pjproject/trunk/pjnath/src/pjnath-test/main.c

    r1093 r1094  
    4949    rc = test_main(); 
    5050 
     51    if (argc == 2 && pj_ansi_strcmp(argv[1], "-i")==0) { 
     52        char buf[10]; 
     53        fgets(buf, sizeof(buf), stdin); 
     54    } 
     55 
    5156    return rc; 
    5257} 
  • pjproject/trunk/pjnath/src/pjnath-test/test.c

    r1093 r1094  
    5050    mem = &caching_pool.factory; 
    5151 
     52#if 0 
    5253    pj_log_set_level(3); 
    5354    pj_log_set_decor(PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_TIME |  
    5455                     PJ_LOG_HAS_MICRO_SEC); 
     56#endif 
    5557 
    5658    rc = pj_init(); 
  • pjproject/trunk/pjnath/src/pjnath/ice.c

    r1093 r1094  
    661661 
    662662static const char *dump_check(char *buffer, unsigned bufsize, 
     663                              const pj_ice *ice, 
    663664                              const pj_ice_check *check) 
    664665{ 
     
    672673    if (lcand->addr.addr.sa_family == PJ_AF_INET) { 
    673674        len = pj_ansi_snprintf(buffer, bufsize, 
    674                                "%s:%d-->%s:%d", 
     675                               "%d: %s:%d-->%s:%d", 
     676                               GET_CHECK_ID(check), 
    675677                               laddr, (int)pj_ntohs(lcand->addr.ipv4.sin_port), 
    676678                               pj_inet_ntoa(rcand->addr.ipv4.sin_addr), 
     
    700702    for (i=0; i<clist->count; ++i) { 
    701703        const pj_ice_check *c = &clist->checks[i]; 
    702         LOG((ice->obj_name, " %d: %s (prio=0x%"PJ_INT64_FMT"x, state=%s)", 
    703              i, dump_check(buffer, sizeof(buffer), c), 
    704              c->prio, check_state_name[c->state])); 
    705     } 
    706 } 
     704        LOG((ice->obj_name, " %s (%s, state=%s)", 
     705             dump_check(buffer, sizeof(buffer), ice, c), 
     706             (c->nominated ? "nominated" : "not nominated"),  
     707             check_state_name[c->state])); 
     708    } 
     709} 
     710 
     711static void dump_valid_list(const char *title, const pj_ice *ice) 
     712{ 
     713    unsigned i; 
     714    char buffer[CHECK_NAME_LEN]; 
     715 
     716    LOG((ice->obj_name, "%s", title)); 
     717    for (i=0; i<ice->valid_cnt; ++i) { 
     718        const pj_ice_check *c = &ice->clist.checks[ice->valid_list[i]]; 
     719        LOG((ice->obj_name, " %s (%s, state=%s)", 
     720             dump_check(buffer, sizeof(buffer), ice, c), 
     721             (c->nominated ? "nominated" : "not nominated"),  
     722             check_state_name[c->state])); 
     723    } 
     724} 
     725 
    707726#else 
    708727#define dump_checklist(ice, clist) 
     
    714733{ 
    715734    char buf[CHECK_NAME_LEN]; 
     735 
     736    pj_assert(check->state < PJ_ICE_CHECK_STATE_SUCCEEDED); 
     737 
    716738    LOG((ice->obj_name, "Check %s: state changed from %s to %s", 
    717          dump_check(buf, sizeof(buf), check), 
     739         dump_check(buf, sizeof(buf), ice, check), 
    718740         check_state_name[check->state], 
    719741         check_state_name[st])); 
     
    725747                            pj_ice_checklist_state st) 
    726748{ 
    727     LOG((ice->obj_name, "Checklist: state changed from %s to %s", 
    728          clist_state_name[clist->state], 
    729          clist_state_name[st])); 
    730     clist->state = st; 
     749    if (clist->state != st) { 
     750        LOG((ice->obj_name, "Checklist: state changed from %s to %s", 
     751             clist_state_name[clist->state], 
     752             clist_state_name[st])); 
     753        clist->state = st; 
     754    } 
    731755} 
    732756 
     
    841865 
    842866            if (ljcand->type == PJ_ICE_CAND_TYPE_SRFLX) 
    843                 ljaddr = &licand->base_addr; 
     867                ljaddr = &ljcand->base_addr; 
    844868            else 
    845                 ljaddr = &licand->addr; 
     869                ljaddr = &ljcand->addr; 
    846870 
    847871            if (sockaddr_cmp(liaddr, ljaddr) == SOCKADDR_EQUAL && 
     
    852876 
    853877                LOG((ice->obj_name, "Check %s pruned", 
    854                     dump_check(buf, sizeof(buf), &clist->checks[j]))); 
     878                    dump_check(buf, sizeof(buf), ice, &clist->checks[j]))); 
    855879 
    856880                pj_array_erase(clist->checks, sizeof(clist->checks[0]), 
     
    872896        ice->is_complete = PJ_TRUE; 
    873897        ice->ice_status = status; 
     898     
     899        /* Log message */ 
     900        LOG((ice->obj_name, "ICE process complete")); 
     901        dump_checklist("Dumping checklist", ice, &ice->clist); 
     902        dump_valid_list("Dumping valid list", ice); 
    874903 
    875904        /* Call callback */ 
     
    899928        pj_ice_comp *comp; 
    900929 
     930        LOG((ice->obj_name, "Check %d is successful and nominated", 
     931             GET_CHECK_ID(check))); 
     932 
    901933        for (i=0; i<ice->clist.count; ++i) { 
    902934            pj_ice_check *c = &ice->clist.checks[i]; 
     
    905937                 c->state==PJ_ICE_CHECK_STATE_WAITING)) 
    906938            { 
    907                 check_set_state(ice, check, PJ_ICE_CHECK_STATE_FAILED, 
     939                LOG((ice->obj_name,  
     940                     "Check %d to be failed because state is %s", 
     941                     i, check_state_name[c->state])); 
     942                check_set_state(ice, c, PJ_ICE_CHECK_STATE_FAILED, 
    908943                                PJ_ECANCELLED); 
    909944            } 
     
    953988 
    954989 
     990#if 0 
    955991    /* For now, just see if we have a valid pair in component 1 and 
    956992     * just terminate ICE. 
     
    9671003        return PJ_TRUE; 
    9681004    } 
    969  
    970     /* We don't have valid pair for component 1. 
    971      * See if we have performed all checks in the checklist. If we do, 
     1005#else 
     1006    /* See if all components have nominated pair. If they do, then mark 
     1007     * ICE processing as success, otherwise wait. 
     1008     */ 
     1009    for (i=0; i<ice->comp_cnt; ++i) { 
     1010        if (ice->comp[i].nominated_check_id == -1) 
     1011            break; 
     1012    } 
     1013    if (i == ice->comp_cnt) { 
     1014        /* All components have nominated pair */ 
     1015        on_ice_complete(ice, PJ_SUCCESS); 
     1016        return PJ_TRUE; 
     1017    } 
     1018#endif 
     1019 
     1020    /*  
     1021     * See if all checks in the checklist have completed. If we do, 
    9721022     * then mark ICE processing as failed. 
    9731023     */ 
     
    11011151 
    11021152    LOG((ice->obj_name,  
    1103          "Sending connectivity check for check %d: %s",  
    1104          check_id, dump_check(buffer, sizeof(buffer), check))); 
     1153         "Sending connectivity check for check %s",  
     1154         dump_check(buffer, sizeof(buffer), ice, check))); 
    11051155 
    11061156    /* Create request */ 
     
    13161366 
    13171367    LOG((ice->obj_name,  
    1318          "Connectivity check %s for check %s", 
    1319          (status==PJ_SUCCESS ? "SUCCESS" : "FAILED"),  
    1320          dump_check(buffer, sizeof(buffer), check))); 
     1368         "Check %s%s: connectivity check %s", 
     1369         dump_check(buffer, sizeof(buffer), ice, check), 
     1370         (check->nominated ? " (nominated)" : " (not nominated)"), 
     1371         (status==PJ_SUCCESS ? "SUCCESS" : "FAILED"))); 
    13211372 
    13221373    if (status != PJ_SUCCESS) { 
    13231374        check_set_state(ice, check, PJ_ICE_CHECK_STATE_FAILED, status); 
     1375        on_check_complete(ice, check); 
    13241376        pj_mutex_unlock(ice->mutex); 
    13251377        return; 
     
    13401392        check_set_state(ice, check, PJ_ICE_CHECK_STATE_FAILED,  
    13411393                        PJNATH_ESTUNNOXORMAP); 
     1394        on_check_complete(ice, check); 
    13421395        pj_mutex_unlock(ice->mutex); 
    13431396        return; 
     
    13661419        if (status != PJ_SUCCESS) { 
    13671420            check_set_state(ice, check, PJ_ICE_CHECK_STATE_FAILED, status); 
     1421            on_check_complete(ice, check); 
    13681422            pj_mutex_unlock(ice->mutex); 
    13691423            return; 
     
    15481602                                                  "f%p",  
    15491603                                                  rcand->foundation.ptr); 
     1604 
     1605        LOG((ice->obj_name,  
     1606             "Added new remote candidate from the request: %s:%d", 
     1607             pj_inet_ntoa(rcand->addr.ipv4.sin_addr), 
     1608             (int)pj_ntohs(rcand->addr.ipv4.sin_port))); 
     1609 
    15501610    } else { 
    15511611        /* Remote candidate found */ 
     
    16071667        pj_ice_check *c = &ice->clist.checks[i]; 
    16081668 
    1609         /* If USE-CANDIDATE is present, set nominated flag */ 
    1610         c->nominated = (uc != NULL); 
     1669        /* If USE-CANDIDATE is present, set nominated flag  
     1670         * Note: DO NOT overwrite nominated flag if one is already set. 
     1671         */ 
     1672        c->nominated = ((uc != NULL) || c->nominated); 
    16111673 
    16121674        if (c->state == PJ_ICE_CHECK_STATE_FROZEN || 
     
    16201682             * TODO 
    16211683             */ 
     1684            LOG((ice->obj_name, "Triggered check for check %d not performed " 
     1685                                "because it's in progress", i)); 
    16221686        } else if (c->state == PJ_ICE_CHECK_STATE_SUCCEEDED) { 
    16231687            /* Check complete for this component. 
     
    16251689             */ 
    16261690            pj_bool_t complete; 
     1691 
     1692            LOG((ice->obj_name, "Triggered check for check %d not performed " 
     1693                                "because it's completed", i)); 
    16271694 
    16281695            complete = on_check_complete(ice, c); 
  • pjproject/trunk/pjnath/src/pjnath/stun_msg.c

    r1093 r1094  
    866866 
    867867    attr = PJ_POOL_ZALLOC_T(pool, pj_stun_empty_attr); 
    868     INIT_ATTR(attr, attr_type, sizeof(pj_stun_empty_attr)); 
     868    INIT_ATTR(attr, attr_type, 0); 
    869869 
    870870    *p_attr = attr; 
     
    909909 
    910910    /* Check that the attribute length is valid */ 
    911     if (attr->hdr.length != ATTR_HDR_LEN) 
     911    if (attr->hdr.length != 0) 
    912912        return PJNATH_ESTUNINATTRLEN; 
    913913 
     
    931931    attr = (pj_stun_empty_attr*) buf; 
    932932    attr->hdr.type = pj_htons(attr->hdr.type); 
    933     pj_assert(attr->hdr.length == ATTR_HDR_LEN); 
    934     attr->hdr.length = pj_htons(ATTR_HDR_LEN); 
     933    attr->hdr.length = 0; 
    935934 
    936935    /* Done */ 
Note: See TracChangeset for help on using the changeset viewer.