ftp.nice.ch/pub/next/developer/resources/classes/misckit/MiscKit.1.10.0.s.gnutar.gz#/MiscKit/Source/MiscKit/MiscINETAddress.m

This is MiscINETAddress.m in view mode; [Download] [Up]

/*  Class to act as a cover for Internet addresses.
 *
 *  Copyright (c) 1994, 1996 Christopher J. Kane.
 *
 *  This software is subject to the terms of the MiscKit license
 *  agreement. Refer to the license document included with the
 *  MiscKit distribution for these terms.
 *
 *  Version 1.2 (29 February 1996)
 */

#import <misckit/MiscINETAddress.h>
#import <libc.h>
#import <netdb.h>
#import <sys/errno.h>
extern char *NXCopyStringBuffer(const char *);
extern int errno;

MiscINETAddress *localAddress = nil;

@implementation MiscINETAddress

+ localAddress
{
	if (localAddress == nil) {
		char host[MAXHOSTNAMELEN+1];
		gethostname(host, MAXHOSTNAMELEN);
		localAddress = [[MiscINETAddress alloc] initFromName:host];
	}
	return localAddress;
}

- init
{
	if (hostname != NULL)
		free(hostname);
	address.s_addr = 0;
	hostname = NULL;
	return self;
}

- initFromName:(const char *)name
{
	int old_errno = errno;
	struct hostent *hent = gethostbyname((char *)name);
	[self init];
	if (hent == NULL) {
		unsigned long tmp = inet_addr((char *)name);
		if (tmp == -1) {
			errno = EINVAL;
			[super free];
			return nil;
		}
		address.s_addr = tmp;
	} else
		memcpy(&address, hent->h_addr, hent->h_length);
	hostname = NULL;
	errno = old_errno;
	return self;
}

- initTo:(struct in_addr)addr
{
	[self init];
	if (addr.s_addr == -1) {
		errno = EINVAL;
		[super free];
		return nil;
	}
	address = addr;
	return self;
}

- copyFromZone:(NXZone *)zone
{
	MiscINETAddress *copy = [super copyFromZone:zone];
	if (hostname != NULL)
		copy->hostname = NXCopyStringBuffer(hostname);
	return copy;
}

- free
{
	if (hostname != NULL)
		free(hostname);
	return [super free];
}

- read:(NXTypedStream *)stream;
{
	[super read:stream];
	NXReadTypes(stream, "L*", &(address.s_addr), &hostname);
	return self;
}

- write:(NXTypedStream *)stream
{
	[super write:stream];
	NXWriteTypes(stream, "L*", &(address.s_addr), &hostname);
	return self;
}

- (struct in_addr)address
{
	return address;
}

- (char *)hostname
{
	int old_errno = errno;
	if (hostname == NULL) {
		struct hostent *hent = gethostbyaddr((char *)&address, sizeof(address), AF_INET);
		if (hent == NULL || hent->h_name == NULL)
			hostname = NXCopyStringBuffer(inet_ntoa(address));
		else
			hostname = NXCopyStringBuffer(hent->h_name);
	}
	errno = old_errno;
	return hostname;
}

- (char *)stringAddress
{
	return inet_ntoa(address);
}

// added by Wolfgang_Baron@ixpoint.de

- initBroadcast
{
	static const char * multi_name = "255.255.255.255";
	[self init];
	address.s_addr = 0xffffffff;
	if (hostname) free( hostname );
	hostname = malloc( strlen(multi_name) + 1 );
	strcpy( hostname, multi_name );
	return self;
}

- increment
{
	address.s_addr++;
	return self;
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.