New file.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@2200 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Andrew McCallum 1997-03-03 19:40:50 +00:00
parent b6759e584d
commit c03261dd56
2 changed files with 324 additions and 0 deletions

View file

@ -0,0 +1,78 @@
/* Interface for 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.
*/
#include <gnustep/base/preface.h>
@class NSString, NSArray, NSMutableArray;
@interface NSHost : NSObject
{
@private
NSMutableArray *names;
NSMutableArray *addresses;
void *reserved;
}
/*
* Get a host object. Hosts are cached for efficiency. Only one
* shared instance of a host will exist.
* Addresses must be "Dotted Decimal" strings, e.g.
*
* NSHost aHost = [NSHost hostWithAddress:@"192.42.172.1"];
*
*/
+ (NSHost*) currentHost;
+ (NSHost*) hostWithName: (NSString*)name;
+ (NSHost*) hostWithAddress: (NSString*)address;
/*
* Host cache management
* If enabled, only one object representing each host will be created, and
* a shared instance will be returned by all methods that return a host.
*/
+ (void) setHostCacheEnabled: (BOOL)flag;
+ (BOOL) isHostCacheEnabled;
+ (void) flushHostCache;
/*
* Compare hosts
* Hosts are equal if they share at least one address
*/
- (BOOL) isEqualToHost: (NSHost*) aHost;
/*
* Host names.
* "name" will return one name (arbitrarily chosen) if a host has several.
*/
- (NSString*) name;
- (NSArray*) names;
/*
* Host addresses.
* Addresses are represented as "Dotted Decimal" strings, e.g. @"192.42.172.1"
* "address" will return one address (arbitrarily chosen) if there are several.
*/
- (NSString*) address;
- (NSArray*) addresses;
@end

246
Source/NSHost.m Normal file
View file

@ -0,0 +1,246 @@
/* 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.
*/
#include <gnustep/base/preface.h>
#include <Foundation/NSLock.h>
#include <Foundation/NSHost.h>
#include <netdb.h>
#include <libc.h>
#ifdef WIN32
#include <Windows32/Sockets.h>
#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])
{
memmove((void *)&in.s_addr, (const void *)ptr,
entry->h_length);
[addresses addObject:[NSString
stringWithCString:inet_ntoa(in)]];
}
[_hostCacheLock lock];
if (_hostCacheEnabled == YES)
{
[_hostCache setObject:self forKey:name];
}
[_hostCacheLock unlock];
return self;
}
@end
@implementation NSHost
- init
{
return nil;
}
+ initialize
{
_hostCacheLock = [[NSConditionLock alloc] init];
_hostCache = [[NSMutableDictionary dictionary] retain];
return self;
}
+ (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];
}
- (BOOL)isEqualToHost:(NSHost *)aHost
{
// how should we check for equality?
return ([[aHost addresses] isEqual:addresses]) && ([[aHost
names] isEqual:names]);
}
- (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