|
发表于 2004-2-28 21:46:59
|
显示全部楼层
嘿嘿~~居然也有人用libpcap~
[code:1]
#include <pcap.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/if_ether.h>
#include <stdio.h>
#include <stdlib.h>
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
char errbuf[PCAP_ERRBUF_SIZE];
char *dev;
typedef struct __eth_hdr
{
u_int8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
u_int8_t ether_shost[ETH_ALEN]; /* source ether addr */
u_int16_t ether_type; /*packet type ID field*/
}eth_hdr;
void err_exit(char *str,int i)
{
fprintf(stderr,"%s%s\n",str,errbuf);
exit(i);
}
void print_ether(u_int8_t *addr)
{
int i;
for(i=0;i<ETH_ALEN-1;i++)
printf("%d:",addr[i]);
printf("%d",addr[ETH_ALEN]);
return ;
}
void fprint_raw_data(FILE *fp,u_char *packet,unsigned len)
{
unsigned i;
for(i=0;i<len;i++)
{
// if(NULL==fp)
printf("%c",packet++);
// else
// fprintf(fp,"%c",packet++);
}
// if(NULL==fp)
printf("\n\n");
// else
// fprintf(fp,"\n\n");
return;
}
void my_call_back(u_char *usr,const struct pcap_pkthdr *pkthdr,
const u_char *packet)
{
struct in_addr addr;
eth_hdr *e_hdr;
struct iphdr *ip_hdr;
struct tcphdr *tcp_hdr;
e_hdr=(eth_hdr *)packet;
printf("%s:%d ",dev,e_hdr->ether_type);
print_ether(e_hdr->ether_shost);
printf("->");
print_ether(e_hdr->ether_dhost);
printf("\n");
if(e_hdr->ether_type==8)
{
printf("\t");
ip_hdr=(struct iphdr *)(packet+sizeof(eth_hdr));
addr.s_addr=ip_hdr->saddr;
printf("%s->",inet_ntoa(addr));
addr.s_addr=ip_hdr->daddr;
printf("%s",inet_ntoa(addr));
printf("\n\t");
printf("\t%d\n",ntohs(ip_hdr->tot_len));
if(ip_hdr->protocol==6)
{
printf("\tProtocal:TCP\n");
tcp_hdr=(struct tcphdr *)(ip_hdr+sizeof(struct iphdr));
printf("\tPort:+%d->%d",
ntohs(tcp_hdr->source),
ntohs(tcp_hdr->dest));
printf("\n");
}
fprint_raw_data(fopen("./out","w+"),
(u_char *)ip_hdr+sizeof(struct iphdr),
ntohs(ip_hdr->tot_len)-sizeof(eth_hdr)-sizeof(struct iphdr));
}
return;
}
int main(int argc,char *argv[])
{
pcap_t *hdl;
bpf_u_int32 netp;
bpf_u_int32 maskp;
struct in_addr addr;
dev=pcap_lookupdev(errbuf);
if(NULL==dev)
err_exit("pcap_loopupdev():",-1);
if(pcap_lookupnet(dev,&netp,&maskp,errbuf)<0)
err_exit("pcap_lookupnet():",-1);
addr.s_addr=netp;
printf("NET:%s\n",inet_ntoa(addr));
addr.s_addr=maskp;
printf("MASK:%s\n",inet_ntoa(addr));
hdl=pcap_open_live(dev,BUFSIZ,0,-1,errbuf);
if(NULL==hdl)
err_exit("pcap_open_live():",-1);
if((argc!=2)||(!isdigit(*argv[1])))
pcap_loop(hdl,-1,my_call_back,NULL);
else
pcap_loop(hdl,atoi(argv[1]),my_call_back,NULL);
printf("END....");
return 0;
}
[/code:1] |
|