« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
|
Blog信息 |
blog名称:FoxWolf 日志总数:127 评论数量:246 留言数量:0 访问次数:854943 建立时间:2006年5月31日 |

| |
[网络编程技术]Winpcap的学习日记(1) 原创空间, 文章收藏, 心得体会
FoxWolf 发表于 2006/5/31 11:17:53 |
今天向老狼拿了一个vc源程序,是用来开发DL层的。是用WinPcap来驱动的。正好看到了二进制的Blog有学习日记(blog.asp?name=binaryluo)准备向其学习。下载好WpdPack_3_2_alpha1.zip(下载地址:http://www.winpcap.org/install/bin/WpdPack_3_2_alpha1.zip),解压后除了有文档,例子外还有Include和lib.
环境的配置:(要先装WinPcap开发包)
一、vc6.0环境。
1、把wincap下面的Include和Lib分别拷入\Microsoft Visual Studio\VC98\Include和Microsoft Visual Studio\VC98\Lib中.查了一些资料也可以设置vc的环境变量,位置在:Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT(不过我设置了一直没有成功)。还有一招简单的,Tools->options->Directories里面加入下载的winpcap开发包解压以后的include文件夹和Tools->options->Directories的lib里面加入下载的winpcap开发包解压以后的lib文件夹.
2、用VC打开项目,然后在"Project->Settings"(Alt+F7),标签栏中选择"C/C++",在"Preprocessor definitions"的输入框里添加"WPCAP",再选择"Link",在"Object/library modules"的输入框里添加"wpcap.lib Packet.lib"。
二、.net环境:vc7.0的开发环境vs.net2003(最初我还不知道).
1.打开.net平台,选择你已经新建的winpcap的示例程序,(我准备采用二进制 的Blog的第一个试验).
2.项目->XX属性,选择配置属性->c/c++的第一项常规。在右边的附加包含目录选择你的wincap的Include文件夹。接着导入LIB,选择配置属性->链接器.第一项常规,在右边的附加库目录中选择你的wincap的Lib目录。(在预处理器中加入WPCAP,这是文档规定的,但我没有加也能过。最好还是加,说不定那个程序要使用上)。另处还要加上
#pragma comment(lib,"Packet.lib")#pragma comment(lib,"wpcap.lib")
#pragma comment(lib,"ws2_32.lib")
3.如果你的程序需要在cmd下通过参数运行,想直接在.net中调试,怎么做到自动加载参数呢?(就是argv)找到配置属性下的调试一项,右边第二行就是命令参数,第三行是你所用资源文件的目录。这样,通过studio调试时你就能模拟cmd的真实环境了。
4.如果报错是说找不到外部变量什么的,肯定是相关lib文件没有包含。(注:3.4两条是引用风雷的文章).
编写第一个示例程序:
#include <pcap.h>#include <remote-ext.h>
int 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) { printf("Error in pcap_findalldevs_ex: %s\n", errbuf); exit(1); }
/* Print the list */ for (d = alldevs; d != NULL; d = d->next) { /* Print the device's name */ printf("%d. %s", ++ i, d->name);
/* Print the device's dscription */ 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; }
/* We don't need any more the device list. Free it */ pcap_freealldevs(alldevs);
return 1;}
typedef struct pcap_if pcap_if_t;
int pcap_findalldevs_ex(char *source, struct pcap_rmtauth *auth, pcap_if_t **alldevs, char *errbuf);//remote-ext.h
#define PCAP_SRC_IF_STRING "rpcap://"
执行完pcap_findalldevs_ex()后,struct pcap_if中的成员就被赋值了。
struct pcap_if { struct pcap_if *next; char *name; /* name to hand to "pcap_open_live()" */ char *description; /* textual description of interface, or NULL */ struct pcap_addr *addresses; bpf_u_int32 flags; /* PCAP_IF_ interface flags */};//pcap.h
name返回的是网卡的信息形如:
rpcap://\Device\NPF_{4E273621-5161-46C8-895A-48D0E52A0B83}
description返回的是网卡的描述:
(Realtek RTL8029(AS) Ethernet Adapter)
pcap_findalldevs_ex()是函数"pcap_findalldevs()"一个超集(superset)。pcap_findalldevs()仅能列出本地机器的当前设备,是一个老的标准。而pcap_findalldevs_ex()能列出当前本地的设备还能列出远程机器的设备,能列出所有可用的pcap到指定的文件夹。因为pcap_findalldevs_ex()是与标准函数pcap_findalldevs()相依赖来获取本地机器的地址,所以pcap_findalldevs_ex()平台无关。
在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) |
|
回复:Winpcap的学习日记(1) 原创空间, 文章收藏, 心得体会
小兵(游客)发表评论于2006/9/20 10:44:48 |
|
回复:Winpcap的学习日记(1) 原创空间, 文章收藏, 心得体会
da zui(游客)发表评论于2006/5/31 15:46:32 |
|
» 1 »
|