1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/**
EOGlobalID.m <title>EOGlobalID</title>
Copyright (C) 2000-2002,2003,2004,2005 Free Software Foundation, Inc.
Author: Mirko Viviani <mirko.viviani@gmail.com>
Date: February 2000
$Revision$
$Date$
<abstract></abstract>
This file is part of the GNUstep Database Library.
<license>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
</license>
**/
#include "config.h"
#ifdef GNUSTEP
#include <Foundation/NSArray.h>
#include <Foundation/NSCoder.h>
#include <Foundation/NSData.h>
#include <Foundation/NSHost.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSString.h>
#else
#include <Foundation/Foundation.h>
#endif
#ifndef GNUSTEP
#include <GNUstepBase/GNUstep.h>
#endif
#include <EOControl/EOGlobalID.h>
#include <EOControl/EODebug.h>
#include <string.h>
#include <limits.h>
NSString *EOGlobalIDChangedNotification = @"EOGlobalIDChangedNotification";
@implementation EOGlobalID
+ (void)initialize
{
if (self == [EOGlobalID class])
{
Class cls = NSClassFromString(@"EODatabaseContext");
if (cls != Nil)
[cls class]; // Insure correct initialization.
}
}
- (BOOL)isEqual: (id)other
{
return NO;
}
- (NSUInteger)hash
{
return 0;
}
- (BOOL)isTemporary
{
return NO;
}
- (id)copyWithZone: (NSZone *)zone
{
return RETAIN(self);
}
@end
@implementation EOTemporaryGlobalID
static unsigned short sequence = USHRT_MAX;
+ (EOTemporaryGlobalID *)temporaryGlobalID
{
return [[[self alloc] init] autorelease];
}
/**
* Fills the supplied buffer with 12 bytes that are unique to the
* subnet. The first two bytes encode a sequence of the process.
* Then two bytes encode the process ID followed by four bytes which
* encode a time stamp. The last four bytes encode the IP address.
* The caller must insure that the buffer pointed to is large enough
* to hold the twelve bytes.
*/
+ (void)assignGloballyUniqueBytes: (unsigned char *)buffer
{
static int pid = 0;
static union { unsigned int i; unsigned char c[4]; } ipComp = { .i = 0 };
unsigned char *bPtr;
unsigned short seq;
unsigned int i;
union { NSTimeInterval interval; unsigned long stamp; } time;
if (pid == 0)
{
NSString *ipString;
NSArray *ipComps;
pid = [[NSProcessInfo processInfo] processIdentifier];
pid %= 0xFFFF;
ipString = [[NSHost currentHost] address];
ipComps = [ipString componentsSeparatedByString: @"."];
if ([ipComps count] == 4)
{ /*IPv4*/
for (i=0; i<4; i++)
{
NSString *comp = [ipComps objectAtIndex: i];
ipComp.c[i] = (unsigned char)[comp intValue];
}
}
else
{ /*IPv6:
This is not globaly unique since we do not utilize
all available data, yet the same goes for RFC1918
IPv4 addresses so this should suffice in practice. */
/*According to C99 Annex J.1
The value of a union member other than the last one stored into
is unspecified behavior. But -base uses this to produce hash
values for NSNumber also and we are pretty much doing the same
here. (also see time union).
*/
ipComp.i = [ipString hash];
}
}
memset (buffer, 0, 12);
seq = sequence-- % 0xFFFF;
bPtr = (unsigned char *)&seq;
buffer[0] = bPtr[0];
buffer[1] = bPtr[1];
bPtr = (unsigned char *)&pid;
buffer[2] = bPtr[0];
buffer[3] = bPtr[1];
time.interval = [NSDate timeIntervalSinceReferenceDate];
time.stamp %= 0xFFFFFFFF;
bPtr = (unsigned char *)&time.stamp;
buffer[4] = bPtr[0];
buffer[5] = bPtr[1];
buffer[6] = bPtr[2];
buffer[7] = bPtr[3];
buffer[8] = ipComp.c[0];
buffer[9] = ipComp.c[1];
buffer[10] = ipComp.c[2];
buffer[11] = ipComp.c[3];
if (sequence == 0)
{
sequence = USHRT_MAX;
}
}
- (id)init
{
if ((self = [super init]))
{
[EOTemporaryGlobalID assignGloballyUniqueBytes:_bytes];
}
return self;
}
- (BOOL)isTemporary
{
return YES;
}
- (BOOL)isEqual: (id)other
{
if (self == other)
return YES;
if ([other isKindOfClass: [EOTemporaryGlobalID class]] == NO)
return NO;
if (!memcmp(_bytes, ((EOTemporaryGlobalID *)other)->_bytes, sizeof(_bytes)))
return YES;
return NO;
}
- (void)encodeWithCoder: (NSCoder *)coder
{
[coder encodeValueOfObjCType: @encode(unsigned) at: &_refCount];
[coder encodeValueOfObjCType: @encode(unsigned char[12]) at: _bytes];
}
- (id)initWithCoder: (NSCoder *)coder
{
self = [super init];
[coder decodeValueOfObjCType: @encode(unsigned) at: &_refCount];
[coder decodeValueOfObjCType: @encode(unsigned char[12]) at: _bytes];
return self;
}
- (NSString *)description
{
unsigned char dst[(EOUniqueBinaryKeyLength<<1) /* 2 x buffer */
+ (EOUniqueBinaryKeyLength>>2) /* + 1 space per 4 byte */
+ 1]; /* + terminator */
unsigned int i,j;
#define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
for (i = 0, j = 0; i < EOUniqueBinaryKeyLength; i++, j++)
{
dst[j] = num2char((_bytes[i]>>4) & 0x0f);
dst[++j] = num2char(_bytes[i] & 0x0f);
/* Insert a space per 4 bytes. */
if ((i & 3) == 3 && i < EOUniqueBinaryKeyLength-1)
{
dst[++j] = ' ';
}
}
dst[j] = 0;
return [NSString stringWithFormat: @"<%s %s>",
object_getClassName(self), dst];
}
@end
|