Ignore:
Timestamp:
Mar 19, 2008 11:00:30 PM (16 years ago)
Author:
bennylp
Message:

Related to ticket #485: huge changeset to update STUN relating to managing authentication. See the ticket for the details

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjnath/src/pjturn-srv/server.c

    r1869 r1877  
    4141                                      const pj_uint8_t *pkt, 
    4242                                      unsigned pkt_len, 
    43                                       const pj_stun_msg *msg, 
     43                                      const pj_stun_rx_data *rdata, 
    4444                                      const pj_sockaddr_t *src_addr, 
    4545                                      unsigned src_addr_len); 
     
    148148    srv->core.cred.data.dyn_cred.user_data = srv; 
    149149    srv->core.cred.data.dyn_cred.get_auth = &pj_turn_get_auth; 
    150     srv->core.cred.data.dyn_cred.get_cred = &pj_turn_get_cred; 
    151150    srv->core.cred.data.dyn_cred.get_password = &pj_turn_get_password; 
    152151    srv->core.cred.data.dyn_cred.verify_nonce = &pj_turn_verify_nonce; 
     
    369368 
    370369    pj_stun_session_set_user_data(sess, lis); 
    371     pj_stun_session_set_credential(sess, &srv->core.cred); 
     370    pj_stun_session_set_credential(sess, PJ_STUN_AUTH_LONG_TERM,  
     371                                   &srv->core.cred); 
    372372 
    373373    srv->core.stun_sess[index] = sess; 
     
    484484 
    485485/* Respond to STUN request */ 
    486 static pj_status_t stun_respond(pj_turn_srv *srv, 
    487                                 pj_stun_session *sess,  
    488                                 const pj_stun_msg *req, 
     486static pj_status_t stun_respond(pj_stun_session *sess,  
     487                                const pj_stun_rx_data *rdata, 
    489488                                unsigned code,  
    490489                                const char *errmsg, 
     
    498497 
    499498    /* Create response */ 
    500     status = pj_stun_session_create_res(sess, req, code,  
     499    status = pj_stun_session_create_res(sess, rdata, code,  
    501500                                        (errmsg?pj_cstr(&reason,errmsg):NULL), 
    502501                                        &tdata); 
     
    504503        return status; 
    505504 
    506     /* Store the credential for future lookup. */ 
    507     if (pj_stun_auth_valid_for_msg(tdata->msg)) { 
    508         pj_turn_srv_put_cred(srv, req, tdata); 
    509     } 
    510  
    511505    /* Send the response */ 
    512506    return pj_stun_session_send_msg(sess, cache, dst_addr,  addr_len, tdata); 
    513 } 
    514  
    515  
    516 /* 
    517  * Store the credential to put placed for the specified message for 
    518  * future retrieval. 
    519  */ 
    520 PJ_DEF(pj_status_t) pj_turn_srv_put_cred(pj_turn_srv *srv, 
    521                                          const pj_stun_msg *req, 
    522                                          pj_stun_tx_data *response) 
    523 { 
    524     pj_stun_username_attr *user; 
    525     pj_stun_realm_attr *realm; 
    526     pj_stun_nonce_attr *nonce; 
    527     struct saved_cred *saved_cred; 
    528     pj_status_t status; 
    529  
    530     realm = (pj_stun_realm_attr*) 
    531             pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REALM, 0); 
    532     PJ_ASSERT_RETURN(realm != NULL, PJ_EBUG); 
    533  
    534     user = (pj_stun_username_attr*) 
    535            pj_stun_msg_find_attr(req, PJ_STUN_ATTR_USERNAME, 0); 
    536     PJ_ASSERT_RETURN(user != NULL, PJ_EBUG); 
    537  
    538     nonce = (pj_stun_nonce_attr*) 
    539             pj_stun_msg_find_attr(req, PJ_STUN_ATTR_NONCE, 0); 
    540     PJ_ASSERT_RETURN(nonce != NULL, PJ_EBUG); 
    541  
    542     saved_cred = PJ_POOL_ALLOC_T(response->pool, struct saved_cred); 
    543  
    544     /* Lookup the password */ 
    545     status = pj_turn_get_password(response->msg, NULL, &realm->value,  
    546                                   &user->value, response->pool,  
    547                                   &saved_cred->data_type,  
    548                                   &saved_cred->data); 
    549     if (status != PJ_SUCCESS) 
    550         return status; 
    551  
    552     /* Store credential */ 
    553     pj_strdup(response->pool, &saved_cred->username, &user->value); 
    554     pj_strdup(response->pool, &saved_cred->realm, &realm->value); 
    555     pj_strdup(response->pool, &saved_cred->nonce, &nonce->value); 
    556  
    557     pj_thread_local_set(srv->core.tls_key, response->msg); 
    558     pj_thread_local_set(srv->core.tls_data, saved_cred); 
    559  
    560     return PJ_SUCCESS; 
    561 } 
    562  
    563  
    564 /** 
    565  * Retrieve previously stored credential for the specified message. 
    566  */ 
    567 PJ_DEF(pj_status_t) pj_turn_srv_get_cred(const pj_stun_msg *msg, 
    568                                          void *user_data, 
    569                                          pj_pool_t *pool, 
    570                                          pj_str_t *realm, 
    571                                          pj_str_t *username, 
    572                                          pj_str_t *nonce, 
    573                                          int *data_type, 
    574                                          pj_str_t *data) 
    575 { 
    576     pj_turn_srv *srv; 
    577     const pj_stun_msg *saved_msg; 
    578     struct saved_cred *saved_cred; 
    579  
    580     PJ_UNUSED_ARG(pool); 
    581  
    582     srv = (pj_turn_srv*)user_data; 
    583  
    584     /* Lookup stored message and make sure it's for the same message */ 
    585     saved_msg = (const pj_stun_msg*) 
    586                 pj_thread_local_get(srv->core.tls_key); 
    587     PJ_ASSERT_RETURN(saved_msg==msg, PJ_ENOTFOUND); 
    588  
    589     /* Lookup saved credential */ 
    590     saved_cred = (struct saved_cred*)  
    591                  pj_thread_local_get(srv->core.tls_data); 
    592     PJ_ASSERT_RETURN(saved_cred != NULL, PJ_ENOTFOUND); 
    593  
    594  
    595     *realm = saved_cred->realm; 
    596     *username = saved_cred->username; 
    597     *nonce = saved_cred->nonce; 
    598     *data_type = saved_cred->data_type; 
    599     *data = saved_cred->data; 
    600  
    601  
    602     /* Don't clear saved_cred as this may be called more than once */ 
    603  
    604     return PJ_SUCCESS; 
    605507} 
    606508 
     
    613515                                      const pj_uint8_t *pkt, 
    614516                                      unsigned pkt_len, 
    615                                       const pj_stun_msg *msg, 
     517                                      const pj_stun_rx_data *rdata, 
    616518                                      const pj_sockaddr_t *src_addr, 
    617519                                      unsigned src_addr_len) 
    618520{ 
    619521    pj_turn_listener *listener; 
     522    const pj_stun_msg *msg = rdata->msg; 
    620523    pj_turn_srv *srv; 
    621524    pj_turn_allocation *alloc; 
     
    630533    /* Respond any requests other than ALLOCATE with 437 response */ 
    631534    if (msg->hdr.type != PJ_STUN_ALLOCATE_REQUEST) { 
    632         stun_respond(srv, sess, msg, PJ_STUN_SC_ALLOCATION_MISMATCH, 
     535        stun_respond(sess, rdata, PJ_STUN_SC_ALLOCATION_MISMATCH, 
    633536                     NULL, PJ_FALSE, src_addr, src_addr_len); 
    634537        return PJ_SUCCESS; 
     
    639542     */ 
    640543    status = pj_turn_allocation_create(listener, src_addr, src_addr_len, 
    641                                        msg, sess, &alloc); 
     544                                       rdata, sess, &alloc); 
    642545    if (status != PJ_SUCCESS) { 
    643546        /* STUN response has been sent, no need to reply here */ 
Note: See TracChangeset for help on using the changeset viewer.