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