Made C99 project default.
5 // Created by Jens Alfke on 1/4/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import <Foundation/Foundation.h>
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>
18 UInt32 _ipv4; // In network byte order (big-endian), just like struct in_addr
19 UInt16 _port; // native byte order
22 /** Initializes an IPAddress from a host name (which may be a DNS name or dotted-quad numeric form)
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;
28 /** Creates an IPAddress from a host name (which may be a DNS name or dotted-quad numeric form)
30 If the hostname is not in dotted-quad form, an instance of the subclass HostAddress will
31 be returned instead. */
32 + (IPAddress*) addressWithHostname: (NSString*)hostname port: (UInt16)port;
34 /** Initializes an IPAddress from a raw IPv4 address (in network byte order, i.e. big-endian)
35 and port number (in native byte order) */
36 - (id) initWithIPv4: (UInt32)ipv4 port: (UInt16)port;
38 /** Initializes an IPAddress from a raw IPv4 address (in network byte order, i.e. big-endian).
39 The port number defaults to zero. */
40 - (id) initWithIPv4: (UInt32)ipv4;
42 /** Initializes an IPAddress from a BSD struct sockaddr. */
43 - (id) initWithSockAddr: (const struct sockaddr*)sockaddr;
45 /** Returns the IP address of this host (plus the specified port number).
46 If multiple network interfaces are active, the main one's address is returned. */
47 + (IPAddress*) localAddressWithPort: (UInt16)port;
49 /** Returns the IP address of this host (with a port number of zero).
50 If multiple network interfaces are active, the main one's address is returned. */
51 + (IPAddress*) localAddress;
53 /** Returns the address of the peer that an open socket is connected to.
54 (This calls getpeername.) */
55 + (IPAddress*) addressOfSocket: (CFSocketNativeHandle)socket;
57 /** Returns YES if the two objects have the same IP address, ignoring port numbers. */
58 - (BOOL) isSameHost: (IPAddress*)addr;
60 /** The raw IPv4 address, in network (big-endian) byte order. */
61 @property (readonly) UInt32 ipv4; // raw address in network byte order
63 /** The address as a dotted-quad string, e.g. @"10.0.1.1". */
64 @property (readonly) NSString* ipv4name;
66 /** The address as a DNS hostname or else a dotted-quad string.
67 (IPAddress itself always returns dotted-quad; HostAddress returns the hostname it was
69 @property (readonly) NSString* hostname; // dotted-quad string, or DNS name if I am a HostAddress
71 /** The port number, or zero if none was specified, in native byte order. */
72 @property (readonly) UInt16 port;
74 /** Is this IP address in a designated private/local address range, such as 10.0.1.X?
75 If so, the address is not globally meaningful outside of the local subnet. */
76 @property (readonly) BOOL isPrivate; // In a private/local addr range like 10.0.1.X?
81 /** A subclass of IPAddress that remembers the DNS hostname instead of a raw address.
82 An instance of HostAddress looks up its ipv4 address on the fly by calling gethostbyname. */
83 @interface HostAddress : IPAddress
89 - (id) initWithHostname: (NSString*)hostname port: (UInt16)port;
91 /** Initializes a HostAddress from a host name, plus a sockaddr struct and a port number.
92 (The port number overrides any port specified in the sockaddr struct.) */
93 - (id) initWithHostname: (NSString*)hostname
94 sockaddr: (const struct sockaddr*)sockaddr
101 /** An IPAddress that can keep track of statistics on when it was last sucessfully used
102 and the number of successful attempts. This is useful when keeping a cache of recent
103 addresses for a peer that doesn't have a stable address. */
104 @interface RecentAddress : IPAddress
107 CFAbsoluteTime _lastSuccess;
111 /** Initializes a RecentAddress from an IPAddress. (You can also initialize RecentAddress using
112 any inherited initializer method.) */
113 - (id) initWithIPAddress: (IPAddress*)addr;
115 /** The absolute time that -noteSuccess or -noteSeen was last called. */
116 @property (readonly) CFAbsoluteTime lastSuccess;
118 /** The number of times that -noteSuccess has been called. */
119 @property (readonly) UInt32 successes;
121 /** Call this to indicate that the address was successfully used to connect to the desired peer.
122 Returns YES if the state of the object has changed and it should be re-archived. */
123 - (BOOL) noteSuccess;
125 /** Call this to indicate that you have received evidence that this address is currently being
126 used by this peer. Unlike -noteSuccess it doesn't increment -successes, and only returns
127 YES (to indicate a persistent change) once every 18 hours (to avoid making the client
128 save its cache too often.) */