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