blob: 05744bc994e1f9ce86a0190744fb2ba9f9610a39 [file] [log] [blame]
Benny Prijono9033e312005-11-21 02:08:39 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono9033e312005-11-21 02:08:39 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pj/guid.h>
20#include <pj/os.h>
21#include <pj/rand.h>
22#include <pj/string.h>
Benny Prijono9033e312005-11-21 02:08:39 +000023
24const unsigned PJ_GUID_STRING_LENGTH=20;
25
26static void init_mac_address(unsigned char mac_addr[16])
27{
28 unsigned long *ulval1 = (unsigned long*) &mac_addr[0];
29 unsigned short *usval1 = (unsigned short*) &mac_addr[4];
30
31 *ulval1 = pj_rand();
32 *usval1 = (unsigned short) pj_rand();
33}
34
35PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str)
36{
37 static int guid_initialized;
38 static unsigned pid;
Benny Prijonoe85bc412006-07-29 20:29:24 +000039 static char str_pid[32];
40 static unsigned char mac_addr[32];
41 static char str_mac_addr[32];
Benny Prijono9033e312005-11-21 02:08:39 +000042 static unsigned clock_seq;
43
44 PJ_CHECK_STACK();
45
46 if (guid_initialized == 0) {
47 pid = pj_getpid();
48 init_mac_address(mac_addr);
49 clock_seq = 0;
50
51 sprintf(str_pid, "%04x", pid);
52 sprintf(str_mac_addr, "%02x%02x%02x%02x%02x%02x",
53 mac_addr[0], mac_addr[1], mac_addr[2],
54 mac_addr[3], mac_addr[4], mac_addr[5]);
55
56 guid_initialized = 1;
57 }
58
59 strcpy(str->ptr, str_pid);
60 sprintf(str->ptr+4, "%04x", clock_seq++);
61 pj_memcpy(str->ptr+8, str_mac_addr, 12);
62 str->slen = 20;
63
64 return str;
65}
66