2 * Copyright (c) 2003-2004 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: ExplorerBarWindow.cpp,v $
26 Revision 1.12 2004/10/26 00:56:03 cheshire
27 Use "#if 0" instead of commenting out code
29 Revision 1.11 2004/10/18 23:49:17 shersche
30 <rdar://problem/3841564> Remove trailing dot from hostname, because some flavors of Windows have difficulty parsing hostnames with a trailing dot.
33 Revision 1.10 2004/09/02 02:18:58 cheshire
34 Minor textual cleanup to improve readability
36 Revision 1.9 2004/09/02 02:11:56 cheshire
37 <rdar://problem/3783611> Fix incorrect testing of MoreComing flag
39 Revision 1.8 2004/07/26 05:47:31 shersche
40 use TXTRecord APIs, fix bug in locating service to be removed
42 Revision 1.7 2004/07/22 16:08:20 shersche
43 clean up debug print statements, re-enable code inadvertently commented out
45 Revision 1.6 2004/07/22 05:27:20 shersche
46 <rdar://problem/3735827> Check to make sure error isn't WSAEWOULDBLOCK when canceling browse
49 Revision 1.5 2004/07/20 06:49:18 shersche
50 clean up socket handling code
52 Revision 1.4 2004/07/13 21:24:21 rpantos
53 Fix for <rdar://problem/3701120>.
55 Revision 1.3 2004/06/27 14:59:59 shersche
56 reference count service info to handle multi-homed hosts
58 Revision 1.2 2004/06/23 16:09:34 shersche
59 Add the resolve DNSServiceRef to list of extant refs. This fixes the "doesn't resolve when double clicking" problem
61 Submitted by: Scott Herscher
63 Revision 1.1 2004/06/18 04:34:59 rpantos
64 Move to Clients from mDNSWindows
66 Revision 1.5 2004/04/15 01:00:05 bradley
67 Removed support for automatically querying for A/AAAA records when resolving names. Platforms
68 without .local name resolving support will need to manually query for A/AAAA records as needed.
70 Revision 1.4 2004/04/09 21:03:15 bradley
71 Changed port numbers to use network byte order for consistency with other platforms.
73 Revision 1.3 2004/04/08 09:43:43 bradley
74 Changed callback calling conventions to __stdcall so they can be used with C# delegates.
76 Revision 1.2 2004/02/21 04:36:19 bradley
77 Enable dot local name lookups now that the NSP is being installed.
79 Revision 1.1 2004/01/30 03:01:56 bradley
80 Explorer Plugin to browse for DNS-SD advertised Web and FTP servers from within Internet Explorer.
86 #include "CommonServices.h"
87 #include "DebugServices.h"
88 #include "WinServices.h"
91 #include "ExplorerBar.h"
92 #include "LoginDialog.h"
95 #include "ExplorerBarWindow.h"
100 #define new DEBUG_NEW
102 static char THIS_FILE
[] = __FILE__
;
106 #pragma mark == Constants ==
109 //===========================================================================================================================
111 //===========================================================================================================================
115 #define IDC_EXPLORER_TREE 1234
119 #define WM_PRIVATE_SERVICE_EVENT ( WM_USER + 0x100 )
123 #define kTXTRecordKeyPath "path"
126 #pragma mark == Prototypes ==
129 //===========================================================================================================================
131 //===========================================================================================================================
133 DEBUG_LOCAL
int FindServiceArrayIndex( const ServiceInfoArray
&inArray
, const ServiceInfo
&inService
, int &outIndex
);
136 #pragma mark == Message Map ==
139 //===========================================================================================================================
141 //===========================================================================================================================
143 BEGIN_MESSAGE_MAP( ExplorerBarWindow
, CWnd
)
147 ON_NOTIFY( NM_DBLCLK
, IDC_EXPLORER_TREE
, OnDoubleClick
)
148 ON_MESSAGE( WM_PRIVATE_SERVICE_EVENT
, OnServiceEvent
)
155 //===========================================================================================================================
157 //===========================================================================================================================
159 ExplorerBarWindow::ExplorerBarWindow( void )
162 mResolveServiceRef
= NULL
;
165 //===========================================================================================================================
166 // ~ExplorerBarWindow
167 //===========================================================================================================================
169 ExplorerBarWindow::~ExplorerBarWindow( void )
178 //===========================================================================================================================
180 //===========================================================================================================================
182 int ExplorerBarWindow::OnCreate( LPCREATESTRUCT inCreateStruct
)
184 AFX_MANAGE_STATE( AfxGetStaticModuleState() );
191 err
= CWnd::OnCreate( inCreateStruct
);
192 require_noerr( err
, exit
);
194 GetClientRect( rect
);
195 mTree
.Create( WS_TABSTOP
| WS_VISIBLE
| WS_CHILD
| TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_HASLINES
| TVS_NOHSCROLL
, rect
, this,
199 ServiceHandlerEntry
* e
;
203 e
= new ServiceHandlerEntry
;
205 e
->type
= "_http._tcp";
206 e
->urlScheme
= "http://";
211 e
->needsLogin
= false;
212 mServiceHandlers
.Add( e
);
214 s
.LoadString( IDS_WEB_SITES
);
215 e
->treeItem
= mTree
.InsertItem( s
, 0, 0 );
216 mTree
.Expand( e
->treeItem
, TVE_EXPAND
);
218 err
= DNSServiceBrowse( &e
->ref
, 0, 0, e
->type
, NULL
, BrowseCallBack
, e
);
219 require_noerr( err
, exit
);
221 err
= WSAAsyncSelect((SOCKET
) DNSServiceRefSockFD(e
->ref
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, FD_READ
|FD_CLOSE
);
222 require_noerr( err
, exit
);
224 m_serviceRefs
.push_back(e
->ref
);
228 e
= new ServiceHandlerEntry
;
230 e
->type
= "_ftp._tcp";
231 e
->urlScheme
= "ftp://";
236 e
->needsLogin
= true;
237 mServiceHandlers
.Add( e
);
239 s
.LoadString( IDS_FTP_SITES
);
240 e
->treeItem
= mTree
.InsertItem( s
, 0, 0 );
241 mTree
.Expand( e
->treeItem
, TVE_EXPAND
);
243 err
= DNSServiceBrowse( &e
->ref
, 0, 0, e
->type
, NULL
, BrowseCallBack
, e
);
244 require_noerr( err
, exit
);
246 err
= WSAAsyncSelect((SOCKET
) DNSServiceRefSockFD(e
->ref
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, FD_READ
|FD_CLOSE
);
247 require_noerr( err
, exit
);
249 m_serviceRefs
.push_back(e
->ref
);
251 m_imageList
.Create(16, 16, ILC_COLORDDB
, 1, 0);
252 bitmap
.LoadBitmap(IDB_LOGO
);
253 m_imageList
.Add(&bitmap
, (CBitmap
*) NULL
);
255 mTree
.SetImageList(&m_imageList
, TVSIL_NORMAL
);
259 // Cannot talk to the mDNSResponder service. Show the error message and exit (with kNoErr so they can see it).
262 s
.LoadString( IDS_MDNSRESPONDER_NOT_AVAILABLE
);
263 mTree
.DeleteAllItems();
264 mTree
.InsertItem( s
, 0, 0, TVI_ROOT
, TVI_LAST
);
271 //===========================================================================================================================
273 //===========================================================================================================================
275 void ExplorerBarWindow::OnDestroy( void )
277 // Stop any resolves that may still be pending (shouldn't be any).
281 // Clean up the extant browses
282 while (m_serviceRefs
.size() > 0)
285 // take the head of the list
287 DNSServiceRef ref
= m_serviceRefs
.front();
290 // Stop will remove it from the list
295 // Clean up the service handlers.
300 n
= (int) mServiceHandlers
.GetSize();
301 for( i
= 0; i
< n
; ++i
)
303 delete mServiceHandlers
[ i
];
309 //===========================================================================================================================
311 //===========================================================================================================================
313 void ExplorerBarWindow::OnSize( UINT inType
, int inX
, int inY
)
315 CWnd::OnSize( inType
, inX
, inY
);
316 mTree
.MoveWindow( 0, 0, inX
, inY
);
319 //===========================================================================================================================
321 //===========================================================================================================================
323 void ExplorerBarWindow::OnDoubleClick( NMHDR
*inNMHDR
, LRESULT
*outResult
)
326 ServiceInfo
* service
;
329 DEBUG_UNUSED( inNMHDR
);
331 item
= mTree
.GetSelectedItem();
332 require( item
, exit
);
334 service
= reinterpret_cast < ServiceInfo
* > ( mTree
.GetItemData( item
) );
335 require_quiet( service
, exit
);
337 err
= StartResolve( service
);
338 require_noerr( err
, exit
);
345 //===========================================================================================================================
347 //===========================================================================================================================
350 ExplorerBarWindow::OnServiceEvent(WPARAM inWParam
, LPARAM inLParam
)
352 if (WSAGETSELECTERROR(inLParam
) && !(HIWORD(inLParam
)))
354 dlog( kDebugLevelError
, "OnServiceEvent: window error\n" );
358 SOCKET sock
= (SOCKET
) inWParam
;
361 ServiceRefList::iterator it
;
363 for (it
= m_serviceRefs
.begin(); it
!= m_serviceRefs
.end(); it
++)
365 DNSServiceRef ref
= *it
;
369 if ((SOCKET
) DNSServiceRefSockFD(ref
) == sock
)
371 DNSServiceErrorType err
;
373 err
= DNSServiceProcessResult(ref
);
379 s
.LoadString( IDS_MDNSRESPONDER_NOT_AVAILABLE
);
380 mTree
.DeleteAllItems();
381 mTree
.InsertItem( s
, 0, 0, TVI_ROOT
, TVI_LAST
);
398 //===========================================================================================================================
400 //===========================================================================================================================
403 ExplorerBarWindow::BrowseCallBack(
405 DNSServiceFlags inFlags
,
406 uint32_t inInterfaceIndex
,
407 DNSServiceErrorType inErrorCode
,
410 const char * inDomain
,
413 ServiceHandlerEntry
* obj
;
414 ServiceInfo
* service
;
417 DEBUG_UNUSED( inRef
);
422 require_noerr( inErrorCode
, exit
);
423 obj
= reinterpret_cast < ServiceHandlerEntry
* > ( inContext
);
428 // set the UI to hold off on updates
430 obj
->obj
->mTree
.SetRedraw(FALSE
);
434 service
= new ServiceInfo
;
435 require_action( service
, exit
, err
= kNoMemoryErr
);
437 err
= UTF8StringToStringObject( inName
, service
->displayName
);
440 service
->name
= strdup( inName
);
441 require_action( service
->name
, exit
, err
= kNoMemoryErr
);
443 service
->type
= strdup( inType
);
444 require_action( service
->type
, exit
, err
= kNoMemoryErr
);
446 service
->domain
= strdup( inDomain
);
447 require_action( service
->domain
, exit
, err
= kNoMemoryErr
);
449 service
->ifi
= inInterfaceIndex
;
450 service
->handler
= obj
;
454 if (inFlags
& kDNSServiceFlagsAdd
) obj
->obj
->OnServiceAdd (service
);
455 else obj
->obj
->OnServiceRemove(service
);
461 dlog( kDebugLevelError
, "BrowseCallBack: exception thrown\n" );
466 // If no more coming, then update UI
468 if (obj
&& obj
->obj
&& ((inFlags
& kDNSServiceFlagsMoreComing
) == 0))
470 obj
->obj
->mTree
.SetRedraw(TRUE
);
471 obj
->obj
->mTree
.Invalidate();
480 //===========================================================================================================================
482 //===========================================================================================================================
484 LONG
ExplorerBarWindow::OnServiceAdd( ServiceInfo
* service
)
486 ServiceHandlerEntry
* handler
;
492 handler
= service
->handler
;
495 cmp
= FindServiceArrayIndex( handler
->array
, *service
, index
);
498 // Found a match so update the item. The index is index + 1 so subtract 1.
501 check( index
< handler
->array
.GetSize() );
503 handler
->array
[ index
]->refs
++;
511 // Insert the new item in sorted order.
513 afterItem
= ( index
> 0 ) ? handler
->array
[ index
- 1 ]->item
: TVI_FIRST
;
514 handler
->array
.InsertAt( index
, service
);
515 service
->item
= mTree
.InsertItem( service
->displayName
, handler
->treeItem
, afterItem
);
516 mTree
.SetItemData( service
->item
, (DWORD_PTR
) service
);
518 // Make sure the item is visible if this is the first time a service was added.
520 if( handler
->treeFirst
)
522 handler
->treeFirst
= false;
523 mTree
.EnsureVisible( service
->item
);
529 //===========================================================================================================================
531 //===========================================================================================================================
533 LONG
ExplorerBarWindow::OnServiceRemove( ServiceInfo
* service
)
535 ServiceHandlerEntry
* handler
;
541 handler
= service
->handler
;
544 // Search to see if we know about this service instance. If so, remove it from the list.
546 cmp
= FindServiceArrayIndex( handler
->array
, *service
, index
);
551 // Possibly found a match remove the item. The index
552 // is index + 1 so subtract 1.
554 check( index
< handler
->array
.GetSize() );
556 if ( --handler
->array
[ index
]->refs
== 0 )
558 mTree
.DeleteItem( handler
->array
[ index
]->item
);
559 delete handler
->array
[ index
];
560 handler
->array
.RemoveAt( index
);
572 //===========================================================================================================================
574 //===========================================================================================================================
576 OSStatus
ExplorerBarWindow::StartResolve( ServiceInfo
*inService
)
582 // Stop any current resolve that may be in progress.
586 // Resolve the service.
587 err
= DNSServiceResolve( &mResolveServiceRef
, 0, 0,
588 inService
->name
, inService
->type
, inService
->domain
, (DNSServiceResolveReply
) ResolveCallBack
, inService
->handler
);
589 require_noerr( err
, exit
);
591 err
= WSAAsyncSelect((SOCKET
) DNSServiceRefSockFD(mResolveServiceRef
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, FD_READ
|FD_CLOSE
);
592 require_noerr( err
, exit
);
594 m_serviceRefs
.push_back(mResolveServiceRef
);
600 //===========================================================================================================================
602 //===========================================================================================================================
604 void ExplorerBarWindow::StopResolve( void )
606 if( mResolveServiceRef
)
608 Stop( mResolveServiceRef
);
609 mResolveServiceRef
= NULL
;
613 //===========================================================================================================================
615 //===========================================================================================================================
618 ExplorerBarWindow::ResolveCallBack(
620 DNSServiceFlags inFlags
,
621 uint32_t inInterfaceIndex
,
622 DNSServiceErrorType inErrorCode
,
623 const char * inFullName
,
624 const char * inHostName
,
630 ExplorerBarWindow
* obj
;
631 ServiceHandlerEntry
* handler
;
634 DEBUG_UNUSED( inRef
);
635 DEBUG_UNUSED( inFlags
);
636 DEBUG_UNUSED( inErrorCode
);
637 DEBUG_UNUSED( inFullName
);
639 require_noerr( inErrorCode
, exit
);
640 handler
= (ServiceHandlerEntry
*) inContext
;
647 ResolveInfo
* resolve
;
650 dlog( kDebugLevelNotice
, "resolved %s on ifi %d to %s\n", inFullName
, inInterfaceIndex
, inHostName
);
652 // Stop resolving after the first good result.
656 // Post a message to the main thread so it can handle it since MFC is not thread safe.
658 resolve
= new ResolveInfo
;
659 require_action( resolve
, exit
, err
= kNoMemoryErr
);
661 UTF8StringToStringObject( inHostName
, resolve
->host
);
663 // rdar://problem/3841564
665 // strip trailing dot from hostname because some flavors of Windows
666 // have trouble parsing it.
668 idx
= resolve
->host
.ReverseFind('.');
670 if ((idx
> 1) && ((resolve
->host
.GetLength() - 1) == idx
))
672 resolve
->host
.Delete(idx
, 1);
675 resolve
->port
= ntohs( inPort
);
676 resolve
->ifi
= inInterfaceIndex
;
677 resolve
->handler
= handler
;
679 err
= resolve
->txt
.SetData( inTXT
, inTXTSize
);
682 obj
->OnResolve(resolve
);
686 dlog( kDebugLevelError
, "ResolveCallBack: exception thrown\n" );
693 //===========================================================================================================================
695 //===========================================================================================================================
697 LONG
ExplorerBarWindow::OnResolve( ResolveInfo
* resolve
)
709 // Get login info if needed.
711 if( resolve
->handler
->needsLogin
)
715 if( !dialog
.GetLogin( username
, password
) )
721 // If the HTTP TXT record is a "path=" entry, use it as the resource path. Otherwise, use "/".
724 if( strcmp( resolve
->handler
->type
, "_http._tcp" ) == 0 )
729 resolve
->txt
.GetData( &txtData
, &txtLen
);
731 path
= (uint8_t*) TXTRecordGetValuePtr(txtLen
, txtData
, kTXTRecordKeyPath
, &pathSize
);
735 path
= (uint8_t*) "";
741 path
= (uint8_t *) "";
745 // Build the URL in the following format:
747 // <urlScheme>[<username>[:<password>]@]<name/ip>[<path>]
749 url
.AppendFormat( TEXT( "%S" ), resolve
->handler
->urlScheme
); // URL Scheme
750 if( username
.GetLength() > 0 )
752 url
.AppendFormat( TEXT( "%s" ), username
); // Username
753 if( password
.GetLength() > 0 )
755 url
.AppendFormat( TEXT( ":%s" ), password
); // Password
757 url
.AppendFormat( TEXT( "@" ) );
760 url
+= resolve
->host
; // Host
761 url
.AppendFormat( TEXT( ":%d" ), resolve
->port
); // :Port
762 url
.AppendFormat( TEXT( "%S" ), pathPrefix
); // Path Prefix ("/" or empty).
763 url
.AppendFormat( TEXT( "%.*S" ), (int) pathSize
, (char *) path
); // Path (possibly empty).
765 // Tell Internet Explorer to go to the URL.
768 mOwner
->GoToURL( url
);
775 //===========================================================================================================================
777 //===========================================================================================================================
778 void ExplorerBarWindow::Stop( DNSServiceRef ref
)
780 m_serviceRefs
.remove( ref
);
782 WSAAsyncSelect(DNSServiceRefSockFD( ref
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, 0);
784 DNSServiceRefDeallocate( ref
);
792 //===========================================================================================================================
793 // FindServiceArrayIndex
794 //===========================================================================================================================
796 DEBUG_LOCAL
int FindServiceArrayIndex( const ServiceInfoArray
&inArray
, const ServiceInfo
&inService
, int &outIndex
)
806 hi
= (int)( inArray
.GetSize() - 1 );
809 mid
= ( lo
+ hi
) / 2;
810 result
= inService
.displayName
.CompareNoCase( inArray
[ mid
]->displayName
);
814 result
= ( (int) inService
.ifi
) - ( (int) inArray
[ mid
]->ifi
);
821 else if( result
< 0 )
832 mid
+= 1; // Bump index so new item is inserted after matching item.
834 else if( result
> 0 )