dialup.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 826 行 · 第 1/2 页

CPP
826
字号
// -*- c++ -*- ////////////////////////////////////////////////////////////////
// Name:        unix/dialup.cpp
// Purpose:     Network related wxWidgets classes and functions
// Author:      Karsten Ball黡er
// Modified by:
// Created:     03.10.99
// RCS-ID:      $Id: dialup.cpp,v 1.44.2.1 2005/10/06 22:52:03 MW Exp $
// Copyright:   (c) Karsten Ball黡er
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#   pragma implementation "dialup.h"
#endif

// for compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#include "wx/setup.h"

#if wxUSE_DIALUP_MANAGER

#ifndef  WX_PRECOMP
#   include "wx/defs.h"
#endif // !PCH

#include "wx/string.h"
#include "wx/event.h"
#include "wx/dialup.h"
#include "wx/timer.h"
#include "wx/filefn.h"
#include "wx/utils.h"
#include "wx/log.h"
#include "wx/ffile.h"
#include "wx/process.h"
#include "wx/intl.h"
#include "wx/app.h"
#include "wx/wxchar.h"

#include <stdlib.h>

#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#define __STRICT_ANSI__
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

DEFINE_EVENT_TYPE(wxEVT_DIALUP_CONNECTED)
DEFINE_EVENT_TYPE(wxEVT_DIALUP_DISCONNECTED)

// ----------------------------------------------------------------------------
// A class which groups functions dealing with connecting to the network from a
// workstation using dial-up access to the net. There is at most one instance
// of this class in the program accessed via GetDialUpManager().
// ----------------------------------------------------------------------------

/* TODO
 *
 * 1. more configurability for Unix: i.e. how to initiate the connection, how
 *    to check for online status, &c.
 * 2. add a "long Dial(long connectionId = -1)" function which asks the user
 *    about which connection to dial (this may be done using native dialogs
 *    under NT, need generic dialogs for all others) and returns the identifier
 *    of the selected connection (it's opaque to the application) - it may be
 *    reused later to dial the same connection later (or use strings instead of
 *    longs may be?)
 * 3. add an async version of dialing functions which notify the caller about
 *    the progress (or may be even start another thread to monitor it)
 * 4. the static creation/accessor functions are not MT-safe - but is this
 *    really crucial? I think we may suppose they're always called from the
 *    main thread?
 */

class WXDLLEXPORT wxDialUpManagerImpl : public wxDialUpManager
{
public:
   wxDialUpManagerImpl();
   ~wxDialUpManagerImpl();

   /** Could the dialup manager be initialized correctly? If this function
       returns false, no other functions will work neither, so it's a good idea
       to call this function and check its result before calling any other
       wxDialUpManager methods.
   */
   virtual bool IsOk() const
      { return true; }

   /** The simplest way to initiate a dial up: this function dials the given
       ISP (exact meaning of the parameter depends on the platform), returns
       true on success or false on failure and logs the appropriate error
       message in the latter case.
       @param nameOfISP optional paramater for dial program
       @param username unused
       @param password unused
   */
   virtual bool Dial(const wxString& nameOfISP,
                     const wxString& WXUNUSED(username),
                     const wxString& WXUNUSED(password),
                     bool async);

   // Hang up the currently active dial up connection.
   virtual bool HangUp();

   // returns true if the computer is connected to the network: under Windows,
   // this just means that a RAS connection exists, under Unix we check that
   // the "well-known host" (as specified by SetWellKnownHost) is reachable
   virtual bool IsOnline() const
      {
         CheckStatus();
         return m_IsOnline == Net_Connected;
      }

   // do we have a constant net connection?
   virtual bool IsAlwaysOnline() const;

   // returns true if (async) dialing is in progress
   virtual bool IsDialing() const
      { return m_DialProcess != NULL; }

   // cancel dialing the number initiated with Dial(async = true)
   // NB: this won't result in DISCONNECTED event being sent
   virtual bool CancelDialing();

   size_t GetISPNames(class wxArrayString &) const
      { return 0; }

