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