fatal error C1189: #error 异常,找不到什么有关问题。
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");
试了很久才把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");