1 // -*- c++ -*- ////////////////////////////////////////////////////////////////
2 // Name: src/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"
15 #if wxUSE_DIALUP_MANAGER
18 #include "wx/string.h"
22 #include "wx/dialup.h"
24 #include "wx/filefn.h"
28 #include "wx/process.h"
31 #include "wx/wxchar.h"
38 #define __STRICT_ANSI__
39 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <arpa/inet.h>
45 DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED
)
46 DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED
)
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 // ----------------------------------------------------------------------------
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
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
71 class WXDLLEXPORT wxDialUpManagerImpl
: public wxDialUpManager
74 wxDialUpManagerImpl();
75 ~wxDialUpManagerImpl();
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.
82 virtual bool IsOk() const
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
93 virtual bool Dial(const wxString
& nameOfISP
,
94 const wxString
& WXUNUSED(username
),
95 const wxString
& WXUNUSED(password
),
98 // Hang up the currently active dial up connection.
99 virtual bool HangUp();
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
107 return m_IsOnline
== Net_Connected
;
110 // do we have a constant net connection?
111 virtual bool IsAlwaysOnline() const;
113 // returns true if (async) dialing is in progress
114 virtual bool IsDialing() const
115 { return m_DialProcess
!= NULL
; }
117 // cancel dialing the number initiated with Dial(async = true)
118 // NB: this won't result in DISCONNECTED event being sent
119 virtual bool CancelDialing();
121 size_t GetISPNames(class wxArrayString
&) const
124 // sometimes the built-in logic for determining the online status may fail,
125 // so, in general, the user should be allowed to override it. This function
126 // allows to forcefully set the online status - whatever our internal
127 // algorithm may think about it.
128 virtual void SetOnlineStatus(bool isOnline
= true)
129 { m_IsOnline
= isOnline
? Net_Connected
: Net_No
; }
131 // set misc wxDialUpManager options
132 // --------------------------------
134 // enable automatical checks for the connection status and sending of
135 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
136 // parameter is only for Unix where we do the check manually: under
137 // Windows, the notification about the change of connection status is
140 // Returns false if couldn't set up automatic check for online status.
141 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
143 // disable automatic check for connection status change - notice that the
144 // wxEVT_DIALUP_XXX events won't be sent any more neither.
145 virtual void DisableAutoCheckOnlineStatus();
147 // under Unix, the value of well-known host is used to check whether we're
148 // connected to the internet. It's unused under Windows, but this function
149 // is always safe to call. The default value is www.yahoo.com.
150 virtual void SetWellKnownHost(const wxString
& hostname
,
152 /** Sets the commands to start up the network and to hang up
153 again. Used by the Unix implementations only.
155 virtual void SetConnectCommand(const wxString
&command
, const wxString
&hupcmd
)
156 { m_ConnectCommand
= command
; m_HangUpCommand
= hupcmd
; }
158 //private: -- Sun CC 4.2 objects to using NetConnection enum as the return
159 // type if it is declared private
161 // the possible results of testing for Online() status
164 Net_Unknown
= -1, // we couldn't learn anything
165 Net_No
, // no network connection [currently]
166 Net_Connected
// currently connected
169 // the possible net connection types
172 NetDevice_None
= 0x0000, // no network devices (authoritative)
173 NetDevice_Unknown
= 0x0001, // test doesn't work on this OS
174 NetDevice_Modem
= 0x0002, // we have a modem
175 NetDevice_LAN
= 0x0004 // a network card
179 // the current status
180 NetConnection m_IsOnline
;
182 // the connection we have with the network card
183 NetConnection m_connCard
;
185 // Can we use ifconfig to list active devices?
186 int m_CanUseIfconfig
;
188 // The path to ifconfig
189 wxString m_IfconfigPath
;
191 // Can we use ping to find hosts?
193 // The path to ping program
197 wxString m_BeaconHost
;
198 // beacon host portnumber for connect:
201 // command to connect to network
202 wxString m_ConnectCommand
;
203 // command to hang up
204 wxString m_HangUpCommand
;
207 // a timer for regular testing
208 class AutoCheckTimer
*m_timer
;
209 friend class AutoCheckTimer
;
211 // a wxProcess for dialling in background
212 class wxDialProcess
*m_DialProcess
;
213 // pid of dial process
215 friend class wxDialProcess
;
218 void CheckStatus(bool fromAsync
= false) const;
221 void CheckStatusInternal();
223 // check /proc/net (Linux only) for ppp/eth interfaces, returns the bit
224 // mask of NetDeviceType constants
227 // check output of ifconfig command for PPP/SLIP/PLIP devices, returns the
228 // bit mask of NetDeviceType constants
231 // combines the 2 possible checks for determining the connection status
232 NetConnection
CheckConnectAndPing();
235 NetConnection
CheckPing();
237 // check by connecting to host on given port.
238 NetConnection
CheckConnect();
242 class AutoCheckTimer
: public wxTimer
245 AutoCheckTimer(wxDialUpManagerImpl
*dupman
)
250 virtual void Notify()
252 wxLogTrace(_T("dialup"), wxT("Checking dial up network status."));
254 m_dupman
->CheckStatus();
258 wxDialUpManagerImpl
*m_dupman
;
261 class wxDialProcess
: public wxProcess
264 wxDialProcess(wxDialUpManagerImpl
*dupman
)
268 void Disconnect() { m_DupMan
= NULL
; }
269 virtual void OnTerminate(int WXUNUSED(pid
), int WXUNUSED(status
))
273 m_DupMan
->m_DialProcess
= NULL
;
274 m_DupMan
->CheckStatus(true);
278 wxDialUpManagerImpl
*m_DupMan
;
282 wxDialUpManagerImpl::wxDialUpManagerImpl()
285 m_connCard
= Net_Unknown
;
286 m_DialProcess
= NULL
;
288 m_CanUseIfconfig
= -1; // unknown
289 m_CanUsePing
= -1; // unknown
290 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
294 m_ConnectCommand
= _T("/usr/etc/ppp");
295 #elif defined(__LINUX__)
296 // default values for Debian/GNU linux
297 m_ConnectCommand
= _T("pon");
298 m_HangUpCommand
= _T("poff");
301 wxChar
* dial
= wxGetenv(_T("WXDIALUP_DIALCMD"));
302 wxChar
* hup
= wxGetenv(_T("WXDIALUP_HUPCMD"));
303 SetConnectCommand(dial
? wxString(dial
) : m_ConnectCommand
,
304 hup
? wxString(hup
) : m_HangUpCommand
);
307 wxDialUpManagerImpl::~wxDialUpManagerImpl()
309 if(m_timer
) delete m_timer
;
312 m_DialProcess
->Disconnect();
313 m_DialProcess
->Detach();
318 wxDialUpManagerImpl::Dial(const wxString
&isp
,
319 const wxString
& WXUNUSED(username
),
320 const wxString
& WXUNUSED(password
),
323 if(m_IsOnline
== Net_Connected
)
327 if(m_ConnectCommand
.Find(wxT("%s")))
328 cmd
.Printf(m_ConnectCommand
,m_ISPname
.c_str());
330 cmd
= m_ConnectCommand
;
334 m_DialProcess
= new wxDialProcess(this);
335 m_DialPId
= (int)wxExecute(cmd
, false, m_DialProcess
);
338 delete m_DialProcess
;
339 m_DialProcess
= NULL
;
346 return wxExecute(cmd
, /* sync */ true) == 0;
349 bool wxDialUpManagerImpl::HangUp()
351 if(m_IsOnline
== Net_No
)
355 wxLogError(_("Already dialling ISP."));
359 if(m_HangUpCommand
.Find(wxT("%s")))
360 cmd
.Printf(m_HangUpCommand
,m_ISPname
.c_str(), m_DialProcess
);
362 cmd
= m_HangUpCommand
;
363 return wxExecute(cmd
, /* sync */ true) == 0;
367 bool wxDialUpManagerImpl::CancelDialing()
371 return kill(m_DialPId
, SIGTERM
) > 0;
374 bool wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds
)
376 DisableAutoCheckOnlineStatus();
377 m_timer
= new AutoCheckTimer(this);
378 bool rc
= m_timer
->Start(nSeconds
*1000);
387 void wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
398 void wxDialUpManagerImpl::SetWellKnownHost(const wxString
& hostname
, int portno
)
400 if(hostname
.length() == 0)
402 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
407 // does hostname contain a port number?
408 wxString port
= hostname
.After(wxT(':'));
411 m_BeaconHost
= hostname
.Before(wxT(':'));
412 m_BeaconPort
= wxAtoi(port
);
416 m_BeaconHost
= hostname
;
417 m_BeaconPort
= portno
;
422 void wxDialUpManagerImpl::CheckStatus(bool fromAsync
) const
424 // This function calls the CheckStatusInternal() helper function
425 // which is OS - specific and then sends the events.
427 NetConnection oldIsOnline
= m_IsOnline
;
428 ( /* non-const */ (wxDialUpManagerImpl
*)this)->CheckStatusInternal();
430 // now send the events as appropriate: i.e. if the status changed and
431 // if we're in defined state
432 if(m_IsOnline
!= oldIsOnline
433 && m_IsOnline
!= Net_Unknown
434 && oldIsOnline
!= Net_Unknown
)
436 wxDialUpEvent
event(m_IsOnline
== Net_Connected
, ! fromAsync
);
437 (void)wxTheApp
->ProcessEvent(event
);
442 We first try to find out if ppp interface is active. If it is, we assume
443 that we're online but don't have a permanent connection (this is false if a
444 networked machine uses modem to connect to somewhere else, but we can't do
445 anything in this case anyhow).
447 If no ppp interface is detected, we check for eth interface. If it is
448 found, we check that we can, indeed, connect to an Internet host. The logic
449 here is that connection check should be fast enough in this case and we
450 don't want to give false positives in a (common) case of a machine on a LAN
451 which is not connected to the outside.
453 If we didn't find either ppp or eth interfaces, we stop here and decide
454 that we're connected. However, if couldn't check for this, we try to ping a
455 remote host just in case.
457 NB1: Checking for the interface presence can be done in 2 ways
458 a) reading /proc/net/dev under Linux
459 b) spawning ifconfig under any OS
461 The first method is faster but only works under Linux.
463 NB2: pinging, actually, means that we first try to connect "manually" to
464 a port on remove machine and if it fails, we run ping.
467 void wxDialUpManagerImpl::CheckStatusInternal()
469 m_IsOnline
= Net_Unknown
;
471 // first do quick checks to determine what kind of network devices do we
473 int netDeviceType
= CheckProcNet();
474 if ( netDeviceType
== NetDevice_Unknown
)
476 // nothing found, try ifconfig too
477 netDeviceType
= CheckIfconfig();
480 switch ( netDeviceType
)
483 // no network devices, no connection
488 // we still do ping to confirm that we're connected but we only do
489 // it once and hope that the purpose of the network card (i.e.
490 // whether it used for connecting to the Internet or just to a
491 // LAN) won't change during the program lifetime
492 if ( m_connCard
== Net_Unknown
)
494 m_connCard
= CheckConnectAndPing();
496 m_IsOnline
= m_connCard
;
499 case NetDevice_Unknown
:
500 // try to ping just in case
501 m_IsOnline
= CheckConnectAndPing();
504 case NetDevice_LAN
+ NetDevice_Modem
:
505 case NetDevice_Modem
:
506 // assume we're connected
507 m_IsOnline
= Net_Connected
;
511 wxFAIL_MSG(_T("Unexpected netDeviceType"));
515 bool wxDialUpManagerImpl::IsAlwaysOnline() const
517 wxDialUpManagerImpl
*self
= wxConstCast(this, wxDialUpManagerImpl
);
519 int netDeviceType
= self
->CheckProcNet();
520 if ( netDeviceType
== NetDevice_Unknown
)
522 // nothing found, try ifconfig too
523 netDeviceType
= self
->CheckIfconfig();
526 if ( netDeviceType
== NetDevice_Unknown
)
528 // this is the only thing we can do unfortunately...
534 // we are only permanently online if we have a network card
535 return (netDeviceType
& NetDevice_LAN
) != 0;
539 wxDialUpManagerImpl::NetConnection
wxDialUpManagerImpl::CheckConnectAndPing()
543 // first try connecting - faster
544 conn
= CheckConnect();
545 if ( conn
== Net_Unknown
)
554 wxDialUpManagerImpl::NetConnection
wxDialUpManagerImpl::CheckConnect()
556 // second method: try to connect to a well known host:
557 // This can be used under Win 9x, too!
559 struct sockaddr_in serv_addr
;
561 if((hp
= gethostbyname(m_BeaconHost
.mb_str())) == NULL
)
562 return Net_No
; // no DNS no net
564 serv_addr
.sin_family
= hp
->h_addrtype
;
565 memcpy(&serv_addr
.sin_addr
,hp
->h_addr
, hp
->h_length
);
566 serv_addr
.sin_port
= htons(m_BeaconPort
);
569 if( ( sockfd
= socket(hp
->h_addrtype
, SOCK_STREAM
, 0)) < 0)
571 return Net_Unknown
; // no info
574 if( connect(sockfd
, (struct sockaddr
*) &serv_addr
,
575 sizeof(serv_addr
)) >= 0)
578 return Net_Connected
; // we can connect, so we have a network!
580 else // failed to connect
582 if(errno
== ENETUNREACH
)
583 return Net_No
; // network is unreachable
585 return Net_Unknown
; // connect failed, but don't know why
591 wxDialUpManagerImpl::CheckProcNet()
593 // assume that the test doesn't work
594 int netDevice
= NetDevice_Unknown
;
597 if (wxFileExists(_T("/proc/net/route")))
599 // cannot use wxFile::Length because file doesn't support seeking, so
600 // use stdio directly
601 FILE *f
= fopen("/proc/net/route", "rt");
604 // now we know that we will find all devices we may have
605 netDevice
= NetDevice_None
;
609 while (fgets(output
, 256, f
) != NULL
)
611 if ( strstr(output
, "eth") ) // network card
613 netDevice
|= NetDevice_LAN
;
615 else if (strstr(output
,"ppp") // ppp
616 || strstr(output
,"sl") // slip
617 || strstr(output
,"pl")) // plip
619 netDevice
|= NetDevice_Modem
;
633 wxDialUpManagerImpl::CheckIfconfig()
636 m_CanUseIfconfig
= 0;
639 // assume that the test doesn't work
640 int netDevice
= NetDevice_Unknown
;
642 // first time check for ifconfig location
643 if ( m_CanUseIfconfig
== -1 ) // unknown
645 static const wxChar
*ifconfigLocations
[] =
647 _T("/sbin"), // Linux, FreeBSD, Darwin
648 _T("/usr/sbin"), // SunOS, Solaris, AIX, HP-UX
649 _T("/usr/etc"), // IRIX
653 for ( size_t n
= 0; n
< WXSIZEOF(ifconfigLocations
); n
++ )
655 wxString
path(ifconfigLocations
[n
]);
656 path
<< _T("/ifconfig");
658 if ( wxFileExists(path
) )
660 m_IfconfigPath
= path
;
666 if ( m_CanUseIfconfig
!= 0 ) // unknown or yes
668 wxLogNull ln
; // suppress all error messages
670 wxASSERT_MSG( m_IfconfigPath
.length(),
671 _T("can't use ifconfig if it wasn't found") );
673 wxString tmpfile
= wxGetTempFileName( wxT("_wxdialuptest") );
674 wxString cmd
= wxT("/bin/sh -c \'");
675 cmd
<< m_IfconfigPath
;
676 #if defined(__AIX__) || \
677 defined(__OSF__) || \
678 defined(__SOLARIS__) || defined (__SUNOS__)
679 // need to add -a flag
681 #elif defined(__LINUX__) || defined(__SGI__)
682 // nothing to be added to ifconfig
683 #elif defined(__FREEBSD__) || defined(__DARWIN__)
686 #elif defined(__HPUX__)
687 // VZ: a wild guess (but without it, ifconfig fails completely)
690 #if defined(__GNUG__)
691 #warning "No ifconfig information for this OS."
693 #pragma warning "No ifconfig information for this OS."
696 m_CanUseIfconfig
= 0;
699 cmd
<< wxT(" >") << tmpfile
<< wxT('\'');
700 /* I tried to add an option to wxExecute() to not close stdout,
701 so we could let ifconfig write directly to the tmpfile, but
702 this does not work. That should be faster, as it doesn´t call
703 the shell first. I have no idea why. :-( (KB) */
704 if ( wxExecute(cmd
,true /* sync */) == 0 )
706 m_CanUseIfconfig
= 1;
708 if( file
.Open(tmpfile
) )
711 if ( file
.ReadAll(&output
) )
713 // FIXME shouldn't we grep for "^ppp"? (VZ)
715 bool hasModem
= false,
718 #if defined(__SOLARIS__) || defined (__SUNOS__)
719 // dialup device under SunOS/Solaris
720 hasModem
= strstr(output
.fn_str(),"ipdptp") != (char *)NULL
;
721 hasLAN
= strstr(output
.fn_str(), "hme") != (char *)NULL
;
722 #elif defined(__LINUX__) || defined (__FREEBSD__)
723 hasModem
= strstr(output
.fn_str(),"ppp") // ppp
724 || strstr(output
.fn_str(),"sl") // slip
725 || strstr(output
.fn_str(),"pl"); // plip
726 hasLAN
= strstr(output
.fn_str(), "eth") != NULL
;
727 #elif defined(__SGI__) // IRIX
728 hasModem
= strstr(output
.fn_str(), "ppp") != NULL
; // PPP
729 #elif defined(__HPUX__)
730 // if could run ifconfig on interface, then it exists
734 netDevice
= NetDevice_None
;
736 netDevice
|= NetDevice_Modem
;
738 netDevice
|= NetDevice_LAN
;
740 //else: error reading the file
742 //else: error opening the file
744 else // could not run ifconfig correctly
746 m_CanUseIfconfig
= 0; // don´t try again
749 (void) wxRemoveFile(tmpfile
);
756 wxDialUpManagerImpl::NetConnection
wxDialUpManagerImpl::CheckPing()
758 // First time check for ping location. We only use the variant
759 // which does not take arguments, a la GNU.
760 if(m_CanUsePing
== -1) // unknown
763 if (wxFileExists( wxT("SYS$SYSTEM:TCPIP$PING.EXE") ))
764 m_PingPath
= wxT("$SYS$SYSTEM:TCPIP$PING");
765 #elif defined(__AIX__)
766 m_PingPath
= _T("/etc/ping");
767 #elif defined(__SGI__)
768 m_PingPath
= _T("/usr/etc/ping");
770 if (wxFileExists( wxT("/bin/ping") ))
771 m_PingPath
= wxT("/bin/ping");
772 else if (wxFileExists( wxT("/usr/sbin/ping") ))
773 m_PingPath
= wxT("/usr/sbin/ping");
783 // we didn't find ping
787 wxLogNull ln
; // suppress all error messages
788 wxASSERT(m_PingPath
.length());
790 cmd
<< m_PingPath
<< wxT(' ');
791 #if defined(__SOLARIS__) || defined (__SUNOS__)
792 // nothing to add to ping command
793 #elif defined(__AIX__) || \
794 defined (__BSD__) || \
795 defined(__LINUX__) || \
796 defined(__OSF__) || \
797 defined(__SGI__) || \
799 cmd
<< wxT("-c 1 "); // only ping once
800 #elif defined(__HPUX__)
801 cmd
<< wxT("64 1 "); // only ping once (need also specify the packet size)
803 #if defined(__GNUG__)
804 #warning "No Ping information for this OS."
806 #pragma warning "No Ping information for this OS."
813 if(wxExecute(cmd
, true /* sync */) == 0)
814 return Net_Connected
;
820 wxDialUpManager
*wxDialUpManager::Create()
822 return new wxDialUpManagerImpl
;
825 #endif // wxUSE_DIALUP_MANAGER