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

git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@1815 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjlib/src/pj/guid_simple.c b/pjlib/src/pj/guid_simple.c
index 0f3d085..031f6b1 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;
 
-    *ulval1 = pj_rand();
-    *usval1 = (unsigned short) pj_rand();
+    for (i=0; i<10; ++i)
+	*p++ = '0'+i;
+
+    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;
-
-	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]);
-
-	guid_initialized = 1;
+    if (guid_chars[0] == '\0') {
+	pj_enter_critical_section();
+	if (guid_chars[0] == '\0') {
+	    init_guid_chars();
+	}
+	pj_enter_critical_section();
     }
 
-    strcpy(str->ptr, str_pid);
-    sprintf(str->ptr+4, "%08x", clock_seq++);
-    pj_memcpy(str->ptr+12, str_mac_addr, 8);
-    str->slen = 20;
+    /* This would only work if PJ_GUID_STRING_LENGTH is multiple of 2 bytes */
+    pj_assert(PJ_GUID_STRING_LENGTH % 2 == 0);
 
+    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];
+    }
+
+    str->slen = PJ_GUID_STRING_LENGTH;
     return str;
 }