1 // -*- c++ -*- ///////////////////////////////////////////////////////////////
2 // Name: unix/dialup.cpp
3 // Purpose: Network related wxWindows classes and functions
4 // Author: Karsten Ballüder
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 # pragma implementation "dialup.h"
18 #if wxUSE_DIALUP_MANAGER
24 #include "wx/string.h"
26 #include "wx/dialup.h"
28 #include "wx/filefn.h"
32 #include "wx/process.h"
35 #include "wx/wxchar.h"
42 #define __STRICT_ANSI__
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 // ----------------------------------------------------------------------------
50 // A class which groups functions dealing with connecting to the network from a
51 // workstation using dial-up access to the net. There is at most one instance
52 // of this class in the program accessed via GetDialUpManager().
53 // ----------------------------------------------------------------------------
57 * 1. more configurability for Unix: i.e. how to initiate the connection, how
58 * to check for online status, &c.
59 * 2. add a "long Dial(long connectionId = -1)" function which asks the user
60 * about which connection to dial (this may be done using native dialogs
61 * under NT, need generic dialogs for all others) and returns the identifier
62 * of the selected connection (it's opaque to the application) - it may be
63 * reused later to dial the same connection later (or use strings instead of
65 * 3. add an async version of dialing functions which notify the caller about
66 * the progress (or may be even start another thread to monitor it)
67 * 4. the static creation/accessor functions are not MT-safe - but is this
68 * really crucial? I think we may suppose they're always called from the
72 class WXDLLEXPORT wxDialUpManagerImpl
: public wxDialUpManager
75 wxDialUpManagerImpl();
76 ~wxDialUpManagerImpl();
78 /** Could the dialup manager be initialized correctly? If this function
79 returns FALSE, no other functions will work neither, so it's a good idea
80 to call this function and check its result before calling any other
81 wxDialUpManager methods.
83 virtual bool IsOk() const
86 /** The simplest way to initiate a dial up: this function dials the given
87 ISP (exact meaning of the parameter depends on the platform), returns
88 TRUE on success or FALSE on failure and logs the appropriate error
89 message in the latter case.
90 @param nameOfISP optional paramater for dial program
91 @param username unused
92 @param password unused
94 virtual bool Dial(const wxString
& nameOfISP
,
95 const wxString
& WXUNUSED(username
),
96 const wxString
& WXUNUSED(password
),
99 /// Hang up the currently active dial up connection.
100 virtual bool HangUp();
102 // returns TRUE if the computer is connected to the network: under Windows,
103 // this just means that a RAS connection exists, under Unix we check that
104 // the "well-known host" (as specified by SetWellKnownHost) is reachable
105 virtual bool IsOnline() const
107 if( (! m_timer
) // we are not polling, so test now:
111 return m_IsOnline
!= 0;
114 /// do we have a constant net connection? -- GUESS!
115 bool IsAlwaysOnline() const
117 ((wxDialUpManagerImpl
*) this)->HangUp(); // brutal but necessary
120 /// returns TRUE if (async) dialing is in progress
121 inline virtual bool IsDialing() const
122 { return m_DialProcess
!= NULL
; }
124 // cancel dialing the number initiated with Dial(async = TRUE)
125 // NB: this won't result in DISCONNECTED event being sent
126 virtual bool CancelDialing();
128 size_t GetISPNames(class wxArrayString
&) const
131 // sometimes the built-in logic for determining the online status may fail,
132 // so, in general, the user should be allowed to override it. This function
133 // allows to forcefully set the online status - whatever our internal
134 // algorithm may think about it.
135 virtual void SetOnlineStatus(bool isOnline
= TRUE
)
136 { m_IsOnline
= isOnline
; }
138 // set misc wxDialUpManager options
139 // --------------------------------
141 // enable automatical checks for the connection status and sending of
142 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
143 // parameter is only for Unix where we do the check manually: under
144 // Windows, the notification about the change of connection status is
147 // Returns FALSE if couldn't set up automatic check for online status.
148 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
150 // disable automatic check for connection status change - notice that the
151 // wxEVT_DIALUP_XXX events won't be sent any more neither.
152 virtual void DisableAutoCheckOnlineStatus();
154 // under Unix, the value of well-known host is used to check whether we're
155 // connected to the internet. It's unused under Windows, but this function
156 // is always safe to call. The default value is www.yahoo.com.
157 virtual void SetWellKnownHost(const wxString
& hostname
,
159 /** Sets the commands to start up the network and to hang up
160 again. Used by the Unix implementations only.
162 virtual void SetConnectCommand(const wxString
&command
, const wxString
&hupcmd
)
163 { m_ConnectCommand
= command
; m_HangUpCommand
= hupcmd
; }
166 /// -1: don´t know, 0 = no, 1 = yes
169 /// Can we use ifconfig to list active devices?
170 int m_CanUseIfconfig
;
171 /// The path to ifconfig
172 wxString m_IfconfigPath
;
174 /// Can we use ping to find hosts?
176 /// The path to ping program
180 wxString m_BeaconHost
;
181 /// beacon host portnumber for connect:
184 /// command to connect to network
185 wxString m_ConnectCommand
;
186 /// command to hang up
187 wxString m_HangUpCommand
;
190 /// a timer for regular testing
191 class AutoCheckTimer
*m_timer
;
192 friend class AutoCheckTimer
;
194 /// a wxProcess for dialling in background
195 class wxDialProcess
*m_DialProcess
;
196 /// pid of dial process
198 friend class wxDialProcess
;
201 void CheckStatus(bool fromAsync
= FALSE
) const;
203 /// real status check
204 void CheckStatusInternal(void);
206 /// Check output of ifconfig command for PPP/SLIP/PLIP devices
207 int CheckIfconfig(void);
208 /// Ping a host: 1 on success, -1 if it cannot be used, 0 if unreachable
210 /// Check by connecting to host on given port.
211 int CheckConnect(void);
216 class AutoCheckTimer
: public wxTimer
219 AutoCheckTimer(wxDialUpManagerImpl
*dupman
)
225 virtual bool Start( int millisecs
= -1, bool WXUNUSED(one_shot
) = FALSE
)
226 { m_started
= TRUE
; return wxTimer::Start(millisecs
, FALSE
); }
228 virtual void Notify()
229 { wxLogTrace(wxT("Checking dial up network status.")); m_dupman
->CheckStatus(); }
232 { if ( m_started
) wxTimer::Stop(); }
235 wxDialUpManagerImpl
*m_dupman
;
238 class wxDialProcess
: public wxProcess
241 wxDialProcess(wxDialUpManagerImpl
*dupman
)
245 void Disconnect(void) { m_DupMan
= NULL
; }
246 virtual void OnTerminate(int WXUNUSED(pid
), int WXUNUSED(status
))
250 m_DupMan
->m_DialProcess
= NULL
;
251 m_DupMan
->CheckStatus(TRUE
);
255 wxDialUpManagerImpl
*m_DupMan
;
259 wxDialUpManagerImpl::wxDialUpManagerImpl()
261 /* The isOnline flag can have the following values internally:
264 -1 : unknown/undefined status
267 m_DialProcess
= NULL
;
269 m_CanUseIfconfig
= -1; // unknown
270 m_CanUsePing
= -1; // unknown
271 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
275 m_ConnectCommand
= _T("/usr/etc/ppp");
276 #elif defined(__LINUX__)
277 // default values for Debian/GNU linux
278 m_ConnectCommand
= _T("pon");
279 m_HangUpCommand
= _T("poff");
282 wxChar
* dial
= wxGetenv(_T("WXDIALUP_DIALCMD"));
283 wxChar
* hup
= wxGetenv(_T("WXDIALUP_HUPCMD"));
284 SetConnectCommand(dial
? wxString(dial
) : m_ConnectCommand
,
285 hup
? wxString(hup
) : m_HangUpCommand
);
288 wxDialUpManagerImpl::~wxDialUpManagerImpl()
290 if(m_timer
) delete m_timer
;
293 m_DialProcess
->Disconnect();
294 m_DialProcess
->Detach();
299 wxDialUpManagerImpl::Dial(const wxString
&isp
,
300 const wxString
& WXUNUSED(username
),
301 const wxString
& WXUNUSED(password
),
308 if(m_ConnectCommand
.Find(wxT("%s")))
309 cmd
.Printf(m_ConnectCommand
,m_ISPname
.c_str());
311 cmd
= m_ConnectCommand
;
315 m_DialProcess
= new wxDialProcess(this);
316 m_DialPId
= wxExecute(cmd
, FALSE
, m_DialProcess
);
319 delete m_DialProcess
;
320 m_DialProcess
= NULL
;
327 return wxExecute(cmd
, /* sync */ TRUE
) == 0;
331 wxDialUpManagerImpl::HangUp(void)
337 wxLogError(_("Already dialling ISP."));
341 if(m_HangUpCommand
.Find(wxT("%s")))
342 cmd
.Printf(m_HangUpCommand
,m_ISPname
.c_str(), m_DialProcess
);
344 cmd
= m_HangUpCommand
;
345 return wxExecute(cmd
, /* sync */ TRUE
) == 0;
350 wxDialUpManagerImpl::CancelDialing()
354 return kill(m_DialPId
, SIGTERM
) > 0;
358 wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds
)
360 DisableAutoCheckOnlineStatus();
361 m_timer
= new AutoCheckTimer(this);
362 bool rc
= m_timer
->Start(nSeconds
*1000);
372 wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
384 wxDialUpManagerImpl::SetWellKnownHost(const wxString
& hostname
, int portno
)
386 if(hostname
.Length() == 0)
388 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
393 /// does hostname contain a port number?
394 wxString port
= hostname
.After(wxT(':'));
397 m_BeaconHost
= hostname
.Before(wxT(':'));
398 m_BeaconPort
= wxAtoi(port
);
402 m_BeaconHost
= hostname
;
403 m_BeaconPort
= portno
;
409 wxDialUpManagerImpl::CheckStatus(bool fromAsync
) const
411 // This function calls the CheckStatusInternal() helper function
412 // which is OS - specific and then sends the events.
414 int oldIsOnline
= m_IsOnline
;
415 ( /* non-const */ (wxDialUpManagerImpl
*)this)->CheckStatusInternal();
417 // now send the events as appropriate:
418 if(m_IsOnline
!= oldIsOnline
// it changed
419 && ( m_IsOnline
== 1 // and it is a defined status
421 // only send events for well defined transitions
422 && ( oldIsOnline
== 1 || oldIsOnline
== 0)
425 wxDialUpEvent
event(m_IsOnline
, ! fromAsync
);
426 (void)wxTheApp
->ProcessEvent(event
);
431 We have three methods that we can use:
433 1. test via /sbin/ifconfig and grep for "sl", "ppp", "pl"
434 --> should be fast enough for regular polling
435 2. test if we can reach the well known beacon host
436 --> too slow for polling
437 3. check /proc/net/dev on linux??
438 This method should be preferred, if possible. Need to do more
444 wxDialUpManagerImpl::CheckStatusInternal(void)
450 testResult
= CheckConnect();
452 testResult
= CheckIfconfig();
454 testResult
= CheckPing();
455 m_IsOnline
= testResult
;
459 wxDialUpManagerImpl::CheckConnect(void)
461 // second method: try to connect to a well known host:
462 // This can be used under Win 9x, too!
464 struct sockaddr_in serv_addr
;
466 if((hp
= gethostbyname(m_BeaconHost
.mb_str())) == NULL
)
467 return 0; // no DNS no net
469 serv_addr
.sin_family
= hp
->h_addrtype
;
470 memcpy(&serv_addr
.sin_addr
,hp
->h_addr
, hp
->h_length
);
471 serv_addr
.sin_port
= htons(m_BeaconPort
);
474 if( ( sockfd
= socket(hp
->h_addrtype
, SOCK_STREAM
, 0)) < 0)
476 return -1; // no info
479 if( connect(sockfd
, (struct sockaddr
*) &serv_addr
,
480 sizeof(serv_addr
)) >= 0)
483 return 1; // we cant connect, so we have a network!
487 if(errno
== ENETUNREACH
)
488 return 0; // network is unreachable
489 // connect failed, but don't know why
494 wxDialUpManagerImpl::CheckIfconfig(void)
498 // First time check for ifconfig location. We only use the variant which
499 // does not take arguments, a la GNU.
500 if ( m_CanUseIfconfig
== -1 ) // unknown
502 static const wxChar
*ifconfigLocations
[] =
504 _T("/sbin"), // Linux, FreeBSD
505 _T("/usr/sbin"), // SunOS, Solaris, AIX, HP-UX
506 _T("/usr/etc"), // IRIX
509 for ( size_t n
= 0; n
< WXSIZEOF(ifconfigLocations
); n
++ )
511 wxString
path(ifconfigLocations
[n
]);
512 path
<< _T("/ifconfig");
514 if ( wxFileExists(path
) )
516 m_IfconfigPath
= path
;
522 wxLogNull ln
; // suppress all error messages
523 // Let´s try the ifconfig method first, should be fastest:
524 if(m_CanUseIfconfig
!= 0) // unknown or yes
526 wxASSERT(m_IfconfigPath
.length());
528 wxString tmpfile
= wxGetTempFileName("_wxdialuptest");
529 wxString cmd
= "/bin/sh -c \'";
530 cmd
<< m_IfconfigPath
;
531 #if defined(__SOLARIS__) || defined (__SUNOS__)
532 // need to add -a flag
534 #elif defined(__LINUX__) || defined (__FREEBSD__) || defined(__SGI__)
535 // nothing to be added to ifconfig
537 # pragma warning "No ifconfig information for this OS."
538 m_CanUseIfconfig
= 0;
541 cmd
<< " >" << tmpfile
<< '\'';
542 /* I tried to add an option to wxExecute() to not close stdout,
543 so we could let ifconfig write directly to the tmpfile, but
544 this does not work. That should be faster, as it doesn´t call
545 the shell first. I have no idea why. :-( (KB) */
546 if(wxExecute(cmd
,TRUE
/* sync */) == 0)
548 m_CanUseIfconfig
= 1;
550 if( file
.Open(tmpfile
) )
552 char *output
= new char [file
.Length()+1];
553 output
[file
.Length()] = '\0';
554 if(file
.Read(output
,file
.Length()) == file
.Length())
556 // FIXME shouldn't we grep for "^ppp"? (VZ)
558 #if defined(__SOLARIS__) || defined (__SUNOS__)
559 // dialup device under SunOS/Solaris
560 rc
= strstr(output
,"ipdptp") != (char *)NULL
;
561 #elif defined(__LINUX__) || defined (__FREEBSD__)
562 rc
= strstr(output
,"ppp") // ppp
563 || strstr(output
,"sl") // slip
564 || strstr(output
,"pl"); // plip
565 #elif defined(__SGI__) // IRIX
566 rc
= strstr(output
, "ppp"); // PPP
572 // else rc remains -1 as we don't know for sure
574 else // could not run ifconfig correctly
575 m_CanUseIfconfig
= 0; // don´t try again
576 (void) wxRemoveFile(tmpfile
);
582 wxDialUpManagerImpl::CheckPing(void)
587 // First time check for ping location. We only use the variant
588 // which does not take arguments, a la GNU.
589 if(m_CanUsePing
== -1) // unknown
591 if(wxFileExists("/bin/ping"))
592 m_PingPath
= "/bin/ping";
593 else if(wxFileExists("/usr/sbin/ping"))
594 m_PingPath
= "/usr/sbin/ping";
602 wxLogNull ln
; // suppress all error messages
603 wxASSERT(m_PingPath
.length());
605 cmd
<< m_PingPath
<< ' ';
606 #if defined(__SOLARIS__) || defined (__SUNOS__)
607 // nothing to add to ping command
608 #elif defined(__LINUX__)
609 cmd
<< "-c 1 "; // only ping once
611 # pragma warning "No Ping information for this OS."
616 if(wxExecute(cmd
, TRUE
/* sync */) == 0)
624 wxDialUpManager::Create(void)
626 return new wxDialUpManagerImpl
;
629 #endif // wxUSE_DIALUP_MANAGER