home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.bin / SourceCode / MiscKit1.2.6 / Source / MiscINETAddress.m < prev    next >
Encoding:
Text File  |  1994-05-15  |  2.7 KB  |  136 lines

  1. /*  Class to act as a cover for Internet addresses.
  2.  *
  3.  *  Copyright (c) 1994 Christopher J. Kane.
  4.  *
  5.  *  This software is subject to the terms of the MiscKit license
  6.  *  agreement. Refer to the license document included with the
  7.  *  MiscKit distribution for these terms.
  8.  *
  9.  *  Version 1.0 BETA (20 April 1994)
  10.  */
  11.  
  12. #import <misckit/MiscINETAddress.h>
  13. #import <libc.h>
  14. #import <netdb.h>
  15. #import <sys/errno.h>
  16. extern char *NXCopyStringBuffer(const char *);
  17. extern int errno;
  18.  
  19. MiscINETAddress *localAddress = nil;
  20.  
  21. @implementation MiscINETAddress
  22.  
  23. + localAddress
  24. {
  25.     if (localAddress == nil) {
  26.         struct sockaddr_in sockaddr;
  27.         int len = sizeof(sockaddr), old_errno = errno;
  28.         int sock = socket(PF_INET, SOCK_DGRAM, 0);
  29.         if (0 <= sock && 0 <= getsockname(sock, (struct sockaddr *)&sockaddr, &len)) {
  30.             close(sock);
  31.             errno = old_errno;
  32.             localAddress = [[MiscINETAddress alloc] initTo:sockaddr.sin_addr];
  33.         } else {
  34.             char host[MAXHOSTNAMELEN+1];
  35.             gethostname(host, MAXHOSTNAMELEN);
  36.             errno = old_errno;
  37.             localAddress = [[MiscINETAddress alloc] initFromName:host];
  38.         }
  39.     }
  40.     return localAddress;
  41. }
  42.  
  43. - init
  44. {
  45.     if (hostname != NULL)
  46.         free(hostname);
  47.     address.s_addr = 0;
  48.     hostname = NULL;
  49.     return self;
  50. }
  51.  
  52. - initFromName:(const char *)name
  53. {
  54.     int old_errno = errno;
  55.     struct hostent *hent = gethostbyname((char *)name);
  56.     [self init];
  57.     if (hent == NULL) {
  58.         unsigned long tmp = inet_addr((char *)name);
  59.         if (tmp == -1) {
  60.             errno = EINVAL;
  61.             return [super free];
  62.         }
  63.         address.s_addr = tmp;
  64.     } else
  65.         memcpy(&address, hent->h_addr, hent->h_length);
  66.     hostname = NULL;
  67.     errno = old_errno;
  68.     return self;
  69. }
  70.  
  71. - initTo:(struct in_addr)addr
  72. {
  73.     [self init];
  74.     if (addr.s_addr == -1) {
  75.         errno = EINVAL;
  76.         return [super free];
  77.     }
  78.     address = addr;
  79.     return self;
  80. }
  81.  
  82. - copyFromZone:(NXZone *)zone
  83. {
  84.     MiscINETAddress *copy = [super copyFromZone:zone];
  85.     if (hostname != NULL)
  86.         copy->hostname = NXCopyStringBuffer(hostname);
  87.     return copy;
  88. }
  89.  
  90. - free
  91. {
  92.     if (hostname != NULL)
  93.         free(hostname);
  94.     return [super free];
  95. }
  96.  
  97. - read:(NXTypedStream *)stream;
  98. {
  99.     [super read:stream];
  100.     NXReadTypes(stream, "L*", &(address.s_addr), &hostname);
  101.     return self;
  102. }
  103.  
  104. - write:(NXTypedStream *)stream
  105. {
  106.     [super write:stream];
  107.     NXWriteTypes(stream, "L*", &(address.s_addr), &hostname);
  108.     return self;
  109. }
  110.  
  111. - (struct in_addr)address
  112. {
  113.     return address;
  114. }
  115.  
  116. - (char *)hostname
  117. {
  118.     int old_errno = errno;
  119.     if (hostname == NULL) {
  120.         struct hostent *hent = gethostbyaddr((char *)&address, sizeof(address), AF_INET);
  121.         if (hent == NULL || hent->h_name == NULL)
  122.             hostname = NXCopyStringBuffer(inet_ntoa(address));
  123.         else
  124.             hostname = NXCopyStringBuffer(hent->h_name);
  125.     }
  126.     errno = old_errno;
  127.     return hostname;
  128. }
  129.  
  130. - (char *)stringAddress
  131. {
  132.     return inet_ntoa(address);
  133. }
  134.  
  135. @end
  136.