   // sometimes the built-in logic for determining the online status may fail,
   // so, in general, the user should be allowed to override it. This function
   // allows to forcefully set the online status - whatever our internal
   // algorithm may think about it.
   virtual void SetOnlineStatus(bool isOnline = true)
      { m_IsOnline = isOnline ? Net_Connected : Net_No; }

   // set misc wxDialUpManager options
   // --------------------------------

   // enable automatical checks for the connection status and sending of
   // wxEVT_DIALUP_CONNECTED/wxEVT_DIALUP_DISCONNECTED events. The interval
   // parameter is only for Unix where we do the check manually: under
   // Windows, the notification about the change of connection status is
   // instantenous.
   //
   // Returns false if couldn't set up automatic check for online status.
   virtual bool EnableAutoCheckOnlineStatus(size_t nSeconds);

   // disable automatic check for connection status change - notice that the
   // wxEVT_DIALUP_XXX events won't be sent any more neither.
   virtual void DisableAutoCheckOnlineStatus();

   // under Unix, the value of well-known host is used to check whether we're
   // connected to the internet. It's unused under Windows, but this function
   // is always safe to call. The default value is www.yahoo.com.
   virtual void SetWellKnownHost(const wxString& hostname,
                                 int portno = 80);
   /** Sets the commands to start up the network and to hang up
       again. Used by the Unix implementations only.
   */
   virtual void SetConnectCommand(const wxString &command, const wxString &hupcmd)
      { m_ConnectCommand = command; m_HangUpCommand = hupcmd; }

//private: -- Sun CC 4.2 objects to using NetConnection enum as the return
//            type if it is declared private

   // the possible results of testing for Online() status
   enum NetConnection
   {
       Net_Unknown = -1,    // we couldn't learn anything
       Net_No,              // no network connection [currently]
       Net_Connected        // currently connected
   };

   // the possible net connection types
   enum NetDeviceType
   {
       NetDevice_None    = 0x0000,  // no network devices (authoritative)
       NetDevice_Unknown = 0x0001,  // test doesn't work on this OS
       NetDevice_Modem   = 0x0002,  // we have a modem
       NetDevice_LAN     = 0x0004   //         a network card
   };

private:
   // the current status
   NetConnection m_IsOnline;

   // the connection we have with the network card
   NetConnection m_connCard;

   // Can we use ifconfig to list active devices?
   int m_CanUseIfconfig;

   // The path to ifconfig
   wxString m_IfconfigPath;

   //  Can we use ping to find hosts?
   int m_CanUsePing;
   // The path to ping program
   wxString m_PingPath;

   // beacon host:
   wxString m_BeaconHost;
   // beacon host portnumber for connect:
   int m_BeaconPort;

   // command to connect to network
   wxString m_ConnectCommand;
   // command to hang up
   wxString m_HangUpCommand;
   // name of ISP
   wxString m_ISPname;
   // a timer for regular testing
   class AutoCheckTimer *m_timer;
   friend class AutoCheckTimer;

   // a wxProcess for dialling in background
   class wxDialProcess *m_DialProcess;
   // pid of dial process
   int m_DialPId;
   friend class wxDialProcess;

   // determine status
   void CheckStatus(bool fromAsync = false) const;

   // real status check
   void CheckStatusInternal();

   // check /proc/net (Linux only) for ppp/eth interfaces, returns the bit
   // mask of NetDeviceType constants
   int CheckProcNet();

   // check output of ifconfig command for PPP/SLIP/PLIP devices, returns the
   // bit mask of NetDeviceType constants
   int CheckIfconfig();

   // combines the 2 possible checks for determining the connection status
   NetConnection CheckConnectAndPing();

   // pings a host
   NetConnection CheckPing();

   // check by connecting to host on given port.
   NetConnection CheckConnect();
};


class AutoCheckTimer : public wxTimer
{
public:
   AutoCheckTimer(wxDialUpManagerImpl *dupman)
   {
       m_dupman = dupman;
   }

   virtual void Notify()
   {
       wxLogTrace(_T("dialup"), wxT("Checking dial up network status."));

       m_dupman->CheckStatus();
   }

public:
   wxDialUpManagerImpl *m_dupman;
};

