1997-03-03 19:40:50 +00:00
|
|
|
/* Implementation of host class
|
|
|
|
Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Luke Howard <lukeh@xedoc.com.au>
|
|
|
|
Date: 1996
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
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 2 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; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
#include <config.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
#include <base/preface.h>
|
1997-03-03 19:40:50 +00:00
|
|
|
#include <Foundation/NSLock.h>
|
|
|
|
#include <Foundation/NSHost.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <Foundation/NSArray.h>
|
1997-05-03 20:38:42 +00:00
|
|
|
#include <Foundation/NSDictionary.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <Foundation/NSString.h>
|
1998-03-05 00:37:44 +00:00
|
|
|
#include <Foundation/NSCoder.h>
|
1997-03-03 19:40:50 +00:00
|
|
|
#include <netdb.h>
|
1997-05-03 20:38:42 +00:00
|
|
|
/* #include <libc.h> */
|
1997-03-03 19:40:50 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
#include <Windows32/Sockets.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#else
|
1997-12-11 19:09:56 +00:00
|
|
|
#include <unistd.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
1997-03-03 19:40:50 +00:00
|
|
|
#endif /* WIN32 */
|
|
|
|
|
|
|
|
static NSLock *_hostCacheLock = nil;
|
|
|
|
static BOOL _hostCacheEnabled = NO;
|
|
|
|
static NSMutableDictionary *_hostCache = nil;
|
|
|
|
|
|
|
|
@interface NSHost (Private)
|
|
|
|
+ (NSHost *)_hostWithHostEntry:(struct hostent *)entry;
|
|
|
|
+ (NSHost *)_hostWithHostEntry:(struct hostent *)entry name:name;
|
|
|
|
- _initWithHostEntry:(struct hostent *)entry name:name;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSHost (Private)
|
|
|
|
|
|
|
|
+ (NSHost *)_hostWithHostEntry:(struct hostent *)entry
|
|
|
|
{
|
|
|
|
if (!entry)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
return [[self class] _hostWithHostEntry:entry
|
|
|
|
name:[NSString stringWithCString:entry->h_name]];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSHost *)_hostWithHostEntry:(struct hostent *)entry name:name
|
|
|
|
{
|
|
|
|
NSHost *res = nil;
|
|
|
|
|
|
|
|
[_hostCacheLock lock];
|
|
|
|
if (_hostCacheEnabled == YES)
|
|
|
|
{
|
|
|
|
res = [_hostCache objectForKey:name];
|
|
|
|
}
|
|
|
|
[_hostCacheLock unlock];
|
|
|
|
|
|
|
|
return (res != nil) ? res : [[[[self class] alloc]
|
|
|
|
_initWithHostEntry:entry name:name] autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- _initWithHostEntry:(struct hostent *)entry name:name
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *ptr;
|
|
|
|
struct in_addr in;
|
|
|
|
NSString *h_name;
|
|
|
|
|
|
|
|
[super init];
|
|
|
|
|
|
|
|
if (entry == (struct hostent *)NULL)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
names = [[NSMutableArray array] retain];
|
|
|
|
addresses = [[NSMutableArray array] retain];
|
|
|
|
|
|
|
|
[names addObject:name];
|
|
|
|
h_name = [NSString stringWithCString:entry->h_name];
|
|
|
|
|
|
|
|
if (![h_name isEqual:name])
|
|
|
|
{
|
|
|
|
[names addObject:h_name];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, ptr = entry->h_aliases[0]; ptr != NULL; i++,
|
|
|
|
ptr = entry->h_aliases[i])
|
|
|
|
{
|
|
|
|
[names addObject:[NSString stringWithCString:ptr]];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, ptr = entry->h_addr_list[0]; ptr != NULL; i++,
|
|
|
|
ptr = entry->h_addr_list[i])
|
|
|
|
{
|
1997-10-17 13:35:52 +00:00
|
|
|
memcpy((void *)&in.s_addr, (const void *)ptr,
|
1997-03-03 19:40:50 +00:00
|
|
|
entry->h_length);
|
|
|
|
[addresses addObject:[NSString
|
1998-07-29 09:22:18 +00:00
|
|
|
stringWithCString:(char*)inet_ntoa(in)]];
|
1997-03-03 19:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[_hostCacheLock lock];
|
|
|
|
if (_hostCacheEnabled == YES)
|
|
|
|
{
|
|
|
|
[_hostCache setObject:self forKey:name];
|
|
|
|
}
|
|
|
|
[_hostCacheLock unlock];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSHost
|
|
|
|
|
|
|
|
- init
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
+ (void)initialize
|
1997-03-03 19:40:50 +00:00
|
|
|
{
|
|
|
|
_hostCacheLock = [[NSConditionLock alloc] init];
|
1998-09-02 12:34:38 +00:00
|
|
|
_hostCache = [NSMutableDictionary new];
|
1997-03-03 19:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSHost *)currentHost
|
|
|
|
{
|
|
|
|
char name[MAXHOSTNAMELEN];
|
|
|
|
int res;
|
|
|
|
struct hostent *h;
|
|
|
|
|
|
|
|
res = gethostname(name, sizeof(name));
|
|
|
|
if (res < 0)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = gethostbyname(name);
|
|
|
|
|
|
|
|
return [self _hostWithHostEntry:h name:[NSString
|
|
|
|
stringWithCString:name]];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSHost *)hostWithName:(NSString *)name
|
|
|
|
{
|
|
|
|
struct hostent *h;
|
|
|
|
|
|
|
|
h = gethostbyname((char *)[name cString]);
|
|
|
|
|
|
|
|
return [self _hostWithHostEntry:h name:name];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSHost *)hostWithAddress:(NSString *)address
|
|
|
|
{
|
|
|
|
struct hostent *h;
|
|
|
|
struct in_addr hostaddr;
|
|
|
|
|
|
|
|
hostaddr.s_addr = inet_addr((char *)[address cString]);
|
|
|
|
if (hostaddr.s_addr == -1)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = gethostbyaddr((char *)&hostaddr, sizeof(hostaddr), AF_INET);
|
|
|
|
return [self _hostWithHostEntry:h];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)setHostCacheEnabled:(BOOL)flag
|
|
|
|
{
|
|
|
|
[_hostCacheLock lock];
|
|
|
|
_hostCacheEnabled = flag;
|
|
|
|
[_hostCacheLock unlock];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (BOOL)isHostCacheEnabled;
|
|
|
|
{
|
|
|
|
BOOL res;
|
|
|
|
|
|
|
|
[_hostCacheLock lock];
|
|
|
|
res = _hostCacheEnabled;
|
|
|
|
[_hostCacheLock unlock];
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)flushHostCache
|
|
|
|
{
|
|
|
|
[_hostCacheLock lock];
|
|
|
|
[_hostCache removeAllObjects];
|
|
|
|
[_hostCacheLock unlock];
|
|
|
|
}
|
|
|
|
|
1998-03-05 00:37:44 +00:00
|
|
|
/* Methods for encoding/decoding */
|
|
|
|
- (Class) classForPortCoder
|
|
|
|
{
|
|
|
|
return [self class];
|
|
|
|
}
|
|
|
|
- replacementObjectForPortCoder:(NSPortCoder*)aCoder
|
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
[super encodeWithCoder: aCoder];
|
|
|
|
[aCoder encodeObject: [self address]];
|
|
|
|
}
|
1998-03-12 14:21:20 +00:00
|
|
|
|
|
|
|
#if 1
|
1998-03-05 00:37:44 +00:00
|
|
|
/* GNUstep specific method for more efficient decoding. */
|
|
|
|
+ (id) newWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
NSString *address = [aCoder decodeObject];
|
|
|
|
return [NSHost hostWithAddress: address];
|
|
|
|
}
|
1998-03-12 14:21:20 +00:00
|
|
|
#else
|
|
|
|
/* OpenStep methods for decoding (not used) */
|
|
|
|
- (id) awakeAfterUsingCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
return [NSHost hostWithAddress: [addresses objectAtIndex: 0]];
|
|
|
|
}
|
|
|
|
|
1998-03-05 00:37:44 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
{
|
|
|
|
NSString *address;
|
|
|
|
|
|
|
|
[super initWithCoder: aCoder];
|
|
|
|
address = [aCoder decodeObject];
|
1998-03-12 14:21:20 +00:00
|
|
|
addresses = [NSArray arrayWithObject: address];
|
|
|
|
return self;
|
1998-03-05 00:37:44 +00:00
|
|
|
}
|
1998-03-12 14:21:20 +00:00
|
|
|
#endif
|
1998-03-05 00:37:44 +00:00
|
|
|
|
1998-11-11 06:09:36 +00:00
|
|
|
/*
|
|
|
|
* The OpenStep spec says that [-hash] must be the same for any two
|
|
|
|
* objects that [-isEqual:] returns YES for. We have a problem in
|
|
|
|
* that [-isEqualToHost:] is specified to return YES if any name or
|
|
|
|
* address part of two hosts is the same. That means we can't
|
|
|
|
* reasonably calculate a hash since two hosts with radically
|
|
|
|
* different ivar contents may be 'equal'. The best I can think of
|
|
|
|
* is for all hosts to hash to the same value - which makes it very
|
|
|
|
* inefficient to store them in a set, dictionary, map or hash table.
|
|
|
|
*/
|
|
|
|
- (unsigned) hash
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) isEqual: (id)other
|
|
|
|
{
|
|
|
|
if (other == self)
|
|
|
|
return YES;
|
|
|
|
if ([other isKindOfClass: [NSHost class]])
|
|
|
|
return [self isEqualToHost: (NSHost*)other];
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
1997-03-03 19:40:50 +00:00
|
|
|
- (BOOL)isEqualToHost:(NSHost *)aHost
|
|
|
|
{
|
1997-09-01 21:59:51 +00:00
|
|
|
NSArray* a;
|
|
|
|
int i;
|
|
|
|
|
1998-11-11 06:09:36 +00:00
|
|
|
if (aHost == self)
|
|
|
|
return YES;
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
a = [aHost addresses];
|
|
|
|
for (i = 0; i < [a count]; i++)
|
|
|
|
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;
|
1997-03-03 19:40:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)name
|
|
|
|
{
|
|
|
|
return [names objectAtIndex:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)names
|
|
|
|
{
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)address
|
|
|
|
{
|
|
|
|
return [addresses objectAtIndex:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)addresses
|
|
|
|
{
|
|
|
|
return addresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"Host %@ (%@ %@)", [self
|
|
|
|
name],
|
|
|
|
[[self names] description], [[self addresses]
|
|
|
|
description]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[names autorelease];
|
|
|
|
[addresses autorelease];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|