]> git.saurik.com Git - wxWidgets.git/blob - src/unix/dialup.cpp
1. Win9x support + async dialing + many more for wxDialUpManager
[wxWidgets.git] / src / unix / dialup.cpp
1 // -*- c++ -*- ///////////////////////////////////////////////////////////////
2 // Name: unix/dialup.cpp
3 // Purpose: Network related wxWindows classes and functions
4 // Author: Karsten Ballüder
5 // Modified by:
6 // Created: 03.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/setup.h"
13
14 #if wxUSE_DIALUP_MANAGER
15
16 #ifndef WX_PRECOMP
17 # include "wx/defs.h"
18 #endif // !PCH
19
20 #include "wx/string.h"
21 #include "wx/event.h"
22 #include "wx/net.h"
23 #include "wx/timer.h"
24 #include "wx/filefn.h"
25 #include "wx/utils.h"
26 #include "wx/log.h"
27
28 #include <stdlib.h>
29
30 #include <signal.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #define __STRICT_ANSI__
34 #include <sys/socket.h>
35 #include <netdb.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38
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 // ----------------------------------------------------------------------------
44
45 /* TODO
46 *
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
54 * longs may be?)
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
59 * main thread?
60 */
61
62 class WXDLLEXPORT wxDialUpManagerImpl : public wxDialUpManager
63 {
64 public:
65 wxDialUpManagerImpl()
66 {
67 m_IsOnline = -1; // unknown
68 m_timer = NULL;
69 m_CanUseIfconfig = -1; // unknown
70 m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST;
71 m_BeaconPort = 80;
72 }
73
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.
78 */
79 virtual bool IsOk() const
80 { return TRUE; }
81
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
89 */
90 virtual bool Dial(const wxString& nameOfISP,
91 const wxString& WXUNUSED(username),
92 const wxString& WXUNUSED(password),
93 bool async);
94
95 /// Hang up the currently active dial up connection.
96 virtual bool HangUp();
97
98 // returns TRUE if the computer is connected to the network: under Windows,
99 // this just means that a RAS connection exists, under Unix we check that
100 // the "well-known host" (as specified by SetWellKnownHost) is reachable
101 virtual bool IsOnline() const
102 {
103 if( (! m_timer) // we are not polling, so test now:
104 || m_IsOnline == -1
105 )
106 CheckStatus();
107 return m_IsOnline != 0;
108 }
109
110 // sometimes the built-in logic for determining the online status may fail,
111 // so, in general, the user should be allowed to override it. This function
112 // allows to forcefully set the online status - whatever our internal
113 // algorithm may think about it.
114 virtual void SetOnlineStatus(bool isOnline = TRUE)
115 { m_IsOnline = isOnline; }
116
117 // set misc wxDialUpManager options
118 // --------------------------------
119
120 // enable automatical checks for the connection status and sending of
121 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
122 // parameter is only for Unix where we do the check manually: under
123 // Windows, the notification about the change of connection status is
124 // instantenous.
125 //
126 // Returns FALSE if couldn't set up automatic check for online status.
127 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds);
128
129 // disable automatic check for connection status change - notice that the
130 // wxEVT_DIALUP_XXX events won't be sent any more neither.
131 virtual void DisableAutoCheckOnlineStatus();
132
133 // under Unix, the value of well-known host is used to check whether we're
134 // connected to the internet. It's unused under Windows, but this function
135 // is always safe to call. The default value is www.yahoo.com.
136 virtual void SetWellKnownHost(const wxString& hostname,
137 int portno = 80);
138 /** Sets the commands to start up the network and to hang up
139 again. Used by the Unix implementations only.
140 */
141 virtual void SetConnectCommand(const wxString &command, const wxString &hupcmd)
142 { m_ConnectCommand = command; m_HangUpCommand = hupcmd; }
143
144 private:
145 /// -1: don´t know, 0 = no, 1 = yes
146 int m_IsOnline;
147
148 /// Can we use ifconfig to list active devices?
149 int m_CanUseIfconfig;
150 /// The path to ifconfig
151 wxString m_IfconfigPath;
152
153 /// beacon host:
154 wxString m_BeaconHost;
155 /// beacon host portnumber for connect:
156 int m_BeaconPort;
157
158 /// command to connect to network
159 wxString m_ConnectCommand;
160 /// command to hang up
161 wxString m_HangUpCommand;
162 /// name of ISP
163 wxString m_ISPname;
164 /// a timer for regular testing
165 class AutoCheckTimer *m_timer;
166
167 friend class AutoCheckTimer;
168 /// determine status
169 void CheckStatus(void) const;
170
171 /// real status check
172 void CheckStatusInternal(void);
173 };
174
175
176 class AutoCheckTimer : public wxTimer
177 {
178 public:
179 AutoCheckTimer(wxDialUpManagerImpl *dupman)
180 {
181 m_dupman = dupman;
182 m_started = FALSE;
183 }
184
185 virtual bool Start( int millisecs = -1 )
186 { m_started = TRUE; return wxTimer::Start(millisecs, FALSE); }
187
188 virtual void Notify()
189 { wxLogTrace("Checking dial up network status."); m_dupman->CheckStatus(); }
190
191 virtual void Stop()
192 { if ( m_started ) wxTimer::Stop(); }
193 public:
194 bool m_started;
195 wxDialUpManagerImpl *m_dupman;
196 };
197
198 bool
199 wxDialUpManagerImpl::Dial(const wxString &isp,
200 const wxString & WXUNUSED(username),
201 const wxString & WXUNUSED(password),
202 bool async)
203 {
204 if(m_IsOnline == 1)
205 return FALSE;
206 m_IsOnline = -1;
207 m_ISPname = isp;
208 wxString cmd;
209 if(m_ConnectCommand.Find("%s"))
210 cmd.Printf(m_ConnectCommand,m_ISPname.c_str());
211 else
212 cmd = m_ConnectCommand;
213
214 if ( async )
215 {
216 wxFAIL_MSG(_T("TODO"));
217 }
218 else
219 {
220 return wxExecute(cmd, /* sync */ TRUE) == 0;
221 }
222 }
223
224 bool
225 wxDialUpManagerImpl::HangUp(void)
226 {
227 if(m_IsOnline == 0)
228 return FALSE;
229 m_IsOnline = -1;
230 wxString cmd;
231 if(m_HangUpCommand.Find("%s"))
232 cmd.Printf(m_HangUpCommand,m_ISPname.c_str());
233 else
234 cmd = m_HangUpCommand;
235 return wxExecute(cmd, /* sync */ TRUE) == 0;
236 }
237
238
239 bool
240 wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds)
241 {
242 wxASSERT(m_timer == NULL);
243 m_timer = new AutoCheckTimer(this);
244 bool rc = m_timer->Start(nSeconds*1000);
245 if(! rc)
246 {
247 delete m_timer;
248 m_timer = NULL;
249 }
250 return rc;
251 }
252
253 void
254 wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
255 {
256 wxASSERT(m_timer != NULL);
257 m_timer->Stop();
258 delete m_timer;
259 m_timer = NULL;
260 }
261
262
263 void
264 wxDialUpManagerImpl::SetWellKnownHost(const wxString& hostname, int portno)
265 {
266 /// does hostname contain a port number?
267 wxString port = hostname.After(':');
268 if(port.Length())
269 {
270 m_BeaconHost = hostname.Before(':');
271 m_BeaconPort = atoi(port);
272 }
273 else
274 {
275 m_BeaconHost = hostname;
276 m_BeaconPort = portno;
277 }
278 }
279
280
281 void
282 wxDialUpManagerImpl::CheckStatus(void) const
283 {
284 // This function calls the CheckStatusInternal() helper function
285 // which is OS - specific and then sends the events.
286
287 int oldIsOnline = m_IsOnline;
288 ( /* non-const */ (wxDialUpManagerImpl *)this)->CheckStatusInternal();
289
290 // now send the events as appropriate:
291 if(m_IsOnline != oldIsOnline)
292 {
293 if(m_IsOnline)
294 ; // send ev
295 else
296 ; // send ev
297 }
298 }
299
300 /*
301 We have three methods that we can use:
302
303 1. test via /sbin/ifconfig and grep for "sl", "ppp", "pl"
304 --> should be fast enough for regular polling
305 2. test if we can reach the well known beacon host
306 --> too slow for polling
307 3. check /proc/net/dev on linux??
308 This method should be preferred, if possible. Need to do more
309 testing.
310
311 */
312
313 void
314 wxDialUpManagerImpl::CheckStatusInternal(void)
315 {
316 m_IsOnline = -1;
317
318 // First time check for ifconfig location. We only use the variant
319 // which does not take arguments, a la GNU.
320 if(m_CanUseIfconfig == -1) // unknown
321 {
322 if(wxFileExists("/sbin/ifconfig"))
323 m_IfconfigPath = "/sbin/ifconfig";
324 else if(wxFileExists("/usr/sbin/ifconfig"))
325 m_IfconfigPath = "/usr/sbin/ifconfig";
326 }
327
328 // Let´s try the ifconfig method first, should be fastest:
329 if(m_CanUseIfconfig != 0) // unknown or yes
330 {
331 wxASSERT(m_IfconfigPath.length());
332
333 wxString tmpfile = wxGetTempFileName("_wxdialuptest");
334 wxString cmd = m_IfconfigPath;
335 cmd << " >" << tmpfile;
336 if(wxExecute(m_IfconfigPath,TRUE /* sync */) == 0)
337 {
338 m_CanUseIfconfig = 1;
339 wxString cmd1 = "grep ppp <"+tmpfile; // PPP
340 wxString cmd2 = "grep sl <"+tmpfile; // SLIP
341 wxString cmd3 = "grep pl <"+tmpfile; // PLIP
342 if(wxExecute(cmd1,TRUE) == 0
343 || wxExecute(cmd2,TRUE) == 0
344 || wxExecute(cmd3,TRUE) == 0
345 )
346 m_IsOnline = 1;
347 }
348 else // could not run ifconfig correctly
349 m_CanUseIfconfig = 0; // don´t try again
350 wxRemoveFile(tmpfile);
351 if(m_IsOnline != -1) // we are done
352 return;
353 }
354
355 // second method: try to connect to well known host:
356 struct hostent *hp;
357 struct sockaddr_in serv_addr;
358 int sockfd;
359
360 m_IsOnline = 0; // assume false
361 if((hp = gethostbyname(m_BeaconHost)) == NULL)
362 return; // no DNS no net
363
364 serv_addr.sin_family = hp->h_addrtype;
365 memcpy(&serv_addr.sin_addr,hp->h_addr, hp->h_length);
366 serv_addr.sin_port = htons(m_BeaconPort);
367 if( ( sockfd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
368 {
369 // sys_error("cannot create socket for gw");
370 return;
371 }
372 if( connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
373 {
374 //sys_error("cannot connect to server");
375 return;
376 }
377 //connected!
378 close(sockfd);
379 }
380
381
382 /* static */
383 wxDialUpManager *
384 wxDialUpManager::wxDialUpManager::Create(void)
385 {
386 return new wxDialUpManagerImpl;
387 }
388
389 #endif // wxUSE_DIALUP_MANAGER