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