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