1 // -*- c++ -*- ////////////////////////////////////////////////////////////////
2 // Name: unix/dialup.cpp
3 // Purpose: Network related wxWidgets classes and functions
4 // Author: Karsten Ballüder
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #if wxUSE_DIALUP_MANAGER
23 #include "wx/string.h"
25 #include "wx/dialup.h"
27 #include "wx/filefn.h"
31 #include "wx/process.h"
34 #include "wx/wxchar.h"
41 #define __STRICT_ANSI__
42 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED
)
49 DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED
)
51 // ----------------------------------------------------------------------------
52 // A class which groups functions dealing with connecting to the network from a
53 // workstation using dial-up access to the net. There is at most one instance
54 // of this class in the program accessed via GetDialUpManager().
55 // ----------------------------------------------------------------------------
59 * 1. more configurability for Unix: i.e. how to initiate the connection, how
60 * to check for online status, &c.
61 * 2. add a "long Dial(long connectionId = -1)" function which asks the user
62 * about which connection to dial (this may be done using native dialogs
63 * under NT, need generic dialogs for all others) and returns the identifier
64 * of the selected connection (it's opaque to the application) - it may be
65 * reused later to dial the same connection later (or use strings instead of
67 * 3. add an async version of dialing functions which notify the caller about
68 * the progress (or may be even start another thread to monitor it)
69 * 4. the static creation/accessor functions are not MT-safe - but is this
70 * really crucial? I think we may suppose they're always called from the
74 class WXDLLEXPORT wxDialUpManagerImpl
: public wxDialUpManager
77 wxDialUpManagerImpl();
78 ~wxDialUpManagerImpl();
80 /** Could the dialup manager be initialized correctly? If this function
81 returns false, no other functions will work neither, so it's a good idea
82 to call this function and check its result before calling any other
83 wxDialUpManager methods.
85 virtual bool IsOk() const
88 /** The simplest way to initiate a dial up: this function dials the given
89 ISP (exact meaning of the parameter depends on the platform), returns
90 true on success or false on failure and logs the appropriate error
91 message in the latter case.
92 @param nameOfISP optional paramater for dial program
93 @param username unused
94 @param password unused
96 virtual bool Dial(const wxString
& nameOfISP
,
97 const wxString
& WXUNUSED(username
),
98 const wxString
& WXUNUSED(password
),
101 // Hang up the currently active dial up connection.
102 virtual bool HangUp();
104 // returns true if the computer is connected to the network: under Windows,
105 // this just means that a RAS connection exists, under Unix we check that
106 // the "well-known host" (as specified by SetWellKnownHost) is reachable
107 virtual bool IsOnline() const
110 return m_IsOnline
== Net_Connected
;
113 // do we have a constant net connection?
114 virtual bool IsAlwaysOnline() const;
116 // returns true if (async) dialing is in progress
117 virtual bool IsDialing() const
118 { return m_DialProcess
!= NULL
; }
120 // cancel dialing the number initiated with Dial(async = true)
121 // NB: this won't result in DISCONNECTED event being sent
122 virtual bool CancelDialing();
124 size_t GetISPNames(class wxArrayString
&) const
127 // sometimes the built-in logic for determining the online status may fail,
128 // so, in general, the user should be allowed to override it. This function
129 // allows to forcefully set the online status - whatever our internal
130 // algorithm may think about it.
131 virtual void SetOnlineStatus(bool isOnline
= true)
132 { m_IsOnline
= isOnline
? Net_Connected
: Net_No
; }
134 // set misc wxDialUpManager options
135 // --------------------------------
137 // enable automatical checks for the connection status and sending of
138 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
139 // parameter is only for Unix where we do the check manually: under
140 // Windows, the notification about the change of connection status is
143 // Returns false if couldn't set up automatic check for online status.
144 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
146 // disable automatic check for connection status change - notice that the
147 // wxEVT_DIALUP_XXX events won't be sent any more neither.
148 virtual void DisableAutoCheckOnlineStatus();
150 // under Unix, the value of well-known host is used to check whether we're
151 // connected to the internet. It's unused under Windows, but this function
152 // is always safe to call. The default value is www.yahoo.com.
153 virtual void SetWellKnownHost(const wxString
& hostname
,
155 /** Sets the commands to start up the network and to hang up
156 again. Used by the Unix implementations only.
158 virtual void SetConnectCommand(const wxString
&command
, const wxString
&hupcmd
)
159 { m_ConnectCommand
= command
; m_HangUpCommand
= hupcmd
; }
161 //private: -- Sun CC 4.2 objects to using NetConnection enum as the return
162 // type if it is declared private
164 // the possible results of testing for Online() status
167 Net_Unknown
= -1, // we couldn't learn anything
168 Net_No
, // no network connection [currently]
169 Net_Connected
// currently connected
172 // the possible net connection types
175 NetDevice_None
= 0x0000, // no network devices (authoritative)
176 NetDevice_Unknown
= 0x0001, // test doesn't work on this OS
177 NetDevice_Modem
= 0x0002, // we have a modem
178 NetDevice_LAN
= 0x0004 // a network card
182 // the current status
183 NetConnection m_IsOnline
;
185 // the connection we have with the network card
186 NetConnection m_connCard
;
188 // Can we use ifconfig to list active devices?
189 int m_CanUseIfconfig
;
191 // The path to ifconfig
192 wxString m_IfconfigPath
;
194 // Can we use ping to find hosts?
196 // The path to ping program
200 wxString m_BeaconHost
;
201 // beacon host portnumber for connect:
204 // command to connect to network
205 wxString m_ConnectCommand
;
206 // command to hang up
207 wxString m_HangUpCommand
;
210 // a timer for regular testing
211 class AutoCheckTimer
*m_timer
;
212 friend class AutoCheckTimer
;
214 // a wxProcess for dialling in background
215 class wxDialProcess
*m_DialProcess
;
216 // pid of dial process
218 friend class wxDialProcess
;
221 void CheckStatus(bool fromAsync
= false) const;
224 void CheckStatusInternal();
226 // check /proc/net (Linux only) for ppp/eth interfaces, returns the bit
227 // mask of NetDeviceType constants
230 // check output of ifconfig command for PPP/SLIP/PLIP devices, returns the
231 // bit mask of NetDeviceType constants
234 // combines the 2 possible checks for determining the connection status
235 NetConnection
CheckConnectAndPing();
238 NetConnection
CheckPing();
240 // check by connecting to host on given port.
241 NetConnection
CheckConnect();
245 class AutoCheckTimer
: public wxTimer
248 AutoCheckTimer(wxDialUpManagerImpl
*dupman
)
253 virtual void Notify()
255 wxLogTrace(_T("dialup"), wxT("Checking dial up network status."));
257 m_dupman
->CheckStatus();
261 wxDialUpManagerImpl
*m_dupman
;
264 class wxDialProcess
: public wxProcess
267 wxDialProcess(wxDialUpManagerImpl
*dupman
)
271 void Disconnect() { m_DupMan
= NULL
; }
272 virtual void OnTerminate(int WXUNUSED(pid
), int WXUNUSED(status
))
276 m_DupMan
->m_DialProcess
= NULL
;
277 m_DupMan
->CheckStatus(true);
281 wxDialUpManagerImpl
*m_DupMan
;
285 wxDialUpManagerImpl::wxDialUpManagerImpl()
288 m_connCard
= Net_Unknown
;
289 m_DialProcess
= NULL
;
291 m_CanUseIfconfig
= -1; // unknown
292 m_CanUsePing
= -1; // unknown
293 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
297 m_ConnectCommand
= _T("/usr/etc/ppp");
298 #elif defined(__LINUX__)
299 // default values for Debian/GNU linux
300 m_ConnectCommand
= _T("pon");
301 m_HangUpCommand
= _T("poff");
304 wxChar
* dial
= wxGetenv(_T("WXDIALUP_DIALCMD"));
305 wxChar
* hup
= wxGetenv(_T("WXDIALUP_HUPCMD"));
306 SetConnectCommand(dial
? wxString(dial
) : m_ConnectCommand
,
307 hup
? wxString(hup
) : m_HangUpCommand
);
310 wxDialUpManagerImpl::~wxDialUpManagerImpl()
312 if(m_timer
) delete m_timer
;
315 m_DialProcess
->Disconnect();
316 m_DialProcess
->Detach();
321 wxDialUpManagerImpl::Dial(const wxString
&isp
,
322 const wxString
& WXUNUSED(username
),
323 const wxString
& WXUNUSED(password
),
326 if(m_IsOnline
== Net_Connected
)
330 if(m_ConnectCommand
.Find(wxT("%s")))
331 cmd
.Printf(m_ConnectCommand
,m_ISPname
.c_str());
333 cmd
= m_ConnectCommand
;
337 m_DialProcess
= new wxDialProcess(this);
338 m_DialPId
= (int)wxExecute(cmd
, false, m_DialProcess
);
341 delete m_DialProcess
;
342 m_DialProcess
= NULL
;
349 return wxExecute(cmd
, /* sync */ true) == 0;
352 bool wxDialUpManagerImpl::HangUp()
354 if(m_IsOnline
== Net_No
)
358 wxLogError(_("Already dialling ISP."));
362 if(m_HangUpCommand
.Find(wxT("%s")))
363 cmd
.Printf(m_HangUpCommand
,m_ISPname
.c_str(), m_DialProcess
);
365 cmd
= m_HangUpCommand
;
366 return wxExecute(cmd
, /* sync */ true) == 0;
370 bool wxDialUpManagerImpl::CancelDialing()
374 return kill(m_DialPId
, SIGTERM
) > 0;
377 bool wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds
)
379 DisableAutoCheckOnlineStatus();
380 m_timer
= new AutoCheckTimer(this);
381 bool rc
= m_timer
->Start(nSeconds
*1000);
390 void wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
401 void wxDialUpManagerImpl::SetWellKnownHost(const wxString
& hostname
, int portno
)
403 if(hostname
.Length() == 0)
405 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
410 // does hostname contain a port number?
411 wxString port
= hostname
.After(wxT(':'));
414 m_BeaconHost
= hostname
.Before(wxT(':'));
415 m_BeaconPort
= wxAtoi(port
);
419 m_BeaconHost
= hostname
;
420 m_BeaconPort
= portno
;
425 void wxDialUpManagerImpl::CheckStatus(bool fromAsync
) const
427 // This function calls the CheckStatusInternal() helper function
428 // which is OS - specific and then sends the events.
430 NetConnection oldIsOnline
= m_IsOnline
;
431 ( /* non-const */ (wxDialUpManagerImpl
*)this)->CheckStatusInternal();
433 // now send the events as appropriate: i.e. if the status changed and
434 // if we're in defined state
435 if(m_IsOnline
!= oldIsOnline
436 && m_IsOnline
!= Net_Unknown
437 && oldIsOnline
!= Net_Unknown
)
439 wxDialUpEvent
event(m_IsOnline
== Net_Connected
, ! fromAsync
);
440 (void)wxTheApp
->ProcessEvent(event
);
445 We first try to find out if ppp interface is active. If it is, we assume
446 that we're online but don't have a permanent connection (this is false if a
447 networked machine uses modem to connect to somewhere else, but we can't do
448 anything in this case anyhow).
450 If no ppp interface is detected, we check for eth interface. If it is
451 found, we check that we can, indeed, connect to an Internet host. The logic
452 here is that connection check should be fast enough in this case and we
453 don't want to give false positives in a (common) case of a machine on a LAN
454 which is not connected to the outside.
456 If we didn't find either ppp or eth interfaces, we stop here and decide
457 that we're connected. However, if couldn't check for this, we try to ping a
458 remote host just in case.
460 NB1: Checking for the interface presence can be done in 2 ways
461 a) reading /proc/net/dev under Linux
462 b) spawning ifconfig under any OS
464 The first method is faster but only works under Linux.
466 NB2: pinging, actually, means that we first try to connect "manually" to
467 a port on remove machine and if it fails, we run ping.
470 void wxDialUpManagerImpl::CheckStatusInternal()
472 m_IsOnline
= Net_Unknown
;
474 // first do quick checks to determine what kind of network devices do we
476 int netDeviceType
= CheckProcNet();
477 if ( netDeviceType
== NetDevice_Unknown
)
479 // nothing found, try ifconfig too
480 netDeviceType
= CheckIfconfig();
483 switch ( netDeviceType
)
486 // no network devices, no connection
491 // we still do ping to confirm that we're connected but we only do
492 // it once and hope that the purpose of the network card (i.e.
493 // whether it used for connecting to the Internet or just to a
494 // LAN) won't change during the program lifetime
495 if ( m_connCard
== Net_Unknown
)
497 m_connCard
= CheckConnectAndPing();
499 m_IsOnline
= m_connCard
;
502 case NetDevice_Unknown
:
503 // try to ping just in case
504 m_IsOnline
= CheckConnectAndPing();
507 case NetDevice_LAN
+ NetDevice_Modem
:
508 case NetDevice_Modem
:
509 // assume we're connected
510 m_IsOnline
= Net_Connected
;
514 wxFAIL_MSG(_T("Unexpected netDeviceType"));
518 bool wxDialUpManagerImpl::IsAlwaysOnline() const
520 wxDialUpManagerImpl
*self
= wxConstCast(this, wxDialUpManagerImpl
);
522 int netDeviceType
= self
->CheckProcNet();
523 if ( netDeviceType
== NetDevice_Unknown
)
525 // nothing found, try ifconfig too
526 netDeviceType
= self
->CheckIfconfig();
529 if ( netDeviceType
== NetDevice_Unknown
)
531 // this is the only thing we can do unfortunately...
537 // we are only permanently online if we have a network card
538 return (netDeviceType
& NetDevice_LAN
) != 0;
542 wxDialUpManagerImpl::NetConnection
wxDialUpManagerImpl::CheckConnectAndPing()
546 // first try connecting - faster
547 conn
= CheckConnect();
548 if ( conn
== Net_Unknown
)
557 wxDialUpManagerImpl::NetConnection
wxDialUpManagerImpl::CheckConnect()
559 // second method: try to connect to a well known host:
560 // This can be used under Win 9x, too!
562 struct sockaddr_in serv_addr
;
564 if((hp
= gethostbyname(m_BeaconHost
.mb_str())) == NULL
)
565 return Net_No
; // no DNS no net
567 serv_addr
.sin_family
= hp
->h_addrtype
;
568 memcpy(&serv_addr
.sin_addr
,hp
->h_addr
, hp
->h_length
);
569 serv_addr
.sin_port
= htons(m_BeaconPort
);
572 if( ( sockfd
= socket(hp
->h_addrtype
, SOCK_STREAM
, 0)) < 0)
574 return Net_Unknown
; // no info
577 if( connect(sockfd
, (struct sockaddr
*) &serv_addr
,
578 sizeof(serv_addr
)) >= 0)
581 return Net_Connected
; // we can connect, so we have a network!
583 else // failed to connect
585 if(errno
== ENETUNREACH
)
586 return Net_No
; // network is unreachable
588 return Net_Unknown
; // connect failed, but don't know why
594 wxDialUpManagerImpl::CheckProcNet()
596 // assume that the test doesn't work
597 int netDevice
= NetDevice_Unknown
;
600 if (wxFileExists(_T("/proc/net/route")))
602 // cannot use wxFile::Length because file doesn't support seeking, so
603 // use stdio directly
604 FILE *f
= fopen("/proc/net/route", "rt");
607 // now we know that we will find all devices we may have
608 netDevice
= NetDevice_None
;
612 while (fgets(output
, 256, f
) != NULL
)
614 if ( strstr(output
, "eth") ) // network card
616 netDevice
|= NetDevice_LAN
;
618 else if (strstr(output
,"ppp") // ppp
619 || strstr(output
,"sl") // slip
620 || strstr(output
,"pl")) // plip
622 netDevice
|= NetDevice_Modem
;
636 wxDialUpManagerImpl::CheckIfconfig()
639 m_CanUseIfconfig
= 0;
642 // assume that the test doesn't work
643 int netDevice
= NetDevice_Unknown
;
645 // first time check for ifconfig location
646 if ( m_CanUseIfconfig
== -1 ) // unknown
648 static const wxChar
*ifconfigLocations
[] =
650 _T("/sbin"), // Linux, FreeBSD, Darwin
651 _T("/usr/sbin"), // SunOS, Solaris, AIX, HP-UX
652 _T("/usr/etc"), // IRIX
656 for ( size_t n
= 0; n
< WXSIZEOF(ifconfigLocations
); n
++ )
658 wxString
path(ifconfigLocations
[n
]);
659 path
<< _T("/ifconfig");
661 if ( wxFileExists(path
) )
663 m_IfconfigPath
= path
;
669 if ( m_CanUseIfconfig
!= 0 ) // unknown or yes
671 wxLogNull ln
; // suppress all error messages
673 wxASSERT_MSG( m_IfconfigPath
.length(),
674 _T("can't use ifconfig if it wasn't found") );
676 wxString tmpfile
= wxGetTempFileName( wxT("_wxdialuptest") );
677 wxString cmd
= wxT("/bin/sh -c \'");
678 cmd
<< m_IfconfigPath
;
679 #if defined(__AIX__) || \
680 defined(__OSF__) || \
681 defined(__SOLARIS__) || defined (__SUNOS__)
682 // need to add -a flag
684 #elif defined(__LINUX__) || defined(__SGI__)
685 // nothing to be added to ifconfig
686 #elif defined(__FREEBSD__) || defined(__DARWIN__)
689 #elif defined(__HPUX__)
690 // VZ: a wild guess (but without it, ifconfig fails completely)
693 #if defined(__GNUG__)
694 #warning "No ifconfig information for this OS."
696 #pragma warning "No ifconfig information for this OS."
699 m_CanUseIfconfig
= 0;
702 cmd
<< wxT(" >") << tmpfile
<< wxT('\'');
703 /* I tried to add an option to wxExecute() to not close stdout,
704 so we could let ifconfig write directly to the tmpfile, but
705 this does not work. That should be faster, as it doesn´t call
706 the shell first. I have no idea why. :-( (KB) */
707 if ( wxExecute(cmd
,true /* sync */) == 0 )
709 m_CanUseIfconfig
= 1;
711 if( file
.Open(tmpfile
) )
714 if ( file
.ReadAll(&output
) )
716 // FIXME shouldn't we grep for "^ppp"? (VZ)
718 bool hasModem
= false,
721 #if defined(__SOLARIS__) || defined (__SUNOS__)
722 // dialup device under SunOS/Solaris
723 hasModem
= strstr(output
.fn_str(),"ipdptp") != (char *)NULL
;
724 hasLAN
= strstr(output
.fn_str(), "hme") != (char *)NULL
;
725 #elif defined(__LINUX__) || defined (__FREEBSD__)
726 hasModem
= strstr(output
.fn_str(),"ppp") // ppp
727 || strstr(output
.fn_str(),"sl") // slip
728 || strstr(output
.fn_str(),"pl"); // plip
729 hasLAN
= strstr(output
.fn_str(), "eth") != NULL
;
730 #elif defined(__SGI__) // IRIX
731 hasModem
= strstr(output
.fn_str(), "ppp") != NULL
; // PPP
732 #elif defined(__HPUX__)
733 // if could run ifconfig on interface, then it exists
737 netDevice
= NetDevice_None
;
739 netDevice
|= NetDevice_Modem
;
741 netDevice
|= NetDevice_LAN
;
743 //else: error reading the file
745 //else: error opening the file
747 else // could not run ifconfig correctly
749 m_CanUseIfconfig
= 0; // don´t try again
752 (void) wxRemoveFile(tmpfile
);
759 wxDialUpManagerImpl::NetConnection
wxDialUpManagerImpl::CheckPing()
761 // First time check for ping location. We only use the variant
762 // which does not take arguments, a la GNU.
763 if(m_CanUsePing
== -1) // unknown
766 if (wxFileExists( wxT("SYS$SYSTEM:TCPIP$PING.EXE") ))
767 m_PingPath
= wxT("$SYS$SYSTEM:TCPIP$PING");
768 #elif defined(__AIX__)
769 m_PingPath
= _T("/etc/ping");
770 #elif defined(__SGI__)
771 m_PingPath
= _T("/usr/etc/ping");
773 if (wxFileExists( wxT("/bin/ping") ))
774 m_PingPath
= wxT("/bin/ping");
775 else if (wxFileExists( wxT("/usr/sbin/ping") ))
776 m_PingPath
= wxT("/usr/sbin/ping");
786 // we didn't find ping
790 wxLogNull ln
; // suppress all error messages
791 wxASSERT(m_PingPath
.length());
793 cmd
<< m_PingPath
<< wxT(' ');
794 #if defined(__SOLARIS__) || defined (__SUNOS__)
795 // nothing to add to ping command
796 #elif defined(__AIX__) || \
797 defined (__BSD__) || \
798 defined(__LINUX__) || \
799 defined(__OSF__) || \
800 defined(__SGI__) || \
802 cmd
<< wxT("-c 1 "); // only ping once
803 #elif defined(__HPUX__)
804 cmd
<< wxT("64 1 "); // only ping once (need also specify the packet size)
806 #if defined(__GNUG__)
807 #warning "No Ping information for this OS."
809 #pragma warning "No Ping information for this OS."
816 if(wxExecute(cmd
, true /* sync */) == 0)
817 return Net_Connected
;
823 wxDialUpManager
*wxDialUpManager::Create()
825 return new wxDialUpManagerImpl
;
828 #endif // wxUSE_DIALUP_MANAGER