2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
25 $Log: DNSServiceBrowser.m,v $
26 Revision 1.30 2005/01/27 17:46:16 cheshire
29 Revision 1.29 2004/06/04 20:58:36 cheshire
30 Move DNSServiceBrowser from mDNSMacOSX directory to Clients directory
32 Revision 1.28 2004/05/18 23:51:26 cheshire
33 Tidy up all checkin comments to use consistent "<rdar://problem/xxxxxxx>" format for bug numbers
35 Revision 1.27 2003/11/19 18:49:48 rpantos
36 <rdar://problem/3282283>: couple of little tweaks to previous checkin
38 Revision 1.26 2003/11/07 19:35:20 rpantos
39 <rdar://problem/3282283>/6: Display multiple IP addresses. Connect using host rather than IP addr.
41 Revision 1.25 2003/10/29 05:16:54 rpantos
42 Checkpoint: transition from DNSServiceDiscovery.h to dns_sd.h
44 Revision 1.24 2003/10/28 02:25:45 rpantos
45 <rdar://problem/3282283>/9,10: Cancel pending resolve when focus changes or service disappears.
47 Revision 1.23 2003/10/28 01:29:15 rpantos
48 <rdar://problem/3282283>/4,5: Restructure a bit to make arrow keys work & views behave better.
50 Revision 1.22 2003/10/28 01:23:27 rpantos
51 <rdar://problem/3282283>/11: Bail if mDNS cannot be initialized at startup.
53 Revision 1.21 2003/10/28 01:19:45 rpantos
54 <rdar://problem/3282283>/3,11: Do not put a trailing '.' on service names. Handle PATH for HTTP txtRecords.
56 Revision 1.20 2003/10/28 01:13:49 rpantos
57 <rdar://problem/3282283>/2: Remove filter when displaying browse results.
59 Revision 1.19 2003/10/28 01:10:14 rpantos
60 <rdar://problem/3282283>/1: Change 'compare' to 'caseInsensitiveCompare' to fix sort order.
62 Revision 1.18 2003/08/12 19:55:07 cheshire
67 #include <arpa/inet.h>
70 #include <sys/select.h>
72 #import <Cocoa/Cocoa.h>
73 #import <DNSServiceDiscovery/DNSServiceDiscovery.h>
75 #include <netinet/in.h>
78 @class ServiceController; // holds state corresponding to outstanding DNSServiceRef
80 @interface BrowserController : NSObject
82 IBOutlet id domainField;
83 IBOutlet id nameField;
84 IBOutlet id typeField;
86 IBOutlet id serviceDisplayTable;
87 IBOutlet id typeColumn;
88 IBOutlet id nameColumn;
89 IBOutlet id serviceTypeField;
90 IBOutlet id serviceNameField;
92 IBOutlet id hostField;
93 IBOutlet id ipAddressField;
94 IBOutlet id ip6AddressField;
95 IBOutlet id portField;
96 IBOutlet id textField;
98 NSMutableArray *srvtypeKeys;
99 NSMutableArray *srvnameKeys;
100 NSMutableArray *domainKeys;
101 NSMutableArray *nameKeys;
107 ServiceController *fDomainBrowser;
108 ServiceController *fServiceBrowser;
109 ServiceController *fServiceResolver;
110 ServiceController *fAddressResolver;
114 - (IBAction)handleDomainClick:(id)sender;
115 - (IBAction)handleNameClick:(id)sender;
116 - (IBAction)handleTypeClick:(id)sender;
117 - (void)notifyTypeSelectionChange:(NSNotification*)note;
118 - (void)notifyNameSelectionChange:(NSNotification*)note;
120 - (IBAction)connect:(id)sender;
122 - (IBAction)handleTableClick:(id)sender;
123 - (IBAction)removeSelected:(id)sender;
124 - (IBAction)addNewService:(id)sender;
126 - (IBAction)update:(NSString *)Type Domain:(NSString *)Domain;
127 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication;
128 - (IBAction)loadDomains:(id)sender;
130 - (void)updateBrowseWithResult:(DNSServiceFlags)flags name:(NSString *)name type:(NSString *)resulttype domain:(NSString *)domain;
131 - (void)updateEnumWithResult:(DNSServiceFlags)flags domain:(NSString *)domain;
132 - (void)resolveClientWitHost:(NSString *)host port:(uint16_t)port interfaceIndex:(uint32_t)interface txtRecord:(const char*)txtRecord txtLen:(uint16_t)txtLen;
133 - (void)updateAddress:(uint16_t)rrtype addr:(const void *)buff addrLen:(uint16_t)addrLen
134 host:(const char*) host interfaceIndex:(uint32_t)interface more:(boolean_t)moreToCome;
136 - (void)_cancelPendingResolve;
137 - (void)_clearResolvedInfo;
141 // The ServiceController manages cleanup of DNSServiceRef & runloop info for an outstanding request
142 @interface ServiceController : NSObject
144 DNSServiceRef fServiceRef;
145 CFSocketRef fSocketRef;
146 CFRunLoopSourceRef fRunloopSrc;
149 - (id) initWithServiceRef:(DNSServiceRef) ref;
150 - (boolean_t) addToCurrentRunLoop;
151 - (DNSServiceRef) serviceRef;
154 @end // interface ServiceController
157 static void ProcessSockData( CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
158 // CFRunloop callback that notifies dns_sd when new data appears on a DNSServiceRef's socket.
160 DNSServiceRef serviceRef = (DNSServiceRef) info;
161 DNSServiceErrorType err = DNSServiceProcessResult( serviceRef);
162 if ( err != kDNSServiceErr_NoError)
163 printf( "DNSServiceProcessResult() returned an error! %d\n", err);
166 static void DomainEnumReply( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
167 DNSServiceErrorType errorCode, const char *replyDomain, void *context )
168 // Report newly-discovered domains to the BrowserController.
170 if ( errorCode == kDNSServiceErr_NoError) {
171 BrowserController *pSelf = (BrowserController*) context;
172 [pSelf updateEnumWithResult:flags domain:[NSString stringWithUTF8String:replyDomain]];
174 printf( "DomainEnumReply got an error! %d\n", errorCode);
178 static void ServiceBrowseReply( DNSServiceRef sdRef, DNSServiceFlags servFlags, uint32_t interfaceIndex, DNSServiceErrorType errorCode,
179 const char *serviceName, const char *regtype, const char *replyDomain, void *context )
180 // Report newly-discovered services to the BrowserController.
182 if ( errorCode == kDNSServiceErr_NoError) {
183 BrowserController *pSelf = (BrowserController*) context;
184 [pSelf updateBrowseWithResult:servFlags name:[NSString stringWithUTF8String:serviceName]
185 type:[NSString stringWithUTF8String:regtype] domain:[NSString stringWithUTF8String:replyDomain]];
187 printf( "ServiceBrowseReply got an error! %d\n", errorCode);
191 static void ServiceResolveReply( DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex,
192 DNSServiceErrorType errorCode, const char *fullname, const char *hosttarget, uint16_t port,
193 uint16_t txtLen, const char *txtRecord, void *context )
194 // Pass along resolved service info to the BrowserController.
196 if ( errorCode == kDNSServiceErr_NoError) {
197 BrowserController *pSelf = (BrowserController*) context;
198 [pSelf resolveClientWitHost:[NSString stringWithUTF8String:hosttarget] port:port interfaceIndex:interfaceIndex txtRecord:txtRecord txtLen:txtLen];
200 printf( "ServiceResolveReply got an error! %d\n", errorCode);
204 static void QueryRecordReply( DNSServiceRef DNSServiceRef, DNSServiceFlags flags, uint32_t interfaceIndex,
205 DNSServiceErrorType errorCode, const char *fullname, uint16_t rrtype, uint16_t rrclass,
206 uint16_t rdlen, const void *rdata, uint32_t ttl, void *context )
207 // DNSServiceQueryRecord callback used to look up IP addresses.
209 BrowserController *pBrowser = (BrowserController*) context;
211 [pBrowser updateAddress:rrtype addr:rdata addrLen:rdlen host:fullname interfaceIndex:interfaceIndex
212 more:((flags & kDNSServiceFlagsMoreComing) != 0)];
216 @implementation BrowserController //Begin implementation of BrowserController methods
218 - (void)registerDefaults
220 NSMutableDictionary *regDict = [NSMutableDictionary dictionary];
222 NSArray *typeArray = [NSArray arrayWithObjects:@"_ftp._tcp", @"_tftp._tcp",
223 @"_ssh._tcp", @"_telnet._tcp",
225 @"_printer._tcp", @"_ipp._tcp",
226 @"_ichat._tcp", @"_eppc._tcp",
227 @"_afpovertcp._tcp", @"_afpovertcp._tcp", @"_MacOSXDupSuppress._tcp", nil];
228 NSArray *nameArray = [NSArray arrayWithObjects:@"File Transfer (ftp)", @"Trivial File Transfer (tftp)",
229 @"Secure Shell (ssh)", @"Telnet",
230 @"Web Server (http)",
231 @"LPR Printer", @"IPP Printer",
232 @"iChat", @"Remote AppleEvents",
233 @"AppleShare Server", @"SMB File Server", @"Mystery Service", nil];
235 [regDict setObject:typeArray forKey:@"SrvTypeKeys"];
236 [regDict setObject:nameArray forKey:@"SrvNameKeys"];
238 [[NSUserDefaults standardUserDefaults] registerDefaults:regDict];
244 [self registerDefaults];
246 fDomainBrowser = nil;
247 fServiceBrowser = nil;
248 fServiceResolver = nil;
249 fAddressResolver = nil;
254 - (void)awakeFromNib //BrowserController startup procedure
258 srvtypeKeys = [NSMutableArray array]; //Define arrays for Type, Domain, and Name
259 srvnameKeys = [NSMutableArray array];
261 domainKeys = [NSMutableArray array];
264 nameKeys = [NSMutableArray array];
267 [srvtypeKeys retain]; //Keep arrays in memory until BrowserController closes
268 [srvnameKeys retain]; //Keep arrays in memory until BrowserController closes
269 [typeField sizeLastColumnToFit]; //Set column sizes to use their whole table's width.
270 [nameField sizeLastColumnToFit];
271 [domainField sizeLastColumnToFit];
272 // (self is specified as the NSTableViews' data source in the nib)
274 [nameField setDoubleAction:@selector(connect:)];
276 // Listen for table selection changes
277 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyTypeSelectionChange:)
278 name:NSTableViewSelectionDidChangeNotification object:typeField];
279 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifyNameSelectionChange:)
280 name:NSTableViewSelectionDidChangeNotification object:nameField];
282 //[srvtypeKeys addObject:@"_ftp._tcp"]; //Add supported protocols and domains to their
283 //[srvnameKeys addObject:@"File Transfer (ftp)"];
284 //[srvtypeKeys addObject:@"_printer._tcp"]; //respective arrays
285 //[srvnameKeys addObject:@"Printer (lpr)"];
286 //[srvtypeKeys addObject:@"_http._tcp"]; //respective arrays
287 //[srvnameKeys addObject:@"Web Server (http)"];
288 //[srvtypeKeys addObject:@"_afp._tcp"]; //respective arrays
289 //[srvnameKeys addObject:@"AppleShare Server (afp)"];
291 [self _clearResolvedInfo];
293 [srvtypeKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvTypeKeys"]];
294 [srvnameKeys addObjectsFromArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"SrvNameKeys"]];
297 [typeField reloadData]; //Reload (redraw) data in fields
298 [domainField reloadData];
300 [self loadDomains:self];
304 - (void)dealloc //Deallocation method
306 [srvtypeKeys release];
307 [srvnameKeys release];
309 [domainKeys release];
312 -(void)tableView:(NSTableView *)theTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
317 - (int)numberOfRowsInTableView:(NSTableView *)theTableView //Begin mandatory TableView methods
319 if (theTableView == typeField)
321 return [srvnameKeys count];
323 if (theTableView == domainField)
325 return [domainKeys count];
327 if (theTableView == nameField)
329 return [nameKeys count];
331 if (theTableView == serviceDisplayTable)
333 return [srvnameKeys count];
338 - (id)tableView:(NSTableView *)theTableView objectValueForTableColumn:(NSTableColumn *)theColumn row:(int)rowIndex
340 if (theTableView == typeField)
342 return [srvnameKeys objectAtIndex:rowIndex];
344 if (theTableView == domainField)
346 return [domainKeys objectAtIndex:rowIndex];
348 if (theTableView == nameField)
350 return [[nameKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:rowIndex];
352 if (theTableView == serviceDisplayTable)
354 if (theColumn == typeColumn) {
355 return [srvtypeKeys objectAtIndex:rowIndex];
357 if (theColumn == nameColumn) {
358 return [srvnameKeys objectAtIndex:rowIndex];
364 } //End of mandatory TableView methods
366 - (IBAction)handleTypeClick:(id)sender //Handle clicks for Type
368 // 3282283: No longer used - update happens in notifyTypeSelectionChange
372 - (IBAction)handleDomainClick:(id)sender //Handle clicks for Domain
374 int index=[sender selectedRow]; //Find index of selected row
375 if (index==-1) return; //Error checking
376 Domain = [domainKeys objectAtIndex:index]; //Save desired Domain
378 [self _cancelPendingResolve];
380 if (SrvType!=NULL) [self update:SrvType Domain:Domain]; //If Type and Domain are set, update records
383 - (IBAction)handleNameClick:(id)sender //Handle clicks for Name
385 // 3282283: No longer used - update happens in notifyNameSelectionChange
388 - (void)notifyTypeSelectionChange:(NSNotification*)note
389 /* Called when the selection of the Type table changes */
391 int index=[[note object] selectedRow]; //Find index of selected row
392 if (index==-1) return; //Error checking
393 SrvType = [srvtypeKeys objectAtIndex:index]; //Save desired Type
394 SrvName = [srvnameKeys objectAtIndex:index]; //Save desired Type
396 [self _cancelPendingResolve];
398 [self update:SrvType Domain:Domain]; //If Type and Domain are set, update records
401 - (void)notifyNameSelectionChange:(NSNotification*)note
402 /* Called when the selection of the Name table changes */
404 int index=[[note object] selectedRow]; //Find index of selected row
406 [self _cancelPendingResolve]; // Cancel any pending Resolve for any table selection change
409 Name = nil; // Name may no longer point to a list member
412 Name=[[nameKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] objectAtIndex:index]; //Save desired name
414 [self _clearResolvedInfo];
416 DNSServiceRef serviceRef;
417 DNSServiceErrorType err;
418 err = DNSServiceResolve ( &serviceRef, (DNSServiceFlags) 0, 0, (char *)[Name UTF8String], (char *)[SrvType UTF8String],
419 (char *)(Domain?[Domain UTF8String]:""), ServiceResolveReply, self);
420 if ( kDNSServiceErr_NoError == err) {
421 fServiceResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
422 [fServiceResolver addToCurrentRunLoop];
426 - (IBAction)loadDomains:(id)sender
428 DNSServiceErrorType err;
429 DNSServiceRef serviceRef;
431 err = DNSServiceEnumerateDomains( &serviceRef, kDNSServiceFlagsBrowseDomains, 0, DomainEnumReply, self);
432 if ( kDNSServiceErr_NoError == err) {
433 fDomainBrowser = [[ServiceController alloc] initWithServiceRef:serviceRef];
434 [fDomainBrowser addToCurrentRunLoop];
437 NSAlert *alert = [NSAlert alertWithMessageText:@"Could not connect to mDNSResponder!"
438 defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:
439 @"Check to see if mDNSResponder is still running."];
446 - (IBAction)update:theType Domain:theDomain; //The Big Kahuna: Fetch PTR records and update application
448 const char * DomainC;
449 const char * TypeC=[theType UTF8String]; //Type in C string format
451 DNSServiceErrorType err = kDNSServiceErr_NoError;
454 DomainC = [theDomain UTF8String]; //Domain in C string format
459 [nameKeys removeAllObjects]; //Get rid of displayed records if we're going to go get new ones
460 [nameField reloadData]; //Reload (redraw) names to show the old data is gone
462 // get rid of the previous browser if one exists
463 if ( fServiceBrowser != nil) {
464 [fServiceBrowser release];
465 fServiceBrowser = nil;
468 // now create a browser to return the values for the nameField ...
469 DNSServiceRef serviceRef;
470 err = DNSServiceBrowse( &serviceRef, (DNSServiceFlags) 0, 0, TypeC, DomainC, ServiceBrowseReply, self);
471 if ( kDNSServiceErr_NoError == err) {
472 fServiceBrowser = [[ServiceController alloc] initWithServiceRef:serviceRef];
473 [fServiceBrowser addToCurrentRunLoop];
478 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication //Quit when main window is closed
483 - (BOOL)windowShouldClose:(NSWindow *)sender //Save domains to our domain file when quitting
485 [domainField reloadData];
489 - (void)updateEnumWithResult:(DNSServiceFlags)flags domain:(NSString *)domain
491 if ( ( flags & kDNSServiceFlagsAdd) != 0) { // new domain received
492 // add the domain to the list
493 [domainKeys addObject:domain];
494 } else if (!(flags & kDNSServiceFlagsAdd)) {
495 // remove the domain from the list
496 NSEnumerator *dmnEnum = [domainKeys objectEnumerator];
497 NSString *aDomain = nil;
499 while (aDomain = [dmnEnum nextObject]) {
500 if ([aDomain isEqualToString:domain]) {
501 [domainKeys removeObject:domain];
506 // update the domain table
507 [domainField reloadData];
509 // Terminate the enumeration once the last domain is delivered.
510 if ( ( flags & kDNSServiceFlagsMoreComing) == 0) {
511 [fDomainBrowser release];
512 fDomainBrowser = nil;
515 // At some point, we may want to support a TableView for domain browsing. For now, just pick first domain that comes up.
517 Domain = [domain retain];
524 - (void)updateBrowseWithResult:(DNSServiceFlags)flags name:(NSString *)name type:(NSString *)resulttype domain:(NSString *)domain
527 //NSLog(@"Received result %@ %@ %@ %d", name, resulttype, domain, type);
529 if (!(flags & kDNSServiceFlagsAdd)) {
530 if ([nameKeys containsObject:name]) {
531 [nameKeys removeObject:name];
533 // 3282283: Cancel pending browse if object goes away.
534 if ( [name isEqualToString:Name])
535 [nameField deselectAll:self];
538 else if ( ( flags & kDNSServiceFlagsAdd) != 0) {
539 if (![nameKeys containsObject:name]) {
540 [nameKeys addObject:name];
544 // If not expecting any more data, then reload (redraw) Name TableView with newly found data
545 if ((flags & kDNSServiceFlagsMoreComing) == 0)
546 [nameField reloadData];
550 - (void)resolveClientWitHost:(NSString *)host port:(uint16_t)port interfaceIndex:(uint32_t)interface
551 txtRecord:(const char*)txtRecord txtLen:(uint16_t)txtLen
552 /* Display resolved information about the selected service. */
554 DNSServiceErrorType err;
555 DNSServiceRef serviceRef;
557 // Start an async lookup for IPv4 & IPv6 addresses
558 if ( fAddressResolver != nil) {
559 [fAddressResolver release];
560 fAddressResolver = nil;
562 err = DNSServiceQueryRecord( &serviceRef, (DNSServiceFlags) 0, interface, [host UTF8String],
563 ns_t_a, ns_c_in, QueryRecordReply, self);
564 if ( err == kDNSServiceErr_NoError) {
565 fAddressResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
566 [fAddressResolver addToCurrentRunLoop];
569 [hostField setStringValue:host];
570 [portField setIntValue:port];
572 // kind of a hack: munge txtRecord so it's human-readable
574 char *readableText = (char*) malloc( txtLen);
575 if ( readableText != nil) {
576 ByteCount index, subStrLen;
577 memcpy( readableText, txtRecord, txtLen);
578 for ( index=0; index < txtLen - 1; index += subStrLen + 1) {
579 subStrLen = readableText[ index];
580 readableText[ index] = '\n';
582 [textField setStringValue:[NSString stringWithCString:&readableText[1] length:txtLen - 1]];
588 - (void)updateAddress:(uint16_t)rrtype addr:(const void *)buff addrLen:(uint16_t)addrLen
589 host:(const char*) host interfaceIndex:(uint32_t)interface more:(boolean_t)moreToCome
590 /* Update address field(s) with info obtained by fAddressResolver. */
592 if ( rrtype == ns_t_a) { // IPv4
594 inet_ntop( AF_INET, buff, addrBuff, sizeof addrBuff);
595 strcat( addrBuff, " ");
596 [ipAddressField setStringValue:[NSString stringWithFormat:@"%@%s", [ipAddressField stringValue], addrBuff]];
599 [fAddressResolver release];
600 fAddressResolver = nil;
602 // After we find v4 we look for v6
603 DNSServiceRef serviceRef;
604 DNSServiceErrorType err;
605 err = DNSServiceQueryRecord( &serviceRef, (DNSServiceFlags) 0, interface, host,
606 ns_t_aaaa, ns_c_in, QueryRecordReply, self);
607 if ( err == kDNSServiceErr_NoError) {
608 fAddressResolver = [[ServiceController alloc] initWithServiceRef:serviceRef];
609 [fAddressResolver addToCurrentRunLoop];
613 else if ( rrtype == ns_t_aaaa) // IPv6
616 inet_ntop( AF_INET6, buff, addrBuff, sizeof addrBuff);
617 strcat( addrBuff, " ");
618 [ip6AddressField setStringValue:[NSString stringWithFormat:@"%@%s", [ip6AddressField stringValue], addrBuff]];
621 [fAddressResolver release];
622 fAddressResolver = nil;
628 - (void)connect:(id)sender
630 NSString *host = [hostField stringValue];
631 int port = [portField intValue];
632 NSString *txtRecord = [textField stringValue];
634 if (!txtRecord) txtRecord = @"";
636 if (!host || !port) return;
638 if ([SrvType isEqualToString:@"_http._tcp"])
640 NSString *pathDelim = @"path=";
643 // If the TXT record specifies a path, extract it.
644 where = [txtRecord rangeOfString:pathDelim options:NSCaseInsensitiveSearch];
647 NSRange targetRange = { where.location + where.length, [txtRecord length] - where.location - where.length };
648 NSRange endDelim = [txtRecord rangeOfString:@"\n" options:kNilOptions range:targetRange];
650 if ( endDelim.length) // if a delimiter was found, truncate the target range
651 targetRange.length = endDelim.location - targetRange.location;
653 NSString *path = [txtRecord substringWithRange:targetRange];
654 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%d%@", host, port, path]]];
657 [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%d", host, port]]];
659 else if ([SrvType isEqualToString:@"_ftp._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"ftp://%@:%d/", host, port]]];
660 else if ([SrvType isEqualToString:@"_tftp._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tftp://%@:%d/", host, port]]];
661 else if ([SrvType isEqualToString:@"_ssh._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"ssh://%@:%d/", host, port]]];
662 else if ([SrvType isEqualToString:@"_telnet._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telnet://%@:%d/", host, port]]];
663 else if ([SrvType isEqualToString:@"_printer._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"lpr://%@:%d/", host, port]]];
664 else if ([SrvType isEqualToString:@"_ipp._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"ipp://%@:%d/", host, port]]];
665 else if ([SrvType isEqualToString:@"_afpovertcp._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"afp://%@:%d/", host, port]]];
666 else if ([SrvType isEqualToString:@"_smb._tcp"]) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"smb://%@:%d/", host, port]]];
671 - (IBAction)handleTableClick:(id)sender
673 //populate the text fields
676 - (IBAction)removeSelected:(id)sender
678 // remove the selected row and force a refresh
680 int selectedRow = [serviceDisplayTable selectedRow];
684 [srvtypeKeys removeObjectAtIndex:selectedRow];
685 [srvnameKeys removeObjectAtIndex:selectedRow];
687 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
688 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
690 [typeField reloadData];
691 [serviceDisplayTable reloadData];
695 - (IBAction)addNewService:(id)sender
697 // add new entries from the edit fields to the arrays for the defaults
698 NSString *newType = [serviceTypeField stringValue];
699 NSString *newName = [serviceNameField stringValue];
701 // 3282283: trim trailing '.' from service type field
702 if ([newType length] && [newType hasSuffix:@"."])
703 newType = [newType substringToIndex:[newType length] - 1];
705 if ([newType length] && [newName length]) {
706 [srvtypeKeys addObject:newType];
707 [srvnameKeys addObject:newName];
709 [[NSUserDefaults standardUserDefaults] setObject:srvtypeKeys forKey:@"SrvTypeKeys"];
710 [[NSUserDefaults standardUserDefaults] setObject:srvnameKeys forKey:@"SrvNameKeys"];
712 [typeField reloadData];
713 [serviceDisplayTable reloadData];
717 - (void)_cancelPendingResolve
718 // If there a a Resolve outstanding, cancel it.
720 if ( fAddressResolver != nil) {
721 [fAddressResolver release];
722 fAddressResolver = nil;
725 if ( fServiceResolver != nil) {
726 [fServiceResolver release];
727 fServiceResolver = nil;
730 [self _clearResolvedInfo];
733 - (void)_clearResolvedInfo
734 // Erase the display of resolved info.
736 [hostField setStringValue:@""];
737 [ipAddressField setStringValue:@""];
738 [ip6AddressField setStringValue:@""];
739 [portField setStringValue:@""];
740 [textField setStringValue:@""];
743 @end // implementation BrowserController
746 @implementation ServiceController : NSObject
748 DNSServiceRef fServiceRef;
749 CFSocketRef fSocketRef;
750 CFRunLoopSourceRef fRunloopSrc;
753 - (id) initWithServiceRef:(DNSServiceRef) ref
760 - (boolean_t) addToCurrentRunLoop
761 /* Add the service to the current runloop. Returns non-zero on success. */
763 CFSocketContext ctx = { 1, (void*) fServiceRef, nil, nil, nil };
765 fSocketRef = CFSocketCreateWithNative( kCFAllocatorDefault, DNSServiceRefSockFD( fServiceRef),
766 kCFSocketReadCallBack, ProcessSockData, &ctx);
767 if ( fSocketRef != nil)
768 fRunloopSrc = CFSocketCreateRunLoopSource( kCFAllocatorDefault, fSocketRef, 1);
769 if ( fRunloopSrc != nil)
770 CFRunLoopAddSource( CFRunLoopGetCurrent(), fRunloopSrc, kCFRunLoopDefaultMode);
772 printf("Could not listen to runloop socket\n");
774 return fRunloopSrc != nil;
777 - (DNSServiceRef) serviceRef
783 /* Remove service from runloop, deallocate service and associated resources */
785 if ( fSocketRef != nil) {
786 CFSocketInvalidate( fSocketRef); // Note: Also closes the underlying socket
787 CFRelease( fSocketRef);
790 if ( fRunloopSrc != nil) {
791 CFRunLoopRemoveSource( CFRunLoopGetCurrent(), fRunloopSrc, kCFRunLoopDefaultMode);
792 CFRelease( fRunloopSrc);
795 DNSServiceRefDeallocate( fServiceRef);
800 @end // implementation ServiceController
802 int main(int argc, const char *argv[])
804 return NSApplicationMain(argc, argv);