]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/DNSServiceBrowser.m
mDNSResponder-170.tar.gz
[apple/mdnsresponder.git] / Clients / DNSServiceBrowser.m
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16
17 Change History (most recent first):
18
19 $Log: DNSServiceBrowser.m,v $
20 Revision 1.35 2006/11/27 08:27:49 mkrochma
21 Fix a crashing bug
22
23 Revision 1.34 2006/11/24 05:41:07 mkrochma
24 More cleanup and more service types
25
26 Revision 1.33 2006/11/24 01:34:24 mkrochma
27 Display interface index and query for IPv6 addresses even when there's no IPv4
28
29 Revision 1.32 2006/11/24 00:25:31 mkrochma
30 <rdar://problem/4084652> Tools: DNS Service Browser contains some bugs
31
32 Revision 1.31 2006/08/14 23:23:55 cheshire
33 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
34
35 Revision 1.30 2005/01/27 17:46:16 cheshire
36 Added comment
37
38 Revision 1.29 2004/06/04 20:58:36 cheshire
39 Move DNSServiceBrowser from mDNSMacOSX directory to Clients directory
40
41 Revision 1.28 2004/05/18 23:51:26 cheshire
42 Tidy up all checkin comments to use consistent "<rdar://problem/xxxxxxx>" format for bug numbers
43
44 Revision 1.27 2003/11/19 18:49:48 rpantos
45 <rdar://problem/3282283> couple of little tweaks to previous checkin
46
47 Revision 1.26 2003/11/07 19:35:20 rpantos
48 <rdar://problem/3282283> Display multiple IP addresses. Connect using host rather than IP addr.
49
50 Revision 1.25 2003/10/29 05:16:54 rpantos
51 Checkpoint: transition from DNSServiceDiscovery.h to dns_sd.h
52
53 Revision 1.24 2003/10/28 02:25:45 rpantos
54 <rdar://problem/3282283> Cancel pending resolve when focus changes or service disappears.
55
56 Revision 1.23 2003/10/28 01:29:15 rpantos
57 <rdar://problem/3282283> Restructure a bit to make arrow keys work & views behave better.
58
59 Revision 1.22 2003/10/28 01:23:27 rpantos
60 <rdar://problem/3282283> Bail if mDNS cannot be initialized at startup.
61
62 Revision 1.21 2003/10/28 01:19:45 rpantos
63 <rdar://problem/3282283> Do not put a trailing '.' on service names. Handle PATH for HTTP txtRecords.
64
65 Revision 1.20 2003/10/28 01:13:49 rpantos
66 <rdar://problem/3282283> Remove filter when displaying browse results.
67
68 Revision 1.19 2003/10/28 01:10:14 rpantos
69 <rdar://problem/3282283> Change 'compare' to 'caseInsensitiveCompare' to fix sort order.
70
71 Revision 1.18 2003/08/12 19:55:07 cheshire
72 Update to APSL 2.0
73
74 */
75
76 #import <Cocoa/Cocoa.h>
77 #include <sys/types.h>
78 #include <sys/socket.h>
79 #include <net/if.h>
80 #include <arpa/inet.h>
81 #include <netdb.h>
82 #include <sys/select.h>
83 #include <netinet/in.h>
84 #include <unistd.h>
85 #include <dns_sd.h>
86
87 @class ServiceController; // holds state corresponding to outstanding DNSServiceRef
88
89 @interface BrowserController : NSObject
90 {
91 IBOutlet id nameField;
92 IBOutlet id typeField;
93
94 IBOutlet id serviceDisplayTable;
95 IBOutlet id typeColumn;
96 IBOutlet id nameColumn;
97 IBOutlet id serviceTypeField;
98 IBOutlet id serviceNameField;
99
100 IBOutlet id hostField;
101 IBOutlet id ipAddressField;
102 IBOutlet id ip6AddressField;
103 IBOutlet id portField;
104 IBOutlet id interfaceField;
105 IBOutlet id textField;
106
107 NSMutableArray *_srvtypeKeys;
108 NSMutableArray *_srvnameKeys;
109 NSMutableArray *_sortedServices;
110 NSMutableDictionary *_servicesDict;
111
112 ServiceController *_serviceBrowser;
113 ServiceController *_serviceResolver;
114 ServiceController *_ipv4AddressResolver;
115 ServiceController *_ipv6AddressResolver;
116 }
117
118 - (void)notifyTypeSelectionChange:(NSNotification*)note;
119 - (void)notifyNameSelectionChange:(NSNotification*)note;
120
121 - (IBAction)connect:(id)sender;
122
123 - (IBAction)handleTableClick:(id)sender;
124 - (IBAction)removeSelected:(id)sender;
125 - (IBAction)addNewService:(id)sender;
126
127 - (IBAction)update:(NSString *)Type;
128
129 - (void)updateBrowseWithName:(const char *)name type:(const char *)resulttype domain:(const char *)domain interface:(uint32_t)interface flags:(DNSServiceFlags)flags;
130 - (void)resolveClientWitHost:(NSString *)host port:(uint16_t)port interfaceIndex:(uint32_t)interface txtRecord:(const char*)txtRecord txtLen:(uint16_t)txtLen;
131 - (void)updateAddress:(uint16_t)rrtype addr:(const void *)buff addrLen:(uint16_t)addrLen host:(const char*)host interfaceIndex:(uint32_t)interface more:(boolean_t)moreToCome;
132
133 - (void)_cancelPendingResolve;
134 - (void)_clearResolvedInfo;
135
136 @end
137
138 // The ServiceController manages cleanup of DNSServiceRef & runloop info for an outstanding request
139 @interface ServiceController : NSObject
140 {
141 DNSServiceRef fServiceRef;
142 CFSocketRef fSocketRef;
143 CFRunLoopSourceRef fRunloopSrc;
144 }
145
146 - (id)initWithServiceRef:(DNSServiceRef)ref;
147 - (void)addToCurrentRunLoop;
148 - (DNSServiceRef)serviceRef;
149 - (void)dealloc;
150
151 @end // interface ServiceController
152
153
154 static void
155 ProcessSockData(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
156 {
157 DNSServiceRef serviceRef = (DNSServiceRef)info;
158 DNSServiceErrorType err = DNSServiceProcessResult(serviceRef);
159 if (err != kDNSServiceErr_NoError) {
160 printf("DNSServiceProcessResult() returned an error! %d\n", err);
161 }
162 }
163
164
165 static void
166 ServiceBrowseReply(DNSServiceRef sdRef, DNSServiceFlags servFlags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
167 const char *serviceName, const char *regtype, const char *replyDomain, void *context)
168 {
169 if (errorCode == kDNSServiceErr_NoError) {
170 [(BrowserController*)context updateBrowseWithName:serviceName type:regtype domain:replyDomain interface:interfaceIndex flags:servFlags];
171 } else {
172 printf("ServiceBrowseReply got an error! %d\n", errorCode);
173 }
174 }
175
176
177 static void
178 ServiceResolveReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
179 const char *fullname, const char *hosttarget, uint16_t port, uint16_t txtLen, const char *txtRecord, void *context)
180 {
181 if (errorCode == kDNSServiceErr_NoError) {
182 [(BrowserController*)context resolveClientWitHost:[NSString stringWithUTF8String:hosttarget] port:port interfaceIndex:interfaceIndex txtRecord:txtRecord txtLen:txtLen];
183 } else {
184 printf("ServiceResolveReply got an error! %d\n", errorCode);
185 }
186 }
187
188
189 static void
190 QueryRecordReply(DNSServiceRef DNSServiceRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
191 const char *fullname, uint16_t rrtype, uint16_t rrclass, uint16_t rdlen, const void *rdata, uint32_t ttl, void *context)
192 {
193 if (errorCode == kDNSServiceErr_NoError) {
194 [(BrowserController*)context updateAddress:rrtype addr:rdata addrLen:rdlen host:fullname interfaceIndex:interfaceIndex more:(flags & kDNSServiceFlagsMoreComing)];
195 } else {
196 printf("QueryRecordReply got an error! %d\n", errorCode);
197 }
198 }
199
200
201 static void
202 InterfaceIndexToName(uint32_t interface, char *interfaceName)
203 {
204 assert(interfaceName);
205
206 if (interface == kDNSServiceInterfaceIndexAny) {
207 // All active network interfaces.
208 strlcpy(interfaceName, "all", IF_NAMESIZE);
209 } else if (interface == kDNSServiceInterfaceIndexLocalOnly) {
210 // Only available locally on this machine.
211 strlcpy(interfaceName, "local", IF_NAMESIZE);
212 } else {
213 // Converts interface index to interface name.
214 if_indextoname(interface, interfaceName);
215 }
216 }
217
218
219 @implementation BrowserController //Begin implementation of BrowserController methods
220
221 - (void)registerDefaults
222 {
223 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
224
225 NSArray *typeArray = [NSArray arrayWithObjects:@"_afpovertcp._tcp",
226 @"_smb._tcp",
227 @"_rfb._tcp",
228 @"_ssh._tcp",
229 @"_ftp._tcp",
230 @"_http._tcp",
231 @"_printer._tcp",
232 @"_ipp._tcp",
233 @"_airport._tcp",
234 @"_presence._tcp",
235 @"_daap._tcp",
236 @"_dpap._tcp",
237 nil];
238
239 NSArray *nameArray = [NSArray arrayWithObjects:@"AppleShare Servers",
240 @"Windows Sharing",
241 @"Screen Sharing",
242 @"Secure Shell",
243 @"FTP Servers",
244 @"Web Servers",
245 @"LPR Printers",
246 @"IPP Printers",
247 @"AirPort Base Stations",
248 @"iChat Buddies",
249 @"iTunes Libraries",
250 @"iPhoto Libraries",
251 nil];
252
253 [regDict setObject:typeArray forKey:@"SrvTypeKeys"];
254 [regDict setObject:nameArray forKey:@"SrvNameKeys"];
255
256 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
257 }
258
259
260 - (id)init
261 {
262 self = [super init];
263 if (self) {
264 _srvtypeKeys = nil;
265 _srvnameKeys = nil;
266 _serviceBrowser = nil;
267 _serviceResolver = nil;
268 _ipv4AddressResolver = nil;
269 _ipv6AddressResolver = nil;
270 _sortedServices = [[NSMutableArray alloc] init];
271 _servicesDict = [[NSMutableDictionary alloc] init];
272 }
273 return self;
274 }
275
276
277 - (void)awakeFromNib
278 {
279 [typeField sizeLastColumnToFit];
280 [nameField sizeLastColumnToFit];
281 [nameField setDoubleAction:@selector(connect:)];
282
283 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyTypeSelectionChange:) name:NSTableViewSelectionDidChangeNotification object:typeField];
284 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyNameSelectionChange:) name:NSTableViewSelectionDidChangeNotification object:nameField];
285
286 _srvtypeKeys = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"] mutableCopy];
287 _srvnameKeys = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"] mutableCopy];
288
289 if (!_srvtypeKeys || !_srvnameKeys) {
290 [_srvtypeKeys release];
291 [_srvnameKeys release];
292 [self registerDefaults];
293 _srvtypeKeys = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"] mutableCopy];
294 _srvnameKeys = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"] mutableCopy];
295 }
296
297 [typeField reloadData];
298 }
299
300
301 - (void)dealloc
302 {
303 [_srvtypeKeys release];
304 [_srvnameKeys release];
305 [_servicesDict release];
306 [_sortedServices release];
307 [super dealloc];
308 }
309
310
311 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
312 {
313 if (row < 0) return;
314 }
315
316
317 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
318 {
319 if (theTableView == typeField) {
320 return [_srvnameKeys count];
321 }
322 if (theTableView == nameField) {
323 return [_servicesDict count];
324 }
325 if (theTableView == serviceDisplayTable) {
326 return [_srvnameKeys count];
327 }
328 return 0;
329 }
330
331
332 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
333 {
334 if (theTableView == typeField) {
335 return [_srvnameKeys objectAtIndex:rowIndex];
336 }
337 if (theTableView == nameField) {
338 return [[_servicesDict objectForKey:[_sortedServices objectAtIndex:rowIndex]] name];
339 }
340 if (theTableView == serviceDisplayTable) {
341 if (theColumn == typeColumn) {
342 return [_srvtypeKeys objectAtIndex:rowIndex];
343 }
344 if (theColumn == nameColumn) {
345 return [_srvnameKeys objectAtIndex:rowIndex];
346 }
347 return nil;
348 }
349
350 return nil;
351 }
352
353
354 - (void)notifyTypeSelectionChange:(NSNotification*)note
355 {
356 [self _cancelPendingResolve];
357
358 int index = [[note object] selectedRow];
359 if (index != -1) {
360 [self update:[_srvtypeKeys objectAtIndex:index]];
361 } else {
362 [self update:nil];
363 }
364 }
365
366
367 - (void)notifyNameSelectionChange:(NSNotification*)note
368 {
369 [self _cancelPendingResolve];
370
371 int index = [[note object] selectedRow];
372 if (index == -1) {
373 return;
374 }
375
376 // Get the currently selected service
377 NSNetService *service = [_servicesDict objectForKey:[_sortedServices objectAtIndex:index]];
378
379 DNSServiceRef serviceRef;
380 DNSServiceErrorType err = DNSServiceResolve(&serviceRef,
381 (DNSServiceFlags)0,
382 kDNSServiceInterfaceIndexAny,
383 (const char *)[[service name] UTF8String],
384 (const char *)[[service type] UTF8String],
385 (const char *)[[service domain] UTF8String],
386 (DNSServiceResolveReply)ServiceResolveReply,
387 self);
388
389 if (kDNSServiceErr_NoError == err) {
390 _serviceResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
391 [_serviceResolver addToCurrentRunLoop];
392 }
393 }
394
395
396 - (IBAction)update:(NSString *)theType
397 {
398 [_servicesDict removeAllObjects];
399 [_sortedServices removeAllObjects];
400 [nameField reloadData];
401
402 // get rid of the previous browser if one exists
403 if (_serviceBrowser != nil) {
404 [_serviceBrowser release];
405 _serviceBrowser = nil;
406 }
407
408 if (theType) {
409 DNSServiceRef serviceRef;
410 DNSServiceErrorType err = DNSServiceBrowse(&serviceRef, (DNSServiceFlags)0, 0, [theType UTF8String], NULL, ServiceBrowseReply, self);
411 if (kDNSServiceErr_NoError == err) {
412 _serviceBrowser = [[ServiceController alloc] initWithServiceRef:serviceRef];
413 [_serviceBrowser addToCurrentRunLoop];
414 }
415 }
416 }
417
418
419 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
420 {
421 return YES;
422 }
423
424
425 - (void)updateBrowseWithName:(const char *)name type:(const char *)type domain:(const char *)domain interface:(uint32_t)interface flags:(DNSServiceFlags)flags
426 {
427 NSString *key = [NSString stringWithFormat:@"%s.%s%s%d", name, type, domain, interface];
428 NSNetService *service = [[NSNetService alloc] initWithDomain:[NSString stringWithUTF8String:domain] type:[NSString stringWithUTF8String:type] name:[NSString stringWithUTF8String:name]];
429
430 if (flags & kDNSServiceFlagsAdd) {
431 [_servicesDict setObject:service forKey:key];
432 } else {
433 [_servicesDict removeObjectForKey:key];
434 }
435
436 // If not expecting any more data, then reload (redraw) TableView with newly found data
437 if (!(flags & kDNSServiceFlagsMoreComing)) {
438
439 // Save the current TableView selection
440 int index = [nameField selectedRow];
441 NSString *selected = (index != -1) ? [[_sortedServices objectAtIndex:index] copy] : nil;
442
443 [_sortedServices release];
444 _sortedServices = [[_servicesDict allKeys] mutableCopy];
445 [_sortedServices sortUsingSelector:@selector(caseInsensitiveCompare:)];
446 [nameField reloadData];
447
448 // Restore the previous TableView selection
449 index = selected ? [_sortedServices indexOfObject:selected] : NSNotFound;
450 if (index != NSNotFound) {
451 [nameField selectRowIndexes:[NSIndexSet indexSetWithIndex:index] byExtendingSelection:NO];
452 [nameField scrollRowToVisible:index];
453 }
454
455 [selected release];
456 }
457
458 [service release];
459
460 return;
461 }
462
463
464 - (void)resolveClientWitHost:(NSString *)host port:(uint16_t)port interfaceIndex:(uint32_t)interface txtRecord:(const char*)txtRecord txtLen:(uint16_t)txtLen
465 {
466 DNSServiceRef serviceRef;
467
468 if (_ipv4AddressResolver) {
469 [_ipv4AddressResolver release];
470 _ipv4AddressResolver = nil;
471 }
472
473 if (_ipv6AddressResolver) {
474 [_ipv6AddressResolver release];
475 _ipv6AddressResolver = nil;
476 }
477
478 // Start an async lookup for IPv4 addresses
479 DNSServiceErrorType err = DNSServiceQueryRecord(&serviceRef, (DNSServiceFlags)0, interface, [host UTF8String], kDNSServiceType_A, kDNSServiceClass_IN, QueryRecordReply, self);
480 if (err == kDNSServiceErr_NoError) {
481 _ipv4AddressResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
482 [_ipv4AddressResolver addToCurrentRunLoop];
483 }
484
485 // Start an async lookup for IPv6 addresses
486 err = DNSServiceQueryRecord(&serviceRef, (DNSServiceFlags)0, interface, [host UTF8String], kDNSServiceType_AAAA, kDNSServiceClass_IN, QueryRecordReply, self);
487 if (err == kDNSServiceErr_NoError) {
488 _ipv6AddressResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
489 [_ipv6AddressResolver addToCurrentRunLoop];
490 }
491
492 char interfaceName[IF_NAMESIZE];
493 InterfaceIndexToName(interface, interfaceName);
494
495 [hostField setStringValue:host];
496 [interfaceField setStringValue:[NSString stringWithUTF8String:interfaceName]];
497 [portField setIntValue:ntohs(port)];
498
499 // kind of a hack: munge txtRecord so it's human-readable
500 if (txtLen > 0) {
501 char *readableText = (char*) malloc(txtLen);
502 if (readableText != nil) {
503 ByteCount index, subStrLen;
504 memcpy(readableText, txtRecord, txtLen);
505 for (index=0; index < txtLen - 1; index += subStrLen + 1) {
506 subStrLen = readableText[index];
507 readableText[index] = ' ';
508 }
509 [textField setStringValue:[NSString stringWithCString:&readableText[1] length:txtLen - 1]];
510 free(readableText);
511 }
512 }
513 }
514
515
516 - (void)updateAddress:(uint16_t)rrtype addr:(const void *)buff addrLen:(uint16_t)addrLen host:(const char*) host interfaceIndex:(uint32_t)interface more:(boolean_t)moreToCome
517 {
518 char addrBuff[256];
519
520 if (rrtype == kDNSServiceType_A) {
521 inet_ntop(AF_INET, buff, addrBuff, sizeof(addrBuff));
522 if ([[ipAddressField stringValue] length] > 0) {
523 [ipAddressField setStringValue:[NSString stringWithFormat:@"%@, ", [ipAddressField stringValue]]];
524 }
525 [ipAddressField setStringValue:[NSString stringWithFormat:@"%@%s", [ipAddressField stringValue], addrBuff]];
526
527 if (!moreToCome) {
528 [_ipv4AddressResolver release];
529 _ipv4AddressResolver = nil;
530 }
531 } else if (rrtype == kDNSServiceType_AAAA) {
532 inet_ntop(AF_INET6, buff, addrBuff, sizeof(addrBuff));
533 if ([[ip6AddressField stringValue] length] > 0) {
534 [ip6AddressField setStringValue:[NSString stringWithFormat:@"%@, ", [ip6AddressField stringValue]]];
535 }
536 [ip6AddressField setStringValue:[NSString stringWithFormat:@"%@%s", [ip6AddressField stringValue], addrBuff]];
537
538 if (!moreToCome) {
539 [_ipv6AddressResolver release];
540 _ipv6AddressResolver = nil;
541 }
542 }
543 }
544
545
546 - (void)connect:(id)sender
547 {
548 NSString *host = [hostField stringValue];
549 NSString *txtRecord = [textField stringValue];
550 int port = [portField intValue];
551
552 int index = [nameField selectedRow];
553 NSString *selected = (index >= 0) ? [_sortedServices objectAtIndex:index] : nil;
554 NSString *type = [[_servicesDict objectForKey:selected] type];
555
556 if ([type isEqual:@"_http._tcp."]) {
557 NSString *pathDelim = @"path=";
558 NSRange where;
559
560 // If the TXT record specifies a path, extract it.
561 where = [txtRecord rangeOfString:pathDelim options:NSCaseInsensitiveSearch];
562 if (where.length) {
563 NSRange targetRange = { where.location + where.length, [txtRecord length] - where.location - where.length };
564 NSRange endDelim = [txtRecord rangeOfString:@"\n" options:kNilOptions range:targetRange];
565
566 if (endDelim.length) // if a delimiter was found, truncate the target range
567 targetRange.length = endDelim.location - targetRange.location;
568
569 NSString *path = [txtRecord substringWithRange:targetRange];
570 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%d%@", host, port, path]]];
571 } else {
572 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%d", host, port]]];
573 }
574 }
575 else if ([type isEqual:@"_ftp._tcp."]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"ftp://%@:%d/", host, port]]];
576 else if ([type isEqual:@"_ssh._tcp."]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"ssh://%@:%d/", host, port]]];
577 else if ([type isEqual:@"_afpovertcp._tcp."]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"afp://%@:%d/", host, port]]];
578 else if ([type isEqual:@"_smb._tcp."]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"smb://%@:%d/", host, port]]];
579 else if ([type isEqual:@"_rfb._tcp."]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"vnc://%@:%d/", host, port]]];
580
581 return;
582 }
583
584
585 - (IBAction)handleTableClick:(id)sender
586 {
587 //populate the text fields
588 }
589
590
591 - (IBAction)removeSelected:(id)sender
592 {
593 // remove the selected row and force a refresh
594
595 int selectedRow = [serviceDisplayTable selectedRow];
596
597 if (selectedRow) {
598
599 [_srvtypeKeys removeObjectAtIndex:selectedRow];
600 [_srvnameKeys removeObjectAtIndex:selectedRow];
601
602 [[NSUserDefaults standardUserDefaults] setObject:_srvtypeKeys forKey:@"SrvTypeKeys"];
603 [[NSUserDefaults standardUserDefaults] setObject:_srvnameKeys forKey:@"SrvNameKeys"];
604
605 [typeField reloadData];
606 [serviceDisplayTable reloadData];
607 }
608 }
609
610
611 - (IBAction)addNewService:(id)sender
612 {
613 // add new entries from the edit fields to the arrays for the defaults
614 NSString *newType = [serviceTypeField stringValue];
615 NSString *newName = [serviceNameField stringValue];
616
617 // 3282283: trim trailing '.' from service type field
618 if ([newType length] && [newType hasSuffix:@"."])
619 newType = [newType substringToIndex:[newType length] - 1];
620
621 if ([newType length] && [newName length]) {
622 [_srvtypeKeys addObject:newType];
623 [_srvnameKeys addObject:newName];
624
625 [[NSUserDefaults standardUserDefaults] setObject:_srvtypeKeys forKey:@"SrvTypeKeys"];
626 [[NSUserDefaults standardUserDefaults] setObject:_srvnameKeys forKey:@"SrvNameKeys"];
627
628 [typeField reloadData];
629 [serviceDisplayTable reloadData];
630 }
631 }
632
633
634 - (void)_cancelPendingResolve
635 {
636 [_ipv4AddressResolver release];
637 _ipv4AddressResolver = nil;
638
639 [_ipv6AddressResolver release];
640 _ipv6AddressResolver = nil;
641
642 [_serviceResolver release];
643 _serviceResolver = nil;
644
645 [self _clearResolvedInfo];
646 }
647
648
649 - (void)_clearResolvedInfo
650 {
651 [hostField setStringValue:@""];
652 [ipAddressField setStringValue:@""];
653 [ip6AddressField setStringValue:@""];
654 [portField setStringValue:@""];
655 [interfaceField setStringValue:@""];
656 [textField setStringValue:@""];
657 }
658
659 @end // implementation BrowserController
660
661
662 @implementation ServiceController : NSObject
663 {
664 DNSServiceRef fServiceRef;
665 CFSocketRef fSocketRef;
666 CFRunLoopSourceRef fRunloopSrc;
667 }
668
669
670 - (id)initWithServiceRef:(DNSServiceRef)ref
671 {
672 self = [super init];
673 if (self) {
674 fServiceRef = ref;
675 fSocketRef = NULL;
676 fRunloopSrc = NULL;
677 }
678 return self;
679 }
680
681
682 - (void)addToCurrentRunLoop
683 {
684 CFSocketContext context = { 0, (void*)fServiceRef, NULL, NULL, NULL };
685
686 fSocketRef = CFSocketCreateWithNative(kCFAllocatorDefault, DNSServiceRefSockFD(fServiceRef), kCFSocketReadCallBack, ProcessSockData, &context);
687 if (fSocketRef) {
688 // Prevent CFSocketInvalidate from closing DNSServiceRef's socket.
689 CFOptionFlags sockFlags = CFSocketGetSocketFlags(fSocketRef);
690 CFSocketSetSocketFlags(fSocketRef, sockFlags & (~kCFSocketCloseOnInvalidate));
691 fRunloopSrc = CFSocketCreateRunLoopSource(kCFAllocatorDefault, fSocketRef, 0);
692 }
693 if (fRunloopSrc) {
694 CFRunLoopAddSource(CFRunLoopGetCurrent(), fRunloopSrc, kCFRunLoopDefaultMode);
695 } else {
696 printf("Could not listen to runloop socket\n");
697 }
698 }
699
700
701 - (DNSServiceRef)serviceRef
702 {
703 return fServiceRef;
704 }
705
706
707 - (void)dealloc
708 {
709 if (fSocketRef) {
710 CFSocketInvalidate(fSocketRef); // Note: Also closes the underlying socket
711 CFRelease(fSocketRef);
712
713 // Workaround that gives time to CFSocket's select thread so it can remove the socket from its
714 // FD set before we close the socket by calling DNSServiceRefDeallocate. <rdar://problem/3585273>
715 usleep(1000);
716 }
717
718 if (fRunloopSrc) {
719 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), fRunloopSrc, kCFRunLoopDefaultMode);
720 CFRelease(fRunloopSrc);
721 }
722
723 DNSServiceRefDeallocate(fServiceRef);
724
725 [super dealloc];
726 }
727
728
729 @end // implementation ServiceController
730
731 int main(int argc, const char *argv[])
732 {
733 return NSApplicationMain(argc, argv);
734 }