Ignore:
Timestamp:
Mar 20, 2007 10:36:54 PM (17 years ago)
Author:
bennylp
Message:

Today's work

File:
1 edited

Legend:

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

    r1080 r1089  
    207207} 
    208208 
    209 static pj_str_t *get_passwd(pj_stun_session *sess) 
    210 { 
    211     if (sess->cred == NULL) 
     209static pj_str_t *get_passwd(pj_stun_session *sess, pj_pool_t *pool, 
     210                            const pj_stun_msg *msg) 
     211{ 
     212    if (sess->cred == NULL) { 
    212213        return NULL; 
    213     else if (sess->cred->type == PJ_STUN_AUTH_CRED_STATIC) 
     214    } else if (sess->cred->type == PJ_STUN_AUTH_CRED_STATIC) { 
    214215        return &sess->cred->data.static_cred.data; 
    215     else 
     216    } else if (sess->cred->type == PJ_STUN_AUTH_CRED_DYNAMIC) { 
     217        pj_str_t realm, username, nonce; 
     218        pj_str_t *password; 
     219        void *user_data = sess->cred->data.dyn_cred.user_data; 
     220        int data_type = 0; 
     221        pj_status_t status; 
     222 
     223        realm.slen = username.slen = nonce.slen = 0; 
     224        password = PJ_POOL_ZALLOC_T(pool, pj_str_t); 
     225        status = (*sess->cred->data.dyn_cred.get_cred)(msg, user_data, pool, 
     226                                                       &realm, &username, 
     227                                                       &nonce, &data_type, 
     228                                                       password); 
     229        return password; 
     230 
     231    } else { 
    216232        return NULL; 
     233    } 
    217234} 
    218235 
     
    222239{ 
    223240    pj_status_t status = 0; 
     241    pj_str_t realm, username, nonce, password; 
     242    int data_type = 0; 
     243 
     244    realm.slen = username.slen = nonce.slen = password.slen = 0; 
    224245 
    225246    /* The server SHOULD include a SERVER attribute in all responses */ 
     
    239260        !PJ_STUN_IS_ERROR_RESPONSE(msg->hdr.type))  
    240261    { 
    241         const pj_str_t *username; 
    242  
    243         /* Create and add USERNAME attribute */ 
    244         username = &sess->cred->data.static_cred.username; 
     262        realm = sess->cred->data.static_cred.realm; 
     263        username = sess->cred->data.static_cred.username; 
     264        data_type = sess->cred->data.static_cred.data_type; 
     265        password = sess->cred->data.static_cred.data; 
     266        nonce = sess->cred->data.static_cred.nonce; 
     267 
     268    } else if (sess->cred && sess->cred->type == PJ_STUN_AUTH_CRED_DYNAMIC && 
     269               !PJ_STUN_IS_ERROR_RESPONSE(msg->hdr.type))  
     270    { 
     271        void *user_data = sess->cred->data.dyn_cred.user_data; 
     272 
     273        status = (*sess->cred->data.dyn_cred.get_cred)(msg, user_data, pool, 
     274                                                       &realm, &username, 
     275                                                       &nonce, &data_type, 
     276                                                       &password); 
     277        if (status != PJ_SUCCESS) 
     278            return status; 
     279    } 
     280 
     281 
     282    /* Create and add USERNAME attribute */ 
     283    status = pj_stun_msg_add_string_attr(pool, msg, 
     284                                         PJ_STUN_ATTR_USERNAME, 
     285                                         &username); 
     286    PJ_ASSERT_RETURN(status==PJ_SUCCESS, status); 
     287 
     288    /* Add REALM only when long term credential is used */ 
     289    if (realm.slen) { 
    245290        status = pj_stun_msg_add_string_attr(pool, msg, 
    246                                              PJ_STUN_ATTR_USERNAME, 
    247                                              username); 
     291                                            PJ_STUN_ATTR_REALM, 
     292                                            &realm); 
    248293        PJ_ASSERT_RETURN(status==PJ_SUCCESS, status); 
    249  
    250         /* Add REALM only when long term credential is used */ 
    251         if (sess->cred->data.static_cred.realm.slen) { 
    252             const pj_str_t *realm = &sess->cred->data.static_cred.realm; 
    253             status = pj_stun_msg_add_string_attr(pool, msg, 
    254                                                 PJ_STUN_ATTR_REALM, 
    255                                                 realm); 
    256         } 
    257  
    258         /* Add MESSAGE-INTEGRITY attribute */ 
    259         status = pj_stun_msg_add_msgint_attr(pool, msg); 
    260         PJ_ASSERT_RETURN(status==PJ_SUCCESS, status); 
    261  
    262     }  
     294    } 
     295 
     296    /* Add NONCE when desired */ 
     297    if (nonce.slen) { 
     298        status = pj_stun_msg_add_string_attr(pool, msg, 
     299                                            PJ_STUN_ATTR_NONCE, 
     300                                            &nonce); 
     301    } 
     302 
     303    /* Add MESSAGE-INTEGRITY attribute */ 
     304    status = pj_stun_msg_add_msgint_attr(pool, msg); 
     305    PJ_ASSERT_RETURN(status==PJ_SUCCESS, status); 
     306 
    263307 
    264308    /* Add FINGERPRINT attribute if necessary */ 
     
    543587    /* Encode message */ 
    544588    status = pj_stun_msg_encode(tdata->msg, tdata->pkt, tdata->max_len, 
    545                                 0, get_passwd(sess), &tdata->pkt_size); 
     589                                0, get_passwd(sess, tdata->pool, tdata->msg), 
     590                                &tdata->pkt_size); 
    546591    if (status != PJ_SUCCESS) { 
    547592        pj_stun_msg_destroy_tdata(sess, tdata); 
     
    640685    pj_status_t status; 
    641686 
     687    /* Apply options */ 
     688    if (!retransmission) { 
     689        status = apply_msg_options(sess, pool, response); 
     690        if (status != PJ_SUCCESS) 
     691            return status; 
     692    } 
     693 
    642694    /* Alloc packet buffer */ 
    643695    out_max_len = PJ_STUN_MAX_PKT_LEN; 
    644696    out_pkt = pj_pool_alloc(pool, out_max_len); 
    645697 
    646     /* Apply options */ 
    647     if (!retransmission) { 
    648         apply_msg_options(sess, pool, response); 
    649     } 
    650  
    651698    /* Encode */ 
    652699    status = pj_stun_msg_encode(response, out_pkt, out_max_len, 0,  
    653                                 get_passwd(sess), &out_len); 
     700                                get_passwd(sess, pool, response), 
     701                                &out_len); 
    654702    if (status != PJ_SUCCESS) { 
    655703        LOG_ERR_(sess, "Error encoding message", status); 
Note: See TracChangeset for help on using the changeset viewer.