温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

基于WinPcap获取设备列表

发布时间:2020-07-22 08:34:37 来源:网络 阅读:413 作者:1406404030 栏目:网络安全

    通常,编写基于WinPcap应用程序的第一件事情,就是获得已连接的网络适配器列表。libpcap和WinPcap都提供了 pcap_findalldevs_ex() 函数来实现这个功能: 这个函数返回一个 pcap_if 结构的链表, 每个这样的结构都包含了一个适配器的详细信息。值得注意的是,数据域 name 和 description 表示一个适配器名称和一个可以让人们理解的描述。

   首先, pcap_findalldevs_ex() ,和其他libpcap函数一样,有一个 errbuf 参数。一旦发生错误,这个参数将会被libpcap写入字符串类型的错误信息。第二要记住,不是所有的操作系统都支持libpcap提供的网络程序接口,因此,如果我们想编写一个可移植的应用程序,我们就必须考虑在什么情况下, description 是 null。本程序中,我们遇到这种情况时,会打印提示语句"No description available"。 

    最后要记住,当我们完成了设备列表的使用,我们要调用 pcap_freealldevs() 函数将其占用的内存资源释放。 

让我们编译并运行我们的第一个示例程序吧! 

实现代码:

// aa.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"

#include "pcap.h"

int _tmain(int argc, _TCHAR* argv[])

{

 pcap_if_t *alldevs;

    pcap_if_t *d;

    int i=0;

    char errbuf[PCAP_ERRBUF_SIZE];

    /* 获取本地机器设备列表 */

    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)

    {

        fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);

        exit(1);

    }

    /* 打印列表 */

    for(d= alldevs; d != NULL; d= d->next)

    {

        printf("%d. %s", ++i, d->name);

        if (d->description)

            printf(" (%s)\n", d->description);

        else

            printf(" (No description available)\n");

    }

    if (i == 0)

    {

        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");

        return 0;

    }

    /* 不再需要设备列表了,释放它 */

    pcap_freealldevs(alldevs);

}

运行

基于WinPcap获取设备列表


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI