Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 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> |
| 23 | #include <pj/compat/sprintf.h> |
| 24 | |
| 25 | const unsigned PJ_GUID_STRING_LENGTH=20; |
| 26 | |
| 27 | static 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 | |
| 36 | PJ_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 | |