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