深度剖析WinPcap之(五)——在Visual studio 2005中使用WinPcap库进行程序开发(2)
2009-09-11 00:00:00 来源:WEB开发网1.6. 使用WinPcap库进行程序开发
下面详细介绍在Microsoft Visual Studio 2005下如何使用WinPcap库来进行开发,实例程序为打开本机的设备列表,并显示出来。下面为具体操作步骤:
1) 建立一个工程名称为devlist的空工程(Empty Project),如图5-6所示。
图5-6 创建devlist工程
2) 添加源代码,文件名为main.cpp,如图5-7、图5-8与图5-9所示。
图5-7添加源代码
图5-8 创建源文件main.cpp
图5-9源文件main.cpp图示
源代码main.cpp文件内容如下所示:
#define WIN32
#define HAVE_REMOTE
#include <stdio.h>
#include "pcap.h"
#include <winsock.h>
//函数原形
void ifprint(pcap_if_t *d);
char *iptos(u_long in);
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
char errbuf[PCAP_ERRBUF_SIZE+1];
char source[PCAP_ERRBUF_SIZE+1];
/*选择要获取设备列表的源*/
printf("Enter the device you want to list:\n"
"rpcap:// ==> lists interfaces in the local machine\n"
"rpcap://hostname:port ==> lists interfaces in a remote machine\n"
" (rpcapd daemon must be up and running\n"
" and it must accept 'null' authentication)\n"
"file://foldername ==> lists all pcap files in the give folder\n\n"
"Enter your choice: ");
fgets(source, PCAP_ERRBUF_SIZE, stdin);
source[PCAP_ERRBUF_SIZE] = '\0';
/*获取接口列表*/
if (pcap_findalldevs_ex(source, NULL, &alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
exit(1);
}
/*遍历列表,打印出每个条目的信息*/
for(d=alldevs;d;d=d->next)
{
ifprint(d);
}
/*释放接口列表*/
pcap_freealldevs(alldevs);
return 1;
}
/* 打印给定接口的所有有效信息*/
void ifprint(pcap_if_t *d)
{
pcap_addr_t *a;
char ip6str[128];
/*名称(Name)*/
printf("\tName:%s\n",d->name);
/*描述(Description)*/
if (d->description)
printf("\tDescription: %s\n",d->description);
/*回环地址(Loopback Address)*/
printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
/*IP地址(IP addresses)*/
for(a=d->addresses;a;a=a->next) {
printf("\tAddress Family: #%d\n",a->addr->sa_family);
if(a->addr->sa_family==AF_INET)
{
printf("\tAddress Family Name: AF_INET\n");
if (a->addr)
printf("\tAddress: %s\n",
iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
if (a->netmask)
printf("\tNetmask: %s\n",iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr));
if (a->broadaddr)
printf("\tBroadcast Address: %s\n",
iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
if (a->dstaddr)
printf("\tDestination Address: %s\n",
iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
}else
{
printf("\tAddress Family Name: Unknown\n");
}
}
printf("\n");
}
/*把一个数字IP地址转换为一个字符串*/
#define IPTOSBUFFERS 12
char *iptos(u_long in)
{
static char output[IPTOSBUFFERS][3*4+3+1];
static short which;
u_char *p;
p = (u_char *)∈
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
_snprintf_s(output[which], sizeof(output[which]),
sizeof(output[which]),"%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return output[which];
}
- ››深度解释攻击linux服务器的四种级别
- ››剖析java.util.concurrent锁
- ››剖析Android智能手机系统的更多功能
- ››深度分析地方社区网站的内容定位
- ››剖析Windows Azure Platform框架与组成
- ››剖析使用 ObjectOutputStream 可能引起的内存泄漏...
- ››剖析EWebEditor编辑器漏洞攻击案例
- ››剖析开源云:构建 Infrastructure as a Service 块...
- ››深度剖析 Android 和 iPhone OS
- ››深度分析:HTML5能否成为Flash终结者
- ››深度挖掘 更多Windows 7快捷模式
- ››深度挖掘 Windows 7快捷模式
更多精彩
赞助商链接