class wxDialProcess : public wxProcess
{
public:
   wxDialProcess(wxDialUpManagerImpl *dupman)
      {
         m_DupMan = dupman;
      }
   void Disconnect() { m_DupMan = NULL; }
   virtual void OnTerminate(int WXUNUSED(pid), int WXUNUSED(status))
      {
         if(m_DupMan)
         {
            m_DupMan->m_DialProcess = NULL;
            m_DupMan->CheckStatus(true);
         }
      }
private:
      wxDialUpManagerImpl *m_DupMan;
};


wxDialUpManagerImpl::wxDialUpManagerImpl()
{
   m_IsOnline =
   m_connCard = Net_Unknown;
   m_DialProcess = NULL;
   m_timer = NULL;
   m_CanUseIfconfig = -1; // unknown
   m_CanUsePing = -1; // unknown
   m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST;
   m_BeaconPort = 80;

#ifdef __SGI__
   m_ConnectCommand = _T("/usr/etc/ppp");
#elif defined(__LINUX__)
   // default values for Debian/GNU linux
   m_ConnectCommand = _T("pon");
   m_HangUpCommand = _T("poff");
#endif

   wxChar * dial = wxGetenv(_T("WXDIALUP_DIALCMD"));
   wxChar * hup = wxGetenv(_T("WXDIALUP_HUPCMD"));
   SetConnectCommand(dial ? wxString(dial) : m_ConnectCommand,
                     hup ? wxString(hup) : m_HangUpCommand);
}

wxDialUpManagerImpl::~wxDialUpManagerImpl()
{
   if(m_timer) delete m_timer;
   if(m_DialProcess)
   {
      m_DialProcess->Disconnect();
      m_DialProcess->Detach();
   }
}

bool
wxDialUpManagerImpl::Dial(const wxString &isp,
                          const wxString & WXUNUSED(username),
                          const wxString & WXUNUSED(password),
                          bool async)
{
    if(m_IsOnline == Net_Connected)
        return false;
    m_ISPname = isp;
    wxString cmd;
    if(m_ConnectCommand.Find(wxT("%s")))
        cmd.Printf(m_ConnectCommand,m_ISPname.c_str());
    else
        cmd = m_ConnectCommand;

    if ( async )
    {
        m_DialProcess = new wxDialProcess(this);
        m_DialPId = (int)wxExecute(cmd, false, m_DialProcess);
        if(m_DialPId == 0)
        {
            delete m_DialProcess;
            m_DialProcess = NULL;
            return false;
        }
        else
            return true;
    }
    else
        return wxExecute(cmd, /* sync */ true) == 0;
}

bool wxDialUpManagerImpl::HangUp()
{
    if(m_IsOnline == Net_No)
        return false;
    if(IsDialing())
    {
        wxLogError(_("Already dialling ISP."));
        return false;
    }
    wxString cmd;
    if(m_HangUpCommand.Find(wxT("%s")))
        cmd.Printf(m_HangUpCommand,m_ISPname.c_str(), m_DialProcess);
    else
        cmd = m_HangUpCommand;
    return wxExecute(cmd, /* sync */ true) == 0;
}


bool wxDialUpManagerImpl::CancelDialing()
{
   if(! IsDialing())
      return false;
   return kill(m_DialPId, SIGTERM) > 0;
}

bool wxDialUpManagerImpl::EnableAutoCheckOnlineStatus(size_t nSeconds)
{
   DisableAutoCheckOnlineStatus();
   m_timer = new AutoCheckTimer(this);
   bool rc = m_timer->Start(nSeconds*1000);
   if(! rc)
   {
      delete m_timer;
      m_timer = NULL;
   }
   return rc;
}

void wxDialUpManagerImpl::DisableAutoCheckOnlineStatus()
{
   if(m_timer != NULL)
   {
      m_timer->Stop();
      delete m_timer;
      m_timer = NULL;
   }
}


void wxDialUpManagerImpl::SetWellKnownHost(const wxString& hostname, int portno)
{
   if(hostname.Length() == 0)
   {
      m_BeaconHost = WXDIALUP_MANAGER_DEFAULT_BEACONHOST;
      m_BeaconPort = 80;
      return;
   }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?