fatal error C1189: #error 错误,找不到什么问题。。。
试了很久才把WinPcap的环境搞好。。。。
调试又出问题,一直是这样不知道怎么搞了都
// udp001.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "remote-ext.h "
#include "pcap.h"
#include "winsock2.h"
#pragma comment(lib, "ws2_32.lib")
//4字节的IP地址
typedef struct ip_address
{
u_char byte1;
u_char byte2;
u_char byte3;
u_char byte4;
}ip_address;
//IPv4 首部
typedef struct ip_header
{
u_char ver_ihl; //版本(4 bits) + 首部长度(4 bits)
u_char tos; //服务类型(Type of service)
u_short tlen; //总长(Total length)
u_short identification; //标识(Identification)
u_short flags_fo; //标志位(Flags)(3 bits) + 段偏移量(Fragment offset)(13 bits)
u_char ttl; //存活时间(Time to live)
u_char proto; //协议(Protocol)
u_short crc; //首部校验和(Header checkSum)
ip_address saddr; //源地址(Source address)
ip_address daddr; //目的地址(Destination address)
u_int op_pad; //选项与填充(Option + Padding)
}ip_header;
//UDP 首部
typedef struct udp_header
{
u_short sport; //源端口(Source port)
u_short dport; //目的端口(Destination port)
u_short len; //UDP数据包长度(Datagram length)
u_short crc; //校验和(Checksum)
}udp_header;
//函数原型
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i = 0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;
//获得设备列表
if(pcap_findalldevs(&alldevs, errbuf) == -1)
{
printf("Error in pcap_findalldevs: %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);
}
printf("Enter the interface number (1-%d):", i);
scanf("%d", &inum);
if(inum < 1 || inum > i)
{
printf("\nInterface number out of range.\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
//跳转到已选设备
for(d = alldevs, i = 0; i < inum - 1; d = d->next, i++);
//打开适配器
if((adhandle = pcap_open(d->name, 65536, 0, 1000, NULL, errbuf)) == NULL)
{
printf("\nUnable to open the adapter. %s is not supported by WinPcap\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
//检查数据链路层,为了简单,我们只考虑以太网
if(pcap_datalink(adhandle) != DLT_EN10MB)
{
printf("\nThis program works only on Ethernet networks.\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
if(d->addresses != NULL)
//获得接口第一个地址的掩码
netmask = ((struct sockaddr_in*)(d->addresses->netmask))->sin_addr.S_un.S_addr;
else
//如果接口没有地址,那么我们假设一个C类的掩码
netmask = 0xffffff;
//编译过滤器
if(pcap_compile(adhandle, &fcode, packet_filter, 1, netmask) < 0)
{
printf("\nUnable to compile the packet filter. Check the syntax.\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
//设置过滤器
if(pcap_setfilter(adhandle, &fcode) < 0)
{
printf("\nError setting the filter.\n");
//释放设备列表
pcap_freealldevs(alldevs);
exit(1);
}
printf("\nlistening on %s...\n", d->description);
//释放设备列表
pcap_freealldevs(alldevs);
//开始捕捉
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
ip_header *ih;
udp_header *uh;
u_int ip_len;
u_short sport, dport;
time_t local_tv_sec;
local_tv_sec = header->ts.tv_sec;
//打印数据包的时间戳和长度
printf("%s. len: %d", ctime(&local_tv_sec), header->len);
//获取IP数据包头部的位置
ih = (ip_header *)(pkt_data + 14);
//获取UDP首部的位置
ip_len = (ih->ver_ihl & 0xf) * 4;
uh = (udp_header *)((u_char*)ih + ip_len);
sport = ntohs(uh->sport);
dport = ntohs(uh->dport);
printf("%d.%d.%d.%d.%d -> %d.%d.%d.%d.%d\n",
ih->saddr.byte1,
ih->saddr.byte2,
ih->saddr.byte3,
ih->saddr.byte4,
sport,
ih->daddr.byte1,
ih->daddr.byte2,
ih->daddr.byte3,
ih->daddr.byte4,
dport);
}
>------ 已启动生成: 项目: udp001, 配置: Debug Win32 ------
1>正在编译...
1>udp001.cpp
1>d:\wpdpack\include\remote-ext.h(39) : fatal error C1189: #error : Please do not include this file directly. Just define HAVE_REMOTE and then include pcap.h
1>生成日志保存在“file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\udp001\udp001\Debug\BuildLog.htm”
1>udp001 - 1 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========