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