等完善工作
/*
show the protocal address,port and the length of a package
compile method: g++ filter.cpp -lpcap -o filter
feuvan@smth
//*******************************************/
#include <pcap.h>
#include <stdio.h>
#include "filter.h"
int main(int argc, char *argv[])
{
pcap_t *handle; /* Session handle */
char *dev; /* The device to sniff on */
char errbuf[PCAP_ERRBUF_SIZE]; /* Error string */
struct bpf_program filter; /* The compiled filter */
char filter_str[] = ""; /* The filter expression */
bpf_u_int32 mask; /* Our netmask */
bpf_u_int32 net; /* Our IP */
struct pcap_pkthdr header; /* The header that pcap gives us */
const u_char *packet; /* The actual packet */
struct ethernet_header *ethernet; /* The ethernet header */
struct ip_header *ip; /* The IP header */
struct tcp_header *tcp; /* The TCP header */
struct udp_header *udp; /* The UDP header */
/* Define the device */
dev = pcap_lookupdev(errbuf);
printf("Found device:%s\n",dev);
/* Find the properties for the device */
pcap_lookupnet(dev, &net, &mask, errbuf);
/* Open the session in promiscuous mode */
handle = pcap_open_live(dev, BUFSIZ, 1, 0, errbuf);
/* Compile and apply the filter */
pcap_compile(handle, &filter, NULL , 1 , 0);
pcap_setfilter(handle, &filter);
printf("Only for ethernet. Press Ctrl+C to quit\n");
while(1)
{
packet = pcap_next(handle, &header);
ethernet = (struct ethernet_header*)(packet);
if(ethernet->ether_type == ETHERNETTYPE_IP) /*IP datagrame*/
{
ip = (struct ip_header*)(packet + ETHERHEADER_LEN);
/*
printf("ip_hl=%d\n",ip->ip_hl);
printf("ip_tos=%d\n",ip->ip_tos);
printf("ip_len=%d\n",ip->ip_len);
printf("ip_id=%d\n",ip->ip_id);
printf("ip_off=%d\n",ip->ip_off);
printf("ip_ttl=%d\n",ip->ip_ttl);
printf("ip_p=%d\n",ip->ip_p);
printf("ip_sum=%d\n",ip->ip_checksum);
*/
//printf("ip_v=%d ip_hl=%d\n",ip->ip_v,ip->ip_hl);
/*
int x=0;
for (;x<header.len;x++)
{
printf("%02X",(packet+ETHERHEADER_LEN)[x]);
if (x % 8 == 7)
printf(" ");
if (x % 32 == 31)
printf("\n");
}
/*****/
if(ip->ip_p == IPTYPE_TCP)
{
tcp = (struct tcp_header*)(packet + ETHERHEADER_LEN + ip->ip_hl*4);
printf("%d.%d.%d.%d(%x %x %x %x %x %x):%d %s",ip->ip_src[0],ip->ip_src[1],ip->ip_src[2],ip->ip_src[3],
ethernet->ether_shost[0],ethernet->ether_shost[1],ethernet->ether_shost[2],ethernet->ether_shost[3],
ethernet->ether_shost[4],ethernet->ether_shost[5],tcp->th_sport,"tcp");
printf(" -> %d.%d.%d.%d(%x %x %x %x %x %x):%d length:%d\n",ip->ip_dst[0],ip->ip_dst[1],ip->ip_dst[2],
ip->ip_dst[3],ethernet->ether_dhost[0],ethernet->ether_dhost[1],ethernet->ether_dhost[2],
ethernet->ether_dhost[3],ethernet->ether_dhost[4],ethernet->ether_dhost[5],tcp->th_dport,header.len);
}
else if(ip->ip_p == IPTYPE_UDP)
{
continue;
udp = (struct udp_header*)(packet +ETHERHEADER_LEN + ip->ip_hl*4);
printf("%d.%d.%d.%d(%x %x %x %x %x %x):%d %s",ip->ip_src[0],ip->ip_src[1],ip->ip_src[2],ip->ip_src[3],
ethernet->ether_shost[0],ethernet->ether_shost[1],ethernet->ether_shost[2],ethernet->ether_shost[3],
ethernet->ether_shost[4],ethernet->ether_shost[5],udp->th_sport,"udp");
printf(" -> %d.%d.%d.%d(%x %x %x %x %x %x):%d length:%d\n",ip->ip_dst[0],ip->ip_dst[1],ip->ip_dst[2],
ip->ip_dst[3],ethernet->ether_dhost[0],ethernet->ether_dhost[1],ethernet->ether_dhost[2],
ethernet->ether_dhost[3],ethernet->ether_dhost[4],ethernet->ether_dhost[5],udp->th_dport,header.len);
}
}
}
pcap_close(handle);
return 0;
}