]> git.saurik.com Git - wxWidgets.git/blob - src/unix/dialup.cpp
Applied a few old patches,
[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/dialup.h"
23 #include "wx/timer.h"
24 #include "wx/filefn.h"
25 #include "wx/utils.h"
26 #include "wx/log.h"
27 #include "wx/file.h"
28 #include "wx/process.h"
29 #include "wx/intl.h"
30 #include "wx/app.h"
31
32 #include <stdlib.h>
33
34 #include <signal.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #define __STRICT_ANSI__
38 #include <sys/socket.h>
39 #include <netdb.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
42
43 // ----------------------------------------------------------------------------
44 // A class which groups functions dealing with connecting to the network from a
45 // workstation using dial-up access to the net. There is at most one instance
46 // of this class in the program accessed via GetDialUpManager().
47 // ----------------------------------------------------------------------------
48
49 /* TODO
50 *
51 * 1. more configurability for Unix: i.e. how to initiate the connection, how
52 * to check for online status, &c.
53 * 2. add a "long Dial(long connectionId = -1)" function which asks the user
54 * about which connection to dial (this may be done using native dialogs
55 * under NT, need generic dialogs for all others) and returns the identifier
56 * of the selected connection (it's opaque to the application) - it may be
57 * reused later to dial the same connection later (or use strings instead of
58 * longs may be?)
59 * 3. add an async version of dialing functions which notify the caller about
60 * the progress (or may be even start another thread to monitor it)
61 * 4. the static creation/accessor functions are not MT-safe - but is this
62 * really crucial? I think we may suppose they're always called from the
63 * main thread?
64 */
65
66 class WXDLLEXPORT wxDialUpManagerImpl : public wxDialUpManager
67 {
68 public:
69 wxDialUpManagerImpl();
70 ~wxDialUpManagerImpl();
71
72 /** Could the dialup manager be initialized correctly? If this function
73 returns FALSE, no other functions will work neither, so it's a good idea
74 to call this function and check its result before calling any other
75 wxDialUpManager methods.
76 */
77 virtual bool IsOk() const
78 { return TRUE; }
79
80 /** The simplest way to initiate a dial up: this function dials the given
81 ISP (exact meaning of the parameter depends on the platform), returns
82 TRUE on success or FALSE on failure and logs the appropriate error
83 message in the latter case.
84 @param nameOfISP optional paramater for dial program
85 @param username unused
86 @param password unused
87 */
88 virtual bool Dial(const wxString& nameOfISP,
89 const wxString& WXUNUSED(username),
90 const wxString& WXUNUSED(password),
91 bool async);
92
93 /// Hang up the currently active dial up connection.
94 virtual bool HangUp();
95
96 // returns TRUE if the computer is connected to the network: under Windows,
97 // this just means that a RAS connection exists, under Unix we check that
98 // the "well-known host" (as specified by SetWellKnownHost) is reachable
99 virtual bool IsOnline() const
100 {
101 if( (! m_timer) // we are not polling, so test now:
102 || m_IsOnline == -1
103 )
104 CheckStatus();
105 return m_IsOnline != 0;
106 }
107
108 /// returns TRUE if (async) dialing is in progress
109 inline virtual bool IsDialling() const
110 { return m_DialProcess != NULL; }
111
112 // cancel dialing the number initiated with Dial(async = TRUE)
113 // NB: this won't result in DISCONNECTED event being sent
114 virtual bool CancelDialing();
115
116 // sometimes the built-in logic for determining the online status may fail,
117 // so, in general, the user should be allowed to override it. This function
118 // allows to forcefully set the online status - whatever our internal
119 // algorithm may think about it.
120 virtual void SetOnlineStatus(bool isOnline = TRUE)
121 { m_IsOnline = isOnline; }
122
123 // set misc wxDialUpManager options
124 // --------------------------------
125
126 // enable automatical checks for the connection status and sending of
127 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
128 // parameter is only for Unix where we do the check manually: under
129 // Windows, the notification about the change of connection status is
130 // instantenous.
131 //
132 // Returns FALSE if couldn't set up automatic check for online status.
133 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds);
134
135 // disable automatic check for connection status change - notice that the
136 // wxEVT_DIALUP_XXX events won't be sent any more neither.
137 virtual void DisableAutoCheckOnlineStatus();
138
139 // under Unix, the value of well-known host is used to check whether we're
140 // connected to the internet. It's unused under Windows, but this function
141 // is always safe to call. The default value is www.yahoo.com.
142 virtual void SetWellKnownHost(const wxString& hostname,
143 int portno = 80);
144 /** Sets the commands to start up the network and to hang up
145 again. Used by the Unix implementations only.
146 */
147 virtual void SetConnectCommand(const wxString &command, const wxString &hupcmd)
148 { m_ConnectCommand = command; m_HangUpCommand = hupcmd; }
149
150 private:
151 /// -1: don´t know, 0 = no, 1 = yes
152 int m_IsOnline;
153
154 /// Can we use ifconfig to list active devices?
155 int m_CanUseIfconfig;
156 /// The path to ifconfig
157 wxString m_IfconfigPath;
158
159 /// beacon host:
160 wxString m_BeaconHost;
161 /// beacon host portnumber for connect:
162 int m_BeaconPort;
163
164 /// command to connect to network
165 wxString m_ConnectCommand;
166 /// command to hang up
167 wxString m_HangUpCommand;
168 /// name of ISP
169 wxString m_ISPname;
170 /// a timer for regular testing
171 class AutoCheckTimer *m_timer;
172 friend class AutoCheckTimer;
173
174 /// a wxProcess for dialling in background
175 class wxDialProcess *m_DialProcess;
176 /// pid of dial process
177 int m_DialPId;
178 friend class wxDialProcess;
179
180 /// determine status
181 void CheckStatus(bool fromAsync = FALSE) const;
182
183 /// real status check
184 void CheckStatusInternal(void);
185 };
186
187
188 class AutoCheckTimer : public wxTimer
189 {
190 public:
191 AutoCheckTimer(wxDialUpManagerImpl *dupman)
192 {
193 m_dupman = dupman;
194 m_started = FALSE;
195 }
196
197 virtual bool Start( int millisecs = -1 )
198 { m_started = TRUE; return wxTimer::Start(millisecs, FALSE); }
199
200 virtual void Notify()
201 { wxLogTrace("Checking dial up network status."); m_dupman->CheckStatus(); }
202
203 virtual void Stop()
204 { if ( m_started ) wxTimer::Stop(); }
205 public:
206 bool m_started;
207 wxDialUpManagerImpl *m_dupman;
208 };
209
210 class wxDialProcess : public wxProcess
211 {
212 public:
213 wxDialProcess(wxDialUpManagerImpl *dupman)
214 {
215 m_DupMan = dupman;
216 }
217 void OnTerminate(int pid, int status) const
218 {
219 m_DupMan->m_DialProcess = NULL;
220 m_DupMan->CheckStatus(TRUE);
221 }
222 private:
223 wxDialUpManagerImpl *m_DupMan;
224 };
225
226
227 wxDialUpManagerImpl::wxDialUpManagerImpl()
228 {
229 m_IsOnline = -1; // unknown
230 m_DialProcess = NULL;
231 m_timer = NULL;
232 m_CanUseIfconfig = -1; // unknown
233 m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST;
234 m_BeaconPort = 80;
235 }
236
237 wxDialUpManagerImpl::~wxDialUpManagerImpl()
238 {
239 if(m_timer) delete m_timer;
240 if(m_DialProcess) m_DialProcess->Detach();
241 }
242
243 bool
244 wxDialUpManagerImpl::Dial(const wxString &isp,
245 const wxString & WXUNUSED(username),
246 const wxString & WXUNUSED(password),
247 bool async)
248 {
249 if(m_IsOnline == 1)
250 return FALSE;
251 m_IsOnline = -1;
252 m_ISPname = isp;
253 wxString cmd;
254 if(m_ConnectCommand.Find("%s"))
255 cmd.Printf(m_ConnectCommand,m_ISPname.c_str());
256 else
257 cmd = m_ConnectCommand;
258
259 if ( async )
260 {
261 m_DialProcess = new wxDialProcess(this);
262 m_DialPId = wxExecute(cmd, FALSE, m_DialProcess);
263 if(m_DialPId == 0)
264 {
265 delete m_DialProcess;
266 m_DialProcess = NULL;
267 return FALSE;
268 }
269 else
270 return TRUE;
271 }
272 else
273 return wxExecute(cmd, /* sync */ TRUE) == 0;
274 }
275
276 bool
277 wxDialUpManagerImpl::HangUp(void)
278 {
279 if(m_IsOnline == 0)
280 return FALSE;
281 if(IsDialling())
282 {
283 wxLogError(_("Already dialling ISP."));
284 return FALSE;
285 }
286 m_IsOnline = -1;
287 wxString cmd;
288 if(m_HangUpCommand.Find("%s"))
289 cmd.Printf(m_HangUpCommand,m_ISPname.c_str(), m_DialProcess);
290 else
291 cmd = m_HangUpCommand;
292 return wxExecute(cmd, /* sync */ TRUE) == 0;
293 }
294
295
296 bool
297 wxDialUpManagerImpl::CancelDialing()
298 {
299 if(! IsDialling())
300 return FALSE;
301 return kill(m_DialPId, SIGTERM) > 0;
302 }
303
304 bool
305 wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds)
306 {
307 wxASSERT(m_timer == NULL);
308 m_timer = new AutoCheckTimer(this);
309 bool rc = m_timer->Start(nSeconds*1000);
310 if(! rc)
311 {
312 delete m_timer;
313 m_timer = NULL;
314 }
315 return rc;
316 }
317
318 void
319 wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
320 {
321 wxASSERT(m_timer != NULL);
322 m_timer->Stop();
323 delete m_timer;
324 m_timer = NULL;
325 }
326
327
328 void
329 wxDialUpManagerImpl::SetWellKnownHost(const wxString& hostname, int portno)
330 {
331 /// does hostname contain a port number?
332 wxString port = hostname.After(':');
333 if(port.Length())
334 {
335 m_BeaconHost = hostname.Before(':');
336 m_BeaconPort = atoi(port);
337 }
338 else
339 {
340 m_BeaconHost = hostname;
341 m_BeaconPort = portno;
342 }
343 }
344
345
346 void
347 wxDialUpManagerImpl::CheckStatus(bool fromAsync) const
348 {
349 // This function calls the CheckStatusInternal() helper function
350 // which is OS - specific and then sends the events.
351
352 int oldIsOnline = m_IsOnline;
353 ( /* non-const */ (wxDialUpManagerImpl *)this)->CheckStatusInternal();
354
355 // now send the events as appropriate:
356 if(m_IsOnline != oldIsOnline)
357 {
358 wxDialUpEvent event(m_IsOnline, ! fromAsync);
359 (void)wxTheApp->ProcessEvent(event);
360 }
361 }
362
363 /*
364 We have three methods that we can use:
365
366 1. test via /sbin/ifconfig and grep for "sl", "ppp", "pl"
367 --> should be fast enough for regular polling
368 2. test if we can reach the well known beacon host
369 --> too slow for polling
370 3. check /proc/net/dev on linux??
371 This method should be preferred, if possible. Need to do more
372 testing.
373
374 */
375
376 void
377 wxDialUpManagerImpl::CheckStatusInternal(void)
378 {
379 m_IsOnline = -1;
380
381 // First time check for ifconfig location. We only use the variant
382 // which does not take arguments, a la GNU.
383 if(m_CanUseIfconfig == -1) // unknown
384 {
385 if(wxFileExists("/sbin/ifconfig"))
386 m_IfconfigPath = "/sbin/ifconfig";
387 else if(wxFileExists("/usr/sbin/ifconfig"))
388 m_IfconfigPath = "/usr/sbin/ifconfig";
389 }
390
391 wxLogNull ln; // suppress all error messages
392 // Let´s try the ifconfig method first, should be fastest:
393 if(m_CanUseIfconfig != 0) // unknown or yes
394 {
395 wxASSERT(m_IfconfigPath.length());
396
397 wxString tmpfile = wxGetTempFileName("_wxdialuptest");
398 wxString cmd = "/bin/sh -c \'";
399 cmd << m_IfconfigPath << " >" << tmpfile << '\'';
400 /* I tried to add an option to wxExecute() to not close stdout,
401 so we could let ifconfig write directly to the tmpfile, but
402 this does not work. That should be faster, as it doesn´t call
403 the shell first. I have no idea why. :-( (KB) */
404 #if 0
405 // temporarily redirect stdout/stderr:
406 int
407 new_stdout = dup(STDOUT_FILENO),
408 new_stderr = dup(STDERR_FILENO);
409 close(STDOUT_FILENO);
410 close(STDERR_FILENO);
411
412 int
413 // new stdout:
414 output_fd = open(tmpfile, O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR),
415 // new stderr:
416 null_fd = open("/dev/null", O_CREAT, S_IRUSR|S_IWUSR);
417 // verify well behaved unix behaviour:
418 wxASSERT(output_fd == STDOUT_FILENO);
419 wxASSERT(null_fd == STDERR_FILENO);
420 int rc = wxExecute(m_IfconfigPath,TRUE /* sync */,NULL ,wxEXECUTE_DONT_CLOSE_FDS);
421 close(null_fd); close(output_fd);
422 // restore old stdout, stderr:
423 int test;
424 test = dup(new_stdout); close(new_stdout); wxASSERT(test == STDOUT_FILENO);
425 test = dup(new_stderr); close(new_stderr); wxASSERT(test == STDERR_FILENO);
426 if(rc == 0)
427 #endif
428 if(wxExecute(cmd,TRUE /* sync */) == 0)
429 {
430 m_CanUseIfconfig = 1;
431 wxFile file;
432 if( file.Open(tmpfile) )
433 {
434 char *output = new char [file.Length()+1];
435 output[file.Length()] = '\0';
436 if(file.Read(output,file.Length()) == file.Length())
437 {
438 if(strstr(output,"ppp") // ppp
439 || strstr(output,"sl") // slip
440 || strstr(output,"pl") // plip
441 )
442 m_IsOnline = 1;
443 else
444 m_IsOnline = 0;
445 }
446 file.Close();
447 delete [] output;
448 }
449 // else m_IsOnline remains -1 as we don't know for sure
450 }
451 else // could not run ifconfig correctly
452 m_CanUseIfconfig = 0; // don´t try again
453 (void) wxRemoveFile(tmpfile);
454 if(m_IsOnline != -1) // we are done
455 return;
456 }
457
458 // second method: try to connect to well known host:
459 // This can be used under Win 9x, too!
460 struct hostent *hp;
461 struct sockaddr_in serv_addr;
462 int sockfd;
463
464 m_IsOnline = 0; // assume false
465 if((hp = gethostbyname(m_BeaconHost)) == NULL)
466 return; // no DNS no net
467
468 serv_addr.sin_family = hp->h_addrtype;
469 memcpy(&serv_addr.sin_addr,hp->h_addr, hp->h_length);
470 serv_addr.sin_port = htons(m_BeaconPort);
471 if( ( sockfd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
472 {
473 // sys_error("cannot create socket for gw");
474 return;
475 }
476 if( connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
477 {
478 //sys_error("cannot connect to server");
479 return;
480 }
481 //connected!
482 close(sockfd);
483 }
484
485
486 /* static */
487 wxDialUpManager *
488 wxDialUpManager::wxDialUpManager::Create(void)
489 {
490 return new wxDialUpManagerImpl;
491 }
492
493 #endif // wxUSE_DIALUP_MANAGER