Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 4 | * |
| 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 Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 23 | |
| 24 | const unsigned PJ_GUID_STRING_LENGTH=20; |
| 25 | |
| 26 | static 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 | |
| 35 | PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) |
| 36 | { |
| 37 | static int guid_initialized; |
| 38 | static unsigned pid; |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 39 | static char str_pid[32]; |
| 40 | static unsigned char mac_addr[32]; |
| 41 | static char str_mac_addr[32]; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 42 | 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 | |