]>
Commit | Line | Data |
---|---|---|
a0b4c98b VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/dialup.cpp | |
3 | // Purpose: MSW implementation of network/dialup classes and functions | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 07.07.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
65571936 | 9 | // Licence: wxWindows licence |
a0b4c98b VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
a0b4c98b VZ |
27 | #if wxUSE_DIALUP_MANAGER |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/log.h" | |
d1f4970d VZ |
31 | #include "wx/intl.h" |
32 | #include "wx/event.h" | |
a0b4c98b VZ |
33 | #endif |
34 | ||
fcec6429 VZ |
35 | #include "wx/timer.h" |
36 | #include "wx/app.h" | |
37 | #include "wx/generic/choicdgg.h" | |
38 | ||
a0b4c98b | 39 | #include "wx/dynlib.h" |
2690830e | 40 | #include "wx/dialup.h" |
8563c6f1 | 41 | #include "wx/module.h" |
a0b4c98b | 42 | |
2e4df4bf VZ |
43 | DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED) |
44 | DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED) | |
45 | ||
3bce6687 JS |
46 | // Doesn't yet compile under VC++ 4, BC++, Watcom C++, |
47 | // Wine: no wininet.h | |
3aabb24c | 48 | #if (!defined(__BORLANDC__) || (__BORLANDC__>=0x550)) && \ |
ce20b5d7 | 49 | (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \ |
7ba4fbeb | 50 | !defined(__GNUWIN32_OLD__) && \ |
5283098e | 51 | !defined(__WINE__) && \ |
7ba4fbeb | 52 | (!defined(__VISUALC__) || (__VISUALC__ >= 1020)) |
106f0395 | 53 | |
a0b4c98b VZ |
54 | #include <ras.h> |
55 | #include <raserror.h> | |
56 | ||
2690830e VZ |
57 | #include <wininet.h> |
58 | ||
a2327a9f JS |
59 | // Not in VC++ 5 |
60 | #ifndef INTERNET_CONNECTION_LAN | |
61 | #define INTERNET_CONNECTION_LAN 2 | |
62 | #endif | |
63 | #ifndef INTERNET_CONNECTION_PROXY | |
64 | #define INTERNET_CONNECTION_PROXY 4 | |
65 | #endif | |
66 | ||
8563c6f1 VZ |
67 | // implemented in utils.cpp |
68 | extern "C" WXDLLIMPEXP_BASE HWND | |
69 | wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc); | |
70 | ||
71 | static const wxChar * | |
72 | wxMSWDIALUP_WNDCLASSNAME = wxT("_wxDialUpManager_Internal_Class"); | |
73 | static const wxChar *gs_classForDialUpWindow = NULL; | |
74 | ||
a0b4c98b VZ |
75 | // ---------------------------------------------------------------------------- |
76 | // constants | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
79 | // this message is sent by the secondary thread when RAS status changes | |
80 | #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010) | |
2690830e | 81 | #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011) |
a0b4c98b VZ |
82 | |
83 | // ---------------------------------------------------------------------------- | |
84 | // types | |
85 | // ---------------------------------------------------------------------------- | |
86 | ||
87 | // the signatures of RAS functions: all this is quite heavy, but we must do it | |
88 | // to allow running wxWin programs on machine which don't have RAS installed | |
89 | // (this does exist) - if we link with rasapi32.lib, the program will fail on | |
90 | // startup because of the missing DLL... | |
91 | ||
92 | #ifndef UNICODE | |
2690830e VZ |
93 | typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCSTR, LPRASDIALPARAMSA, DWORD, LPVOID, LPHRASCONN ); |
94 | typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNA, LPDWORD, LPDWORD ); | |
95 | typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCSTR, LPCSTR, LPRASENTRYNAMEA, LPDWORD, LPDWORD ); | |
96 | typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSA ); | |
97 | typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPSTR, DWORD ); | |
98 | typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN ); | |
99 | typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD ); | |
100 | typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCSTR ); | |
101 | typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCSTR, LPCSTR ); | |
102 | typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, BOOL ); | |
103 | typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, LPBOOL ); | |
104 | typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOA, LPDWORD, LPDWORD ); | |
105 | typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOA, LPDWORD ); | |
106 | typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, LPDWORD, LPBYTE, LPDWORD ); | |
107 | typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, DWORD, LPBYTE, DWORD ); | |
108 | typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCSTR, LPCSTR, LPCSTR ); | |
109 | typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCSTR, LPCSTR ); | |
110 | typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCSTR, LPCSTR ); | |
111 | typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD ); | |
a0b4c98b | 112 | |
f6bcfd97 | 113 | static const wxChar gs_funcSuffix = _T('A'); |
a0b4c98b | 114 | #else // Unicode |
2690830e VZ |
115 | typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCWSTR, LPRASDIALPARAMSW, DWORD, LPVOID, LPHRASCONN ); |
116 | typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNW, LPDWORD, LPDWORD ); | |
117 | typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCWSTR, LPCWSTR, LPRASENTRYNAMEW, LPDWORD, LPDWORD ); | |
118 | typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSW ); | |
119 | typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPWSTR, DWORD ); | |
120 | typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN ); | |
121 | typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD ); | |
122 | typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCWSTR ); | |
123 | typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCWSTR, LPCWSTR ); | |
124 | typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, BOOL ); | |
125 | typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, LPBOOL ); | |
126 | typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOW, LPDWORD, LPDWORD ); | |
127 | typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOW, LPDWORD ); | |
128 | typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, LPDWORD, LPBYTE, LPDWORD ); | |
129 | typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, DWORD, LPBYTE, DWORD ); | |
130 | typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCWSTR, LPCWSTR, LPCWSTR ); | |
131 | typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCWSTR, LPCWSTR ); | |
132 | typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCWSTR, LPCWSTR ); | |
133 | typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD ); | |
a0b4c98b | 134 | |
f6bcfd97 | 135 | static const wxChar gs_funcSuffix = _T('W'); |
a0b4c98b VZ |
136 | #endif // ASCII/Unicode |
137 | ||
138 | // structure passed to the secondary thread | |
3b415ba4 | 139 | struct WXDLLEXPORT wxRasThreadData |
a0b4c98b VZ |
140 | { |
141 | wxRasThreadData() | |
142 | { | |
143 | hWnd = 0; | |
97247d36 VZ |
144 | hEventRas = |
145 | hEventQuit = 0; | |
a0b4c98b VZ |
146 | dialUpManager = NULL; |
147 | } | |
148 | ||
97247d36 VZ |
149 | ~wxRasThreadData() |
150 | { | |
151 | if ( hWnd ) | |
152 | DestroyWindow(hWnd); | |
153 | ||
154 | if ( hEventQuit ) | |
155 | CloseHandle(hEventQuit); | |
156 | ||
157 | if ( hEventRas ) | |
158 | CloseHandle(hEventRas); | |
159 | } | |
160 | ||
a0b4c98b | 161 | HWND hWnd; // window to send notifications to |
97247d36 VZ |
162 | HANDLE hEventRas, // automatic event which RAS signals when status changes |
163 | hEventQuit; // manual event which we signal when we terminate | |
a0b4c98b VZ |
164 | |
165 | class WXDLLEXPORT wxDialUpManagerMSW *dialUpManager; // the owner | |
166 | }; | |
167 | ||
168 | // ---------------------------------------------------------------------------- | |
169 | // wxDialUpManager class for MSW | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
172 | class WXDLLEXPORT wxDialUpManagerMSW : public wxDialUpManager | |
173 | { | |
174 | public: | |
175 | // ctor & dtor | |
176 | wxDialUpManagerMSW(); | |
177 | virtual ~wxDialUpManagerMSW(); | |
178 | ||
179 | // implement base class pure virtuals | |
180 | virtual bool IsOk() const; | |
2690830e | 181 | virtual size_t GetISPNames(wxArrayString& names) const; |
a0b4c98b VZ |
182 | virtual bool Dial(const wxString& nameOfISP, |
183 | const wxString& username, | |
184 | const wxString& password, | |
185 | bool async); | |
186 | virtual bool IsDialing() const; | |
187 | virtual bool CancelDialing(); | |
188 | virtual bool HangUp(); | |
2690830e | 189 | virtual bool IsAlwaysOnline() const; |
a0b4c98b | 190 | virtual bool IsOnline() const; |
d71cc120 | 191 | virtual void SetOnlineStatus(bool isOnline = true); |
a0b4c98b VZ |
192 | virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds); |
193 | virtual void DisableAutoCheckOnlineStatus(); | |
194 | virtual void SetWellKnownHost(const wxString& hostname, int port); | |
195 | virtual void SetConnectCommand(const wxString& commandDial, | |
196 | const wxString& commandHangup); | |
197 | ||
198 | // for RasTimer | |
199 | void CheckRasStatus(); | |
200 | ||
201 | // for wxRasStatusWindowProc | |
202 | void OnConnectStatusChange(); | |
2690830e | 203 | void OnDialProgress(RASCONNSTATE rasconnstate, DWORD dwError); |
a0b4c98b VZ |
204 | |
205 | // for wxRasDialFunc | |
2690830e | 206 | static HWND GetRasWindow() { return ms_hwndRas; } |
8563c6f1 | 207 | static void ResetRasWindow() { ms_hwndRas = NULL; } |
a0b4c98b VZ |
208 | static wxDialUpManagerMSW *GetDialer() { return ms_dialer; } |
209 | ||
210 | private: | |
211 | // return the error string for the given RAS error code | |
212 | static wxString GetErrorString(DWORD error); | |
213 | ||
214 | // find the (first) handle of the active connection | |
215 | static HRASCONN FindActiveConnection(); | |
216 | ||
217 | // notify the application about status change | |
d71cc120 | 218 | void NotifyApp(bool connected, bool fromOurselves = false) const; |
a0b4c98b VZ |
219 | |
220 | // destroy the thread data and the thread itself | |
221 | void CleanUpThreadData(); | |
222 | ||
6bba111c VZ |
223 | // number of times EnableAutoCheckOnlineStatus() had been called minus the |
224 | // number of times DisableAutoCheckOnlineStatus() had been called | |
225 | int m_autoCheckLevel; | |
226 | ||
a0b4c98b | 227 | // timer used for polling RAS status |
3b415ba4 | 228 | class WXDLLEXPORT RasTimer : public wxTimer |
a0b4c98b VZ |
229 | { |
230 | public: | |
231 | RasTimer(wxDialUpManagerMSW *dialUpManager) | |
232 | { m_dialUpManager = dialUpManager; } | |
233 | ||
234 | virtual void Notify() { m_dialUpManager->CheckRasStatus(); } | |
235 | ||
236 | private: | |
237 | wxDialUpManagerMSW *m_dialUpManager; | |
2eb10e2a VZ |
238 | |
239 | DECLARE_NO_COPY_CLASS(RasTimer) | |
a0b4c98b VZ |
240 | } m_timerStatusPolling; |
241 | ||
242 | // thread handle for the thread sitting on connection change event | |
243 | HANDLE m_hThread; | |
244 | ||
245 | // data used by this thread and our hidden window to send messages between | |
246 | // each other | |
97247d36 | 247 | wxRasThreadData *m_data; |
a0b4c98b | 248 | |
4f89dbc4 | 249 | // the handle of rasapi32.dll when it's loaded |
4522bb3e | 250 | wxDynamicLibrary m_dllRas; |
4f89dbc4 | 251 | |
2690830e VZ |
252 | // the hidden window we use for passing messages between threads |
253 | static HWND ms_hwndRas; | |
254 | ||
a0b4c98b VZ |
255 | // the handle of the connection we initiated or 0 if none |
256 | static HRASCONN ms_hRasConnection; | |
257 | ||
a0b4c98b VZ |
258 | // the pointers to RAS functions |
259 | static RASDIAL ms_pfnRasDial; | |
260 | static RASENUMCONNECTIONS ms_pfnRasEnumConnections; | |
261 | static RASENUMENTRIES ms_pfnRasEnumEntries; | |
262 | static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus; | |
263 | static RASGETERRORSTRING ms_pfnRasGetErrorString; | |
264 | static RASHANGUP ms_pfnRasHangUp; | |
265 | static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo; | |
266 | static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry; | |
267 | static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry; | |
268 | static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams; | |
269 | static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams; | |
270 | static RASENUMDEVICES ms_pfnRasEnumDevices; | |
271 | static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo; | |
272 | static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties; | |
273 | static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties; | |
274 | static RASRENAMEENTRY ms_pfnRasRenameEntry; | |
275 | static RASDELETEENTRY ms_pfnRasDeleteEntry; | |
276 | static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName; | |
277 | ||
278 | // this function is not supported by Win95 | |
279 | static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification; | |
280 | ||
281 | // if this flag is different from -1, it overrides IsOnline() | |
282 | static int ms_userSpecifiedOnlineStatus; | |
283 | ||
284 | // this flag tells us if we're online | |
285 | static int ms_isConnected; | |
286 | ||
287 | // this flag tells us whether a call to RasDial() is in progress | |
288 | static wxDialUpManagerMSW *ms_dialer; | |
2eb10e2a VZ |
289 | |
290 | DECLARE_NO_COPY_CLASS(wxDialUpManagerMSW) | |
a0b4c98b VZ |
291 | }; |
292 | ||
8563c6f1 VZ |
293 | // module to destroy helper window created by wxDialUpManagerMSW |
294 | class wxDialUpManagerModule : public wxModule | |
295 | { | |
296 | public: | |
297 | bool OnInit() { return true; } | |
298 | void OnExit() | |
299 | { | |
300 | HWND hwnd = wxDialUpManagerMSW::GetRasWindow(); | |
301 | if ( hwnd ) | |
302 | { | |
303 | ::DestroyWindow(hwnd); | |
304 | wxDialUpManagerMSW::ResetRasWindow(); | |
305 | } | |
306 | ||
307 | if ( gs_classForDialUpWindow ) | |
308 | { | |
309 | ::UnregisterClass(wxMSWDIALUP_WNDCLASSNAME, wxGetInstance()); | |
310 | gs_classForDialUpWindow = NULL; | |
311 | } | |
312 | } | |
313 | ||
314 | private: | |
315 | DECLARE_DYNAMIC_CLASS(wxDialUpManagerModule) | |
316 | }; | |
317 | ||
318 | IMPLEMENT_DYNAMIC_CLASS(wxDialUpManagerModule, wxModule) | |
319 | ||
a0b4c98b VZ |
320 | // ---------------------------------------------------------------------------- |
321 | // private functions | |
322 | // ---------------------------------------------------------------------------- | |
323 | ||
324 | static LRESULT WINAPI wxRasStatusWindowProc(HWND hWnd, UINT message, | |
325 | WPARAM wParam, LPARAM lParam); | |
326 | ||
327 | static DWORD wxRasMonitorThread(wxRasThreadData *data); | |
328 | ||
329 | static void WINAPI wxRasDialFunc(UINT unMsg, | |
330 | RASCONNSTATE rasconnstate, | |
331 | DWORD dwError); | |
332 | ||
333 | // ============================================================================ | |
334 | // implementation | |
335 | // ============================================================================ | |
336 | ||
337 | // ---------------------------------------------------------------------------- | |
338 | // init the static variables | |
339 | // ---------------------------------------------------------------------------- | |
340 | ||
341 | HRASCONN wxDialUpManagerMSW::ms_hRasConnection = 0; | |
342 | ||
2690830e VZ |
343 | HWND wxDialUpManagerMSW::ms_hwndRas = 0; |
344 | ||
a0b4c98b VZ |
345 | RASDIAL wxDialUpManagerMSW::ms_pfnRasDial = 0; |
346 | RASENUMCONNECTIONS wxDialUpManagerMSW::ms_pfnRasEnumConnections = 0; | |
347 | RASENUMENTRIES wxDialUpManagerMSW::ms_pfnRasEnumEntries = 0; | |
348 | RASGETCONNECTSTATUS wxDialUpManagerMSW::ms_pfnRasGetConnectStatus = 0; | |
349 | RASGETERRORSTRING wxDialUpManagerMSW::ms_pfnRasGetErrorString = 0; | |
350 | RASHANGUP wxDialUpManagerMSW::ms_pfnRasHangUp = 0; | |
351 | RASGETPROJECTIONINFO wxDialUpManagerMSW::ms_pfnRasGetProjectionInfo = 0; | |
352 | RASCREATEPHONEBOOKENTRY wxDialUpManagerMSW::ms_pfnRasCreatePhonebookEntry = 0; | |
353 | RASEDITPHONEBOOKENTRY wxDialUpManagerMSW::ms_pfnRasEditPhonebookEntry = 0; | |
354 | RASSETENTRYDIALPARAMS wxDialUpManagerMSW::ms_pfnRasSetEntryDialParams = 0; | |
355 | RASGETENTRYDIALPARAMS wxDialUpManagerMSW::ms_pfnRasGetEntryDialParams = 0; | |
356 | RASENUMDEVICES wxDialUpManagerMSW::ms_pfnRasEnumDevices = 0; | |
357 | RASGETCOUNTRYINFO wxDialUpManagerMSW::ms_pfnRasGetCountryInfo = 0; | |
358 | RASGETENTRYPROPERTIES wxDialUpManagerMSW::ms_pfnRasGetEntryProperties = 0; | |
359 | RASSETENTRYPROPERTIES wxDialUpManagerMSW::ms_pfnRasSetEntryProperties = 0; | |
360 | RASRENAMEENTRY wxDialUpManagerMSW::ms_pfnRasRenameEntry = 0; | |
361 | RASDELETEENTRY wxDialUpManagerMSW::ms_pfnRasDeleteEntry = 0; | |
362 | RASVALIDATEENTRYNAME wxDialUpManagerMSW::ms_pfnRasValidateEntryName = 0; | |
363 | RASCONNECTIONNOTIFICATION wxDialUpManagerMSW::ms_pfnRasConnectionNotification = 0; | |
364 | ||
365 | int wxDialUpManagerMSW::ms_userSpecifiedOnlineStatus = -1; | |
366 | int wxDialUpManagerMSW::ms_isConnected = -1; | |
367 | wxDialUpManagerMSW *wxDialUpManagerMSW::ms_dialer = NULL; | |
368 | ||
369 | // ---------------------------------------------------------------------------- | |
370 | // ctor and dtor: the dynamic linking happens here | |
371 | // ---------------------------------------------------------------------------- | |
372 | ||
373 | // the static creator function is implemented here | |
374 | wxDialUpManager *wxDialUpManager::Create() | |
375 | { | |
376 | return new wxDialUpManagerMSW; | |
377 | } | |
378 | ||
379 | #ifdef __VISUALC__ | |
380 | // warning about "'this' : used in base member initializer list" - so what? | |
381 | #pragma warning(disable:4355) | |
382 | #endif // VC++ | |
383 | ||
384 | wxDialUpManagerMSW::wxDialUpManagerMSW() | |
4522bb3e VZ |
385 | : m_timerStatusPolling(this), |
386 | m_dllRas(_T("RASAPI32")) | |
a0b4c98b VZ |
387 | { |
388 | // initialize our data | |
6bba111c | 389 | m_autoCheckLevel = 0; |
a0b4c98b | 390 | m_hThread = 0; |
97247d36 | 391 | m_data = new wxRasThreadData; |
a0b4c98b | 392 | |
4f89dbc4 RL |
393 | if ( !m_dllRas.IsLoaded() ) |
394 | { | |
395 | wxLogError(_("Dial up functions are unavailable because the remote access service (RAS) is not installed on this machine. Please install it.")); | |
396 | } | |
1c25d245 | 397 | else if ( !ms_pfnRasDial ) |
a0b4c98b | 398 | { |
4f89dbc4 RL |
399 | // resolve the functions we need |
400 | ||
401 | // this will contain the name of the function we failed to resolve | |
402 | // if any at the end | |
403 | const char *funcName = NULL; | |
404 | ||
405 | // get the function from rasapi32.dll and abort if it's not found | |
406 | #define RESOLVE_RAS_FUNCTION(type, name) \ | |
407 | ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \ | |
408 | + gs_funcSuffix); \ | |
409 | if ( !ms_pfn##name ) \ | |
410 | { \ | |
411 | funcName = #name; \ | |
412 | goto exit; \ | |
413 | } | |
414 | ||
415 | // a variant of above macro which doesn't abort if the function is | |
416 | // not found in the DLL | |
417 | #define RESOLVE_OPTIONAL_RAS_FUNCTION(type, name) \ | |
418 | ms_pfn##name = (type)m_dllRas.GetSymbol( wxString(_T(#name)) \ | |
419 | + gs_funcSuffix); | |
420 | ||
421 | RESOLVE_RAS_FUNCTION(RASDIAL, RasDial); | |
422 | RESOLVE_RAS_FUNCTION(RASENUMCONNECTIONS, RasEnumConnections); | |
423 | RESOLVE_RAS_FUNCTION(RASENUMENTRIES, RasEnumEntries); | |
424 | RESOLVE_RAS_FUNCTION(RASGETCONNECTSTATUS, RasGetConnectStatus); | |
425 | RESOLVE_RAS_FUNCTION(RASGETERRORSTRING, RasGetErrorString); | |
426 | RESOLVE_RAS_FUNCTION(RASHANGUP, RasHangUp); | |
427 | RESOLVE_RAS_FUNCTION(RASGETENTRYDIALPARAMS, RasGetEntryDialParams); | |
428 | ||
429 | // suppress error messages about missing (non essential) functions | |
a0b4c98b | 430 | { |
4f89dbc4 RL |
431 | wxLogNull noLog; |
432 | ||
433 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETPROJECTIONINFO, RasGetProjectionInfo); | |
434 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASCREATEPHONEBOOKENTRY, RasCreatePhonebookEntry); | |
435 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASEDITPHONEBOOKENTRY, RasEditPhonebookEntry); | |
436 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYDIALPARAMS, RasSetEntryDialParams); | |
437 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETENTRYPROPERTIES, RasGetEntryProperties); | |
438 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASSETENTRYPROPERTIES, RasSetEntryProperties); | |
439 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASRENAMEENTRY, RasRenameEntry); | |
440 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASDELETEENTRY, RasDeleteEntry); | |
441 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASVALIDATEENTRYNAME, RasValidateEntryName); | |
442 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASGETCOUNTRYINFO, RasGetCountryInfo); | |
443 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASENUMDEVICES, RasEnumDevices); | |
444 | RESOLVE_OPTIONAL_RAS_FUNCTION(RASCONNECTIONNOTIFICATION, RasConnectionNotification); | |
a0b4c98b | 445 | } |
a0b4c98b | 446 | |
4f89dbc4 RL |
447 | // keep your preprocessor name space clean |
448 | #undef RESOLVE_RAS_FUNCTION | |
449 | #undef RESOLVE_OPTIONAL_RAS_FUNCTION | |
a0b4c98b VZ |
450 | |
451 | exit: | |
4f89dbc4 RL |
452 | if ( funcName ) |
453 | { | |
454 | static const wxChar *msg = wxTRANSLATE( | |
f6bcfd97 BP |
455 | "The version of remote access service (RAS) installed on this machine is too\ |
456 | old, please upgrade (the following required function is missing: %s)." | |
4f89dbc4 | 457 | ); |
a0b4c98b | 458 | |
4f89dbc4 RL |
459 | wxLogError(wxGetTranslation(msg), funcName); |
460 | m_dllRas.Unload(); | |
461 | return; | |
a0b4c98b VZ |
462 | } |
463 | } | |
464 | ||
465 | // enable auto check by default | |
466 | EnableAutoCheckOnlineStatus(0); | |
467 | } | |
468 | ||
469 | wxDialUpManagerMSW::~wxDialUpManagerMSW() | |
470 | { | |
471 | CleanUpThreadData(); | |
a0b4c98b VZ |
472 | } |
473 | ||
474 | // ---------------------------------------------------------------------------- | |
475 | // helper functions | |
476 | // ---------------------------------------------------------------------------- | |
477 | ||
478 | wxString wxDialUpManagerMSW::GetErrorString(DWORD error) | |
479 | { | |
480 | wxChar buffer[512]; // this should be more than enough according to MS docs | |
481 | DWORD dwRet = ms_pfnRasGetErrorString(error, buffer, WXSIZEOF(buffer)); | |
482 | switch ( dwRet ) | |
483 | { | |
484 | case ERROR_INVALID_PARAMETER: | |
485 | // this was a standard Win32 error probably | |
486 | return wxString(wxSysErrorMsg(error)); | |
487 | ||
488 | default: | |
489 | { | |
f6bcfd97 BP |
490 | wxLogSysError(dwRet, |
491 | _("Failed to retrieve text of RAS error message")); | |
a0b4c98b VZ |
492 | |
493 | wxString msg; | |
494 | msg.Printf(_("unknown error (error code %08x)."), error); | |
495 | return msg; | |
496 | } | |
497 | ||
498 | case 0: | |
499 | // we want the error message to start from a lower case letter | |
907173e5 | 500 | buffer[0] = (wxChar)wxTolower(buffer[0]); |
a0b4c98b VZ |
501 | |
502 | return wxString(buffer); | |
503 | } | |
504 | } | |
505 | ||
506 | HRASCONN wxDialUpManagerMSW::FindActiveConnection() | |
507 | { | |
508 | // enumerate connections | |
509 | DWORD cbBuf = sizeof(RASCONN); | |
510 | LPRASCONN lpRasConn = (LPRASCONN)malloc(cbBuf); | |
511 | if ( !lpRasConn ) | |
512 | { | |
513 | // out of memory | |
514 | return 0; | |
515 | } | |
516 | ||
517 | lpRasConn->dwSize = sizeof(RASCONN); | |
518 | ||
519 | DWORD nConnections = 0; | |
520 | DWORD dwRet = ERROR_BUFFER_TOO_SMALL; | |
521 | ||
522 | while ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
523 | { | |
524 | dwRet = ms_pfnRasEnumConnections(lpRasConn, &cbBuf, &nConnections); | |
525 | ||
526 | if ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
527 | { | |
528 | LPRASCONN lpRasConnOld = lpRasConn; | |
529 | lpRasConn = (LPRASCONN)realloc(lpRasConn, cbBuf); | |
530 | if ( !lpRasConn ) | |
531 | { | |
532 | // out of memory | |
533 | free(lpRasConnOld); | |
534 | ||
535 | return 0; | |
536 | } | |
537 | } | |
538 | else if ( dwRet == 0 ) | |
539 | { | |
540 | // ok, success | |
541 | break; | |
542 | } | |
543 | else | |
544 | { | |
3103e8a9 | 545 | // an error occurred |
a0b4c98b | 546 | wxLogError(_("Cannot find active dialup connection: %s"), |
5b09e9c2 | 547 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
548 | return 0; |
549 | } | |
550 | } | |
551 | ||
552 | HRASCONN hrasconn; | |
553 | ||
554 | switch ( nConnections ) | |
555 | { | |
556 | case 0: | |
557 | // no connections | |
558 | hrasconn = 0; | |
559 | break; | |
560 | ||
561 | default: | |
562 | // more than 1 connection - we don't know what to do with this | |
563 | // case, so give a warning but continue (taking the first | |
564 | // connection) - the warning is really needed because this function | |
565 | // is used, for example, to select the connection to hang up and so | |
566 | // we may hang up the wrong connection here... | |
f6bcfd97 | 567 | wxLogWarning(_("Several active dialup connections found, choosing one randomly.")); |
a0b4c98b VZ |
568 | // fall through |
569 | ||
570 | case 1: | |
571 | // exactly 1 connection, great | |
572 | hrasconn = lpRasConn->hrasconn; | |
573 | } | |
574 | ||
575 | free(lpRasConn); | |
576 | ||
577 | return hrasconn; | |
578 | } | |
579 | ||
580 | void wxDialUpManagerMSW::CleanUpThreadData() | |
581 | { | |
582 | if ( m_hThread ) | |
583 | { | |
97247d36 | 584 | if ( !SetEvent(m_data->hEventQuit) ) |
a0b4c98b | 585 | { |
fcec6429 | 586 | wxLogLastError(_T("SetEvent(RasThreadQuit)")); |
a0b4c98b | 587 | } |
97247d36 VZ |
588 | else // sent quit request to the background thread |
589 | { | |
590 | // the thread still needs m_data so we can't free it here, rather | |
591 | // let the thread do it itself | |
592 | m_data = NULL; | |
593 | } | |
a0b4c98b VZ |
594 | |
595 | CloseHandle(m_hThread); | |
596 | ||
597 | m_hThread = 0; | |
598 | } | |
599 | ||
97247d36 | 600 | if ( m_data ) |
a0b4c98b | 601 | { |
97247d36 VZ |
602 | delete m_data; |
603 | m_data = NULL; | |
a0b4c98b VZ |
604 | } |
605 | } | |
606 | ||
607 | // ---------------------------------------------------------------------------- | |
608 | // connection status | |
609 | // ---------------------------------------------------------------------------- | |
610 | ||
611 | void wxDialUpManagerMSW::CheckRasStatus() | |
612 | { | |
613 | // use int, not bool to compare with -1 | |
614 | int isConnected = FindActiveConnection() != 0; | |
615 | if ( isConnected != ms_isConnected ) | |
616 | { | |
617 | if ( ms_isConnected != -1 ) | |
618 | { | |
619 | // notify the program | |
620 | NotifyApp(isConnected != 0); | |
621 | } | |
622 | // else: it's the first time we're called, just update the flag | |
623 | ||
624 | ms_isConnected = isConnected; | |
625 | } | |
626 | } | |
627 | ||
628 | void wxDialUpManagerMSW::NotifyApp(bool connected, bool fromOurselves) const | |
629 | { | |
630 | wxDialUpEvent event(connected, fromOurselves); | |
631 | (void)wxTheApp->ProcessEvent(event); | |
632 | } | |
633 | ||
634 | // this function is called whenever the status of any RAS connection on this | |
635 | // machine changes by RAS itself | |
636 | void wxDialUpManagerMSW::OnConnectStatusChange() | |
637 | { | |
638 | // we know that status changed, but we don't know whether we're connected | |
639 | // or not - so find it out | |
640 | CheckRasStatus(); | |
641 | } | |
642 | ||
643 | // this function is called by our callback which we give to RasDial() when | |
644 | // calling it asynchronously | |
645 | void wxDialUpManagerMSW::OnDialProgress(RASCONNSTATE rasconnstate, | |
646 | DWORD dwError) | |
647 | { | |
648 | if ( !GetDialer() ) | |
649 | { | |
650 | // this probably means that CancelDialing() was called and we get | |
651 | // "disconnected" notification | |
652 | return; | |
653 | } | |
654 | ||
655 | // we're only interested in 2 events: connected and disconnected | |
656 | if ( dwError ) | |
657 | { | |
658 | wxLogError(_("Failed to establish dialup connection: %s"), | |
5b09e9c2 | 659 | GetErrorString(dwError).c_str()); |
a0b4c98b VZ |
660 | |
661 | // we should still call RasHangUp() if we got a non 0 connection | |
662 | if ( ms_hRasConnection ) | |
663 | { | |
664 | ms_pfnRasHangUp(ms_hRasConnection); | |
665 | ms_hRasConnection = 0; | |
666 | } | |
667 | ||
668 | ms_dialer = NULL; | |
669 | ||
d71cc120 | 670 | NotifyApp(false /* !connected */, true /* we dialed ourselves */); |
a0b4c98b VZ |
671 | } |
672 | else if ( rasconnstate == RASCS_Connected ) | |
673 | { | |
d71cc120 | 674 | ms_isConnected = true; |
a0b4c98b VZ |
675 | ms_dialer = NULL; |
676 | ||
d71cc120 | 677 | NotifyApp(true /* connected */, true /* we dialed ourselves */); |
a0b4c98b VZ |
678 | } |
679 | } | |
680 | ||
681 | // ---------------------------------------------------------------------------- | |
682 | // implementation of wxDialUpManager functions | |
683 | // ---------------------------------------------------------------------------- | |
684 | ||
685 | bool wxDialUpManagerMSW::IsOk() const | |
686 | { | |
4f89dbc4 | 687 | return m_dllRas.IsLoaded(); |
a0b4c98b VZ |
688 | } |
689 | ||
2690830e VZ |
690 | size_t wxDialUpManagerMSW::GetISPNames(wxArrayString& names) const |
691 | { | |
692 | // fetch the entries | |
693 | DWORD size = sizeof(RASENTRYNAME); | |
694 | RASENTRYNAME *rasEntries = (RASENTRYNAME *)malloc(size); | |
695 | rasEntries->dwSize = sizeof(RASENTRYNAME); | |
696 | ||
697 | DWORD nEntries; | |
698 | DWORD dwRet; | |
699 | do | |
700 | { | |
701 | dwRet = ms_pfnRasEnumEntries | |
702 | ( | |
703 | NULL, // reserved | |
704 | NULL, // default phone book (or all) | |
705 | rasEntries, // [out] buffer for the entries | |
706 | &size, // [in/out] size of the buffer | |
707 | &nEntries // [out] number of entries fetched | |
708 | ); | |
709 | ||
710 | if ( dwRet == ERROR_BUFFER_TOO_SMALL ) | |
711 | { | |
712 | // reallocate the buffer | |
713 | rasEntries = (RASENTRYNAME *)realloc(rasEntries, size); | |
714 | } | |
715 | else if ( dwRet != 0 ) | |
716 | { | |
717 | // some other error - abort | |
5b09e9c2 VZ |
718 | wxLogError(_("Failed to get ISP names: %s"), |
719 | GetErrorString(dwRet).c_str()); | |
2690830e VZ |
720 | |
721 | free(rasEntries); | |
722 | ||
723 | return 0u; | |
724 | } | |
725 | } | |
726 | while ( dwRet != 0 ); | |
727 | ||
728 | // process them | |
729 | names.Empty(); | |
730 | for ( size_t n = 0; n < (size_t)nEntries; n++ ) | |
731 | { | |
732 | names.Add(rasEntries[n].szEntryName); | |
733 | } | |
734 | ||
735 | free(rasEntries); | |
736 | ||
737 | // return the number of entries | |
738 | return names.GetCount(); | |
739 | } | |
740 | ||
a0b4c98b VZ |
741 | bool wxDialUpManagerMSW::Dial(const wxString& nameOfISP, |
742 | const wxString& username, | |
743 | const wxString& password, | |
744 | bool async) | |
745 | { | |
2690830e | 746 | // check preconditions |
d71cc120 | 747 | wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
748 | |
749 | if ( ms_hRasConnection ) | |
750 | { | |
223d09f6 | 751 | wxFAIL_MSG(wxT("there is already an active connection")); |
a0b4c98b | 752 | |
d71cc120 | 753 | return true; |
a0b4c98b VZ |
754 | } |
755 | ||
2690830e VZ |
756 | // get the default ISP if none given |
757 | wxString entryName(nameOfISP); | |
758 | if ( !entryName ) | |
759 | { | |
760 | wxArrayString names; | |
761 | size_t count = GetISPNames(names); | |
762 | switch ( count ) | |
763 | { | |
764 | case 0: | |
765 | // no known ISPs, abort | |
766 | wxLogError(_("Failed to connect: no ISP to dial.")); | |
767 | ||
d71cc120 | 768 | return false; |
2690830e VZ |
769 | |
770 | case 1: | |
771 | // only one ISP, choose it | |
772 | entryName = names[0u]; | |
773 | break; | |
774 | ||
775 | default: | |
776 | // several ISPs, let the user choose | |
777 | { | |
778 | wxString *strings = new wxString[count]; | |
779 | for ( size_t i = 0; i < count; i++ ) | |
780 | { | |
781 | strings[i] = names[i]; | |
782 | } | |
783 | ||
784 | entryName = wxGetSingleChoice | |
785 | ( | |
786 | _("Choose ISP to dial"), | |
f6bcfd97 | 787 | _("Please choose which ISP do you want to connect to"), |
2690830e VZ |
788 | count, |
789 | strings | |
790 | ); | |
791 | ||
792 | delete [] strings; | |
793 | ||
794 | if ( !entryName ) | |
795 | { | |
796 | // cancelled by user | |
d71cc120 | 797 | return false; |
2690830e VZ |
798 | } |
799 | } | |
800 | } | |
801 | } | |
802 | ||
a0b4c98b VZ |
803 | RASDIALPARAMS rasDialParams; |
804 | rasDialParams.dwSize = sizeof(rasDialParams); | |
f6bcfd97 | 805 | wxStrncpy(rasDialParams.szEntryName, entryName, RAS_MaxEntryName); |
2690830e VZ |
806 | |
807 | // do we have the username and password? | |
808 | if ( !username || !password ) | |
809 | { | |
810 | BOOL gotPassword; | |
811 | DWORD dwRet = ms_pfnRasGetEntryDialParams | |
812 | ( | |
813 | NULL, // default phonebook | |
814 | &rasDialParams, // [in/out] the params of this entry | |
815 | &gotPassword // [out] did we get password? | |
816 | ); | |
817 | ||
818 | if ( dwRet != 0 ) | |
819 | { | |
820 | wxLogError(_("Failed to connect: missing username/password.")); | |
821 | ||
d71cc120 | 822 | return false; |
2690830e VZ |
823 | } |
824 | } | |
4f89dbc4 RL |
825 | else |
826 | { | |
827 | wxStrncpy(rasDialParams.szUserName, username, UNLEN); | |
828 | wxStrncpy(rasDialParams.szPassword, password, PWLEN); | |
829 | } | |
2690830e | 830 | |
4f89dbc4 | 831 | // default values for other fields |
a0b4c98b VZ |
832 | rasDialParams.szPhoneNumber[0] = '\0'; |
833 | rasDialParams.szCallbackNumber[0] = '\0'; | |
834 | rasDialParams.szCallbackNumber[0] = '\0'; | |
835 | ||
a0b4c98b VZ |
836 | rasDialParams.szDomain[0] = '*'; |
837 | rasDialParams.szDomain[1] = '\0'; | |
838 | ||
839 | // apparently, this is not really necessary - passing NULL instead of the | |
840 | // phone book has the same effect | |
841 | #if 0 | |
842 | wxString phoneBook; | |
843 | if ( wxGetOsVersion() == wxWINDOWS_NT ) | |
844 | { | |
845 | // first get the length | |
846 | UINT nLen = ::GetSystemDirectory(NULL, 0); | |
847 | nLen++; | |
848 | ||
849 | if ( !::GetSystemDirectory(phoneBook.GetWriteBuf(nLen), nLen) ) | |
850 | { | |
851 | wxLogSysError(_("Cannot find the location of address book file")); | |
852 | } | |
853 | ||
854 | phoneBook.UngetWriteBuf(); | |
855 | ||
856 | // this is the default phone book | |
857 | phoneBook << "\\ras\\rasphone.pbk"; | |
858 | } | |
859 | #endif // 0 | |
860 | ||
861 | // TODO may be we should disable auto check while async dialing is in | |
862 | // progress? | |
863 | ||
864 | ms_dialer = this; | |
865 | ||
866 | DWORD dwRet = ms_pfnRasDial | |
867 | ( | |
1c25d245 VZ |
868 | NULL, // no extended features |
869 | NULL, // default phone book file (NT only) | |
a0b4c98b | 870 | &rasDialParams, |
1c25d245 VZ |
871 | 0, // use callback for notifications |
872 | async ? (void *)wxRasDialFunc // cast needed for gcc 3.1 | |
873 | : 0, // no notifications, sync operation | |
a0b4c98b VZ |
874 | &ms_hRasConnection |
875 | ); | |
876 | ||
877 | if ( dwRet != 0 ) | |
878 | { | |
bf9b6266 | 879 | // can't pass a wxWCharBuffer through ( ... ) |
a0b4c98b | 880 | wxLogError(_("Failed to %s dialup connection: %s"), |
5438a566 | 881 | wxString(async ? _("initiate") : _("establish")).c_str(), |
5b09e9c2 | 882 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
883 | |
884 | // we should still call RasHangUp() if we got a non 0 connection | |
885 | if ( ms_hRasConnection ) | |
886 | { | |
887 | ms_pfnRasHangUp(ms_hRasConnection); | |
888 | ms_hRasConnection = 0; | |
889 | } | |
890 | ||
891 | ms_dialer = NULL; | |
892 | ||
d71cc120 | 893 | return false; |
a0b4c98b VZ |
894 | } |
895 | ||
896 | // for async dialing, we're not yet connected | |
897 | if ( !async ) | |
898 | { | |
d71cc120 | 899 | ms_isConnected = true; |
a0b4c98b VZ |
900 | } |
901 | ||
d71cc120 | 902 | return true; |
a0b4c98b VZ |
903 | } |
904 | ||
905 | bool wxDialUpManagerMSW::IsDialing() const | |
906 | { | |
907 | return GetDialer() != NULL; | |
908 | } | |
909 | ||
910 | bool wxDialUpManagerMSW::CancelDialing() | |
911 | { | |
912 | if ( !GetDialer() ) | |
913 | { | |
914 | // silently ignore | |
d71cc120 | 915 | return false; |
a0b4c98b VZ |
916 | } |
917 | ||
223d09f6 | 918 | wxASSERT_MSG( ms_hRasConnection, wxT("dialing but no connection?") ); |
a0b4c98b VZ |
919 | |
920 | ms_dialer = NULL; | |
921 | ||
922 | return HangUp(); | |
923 | } | |
924 | ||
925 | bool wxDialUpManagerMSW::HangUp() | |
926 | { | |
d71cc120 | 927 | wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
928 | |
929 | // we may terminate either the connection we initiated or another one which | |
930 | // is active now | |
931 | HRASCONN hRasConn; | |
932 | if ( ms_hRasConnection ) | |
933 | { | |
934 | hRasConn = ms_hRasConnection; | |
935 | ||
936 | ms_hRasConnection = 0; | |
937 | } | |
938 | else | |
939 | { | |
940 | hRasConn = FindActiveConnection(); | |
941 | } | |
942 | ||
943 | if ( !hRasConn ) | |
944 | { | |
945 | wxLogError(_("Cannot hang up - no active dialup connection.")); | |
946 | ||
d71cc120 | 947 | return false; |
a0b4c98b VZ |
948 | } |
949 | ||
950 | DWORD dwRet = ms_pfnRasHangUp(hRasConn); | |
951 | if ( dwRet != 0 ) | |
952 | { | |
953 | wxLogError(_("Failed to terminate the dialup connection: %s"), | |
5b09e9c2 | 954 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
955 | } |
956 | ||
d71cc120 | 957 | ms_isConnected = false; |
a0b4c98b | 958 | |
d71cc120 | 959 | return true; |
a0b4c98b VZ |
960 | } |
961 | ||
2690830e VZ |
962 | bool wxDialUpManagerMSW::IsAlwaysOnline() const |
963 | { | |
19caf229 | 964 | // assume no permanent connection by default |
d71cc120 | 965 | bool isAlwaysOnline = false; |
2690830e | 966 | |
19caf229 | 967 | // try to use WinInet functions |
4f89dbc4 RL |
968 | |
969 | // NB: we could probably use wxDynamicLibrary here just as well, | |
970 | // but we allow multiple instances of wxDialUpManagerMSW so | |
971 | // we might as well use the ref counted version here too. | |
972 | ||
4522bb3e | 973 | wxDynamicLibrary hDll(_T("WININET")); |
4f89dbc4 | 974 | if ( hDll.IsLoaded() ) |
2690830e | 975 | { |
5a56be28 | 976 | typedef BOOL (WINAPI *INTERNETGETCONNECTEDSTATE)(LPDWORD, DWORD); |
2690830e VZ |
977 | INTERNETGETCONNECTEDSTATE pfnInternetGetConnectedState; |
978 | ||
979 | #define RESOLVE_FUNCTION(type, name) \ | |
4f89dbc4 | 980 | pfn##name = (type)hDll.GetSymbol(_T(#name)) |
2690830e VZ |
981 | |
982 | RESOLVE_FUNCTION(INTERNETGETCONNECTEDSTATE, InternetGetConnectedState); | |
983 | ||
984 | if ( pfnInternetGetConnectedState ) | |
985 | { | |
986 | DWORD flags = 0; | |
987 | if ( pfnInternetGetConnectedState(&flags, 0 /* reserved */) ) | |
988 | { | |
989 | // there is some connection to the net, see of which type | |
19caf229 VZ |
990 | isAlwaysOnline = (flags & (INTERNET_CONNECTION_LAN | |
991 | INTERNET_CONNECTION_PROXY)) != 0; | |
2690830e | 992 | } |
19caf229 | 993 | //else: no Internet connection at all |
2690830e VZ |
994 | } |
995 | } | |
996 | ||
19caf229 | 997 | return isAlwaysOnline; |
2690830e VZ |
998 | } |
999 | ||
a0b4c98b VZ |
1000 | bool wxDialUpManagerMSW::IsOnline() const |
1001 | { | |
d71cc120 | 1002 | wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b | 1003 | |
90e9494a VZ |
1004 | if ( IsAlwaysOnline() ) |
1005 | { | |
1006 | // always => now | |
1007 | return true; | |
1008 | } | |
1009 | ||
a0b4c98b VZ |
1010 | if ( ms_userSpecifiedOnlineStatus != -1 ) |
1011 | { | |
1012 | // user specified flag overrides our logic | |
1013 | return ms_userSpecifiedOnlineStatus != 0; | |
1014 | } | |
1015 | else | |
1016 | { | |
d71cc120 | 1017 | // return true if there is at least one active connection |
a0b4c98b VZ |
1018 | return FindActiveConnection() != 0; |
1019 | } | |
1020 | } | |
1021 | ||
1022 | void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline) | |
1023 | { | |
223d09f6 | 1024 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1025 | |
1026 | ms_userSpecifiedOnlineStatus = isOnline; | |
1027 | } | |
1028 | ||
1029 | bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds) | |
1030 | { | |
d71cc120 | 1031 | wxCHECK_MSG( IsOk(), false, wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b | 1032 | |
6bba111c VZ |
1033 | if ( m_autoCheckLevel++ ) |
1034 | { | |
1035 | // already checking | |
d71cc120 | 1036 | return true; |
6bba111c VZ |
1037 | } |
1038 | ||
a0b4c98b VZ |
1039 | bool ok = ms_pfnRasConnectionNotification != 0; |
1040 | ||
1041 | if ( ok ) | |
1042 | { | |
1043 | // we're running under NT 4.0, Windows 98 or later and can use | |
1044 | // RasConnectionNotification() to be notified by a secondary thread | |
1045 | ||
1046 | // first, see if we don't have this thread already running | |
1047 | if ( m_hThread != 0 ) | |
1048 | { | |
6bba111c | 1049 | if ( ::ResumeThread(m_hThread) != (DWORD)-1 ) |
d71cc120 | 1050 | return true; |
a0b4c98b | 1051 | |
6bba111c VZ |
1052 | // we're leaving a zombie thread... but what else can we do? |
1053 | wxLogLastError(wxT("ResumeThread(RasThread)")); | |
a0b4c98b | 1054 | |
d71cc120 | 1055 | ok = false; |
a0b4c98b VZ |
1056 | } |
1057 | } | |
1058 | ||
1059 | // create all the stuff we need to be notified about RAS connection | |
1060 | // status change | |
1061 | ||
1062 | if ( ok ) | |
1063 | { | |
1064 | // first create an event to wait on | |
d71cc120 | 1065 | m_data->hEventRas = ::CreateEvent |
97247d36 | 1066 | ( |
a0b4c98b | 1067 | NULL, // security attribute (default) |
97247d36 | 1068 | FALSE, // manual reset (no, it is automatic) |
a0b4c98b VZ |
1069 | FALSE, // initial state (not signaled) |
1070 | NULL // name (no) | |
97247d36 VZ |
1071 | ); |
1072 | if ( !m_data->hEventRas ) | |
a0b4c98b | 1073 | { |
f6bcfd97 | 1074 | wxLogLastError(wxT("CreateEvent(RasStatus)")); |
a0b4c98b | 1075 | |
d71cc120 | 1076 | ok = false; |
a0b4c98b VZ |
1077 | } |
1078 | } | |
1079 | ||
1080 | if ( ok ) | |
1081 | { | |
97247d36 VZ |
1082 | // create the event we use to quit the thread: using a manual event |
1083 | // here avoids problems with missing the event if wxDialUpManagerMSW | |
1084 | // is created and destroyed immediately, before wxRasStatusWindowProc | |
1085 | // starts waiting on the event | |
d71cc120 | 1086 | m_data->hEventQuit = ::CreateEvent |
97247d36 VZ |
1087 | ( |
1088 | NULL, // default security | |
1089 | TRUE, // manual event | |
1090 | FALSE, // initially non signalled | |
1091 | NULL // nameless | |
1092 | ); | |
1093 | if ( !m_data->hEventQuit ) | |
a0b4c98b | 1094 | { |
f6bcfd97 | 1095 | wxLogLastError(wxT("CreateEvent(RasThreadQuit)")); |
a0b4c98b VZ |
1096 | |
1097 | CleanUpThreadData(); | |
1098 | ||
d71cc120 | 1099 | ok = false; |
a0b4c98b VZ |
1100 | } |
1101 | } | |
1102 | ||
2690830e | 1103 | if ( ok && !ms_hwndRas ) |
a0b4c98b VZ |
1104 | { |
1105 | // create a hidden window to receive notification about connections | |
1106 | // status change | |
8563c6f1 VZ |
1107 | ms_hwndRas = wxCreateHiddenWindow |
1108 | ( | |
1109 | &gs_classForDialUpWindow, | |
1110 | wxMSWDIALUP_WNDCLASSNAME, | |
1111 | wxRasStatusWindowProc | |
1112 | ); | |
2690830e | 1113 | if ( !ms_hwndRas ) |
a0b4c98b | 1114 | { |
f6bcfd97 | 1115 | wxLogLastError(wxT("CreateWindow(RasHiddenWindow)")); |
a0b4c98b VZ |
1116 | |
1117 | CleanUpThreadData(); | |
1118 | ||
d71cc120 | 1119 | ok = false; |
a0b4c98b | 1120 | } |
a0b4c98b VZ |
1121 | } |
1122 | ||
97247d36 | 1123 | m_data->hWnd = ms_hwndRas; |
2690830e | 1124 | |
a0b4c98b VZ |
1125 | if ( ok ) |
1126 | { | |
1127 | // start the secondary thread | |
97247d36 | 1128 | m_data->dialUpManager = this; |
a0b4c98b VZ |
1129 | |
1130 | DWORD tid; | |
1131 | m_hThread = CreateThread | |
1132 | ( | |
1133 | NULL, | |
1134 | 0, | |
1135 | (LPTHREAD_START_ROUTINE)wxRasMonitorThread, | |
97247d36 | 1136 | (void *)m_data, |
a0b4c98b VZ |
1137 | 0, |
1138 | &tid | |
1139 | ); | |
1140 | ||
1141 | if ( !m_hThread ) | |
1142 | { | |
f6bcfd97 | 1143 | wxLogLastError(wxT("CreateThread(RasStatusThread)")); |
a0b4c98b VZ |
1144 | |
1145 | CleanUpThreadData(); | |
1146 | } | |
1147 | } | |
1148 | ||
1149 | if ( ok ) | |
1150 | { | |
1151 | // start receiving RAS notifications | |
1152 | DWORD dwRet = ms_pfnRasConnectionNotification | |
1153 | ( | |
1154 | (HRASCONN)INVALID_HANDLE_VALUE, | |
97247d36 | 1155 | m_data->hEventRas, |
a0b4c98b VZ |
1156 | 3 /* RASCN_Connection | RASCN_Disconnection */ |
1157 | ); | |
1158 | ||
1159 | if ( dwRet != 0 ) | |
1160 | { | |
223d09f6 | 1161 | wxLogDebug(wxT("RasConnectionNotification() failed: %s"), |
5b09e9c2 | 1162 | GetErrorString(dwRet).c_str()); |
a0b4c98b VZ |
1163 | |
1164 | CleanUpThreadData(); | |
1165 | } | |
1166 | else | |
1167 | { | |
d71cc120 | 1168 | return true; |
a0b4c98b VZ |
1169 | } |
1170 | } | |
1171 | ||
1172 | // we're running under Windows 95 and have to poll ourselves | |
1173 | // (or, alternatively, the code above for NT/98 failed) | |
1174 | m_timerStatusPolling.Stop(); | |
1175 | if ( nSeconds == 0 ) | |
1176 | { | |
1177 | // default value | |
1178 | nSeconds = 60; | |
1179 | } | |
1180 | m_timerStatusPolling.Start(nSeconds * 1000); | |
1181 | ||
d71cc120 | 1182 | return true; |
a0b4c98b VZ |
1183 | } |
1184 | ||
1185 | void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus() | |
1186 | { | |
223d09f6 | 1187 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b | 1188 | |
6bba111c VZ |
1189 | if ( --m_autoCheckLevel ) |
1190 | { | |
1191 | // still checking | |
1192 | return; | |
1193 | } | |
1194 | ||
a0b4c98b VZ |
1195 | if ( m_hThread ) |
1196 | { | |
1197 | // we have running secondary thread, it's just enough to suspend it | |
1198 | if ( SuspendThread(m_hThread) == (DWORD)-1 ) | |
1199 | { | |
f6bcfd97 | 1200 | wxLogLastError(wxT("SuspendThread(RasThread)")); |
a0b4c98b VZ |
1201 | } |
1202 | } | |
1203 | else | |
1204 | { | |
1205 | // even simpler - just stop the timer | |
1206 | m_timerStatusPolling.Stop(); | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | // ---------------------------------------------------------------------------- | |
1211 | // stubs which don't do anything in MSW version | |
1212 | // ---------------------------------------------------------------------------- | |
1213 | ||
1214 | void wxDialUpManagerMSW::SetWellKnownHost(const wxString& WXUNUSED(hostname), | |
1215 | int WXUNUSED(port)) | |
1216 | { | |
223d09f6 | 1217 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1218 | |
1219 | // nothing to do - we don't use this | |
1220 | } | |
1221 | ||
1222 | void wxDialUpManagerMSW::SetConnectCommand(const wxString& WXUNUSED(dial), | |
1223 | const wxString& WXUNUSED(hangup)) | |
1224 | { | |
223d09f6 | 1225 | wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") ); |
a0b4c98b VZ |
1226 | |
1227 | // nothing to do - we don't use this | |
1228 | } | |
1229 | ||
1230 | // ---------------------------------------------------------------------------- | |
1231 | // callbacks | |
1232 | // ---------------------------------------------------------------------------- | |
1233 | ||
1234 | static DWORD wxRasMonitorThread(wxRasThreadData *data) | |
1235 | { | |
1236 | HANDLE handles[2]; | |
1237 | handles[0] = data->hEventRas; | |
1238 | handles[1] = data->hEventQuit; | |
1239 | ||
d71cc120 | 1240 | bool cont = true; |
a0b4c98b VZ |
1241 | while ( cont ) |
1242 | { | |
d71cc120 | 1243 | DWORD dwRet = ::WaitForMultipleObjects(2, handles, FALSE, INFINITE); |
a0b4c98b VZ |
1244 | |
1245 | switch ( dwRet ) | |
1246 | { | |
1247 | case WAIT_OBJECT_0: | |
1248 | // RAS connection status changed | |
1249 | SendMessage(data->hWnd, wxWM_RAS_STATUS_CHANGED, | |
1250 | 0, (LPARAM)data); | |
1251 | break; | |
1252 | ||
1253 | case WAIT_OBJECT_0 + 1: | |
d71cc120 | 1254 | cont = false; |
a0b4c98b VZ |
1255 | break; |
1256 | ||
97247d36 VZ |
1257 | default: |
1258 | wxFAIL_MSG( _T("unexpected return of WaitForMultipleObjects()") ); | |
1259 | // fall through | |
1260 | ||
a0b4c98b | 1261 | case WAIT_FAILED: |
97247d36 VZ |
1262 | #ifdef __WXDEBUG__ |
1263 | // using wxLogLastError() from here is dangerous: we risk to | |
1264 | // deadlock the main thread if wxLog sends output to GUI | |
1265 | DWORD err = GetLastError(); | |
141f4958 WS |
1266 | wxMessageOutputDebug dbg; |
1267 | dbg.Printf | |
97247d36 VZ |
1268 | ( |
1269 | wxT("WaitForMultipleObjects(RasMonitor) failed: 0x%08lx (%s)"), | |
1270 | err, | |
1271 | wxSysErrorMsg(err) | |
1272 | ); | |
1273 | #endif // __WXDEBUG__ | |
1274 | ||
1275 | // no sense in continuing, who knows if the handles we're | |
1276 | // waiting for even exist yet... | |
1277 | return (DWORD)-1; | |
a0b4c98b VZ |
1278 | } |
1279 | } | |
1280 | ||
97247d36 VZ |
1281 | // we don't need it any more now and if this thread ran, it is our |
1282 | // responsability to free the data | |
1283 | delete data; | |
1284 | ||
a0b4c98b VZ |
1285 | return 0; |
1286 | } | |
1287 | ||
1288 | static LRESULT APIENTRY wxRasStatusWindowProc(HWND hWnd, UINT message, | |
1289 | WPARAM wParam, LPARAM lParam) | |
1290 | { | |
cd6af1cb | 1291 | switch ( message ) |
a0b4c98b | 1292 | { |
cd6af1cb VZ |
1293 | case wxWM_RAS_STATUS_CHANGED: |
1294 | { | |
1295 | wxRasThreadData *data = (wxRasThreadData *)lParam; | |
1296 | data->dialUpManager->OnConnectStatusChange(); | |
1297 | } | |
1298 | break; | |
2690830e | 1299 | |
cd6af1cb VZ |
1300 | case wxWM_RAS_DIALING_PROGRESS: |
1301 | { | |
1302 | wxDialUpManagerMSW *dialMan = wxDialUpManagerMSW::GetDialer(); | |
1303 | ||
1304 | dialMan->OnDialProgress((RASCONNSTATE)wParam, lParam); | |
1305 | } | |
1306 | break; | |
1307 | ||
1308 | default: | |
1309 | return ::DefWindowProc(hWnd, message, wParam, lParam); | |
2690830e | 1310 | } |
a0b4c98b VZ |
1311 | |
1312 | return 0; | |
1313 | } | |
1314 | ||
2eb10e2a | 1315 | static void WINAPI wxRasDialFunc(UINT WXUNUSED(unMsg), |
a0b4c98b VZ |
1316 | RASCONNSTATE rasconnstate, |
1317 | DWORD dwError) | |
1318 | { | |
1319 | wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer(); | |
1320 | ||
223d09f6 | 1321 | wxCHECK_RET( dialUpManager, wxT("who started to dial then?") ); |
a0b4c98b | 1322 | |
2a1f999f | 1323 | SendMessage(wxDialUpManagerMSW::GetRasWindow(), wxWM_RAS_DIALING_PROGRESS, |
2690830e | 1324 | rasconnstate, dwError); |
a0b4c98b VZ |
1325 | } |
1326 | ||
1c25d245 VZ |
1327 | #endif // __BORLANDC__ |
1328 | ||
a0b4c98b | 1329 | #endif // wxUSE_DIALUP_MANAGER |
4f89dbc4 | 1330 |