-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHHService.m
executable file
·325 lines (241 loc) · 10.5 KB
/
HHService.m
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//
// HHService.m
// Part of Hejsan-Hoppsan-Services : http://www.github.com/tolo/HHServices
//
// Created by Tobias on 2011-11-02.
// Copyright (c) 2011 Leafnode AB. All rights reserved.
//
#import "HHService.h"
#import "HHServiceSupport+Private.h"
#import <netinet/in.h>
#import <arpa/inet.h>
#import <net/if.h>
@interface ResolveResult : NSObject
@property (nonatomic, assign) HHService* serviceResolver;
@property (nonatomic, retain) NSString* hostName;
@property (nonatomic) uint16_t port; // Network byte order
@property (nonatomic) uint32_t interfaceIndex;
@end
@implementation ResolveResult
@synthesize hostName, port, interfaceIndex;
- (void)dealloc {
self.hostName = nil;
[super dealloc];
}
@end
/** "Private" interface */
@interface HHService ()
// Redefinition of read only properties to readwrite
@property (nonatomic, retain, readwrite) NSString* name;
@property (nonatomic, retain, readwrite) NSString* type;
@property (nonatomic, retain, readwrite) NSString* domain;
@property (nonatomic, readwrite) BOOL resolved;
@property (nonatomic, retain, readwrite) NSString* resolvedHostName;
@property (nonatomic, retain, readwrite) NSArray* resolvedAddresses;
@property (nonatomic, retain, readwrite) NSData* txtData;
@property (nonatomic, retain) NSMutableArray* resolveResults;
@property (assign) uint16_t lastResolvedPort;
- (void) didResolveService:(ResolveResult*)resolveResult txtData:(NSData*)svcTxtData moreComing:(BOOL)moreComing error:(DNSServiceErrorType)error;
- (void) didResolveServiceAddress:(NSData*)addressData error:(DNSServiceErrorType)error;
@end
#pragma mark -
#pragma mark Callbacks
static void getAddrInfoCallback(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
const char* hostName, const struct sockaddr* address, uint32_t ttl, void* context ) {
ContextWrapper* contextWrapper = (ContextWrapper*)context;
HHService* serviceResolver = contextWrapper.contextRetained;
if( serviceResolver ) {
NSData* addressData = nil;
if ( errorCode == kDNSServiceErr_NoError ) {
// Set port if not set
struct sockaddr_in* sin = (struct sockaddr_in*)address;
if( sin->sin_port == 0 ) sin->sin_port = serviceResolver.lastResolvedPort;
addressData = [[NSData alloc] initWithBytes:address length:sizeof(struct sockaddr)];
}
dispatch_async(serviceResolver.mainDispatchQueue, ^{
[serviceResolver didResolveServiceAddress:addressData error:errorCode];
[addressData release];
});
}
[serviceResolver release];
}
static void resolveCallback(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
const char* fullname, const char* hosttarget, uint16_t port, uint16_t txtLen,
const unsigned char* txtRecord, void* context) {
ContextWrapper* contextWrapper = (ContextWrapper*)context;
HHService* serviceResolver = contextWrapper.contextRetained;
if( serviceResolver ) {
BOOL moreComing = flags & kDNSServiceFlagsMoreComing;
ResolveResult* resolveResult = nil;
NSString* newName = [[NSString alloc] initWithCString:hosttarget encoding:NSUTF8StringEncoding];
if (errorCode == kDNSServiceErr_NoError) {
char interfaceName[IFNAMSIZ];
if( if_indextoname(interfaceIndex, interfaceName) != NULL ) {
[serviceResolver HHLogDebug:@"Resolved host %@, port %d, interface index %d ('%s') - getting address info", newName, port, interfaceIndex, interfaceName];
resolveResult = [[ResolveResult alloc] init];
resolveResult.hostName = newName;
resolveResult.port = port; // Keep network byte ordering since we will use this in struct sockaddr_in
resolveResult.interfaceIndex = interfaceIndex;
} else {
[serviceResolver HHLogDebug:@"Resolve returned invalid interface index (%d)", interfaceIndex];
}
}
NSData* txtData = [[NSData alloc] initWithBytes:txtRecord length:txtLen];
dispatch_async(serviceResolver.mainDispatchQueue, ^{
[serviceResolver didResolveService:resolveResult txtData:txtData moreComing:moreComing error:errorCode];
[resolveResult release];
[newName release];
[txtData release];
});
}
[serviceResolver release];
}
#pragma mark -
#pragma mark HHService
@implementation HHService
@synthesize delegate,
name, type, domain, includeP2P,
resolved, resolvedHostName, resolvedAddresses, txtData;
#pragma mark -
#pragma mark Internal methods
- (void) didResolveService:(ResolveResult*)resolveResult txtData:(NSData*)svcTxtData moreComing:(BOOL)moreComing error:(DNSServiceErrorType)error {
self.lastError = error;
if ( error == kDNSServiceErr_NoError ) {
resolveResult.serviceResolver = self;
[self.resolveResults addObject:resolveResult];
if( !moreComing ) {
[self HHLogDebug:@"Getting address info for resolved addresses"];
self.resolvedHostName = resolveResult.hostName;
self.txtData = svcTxtData;
[self getNextAddressInfo];
}
} else {
[self HHLogDebug:@"Error resolving service: %d", error];
self.resolved = NO;
[self.delegate serviceDidNotResolve:self];
}
}
- (void) didResolveServiceAddress:(NSData*)addressData error:(DNSServiceErrorType)error {
self.lastError = error;
if ( error == kDNSServiceErr_NoError ) {
// Add address if not already added
for(NSData* existingAddrData in self.resolvedAddresses) {
if ( [existingAddrData isEqualToData:addressData] ) {
addressData = nil; // Adress already added
break;
}
}
if( addressData ) [(NSMutableArray*)self.resolvedAddresses addObject:addressData];
[self.resolveResults removeLastObject];
if( self.resolveResults.count ) {
[self HHLogDebug:@"Getting address info for next address"];
[self getNextAddressInfo];
} else {
[self HHLogDebug:@"Resolved last address"];
self.resolved = YES;
[self.delegate serviceDidResolve:self];
}
} else {
[self HHLogDebug:@"Error getting address info: %d", error];
self.resolved = NO;
[self.delegate serviceDidNotResolve:self];
}
}
- (void) dnsServiceError:(DNSServiceErrorType)error {
[super dnsServiceError:error];
self.resolved = NO;
[self.delegate serviceDidNotResolve:self];
}
#pragma mark -
#pragma mark Creation and destruction
- (id) initWithName:(NSString*)svcName type:(NSString*)svcType domain:(NSString*)svcDomain {
return [self initWithName:svcName type:svcType domain:svcDomain includeP2P:NO];
}
- (id) initWithName:(NSString*)svcName type:(NSString*)svcType domain:(NSString*)svcDomain includeP2P:(BOOL)shouldIncludeP2P {
if( (self = [super init]) ) {
includeP2P = shouldIncludeP2P;
self.name = svcName;
self.type = svcType;
self.domain = svcDomain;
}
return self;
}
- (void) dealloc {
self.name = nil;
self.type = nil;
self.domain = nil;
self.resolvedHostName = nil;
self.resolvedAddresses = nil;
self.txtData = nil;
self.resolveResults = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Get address info methods (resolve step2)
- (void) getNextAddressInfo {
ResolveResult* result = [self.resolveResults lastObject];
if( result ) {
DNSServiceRef getAddressInfoRef = NULL;
self.lastResolvedPort = result.port;
const char* hosttarget = [result.hostName cStringUsingEncoding:NSUTF8StringEncoding];
DNSServiceErrorType err = DNSServiceGetAddrInfo(&getAddressInfoRef, 0, result.interfaceIndex, kDNSServiceProtocol_IPv4,
hosttarget, getAddrInfoCallback, [self setCurrentCallbackContextWithSelf]);
if( err == kDNSServiceErr_NoError ) {
[self HHLogDebug:@"Beginning address lookup"];
[super setServiceRef:getAddressInfoRef];
} else {
[self HHLogDebug:@"Error doing address lookup"];
[self dnsServiceError:self.lastError];
}
}
}
#pragma mark -
#pragma mark HHService public methods
- (BOOL) beginResolve {
//[super resetServiceRef];
self.resolved = NO;
self.resolvedAddresses = [NSMutableArray array];
self.resolveResults = [NSMutableArray array];
const char* _name = [self.name cStringUsingEncoding:NSUTF8StringEncoding];
const char* _type = [self.type cStringUsingEncoding:NSUTF8StringEncoding];
const char* _domain = [self.domain cStringUsingEncoding:NSUTF8StringEncoding];
DNSServiceFlags flags = 0;
#if TARGET_OS_IPHONE == 1
flags |= (uint32_t)(includeP2P ? kDNSServiceFlagsIncludeP2P : 0);
#endif
DNSServiceRef resolveRef = nil;
DNSServiceErrorType err = DNSServiceResolve(&resolveRef, flags, kDNSServiceInterfaceIndexAny,
_name, _type, _domain, resolveCallback, [self setCurrentCallbackContextWithSelf]);
if( err == kDNSServiceErr_NoError ) {
[self HHLogDebug:@"Beginning resolve"];
return [super setServiceRef:resolveRef];
} else {
[self HHLogDebug:@"Error starting resolve"];
[self dnsServiceError:err];
return NO;
}
}
- (void) endResolve {
[super resetServiceRef];
}
#pragma mark -
#pragma mark Equals & hashcode etc
- (NSString*) identityString {
return [NSString stringWithFormat:@"%@|%@|%@", self.name, self.type, self.domain];
}
- (NSUInteger) hash {
return [self.identityString hash];
}
- (BOOL) isEqual:(id)anObject {
return [anObject isKindOfClass:[self class]] && [self.identityString isEqual:((HHService*)anObject).identityString];
}
- (NSString*) description {
NSMutableArray* addressesAsStrings = [NSMutableArray array];
for (int i=0; i<self.resolvedAddresses.count; i++) {
const struct sockaddr_in* sin = (struct sockaddr_in*)[self.resolvedAddresses[i] bytes];
[addressesAsStrings addObject:[NSString stringWithFormat:@"%@:%d", @(inet_ntoa(sin->sin_addr)), ntohs(sin->sin_port)]];
}
return [NSString stringWithFormat:@"HHService[0x%08X, %@, %@, %@, %@, %@, %d]", (unsigned int)self,
self.name, self.type, self.domain, self.resolvedHostName, addressesAsStrings, txtData.length];
}
@end