1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
19 $Log: DNSServiceBrowser.m,v $
20 Revision 1.31 2006/08/14 23:23:55 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
23 Revision 1.30 2005/01/27 17:46:16 cheshire
26 Revision 1.29 2004/06/04 20:58:36 cheshire
27 Move DNSServiceBrowser from mDNSMacOSX directory to Clients directory
29 Revision 1.28 2004/05/18 23:51:26 cheshire
30 Tidy up all checkin comments to use consistent "<rdar://problem/xxxxxxx>" format for bug numbers
32 Revision 1.27 2003/11/19 18:49:48 rpantos
33 <rdar://problem/3282283>: couple of little tweaks to previous checkin
35 Revision 1.26 2003/11/07 19:35:20 rpantos
36 <rdar://problem/3282283>/6: Display multiple IP addresses. Connect using host rather than IP addr.
38 Revision 1.25 2003/10/29 05:16:54 rpantos
39 Checkpoint: transition from DNSServiceDiscovery.h to dns_sd.h
41 Revision 1.24 2003/10/28 02:25:45 rpantos
42 <rdar://problem/3282283>/9,10: Cancel pending resolve when focus changes or service disappears.
44 Revision 1.23 2003/10/28 01:29:15 rpantos
45 <rdar://problem/3282283>/4,5: Restructure a bit to make arrow keys work & views behave better.
47 Revision 1.22 2003/10/28 01:23:27 rpantos
48 <rdar://problem/3282283>/11: Bail if mDNS cannot be initialized at startup.
50 Revision 1.21 2003/10/28 01:19:45 rpantos
51 <rdar://problem/3282283>/3,11: Do not put a trailing '.' on service names. Handle PATH for HTTP txtRecords.
53 Revision 1.20 2003/10/28 01:13:49 rpantos
54 <rdar://problem/3282283>/2: Remove filter when displaying browse results.
56 Revision 1.19 2003/10/28 01:10:14 rpantos
57 <rdar://problem/3282283>/1: Change 'compare' to 'caseInsensitiveCompare' to fix sort order.
59 Revision 1.18 2003/08/12 19:55:07 cheshire
64 #include <arpa/inet.h>
67 #include <sys/select.h>
69 #import <Cocoa/Cocoa.h>
70 #import <DNSServiceDiscovery/DNSServiceDiscovery.h>
72 #include <netinet/in.h>
75 @class ServiceController; // holds state corresponding to outstanding DNSServiceRef
77 @interface BrowserController : NSObject
79 IBOutlet id domainField;
80 IBOutlet id nameField;
81 IBOutlet id typeField;
83 IBOutlet id serviceDisplayTable;
84 IBOutlet id typeColumn;
85 IBOutlet id nameColumn;
86 IBOutlet id serviceTypeField;
87 IBOutlet id serviceNameField;
89 IBOutlet id hostField;
90 IBOutlet id ipAddressField;
91 IBOutlet id ip6AddressField;
92 IBOutlet id portField;
93 IBOutlet id textField;
95 NSMutableArray *srvtypeKeys;
96 NSMutableArray *srvnameKeys;
97 NSMutableArray *domainKeys;
98 NSMutableArray *nameKeys;
104 ServiceController *fDomainBrowser;
105 ServiceController *fServiceBrowser;
106 ServiceController *fServiceResolver;
107 ServiceController *fAddressResolver;
111 - (IBAction)handleDomainClick:(id)sender;
112 - (IBAction)handleNameClick:(id)sender;
113 - (IBAction)handleTypeClick:(id)sender;
114 - (void)notifyTypeSelectionChange:(NSNotification*)note;
115 - (void)notifyNameSelectionChange:(NSNotification*)note;
117 - (IBAction)connect:(id)sender;
119 - (IBAction)handleTableClick:(id)sender;
120 - (IBAction)removeSelected:(id)sender;
121 - (IBAction)addNewService:(id)sender;
123 - (IBAction)update:(NSString *)Type Domain:(NSString *)Domain;
124 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
125 - (IBAction)loadDomains:(id)sender;
127 - (void)updateBrowseWithResult:(DNSServiceFlags)flags name:(NSString *)name type:(NSString *)resulttype domain:(NSString *)domain;
128 - (void)updateEnumWithResult:(DNSServiceFlags)flags domain:(NSString *)domain;
129 - (void)resolveClientWitHost:(NSString *)host port:(uint16_t)port interfaceIndex:(uint32_t)interface txtRecord:(const char*)txtRecord txtLen:(uint16_t)txtLen;
130 - (void)updateAddress:(uint16_t)rrtype addr:(const void *)buff addrLen:(uint16_t)addrLen
131 host:(const char*) host interfaceIndex:(uint32_t)interface more:(boolean_t)moreToCome;
133 - (void)_cancelPendingResolve;
134 - (void)_clearResolvedInfo;
138 // The ServiceController manages cleanup of DNSServiceRef & runloop info for an outstanding request
139 @interface ServiceController : NSObject
141 DNSServiceRef fServiceRef;
142 CFSocketRef fSocketRef;
143 CFRunLoopSourceRef fRunloopSrc;
146 - (id) initWithServiceRef:(DNSServiceRef) ref;
147 - (boolean_t) addToCurrentRunLoop;
148 - (DNSServiceRef) serviceRef;
151 @end // interface ServiceController
154 static void ProcessSockData( CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
155 // CFRunloop callback that notifies dns_sd when new data appears on a DNSServiceRef's socket.
157 DNSServiceRef serviceRef = (DNSServiceRef) info;
158 DNSServiceErrorType err = DNSServiceProcessResult( serviceRef);
159 if ( err != kDNSServiceErr_NoError)
160 printf( "DNSServiceProcessResult() returned an error! %d\n", err);
163 static void DomainEnumReply( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
164 DNSServiceErrorType errorCode, const char *replyDomain, void *context )
165 // Report newly-discovered domains to the BrowserController.
167 if ( errorCode == kDNSServiceErr_NoError) {
168 BrowserController *pSelf = (BrowserController*) context;
169 [pSelf updateEnumWithResult:flags domain:[NSString stringWithUTF8String:replyDomain]];
171 printf( "DomainEnumReply got an error! %d\n", errorCode);
175 static void ServiceBrowseReply( DNSServiceRef sdRef, DNSServiceFlags servFlags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
176 const char *serviceName, const char *regtype, const char *replyDomain, void *context )
177 // Report newly-discovered services to the BrowserController.
179 if ( errorCode == kDNSServiceErr_NoError) {
180 BrowserController *pSelf = (BrowserController*) context;
181 [pSelf updateBrowseWithResult:servFlags name:[NSString stringWithUTF8String:serviceName]
182 type:[NSString stringWithUTF8String:regtype] domain:[NSString stringWithUTF8String:replyDomain]];
184 printf( "ServiceBrowseReply got an error! %d\n", errorCode);
188 static void ServiceResolveReply( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
189 DNSServiceErrorType errorCode, const char *fullname, const char *hosttarget, uint16_t port,
190 uint16_t txtLen, const char *txtRecord, void *context )
191 // Pass along resolved service info to the BrowserController.
193 if ( errorCode == kDNSServiceErr_NoError) {
194 BrowserController *pSelf = (BrowserController*) context;
195 [pSelf resolveClientWitHost:[NSString stringWithUTF8String:hosttarget] port:port interfaceIndex:interfaceIndex txtRecord:txtRecord txtLen:txtLen];
197 printf( "ServiceResolveReply got an error! %d\n", errorCode);
201 static void QueryRecordReply( DNSServiceRef DNSServiceRef, DNSServiceFlags flags, uint32_t interfaceIndex,
202 DNSServiceErrorType errorCode, const char *fullname, uint16_t rrtype, uint16_t rrclass,
203 uint16_t rdlen, const void *rdata, uint32_t ttl, void *context )
204 // DNSServiceQueryRecord callback used to look up IP addresses.
206 BrowserController *pBrowser = (BrowserController*) context;
208 [pBrowser updateAddress:rrtype addr:rdata addrLen:rdlen host:fullname interfaceIndex:interfaceIndex
209 more:((flags & kDNSServiceFlagsMoreComing) != 0)];
213 @implementation BrowserController //Begin implementation of BrowserController methods
215 - (void)registerDefaults
217 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
219 NSArray *typeArray = [NSArray arrayWithObjects:@"_ftp._tcp", @"_tftp._tcp",
220 @"_ssh._tcp", @"_telnet._tcp",
222 @"_printer._tcp", @"_ipp._tcp",
223 @"_ichat._tcp", @"_eppc._tcp",
224 @"_afpovertcp._tcp", @"_afpovertcp._tcp", @"_MacOSXDupSuppress._tcp", nil];
225 NSArray *nameArray = [NSArray arrayWithObjects:@"File Transfer (ftp)", @"Trivial File Transfer (tftp)",
226 @"Secure Shell (ssh)", @"Telnet",
227 @"Web Server (http)",
228 @"LPR Printer", @"IPP Printer",
229 @"iChat", @"Remote AppleEvents",
230 @"AppleShare Server", @"SMB File Server", @"Mystery Service", nil];
232 [regDict setObject:typeArray forKey:@"SrvTypeKeys"];
233 [regDict setObject:nameArray forKey:@"SrvNameKeys"];
235 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
241 [self registerDefaults];
243 fDomainBrowser = nil;
244 fServiceBrowser = nil;
245 fServiceResolver = nil;
246 fAddressResolver = nil;
251 - (void)awakeFromNib //BrowserController startup procedure
255 srvtypeKeys = [NSMutableArray array]; //Define arrays for Type, Domain, and Name
256 srvnameKeys = [NSMutableArray array];
258 domainKeys = [NSMutableArray array];
261 nameKeys = [NSMutableArray array];
264 [srvtypeKeys retain]; //Keep arrays in memory until BrowserController closes
265 [srvnameKeys retain]; //Keep arrays in memory until BrowserController closes
266 [typeField sizeLastColumnToFit]; //Set column sizes to use their whole table's width.
267 [nameField sizeLastColumnToFit];
268 [domainField sizeLastColumnToFit];
269 // (self is specified as the NSTableViews' data source in the nib)
271 [nameField setDoubleAction:@selector(connect:)];
273 // Listen for table selection changes
274 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyTypeSelectionChange:)
275 name:NSTableViewSelectionDidChangeNotification object:typeField];
276 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyNameSelectionChange:)
277 name:NSTableViewSelectionDidChangeNotification object:nameField];
279 //[srvtypeKeys addObject:@"_ftp._tcp"]; //Add supported protocols and domains to their
280 //[srvnameKeys addObject:@"File Transfer (ftp)"];
281 //[srvtypeKeys addObject:@"_printer._tcp"]; //respective arrays
282 //[srvnameKeys addObject:@"Printer (lpr)"];
283 //[srvtypeKeys addObject:@"_http._tcp"]; //respective arrays
284 //[srvnameKeys addObject:@"Web Server (http)"];
285 //[srvtypeKeys addObject:@"_afp._tcp"]; //respective arrays
286 //[srvnameKeys addObject:@"AppleShare Server (afp)"];
288 [self _clearResolvedInfo];
290 [srvtypeKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"]];
291 [srvnameKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"]];
294 [typeField reloadData]; //Reload (redraw) data in fields
295 [domainField reloadData];
297 [self loadDomains:self];
301 - (void)dealloc //Deallocation method
303 [srvtypeKeys release];
304 [srvnameKeys release];
306 [domainKeys release];
309 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
314 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
316 if (theTableView == typeField)
318 return [srvnameKeys count];
320 if (theTableView == domainField)
322 return [domainKeys count];
324 if (theTableView == nameField)
326 return [nameKeys count];
328 if (theTableView == serviceDisplayTable)
330 return [srvnameKeys count];
335 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
337 if (theTableView == typeField)
339 return [srvnameKeys objectAtIndex:rowIndex];
341 if (theTableView == domainField)
343 return [domainKeys objectAtIndex:rowIndex];
345 if (theTableView == nameField)
347 return [[nameKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:rowIndex];
349 if (theTableView == serviceDisplayTable)
351 if (theColumn == typeColumn) {
352 return [srvtypeKeys objectAtIndex:rowIndex];
354 if (theColumn == nameColumn) {
355 return [srvnameKeys objectAtIndex:rowIndex];
361 } //End of mandatory TableView methods
363 - (IBAction)handleTypeClick:(id)sender //Handle clicks for Type
365 // 3282283: No longer used - update happens in notifyTypeSelectionChange
369 - (IBAction)handleDomainClick:(id)sender //Handle clicks for Domain
371 int index=[sender selectedRow]; //Find index of selected row
372 if (index==-1) return; //Error checking
373 Domain = [domainKeys objectAtIndex:index]; //Save desired Domain
375 [self _cancelPendingResolve];
377 if (SrvType!=NULL) [self update:SrvType Domain:Domain]; //If Type and Domain are set, update records
380 - (IBAction)handleNameClick:(id)sender //Handle clicks for Name
382 // 3282283: No longer used - update happens in notifyNameSelectionChange
385 - (void)notifyTypeSelectionChange:(NSNotification*)note
386 /* Called when the selection of the Type table changes */
388 int index=[[note object] selectedRow]; //Find index of selected row
389 if (index==-1) return; //Error checking
390 SrvType = [srvtypeKeys objectAtIndex:index]; //Save desired Type
391 SrvName = [srvnameKeys objectAtIndex:index]; //Save desired Type
393 [self _cancelPendingResolve];
395 [self update:SrvType Domain:Domain]; //If Type and Domain are set, update records
398 - (void)notifyNameSelectionChange:(NSNotification*)note
399 /* Called when the selection of the Name table changes */
401 int index=[[note object] selectedRow]; //Find index of selected row
403 [self _cancelPendingResolve]; // Cancel any pending Resolve for any table selection change
406 Name = nil; // Name may no longer point to a list member
409 Name=[[nameKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:index]; //Save desired name
411 [self _clearResolvedInfo];
413 DNSServiceRef serviceRef;
414 DNSServiceErrorType err;
415 err = DNSServiceResolve ( &serviceRef, (DNSServiceFlags) 0, 0, (char *)[Name UTF8String], (char *)[SrvType UTF8String],
416 (char *)(Domain?[Domain UTF8String]:""), ServiceResolveReply, self);
417 if ( kDNSServiceErr_NoError == err) {
418 fServiceResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
419 [fServiceResolver addToCurrentRunLoop];
423 - (IBAction)loadDomains:(id)sender
425 DNSServiceErrorType err;
426 DNSServiceRef serviceRef;
428 err = DNSServiceEnumerateDomains( &serviceRef, kDNSServiceFlagsBrowseDomains, 0, DomainEnumReply, self);
429 if ( kDNSServiceErr_NoError == err) {
430 fDomainBrowser = [[ServiceController alloc] initWithServiceRef:serviceRef];
431 [fDomainBrowser addToCurrentRunLoop];
434 NSAlert *alert = [NSAlert alertWithMessageText:@"Could not connect to mDNSResponder!"
435 defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:
436 @"Check to see if mDNSResponder is still running."];
443 - (IBAction)update:theType Domain:theDomain; //The Big Kahuna: Fetch PTR records and update application
445 const char * DomainC;
446 const char * TypeC=[theType UTF8String]; //Type in C string format
448 DNSServiceErrorType err = kDNSServiceErr_NoError;
451 DomainC = [theDomain UTF8String]; //Domain in C string format
456 [nameKeys removeAllObjects]; //Get rid of displayed records if we're going to go get new ones
457 [nameField reloadData]; //Reload (redraw) names to show the old data is gone
459 // get rid of the previous browser if one exists
460 if ( fServiceBrowser != nil) {
461 [fServiceBrowser release];
462 fServiceBrowser = nil;
465 // now create a browser to return the values for the nameField ...
466 DNSServiceRef serviceRef;
467 err = DNSServiceBrowse( &serviceRef, (DNSServiceFlags) 0, 0, TypeC, DomainC, ServiceBrowseReply, self);
468 if ( kDNSServiceErr_NoError == err) {
469 fServiceBrowser = [[ServiceController alloc] initWithServiceRef:serviceRef];
470 [fServiceBrowser addToCurrentRunLoop];
475 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication //Quit when main window is closed
480 - (BOOL)windowShouldClose:(NSWindow *)sender //Save domains to our domain file when quitting
482 [domainField reloadData];
486 - (void)updateEnumWithResult:(DNSServiceFlags)flags domain:(NSString *)domain
488 if ( ( flags & kDNSServiceFlagsAdd) != 0) { // new domain received
489 // add the domain to the list
490 [domainKeys addObject:domain];
491 } else if (!(flags & kDNSServiceFlagsAdd)) {
492 // remove the domain from the list
493 NSEnumerator *dmnEnum = [domainKeys objectEnumerator];
494 NSString *aDomain = nil;
496 while (aDomain = [dmnEnum nextObject]) {
497 if ([aDomain isEqualToString:domain]) {
498 [domainKeys removeObject:domain];
503 // update the domain table
504 [domainField reloadData];
506 // Terminate the enumeration once the last domain is delivered.
507 if ( ( flags & kDNSServiceFlagsMoreComing) == 0) {
508 [fDomainBrowser release];
509 fDomainBrowser = nil;
512 // At some point, we may want to support a TableView for domain browsing. For now, just pick first domain that comes up.
514 Domain = [domain retain];
521 - (void)updateBrowseWithResult:(DNSServiceFlags)flags name:(NSString *)name type:(NSString *)resulttype domain:(NSString *)domain
524 //NSLog(@"Received result %@ %@ %@ %d", name, resulttype, domain, type);
526 if (!(flags & kDNSServiceFlagsAdd)) {
527 if ([nameKeys containsObject:name]) {
528 [nameKeys removeObject:name];
530 // 3282283: Cancel pending browse if object goes away.
531 if ( [name isEqualToString:Name])
532 [nameField deselectAll:self];
535 else if ( ( flags & kDNSServiceFlagsAdd) != 0) {
536 if (![nameKeys containsObject:name]) {
537 [nameKeys addObject:name];
541 // If not expecting any more data, then reload (redraw) Name TableView with newly found data
542 if ((flags & kDNSServiceFlagsMoreComing) == 0)
543 [nameField reloadData];
547 - (void)resolveClientWitHost:(NSString *)host port:(uint16_t)port interfaceIndex:(uint32_t)interface
548 txtRecord:(const char*)txtRecord txtLen:(uint16_t)txtLen
549 /* Display resolved information about the selected service. */
551 DNSServiceErrorType err;
552 DNSServiceRef serviceRef;
554 // Start an async lookup for IPv4 & IPv6 addresses
555 if ( fAddressResolver != nil) {
556 [fAddressResolver release];
557 fAddressResolver = nil;
559 err = DNSServiceQueryRecord( &serviceRef, (DNSServiceFlags) 0, interface, [host UTF8String],
560 ns_t_a, ns_c_in, QueryRecordReply, self);
561 if ( err == kDNSServiceErr_NoError) {
562 fAddressResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
563 [fAddressResolver addToCurrentRunLoop];
566 [hostField setStringValue:host];
567 [portField setIntValue:port];
569 // kind of a hack: munge txtRecord so it's human-readable
571 char *readableText = (char*) malloc( txtLen);
572 if ( readableText != nil) {
573 ByteCount index, subStrLen;
574 memcpy( readableText, txtRecord, txtLen);
575 for ( index=0; index < txtLen - 1; index += subStrLen + 1) {
576 subStrLen = readableText[ index];
577 readableText[ index] = '\n';
579 [textField setStringValue:[NSString stringWithCString:&readableText[1] length:txtLen - 1]];
585 - (void)updateAddress:(uint16_t)rrtype addr:(const void *)buff addrLen:(uint16_t)addrLen
586 host:(const char*) host interfaceIndex:(uint32_t)interface more:(boolean_t)moreToCome
587 /* Update address field(s) with info obtained by fAddressResolver. */
589 if ( rrtype == ns_t_a) { // IPv4
591 inet_ntop( AF_INET, buff, addrBuff, sizeof addrBuff);
592 strcat( addrBuff, " ");
593 [ipAddressField setStringValue:[NSString stringWithFormat:@"%@%s", [ipAddressField stringValue], addrBuff]];
596 [fAddressResolver release];
597 fAddressResolver = nil;
599 // After we find v4 we look for v6
600 DNSServiceRef serviceRef;
601 DNSServiceErrorType err;
602 err = DNSServiceQueryRecord( &serviceRef, (DNSServiceFlags) 0, interface, host,
603 ns_t_aaaa, ns_c_in, QueryRecordReply, self);
604 if ( err == kDNSServiceErr_NoError) {
605 fAddressResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
606 [fAddressResolver addToCurrentRunLoop];
610 else if ( rrtype == ns_t_aaaa) // IPv6
613 inet_ntop( AF_INET6, buff, addrBuff, sizeof addrBuff);
614 strcat( addrBuff, " ");
615 [ip6AddressField setStringValue:[NSString stringWithFormat:@"%@%s", [ip6AddressField stringValue], addrBuff]];
618 [fAddressResolver release];
619 fAddressResolver = nil;
625 - (void)connect:(id)sender
627 NSString *host = [hostField stringValue];
628 int port = [portField intValue];
629 NSString *txtRecord = [textField stringValue];
631 if (!txtRecord) txtRecord = @"";
633 if (!host || !port) return;
635 if ([SrvType isEqualToString:@"_http._tcp"])
637 NSString *pathDelim = @"path=";
640 // If the TXT record specifies a path, extract it.
641 where = [txtRecord rangeOfString:pathDelim options:NSCaseInsensitiveSearch];
644 NSRange targetRange = { where.location + where.length, [txtRecord length] - where.location - where.length };
645 NSRange endDelim = [txtRecord rangeOfString:@"\n" options:kNilOptions range:targetRange];
647 if ( endDelim.length) // if a delimiter was found, truncate the target range
648 targetRange.length = endDelim.location - targetRange.location;
650 NSString *path = [txtRecord substringWithRange:targetRange];
651 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%d%@", host, port, path]]];
654 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%d", host, port]]];
656 else if ([SrvType isEqualToString:@"_ftp._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"ftp://%@:%d/", host, port]]];
657 else if ([SrvType isEqualToString:@"_tftp._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tftp://%@:%d/", host, port]]];
658 else if ([SrvType isEqualToString:@"_ssh._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"ssh://%@:%d/", host, port]]];
659 else if ([SrvType isEqualToString:@"_telnet._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telnet://%@:%d/", host, port]]];
660 else if ([SrvType isEqualToString:@"_printer._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"lpr://%@:%d/", host, port]]];
661 else if ([SrvType isEqualToString:@"_ipp._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"ipp://%@:%d/", host, port]]];
662 else if ([SrvType isEqualToString:@"_afpovertcp._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"afp://%@:%d/", host, port]]];
663 else if ([SrvType isEqualToString:@"_smb._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"smb://%@:%d/", host, port]]];
668 - (IBAction)handleTableClick:(id)sender
670 //populate the text fields
673 - (IBAction)removeSelected:(id)sender
675 // remove the selected row and force a refresh
677 int selectedRow = [serviceDisplayTable selectedRow];
681 [srvtypeKeys removeObjectAtIndex:selectedRow];
682 [srvnameKeys removeObjectAtIndex:selectedRow];
684 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
685 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
687 [typeField reloadData];
688 [serviceDisplayTable reloadData];
692 - (IBAction)addNewService:(id)sender
694 // add new entries from the edit fields to the arrays for the defaults
695 NSString *newType = [serviceTypeField stringValue];
696 NSString *newName = [serviceNameField stringValue];
698 // 3282283: trim trailing '.' from service type field
699 if ([newType length] && [newType hasSuffix:@"."])
700 newType = [newType substringToIndex:[newType length] - 1];
702 if ([newType length] && [newName length]) {
703 [srvtypeKeys addObject:newType];
704 [srvnameKeys addObject:newName];
706 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
707 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
709 [typeField reloadData];
710 [serviceDisplayTable reloadData];
714 - (void)_cancelPendingResolve
715 // If there a a Resolve outstanding, cancel it.
717 if ( fAddressResolver != nil) {
718 [fAddressResolver release];
719 fAddressResolver = nil;
722 if ( fServiceResolver != nil) {
723 [fServiceResolver release];
724 fServiceResolver = nil;
727 [self _clearResolvedInfo];
730 - (void)_clearResolvedInfo
731 // Erase the display of resolved info.
733 [hostField setStringValue:@""];
734 [ipAddressField setStringValue:@""];
735 [ip6AddressField setStringValue:@""];
736 [portField setStringValue:@""];
737 [textField setStringValue:@""];
740 @end // implementation BrowserController
743 @implementation ServiceController : NSObject
745 DNSServiceRef fServiceRef;
746 CFSocketRef fSocketRef;
747 CFRunLoopSourceRef fRunloopSrc;
750 - (id) initWithServiceRef:(DNSServiceRef) ref
757 - (boolean_t) addToCurrentRunLoop
758 /* Add the service to the current runloop. Returns non-zero on success. */
760 CFSocketContext ctx = { 1, (void*) fServiceRef, nil, nil, nil };
762 fSocketRef = CFSocketCreateWithNative( kCFAllocatorDefault, DNSServiceRefSockFD( fServiceRef),
763 kCFSocketReadCallBack, ProcessSockData, &ctx);
764 if ( fSocketRef != nil)
765 fRunloopSrc = CFSocketCreateRunLoopSource( kCFAllocatorDefault, fSocketRef, 1);
766 if ( fRunloopSrc != nil)
767 CFRunLoopAddSource( CFRunLoopGetCurrent(), fRunloopSrc, kCFRunLoopDefaultMode);
769 printf("Could not listen to runloop socket\n");
771 return fRunloopSrc != nil;
774 - (DNSServiceRef) serviceRef
780 /* Remove service from runloop, deallocate service and associated resources */
782 if ( fSocketRef != nil) {
783 CFSocketInvalidate( fSocketRef); // Note: Also closes the underlying socket
784 CFRelease( fSocketRef);
787 if ( fRunloopSrc != nil) {
788 CFRunLoopRemoveSource( CFRunLoopGetCurrent(), fRunloopSrc, kCFRunLoopDefaultMode);
789 CFRelease( fRunloopSrc);
792 DNSServiceRefDeallocate( fServiceRef);
797 @end // implementation ServiceController
799 int main(int argc, const char *argv[])
801 return NSApplicationMain(argc, argv);