Ignore:
Timestamp:
Sep 18, 2007 7:33:33 PM (17 years ago)
Author:
bennylp
Message:

Ticket #374: Update STUN specification from rfc3489bis-06 to rfc3489bis-10

File:
1 edited

Legend:

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

    r1410 r1439  
    3232#define STUN_XOR_FINGERPRINT    0x5354554eL 
    3333 
     34static int padding_char; 
     35 
    3436static const char *stun_method_names[] =  
    3537{ 
     
    5557    { PJ_STUN_SC_UNAUTHORIZED,              "Unauthorized"}, 
    5658    { PJ_STUN_SC_UNKNOWN_ATTRIBUTE,         "Unknown Attribute"}, 
    57     { PJ_STUN_SC_STALE_CREDENTIALS,         "Stale Credentials"}, 
    58     { PJ_STUN_SC_INTEGRITY_CHECK_FAILURE,  "Integrity Check Failure"}, 
    59     { PJ_STUN_SC_MISSING_USERNAME,          "Missing Username"}, 
    60     { PJ_STUN_SC_USE_TLS,                   "Use TLS"}, 
    61     { PJ_STUN_SC_MISSING_REALM,             "Missing Realm"}, 
    62     { PJ_STUN_SC_MISSING_NONCE,             "Missing Nonce"}, 
    63     { PJ_STUN_SC_UNKNOWN_USERNAME,          "Unknown Username"}, 
    64     { PJ_STUN_SC_NO_BINDING,                "No Binding"}, 
     59    //{ PJ_STUN_SC_STALE_CREDENTIALS,       "Stale Credentials"}, 
     60    //{ PJ_STUN_SC_INTEGRITY_CHECK_FAILURE, "Integrity Check Failure"}, 
     61    //{ PJ_STUN_SC_MISSING_USERNAME,        "Missing Username"}, 
     62    //{ PJ_STUN_SC_USE_TLS,                 "Use TLS"}, 
     63    //{ PJ_STUN_SC_MISSING_REALM,           "Missing Realm"}, 
     64    //{ PJ_STUN_SC_MISSING_NONCE,           "Missing Nonce"}, 
     65    //{ PJ_STUN_SC_UNKNOWN_USERNAME,        "Unknown Username"}, 
     66    //{ PJ_STUN_SC_NO_BINDING,              "No Binding"}, 
    6567    { PJ_STUN_SC_STALE_NONCE,               "Stale Nonce"}, 
    6668    { PJ_STUN_SC_TRANSITIONING,             "Active Destination Already Set"}, 
     
    569571 
    570572 
     573/* 
     574 * Set padding character. 
     575 */ 
     576PJ_DEF(int) pj_stun_set_padding_char(int chr) 
     577{ 
     578    int old_pad = padding_char; 
     579    padding_char = chr; 
     580    return old_pad; 
     581} 
     582 
     583 
    571584////////////////////////////////////////////////////////////////////////////// 
    572585 
     
    900913    /* Copy the string */ 
    901914    pj_memcpy(buf+ATTR_HDR_LEN, ca->value.ptr, ca->value.slen); 
     915 
     916    /* Add padding character, if string is not 4-bytes aligned. */ 
     917    if (ca->value.slen & 0x03) { 
     918        pj_uint8_t pad[3]; 
     919        pj_memset(pad, padding_char, sizeof(pad)); 
     920        pj_memcpy(buf+ATTR_HDR_LEN+ca->value.slen, pad, 
     921                  4-(ca->value.slen & 0x03)); 
     922    } 
    902923 
    903924    /* Done */ 
     
    13791400     * attributes MUST be repeated in the list. 
    13801401     */ 
     1402    /* No longer necessary 
    13811403    if ((attr_cnt & 0x01)) { 
    13821404        attr->attrs[attr_cnt] = attr_array[attr_cnt-1]; 
    13831405    } 
    1384  
    1385     *p_attr = NULL; 
     1406    */ 
     1407 
     1408    *p_attr = attr; 
    13861409 
    13871410    return PJ_SUCCESS; 
     
    16341657        ((options & PJ_STUN_IS_DATAGRAM) && msg_len + 20 != pdu_len)) 
    16351658    { 
     1659        return PJNATH_EINSTUNMSGLEN; 
     1660    } 
     1661 
     1662    /* STUN message is always padded to the nearest 4 bytes, thus 
     1663     * the last two bits of the length field are always zero. 
     1664     */ 
     1665    if ((msg_len & 0x03) != 0) { 
    16361666        return PJNATH_EINSTUNMSGLEN; 
    16371667    } 
     
    18791909                has_fingerprint = PJ_TRUE; 
    18801910            } else { 
    1881                 if (has_msg_int || has_fingerprint) { 
     1911                if (has_fingerprint) { 
    18821912                    /* Another attribute is found which is not FINGERPRINT 
    1883                      * after FINGERPRINT or MESSAGE-INTEGRITY */ 
     1913                     * after FINGERPRINT. Note that non-FINGERPRINT is 
     1914                     * allowed to appear after M-I 
     1915                     */ 
    18841916                    if (p_response) { 
    18851917                        pj_stun_msg_create_response(pool, msg, 
     
    18871919                                                    NULL, p_response); 
    18881920                    } 
    1889                     return has_fingerprint ? PJNATH_ESTUNFINGERPOS : 
    1890                                              PJNATH_ESTUNMSGINTPOS; 
     1921                    return PJNATH_ESTUNFINGERPOS; 
    18911922                } 
    18921923            } 
     
    21152146    } 
    21162147 
    2117     /* We MUST update the message length in the header NOW before 
    2118      * calculating MESSAGE-INTEGRITY and FINGERPRINT.  
    2119      * Note that length is not including the 20 bytes header. 
     2148    /* If MESSAGE-INTEGRITY is present, include the M-I attribute 
     2149     * in message length before calculating M-I 
    21202150     */ 
    2121     if (amsgint && afingerprint) { 
    2122         body_len = (pj_uint16_t)((buf - start) - 20 + 24 + 8); 
    2123     } else if (amsgint) { 
     2151    if (amsgint) { 
    21242152        body_len = (pj_uint16_t)((buf - start) - 20 + 24); 
    2125     } else if (afingerprint) { 
    2126         body_len = (pj_uint16_t)((buf - start) - 20 + 8); 
    21272153    } else { 
    21282154        body_len = (pj_uint16_t)((buf - start) - 20); 
     
    21622188        pj_hmac_sha1_init(&ctx, (pj_uint8_t*)key->ptr, key->slen); 
    21632189        pj_hmac_sha1_update(&ctx, (pj_uint8_t*)start, buf-start); 
    2164         if ((buf-start) & 0x3F) { 
    2165             pj_uint8_t zeroes[64]; 
    2166             pj_bzero(zeroes, sizeof(zeroes)); 
    2167             pj_hmac_sha1_update(&ctx, zeroes, 64-((buf-start) & 0x3F)); 
    2168         } 
     2190        // These are obsoleted in rfc3489bis-08 
     2191        //if ((buf-start) & 0x3F) { 
     2192        //    pj_uint8_t zeroes[64]; 
     2193        //    pj_bzero(zeroes, sizeof(zeroes)); 
     2194        //    pj_hmac_sha1_update(&ctx, zeroes, 64-((buf-start) & 0x3F)); 
     2195        //} 
    21692196        pj_hmac_sha1_final(&ctx, amsgint->hmac); 
    21702197 
     
    21812208    /* Calculate FINGERPRINT if present */ 
    21822209    if (afingerprint != NULL) { 
     2210        /* Update message length */ 
     2211        PUTVAL16H(start, 2,  
     2212                 (pj_uint16_t)(GETVAL16H(start, 2)+8)); 
     2213 
    21832214        afingerprint->value = pj_crc32_calc(start, buf-start); 
    21842215        afingerprint->value ^= STUN_XOR_FINGERPRINT; 
Note: See TracChangeset for help on using the changeset viewer.