00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 #include <ptlib.h>
00079
00080 #ifdef __GNUC__
00081 #pragma implementation "guid.h"
00082 #endif
00083
00084 #include "guid.h"
00085
00086 #include <ptlib/sockets.h>
00087 #include <ptclib/random.h>
00088 #include <ptclib/asner.h>
00089
00090
00091 #define GUID_SIZE 16
00092
00093
00095
00096 OpalGloballyUniqueID::OpalGloballyUniqueID()
00097 : PBYTEArray(GUID_SIZE)
00098 {
00099
00100 PInt64 timestamp;
00101 static PInt64 deltaTime = PInt64(10000000)*24*60*60*
00102 ( 16
00103 + 31
00104 + 30
00105 #ifdef _WIN32
00106 + (1601-1583)*365
00107 + (1601-1583)/4);
00108
00109
00110 #ifndef _WIN32_WCE
00111 GetSystemTimeAsFileTime((LPFILETIME)×tamp);
00112 #else
00113 SYSTEMTIME SystemTime;
00114 GetSystemTime(&SystemTime);
00115 SystemTimeToFileTime(&SystemTime, (LPFILETIME)×tamp);
00116 #endif // _WIN32_WCE
00117
00118 timestamp /= 100;
00119 #else // _WIN32
00120 + (1970-1583)*365
00121 + (1970-1583)/4
00122 - 3);
00123
00124 #ifdef P_VXWORKS
00125 struct timespec ts;
00126 clock_gettime(0,&ts);
00127 timestamp = (ts.tv_sec*(PInt64)1000000 + ts.tv_nsec*1000)*10;
00128 #else
00129 struct timeval tv;
00130 gettimeofday(&tv, NULL);
00131 timestamp = (tv.tv_sec*(PInt64)1000000 + tv.tv_usec)*10;
00132 #endif // P_VXWORKS
00133 #endif // _WIN32
00134
00135 timestamp += deltaTime;
00136
00137 theArray[0] = (BYTE)(timestamp&0xff);
00138 theArray[1] = (BYTE)((timestamp>>8)&0xff);
00139 theArray[2] = (BYTE)((timestamp>>16)&0xff);
00140 theArray[3] = (BYTE)((timestamp>>24)&0xff);
00141 theArray[4] = (BYTE)((timestamp>>32)&0xff);
00142 theArray[5] = (BYTE)((timestamp>>40)&0xff);
00143 theArray[6] = (BYTE)((timestamp>>48)&0xff);
00144 theArray[7] = (BYTE)(((timestamp>>56)&0x0f) + 0x10);
00145
00146 static WORD clockSequence = (WORD)PRandom::Number();
00147 static PInt64 lastTimestamp = 0;
00148 if (lastTimestamp < timestamp)
00149 lastTimestamp = timestamp;
00150 else
00151 clockSequence++;
00152
00153 theArray[8] = (BYTE)(((clockSequence>>8)&0x1f) | 0x80);
00154 theArray[9] = (BYTE)clockSequence;
00155
00156 static PEthSocket::Address macAddress;
00157 static BOOL needMacAddress = TRUE;
00158 if (needMacAddress) {
00159 PIPSocket::InterfaceTable interfaces;
00160 if (PIPSocket::GetInterfaceTable(interfaces)) {
00161 for (PINDEX i = 0; i < interfaces.GetSize(); i++) {
00162 PString macAddrStr = interfaces[i].GetMACAddress();
00163 if (!macAddrStr && macAddrStr != "44-45-53-54-00-00") {
00164 macAddress = macAddrStr;
00165 if (macAddress != NULL) {
00166 needMacAddress = FALSE;
00167 break;
00168 }
00169 }
00170 }
00171 }
00172
00173 if (needMacAddress) {
00174 PRandom rand;
00175 macAddress.ls.l = rand;
00176 macAddress.ls.s = (WORD)rand;
00177 macAddress.b[0] |= '\x80';
00178
00179 needMacAddress = FALSE;
00180 }
00181 }
00182
00183 memcpy(theArray+10, macAddress.b, 6);
00184 }
00185
00186
00187 OpalGloballyUniqueID::OpalGloballyUniqueID(const char * cstr)
00188 : PBYTEArray(GUID_SIZE)
00189 {
00190 if (cstr != NULL && *cstr != '\0') {
00191 PStringStream strm(cstr);
00192 ReadFrom(strm);
00193 }
00194 }
00195
00196
00197 OpalGloballyUniqueID::OpalGloballyUniqueID(const PString & str)
00198 : PBYTEArray(GUID_SIZE)
00199 {
00200 PStringStream strm(str);
00201 ReadFrom(strm);
00202 }
00203
00204
00205 OpalGloballyUniqueID::OpalGloballyUniqueID(const PASN_OctetString & newId)
00206 : PBYTEArray(newId)
00207 {
00208 PAssert(GetSize() == GUID_SIZE, PInvalidParameter);
00209 SetSize(GUID_SIZE);
00210 }
00211
00212
00213 PObject * OpalGloballyUniqueID::Clone() const
00214 {
00215 PAssert(GetSize() == GUID_SIZE, "OpalGloballyUniqueID is invalid size");
00216
00217 return new OpalGloballyUniqueID(*this);
00218 }
00219
00220
00221 PINDEX OpalGloballyUniqueID::HashFunction() const
00222 {
00223 PAssert(GetSize() == GUID_SIZE, "OpalGloballyUniqueID is invalid size");
00224
00225 DWORD * words = (DWORD *)theArray;
00226 DWORD sum = words[0] + words[1] + words[2] + words[3];
00227 return ((sum >> 25)+(sum >> 15)+sum)%23;
00228 }
00229
00230
00231 void OpalGloballyUniqueID::PrintOn(ostream & strm) const
00232 {
00233 PAssert(GetSize() == GUID_SIZE, "OpalGloballyUniqueID is invalid size");
00234
00235 char fillchar = strm.fill();
00236 strm << hex << setfill('0')
00237 << setw(2) << (unsigned)(BYTE)theArray[0]
00238 << setw(2) << (unsigned)(BYTE)theArray[1]
00239 << setw(2) << (unsigned)(BYTE)theArray[2]
00240 << setw(2) << (unsigned)(BYTE)theArray[3] << '-'
00241 << setw(2) << (unsigned)(BYTE)theArray[4]
00242 << setw(2) << (unsigned)(BYTE)theArray[5] << '-'
00243 << setw(2) << (unsigned)(BYTE)theArray[6]
00244 << setw(2) << (unsigned)(BYTE)theArray[7] << '-'
00245 << setw(2) << (unsigned)(BYTE)theArray[8]
00246 << setw(2) << (unsigned)(BYTE)theArray[9] << '-'
00247 << setw(2) << (unsigned)(BYTE)theArray[10]
00248 << setw(2) << (unsigned)(BYTE)theArray[11]
00249 << setw(2) << (unsigned)(BYTE)theArray[12]
00250 << setw(2) << (unsigned)(BYTE)theArray[13]
00251 << setw(2) << (unsigned)(BYTE)theArray[14]
00252 << setw(2) << (unsigned)(BYTE)theArray[15]
00253 << dec << setfill(fillchar);
00254 }
00255
00256
00257 void OpalGloballyUniqueID::ReadFrom(istream & strm)
00258 {
00259 PAssert(GetSize() == GUID_SIZE, "OpalGloballyUniqueID is invalid size");
00260 SetSize(16);
00261
00262 strm >> ws;
00263
00264 PINDEX count = 0;
00265
00266 while (count < 2*GUID_SIZE) {
00267 if (isxdigit(strm.peek())) {
00268 char digit = (char)(strm.get() - '0');
00269 if (digit >= 10) {
00270 digit -= 'A'-('9'+1);
00271 if (digit >= 16)
00272 digit -= 'a'-'A';
00273 }
00274 theArray[count/2] = (BYTE)((theArray[count/2] << 4) | digit);
00275 count++;
00276 }
00277 else if (strm.peek() == '-') {
00278 if (count != 8 && count != 12 && count != 16 && count != 20)
00279 break;
00280 strm.get();
00281 }
00282 else
00283 break;
00284 }
00285
00286 if (count < 2*GUID_SIZE) {
00287 memset(theArray, 0, GUID_SIZE);
00288 strm.clear(ios::failbit);
00289 }
00290 }
00291
00292
00293 PString OpalGloballyUniqueID::AsString() const
00294 {
00295 PStringStream strm;
00296 PrintOn(strm);
00297 return strm;
00298 }
00299
00300
00301 BOOL OpalGloballyUniqueID::IsNULL() const
00302 {
00303 PAssert(GetSize() == GUID_SIZE, "OpalGloballyUniqueID is invalid size");
00304
00305 return memcmp(theArray, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16) == 0;
00306 }
00307
00308