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