IpHelper API GetIfTable

先用GetInterfaceInfo获取所有适配器列表(其中包括拨号网络)   
  在针对每个适配器,用GetIfEntry获得每个适配器的详细信息   
  这2个函数是iphlpapi中的函数,需要在代码前面包含头文件,并指明链接库文件   
  #include   <iphlpapi.h>   
  #pragma   comment(lib,   "iphlpapi.lib")   
    
  下面是我以前写过的部分代码:   
  DWORD   dwSize   =   0;   
  GetInterfaceInfo(NULL,   &dwSize);   
  if   (dwSize   <=   0)   return;   
  PIP_INTERFACE_INFO   pIfTable   =   (PIP_INTERFACE_INFO)malloc(dwSize);   
  if   (!pIfTable)   
  return;   
  if   (GetInterfaceInfo(pIfTable,   &dwSize)   !=   NO_ERROR)   
  {   
  free(pIfTable);   
  return;   
  }   
    
    
  for   (int   i   =   0;   i   <   (int)pIfTable->NumAdapters;   i++)   
  {   
  MIB_IFROW   IfRow;   
  IfRow.dwIndex   =   pIfTable->Adapter[i].Index;   
  if   (GetIfEntry(&IfRow)   ==   NO_ERROR)   
  {   
  //在这里可以判断IfRow中的dwType,MIB_IF_TYPE_PPP是拨号   
  //ifRow.dwSpeed就是速率   
  }   
  }   
    
  free(pIfTable);   

DWORD dwIfBufSize = 0;
    BOOL bRet;
 
    if(m_pMIT == NULL)
        m_pMIT = new MIB_IFTABLE[sizeof(MIB_IFTABLE)];
 
    bRet = ::GetIfTable(m_pMIT,&dwIfBufSize,0);
    if(bRet == ERROR_INSUFFICIENT_BUFFER)//如果内存不足,则重新分配内存大小
    {
        if(dwIfBufSize != NULL)
            delete [] m_pMIT;
        m_pMIT = new MIB_IFTABLE[dwIfBufSize];
    }
 
    bRet = ::GetIfTable(m_pMIT,&dwIfBufSize,0);
    if (bRet == NO_ERROR)
    {
        if (m_pMIT->dwNumEntries <= 1)
        {
            //bResult = false;
        }
        else
        {
            __int64 i64TotalOutOctets = 0;
            __int64 i64TotalInOctets = 0;
            //多網卡
            for(int i=0; i<(m_pMIT->dwNumEntries); i++)
            {
                if (m_pMIT->table[i].dwType <= 23)
                {
                    //当前上传
                    i64TotalOutOctets += m_pMIT->table[i].dwOutOctets;
                    //当前下载
                    i64TotalInOctets += m_pMIT->table[i].dwInOctets;
                    //bResult = mit.table[i].dwOperStatus;
                    //if (bResult)
                    {
                        //return true;
                    }
                }
            }
            memset(szVal,0,sizeof(szVal));
            if(m_oldUploadOctets != 0 && (i64TotalOutOctets > m_oldUploadOctets))
                m_curUploadOctets = i64TotalOutOctets - m_oldUploadOctets;
            m_oldUploadOctets = i64TotalOutOctets;
            lengthToStr(m_curUploadOctets * 8,szVal);
            m_CapabilityDlgObj.GetDlgItem(IDC_STA_NETUPSPEED)->SetWindowText(szVal);
            //下载
            memset(szVal,0,sizeof(szVal));
            if(m_oldDownloadOctets != 0 && (i64TotalInOctets > m_oldDownloadOctets))
                m_curDownloadOctets = i64TotalInOctets - m_oldDownloadOctets;
            m_oldDownloadOctets = i64TotalInOctets;
            lengthToStr(m_curDownloadOctets * 8,szVal);
            m_CapabilityDlgObj.GetDlgItem(IDC_STA_NETDNSPEED)->SetWindowText(szVal);
        }
    }

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
 
#include "include/pcap.h"
 
#pragma comment(lib,"lib/Packet.lib")
#pragma comment(lib,"lib/wpcap.lib")
 
void usage();
 
void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);
 
void _tmain(int argc, _TCHAR* argv[])
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
struct timeval st_ts;
u_int netmask;
struct bpf_program fcode;
   
    /* 检查命令行参数的合法性 */
 
         
    /* 打开输出适配器 */
    if((fp= pcap_open_live("\\Device\\NPF_{4C280CAF-B56F-467D-91E2-43F8C54B5BD2}"/*我的网卡名*/,100,1,1000,errbuf))==NULL)
    {
        fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf);
        return;
    }
 
    /* 不用关心掩码,在这个过滤器中,它不会被使用 */
    netmask=0xffffff; 
 
    // 编译过滤器
    if (pcap_compile(fp, &fcode, "ether proto 0x8864"/*我用的是ADSL这里是设置只接收PPPOE的包*/, 1, netmask) <0 )
    {
        fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
        /* 释放设备列表 */
        return;
    }
     
    //设置过滤器
    if (pcap_setfilter(fp, &fcode)<0)
    {
        fprintf(stderr,"\nError setting the filter.\n");
        pcap_close(fp);
        /* 释放设备列表 */
        return;
    }
 
    /* 将接口设置为统计模式 */
    if (pcap_setmode(fp, MODE_STAT)<0)
    {
        fprintf(stderr,"\nError setting the mode.\n");
        pcap_close(fp);
        /* 释放设备列表 */
        return;
    }
 
 
    printf("NetWork traffic summary:\n");
 
    /* 开始主循环 */
    pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);
 
    pcap_close(fp);
    return;
}
 
void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    struct timeval *old_ts = (struct timeval *)state;
    u_int delay;
    LARGE_INTEGER Bps,Pps;
    struct tm *ltime;
    char timestr[16];
    time_t local_tv_sec;
 
    /* 以毫秒计算上一次采样的延迟时间 */
    /* 这个值通过采样到的时间戳获得 */
    delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec;
    /* 获取每秒的比特数b/s */
    Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay));
    /*                                            ^      ^
                                                  |      |
                                                  |      | 
                                                  |      |
                              将字节转换成比特 --   |
                                                         |
                                       延时是以毫秒表示的 --
    */
 
    /* 得到每秒的数据包数量 */
    Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay));
 
    /* 将时间戳转化为可识别的格式 */
    local_tv_sec = header->ts.tv_sec;
    ltime=localtime(&local_tv_sec);
    strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
 
    /* 打印时间戳*/
    printf("%s ", timestr);
 
    /* 打印采样结果 */
    printf("BPS=%I64u ", Bps.QuadPart);
    printf("PPS=%I64u\n", Pps.QuadPart);
 
    //存储当前的时间戳
    old_ts->tv_sec=header->ts.tv_sec;
    old_ts->tv_usec=header->ts.tv_usec;
}
 
 
void usage()
{
     
    printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n");
    printf("\nUsage:\n");
    printf("\t tcptop adapter\n");
    printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n");
 
    exit(0);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值