00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "copyrt.h"
00022 #include <stdio.h>
00023 #include <memory.h>
00024 #include <unistd.h>
00025 #include "sysdep.h"
00026
00027 #ifdef Darwin
00028 #include <mach/thread_act.h>
00029 #include <mach/thread_info.h>
00030 #include <mach/mach_init.h>
00031 #include <mach/mach.h>
00032 #include <mach/task.h>
00033 #include <mach/semaphore.h>
00034 #endif
00035
00036
00037
00038
00039 void get_ieee_node_identifier(uuid_node_t *node) {
00040 char seed[16];
00041 FILE * fd;
00042 static int inited = 0;
00043 static uuid_node_t saved_node;
00044
00045 if (!inited) {
00046 fd = fopen("nodeid", "rb");
00047 if (fd) {
00048 fread(&saved_node, sizeof(uuid_node_t), 1, fd);
00049 fclose(fd);
00050 }
00051 else {
00052 get_random_info(seed);
00053 seed[0] |= 0x80;
00054 memcpy(&saved_node, seed, sizeof(uuid_node_t));
00055 fd = fopen("nodeid", "wb");
00056 if (fd) {
00057 fwrite(&saved_node, sizeof(uuid_node_t), 1, fd);
00058 fclose(fd);
00059 };
00060 };
00061 inited = 1;
00062 };
00063
00064
00065 *node = saved_node;
00066 };
00067
00068
00069
00070
00071
00072 #ifdef _WINDOWS_
00073
00074 void get_system_time(uuid_time_t *uuid_time) {
00075 ULARGE_INTEGER time;
00076
00077 GetSystemTimeAsFileTime((FILETIME *)&time);
00078
00079
00080
00081
00082
00083
00084
00085 time.QuadPart +=
00086 (unsigned __int64) (1000*1000*10)
00087 * (unsigned __int64) (60 * 60 * 24)
00088 * (unsigned __int64) (17+30+31+365*18+5);
00089
00090 *uuid_time = time.QuadPart;
00091
00092 };
00093
00094
00095 void get_random_info(char seed[16]) {
00096 MD5_CTX c;
00097 typedef struct {
00098 MEMORYSTATUS m;
00099 SYSTEM_INFO s;
00100 FILETIME t;
00101 LARGE_INTEGER pc;
00102 DWORD tc;
00103 DWORD l;
00104 char hostname[MAX_COMPUTERNAME_LENGTH + 1];
00105 } randomness;
00106 randomness r;
00107
00108 MD5Init(&c);
00109
00110 GlobalMemoryStatus(&r.m);
00111
00112 GetSystemInfo(&r.s);
00113
00114 GetSystemTimeAsFileTime(&r.t);
00115
00116 QueryPerformanceCounter(&r.pc);
00117
00118 r.tc = GetTickCount();
00119 r.l = MAX_COMPUTERNAME_LENGTH + 1;
00120
00121 GetComputerName(r.hostname, &r.l );
00122 MD5Update(&c, &r, sizeof(randomness));
00123 MD5Final(seed, &c);
00124 };
00125 #else
00126
00127 void get_system_time(uuid_time_t *uuid_time)
00128 {
00129 struct timeval tp;
00130
00131 gettimeofday(&tp, (struct timezone *)0);
00132
00133
00134
00135
00136
00137 *uuid_time = (tp.tv_sec * 10000000) + (tp.tv_usec * 10) +
00138 I64(0x01B21DD213814000);
00139 };
00140
00141 #ifdef Darwin
00142
00143 void get_random_info(char seed[16]) {
00144 MD5_CTX c;
00145 typedef struct {
00146 struct thread_basic_info b;
00147 struct timeval t;
00148 char hostname[257];
00149 } randomness;
00150 randomness r;
00151
00152 MD5Init(&c);
00153
00154
00155 mach_msg_type_number_t my_info_size;
00156 mach_port_t my_thread_self;
00157 kern_return_t kr;
00158
00159 my_thread_self = mach_thread_self();
00160 my_info_size = sizeof(r.b)/sizeof(int);
00161 kr = thread_info(my_thread_self, THREAD_BASIC_INFO, (thread_info_t)&r.b,&my_info_size);
00162 mach_port_deallocate(mach_task_self(), my_thread_self);
00163
00164 if (kr != KERN_SUCCESS) {
00165
00166 }
00167 else {
00168
00169 }
00170
00171 gettimeofday(&r.t, (struct timezone *)0);
00172 gethostname(r.hostname, 256);
00173 MD5Update(&c, ((unsigned char*)(&r)), sizeof(randomness));
00174 MD5Final((unsigned char*)seed, &c);
00175 };
00176
00177 #else
00178
00179 void get_random_info(char seed[16]) {
00180 MD5_CTX c;
00181 typedef struct {
00182 struct sysinfo s;
00183 struct timeval t;
00184 char hostname[257];
00185 } randomness;
00186 randomness r;
00187
00188 MD5Init(&c);
00189 sysinfo(&r.s);
00190 gettimeofday(&r.t, (struct timezone *)0);
00191 gethostname(r.hostname, 256);
00192 MD5Update(&c, ((unsigned char*)(&r)), sizeof(randomness));
00193 MD5Final((unsigned char*)seed, &c);
00194 };
00195
00196 #endif
00197 #endif
00198