📄 get_packet.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <pcap.h>
#define ETH_HEAD 14
typedef struct IP
{
unsigned char ver_h_len;
unsigned char tos;
unsigned short total_len;
unsigned short identifier;
unsigned short flag;
unsigned char ttl;
unsigned char protocol;
unsigned short check;
unsigned char src_ip[4];
unsigned char dest_ip[4];
}ip_hdr;/* ip header */
int main(int argc,char **argv){
pcap_t *pd;/* package identifier */
char *device = NULL;/* Network device like 'eth0' */
char stbuf[10] ={0};
char * pstbuf = &stbuf[0];
char ebuf[PCAP_ERRBUF_SIZE];/* error buffer */
char *ptr = NULL;
ip_hdr *ip;
struct pcap_pkthdr hdr;
unsigned char version,length;
unsigned long counter = 0;
char *content;
if(argc < 2){
printf("Usage: ./get_packet XXX (XXX represent the last segment of the source ip).\n");
exit(1);
}
unsigned int ip4 = atoi(argv[1]);
if(ip4 > 255){
printf("%d,invalid ip segment!\n",ip4);
exit(1);
}
// device = pcap_lookupdev(ebuf);/* get device name */
// printf("DEV is %s\n",device);
strcpy(pstbuf,"eth4");
device = pstbuf;
pd = pcap_open_live(device,1800,1,500,ebuf);/* get package identifier */
while(1){
if(( ptr = (char *)pcap_next(pd,&hdr)) != NULL){
ip = (ip_hdr *)(ptr + ETH_HEAD);
if((ip->src_ip[3] == ip4) || (ip->dest_ip[3] == ip4)){
counter ++;
printf("%ld ",counter);
printf("packet length: %d\n",ntohs(ip->total_len));
}
}
/* deal with the packages */
}
pcap_close(pd);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -