diff options
-rw-r--r-- | pjlib/include/pj/guid.h | 3 | ||||
-rw-r--r-- | pjlib/src/pj/guid_simple.c | 61 | ||||
-rw-r--r-- | pjnath/build/Makefile | 3 | ||||
-rw-r--r-- | pjsip/src/pjsua-lib/pjsua_core.c | 31 |
4 files changed, 68 insertions, 30 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; } diff --git a/pjnath/build/Makefile b/pjnath/build/Makefile index 42c8e12a..319893e3 100644 --- a/pjnath/build/Makefile +++ b/pjnath/build/Makefile @@ -71,7 +71,8 @@ export CC_OUT CC AR RANLIB HOST_MV HOST_RM HOST_RMDIR HOST_MKDIR OBJEXT LD LDOUT # # $(TARGET) is defined in os-$(OS_NAME).mak file in current directory. # -TARGETS := pjnath pjnath-test pjstun-client pjstun-srv-test +TARGETS := pjnath pjnath-test +#pjstun-client pjstun-srv-test all: $(TARGETS) diff --git a/pjsip/src/pjsua-lib/pjsua_core.c b/pjsip/src/pjsua-lib/pjsua_core.c index edb537c4..9f0ff6e7 100644 --- a/pjsip/src/pjsua-lib/pjsua_core.c +++ b/pjsip/src/pjsua-lib/pjsua_core.c @@ -511,6 +511,35 @@ static int worker_thread(void *arg) } +/* Init random seed */ +static void init_random_seed(void) +{ + pj_sockaddr addr; + const pj_str_t *hostname; + pj_uint32_t pid; + pj_time_val t; + unsigned seed=0; + + /* Add hostname */ + hostname = pj_gethostname(); + seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen); + + /* Add primary IP address */ + if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS) + seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4); + + /* Get timeofday */ + pj_gettimeofday(&t); + seed = pj_hash_calc(seed, &t, sizeof(t)); + + /* Add PID */ + pid = pj_getpid(); + seed = pj_hash_calc(seed, &pid, sizeof(pid)); + + /* Init random seed */ + pj_srand(seed); +} + /* * Instantiate pjsua application. */ @@ -528,6 +557,8 @@ PJ_DEF(pj_status_t) pjsua_create(void) status = pj_init(); PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); + /* Init random seed */ + init_random_seed(); /* Init PJLIB-UTIL: */ status = pjlib_util_init(); |