diff options
author | Benny Prijono <bennylp@teluu.com> | 2008-02-21 21:36:34 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2008-02-21 21:36:34 +0000 |
commit | 2fe9c49e7d023c2efcc69ff835c49a3c3d8662e5 (patch) | |
tree | ce9de86943760d878966a43bbb9978895d84e170 /pjlib | |
parent | 9dafbac6b6e70d84ed1f925c1ec1ee00b5c4411d (diff) |
Ticket #412: increased randomness of guid_simple.c to 192-bits, and seed random number generator in pjsua_core.c
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1815 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib')
-rw-r--r-- | pjlib/include/pj/guid.h | 3 | ||||
-rw-r--r-- | pjlib/src/pj/guid_simple.c | 61 |
2 files changed, 35 insertions, 29 deletions
diff --git a/pjlib/include/pj/guid.h b/pjlib/include/pj/guid.h index a437646f..5ce1adf0 100644 --- a/pjlib/include/pj/guid.h +++ b/pjlib/include/pj/guid.h @@ -49,8 +49,7 @@ PJ_BEGIN_DECL * PJ_GUID_STRING_LENGTH specifies length of GUID string. The value is * dependent on the algorithm used internally to generate the GUID string. * If real GUID generator is used, then the length will be between 32 and - * 36 bytes. If shadow GUID generator is used, then the length - * will be 20 bytes. Application should not assume which algorithm will + * 36 bytes. Application should not assume which algorithm will * be used by GUID generator. * * Regardless of the actual length of the GUID, it will not exceed diff --git a/pjlib/src/pj/guid_simple.c b/pjlib/src/pj/guid_simple.c index 0f3d0853..031f6b12 100644 --- a/pjlib/src/pj/guid_simple.c +++ b/pjlib/src/pj/guid_simple.c @@ -17,55 +17,62 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include <pj/guid.h> -#include <pj/os.h> +#include <pj/assert.h> #include <pj/rand.h> +#include <pj/os.h> #include <pj/string.h> -PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=20; +PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=32; + +static char guid_chars[64]; PJ_DEF(unsigned) pj_GUID_STRING_LENGTH() { return PJ_GUID_STRING_LENGTH; } -static void init_mac_address(unsigned char mac_addr[16]) +static void init_guid_chars(void) { - unsigned long *ulval1 = (unsigned long*) &mac_addr[0]; - unsigned short *usval1 = (unsigned short*) &mac_addr[4]; + char *p = guid_chars; + unsigned i; + + for (i=0; i<10; ++i) + *p++ = '0'+i; - *ulval1 = pj_rand(); - *usval1 = (unsigned short) pj_rand(); + for (i=0; i<26; ++i) { + *p++ = 'a'+i; + *p++ = 'A'+i; + } + + *p++ = '-'; + *p++ = '.'; } PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) { - static int guid_initialized; - static unsigned pid; - static char str_pid[32]; - static unsigned char mac_addr[32]; - static char str_mac_addr[32]; - static unsigned clock_seq; + char *p, *end; PJ_CHECK_STACK(); - if (guid_initialized == 0) { - pid = pj_getpid(); - init_mac_address(mac_addr); - clock_seq = 0; + if (guid_chars[0] == '\0') { + pj_enter_critical_section(); + if (guid_chars[0] == '\0') { + init_guid_chars(); + } + pj_enter_critical_section(); + } - sprintf(str_pid, "%04x", pid); - sprintf(str_mac_addr, "%02x%02x%02x%02x%02x%02x", - mac_addr[0], mac_addr[1], mac_addr[2], - mac_addr[3], mac_addr[4], mac_addr[5]); + /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */ + pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0); - guid_initialized = 1; + for (p=str->ptr, end=p+PJ_GUID_STRING_LENGTH; p<end; ) { + /* Assumes rand() only has 16bit randomness */ + unsigned short val = pj_rand(); + *p++ = guid_chars[(val >> 8) & 63]; + *p++ = guid_chars[(val & 0xFF) & 63]; } - strcpy(str->ptr, str_pid); - sprintf(str->ptr+4, "%08x", clock_seq++); - pj_memcpy(str->ptr+12, str_mac_addr, 8); - str->slen = 20; - + str->slen = PJ_GUID_STRING_LENGTH; return str; } |