- 论坛徽章:
- 0
|
以太网帧的监视小程序,基本书上COPY的 就修改了下
#include <pcap.h>
#include <stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdlib.h>
#include<math.h>
#include<errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <cf.h>
#include <sys/cfgodm.h>
#include <sys/cfgdb.h>
#define DEFAULT_SNAPLEN 68
void packet_print
(u_char* user, const struct pcap_pkthdr* h, const u_char* p)
{
static int icount=1;
printf("find %d packet\n", icount++);
}
void main()
{
char error_content[PCAP_ERRBUF_SIZE];
struct in_addr net_ip_address;
struct in_addr net_mask_address;
char *net_interface;
char *net_ip_string;
char *net_mask_string;
bpf_u_int32 net_ip;
u_int32_t net_mask;
/*net_interface = pcap_lookupdev(error_content);
if ( net_interface == NULL)
{
fprintf(stderr, "pcap_lookupdev() error: %s\n", error_content);
exit(-1);
} */
printf("网络接口:%s\n", net_interface);
pcap_lookupnet("en0", &net_ip, &net_mask, error_content);
net_ip_address.s_addr = net_ip;
net_ip_string = inet_ntoa(net_ip_address);
printf("网络地址:%s\n", inet_ntoa(net_ip_address));
net_mask_address.s_addr = net_mask;
net_mask_string = inet_ntoa(net_mask_address);
printf("网络掩码:%s\n", net_mask_string);
//
pcap_t *pd;
if ( genmajor("bpf") == -1 )
{
perror("genmajor");
exit(-1);
}
memset(error_content,0x00, sizeof(error_content));
char tmpstr[]="en0";
if ( (pd=pcap_open_live(tmpstr,DEFAULT_SNAPLEN, 0, 1000, error_content)) == NULL )
{
perror("pcap_open_live");
fprintf(stderr, "%s\n", error_content);
exit(-1);
}
memset(error_content,0x00, sizeof(error_content));
if ( pcap_loop(pd, 10, packet_print, NULL) < 0 )
{
fprintf(stderr, "pcap_loop() error: %s\n", error_content);
exit(-1);
}
pcap_close(pd);
exit(0);
}
//报错信息:
ÍøÂç½Ó¿Ú:
ÍøÂçµØÖ·:10.40.96.0
ÍøÂçÑÚÂë:255.255.255.0
genmajor: No such file or directory
红色 部分是我测试genmajor这个函数用的
如果删除去的话 在pcap_open_live报错
信息如下:
ÍøÂç½Ó¿Ú:
ÍøÂçµØÖ·:10.40.96.0
ÍøÂçÑÚÂë:255.255.255.0
pcap_open_live: Bad file number
bpf_load: genmajor failed: Error 0
我的系统是AIX |
|