1 // -*- c++ -*- ///////////////////////////////////////////////////////////////
3 // Purpose: Network related wxWindows classes and functions
4 // Author: Karsten Ballüder
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
14 #if wxUSE_DIALUP_MANAGER
20 #include "wx/string.h"
24 #include "wx/filefn.h"
33 #define __STRICT_ANSI__
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
39 // ----------------------------------------------------------------------------
40 // A class which groups functions dealing with connecting to the network from a
41 // workstation using dial-up access to the net. There is at most one instance
42 // of this class in the program accessed via GetDialUpManager().
43 // ----------------------------------------------------------------------------
47 * 1. more configurability for Unix: i.e. how to initiate the connection, how
48 * to check for online status, &c.
49 * 2. add a "long Dial(long connectionId = -1)" function which asks the user
50 * about which connection to dial (this may be done using native dialogs
51 * under NT, need generic dialogs for all others) and returns the identifier
52 * of the selected connection (it's opaque to the application) - it may be
53 * reused later to dial the same connection later (or use strings instead of
55 * 3. add an async version of dialing functions which notify the caller about
56 * the progress (or may be even start another thread to monitor it)
57 * 4. the static creation/accessor functions are not MT-safe - but is this
58 * really crucial? I think we may suppose they're always called from the
62 class WXDLLEXPORT wxDialUpManagerImpl
: public wxDialUpManager
67 m_IsOnline
= -1; // unknown
69 m_CanUseIfconfig
= -1; // unknown
70 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
74 /** Could the dialup manager be initialized correctly? If this function
75 returns FALSE, no other functions will work neither, so it's a good idea
76 to call this function and check its result before calling any other
77 wxDialUpManager methods.
79 virtual bool IsOk() const
82 /** The simplest way to initiate a dial up: this function dials the given
83 ISP (exact meaning of the parameter depends on the platform), returns
84 TRUE on success or FALSE on failure and logs the appropriate error
85 message in the latter case.
86 @param nameOfISP optional paramater for dial program
87 @param username unused
88 @param password unused
90 virtual bool Dial(const wxString
& nameOfISP
,
91 const wxString
& WXUNUSED(username
),
92 const wxString
& WXUNUSED(password
));
94 /// Hang up the currently active dial up connection.
95 virtual bool HangUp();
97 // returns TRUE if the computer is connected to the network: under Windows,
98 // this just means that a RAS connection exists, under Unix we check that
99 // the "well-known host" (as specified by SetWellKnownHost) is reachable
100 virtual bool IsOnline() const
102 if( (! m_timer
) // we are not polling, so test now:
106 return m_IsOnline
!= 0;
109 // sometimes the built-in logic for determining the online status may fail,
110 // so, in general, the user should be allowed to override it. This function
111 // allows to forcefully set the online status - whatever our internal
112 // algorithm may think about it.
113 virtual void SetOnlineStatus(bool isOnline
= TRUE
)
114 { m_IsOnline
= isOnline
; }
116 // set misc wxDialUpManager options
117 // --------------------------------
119 // enable automatical checks for the connection status and sending of
120 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
121 // parameter is only for Unix where we do the check manually: under
122 // Windows, the notification about the change of connection status is
125 // Returns FALSE if couldn't set up automatic check for online status.
126 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
128 // disable automatic check for connection status change - notice that the
129 // wxEVT_DIALUP_XXX events won't be sent any more neither.
130 virtual void DisableAutoCheckOnlineStatus();
132 // under Unix, the value of well-known host is used to check whether we're
133 // connected to the internet. It's unused under Windows, but this function
134 // is always safe to call. The default value is www.yahoo.com.
135 virtual void SetWellKnownHost(const wxString
& hostname
,
137 /** Sets the commands to start up the network and to hang up
138 again. Used by the Unix implementations only.
140 virtual void SetConnectCommand(const wxString
&command
, const wxString
&hupcmd
)
141 { m_ConnectCommand
= command
; m_HangUpCommand
= hupcmd
; }
144 /// -1: don´t know, 0 = no, 1 = yes
147 /// Can we use ifconfig to list active devices?
148 int m_CanUseIfconfig
;
149 /// The path to ifconfig
150 wxString m_IfconfigPath
;
153 wxString m_BeaconHost
;
154 /// beacon host portnumber for connect:
157 /// command to connect to network
158 wxString m_ConnectCommand
;
159 /// command to hang up
160 wxString m_HangUpCommand
;
163 /// a timer for regular testing
164 class AutoCheckTimer
*m_timer
;
166 friend class AutoCheckTimer
;
168 void CheckStatus(void) const;
170 /// real status check
171 void CheckStatusInternal(void);
175 class AutoCheckTimer
: public wxTimer
178 AutoCheckTimer(wxDialUpManagerImpl
*dupman
)
184 virtual bool Start( int millisecs
= -1 )
185 { m_started
= TRUE
; return wxTimer::Start(millisecs
, FALSE
); }
187 virtual void Notify()
188 { wxLogTrace("Checking dial up network status."); m_dupman
->CheckStatus(); }
191 { if ( m_started
) wxTimer::Stop(); }
194 wxDialUpManagerImpl
*m_dupman
;
198 wxDialUpManagerImpl::Dial(const wxString
&isp
,
199 const wxString
& WXUNUSED(username
),
200 const wxString
& WXUNUSED(password
))
207 if(m_ConnectCommand
.Find("%s"))
208 cmd
.Printf(m_ConnectCommand
,m_ISPname
.c_str());
210 cmd
= m_ConnectCommand
;
211 return wxExecute(cmd
, /* sync */ TRUE
) == 0;
215 wxDialUpManagerImpl::HangUp(void)
221 if(m_HangUpCommand
.Find("%s"))
222 cmd
.Printf(m_HangUpCommand
,m_ISPname
.c_str());
224 cmd
= m_HangUpCommand
;
225 return wxExecute(cmd
, /* sync */ TRUE
) == 0;
230 wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds
)
232 wxASSERT(m_timer
== NULL
);
233 m_timer
= new AutoCheckTimer(this);
234 bool rc
= m_timer
->Start(nSeconds
*1000);
244 wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
246 wxASSERT(m_timer
!= NULL
);
254 wxDialUpManagerImpl::SetWellKnownHost(const wxString
& hostname
, int portno
)
256 /// does hostname contain a port number?
257 wxString port
= hostname
.After(':');
260 m_BeaconHost
= hostname
.Before(':');
261 m_BeaconPort
= atoi(port
);
265 m_BeaconHost
= hostname
;
266 m_BeaconPort
= portno
;
272 wxDialUpManagerImpl::CheckStatus(void) const
274 // This function calls the CheckStatusInternal() helper function
275 // which is OS - specific and then sends the events.
277 int oldIsOnline
= m_IsOnline
;
278 ( /* non-const */ (wxDialUpManagerImpl
*)this)->CheckStatusInternal();
280 // now send the events as appropriate:
281 if(m_IsOnline
!= oldIsOnline
)
291 We have three methods that we can use:
293 1. test via /sbin/ifconfig and grep for "sl", "ppp", "pl"
294 --> should be fast enough for regular polling
295 2. test if we can reach the well known beacon host
296 --> too slow for polling
297 3. check /proc/net/dev on linux??
298 This method should be preferred, if possible. Need to do more
304 wxDialUpManagerImpl::CheckStatusInternal(void)
308 // First time check for ifconfig location. We only use the variant
309 // which does not take arguments, a la GNU.
310 if(m_CanUseIfconfig
== -1) // unknown
312 if(wxFileExists("/sbin/ifconfig"))
313 m_IfconfigPath
= "/sbin/ifconfig";
314 else if(wxFileExists("/usr/sbin/ifconfig"))
315 m_IfconfigPath
= "/usr/sbin/ifconfig";
318 // Let´s try the ifconfig method first, should be fastest:
319 if(m_CanUseIfconfig
!= 0) // unknown or yes
321 wxASSERT(m_IfconfigPath
.length());
323 wxString tmpfile
= wxGetTempFileName("_wxdialuptest");
324 wxString cmd
= m_IfconfigPath
;
325 cmd
<< " >" << tmpfile
;
326 if(wxExecute(m_IfconfigPath
,TRUE
/* sync */) == 0)
328 m_CanUseIfconfig
= 1;
329 wxString cmd1
= "grep ppp <"+tmpfile
; // PPP
330 wxString cmd2
= "grep sl <"+tmpfile
; // SLIP
331 wxString cmd3
= "grep pl <"+tmpfile
; // PLIP
332 if(wxExecute(cmd1
,TRUE
) == 0
333 || wxExecute(cmd2
,TRUE
) == 0
334 || wxExecute(cmd3
,TRUE
) == 0
338 else // could not run ifconfig correctly
339 m_CanUseIfconfig
= 0; // don´t try again
340 wxRemoveFile(tmpfile
);
341 if(m_IsOnline
!= -1) // we are done
345 // second method: try to connect to well known host:
347 struct sockaddr_in serv_addr
;
350 m_IsOnline
= 0; // assume false
351 if((hp
= gethostbyname(m_BeaconHost
)) == NULL
)
352 return; // no DNS no net
354 serv_addr
.sin_family
= hp
->h_addrtype
;
355 memcpy(&serv_addr
.sin_addr
,hp
->h_addr
, hp
->h_length
);
356 serv_addr
.sin_port
= htons(m_BeaconPort
);
357 if( ( sockfd
= socket(hp
->h_addrtype
, SOCK_STREAM
, 0)) < 0)
359 // sys_error("cannot create socket for gw");
362 if( connect(sockfd
, (struct sockaddr
*) &serv_addr
, sizeof(serv_addr
)) < 0)
364 //sys_error("cannot connect to server");
374 wxDialUpManager::wxDialUpManager::Create(void)
376 return new wxDialUpManagerImpl
;
379 #endif // wxUSE_DIALUP_MANAGER