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