1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2004 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.
18 #include "ConfigPropertySheet.h"
19 #include <WinServices.h>
22 #include <ClientCommon.h>
28 #define WM_DATAREADY ( WM_USER + 0x100 )
31 IMPLEMENT_DYNCREATE(CConfigPropertySheet
, CPropertySheet
)
34 //---------------------------------------------------------------------------------------------------------------------------
35 // CConfigPropertySheet::CConfigPropertySheet
36 //---------------------------------------------------------------------------------------------------------------------------
38 CConfigPropertySheet::CConfigPropertySheet()
41 m_browseDomainsRef( NULL
),
43 m_threadExited( NULL
)
45 AddPage(&m_firstPage
);
46 AddPage(&m_secondPage
);
47 AddPage(&m_thirdPage
);
49 InitializeCriticalSection( &m_lock
);
53 //---------------------------------------------------------------------------------------------------------------------------
54 // CConfigPropertySheet::~CConfigPropertySheet
55 //---------------------------------------------------------------------------------------------------------------------------
57 CConfigPropertySheet::~CConfigPropertySheet()
59 DeleteCriticalSection( &m_lock
);
63 BEGIN_MESSAGE_MAP(CConfigPropertySheet
, CPropertySheet
)
64 //{{AFX_MSG_MAP(CConfigPropertySheet)
66 ON_MESSAGE( WM_DATAREADY
, OnDataReady
)
70 //---------------------------------------------------------------------------------------------------------------------------
71 // CConfigPropertySheet::OnInitDialog
72 //---------------------------------------------------------------------------------------------------------------------------
75 CConfigPropertySheet::OnInitDialog()
79 BOOL b
= CPropertySheet::OnInitDialog();
81 err
= SetupBrowsing();
82 require_noerr( err
, exit
);
90 //---------------------------------------------------------------------------------------------------------------------------
91 // CConfigPropertySheet::OnCommand
92 //---------------------------------------------------------------------------------------------------------------------------
95 CConfigPropertySheet::OnCommand(WPARAM wParam
, LPARAM lParam
)
97 // Check if OK or Cancel was hit
99 if ( ( wParam
== ID_WIZFINISH
) || ( wParam
== IDOK
) || ( wParam
== IDCANCEL
) )
104 return CPropertySheet::OnCommand(wParam
, lParam
);
108 //---------------------------------------------------------------------------------------------------------------------------
109 // CConfigPropertySheet::OnDataReady
110 //---------------------------------------------------------------------------------------------------------------------------
113 CConfigPropertySheet::OnDataReady(WPARAM inWParam
, LPARAM inLParam
)
115 if (WSAGETSELECTERROR(inLParam
) && !(HIWORD(inLParam
)))
117 dlog( kDebugLevelError
, "OnSocket: window error\n" );
121 SOCKET sock
= (SOCKET
) inWParam
;
123 if ( m_browseDomainsRef
&& DNSServiceRefSockFD( m_browseDomainsRef
) == (int) sock
)
125 DNSServiceProcessResult( m_browseDomainsRef
);
133 //---------------------------------------------------------------------------------------------------------------------------
134 // CConfigPropertySheet::OnEndDialog
135 //---------------------------------------------------------------------------------------------------------------------------
138 CConfigPropertySheet::OnEndDialog()
142 err
= TearDownBrowsing();
147 //---------------------------------------------------------------------------------------------------------------------------
148 // CConfigPropertySheet::SetupBrowsing
149 //---------------------------------------------------------------------------------------------------------------------------
152 CConfigPropertySheet::SetupBrowsing()
156 // Start browsing for browse domains
158 err
= DNSServiceEnumerateDomains( &m_browseDomainsRef
, kDNSServiceFlagsBrowseDomains
, 0, BrowseDomainsReply
, this );
159 require_noerr( err
, exit
);
161 err
= WSAAsyncSelect( DNSServiceRefSockFD( m_browseDomainsRef
), m_hWnd
, WM_DATAREADY
, FD_READ
|FD_CLOSE
);
162 require_noerr( err
, exit
);
175 //---------------------------------------------------------------------------------------------------------------------------
176 // CConfigPropertySheet::TearDownBrowsing
177 //---------------------------------------------------------------------------------------------------------------------------
180 CConfigPropertySheet::TearDownBrowsing()
182 OSStatus err
= kNoErr
;
184 if ( m_browseDomainsRef
)
186 err
= WSAAsyncSelect( DNSServiceRefSockFD( m_browseDomainsRef
), m_hWnd
, 0, 0 );
189 DNSServiceRefDeallocate( m_browseDomainsRef
);
191 m_browseDomainsRef
= NULL
;
198 //---------------------------------------------------------------------------------------------------------------------------
199 // CConfigPropertySheet::DecodeDomainName
200 //---------------------------------------------------------------------------------------------------------------------------
203 CConfigPropertySheet::DecodeDomainName( const char * raw
, CString
& decoded
)
205 char nextLabel
[128] = "\0";
206 char decodedDomainString
[kDNSServiceMaxDomainName
];
207 char * buffer
= (char *) raw
;
210 const char *label
[128];
215 decodedDomainString
[0] = '\0';
221 label
[labels
++] = buffer
;
222 buffer
= (char *) GetNextLabel(buffer
, text
);
225 buffer
= (char*) raw
;
227 for (i
= 0; i
< labels
; i
++)
229 buffer
= (char *)GetNextLabel(buffer
, nextLabel
);
230 strcat_s(decodedDomainString
, sizeof(decodedDomainString
), nextLabel
);
231 strcat_s(decodedDomainString
, sizeof(decodedDomainString
), ".");
234 // Remove trailing dot from domain name.
236 decodedDomainString
[ strlen( decodedDomainString
) - 1 ] = '\0';
238 // Convert to Unicode
240 err
= UTF8StringToStringObject( decodedDomainString
, decoded
);
246 //---------------------------------------------------------------------------------------------------------------------------
247 // CConfigPropertySheet::BrowseDomainsReply
248 //---------------------------------------------------------------------------------------------------------------------------
251 CConfigPropertySheet::BrowseDomainsReply
254 DNSServiceFlags flags
,
255 uint32_t interfaceIndex
,
256 DNSServiceErrorType errorCode
,
257 const char * replyDomain
,
261 CConfigPropertySheet
* self
= reinterpret_cast<CConfigPropertySheet
*>(context
);
265 DEBUG_UNUSED( sdRef
);
266 DEBUG_UNUSED( interfaceIndex
);
273 check( replyDomain
);
275 // Ignore local domains
277 if ( strcmp( replyDomain
, "local." ) == 0 )
282 err
= self
->DecodeDomainName( replyDomain
, decoded
);
283 require_noerr( err
, exit
);
285 // Remove trailing '.'
287 decoded
.TrimRight( '.' );
289 if ( flags
& kDNSServiceFlagsAdd
)
291 self
->m_browseDomains
.push_back( decoded
);
295 self
->m_browseDomains
.remove( decoded
);