diff --git a/ChangeLog b/ChangeLog index 24c0d536a..28b1c9d5a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Wed Dec 15 18:50:00 1999 Richard Frith-Macdonald + + * Source/NSHost.m: Update again - record current host name for duration + of process and cache hosts keyed on the name under which they were + created. This can result in multiple cache entries for the same host, + but this seems to be necessary as DNS name <--> address mappings can + be asymmetric. + Thanks to dan@services.iiruc.ro for pointing this out. + Wed Dec 15 17:30:00 1999 Richard Frith-Macdonald * Source/Foundation/NSObject.h: dealloc notification stuff for Helge diff --git a/Source/NSHost.m b/Source/NSHost.m index 2db4928c2..16c82718b 100644 --- a/Source/NSHost.m +++ b/Source/NSHost.m @@ -69,6 +69,11 @@ static NSMutableDictionary *_hostCache = nil; { return nil; } + if (name == nil) + { + RELEASE(self); + return nil; + } if (entry == (struct hostent*)NULL) { RELEASE(self); @@ -99,26 +104,7 @@ static NSMutableDictionary *_hostCache = nil; if (_hostCacheEnabled == YES) { - NSEnumerator *enumerator; - NSString *key; - - enumerator = [_names objectEnumerator]; - while ((key = [enumerator nextObject]) != nil) - { - [_hostCache setObject: self forKey: key]; - } - enumerator = [_addresses objectEnumerator]; - while ((key = [enumerator nextObject]) != nil) - { - [_hostCache setObject: self forKey: key]; - } - /* - * In addition to official names, use the name we were called with. - */ - if (name != nil) - { - [_hostCache setObject: self forKey: name]; - } + [_hostCache setObject: self forKey: name]; } return self; @@ -128,38 +114,38 @@ static NSMutableDictionary *_hostCache = nil; @implementation NSHost -- (id) init -{ - [self dealloc]; - return nil; -} - -+ (void) initialize -{ - if (self == [NSHost class]) - { - _hostCacheLock = [[NSLock alloc] init]; - _hostCache = [NSMutableDictionary new]; - } -} - /* * Max hostname length in line with RFC 1123 */ #define GSMAXHOSTNAMELEN 255 +static NSString *myHost = nil; + ++ (void) initialize +{ + if (self == [NSHost class]) + { + char buf[GSMAXHOSTNAMELEN+1]; + int res; + + res = gethostname(buf, GSMAXHOSTNAMELEN); + if (res < 0) + { + NSLog(@"Unable to get name of current host - using 'localhost'"); + myHost = @"localhost"; + } + else + { + myHost = [[NSString alloc] initWithCString: buf]; + } + _hostCacheLock = [[NSLock alloc] init]; + _hostCache = [NSMutableDictionary new]; + } +} + + (NSHost*) currentHost { - char name[GSMAXHOSTNAMELEN+1]; - int res; - - res = gethostname(name, GSMAXHOSTNAMELEN); - if (res < 0) - { - return nil; - } - name[GSMAXHOSTNAMELEN] = '\0'; - return [self hostWithName: [NSString stringWithCString: name]]; + return [self hostWithName: myHost]; } + (NSHost*) hostWithName: (NSString*)name @@ -168,7 +154,7 @@ static NSMutableDictionary *_hostCache = nil; if (name == nil) { - NSLog(@"Nil host name sent to +[NSHost hostWithName]"); + NSLog(@"Nil host name sent to [NSHost +hostWithName:]"); return nil; } @@ -196,7 +182,7 @@ static NSMutableDictionary *_hostCache = nil; if (address == nil) { - NSLog(@"Nil host address sent to +[NSHost hostWithName]"); + NSLog(@"Nil host address sent to [NSHost +hostWithName:]"); return nil; } @@ -289,6 +275,19 @@ static NSMutableDictionary *_hostCache = nil; return host; } +- (void) dealloc +{ + RELEASE(_names); + RELEASE(_addresses); + [super dealloc]; +} + +- (id) init +{ + [self dealloc]; + return nil; +} + /* * The OpenStep spec says that [-hash] must be the same for any two * objects that [-isEqual: ] returns YES for. We have a problem in @@ -326,11 +325,6 @@ static NSMutableDictionary *_hostCache = nil; if ([_addresses containsObject: [a objectAtIndex: i]]) return YES; - a = [aHost names]; - for (i = 0; i < [a count]; i++) - if ([_addresses containsObject: [a objectAtIndex: i]]) - return YES; - return NO; } @@ -360,11 +354,4 @@ static NSMutableDictionary *_hostCache = nil; [self name], [self names], [self addresses]]; } -- (void) dealloc -{ - RELEASE(_names); - RELEASE(_addresses); - [super dealloc]; -} - @end diff --git a/Testing/nshost.m b/Testing/nshost.m index bca17a5d1..ba1a37355 100644 --- a/Testing/nshost.m +++ b/Testing/nshost.m @@ -37,12 +37,24 @@ main () displayHost(a); printf("c:%lx, n:%lx, a:%lx\n", c, n, a); + printf("c isEqual: n ... %d\n", [c isEqual: n]); + printf("n isEqual: c ... %d\n", [n isEqual: c]); + printf("c isEqual: a ... %d\n", [c isEqual: a]); + printf("a isEqual: c ... %d\n", [a isEqual: c]); + printf("n isEqual: a ... %d\n", [n isEqual: a]); + printf("a isEqual: n ... %d\n", [a isEqual: n]); [NSHost setHostCacheEnabled:NO]; n = [NSHost hostWithName:[c name]]; displayHost(n); printf("c:%lx, n:%lx, a:%lx\n", c, n, a); + printf("c isEqual: n ... %d\n", [c isEqual: n]); + printf("n isEqual: c ... %d\n", [n isEqual: c]); + printf("c isEqual: a ... %d\n", [c isEqual: a]); + printf("a isEqual: c ... %d\n", [a isEqual: c]); + printf("n isEqual: a ... %d\n", [n isEqual: a]); + printf("a isEqual: n ... %d\n", [a isEqual: n]); [arp release]; exit (0);