IPAddress.h
author jim@Baldrick.local
Tue May 05 23:24:50 2009 -0700 (2009-05-05)
changeset 43 aab592ac36fc
parent 28 732576fa8a0d
child 49 20cccc7c26ee
permissions -rw-r--r--
bug fixes and improvements to new bonjour classes and tcplistener, also added a static library target
     1 //
     2 //  IPAddress.h
     3 //  MYNetwork
     4 //
     5 //  Created by Jens Alfke on 1/4/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 
    12 /** Represents an Internet Protocol address and port number (similar to a sockaddr_in).
    13     IPAddress itself only remembers the raw 32-bit IPv4 address; the subclass HostAddress
    14     also remembers the DNS host-name. */
    15 @interface IPAddress : NSObject <NSCoding, NSCopying>
    16 {
    17     @private
    18     UInt32 _ipv4;       // In network byte order (big-endian), just like struct in_addr
    19     UInt16 _port;       // native byte order
    20 }
    21 
    22 /** Initializes an IPAddress from a host name (which may be a DNS name or dotted-quad numeric form)
    23     and port number.
    24     If the hostname is not in dotted-quad form, an instance of the subclass HostAddress will
    25     be returned instead. */
    26 - (id) initWithHostname: (NSString*)hostname port: (UInt16)port;
    27 
    28 /** Initializes an IPAddress from a raw IPv4 address (in network byte order, i.e. big-endian)
    29     and port number (in native byte order) */
    30 - (id) initWithIPv4: (UInt32)ipv4 port: (UInt16)port;
    31 
    32 /** Initializes an IPAddress from a raw IPv4 address (in network byte order, i.e. big-endian).
    33     The port number defaults to zero. */
    34 - (id) initWithIPv4: (UInt32)ipv4;
    35 
    36 /** Initializes an IPAddress from a BSD struct sockaddr. */
    37 - (id) initWithSockAddr: (const struct sockaddr*)sockaddr;
    38 
    39 /** Returns the IP address of this host (plus the specified port number).
    40     If multiple network interfaces are active, the main one's address is returned. */
    41 + (IPAddress*) localAddressWithPort: (UInt16)port;
    42 
    43 /** Returns the IP address of this host (with a port number of zero).
    44     If multiple network interfaces are active, the main one's address is returned. */
    45 + (IPAddress*) localAddress;
    46 
    47 /** Returns the address of the peer that an open socket is connected to.
    48     (This calls getpeername.) */
    49 + (IPAddress*) addressOfSocket: (CFSocketNativeHandle)socket;
    50 
    51 /** Returns YES if the two objects have the same IP address, ignoring port numbers. */
    52 - (BOOL) isSameHost: (IPAddress*)addr;
    53 
    54 /** The raw IPv4 address, in network (big-endian) byte order. */
    55 @property (readonly) UInt32 ipv4;               // raw address in network byte order
    56 
    57 /** The address as a dotted-quad string, e.g. @"10.0.1.1". */
    58 @property (readonly) NSString* ipv4name;
    59 
    60 /** The address as a DNS hostname or else a dotted-quad string.
    61     (IPAddress itself always returns dotted-quad; HostAddress returns the hostname it was
    62     initialized with.) */
    63 @property (readonly) NSString* hostname;        // dotted-quad string, or DNS name if I am a HostAddress
    64 
    65 /** The port number, or zero if none was specified, in native byte order. */
    66 @property (readonly) UInt16 port;
    67 
    68 /** Is this IP address in a designated private/local address range, such as 10.0.1.X?
    69     If so, the address is not globally meaningful outside of the local subnet. */
    70 @property (readonly) BOOL isPrivate;            // In a private/local addr range like 10.0.1.X?
    71 @end
    72 
    73 
    74 
    75 /** A subclass of IPAddress that remembers the DNS hostname instead of a raw address.
    76     An instance of HostAddress looks up its ipv4 address on the fly by calling gethostbyname. */
    77 @interface HostAddress : IPAddress
    78 {
    79     @private
    80     NSString *_hostname;
    81 }
    82 
    83 - (id) initWithHostname: (NSString*)hostname port: (UInt16)port;
    84 
    85 /** Initializes a HostAddress from a host name, plus a sockaddr struct and a port number.
    86     (The port number overrides any port specified in the sockaddr struct.) */
    87 - (id) initWithHostname: (NSString*)hostname
    88                sockaddr: (const struct sockaddr*)sockaddr
    89                    port: (UInt16)port;
    90 
    91 @end
    92 
    93 
    94 
    95 /** An IPAddress that can keep track of statistics on when it was last sucessfully used
    96     and the number of successful attempts. This is useful when keeping a cache of recent
    97     addresses for a peer that doesn't have a stable address. */
    98 @interface RecentAddress : IPAddress
    99 {
   100     @private
   101     CFAbsoluteTime _lastSuccess;
   102     UInt32 _successes;
   103 }
   104 
   105 /** Initializes a RecentAddress from an IPAddress. (You can also initialize RecentAddress using
   106     any inherited initializer method.) */
   107 - (id) initWithIPAddress: (IPAddress*)addr;
   108 
   109 /** The absolute time that -noteSuccess or -noteSeen was last called. */
   110 @property (readonly) CFAbsoluteTime lastSuccess;
   111 
   112 /** The number of times that -noteSuccess has been called. */
   113 @property (readonly) UInt32 successes;
   114 
   115 /** Call this to indicate that the address was successfully used to connect to the desired peer.
   116     Returns YES if the state of the object has changed and it should be re-archived. */
   117 - (BOOL) noteSuccess;
   118 
   119 /** Call this to indicate that you have received evidence that this address is currently being
   120     used by this peer. Unlike -noteSuccess it doesn't increment -successes, and only returns
   121     YES (to indicate a persistent change) once every 18 hours (to avoid making the client
   122     save its cache too often.) */
   123 - (BOOL) noteSeen;
   124 
   125 @end