blob: d2b25569ea846622d1f7897f9db3ccd844f1f9b9 [file] [log] [blame]
Benny Prijonoe0312a72005-11-18 00:16:43 +00001/* $Id$ */
Benny Prijonoe7224612005-11-13 19:40:44 +00002/*
Benny Prijonoe0312a72005-11-18 00:16:43 +00003 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
Benny Prijonoe7224612005-11-13 19:40:44 +00004 *
Benny Prijonoe0312a72005-11-18 00:16:43 +00005 * 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.
Benny Prijonoe7224612005-11-13 19:40:44 +00009 *
Benny Prijonoe0312a72005-11-18 00:16:43 +000010 * This program is distributed in the hope that it will be useful,
Benny Prijonoe7224612005-11-13 19:40:44 +000011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Benny Prijonoe0312a72005-11-18 00:16:43 +000012 * 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
Benny Prijonoe7224612005-11-13 19:40:44 +000018 */
19#include <pj/guid.h>
20#include <pj/os.h>
21#include <pj/rand.h>
22#include <pj/string.h>
23#include <pj/compat/sprintf.h>
24
25const unsigned PJ_GUID_STRING_LENGTH=20;
26
27static void init_mac_address(unsigned char mac_addr[16])
28{
29 unsigned long *ulval1 = (unsigned long*) &mac_addr[0];
30 unsigned short *usval1 = (unsigned short*) &mac_addr[4];
31
32 *ulval1 = pj_rand();
33 *usval1 = (unsigned short) pj_rand();
34}
35
36PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str)
37{
38 static int guid_initialized;
39 static unsigned pid;
40 static char str_pid[5];
41 static unsigned char mac_addr[6];
42 static char str_mac_addr[16];
43 static unsigned clock_seq;
44
45 PJ_CHECK_STACK();
46
47 if (guid_initialized == 0) {
48 pid = pj_getpid();
49 init_mac_address(mac_addr);
50 clock_seq = 0;
51
52 sprintf(str_pid, "%04x", pid);
53 sprintf(str_mac_addr, "%02x%02x%02x%02x%02x%02x",
54 mac_addr[0], mac_addr[1], mac_addr[2],
55 mac_addr[3], mac_addr[4], mac_addr[5]);
56
57 guid_initialized = 1;
58 }
59
60 strcpy(str->ptr, str_pid);
61 sprintf(str->ptr+4, "%04x", clock_seq++);
62 pj_memcpy(str->ptr+8, str_mac_addr, 12);
63 str->slen = 20;
64
65 return str;
66}
67