Changeset 1815


Ignore:
Timestamp:
Feb 21, 2008 9:36:34 PM (17 years ago)
Author:
bennylp
Message:

Ticket #412: increased randomness of guid_simple.c to 192-bits, and seed random number generator in pjsua_core.c

Location:
pjproject/trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/include/pj/guid.h

    r1417 r1815  
    5050 * dependent on the algorithm used internally to generate the GUID string. 
    5151 * If real GUID generator is used, then the length will be between 32 and 
    52  * 36 bytes. If shadow GUID generator is used, then the length 
    53  * will be 20 bytes. Application should not assume which algorithm will 
     52 * 36 bytes. Application should not assume which algorithm will 
    5453 * be used by GUID generator. 
    5554 * 
  • pjproject/trunk/pjlib/src/pj/guid_simple.c

    r1417 r1815  
    1818 */ 
    1919#include <pj/guid.h> 
     20#include <pj/assert.h> 
     21#include <pj/rand.h> 
    2022#include <pj/os.h> 
    21 #include <pj/rand.h> 
    2223#include <pj/string.h> 
    2324 
    24 PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=20; 
     25PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=32; 
     26 
     27static char guid_chars[64]; 
    2528 
    2629PJ_DEF(unsigned) pj_GUID_STRING_LENGTH() 
     
    2932} 
    3033 
    31 static void init_mac_address(unsigned char mac_addr[16]) 
     34static void init_guid_chars(void) 
    3235{ 
    33     unsigned long *ulval1 = (unsigned long*) &mac_addr[0]; 
    34     unsigned short *usval1 = (unsigned short*) &mac_addr[4]; 
     36    char *p = guid_chars; 
     37    unsigned i; 
    3538 
    36     *ulval1 = pj_rand(); 
    37     *usval1 = (unsigned short) pj_rand(); 
     39    for (i=0; i<10; ++i) 
     40        *p++ = '0'+i; 
     41 
     42    for (i=0; i<26; ++i) { 
     43        *p++ = 'a'+i; 
     44        *p++ = 'A'+i; 
     45    } 
     46 
     47    *p++ = '-'; 
     48    *p++ = '.'; 
    3849} 
    3950 
    4051PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) 
    4152{ 
    42     static int guid_initialized; 
    43     static unsigned pid; 
    44     static char str_pid[32]; 
    45     static unsigned char mac_addr[32]; 
    46     static char str_mac_addr[32]; 
    47     static unsigned clock_seq; 
     53    char *p, *end; 
    4854 
    4955    PJ_CHECK_STACK(); 
    5056 
    51     if (guid_initialized == 0) { 
    52         pid = pj_getpid(); 
    53         init_mac_address(mac_addr); 
    54         clock_seq = 0; 
    55  
    56         sprintf(str_pid, "%04x", pid); 
    57         sprintf(str_mac_addr, "%02x%02x%02x%02x%02x%02x", 
    58             mac_addr[0], mac_addr[1], mac_addr[2], 
    59             mac_addr[3], mac_addr[4], mac_addr[5]); 
    60  
    61         guid_initialized = 1; 
     57    if (guid_chars[0] == '\0') { 
     58        pj_enter_critical_section(); 
     59        if (guid_chars[0] == '\0') { 
     60            init_guid_chars(); 
     61        } 
     62        pj_enter_critical_section(); 
    6263    } 
    6364 
    64     strcpy(str->ptr, str_pid); 
    65     sprintf(str->ptr+4, "%08x", clock_seq++); 
    66     pj_memcpy(str->ptr+12, str_mac_addr, 8); 
    67     str->slen = 20; 
     65    /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */ 
     66    pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0); 
    6867 
     68    for (p=str->ptr, end=p+PJ_GUID_STRING_LENGTH; p<end; ) { 
     69        /* Assumes rand() only has 16bit randomness */ 
     70        unsigned short val = pj_rand(); 
     71        *p++ = guid_chars[(val >> 8)   & 63]; 
     72        *p++ = guid_chars[(val & 0xFF) & 63]; 
     73    } 
     74 
     75    str->slen = PJ_GUID_STRING_LENGTH; 
    6976    return str; 
    7077} 
  • pjproject/trunk/pjnath/build/Makefile

    r1495 r1815  
    7272# $(TARGET) is defined in os-$(OS_NAME).mak file in current directory. 
    7373# 
    74 TARGETS := pjnath pjnath-test pjstun-client pjstun-srv-test 
     74TARGETS := pjnath pjnath-test  
     75#pjstun-client pjstun-srv-test 
    7576 
    7677all: $(TARGETS) 
  • pjproject/trunk/pjsip/src/pjsua-lib/pjsua_core.c

    r1735 r1815  
    512512 
    513513 
     514/* Init random seed */ 
     515static void init_random_seed(void) 
     516{ 
     517    pj_sockaddr addr; 
     518    const pj_str_t *hostname; 
     519    pj_uint32_t pid; 
     520    pj_time_val t; 
     521    unsigned seed=0; 
     522 
     523    /* Add hostname */ 
     524    hostname = pj_gethostname(); 
     525    seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen); 
     526 
     527    /* Add primary IP address */ 
     528    if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS) 
     529        seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4); 
     530 
     531    /* Get timeofday */ 
     532    pj_gettimeofday(&t); 
     533    seed = pj_hash_calc(seed, &t, sizeof(t)); 
     534 
     535    /* Add PID */ 
     536    pid = pj_getpid(); 
     537    seed = pj_hash_calc(seed, &pid, sizeof(pid)); 
     538 
     539    /* Init random seed */ 
     540    pj_srand(seed); 
     541} 
     542 
    514543/* 
    515544 * Instantiate pjsua application. 
     
    529558    PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); 
    530559 
     560    /* Init random seed */ 
     561    init_random_seed(); 
    531562 
    532563    /* Init PJLIB-UTIL: */ 
Note: See TracChangeset for help on using the changeset viewer.