dialup.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 826 行 · 第 1/2 页
CPP
826 行
// does hostname contain a port number?
wxString port = hostname.After(wxT(':'));
if(port.Length())
{
m_BeaconHost = hostname.Before(wxT(':'));
m_BeaconPort = wxAtoi(port);
}
else
{
m_BeaconHost = hostname;
m_BeaconPort = portno;
}
}
void wxDialUpManagerImpl::CheckStatus(bool fromAsync) const
{
// This function calls the CheckStatusInternal() helper function
// which is OS - specific and then sends the events.
NetConnection oldIsOnline = m_IsOnline;
( /* non-const */ (wxDialUpManagerImpl *)this)->CheckStatusInternal();
// now send the events as appropriate: i.e. if the status changed and
// if we're in defined state
if(m_IsOnline != oldIsOnline
&& m_IsOnline != Net_Unknown
&& oldIsOnline != Net_Unknown )
{
wxDialUpEvent event(m_IsOnline == Net_Connected, ! fromAsync);
(void)wxTheApp->ProcessEvent(event);
}
}
/*
We first try to find out if ppp interface is active. If it is, we assume
that we're online but don't have a permanent connection (this is false if a
networked machine uses modem to connect to somewhere else, but we can't do
anything in this case anyhow).
If no ppp interface is detected, we check for eth interface. If it is
found, we check that we can, indeed, connect to an Internet host. The logic
here is that connection check should be fast enough in this case and we
don't want to give false positives in a (common) case of a machine on a LAN
which is not connected to the outside.
If we didn't find either ppp or eth interfaces, we stop here and decide
that we're connected. However, if couldn't check for this, we try to ping a
remote host just in case.
NB1: Checking for the interface presence can be done in 2 ways
a) reading /proc/net/dev under Linux
b) spawning ifconfig under any OS
The first method is faster but only works under Linux.
NB2: pinging, actually, means that we first try to connect "manually" to
a port on remove machine and if it fails, we run ping.
*/
void wxDialUpManagerImpl::CheckStatusInternal()
{
m_IsOnline = Net_Unknown;
// first do quick checks to determine what kind of network devices do we
// have
int netDeviceType = CheckProcNet();
if ( netDeviceType == NetDevice_Unknown )
{
// nothing found, try ifconfig too
netDeviceType = CheckIfconfig();
}
switch ( netDeviceType )
{
case NetDevice_None:
// no network devices, no connection
m_IsOnline = Net_No;
break;
case NetDevice_LAN:
// we still do ping to confirm that we're connected but we only do
// it once and hope that the purpose of the network card (i.e.
// whether it used for connecting to the Internet or just to a
// LAN) won't change during the program lifetime
if ( m_connCard == Net_Unknown )
{
m_connCard = CheckConnectAndPing();
}
m_IsOnline = m_connCard;
break;
case NetDevice_Unknown:
// try to ping just in case
m_IsOnline = CheckConnectAndPing();
break;
case NetDevice_LAN + NetDevice_Modem:
case NetDevice_Modem:
// assume we're connected
m_IsOnline = Net_Connected;
break;
default:
wxFAIL_MSG(_T("Unexpected netDeviceType"));
}
}
bool wxDialUpManagerImpl::IsAlwaysOnline() const
{
wxDialUpManagerImpl *self = wxConstCast(this, wxDialUpManagerImpl);
int netDeviceType = self->CheckProcNet();
if ( netDeviceType == NetDevice_Unknown )
{
// nothing found, try ifconfig too
netDeviceType = self->CheckIfconfig();
}
if ( netDeviceType == NetDevice_Unknown )
{
// this is the only thing we can do unfortunately...
self->HangUp();
return IsOnline();
}
else
{
// we are only permanently online if we have a network card
return (netDeviceType & NetDevice_LAN) != 0;
}
}
wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckConnectAndPing()
{
NetConnection conn;
// first try connecting - faster
conn = CheckConnect();
if ( conn == Net_Unknown )
{
// try pinging too
conn = CheckPing();
}
return conn;
}
wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckConnect()
{
// second method: try to connect to a well known host:
// This can be used under Win 9x, too!
struct hostent *hp;
struct sockaddr_in serv_addr;
if((hp = gethostbyname(m_BeaconHost.mb_str())) == NULL)
return Net_No; // no DNS no net
serv_addr.sin_family = hp->h_addrtype;
memcpy(&serv_addr.sin_addr,hp->h_addr, hp->h_length);
serv_addr.sin_port = htons(m_BeaconPort);
int sockfd;
if( ( sockfd = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0)
{
return Net_Unknown; // no info
}
if( connect(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) >= 0)
{
close(sockfd);
return Net_Connected; // we can connect, so we have a network!
}
else // failed to connect
{
if(errno == ENETUNREACH)
return Net_No; // network is unreachable
else
return Net_Unknown; // connect failed, but don't know why
}
}
int
wxDialUpManagerImpl::CheckProcNet()
{
// assume that the test doesn't work
int netDevice = NetDevice_Unknown;
#ifdef __LINUX__
if (wxFileExists(_T("/proc/net/route")))
{
// cannot use wxFile::Length because file doesn't support seeking, so
// use stdio directly
FILE *f = fopen("/proc/net/route", "rt");
if (f != NULL)
{
// now we know that we will find all devices we may have
netDevice = NetDevice_None;
char output[256];
while (fgets(output, 256, f) != NULL)
{
if ( strstr(output, "eth") ) // network card
{
netDevice |= NetDevice_LAN;
}
else if (strstr(output,"ppp") // ppp
|| strstr(output,"sl") // slip
|| strstr(output,"pl")) // plip
{
netDevice |= NetDevice_Modem;
}
}
fclose(f);
}
}
#endif // __LINUX__
return netDevice;
}
int
wxDialUpManagerImpl::CheckIfconfig()
{
#ifdef __VMS
m_CanUseIfconfig = 0;
return -1;
#else
// assume that the test doesn't work
int netDevice = NetDevice_Unknown;
// first time check for ifconfig location
if ( m_CanUseIfconfig == -1 ) // unknown
{
static const wxChar *ifconfigLocations[] =
{
_T("/sbin"), // Linux, FreeBSD, Darwin
_T("/usr/sbin"), // SunOS, Solaris, AIX, HP-UX
_T("/usr/etc"), // IRIX
_T("/etc"), // AIX 5
};
for ( size_t n = 0; n < WXSIZEOF(ifconfigLocations); n++ )
{
wxString path(ifconfigLocations[n]);
path << _T("/ifconfig");
if ( wxFileExists(path) )
{
m_IfconfigPath = path;
break;
}
}
}
if ( m_CanUseIfconfig != 0 ) // unknown or yes
{
wxLogNull ln; // suppress all error messages
wxASSERT_MSG( m_IfconfigPath.length(),
_T("can't use ifconfig if it wasn't found") );
wxString tmpfile = wxGetTempFileName( wxT("_wxdialuptest") );
wxString cmd = wxT("/bin/sh -c \'");
cmd << m_IfconfigPath;
#if defined(__AIX__) || defined(__SOLARIS__) || defined (__SUNOS__)
// need to add -a flag
cmd << wxT(" -a");
#elif defined(__LINUX__) || defined(__SGI__)
// nothing to be added to ifconfig
#elif defined(__FREEBSD__) || defined(__DARWIN__)
// add -l flag
cmd << wxT(" -l");
#elif defined(__HPUX__)
// VZ: a wild guess (but without it, ifconfig fails completely)
cmd << wxT(" ppp0");
#else
#if defined(__GNUG__)
#warning "No ifconfig information for this OS."
#else
#pragma warning "No ifconfig information for this OS."
#endif
m_CanUseIfconfig = 0;
return -1;
#endif
cmd << wxT(" >") << tmpfile << wxT('\'');
/* I tried to add an option to wxExecute() to not close stdout,
so we could let ifconfig write directly to the tmpfile, but
this does not work. That should be faster, as it doesn磘 call
the shell first. I have no idea why. :-( (KB) */
if ( wxExecute(cmd,true /* sync */) == 0 )
{
m_CanUseIfconfig = 1;
wxFFile file;
if( file.Open(tmpfile) )
{
wxString output;
if ( file.ReadAll(&output) )
{
// FIXME shouldn't we grep for "^ppp"? (VZ)
bool hasModem = false,
hasLAN = false;
#if defined(__SOLARIS__) || defined (__SUNOS__)
// dialup device under SunOS/Solaris
hasModem = strstr(output.fn_str(),"ipdptp") != (char *)NULL;
hasLAN = strstr(output.fn_str(), "hme") != (char *)NULL;
#elif defined(__LINUX__) || defined (__FREEBSD__)
hasModem = strstr(output.fn_str(),"ppp") // ppp
|| strstr(output.fn_str(),"sl") // slip
|| strstr(output.fn_str(),"pl"); // plip
hasLAN = strstr(output.fn_str(), "eth") != NULL;
#elif defined(__SGI__) // IRIX
hasModem = strstr(output.fn_str(), "ppp") != NULL; // PPP
#elif defined(__HPUX__)
// if could run ifconfig on interface, then it exists
hasModem = true;
#endif
netDevice = NetDevice_None;
if ( hasModem )
netDevice |= NetDevice_Modem;
if ( hasLAN )
netDevice |= NetDevice_LAN;
}
//else: error reading the file
}
//else: error opening the file
}
else // could not run ifconfig correctly
{
m_CanUseIfconfig = 0; // don磘 try again
}
(void) wxRemoveFile(tmpfile);
}
return netDevice;
#endif
}
wxDialUpManagerImpl::NetConnection wxDialUpManagerImpl::CheckPing()
{
// First time check for ping location. We only use the variant
// which does not take arguments, a la GNU.
if(m_CanUsePing == -1) // unknown
{
#ifdef __VMS
if (wxFileExists( wxT("SYS$SYSTEM:TCPIP$PING.EXE") ))
m_PingPath = wxT("$SYS$SYSTEM:TCPIP$PING");
#elif defined(__AIX__)
m_PingPath = _T("/etc/ping");
#elif defined(__SGI__)
m_PingPath = _T("/usr/etc/ping");
#else
if (wxFileExists( wxT("/bin/ping") ))
m_PingPath = wxT("/bin/ping");
else if (wxFileExists( wxT("/usr/sbin/ping") ))
m_PingPath = wxT("/usr/sbin/ping");
#endif
if (!m_PingPath)
{
m_CanUsePing = 0;
}
}
if(! m_CanUsePing)
{
// we didn't find ping
return Net_Unknown;
}
wxLogNull ln; // suppress all error messages
wxASSERT(m_PingPath.length());
wxString cmd;
cmd << m_PingPath << wxT(' ');
#if defined(__SOLARIS__) || defined (__SUNOS__)
// nothing to add to ping command
#elif defined(__AIX__) || defined(__LINUX__) || defined (__BSD__) || defined(__VMS) || defined(__SGI__)
cmd << wxT("-c 1 "); // only ping once
#elif defined(__HPUX__)
cmd << wxT("64 1 "); // only ping once (need also specify the packet size)
#else
#if defined(__GNUG__)
#warning "No Ping information for this OS."
#else
#pragma warning "No Ping information for this OS."
#endif
m_CanUsePing = 0;
return Net_Unknown;
#endif
cmd << m_BeaconHost;
if(wxExecute(cmd, true /* sync */) == 0)
return Net_Connected;
else
return Net_No;
}
/* static */
wxDialUpManager *wxDialUpManager::Create()
{
return new wxDialUpManagerImpl;
}
#endif // wxUSE_DIALUP_MANAGER
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?