forked from jarnoh/cordova-dnssd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnssd.m
187 lines (149 loc) · 6.92 KB
/
dnssd.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
/*
* Bonjour DNS-SD plugin for Cordova.
* Allows browsing and resolving of local ad-hoc services
*
* jarnoh@komplex.org - March 2012
*
*/
#import "dnssd.h"
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
@implementation CDVBonjour
@synthesize browseCallback;
@synthesize resolveCallback;
@synthesize netServiceBrowser;
@synthesize currentResolve;
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error This plugin requires ARC
#endif
- (void)stopBrowsing:(CDVInvokedUrlCommand*)command
{
[self.netServiceBrowser stop];
self.netServiceBrowser = nil;
[self.currentResolve stop];
self.currentResolve = nil;
CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
//- (void)browse:(NSArray*)arguments withDict:(NSDictionary*)options
- (void)browse:(CDVInvokedUrlCommand*)command
{
[self.netServiceBrowser stop];
// [self.netServiceBrowser release];
self.netServiceBrowser=nil;
NSUInteger argc = [command.arguments count];
NSLog(@"browse argc %d", (int)argc);
if (argc < 2) {
CDVPluginResult* result = [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
];
[result setKeepCallbackAsBool:NO];
[self.commandDelegate sendPluginResult:result callbackId:self.browseCallback];
self.browseCallback=nil;
return;
}
self.browseCallback = command.callbackId;
NSString *regType = [command.arguments objectAtIndex:0];
NSString *domain = [command.arguments objectAtIndex:1];
self.netServiceBrowser = [[NSNetServiceBrowser alloc] init];
self.netServiceBrowser.delegate = self;
[self.netServiceBrowser searchForServicesOfType:regType inDomain:domain];
}
- (void)resolve:(CDVInvokedUrlCommand*)command
{
[self.currentResolve stop];
// [self.currentResolve release];
self.currentResolve = nil;
NSUInteger argc = [command.arguments count];
NSLog(@"resolve argc %d", (int)argc);
if (argc < 3) {
CDVPluginResult* result = [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
];
[result setKeepCallbackAsBool:NO];
[self.commandDelegate sendPluginResult:result callbackId:self.resolveCallback];
self.resolveCallback=nil;
return;
}
self.resolveCallback = command.callbackId;
NSString *serviceName = [command.arguments objectAtIndex:0];
NSString *regType = [command.arguments objectAtIndex:1];
NSString *domain = [command.arguments objectAtIndex:2];
self.currentResolve = [[NSNetService alloc] initWithDomain:domain type:regType name:serviceName];
NSLog(@"currentResolve %@", self.currentResolve);
[self.currentResolve setDelegate:self];
[self.currentResolve resolveWithTimeout:0.0];
}
//
- (void)netServiceBrowser:(NSNetServiceBrowser*)netServiceBrowser didRemoveService:(NSNetService*)service moreComing:(BOOL)moreComing {
NSLog(@"didRemoveService");
NSMutableDictionary* resultDict = [[NSMutableDictionary alloc] init];
[resultDict setObject:[NSNumber numberWithBool:TRUE] forKey:@"serviceLost"];
[resultDict setObject:service.name forKey:@"serviceName"];
[resultDict setObject:service.type forKey:@"regType"];
[resultDict setObject:service.domain forKey:@"domain"];
[resultDict setObject:[NSNumber numberWithBool:moreComing] forKey:@"moreComing"];
CDVPluginResult* result = [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
messageAsDictionary: resultDict
];
[result setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:result callbackId:self.browseCallback];
}
- (void)netServiceBrowser:(NSNetServiceBrowser*)netServiceBrowser didFindService:(NSNetService*)service moreComing:(BOOL)moreComing {
NSLog(@"didFindService name %@", service.name);
NSMutableDictionary* resultDict = [[NSMutableDictionary alloc] init];
[resultDict setObject:[NSNumber numberWithBool:TRUE] forKey:@"serviceFound"];
[resultDict setObject:service.name forKey:@"serviceName"];
[resultDict setObject:service.type forKey:@"regType"];
[resultDict setObject:service.domain forKey:@"domain"];
[resultDict setObject:[NSNumber numberWithBool:moreComing] forKey:@"moreComing"];
CDVPluginResult* result = [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
messageAsDictionary: resultDict
];
[result setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:result callbackId:self.browseCallback];
}
- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict {
NSLog(@"didNotResolve");
// TODO not used (timeout 0)
}
- (void)netServiceDidResolveAddress:(NSNetService *)service {
NSLog(@"netServiceDidResolveAddress");
NSMutableDictionary* resultDict = [[NSMutableDictionary alloc] init];
// FIXME ugly hack to pass IP addresses to javascript, the application server does not support ipv6,
// and my server is resolving names as ipv6 addresses.
for (NSData* data in [service addresses]) {
struct sockaddr_in* socketAddress = (struct sockaddr_in*) [data bytes];
int sockFamily = socketAddress->sin_family;
if (sockFamily == AF_INET) {
char buf[100];
const char* addressStr = inet_ntop(sockFamily, &(socketAddress->sin_addr), buf, sizeof(buf));
NSString *address = [NSString stringWithUTF8String:addressStr];
[resultDict setObject:address forKey:@"address"];
}
if (sockFamily == AF_INET6) {
char buf[100];
const char* addressStr = inet_ntop(sockFamily, &(socketAddress->sin_addr), buf, sizeof(buf));
NSString *address = [NSString stringWithUTF8String:addressStr];
[resultDict setObject:address forKey:@"address6"];
}
}
[resultDict setObject:[NSNumber numberWithBool:TRUE] forKey:@"serviceResolved"];
[resultDict setObject:service.hostName forKey:@"hostName"];
[resultDict setObject:[NSNumber numberWithInteger:service.port] forKey:@"port"];
[resultDict setObject:service.name forKey:@"serviceName"];
[resultDict setObject:service.type forKey:@"regType"];
[resultDict setObject:service.domain forKey:@"domain"];
CDVPluginResult* result = [CDVPluginResult
resultWithStatus: CDVCommandStatus_OK
messageAsDictionary: resultDict];
[result setKeepCallbackAsBool:NO];
[self.commandDelegate sendPluginResult:result callbackId:self.resolveCallback];
[self.currentResolve stop];
// [self.currentResolve release];
self.currentResolve = nil;
}
@end