高分相求---关于通过libpcap函数库访问链路层程序的运行问题
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 1000
#define PCAP_ERRBUF_SIZE 200
void Display(const u_char *packet,const size_t length);
/*list all packets be filtered*/
void my_callback(u_char *none,const struct pcap_pkthdr *pkthdr,const u_char *packet)
{
Display((u_char *)packet,(size_t)(pkthdr->caplen));
return;
}
int main(int argc,char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr;
struct bpf_program fp;
bpf_u_int32 maskp;
bpf_u_int32 netp;
}
if(argc!=2)
{
fprintf(stdout,"Usage:%s\"filter program\"\n",argv[0]);
return 0;
}
/*grab a device*/
dev=pcap_lookupdev(errbuf);
if(dev==NULL){
fprintf(stderr,"%s\n",errbuf);
exit(1);
}
/*ask pcap for the network address and mask of the device*/
pcap_lookupnet(dev,&netp,&maskp,errbuf);
/*open device for reading this time and set it in promiscuous mode*/
descr=pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
if(descr==NULL){
printf("pcap_open_live():%s\n",errbuf);
exit(1);
}
/*try and compile the program ..non-optimized*/
if(pcap_compile(descr,&fp,argv[1],0,netp)==-1){
fprintf(stderr,"Error calling pcap_compile\n");
exit(1);
}
/*set the compiled program as the filter*/
if(pcap_setfilter(descr,&fp)==-1){
fprintf(stderr,"Error setting filter\n");
exit(1);
}
/*capture packets*/
pcap_loop(descr,-1,my_callback,NULL);
return 0;
}
void Display(const u_char *packet,const size_t length)
{
u_long offset;
int i,j,k;
printf("packet[%lu bytes]:\n",(long unsigned int)length);
if(length<=0){
ruturn;
}
i=0;
offset=0;
for(k=length/16;k>0,k--,offset+=16){
printf("%08X",(unsigned int)offset);
for(j=0;j<16;j++,i++){
if(j==8){
printf("-%02X",packet[i]);
}
else printf("%02X",packet[i]);}
printf(" ");
i-=16;
for(j=0;j<16;j++,i++){
if((packet[i]>='')&&(packet[i]<=255))
{
printf("%c",packet[i]);
}
else printf(".");
}
printf("\n");
}
k=length-i;
if(k<=0){
return;
}
printf("%08X",(unsigned int)offset);
for(j=0;j<k;j++,i++){
if(j==8){
printf("-%02X",packet[i]);
}
else printf("%02X",packe[i]);
}
i-=k;
for(j=16-k;j>0;j--){
printf(" ");
}
printf(" ");
for(j=0;j<k;j++,i++){
if((packet[i]>=' ')&&(packet[i]<=255)){
printf("%c",packet[i]);
}
else {
printf(".");
}
}
printf("\n");
return;
}
在linux7.3下运行出现syntax error near unexpected token 'my callback(u'
'void my_callback(u.char *none,const struct--)的错误,我是这方面的新手,望高高手不吝赐教!
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 1000
#define PCAP_ERRBUF_SIZE 200
void Display(const u_char *packet,const size_t length);
/*list all packets be filtered*/
void my_callback(u_char *none,const struct pcap_pkthdr *pkthdr,const u_char *packet)
{
Display((u_char *)packet,(size_t)(pkthdr->caplen));
return;
}
int main(int argc,char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr;
struct bpf_program fp;
bpf_u_int32 maskp;
bpf_u_int32 netp;
}
if(argc!=2)
{
fprintf(stdout,"Usage:%s\"filter program\"\n",argv[0]);
return 0;
}
/*grab a device*/
dev=pcap_lookupdev(errbuf);
if(dev==NULL){
fprintf(stderr,"%s\n",errbuf);
exit(1);
}
/*ask pcap for the network address and mask of the device*/
pcap_lookupnet(dev,&netp,&maskp,errbuf);
/*open device for reading this time and set it in promiscuous mode*/
descr=pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
if(descr==NULL){
printf("pcap_open_live():%s\n",errbuf);
exit(1);
}
/*try and compile the program ..non-optimized*/
if(pcap_compile(descr,&fp,argv[1],0,netp)==-1){
fprintf(stderr,"Error calling pcap_compile\n");
exit(1);
}
/*set the compiled program as the filter*/
if(pcap_setfilter(descr,&fp)==-1){
fprintf(stderr,"Error setting filter\n");
exit(1);
}
/*capture packets*/
pcap_loop(descr,-1,my_callback,NULL);
return 0;
}
void Display(const u_char *packet,const size_t length)
{
u_long offset;
int i,j,k;
printf("packet[%lu bytes]:\n",(long unsigned int)length);
if(length<=0){
ruturn;
}
i=0;
offset=0;
for(k=length/16;k>0,k--,offset+=16){
printf("%08X",(unsigned int)offset);
for(j=0;j<16;j++,i++){
if(j==8){
printf("-%02X",packet[i]);
}
else printf("%02X",packet[i]);}
printf(" ");
i-=16;
for(j=0;j<16;j++,i++){
if((packet[i]>='')&&(packet[i]<=255))
{
printf("%c",packet[i]);
}
else printf(".");
}
printf("\n");
}
k=length-i;
if(k<=0){
return;
}
printf("%08X",(unsigned int)offset);
for(j=0;j<k;j++,i++){
if(j==8){
printf("-%02X",packet[i]);
}
else printf("%02X",packe[i]);
}
i-=k;
for(j=16-k;j>0;j--){
printf(" ");
}
printf(" ");
for(j=0;j<k;j++,i++){
if((packet[i]>=' ')&&(packet[i]<=255)){
printf("%c",packet[i]);
}
else {
printf(".");
}
}
printf("\n");
return;
}
在linux7.3下运行出现syntax error near unexpected token 'my callback(u'
'void my_callback(u.char *none,const struct--)的错误,我是这方面的新手,望高高手不吝赐教!
...全文
相关推荐
libpcap-tutorial.pdf
- **权限管理**:在运行 libpcap 程序时可能需要管理员权限,务必小心操作以免造成系统不稳定。
基于Suricata构建网络入侵检测系统:从规则编写到实战部署全解析
网络入侵检测系统(NIDS)是企业安全防御体系中的重要一环,其核心价值在于实时分析网络流量并精准识别潜在攻击行为。Suricata作为一款高性能开源引擎,凭借多线程处理、自动协议解析、原生JSON日志输出等特性,已逐步取代传统单线程工具成为主流选择。其检测原理主要基于特征规则匹配,结合协议解析与状态跟踪,实现从抓包、解析、匹配到告警的完整闭环。在工程实践中,规则语法的正确性、配置文件路径的准确性以及日志输出的可观测性,都是影响系统落地效果的关键因素。该技术广泛应用于校园网监测、企业边界防护、安全研究等场景,
Libpcap详细教程
Libpcap 的工作原理可以描述为,当一个数据包到达网卡时,Libpcap 利用创建的套接字从链路层驱动程序中获得该数据包的拷贝,即旁路机制,同时通过 Tap 函数将数据包发给 BPF 过滤器。
libpcap qt
**实现入侵检测**:考虑到文件名为“Linux 入侵检测”,这个嗅探程序可能包含了入侵检测的功能。通过分析数据包内容,比如异常的访问模式、重复的失败登录尝试等,可以实现基本的入侵检测。
libpcap抓包原理[参考].pdf
当一个数据包到达网络接口时,libpcap 首先利用已经创建的 Socket 从链路层驱动程序中获得该数据包的拷贝,再通过 Tap 函数将数据包发给 BPF 过滤器。


