拨号

// LSNetConnectUtil.h

 

// LSNetConnectUtil.h: interface for the CLSNetConnectUtil class.
//
//

#if !defined(AFX_LSNETCONNECTUTIL_H__4D8C6656_8D4E_44AF_AD9F_D7ED5D74D6C9__INCLUDED_)
#define AFX_LSNETCONNECTUTIL_H__4D8C6656_8D4E_44AF_AD9F_D7ED5D74D6C9__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

namespace LSSDK
{

  /* 保存拨号连接信息的结构体 */
 typedef struct _LS_RAS_CONNECTION_INFO
 {
  CString  strDeviceName;  // 设备名称
  CString  strEntryName;  // 条目名称
  CString  strPhoneName;  // 电话名称
  
 }RAS_CONNECTION_INFO_ST;

 

 /********************************************************************
    Summary:
    网络连接监控
    History:
    <TABLE>
    最后修改时间   修改人  修改后版本  修改说明
    ------------   ------  ----------  ---------
    2009-05-25     李小刚    1.0         创建
    </TABLE>
    TODO:
    * TODO: 获取所有网卡描述信息.
    * TODO: 获取所有拨号连接信息.
    * TODO: 判断是否可以连接到Internet.
    *********************************************************************/

 class AFX_EXT_CLASS CLSNetConnectUtil 
 {
 public:

  /********************************************************************
  Summary:
   CLSNetConnectUtil类对象构造函数
  *********************************************************************/
  CLSNetConnectUtil();

  /********************************************************************
  Summary:
   CLSNetConnectUtil类对象析构函数
  *********************************************************************/
  virtual ~CLSNetConnectUtil();
 
 public:

  /********************************************************************
        Summary:
                获取所有网卡描述信息
        Parameters:
                vNetCardInfo - 网卡描述信息接收结构                                
        Returns:
    返回网卡的个数
        *********************************************************************/
  int EnumAllNetCard( vector<CString>& vNetCardInfo );


  /********************************************************************
        Summary:
                获取所有拨号连接信息
        Parameters:
                vRasConnectionInfo - 拨号连接信息结构体                                
        Returns:
    返回拨号连接个数
        *********************************************************************/
  int EnumRAS( vector<RAS_CONNECTION_INFO_ST>& vRasConnectionInfo );


  /********************************************************************
        Summary:
                判断是否可以连接到Internet网络
        Parameters:
                strServerNames - DNS服务器地址
    strTargetNames - 目的地址
        Returns:
    TRUE - 可以连接到网络  FALSE - 不能连接到网络
        *********************************************************************/
  BOOL IsConnectInternet(CString strServerNames, CString strTargetNames);


  /********************************************************************
        Summary:
                是否使用本地的DNS检查是否可以连接到互联网
        Parameters:
                bSet - TRUE  使用本地DNS检测是否可以连接到互联网
      - FALSE 不使用本地DNS检测是否可以连接到互联网
        Returns:
    无
        *********************************************************************/
  void IsUseLocalDNSForCheck(BOOL bSet);

 protected:

  /********************************************************************
        Summary:
                判断是否可以连接到Internet网络
        Parameters:
                无                                
        Returns:
    TargetName - 要连接到得目的域名 
    sDnsName   - DNS名称  
        *********************************************************************/
  BOOL UDP_DNS_DNSQuery(const char *TargetName, const char *sDnsName);


  /********************************************************************
        Summary:
                工具函数
        Parameters:
                无                                
        Returns:
    strTarget  - 要拆分的字符串 
    strArray   - 拆分后的字符串保存的结构
    strTag    - 标志值
        *********************************************************************/
  int SplitStringWith(CString strTarget, CStringArray &strArray, CString strTag);


 private:
  BOOL  m_bStartLocalDns;

 };
}

#endif // !defined(AFX_LSNETCONNECTUTIL_H__4D8C6656_8D4E_44AF_AD9F_D7ED5D74D6C9__INCLUDED_)

 

 

 

 

 

 

 

// LSNetConnectUtil.cpp: implementation of the CLSNetConnectUtil class.
//
//

#include "stdafx.h"
#include "LSNetConnectUtil.h"
#include <Ras.h>


#pragma comment(lib, "Rasapi32.lib")
//
// Construction/Destruction
//

#define MAX_RAS_CONN 32     // RAS拨号的条目数最大值预定义

CLSNetConnectUtil::CLSNetConnectUtil()
{

}

CLSNetConnectUtil::~CLSNetConnectUtil()
{

}

int CLSNetConnectUtil::EnumAllNetCard( vector<CString>& vNetCardInfo )
{
 vNetCardInfo.clear();
 
 PIP_ADAPTER_INFO pInfo=NULL,pInfoTemp=NULL;
 ULONG ulSize=0;
 GetAdaptersInfo(pInfo,&ulSize);
 pInfoTemp=pInfo=(PIP_ADAPTER_INFO)new(char[ulSize]);
 GetAdaptersInfo(pInfo,&ulSize);
 
 while(pInfo)
 {
  vNetCardInfo.push_back(pInfo->Description);
  
  pInfo=pInfo->Next; 
 }
 
 delete pInfoTemp;

 return vNetCardInfo.size();
}


