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