]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dialup.cpp
Corrected wxFormatConverter for strings which need
[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++: no wininet.h
52 #if !defined(__BORLANDC__) && \
53 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION(0, 5)) && \
54 !defined(__GNUWIN32_OLD__) && \
55 !defined(__WATCOMC__) && \
56 (!defined(__VISUALC__) || (__VISUALC__ >= 1020))
57
58 #include <ras.h>
59 #include <raserror.h>
60
61 #include <wininet.h>
62
63 // Not in VC++ 5
64 #ifndef INTERNET_CONNECTION_LAN
65 #define INTERNET_CONNECTION_LAN 2
66 #endif
67 #ifndef INTERNET_CONNECTION_PROXY
68 #define INTERNET_CONNECTION_PROXY 4
69 #endif
70
71 // ----------------------------------------------------------------------------
72 // constants
73 // ----------------------------------------------------------------------------
74
75 // this message is sent by the secondary thread when RAS status changes
76 #define wxWM_RAS_STATUS_CHANGED (WM_USER + 10010)
77 #define wxWM_RAS_DIALING_PROGRESS (WM_USER + 10011)
78
79 // ----------------------------------------------------------------------------
80 // types
81 // ----------------------------------------------------------------------------
82
83 // the signatures of RAS functions: all this is quite heavy, but we must do it
84 // to allow running wxWin programs on machine which don't have RAS installed
85 // (this does exist) - if we link with rasapi32.lib, the program will fail on
86 // startup because of the missing DLL...
87
88 #ifndef UNICODE
89 typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCSTR, LPRASDIALPARAMSA, DWORD, LPVOID, LPHRASCONN );
90 typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNA, LPDWORD, LPDWORD );
91 typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCSTR, LPCSTR, LPRASENTRYNAMEA, LPDWORD, LPDWORD );
92 typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSA );
93 typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPSTR, DWORD );
94 typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN );
95 typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD );
96 typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCSTR );
97 typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCSTR, LPCSTR );
98 typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, BOOL );
99 typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCSTR, LPRASDIALPARAMSA, LPBOOL );
100 typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOA, LPDWORD, LPDWORD );
101 typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOA, LPDWORD );
102 typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, LPDWORD, LPBYTE, LPDWORD );
103 typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCSTR, LPCSTR, LPRASENTRYA, DWORD, LPBYTE, DWORD );
104 typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCSTR, LPCSTR, LPCSTR );
105 typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCSTR, LPCSTR );
106 typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCSTR, LPCSTR );
107 typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD );
108
109 static const wxChar gs_funcSuffix = _T('A');
110 #else // Unicode
111 typedef DWORD (APIENTRY * RASDIAL)( LPRASDIALEXTENSIONS, LPCWSTR, LPRASDIALPARAMSW, DWORD, LPVOID, LPHRASCONN );
112 typedef DWORD (APIENTRY * RASENUMCONNECTIONS)( LPRASCONNW, LPDWORD, LPDWORD );
113 typedef DWORD (APIENTRY * RASENUMENTRIES)( LPCWSTR, LPCWSTR, LPRASENTRYNAMEW, LPDWORD, LPDWORD );
114 typedef DWORD (APIENTRY * RASGETCONNECTSTATUS)( HRASCONN, LPRASCONNSTATUSW );
115 typedef DWORD (APIENTRY * RASGETERRORSTRING)( UINT, LPWSTR, DWORD );
116 typedef DWORD (APIENTRY * RASHANGUP)( HRASCONN );
117 typedef DWORD (APIENTRY * RASGETPROJECTIONINFO)( HRASCONN, RASPROJECTION, LPVOID, LPDWORD );
118 typedef DWORD (APIENTRY * RASCREATEPHONEBOOKENTRY)( HWND, LPCWSTR );
119 typedef DWORD (APIENTRY * RASEDITPHONEBOOKENTRY)( HWND, LPCWSTR, LPCWSTR );
120 typedef DWORD (APIENTRY * RASSETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, BOOL );
121 typedef DWORD (APIENTRY * RASGETENTRYDIALPARAMS)( LPCWSTR, LPRASDIALPARAMSW, LPBOOL );
122 typedef DWORD (APIENTRY * RASENUMDEVICES)( LPRASDEVINFOW, LPDWORD, LPDWORD );
123 typedef DWORD (APIENTRY * RASGETCOUNTRYINFO)( LPRASCTRYINFOW, LPDWORD );
124 typedef DWORD (APIENTRY * RASGETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, LPDWORD, LPBYTE, LPDWORD );
125 typedef DWORD (APIENTRY * RASSETENTRYPROPERTIES)( LPCWSTR, LPCWSTR, LPRASENTRYW, DWORD, LPBYTE, DWORD );
126 typedef DWORD (APIENTRY * RASRENAMEENTRY)( LPCWSTR, LPCWSTR, LPCWSTR );
127 typedef DWORD (APIENTRY * RASDELETEENTRY)( LPCWSTR, LPCWSTR );
128 typedef DWORD (APIENTRY * RASVALIDATEENTRYNAME)( LPCWSTR, LPCWSTR );
129 typedef DWORD (APIENTRY * RASCONNECTIONNOTIFICATION)( HRASCONN, HANDLE, DWORD );
130
131 static const wxChar gs_funcSuffix = _T('W');
132 #endif // ASCII/Unicode
133
134 // structure passed to the secondary thread
135 struct WXDLLEXPORT wxRasThreadData
136 {
137 wxRasThreadData()
138 {
139 hWnd = 0;
140 hEventRas = hEventQuit = INVALID_HANDLE_VALUE;
141 dialUpManager = NULL;
142 }
143
144 HWND hWnd; // window to send notifications to
145 HANDLE hEventRas, // event which RAS signals when status changes
146 hEventQuit; // event which we signal when we terminate
147
148 class WXDLLEXPORT wxDialUpManagerMSW *dialUpManager; // the owner
149 };
150
151 // ----------------------------------------------------------------------------
152 // wxDialUpManager class for MSW
153 // ----------------------------------------------------------------------------
154
155 class WXDLLEXPORT wxDialUpManagerMSW : public wxDialUpManager
156 {
157 public:
158 // ctor & dtor
159 wxDialUpManagerMSW();
160 virtual ~wxDialUpManagerMSW();
161
162 // implement base class pure virtuals
163 virtual bool IsOk() const;
164 virtual size_t GetISPNames(wxArrayString& names) const;
165 virtual bool Dial(const wxString& nameOfISP,
166 const wxString& username,
167 const wxString& password,
168 bool async);
169 virtual bool IsDialing() const;
170 virtual bool CancelDialing();
171 virtual bool HangUp();
172 virtual bool IsAlwaysOnline() const;
173 virtual bool IsOnline() const;
174 virtual void SetOnlineStatus(bool isOnline = TRUE);
175 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds);
176 virtual void DisableAutoCheckOnlineStatus();
177 virtual void SetWellKnownHost(const wxString& hostname, int port);
178 virtual void SetConnectCommand(const wxString& commandDial,
179 const wxString& commandHangup);
180
181 // for RasTimer
182 void CheckRasStatus();
183
184 // for wxRasStatusWindowProc
185 void OnConnectStatusChange();
186 void OnDialProgress(RASCONNSTATE rasconnstate, DWORD dwError);
187
188 // for wxRasDialFunc
189 static HWND GetRasWindow() { return ms_hwndRas; }
190 static wxDialUpManagerMSW *GetDialer() { return ms_dialer; }
191
192 private:
193 // return the error string for the given RAS error code
194 static wxString GetErrorString(DWORD error);
195
196 // find the (first) handle of the active connection
197 static HRASCONN FindActiveConnection();
198
199 // notify the application about status change
200 void NotifyApp(bool connected, bool fromOurselves = FALSE) const;
201
202 // destroy the thread data and the thread itself
203 void CleanUpThreadData();
204
205 // timer used for polling RAS status
206 class WXDLLEXPORT RasTimer : public wxTimer
207 {
208 public:
209 RasTimer(wxDialUpManagerMSW *dialUpManager)
210 { m_dialUpManager = dialUpManager; }
211
212 virtual void Notify() { m_dialUpManager->CheckRasStatus(); }
213
214 private:
215 wxDialUpManagerMSW *m_dialUpManager;
216 } m_timerStatusPolling;
217
218 // thread handle for the thread sitting on connection change event
219 HANDLE m_hThread;
220
221 // data used by this thread and our hidden window to send messages between
222 // each other
223 wxRasThreadData m_data;
224
225 // the handle of rasapi32.dll when it's loaded
226 wxPluginManager m_dllRas;
227
228 // the hidden window we use for passing messages between threads
229 static HWND ms_hwndRas;
230
231 // the handle of the connection we initiated or 0 if none
232 static HRASCONN ms_hRasConnection;
233
234 // FIXME: There is probably no reason these really need to
235 // be static anymore since the dll refcounting is
236 // handled by wxPluginManager now. Whether or not
237 // we still _want_ them to be static is another
238 // issue entirely..
239
240 // the pointers to RAS functions
241 static RASDIAL ms_pfnRasDial;
242 static RASENUMCONNECTIONS ms_pfnRasEnumConnections;
243 static RASENUMENTRIES ms_pfnRasEnumEntries;
244 static RASGETCONNECTSTATUS ms_pfnRasGetConnectStatus;
245 static RASGETERRORSTRING ms_pfnRasGetErrorString;
246 static RASHANGUP ms_pfnRasHangUp;
247 static RASGETPROJECTIONINFO ms_pfnRasGetProjectionInfo;
248 static RASCREATEPHONEBOOKENTRY ms_pfnRasCreatePhonebookEntry;
249 static RASEDITPHONEBOOKENTRY ms_pfnRasEditPhonebookEntry;
250 static RASSETENTRYDIALPARAMS ms_pfnRasSetEntryDialParams;
251 static RASGETENTRYDIALPARAMS ms_pfnRasGetEntryDialParams;
252 static RASENUMDEVICES ms_pfnRasEnumDevices;
253 static RASGETCOUNTRYINFO ms_pfnRasGetCountryInfo;
254 static RASGETENTRYPROPERTIES ms_pfnRasGetEntryProperties;
255 static RASSETENTRYPROPERTIES ms_pfnRasSetEntryProperties;
256 static RASRENAMEENTRY ms_pfnRasRenameEntry;
257 static RASDELETEENTRY ms_pfnRasDeleteEntry;
258 static RASVALIDATEENTRYNAME ms_pfnRasValidateEntryName;
259
260 // this function is not supported by Win95
261 static RASCONNECTIONNOTIFICATION ms_pfnRasConnectionNotification;
262
263 // if this flag is different from -1, it overrides IsOnline()
264 static int ms_userSpecifiedOnlineStatus;
265
266 // this flag tells us if we're online
267 static int ms_isConnected;
268
269 // this flag tells us whether a call to RasDial() is in progress
270 static wxDialUpManagerMSW *ms_dialer;
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 wxPluginManager 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 ( ms_userSpecifiedOnlineStatus != -1 )
965 {
966 // user specified flag overrides our logic
967 return ms_userSpecifiedOnlineStatus != 0;
968 }
969 else
970 {
971 // return TRUE if there is at least one active connection
972 return FindActiveConnection() != 0;
973 }
974 }
975
976 void wxDialUpManagerMSW::SetOnlineStatus(bool isOnline)
977 {
978 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
979
980 ms_userSpecifiedOnlineStatus = isOnline;
981 }
982
983 bool wxDialUpManagerMSW::EnableAutoCheckOnlineStatus(size_t nSeconds)
984 {
985 wxCHECK_MSG( IsOk(), FALSE, wxT("using uninitialized wxDialUpManager") );
986
987 bool ok = ms_pfnRasConnectionNotification != 0;
988
989 if ( ok )
990 {
991 // we're running under NT 4.0, Windows 98 or later and can use
992 // RasConnectionNotification() to be notified by a secondary thread
993
994 // first, see if we don't have this thread already running
995 if ( m_hThread != 0 )
996 {
997 DWORD dwSuspendCount = 2;
998 while ( dwSuspendCount > 1 )
999 {
1000 dwSuspendCount = ResumeThread(m_hThread);
1001 if ( dwSuspendCount == (DWORD)-1 )
1002 {
1003 wxLogLastError(wxT("ResumeThread(RasThread)"));
1004
1005 ok = FALSE;
1006 }
1007 }
1008
1009 if ( ok )
1010 {
1011 return TRUE;
1012 }
1013 }
1014 }
1015
1016 // create all the stuff we need to be notified about RAS connection
1017 // status change
1018
1019 if ( ok )
1020 {
1021 // first create an event to wait on
1022 m_data.hEventRas = CreateEvent
1023 (
1024 NULL, // security attribute (default)
1025 FALSE, // manual reset (not)
1026 FALSE, // initial state (not signaled)
1027 NULL // name (no)
1028 );
1029 if ( !m_data.hEventRas )
1030 {
1031 wxLogLastError(wxT("CreateEvent(RasStatus)"));
1032
1033 ok = FALSE;
1034 }
1035 }
1036
1037 if ( ok )
1038 {
1039 // create the event we use to quit the thread
1040 m_data.hEventQuit = CreateEvent(NULL, FALSE, FALSE, NULL);
1041 if ( !m_data.hEventQuit )
1042 {
1043 wxLogLastError(wxT("CreateEvent(RasThreadQuit)"));
1044
1045 CleanUpThreadData();
1046
1047 ok = FALSE;
1048 }
1049 }
1050
1051 if ( ok && !ms_hwndRas )
1052 {
1053 // create a hidden window to receive notification about connections
1054 // status change
1055 extern const wxChar *wxCanvasClassName;
1056 ms_hwndRas = ::CreateWindow(wxCanvasClassName, NULL,
1057 0, 0, 0, 0,
1058 0, NULL,
1059 (HMENU)NULL, wxGetInstance(), 0);
1060 if ( !ms_hwndRas )
1061 {
1062 wxLogLastError(wxT("CreateWindow(RasHiddenWindow)"));
1063
1064 CleanUpThreadData();
1065
1066 ok = FALSE;
1067 }
1068
1069 // and subclass it
1070 FARPROC windowProc = MakeProcInstance
1071 (
1072 (FARPROC)wxRasStatusWindowProc,
1073 wxGetInstance()
1074 );
1075
1076 ::SetWindowLong(ms_hwndRas, GWL_WNDPROC, (LONG) windowProc);
1077 }
1078
1079 m_data.hWnd = ms_hwndRas;
1080
1081 if ( ok )
1082 {
1083 // start the secondary thread
1084 m_data.dialUpManager = this;
1085
1086 DWORD tid;
1087 m_hThread = CreateThread
1088 (
1089 NULL,
1090 0,
1091 (LPTHREAD_START_ROUTINE)wxRasMonitorThread,
1092 (void *)&m_data,
1093 0,
1094 &tid
1095 );
1096
1097 if ( !m_hThread )
1098 {
1099 wxLogLastError(wxT("CreateThread(RasStatusThread)"));
1100
1101 CleanUpThreadData();
1102 }
1103 }
1104
1105 if ( ok )
1106 {
1107 // start receiving RAS notifications
1108 DWORD dwRet = ms_pfnRasConnectionNotification
1109 (
1110 (HRASCONN)INVALID_HANDLE_VALUE,
1111 m_data.hEventRas,
1112 3 /* RASCN_Connection | RASCN_Disconnection */
1113 );
1114
1115 if ( dwRet != 0 )
1116 {
1117 wxLogDebug(wxT("RasConnectionNotification() failed: %s"),
1118 GetErrorString(dwRet).c_str());
1119
1120 CleanUpThreadData();
1121 }
1122 else
1123 {
1124 return TRUE;
1125 }
1126 }
1127
1128 // we're running under Windows 95 and have to poll ourselves
1129 // (or, alternatively, the code above for NT/98 failed)
1130 m_timerStatusPolling.Stop();
1131 if ( nSeconds == 0 )
1132 {
1133 // default value
1134 nSeconds = 60;
1135 }
1136 m_timerStatusPolling.Start(nSeconds * 1000);
1137
1138 return TRUE;
1139 }
1140
1141 void wxDialUpManagerMSW::DisableAutoCheckOnlineStatus()
1142 {
1143 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1144
1145 if ( m_hThread )
1146 {
1147 // we have running secondary thread, it's just enough to suspend it
1148 if ( SuspendThread(m_hThread) == (DWORD)-1 )
1149 {
1150 wxLogLastError(wxT("SuspendThread(RasThread)"));
1151 }
1152 }
1153 else
1154 {
1155 // even simpler - just stop the timer
1156 m_timerStatusPolling.Stop();
1157 }
1158 }
1159
1160 // ----------------------------------------------------------------------------
1161 // stubs which don't do anything in MSW version
1162 // ----------------------------------------------------------------------------
1163
1164 void wxDialUpManagerMSW::SetWellKnownHost(const wxString& WXUNUSED(hostname),
1165 int WXUNUSED(port))
1166 {
1167 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1168
1169 // nothing to do - we don't use this
1170 }
1171
1172 void wxDialUpManagerMSW::SetConnectCommand(const wxString& WXUNUSED(dial),
1173 const wxString& WXUNUSED(hangup))
1174 {
1175 wxCHECK_RET( IsOk(), wxT("using uninitialized wxDialUpManager") );
1176
1177 // nothing to do - we don't use this
1178 }
1179
1180 // ----------------------------------------------------------------------------
1181 // callbacks
1182 // ----------------------------------------------------------------------------
1183
1184 static DWORD wxRasMonitorThread(wxRasThreadData *data)
1185 {
1186 HANDLE handles[2];
1187 handles[0] = data->hEventRas;
1188 handles[1] = data->hEventQuit;
1189
1190 bool cont = TRUE;
1191 while ( cont )
1192 {
1193 DWORD dwRet = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
1194
1195 switch ( dwRet )
1196 {
1197 case WAIT_OBJECT_0:
1198 // RAS connection status changed
1199 SendMessage(data->hWnd, wxWM_RAS_STATUS_CHANGED,
1200 0, (LPARAM)data);
1201 break;
1202
1203 case WAIT_OBJECT_0 + 1:
1204 cont = FALSE;
1205 break;
1206
1207 case WAIT_FAILED:
1208 wxLogLastError(wxT("WaitForMultipleObjects(RasMonitor)"));
1209 break;
1210 }
1211 }
1212
1213 return 0;
1214 }
1215
1216 static LRESULT APIENTRY wxRasStatusWindowProc(HWND hWnd, UINT message,
1217 WPARAM wParam, LPARAM lParam)
1218 {
1219 switch ( message )
1220 {
1221 case wxWM_RAS_STATUS_CHANGED:
1222 {
1223 wxRasThreadData *data = (wxRasThreadData *)lParam;
1224 data->dialUpManager->OnConnectStatusChange();
1225 }
1226 break;
1227
1228 case wxWM_RAS_DIALING_PROGRESS:
1229 {
1230 wxDialUpManagerMSW *dialMan = wxDialUpManagerMSW::GetDialer();
1231
1232 dialMan->OnDialProgress((RASCONNSTATE)wParam, lParam);
1233 }
1234 break;
1235
1236 default:
1237 return ::DefWindowProc(hWnd, message, wParam, lParam);
1238 }
1239
1240 return 0;
1241 }
1242
1243 static void WINAPI wxRasDialFunc(UINT unMsg,
1244 RASCONNSTATE rasconnstate,
1245 DWORD dwError)
1246 {
1247 wxDialUpManagerMSW *dialUpManager = wxDialUpManagerMSW::GetDialer();
1248
1249 wxCHECK_RET( dialUpManager, wxT("who started to dial then?") );
1250
1251 SendMessage(dialUpManager->GetRasWindow(), wxWM_RAS_DIALING_PROGRESS,
1252 rasconnstate, dwError);
1253 }
1254
1255 #endif // __BORLANDC__
1256
1257 #endif // wxUSE_DIALUP_MANAGER
1258