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.18 2005/02/26 01:24:05 shersche
27 Remove display lines in tree control
29 Revision 1.17 2005/02/25 19:57:30 shersche
30 <rdar://problem/4023323> Remove FTP browsing from plugin
32 Revision 1.16 2005/02/08 23:31:06 shersche
33 Move "About ..." item underneath WebSites, change icons for discovered sites and "About ..." item
35 Revision 1.15 2005/01/27 22:38:27 shersche
36 add About item to tree list
38 Revision 1.14 2005/01/25 17:55:39 shersche
39 <rdar://problem/3911084> Get bitmaps from non-localizable resource module
42 Revision 1.13 2005/01/06 21:13:09 shersche
43 <rdar://problem/3796779> Handle kDNSServiceErr_Firewall return codes
46 Revision 1.12 2004/10/26 00:56:03 cheshire
47 Use "#if 0" instead of commenting out code
49 Revision 1.11 2004/10/18 23:49:17 shersche
50 <rdar://problem/3841564> Remove trailing dot from hostname, because some flavors of Windows have difficulty parsing hostnames with a trailing dot.
53 Revision 1.10 2004/09/02 02:18:58 cheshire
54 Minor textual cleanup to improve readability
56 Revision 1.9 2004/09/02 02:11:56 cheshire
57 <rdar://problem/3783611> Fix incorrect testing of MoreComing flag
59 Revision 1.8 2004/07/26 05:47:31 shersche
60 use TXTRecord APIs, fix bug in locating service to be removed
62 Revision 1.7 2004/07/22 16:08:20 shersche
63 clean up debug print statements, re-enable code inadvertently commented out
65 Revision 1.6 2004/07/22 05:27:20 shersche
66 <rdar://problem/3735827> Check to make sure error isn't WSAEWOULDBLOCK when canceling browse
69 Revision 1.5 2004/07/20 06:49:18 shersche
70 clean up socket handling code
72 Revision 1.4 2004/07/13 21:24:21 rpantos
73 Fix for <rdar://problem/3701120>.
75 Revision 1.3 2004/06/27 14:59:59 shersche
76 reference count service info to handle multi-homed hosts
78 Revision 1.2 2004/06/23 16:09:34 shersche
79 Add the resolve DNSServiceRef to list of extant refs. This fixes the "doesn't resolve when double clicking" problem
81 Submitted by: Scott Herscher
83 Revision 1.1 2004/06/18 04:34:59 rpantos
84 Move to Clients from mDNSWindows
86 Revision 1.5 2004/04/15 01:00:05 bradley
87 Removed support for automatically querying for A/AAAA records when resolving names. Platforms
88 without .local name resolving support will need to manually query for A/AAAA records as needed.
90 Revision 1.4 2004/04/09 21:03:15 bradley
91 Changed port numbers to use network byte order for consistency with other platforms.
93 Revision 1.3 2004/04/08 09:43:43 bradley
94 Changed callback calling conventions to __stdcall so they can be used with C# delegates.
96 Revision 1.2 2004/02/21 04:36:19 bradley
97 Enable dot local name lookups now that the NSP is being installed.
99 Revision 1.1 2004/01/30 03:01:56 bradley
100 Explorer Plugin to browse for DNS-SD advertised Web and FTP servers from within Internet Explorer.
106 #include "CommonServices.h"
107 #include "DebugServices.h"
108 #include "WinServices.h"
111 #include "ExplorerBar.h"
112 #include "LoginDialog.h"
113 #include "Resource.h"
115 #include "ExplorerBarWindow.h"
116 #include "ExplorerPlugin.h"
121 #define new DEBUG_NEW
123 static char THIS_FILE
[] = __FILE__
;
127 #pragma mark == Constants ==
130 //===========================================================================================================================
132 //===========================================================================================================================
136 #define IDC_EXPLORER_TREE 1234
140 #define WM_PRIVATE_SERVICE_EVENT ( WM_USER + 0x100 )
144 #define kTXTRecordKeyPath "path"
148 #pragma mark == Prototypes ==
151 //===========================================================================================================================
153 //===========================================================================================================================
155 DEBUG_LOCAL
int FindServiceArrayIndex( const ServiceInfoArray
&inArray
, const ServiceInfo
&inService
, int &outIndex
);
158 #pragma mark == Message Map ==
161 //===========================================================================================================================
163 //===========================================================================================================================
165 BEGIN_MESSAGE_MAP( ExplorerBarWindow
, CWnd
)
169 ON_NOTIFY( NM_DBLCLK
, IDC_EXPLORER_TREE
, OnDoubleClick
)
170 ON_MESSAGE( WM_PRIVATE_SERVICE_EVENT
, OnServiceEvent
)
177 //===========================================================================================================================
179 //===========================================================================================================================
181 ExplorerBarWindow::ExplorerBarWindow( void )
184 mResolveServiceRef
= NULL
;
187 //===========================================================================================================================
188 // ~ExplorerBarWindow
189 //===========================================================================================================================
191 ExplorerBarWindow::~ExplorerBarWindow( void )
200 //===========================================================================================================================
202 //===========================================================================================================================
204 int ExplorerBarWindow::OnCreate( LPCREATESTRUCT inCreateStruct
)
206 AFX_MANAGE_STATE( AfxGetStaticModuleState() );
213 err
= CWnd::OnCreate( inCreateStruct
);
214 require_noerr( err
, exit
);
216 GetClientRect( rect
);
217 mTree
.Create( WS_TABSTOP
| WS_VISIBLE
| WS_CHILD
| TVS_HASBUTTONS
| TVS_LINESATROOT
| TVS_NOHSCROLL
, rect
, this,
220 ServiceHandlerEntry
* e
;
224 e
= new ServiceHandlerEntry
;
226 e
->type
= "_http._tcp";
227 e
->urlScheme
= "http://";
230 e
->needsLogin
= false;
231 mServiceHandlers
.Add( e
);
233 s
.LoadString( IDS_ABOUT
);
234 m_about
= mTree
.InsertItem( s
, 1, 1 );
236 err
= DNSServiceBrowse( &e
->ref
, 0, 0, e
->type
, NULL
, BrowseCallBack
, e
);
237 require_noerr( err
, exit
);
239 err
= WSAAsyncSelect((SOCKET
) DNSServiceRefSockFD(e
->ref
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, FD_READ
|FD_CLOSE
);
240 require_noerr( err
, exit
);
242 m_serviceRefs
.push_back(e
->ref
);
244 m_imageList
.Create( 16, 16, ILC_COLORDDB
, 2, 0);
245 bitmap
.Attach( ::LoadBitmap( GetNonLocalizedResources(), MAKEINTRESOURCE( IDB_GLOBE
) ) );
246 m_imageList
.Add( &bitmap
, (CBitmap
*) NULL
);
248 bitmap
.Attach( ::LoadBitmap( GetNonLocalizedResources(), MAKEINTRESOURCE( IDB_LOGO
) ) );
249 m_imageList
.Add( &bitmap
, (CBitmap
*) NULL
);
251 mTree
.SetImageList(&m_imageList
, TVSIL_NORMAL
);
255 // Cannot talk to the mDNSResponder service. Show the error message and exit (with kNoErr so they can see it).
258 if ( err
== kDNSServiceErr_Firewall
)
260 s
.LoadString( IDS_FIREWALL
);
264 s
.LoadString( IDS_MDNSRESPONDER_NOT_AVAILABLE
);
267 mTree
.DeleteAllItems();
268 mTree
.InsertItem( s
, 0, 0, TVI_ROOT
, TVI_LAST
);
276 //===========================================================================================================================
278 //===========================================================================================================================
280 void ExplorerBarWindow::OnDestroy( void )
282 // Stop any resolves that may still be pending (shouldn't be any).
286 // Clean up the extant browses
287 while (m_serviceRefs
.size() > 0)
290 // take the head of the list
292 DNSServiceRef ref
= m_serviceRefs
.front();
295 // Stop will remove it from the list
300 // Clean up the service handlers.
305 n
= (int) mServiceHandlers
.GetSize();
306 for( i
= 0; i
< n
; ++i
)
308 delete mServiceHandlers
[ i
];
314 //===========================================================================================================================
316 //===========================================================================================================================
318 void ExplorerBarWindow::OnSize( UINT inType
, int inX
, int inY
)
320 CWnd::OnSize( inType
, inX
, inY
);
321 mTree
.MoveWindow( 0, 0, inX
, inY
);
324 //===========================================================================================================================
326 //===========================================================================================================================
328 void ExplorerBarWindow::OnDoubleClick( NMHDR
*inNMHDR
, LRESULT
*outResult
)
331 ServiceInfo
* service
;
334 DEBUG_UNUSED( inNMHDR
);
336 item
= mTree
.GetSelectedItem();
337 require( item
, exit
);
339 // Tell Internet Explorer to go to the URL if it's about item
341 if ( item
== m_about
)
347 url
.LoadString( IDS_ABOUT_URL
);
348 mOwner
->GoToURL( url
);
352 service
= reinterpret_cast < ServiceInfo
* > ( mTree
.GetItemData( item
) );
353 require_quiet( service
, exit
);
355 err
= StartResolve( service
);
356 require_noerr( err
, exit
);
364 //===========================================================================================================================
366 //===========================================================================================================================
369 ExplorerBarWindow::OnServiceEvent(WPARAM inWParam
, LPARAM inLParam
)
371 if (WSAGETSELECTERROR(inLParam
) && !(HIWORD(inLParam
)))
373 dlog( kDebugLevelError
, "OnServiceEvent: window error\n" );
377 SOCKET sock
= (SOCKET
) inWParam
;
380 ServiceRefList::iterator it
;
382 for (it
= m_serviceRefs
.begin(); it
!= m_serviceRefs
.end(); it
++)
384 DNSServiceRef ref
= *it
;
388 if ((SOCKET
) DNSServiceRefSockFD(ref
) == sock
)
390 DNSServiceErrorType err
;
392 err
= DNSServiceProcessResult(ref
);
398 s
.LoadString( IDS_MDNSRESPONDER_NOT_AVAILABLE
);
399 mTree
.DeleteAllItems();
400 mTree
.InsertItem( s
, 0, 0, TVI_ROOT
, TVI_LAST
);
417 //===========================================================================================================================
419 //===========================================================================================================================
422 ExplorerBarWindow::BrowseCallBack(
424 DNSServiceFlags inFlags
,
425 uint32_t inInterfaceIndex
,
426 DNSServiceErrorType inErrorCode
,
429 const char * inDomain
,
432 ServiceHandlerEntry
* obj
;
433 ServiceInfo
* service
;
436 DEBUG_UNUSED( inRef
);
441 require_noerr( inErrorCode
, exit
);
442 obj
= reinterpret_cast < ServiceHandlerEntry
* > ( inContext
);
447 // set the UI to hold off on updates
449 obj
->obj
->mTree
.SetRedraw(FALSE
);
453 service
= new ServiceInfo
;
454 require_action( service
, exit
, err
= kNoMemoryErr
);
456 err
= UTF8StringToStringObject( inName
, service
->displayName
);
459 service
->name
= strdup( inName
);
460 require_action( service
->name
, exit
, err
= kNoMemoryErr
);
462 service
->type
= strdup( inType
);
463 require_action( service
->type
, exit
, err
= kNoMemoryErr
);
465 service
->domain
= strdup( inDomain
);
466 require_action( service
->domain
, exit
, err
= kNoMemoryErr
);
468 service
->ifi
= inInterfaceIndex
;
469 service
->handler
= obj
;
473 if (inFlags
& kDNSServiceFlagsAdd
) obj
->obj
->OnServiceAdd (service
);
474 else obj
->obj
->OnServiceRemove(service
);
480 dlog( kDebugLevelError
, "BrowseCallBack: exception thrown\n" );
485 // If no more coming, then update UI
487 if (obj
&& obj
->obj
&& ((inFlags
& kDNSServiceFlagsMoreComing
) == 0))
489 obj
->obj
->mTree
.SetRedraw(TRUE
);
490 obj
->obj
->mTree
.Invalidate();
499 //===========================================================================================================================
501 //===========================================================================================================================
503 LONG
ExplorerBarWindow::OnServiceAdd( ServiceInfo
* service
)
505 ServiceHandlerEntry
* handler
;
511 handler
= service
->handler
;
514 cmp
= FindServiceArrayIndex( handler
->array
, *service
, index
);
517 // Found a match so update the item. The index is index + 1 so subtract 1.
520 check( index
< handler
->array
.GetSize() );
522 handler
->array
[ index
]->refs
++;
530 // Insert the new item in sorted order.
532 afterItem
= ( index
> 0 ) ? handler
->array
[ index
- 1 ]->item
: m_about
;
533 handler
->array
.InsertAt( index
, service
);
534 service
->item
= mTree
.InsertItem( service
->displayName
, 0, 0, NULL
, afterItem
);
535 mTree
.SetItemData( service
->item
, (DWORD_PTR
) service
);
540 //===========================================================================================================================
542 //===========================================================================================================================
544 LONG
ExplorerBarWindow::OnServiceRemove( ServiceInfo
* service
)
546 ServiceHandlerEntry
* handler
;
552 handler
= service
->handler
;
555 // Search to see if we know about this service instance. If so, remove it from the list.
557 cmp
= FindServiceArrayIndex( handler
->array
, *service
, index
);
562 // Possibly found a match remove the item. The index
563 // is index + 1 so subtract 1.
565 check( index
< handler
->array
.GetSize() );
567 if ( --handler
->array
[ index
]->refs
== 0 )
569 mTree
.DeleteItem( handler
->array
[ index
]->item
);
570 delete handler
->array
[ index
];
571 handler
->array
.RemoveAt( index
);
583 //===========================================================================================================================
585 //===========================================================================================================================
587 OSStatus
ExplorerBarWindow::StartResolve( ServiceInfo
*inService
)
593 // Stop any current resolve that may be in progress.
597 // Resolve the service.
598 err
= DNSServiceResolve( &mResolveServiceRef
, 0, 0,
599 inService
->name
, inService
->type
, inService
->domain
, (DNSServiceResolveReply
) ResolveCallBack
, inService
->handler
);
600 require_noerr( err
, exit
);
602 err
= WSAAsyncSelect((SOCKET
) DNSServiceRefSockFD(mResolveServiceRef
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, FD_READ
|FD_CLOSE
);
603 require_noerr( err
, exit
);
605 m_serviceRefs
.push_back(mResolveServiceRef
);
611 //===========================================================================================================================
613 //===========================================================================================================================
615 void ExplorerBarWindow::StopResolve( void )
617 if( mResolveServiceRef
)
619 Stop( mResolveServiceRef
);
620 mResolveServiceRef
= NULL
;
624 //===========================================================================================================================
626 //===========================================================================================================================
629 ExplorerBarWindow::ResolveCallBack(
631 DNSServiceFlags inFlags
,
632 uint32_t inInterfaceIndex
,
633 DNSServiceErrorType inErrorCode
,
634 const char * inFullName
,
635 const char * inHostName
,
641 ExplorerBarWindow
* obj
;
642 ServiceHandlerEntry
* handler
;
645 DEBUG_UNUSED( inRef
);
646 DEBUG_UNUSED( inFlags
);
647 DEBUG_UNUSED( inErrorCode
);
648 DEBUG_UNUSED( inFullName
);
650 require_noerr( inErrorCode
, exit
);
651 handler
= (ServiceHandlerEntry
*) inContext
;
658 ResolveInfo
* resolve
;
661 dlog( kDebugLevelNotice
, "resolved %s on ifi %d to %s\n", inFullName
, inInterfaceIndex
, inHostName
);
663 // Stop resolving after the first good result.
667 // Post a message to the main thread so it can handle it since MFC is not thread safe.
669 resolve
= new ResolveInfo
;
670 require_action( resolve
, exit
, err
= kNoMemoryErr
);
672 UTF8StringToStringObject( inHostName
, resolve
->host
);
674 // rdar://problem/3841564
676 // strip trailing dot from hostname because some flavors of Windows
677 // have trouble parsing it.
679 idx
= resolve
->host
.ReverseFind('.');
681 if ((idx
> 1) && ((resolve
->host
.GetLength() - 1) == idx
))
683 resolve
->host
.Delete(idx
, 1);
686 resolve
->port
= ntohs( inPort
);
687 resolve
->ifi
= inInterfaceIndex
;
688 resolve
->handler
= handler
;
690 err
= resolve
->txt
.SetData( inTXT
, inTXTSize
);
693 obj
->OnResolve(resolve
);
697 dlog( kDebugLevelError
, "ResolveCallBack: exception thrown\n" );
704 //===========================================================================================================================
706 //===========================================================================================================================
708 LONG
ExplorerBarWindow::OnResolve( ResolveInfo
* resolve
)
720 // Get login info if needed.
722 if( resolve
->handler
->needsLogin
)
726 if( !dialog
.GetLogin( username
, password
) )
732 // If the HTTP TXT record is a "path=" entry, use it as the resource path. Otherwise, use "/".
735 if( strcmp( resolve
->handler
->type
, "_http._tcp" ) == 0 )
740 resolve
->txt
.GetData( &txtData
, &txtLen
);
742 path
= (uint8_t*) TXTRecordGetValuePtr(txtLen
, txtData
, kTXTRecordKeyPath
, &pathSize
);
746 path
= (uint8_t*) "";
752 path
= (uint8_t *) "";
756 // Build the URL in the following format:
758 // <urlScheme>[<username>[:<password>]@]<name/ip>[<path>]
760 url
.AppendFormat( TEXT( "%S" ), resolve
->handler
->urlScheme
); // URL Scheme
761 if( username
.GetLength() > 0 )
763 url
.AppendFormat( TEXT( "%s" ), username
); // Username
764 if( password
.GetLength() > 0 )
766 url
.AppendFormat( TEXT( ":%s" ), password
); // Password
768 url
.AppendFormat( TEXT( "@" ) );
771 url
+= resolve
->host
; // Host
772 url
.AppendFormat( TEXT( ":%d" ), resolve
->port
); // :Port
773 url
.AppendFormat( TEXT( "%S" ), pathPrefix
); // Path Prefix ("/" or empty).
774 url
.AppendFormat( TEXT( "%.*S" ), (int) pathSize
, (char *) path
); // Path (possibly empty).
776 // Tell Internet Explorer to go to the URL.
779 mOwner
->GoToURL( url
);
786 //===========================================================================================================================
788 //===========================================================================================================================
789 void ExplorerBarWindow::Stop( DNSServiceRef ref
)
791 m_serviceRefs
.remove( ref
);
793 WSAAsyncSelect(DNSServiceRefSockFD( ref
), m_hWnd
, WM_PRIVATE_SERVICE_EVENT
, 0);
795 DNSServiceRefDeallocate( ref
);
803 //===========================================================================================================================
804 // FindServiceArrayIndex
805 //===========================================================================================================================
807 DEBUG_LOCAL
int FindServiceArrayIndex( const ServiceInfoArray
&inArray
, const ServiceInfo
&inService
, int &outIndex
)
817 hi
= (int)( inArray
.GetSize() - 1 );
820 mid
= ( lo
+ hi
) / 2;
821 result
= inService
.displayName
.CompareNoCase( inArray
[ mid
]->displayName
);
825 result
= ( (int) inService
.ifi
) - ( (int) inArray
[ mid
]->ifi
);
832 else if( result
< 0 )
843 mid
+= 1; // Bump index so new item is inserted after matching item.
845 else if( result
> 0 )