1 // -*- c++ -*- ////////////////////////////////////////////////////////////////
2 // Name: src/unix/dialup.cpp
3 // Purpose: Network related wxWidgets classes and functions
4 // Author: Karsten Ballüder
7 // Copyright: (c) Karsten Ballüder
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // for compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #if wxUSE_DIALUP_MANAGER
16 #include "wx/dialup.h"
19 #include "wx/string.h"
28 #include "wx/filename.h"
30 #include "wx/process.h"
38 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
44 wxDEFINE_EVENT( wxEVT_DIALUP_CONNECTED
, wxDialUpEvent
);
45 wxDEFINE_EVENT( wxEVT_DIALUP_DISCONNECTED
, wxDialUpEvent
);
47 // ----------------------------------------------------------------------------
48 // A class which groups functions dealing with connecting to the network from a
49 // workstation using dial-up access to the net. There is at most one instance
50 // of this class in the program accessed via GetDialUpManager().
51 // ----------------------------------------------------------------------------
55 * 1. more configurability for Unix: i.e. how to initiate the connection, how
56 * to check for online status, &c.
57 * 2. add a "long Dial(long connectionId = -1)" function which asks the user
58 * about which connection to dial (this may be done using native dialogs
59 * under NT, need generic dialogs for all others) and returns the identifier
60 * of the selected connection (it's opaque to the application) - it may be
61 * reused later to dial the same connection later (or use strings instead of
63 * 3. add an async version of dialing functions which notify the caller about
64 * the progress (or may be even start another thread to monitor it)
65 * 4. the static creation/accessor functions are not MT-safe - but is this
66 * really crucial? I think we may suppose they're always called from the
70 class WXDLLEXPORT wxDialUpManagerImpl
: public wxDialUpManager
73 wxDialUpManagerImpl();
74 virtual ~wxDialUpManagerImpl();
76 /** Could the dialup manager be initialized correctly? If this function
77 returns false, no other functions will work neither, so it's a good idea
78 to call this function and check its result before calling any other
79 wxDialUpManager methods.
81 virtual bool IsOk() const
84 /** The simplest way to initiate a dial up: this function dials the given
85 ISP (exact meaning of the parameter depends on the platform), returns
86 true on success or false on failure and logs the appropriate error
87 message in the latter case.
88 @param nameOfISP optional paramater for dial program
89 @param username unused
90 @param password unused
92 virtual bool Dial(const wxString
& nameOfISP
,
93 const wxString
& WXUNUSED(username
),
94 const wxString
& WXUNUSED(password
),
97 // Hang up the currently active dial up connection.
98 virtual bool HangUp();
100 // returns true if the computer is connected to the network: under Windows,
101 // this just means that a RAS connection exists, under Unix we check that
102 // the "well-known host" (as specified by SetWellKnownHost) is reachable
103 virtual bool IsOnline() const
106 return m_IsOnline
== Net_Connected
;
109 // do we have a constant net connection?
110 virtual bool IsAlwaysOnline() const;
112 // returns true if (async) dialing is in progress
113 virtual bool IsDialing() const
114 { return m_DialProcess
!= NULL
; }
116 // cancel dialing the number initiated with Dial(async = true)
117 // NB: this won't result in DISCONNECTED event being sent
118 virtual bool CancelDialing();
120 size_t GetISPNames(class wxArrayString
&) const
123 // sometimes the built-in logic for determining the online status may fail,
124 // so, in general, the user should be allowed to override it. This function
125 // allows to forcefully set the online status - whatever our internal
126 // algorithm may think about it.
127 virtual void SetOnlineStatus(bool isOnline
= true)
128 { m_IsOnline
= isOnline
? Net_Connected
: Net_No
; }
130 // set misc wxDialUpManager options
131 // --------------------------------
133 // enable automatical checks for the connection status and sending of
134 // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
135 // parameter is only for Unix where we do the check manually: under
136 // Windows, the notification about the change of connection status is
139 // Returns false if couldn't set up automatic check for online status.
140 virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds
);
142 // disable automatic check for connection status change - notice that the
143 // wxEVT_DIALUP_XXX events won't be sent any more neither.
144 virtual void DisableAutoCheckOnlineStatus();
146 // under Unix, the value of well-known host is used to check whether we're
147 // connected to the internet. It's unused under Windows, but this function
148 // is always safe to call. The default value is www.yahoo.com.
149 virtual void SetWellKnownHost(const wxString
& hostname
,
151 /** Sets the commands to start up the network and to hang up
152 again. Used by the Unix implementations only.
154 virtual void SetConnectCommand(const wxString
&command
, const wxString
&hupcmd
)
155 { m_ConnectCommand
= command
; m_HangUpCommand
= hupcmd
; }
157 //private: -- Sun CC 4.2 objects to using NetConnection enum as the return
158 // type if it is declared private
160 // the possible results of testing for Online() status
163 Net_Unknown
= -1, // we couldn't learn anything
164 Net_No
, // no network connection [currently]
165 Net_Connected
// currently connected
168 // the possible net connection types
171 NetDevice_None
= 0x0000, // no network devices (authoritative)
172 NetDevice_Unknown
= 0x0001, // test doesn't work on this OS
173 NetDevice_Modem
= 0x0002, // we have a modem
174 NetDevice_LAN
= 0x0004 // a network card
178 // the current status
179 NetConnection m_IsOnline
;
181 // the connection we have with the network card
182 NetConnection m_connCard
;
184 // Can we use ifconfig to list active devices?
185 int m_CanUseIfconfig
;
187 // The path to ifconfig
188 wxString m_IfconfigPath
;
190 // Can we use ping to find hosts?
192 // The path to ping program
196 wxString m_BeaconHost
;
197 // beacon host portnumber for connect:
200 // command to connect to network
201 wxString m_ConnectCommand
;
202 // command to hang up
203 wxString m_HangUpCommand
;
206 // a timer for regular testing
207 class AutoCheckTimer
*m_timer
;
208 friend class AutoCheckTimer
;
210 // a wxProcess for dialling in background
211 class wxDialProcess
*m_DialProcess
;
212 // pid of dial process
214 friend class wxDialProcess
;
217 void CheckStatus(bool fromAsync
= false) const;
220 void CheckStatusInternal();
222 // check /proc/net (Linux only) for ppp/eth interfaces, returns the bit
223 // mask of NetDeviceType constants
226 // check output of ifconfig command for PPP/SLIP/PLIP devices, returns the
227 // bit mask of NetDeviceType constants
230 // combines the 2 possible checks for determining the connection status
231 NetConnection
CheckConnectAndPing();
234 NetConnection
CheckPing();
236 // check by connecting to host on given port.
237 NetConnection
CheckConnect();
241 class AutoCheckTimer
: public wxTimer
244 AutoCheckTimer(wxDialUpManagerImpl
*dupman
)
249 virtual void Notify()
251 wxLogTrace(wxT("dialup"), wxT("Checking dial up network status."));
253 m_dupman
->CheckStatus();
257 wxDialUpManagerImpl
*m_dupman
;
260 class wxDialProcess
: public wxProcess
263 wxDialProcess(wxDialUpManagerImpl
*dupman
)
267 void Disconnect() { m_DupMan
= NULL
; }
268 virtual void OnTerminate(int WXUNUSED(pid
), int WXUNUSED(status
))
272 m_DupMan
->m_DialProcess
= NULL
;
273 m_DupMan
->CheckStatus(true);
277 wxDialUpManagerImpl
*m_DupMan
;
281 wxDialUpManagerImpl::wxDialUpManagerImpl()
284 m_connCard
= Net_Unknown
;
285 m_DialProcess
= NULL
;
287 m_CanUseIfconfig
= -1; // unknown
288 m_CanUsePing
= -1; // unknown
289 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
293 m_ConnectCommand
= wxT("/usr/etc/ppp");
294 #elif defined(__LINUX__)
295 // default values for Debian/GNU linux
296 m_ConnectCommand
= wxT("pon");
297 m_HangUpCommand
= wxT("poff");
300 wxChar
* dial
= wxGetenv(wxT("WXDIALUP_DIALCMD"));
301 wxChar
* hup
= wxGetenv(wxT("WXDIALUP_HUPCMD"));
302 SetConnectCommand(dial
? wxString(dial
) : m_ConnectCommand
,
303 hup
? wxString(hup
) : m_HangUpCommand
);
306 wxDialUpManagerImpl::~wxDialUpManagerImpl()
308 if(m_timer
) delete m_timer
;
311 m_DialProcess
->Disconnect();
312 m_DialProcess
->Detach();
317 wxDialUpManagerImpl::Dial(const wxString
&isp
,
318 const wxString
& WXUNUSED(username
),
319 const wxString
& WXUNUSED(password
),
322 if(m_IsOnline
== Net_Connected
)
326 if(m_ConnectCommand
.Find(wxT("%s")))
327 cmd
.Printf(m_ConnectCommand
,m_ISPname
.c_str());
329 cmd
= m_ConnectCommand
;
333 m_DialProcess
= new wxDialProcess(this);
334 m_DialPId
= (int)wxExecute(cmd
, false, m_DialProcess
);
337 wxDELETE(m_DialProcess
);
344 return wxExecute(cmd
, /* sync */ true) == 0;
347 bool wxDialUpManagerImpl::HangUp()
349 if(m_IsOnline
== Net_No
)
353 wxLogError(_("Already dialling ISP."));
357 if(m_HangUpCommand
.Find(wxT("%s")))
358 cmd
.Printf(m_HangUpCommand
,m_ISPname
.c_str(), m_DialProcess
);
360 cmd
= m_HangUpCommand
;
361 return wxExecute(cmd
, /* sync */ true) == 0;
365 bool wxDialUpManagerImpl::CancelDialing()
369 return kill(m_DialPId
, SIGTERM
) > 0;
372 bool wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds
)
374 DisableAutoCheckOnlineStatus();
375 m_timer
= new AutoCheckTimer(this);
376 bool rc
= m_timer
->Start(nSeconds
*1000);
384 void wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
394 void wxDialUpManagerImpl::SetWellKnownHost(const wxString
& hostname
, int portno
)
396 if( hostname
.empty() )
398 m_BeaconHost
= WXDIALUP_MANAGER_DEFAULT_BEACONHOST
;
403 // does hostname contain a port number?
404 wxString port
= hostname
.After(wxT(':'));
407 m_BeaconHost
= hostname
.Before(wxT(':'));
408 m_BeaconPort
= wxAtoi(port
);
412 m_BeaconHost
= hostname
;
413 m_BeaconPort
= portno
;
418 void wxDialUpManagerImpl::CheckStatus(bool fromAsync
) const
420 // This function calls the CheckStatusInternal() helper function
421 // which is OS - specific and then sends the events.
423 NetConnection oldIsOnline
= m_IsOnline
;
424 ( /* non-const */ (wxDialUpManagerImpl
*)this)->CheckStatusInternal();
426 // now send the events as appropriate: i.e. if the status changed and
427 // if we're in defined state
428 if(m_IsOnline
!= oldIsOnline
429 && m_IsOnline
!= Net_Unknown
430 && oldIsOnline
!= Net_Unknown
)
432 wxDialUpEvent
event(m_IsOnline
== Net_Connected
, ! fromAsync
);
433 (void)wxTheApp
->ProcessEvent(event
);
438 We first try to find out if ppp interface is active. If it is, we assume
439 that we're online but don't have a permanent connection (this is false if a
440 networked machine uses modem to connect to somewhere else, but we can't do
441 anything in this case anyhow).
443 If no ppp interface is detected, we check for eth interface. If it is
444 found, we check that we can, indeed, connect to an Internet host. The logic
445 here is that connection check should be fast enough in this case and we
446 don't want to give false positives in a (common) case of a machine on a LAN
447 which is not connected to the outside.
449 If we didn't find either ppp or eth interfaces, we stop here and decide
450 that we're connected. However, if couldn't check for this, we try to ping a
451 remote host just in case.
453 NB1: Checking for the interface presence can be done in 2 ways
454 a) reading /proc/net/dev under Linux
455 b) spawning ifconfig under any OS
457 The first method is faster but only works under Linux.
459 NB2: pinging, actually, means that we first try to connect "manually" to
460 a port on remove machine and if it fails, we run ping.
463 void wxDialUpManagerImpl::CheckStatusInternal()
465 m_IsOnline
= Net_Unknown
;
467 // first do quick checks to determine what kind of network devices do we
469 int netDeviceType
= CheckProcNet();
470 if ( netDeviceType
== NetDevice_Unknown
)
472 // nothing found, try ifconfig too
473 netDeviceType
= CheckIfconfig();
476 switch ( netDeviceType
)
479 // no network devices, no connection
484 // we still do ping to confirm that we're connected but we only do
485 // it once and hope that the purpose of the network card (i.e.
486 // whether it used for connecting to the Internet or just to a
487 // LAN) won't change during the program lifetime
488 if ( m_connCard
== Net_Unknown
)
490 m_connCard
= CheckConnectAndPing();
492 m_IsOnline
= m_connCard
;
495 case NetDevice_Unknown
:
496 // try to ping just in case
497 m_IsOnline
= CheckConnectAndPing();
500 case NetDevice_LAN
+ NetDevice_Modem
:
501 case NetDevice_Modem
:
502 // assume we're connected
503 m_IsOnline
= Net_Connected
;
507 wxFAIL_MSG(wxT("Unexpected netDeviceType"));
511 bool wxDialUpManagerImpl::IsAlwaysOnline() const
513 wxDialUpManagerImpl
*self
= wxConstCast(this, wxDialUpManagerImpl
);
515 int netDeviceType
= self
->CheckProcNet();
516 if ( netDeviceType
== NetDevice_Unknown
)
518 // nothing found, try ifconfig too
519 netDeviceType
= self
->CheckIfconfig();
522 if ( netDeviceType
== NetDevice_Unknown
)
524 // this is the only thing we can do unfortunately...
530 // we are only permanently online if we have a network card
531 return (netDeviceType
& NetDevice_LAN
) != 0;
535 wxDialUpManagerImpl::NetConnection
wxDialUpManagerImpl::CheckConnectAndPing()
539 // first try connecting - faster
540 conn
= CheckConnect();
541 if ( conn
== Net_Unknown
)
550 wxDialUpManagerImpl::NetConnection
wxDialUpManagerImpl::CheckConnect()
552 // second method: try to connect to a well known host:
553 // This can be used under Win 9x, too!
555 struct sockaddr_in serv_addr
;
557 if((hp
= gethostbyname(m_BeaconHost
.mb_str())) == NULL
)
558 return Net_No
; // no DNS no net
560 serv_addr
.sin_family
= hp
->h_addrtype
;
561 memcpy(&serv_addr
.sin_addr
,hp
->h_addr
, hp
->h_length
);
562 serv_addr
.sin_port
= htons(m_BeaconPort
);
565 if( ( sockfd
= socket(hp
->h_addrtype
, SOCK_STREAM
, 0)) < 0)
567 return Net_Unknown
; // no info
570 if( connect(sockfd
, (struct sockaddr
*) &serv_addr
,
571 sizeof(serv_addr
)) >= 0)
574 return Net_Connected
; // we can connect, so we have a network!
576 else // failed to connect
579 if(errno
== ENETUNREACH
)
580 return Net_No
; // network is unreachable
583 return Net_Unknown
; // connect failed, but don't know why
589 wxDialUpManagerImpl::CheckProcNet()
591 // assume that the test doesn't work
592 int netDevice
= NetDevice_Unknown
;
595 if (wxFileExists(wxT("/proc/net/route")))
597 // cannot use wxFile::Length because file doesn't support seeking, so
598 // use stdio directly
599 FILE *f
= fopen("/proc/net/route", "rt");
602 // now we know that we will find all devices we may have
603 netDevice
= NetDevice_None
;
607 while (fgets(output
, 256, f
) != NULL
)
609 // Test for the known network interface names
610 if ( strstr(output
, "eth")
611 || strstr(output
, "wlan")
612 || strstr(output
, "ath") )
614 netDevice
|= NetDevice_LAN
;
616 else if (strstr(output
,"ppp") // ppp
617 || strstr(output
,"sl") // slip
618 || strstr(output
,"pl")) // plip
620 netDevice
|= NetDevice_Modem
;
634 wxDialUpManagerImpl::CheckIfconfig()
637 m_CanUseIfconfig
= 0;
640 // assume that the test doesn't work
641 int netDevice
= NetDevice_Unknown
;
643 // first time check for ifconfig location
644 if ( m_CanUseIfconfig
== -1 ) // unknown
646 static const wxChar
*const ifconfigLocations
[] =
648 wxT("/sbin"), // Linux, FreeBSD, Darwin
649 wxT("/usr/sbin"), // SunOS, Solaris, AIX, HP-UX
650 wxT("/usr/etc"), // IRIX
651 wxT("/etc"), // AIX 5
654 for ( size_t n
= 0; n
< WXSIZEOF(ifconfigLocations
); n
++ )
656 wxString
path(ifconfigLocations
[n
]);
657 path
<< wxT("/ifconfig");
659 if ( wxFileExists(path
) )
661 m_IfconfigPath
= path
;
667 if ( m_CanUseIfconfig
!= 0 ) // unknown or yes
669 wxLogNull ln
; // suppress all error messages
671 wxASSERT_MSG( !m_IfconfigPath
.empty(),
672 wxT("can't use ifconfig if it wasn't found") );
674 wxString tmpfile
= wxFileName::CreateTempFileName( wxT("_wxdialuptest") );
675 wxString cmd
= wxT("/bin/sh -c \'");
676 cmd
<< m_IfconfigPath
;
677 #if defined(__AIX__) || \
678 defined(__NETBSD__) || \
679 defined(__OSF__) || \
680 defined(__SOLARIS__) || defined (__SUNOS__)
681 // need to add -a flag
683 #elif defined(__LINUX__) || defined(__SGI__) || defined(__OPENBSD__)
684 // nothing to be added to ifconfig
685 #elif defined(__FREEBSD__) || defined(__DARWIN__) || defined(__QNX__)
688 #elif defined(__HPUX__)
689 // VZ: a wild guess (but without it, ifconfig fails completely)
692 #if defined(__GNUG__)
693 #warning "No ifconfig information for this OS."
695 #pragma warning "No ifconfig information for this OS."
698 m_CanUseIfconfig
= 0;
701 cmd
<< wxT(" >") << tmpfile
<< wxT('\'');
702 /* I tried to add an option to wxExecute() to not close stdout,
703 so we could let ifconfig write directly to the tmpfile, but
704 this does not work. That should be faster, as it doesn't call
705 the shell first. I have no idea why. :-( (KB) */
706 if ( wxExecute(cmd
,true /* sync */) == 0 )
708 m_CanUseIfconfig
= 1;
710 if( file
.Open(tmpfile
) )
713 if ( file
.ReadAll(&output
) )
715 // FIXME shouldn't we grep for "^ppp"? (VZ)
717 bool hasModem
= false,
720 #if defined(__SOLARIS__) || defined (__SUNOS__)
721 // dialup device under SunOS/Solaris
722 hasModem
= strstr(output
.fn_str(),"ipdptp") != NULL
;
723 hasLAN
= strstr(output
.fn_str(), "hme") != NULL
;
724 #elif defined(__LINUX__) || defined (__FREEBSD__) || defined (__QNX__) || \
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
= wxT("/etc/ping");
770 #elif defined(__SGI__)
771 m_PingPath
= wxT("/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__) || \
803 cmd
<< wxT("-c 1 "); // only ping once
804 #elif defined(__HPUX__)
805 cmd
<< wxT("64 1 "); // only ping once (need also specify the packet size)
807 #if defined(__GNUG__)
808 #warning "No Ping information for this OS."
810 #pragma warning "No Ping information for this OS."
817 if(wxExecute(cmd
, true /* sync */) == 0)
818 return Net_Connected
;
824 wxDialUpManager
*wxDialUpManager::Create()
826 return new wxDialUpManagerImpl
;
829 #endif // wxUSE_DIALUP_MANAGER