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