int CLSNetConnectUtil::EnumRAS( vector<RAS_CONNECTION_INFO_ST>& vRasConnectionInfo )
{
 int ReturnCode = 0;
 RASCONN ras[MAX_RAS_CONN];
 DWORD dSize,dNumber,dCount;
 
 ras[0].dwSize = sizeof(RASCONN);
 dSize = sizeof( ras );
 try{
  //获取拨号连接
  ReturnCode = RasEnumConnectionsA( ras, &dSize, &dNumber );
  if(ReturnCode == 0 )
  {
   for(dCount = 0; dCount < dNumber; dCount++ )
   {
    time_t ltime;
    time(&ltime);
    struct tm * tmConnectTime = localtime(&ltime);
    
                CString strDeviceName,strDeviceType;
    CString strEntryName, strPhoneNumber;
    
    strDeviceName = ras[dCount].szDeviceName;
    strDeviceType = ras[dCount].szDeviceType;
    strEntryName  = ras[dCount].szEntryName;
    
    //根据EntryName获取电话号码
    DWORD dwEntryInfoSize = 0;
    RasGetEntryPropertiesA(NULL, NULL,NULL, &dwEntryInfoSize, NULL, NULL);
    RASENTRY *EntryParam;
    EntryParam = (RASENTRY*)malloc(dwEntryInfoSize);
    EntryParam->dwSize = dwEntryInfoSize;
    RasGetEntryPropertiesA(NULL,ras[dCount].szEntryName,EntryParam, &dwEntryInfoSize, NULL, NULL);
    strPhoneNumber.Format("%s-%s",EntryParam->szAreaCode,EntryParam->szLocalPhoneNumber);
    free(EntryParam);
    
    RAS_CONNECTION_INFO_ST stResInfo;
    stResInfo.strDeviceName = strDeviceName;
    stResInfo.strEntryName  = strEntryName;
    stResInfo.strPhoneName  = strPhoneNumber;
    
    vRasConnectionInfo.push_back(stResInfo);
    
    RasHangUpA(ras[dCount].hrasconn);
   }
  }
 }catch (...)
 {
 } 

 return vRasConnectionInfo.size();
}


BOOL CLSNetConnectUtil::UDP_DNS_DNSQuery(const char *TargetName, const char *sDnsName)
{
 DNS_RECORD* pQueryResultsSet = NULL;
 if(sDnsName != NULL)
 {
  IP4_ARRAY ip4;
  ip4.AddrCount=1;
  ip4.AddrArray[0] = inet_addr(sDnsName);
  
  DnsQuery(TargetName,1,DNS_QUERY_STANDARD|DNS_QUERY_BYPASS_CACHE,&ip4,&pQueryResultsSet,NULL);
 }else
 {
  DnsQuery(TargetName,1,DNS_QUERY_STANDARD|DNS_QUERY_BYPASS_CACHE,NULL,&pQueryResultsSet,NULL);
 }
 
 if(pQueryResultsSet != NULL)
 {
  DnsRecordListFree(pQueryResultsSet,DnsFreeRecordListDeep);
  return TRUE;
 }else
 {
  return FALSE;
 }
}


BOOL CLSNetConnectUtil::IsConnectInternet(CString strServerNames, CString strTargetNames)
{
 CStringArray strTargetArray;
 CStringArray strDNSArray;
 
 SplitStringWith(strServerNames, strDNSArray, ";");
 SplitStringWith(strTargetNames, strTargetArray, ";");

 
 BOOL bQueryOK = FALSE;
 int iTargetCount = strTargetArray.GetSize();
 int iDNSCount    = strDNSArray.GetSize();
 
 for(int i = 0;i<iTargetCount;i++)
 {
  for(int k = 0;k<iDNSCount;k++)
  {
   //测试指定DNS服务器
   if(UDP_DNS_DNSQuery(strTargetArray.GetAt(i), strDNSArray.GetAt(k)))
   {
    return TRUE;
   }
  }
  
  if( m_bStartLocalDns == TRUE )
  {
   //测试本地DNS服务器
   if(UDP_DNS_DNSQuery(strTargetArray.GetAt(i),NULL))
   {
    return TRUE;
   }
  }
 }
 
 return FALSE;
}

void CLSNetConnectUtil::IsUseLocalDNSForCheck(BOOL bSet)
{
 m_bStartLocalDns = bSet;
}

int CLSNetConnectUtil::SplitStringWith(CString strTarget, CStringArray &strArray, CString strTag)
{
 int iCount = 0;
 
 if (strTarget.IsEmpty())
 {
  return 0;
 }
 
 try
 {
  CString strTemp = strTarget;
  int nPos = strTemp.Find(strTag);
  if(nPos > 0)
  {
   while(nPos > 0)  
   {
    CString strSubString=strTemp.Left(nPos);
    strArray.Add(strSubString);
    iCount++;
    
    strTemp = strTarget.Right(strTemp.GetLength()-nPos-strTag.GetLength());
    
    nPos =strTemp.Find(strTag);
   }
   
   if( !strTemp.IsEmpty() ) // added by LML 08.8.20
   {
    strArray.Add(strTemp);
    iCount++;
   }
   
  }else{
   if( !strTemp.IsEmpty() ) // added by LML 08.8.20
   {
    strArray.Add(strTemp);
    iCount++;
   }
  }
 }catch(...)
 {
  return 0;
 }
 return iCount;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值