WinPcap文档翻译第一章

                                                                    WinPcap 导引 :使用WinPcap简明指导
                                                       WinPcap tutorial:a step by step guide to using WinPcap
                                                                 翻译自WinPcap开发包的帮助文档 –俞凌峰
   
                                                                             第一篇:获得网络设备列表
                                                                              Obtaining the device list
    Typically,the first thing that a WinPcap-based application does is get a list of attached network adapters.Both libpcap and WinPcap provide the pcap_findalldevs_ex() function for this purpost:this function returns a linked list of pcap_if struct,each of which contains comprehensive information about an attached adapter.In particular,the fields name and description contain the name and a human readable description,respectively,of the corresponding device.
    一般来讲,一个基于WinPcap的应用程序首先做的就是获取一个网络适配器列表。Linux下的libpcap和Windows下的WinPcap都提供了pcap_findalldevs_ex()函数来达到此目的:该函数会返回包含有pcap_if结构体的链表,每个结构体中都包含有活动的网络适配器的详细信息。结构体中的name和description分别包含了网络适配器的名字和人可读的相关描述。
    The following code retrieves the adapter list and shows it on the screen,printing an error if no adapters are found.
    以下代码遍历了网络适配器列表并显示在屏幕中,如果找不到网络适配器的话,打印一个错误。

/* code

#include “pcap.h”

main()
{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    int i=0;
    char errbuf[PCAP_ERRBUF_SIZE];

    /*Retrieve the device list from the local machine*/
    /*遍历本地机器中的网络适配器*/
    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);
    }

    /*Print the list*/
    /*打印出适配器列表*/
    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 install./n”);
        return;
     }

     /*We don't need any more the device list.Free it*/
     pcap_freealldevs(alldevs);
}

code */

    Some comments about this code.关于此代码的一些评论。
    First of all,pcap_findalldevs_ex(),like other libpcap functions,has an errbuf parameter.This parameter points to a string filled by libpcap with a description of the error if something goes wrong.
   首先,pcap_findalldevs_ex()函数和其他libpcap函数一样,包含一个错误消息缓冲参数(errbuf)。该参数指向一个字符串地址,该字符串由libpcap填充,包含了函数发生错误时的描述信息。
   Second,remember that not all the Oses supported by libpcap provide a description of the network interfaces,therefore if we want to write a portable application,we must consider the case in which description is null;we print the string “No description available”in that situation.
   其次,记住不是所有的操作系统都支持libpcap并能提供一个网络接口描述,所以,假如你打算写一个可移植的应用程序,你必须考虑描述为空的情况;我们针对这种情况打印”No description available”。
   Note finally that we free the list with pcap_freealldevs() once when we have finished with it.
  最后注意别忘记释放适配器列表,通过调用pcap_freealldevs()来释放列表当你使用完该列表时。
  Let's try to compile and run the code of this first sample.In order to compile it under Unix or Cygwin,simply type:
  gcc -o testprog testprog.c -lpcap
  On Windows,you will need to create a project,following the instructions in the Using WinPcap in your programs section of this manual.However,we suggest that you use the WinPcap developer's pack(available at the WinPcap website,http://www.winpcap.org),since it provides many examples already configured as projects including all the code presented in this tutorial and the 
 includes and libraries needed to compile and run the examples.
    让我们试着编译并运行该代码吧。为了在Unix和Cygwin(Windows下模拟Unix环境的一个软件)下能编译该代码,可以如下输入:
    gcc -o testprog testprog.c -lpcap
    在windows环境下编译代码,你需要创建一个工程,可以根据本手册中“在你的程序中使用WinPcap”(Using WinPcap in your programs)章节中的指导步骤进行一步步的操作。然而,我们建议你使用WinPcap 开发者包(WinPcap developer's pack,在WinPcap的官方网站,http://www.winpcap.org可以下载),因为该包提供了很多的示列并提供了运行本手册中代码的工程配置。
    Assuming we have compiled the program,let's try to run it.On a particular WinXP workstation,the result we optained is
1./Device/NPF_{4E273621-5161-46C8-895A-48D0E52A0B83} (Realtek RTL8029(AS)Ethernet Adapter)
2./Device/NPF_{5D24AE04-C486-4A96-83FB-8B5EC6C7F430} (3Com EtherLink PCI)
    As you can see,the name of the network adapters(that will be passed to libpcap when opening the devices)under Windows are quite unreadable,so the parenthetical descriptions can be very helpful.
   假设我们编译完了代码,我们就可以运行该程序了。在一个特定的WinXP工作站上运行该程序,得到的结果如下:
    1. /Device/NPF_{4E273621-5161-46C8-895A-48D0E52A0B83} (Realtek RTL8029(AS)Ethernet Adapter)
2./Device/NPF_{5D24AE04-C486-4A96-83FB-8B5EC6C7F430} (3Com EtherLink PCI)
    正如你所看到的,网络接口的名字(name)字段(该字段将会传递给libpcap当你当开一个适配器时)不是十分可读,所以附加的描述字段将会十分的有用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值