blob: f802417325e9341090ba75db164085e2cb2d9ab3 [file] [log] [blame]
Benny Prijonodd859a62005-11-01 16:42:51 +00001/* $Header: /pjproject-0.3/pjlib/src/pj/guid_simple.c 3 10/14/05 12:26a Bennylp $ */
2/* $Log: /pjproject-0.3/pjlib/src/pj/guid_simple.c $
3 *
4 * 3 10/14/05 12:26a Bennylp
5 * Finished error code framework, some fixes in ioqueue, etc. Pretty
6 * major.
7 *
8 * 2 9/17/05 10:37a Bennylp
9 * Major reorganization towards version 0.3.
10 *
11 */
12#include <pj/guid.h>
13#include <pj/os.h>
14#include <pj/rand.h>
15#include <pj/string.h>
16#include <pj/compat/sprintf.h>
17
18const unsigned PJ_GUID_STRING_LENGTH=20;
19
20static void init_mac_address(unsigned char mac_addr[16])
21{
22 unsigned long *ulval1 = (unsigned long*) &mac_addr[0];
23 unsigned short *usval1 = (unsigned short*) &mac_addr[4];
24
25 *ulval1 = pj_rand();
26 *usval1 = (unsigned short) pj_rand();
27}
28
29PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str)
30{
31 static int guid_initialized;
32 static unsigned pid;
33 static char str_pid[5];
34 static unsigned char mac_addr[6];
35 static char str_mac_addr[16];
36 static unsigned clock_seq;
37
38 PJ_CHECK_STACK();
39
40 if (guid_initialized == 0) {
41 pid = pj_getpid();
42 init_mac_address(mac_addr);
43 clock_seq = 0;
44
45 sprintf(str_pid, "%04x", pid);
46 sprintf(str_mac_addr, "%02x%02x%02x%02x%02x%02x",
47 mac_addr[0], mac_addr[1], mac_addr[2],
48 mac_addr[3], mac_addr[4], mac_addr[5]);
49
50 guid_initialized = 1;
51 }
52
53 strcpy(str->ptr, str_pid);
54 sprintf(str->ptr+4, "%04x", clock_seq++);
55 pj_memcpy(str->ptr+8, str_mac_addr, 12);
56 str->slen = 20;
57
58 return str;
59}
60