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