用 WinPcap 获取网络接口列表

46 篇文章 0 订阅

在 WinPcap SDK 中,有一个函数:pcap_findalldevs_ex,这个函数可以获得网络接口列表。

示例程序:

// SimpleSniffer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <pcap.h>


int _tmain()
{
    pcap_if_t *alldevs = nullptr;
    pcap_if_t *d = nullptr;
    char errbuf[PCAP_ERRBUF_SIZE];
    int count = 0;

    // retrieve the adapters from the computer
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
    {
        _ftprintf(stderr, _T("Error in pcap_findalldevs_ex: %s\n"), errbuf);
        exit(1);
    }

    // if there are no adapters, print an error
    if (alldevs == nullptr)
    {
        _ftprintf(stderr, _T("\nNo adapters found! Make sure WinPcap is installed.\n"), errbuf);
        exit(2);
    }

    // print the list of adapters along with basic information about an adapter
    for (d = alldevs; d != NULL; d = d->next)
    {
        printf("%2d. %s\n", ++count, d->name);
        if (d->description)
        {
            printf("    %s\n\n", d->description);
        }
        else
        {
            printf(" (No description available)\n");
        }
    }

    if (count == 0)
    {
        _tprintf(_T("\nNo interfaces found! Make sure WinPcap is installed.\n"));
    }

    // free the network adapter list
    pcap_freealldevs(alldevs);

    return 0;
}

运行结果:
这里写图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值