Improve thread ID and name code to match OSX. Add some initial getaddrinfo code

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38442 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2015-03-31 09:14:01 +00:00
parent ef42c2f44c
commit 33688ccb99
8 changed files with 419 additions and 41 deletions

View file

@ -63,7 +63,10 @@ static id null = nil;
@interface NSHost (Private)
- (void) _addName: (NSString*)name;
- (id) _initWithHostEntry: (struct hostent*)entry key: (NSString*)key;
#if defined(HAVE_GETADDRINFO)
- (id) _initWithAddrinfo: (struct addrinfo*)entry key: (NSString*)name;
#endif
- (id) _initWithHostEntry: (struct hostent*)entry key: (NSString*)name;
+ (NSMutableSet*) _localAddresses;
@end
@ -101,6 +104,125 @@ static id null = nil;
return self;
}
#if defined(HAVE_GETADDRINFO)
- (id) _initWithAddrinfo: (struct addrinfo*)entry key: (NSString*)name
{
NSMutableSet *names;
NSMutableSet *addresses;
NSMutableSet *extra;
if ((self = [super init]) == nil)
{
return nil;
}
if ([name isEqualToString: localHostName] == NO
&& entry == (struct addrinfo*)NULL)
{
NSLog(@"Host '%@' init failed - perhaps the name/address is wrong or "
@"networking is not set up on your machine", name);
DESTROY(self);
return nil;
}
else if (name == nil && entry != (struct addrinfo*)NULL)
{
NSLog(@"Nil hostname supplied but network database entry is not empty");
DESTROY(self);
return nil;
}
names = [NSMutableSet new];
addresses = [NSMutableSet new];
if ([name isEqualToString: localHostName] == YES)
{
extra = [hostClass _localAddresses];
}
else
{
extra = nil;
}
for (;;)
{
struct addrinfo *tmp;
/*
* We remove all the IP addresses that we have added to the host so
* far from the set of extra addresses available on the current host.
* Then we try to find a new network database entry for one of the
* remaining extra addresses, and loop round to add all the names
* and addresses for that entry.
*/
[extra minusSet: addresses];
while (entry == 0 && [extra count] > 0)
{
NSString *a = [extra anyObject];
struct addrinfo hints;
memset(&hints, '\0', sizeof(hints));
hints.ai_flags = AI_CANONNAME;
getaddrinfo([a UTF8String], 0, &hints, &entry);
if (0 == entry)
{
/*
* Can't find a database entry for this IP address, but since
* we know the address is valid, we add it to the list of
* addresses for this host anyway.
*/
[addresses addObject: a];
[extra removeObject: a];
}
}
if (0 == entry)
{
break;
}
for (tmp = entry; tmp != 0; tmp = tmp->ai_next)
{
if (tmp->ai_canonname && *tmp->ai_canonname)
{
NSString *n;
n = [[NSString alloc] initWithUTF8String: tmp->ai_canonname];
[names addObject: n];
[n release];
}
if (tmp->ai_addrlen > 0)
{
char host[NI_MAXHOST];
char port[NI_MAXSERV];
if (0 == getnameinfo(tmp->ai_addr, tmp->ai_addrlen,
host, NI_MAXHOST, port, NI_MAXSERV, NI_NUMERICSERV))
{
NSString *a;
a = [[NSString alloc] initWithUTF8String: host];
[addresses addObject: a];
[a release];
}
}
}
freeaddrinfo(entry);
entry = 0;
}
_names = [names copy];
RELEASE(names);
_addresses = [addresses copy];
RELEASE(addresses);
if (YES == _hostCacheEnabled)
{
[_hostCache setObject: self forKey: name];
}
return self;
}
#endif
- (id) _initWithHostEntry: (struct hostent*)entry key: (NSString*)name
{
int i;