|
![]() | 作者: hack520 [hack520]
![]() |
登录 |
请提示如下代码: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netinet/in_systm.h> #include<netinet/ip.h> #include<netinet/if_ether.h> #include<pcap.h> #include<netdb.h> #define MAXSTRINGSIZE 256 #define MAXSIZE 1024 #fefine DEFAULT_SNAPLEN 68 typedef struct { unsigned long int ipaddr; char hostname[MAXSTRINGSIZE]; }dnstable; typedef struct { dnstable table[MAXSIZE]; int front; int rear; }sequeue; sequeue *sq; sq->rear=sq->front=0; void print_hwadd(u_char * hwadd) { for(int i=0,i<5;++i) printf("%2x:",hwadd[i]); printf("%2x",hwadd[i]); } void print_ipadd(u_char *ipadd) { for(int i=0;i<3;++i) printf("%d.",ipadd[i]); printf("%d",ipadd[i]); } void getportname(int portno,char portna[],char* proto) { if(getservbyport(htons(portno),proto)!=NULL) { strcpy(portna,getservbyport(htons(portno),proto)->s_name); } else sprintf(portna,"%d",portno); } void iptohost(unsigned long int ipad,char* hostn) { struct hostent * shostname; int m,n,i; m=sq->rear; n=sq->front; for(i=n%MAXSIZE;i=m%MAXSIZE;i=(++n)%MAXSIZE) { if(sq->table[i].ipaddr==ipad) { strcpy(hostn,sq->table[i].hostname); break; } } if(i=m%MAXSIZE) { if((sq->rear+1)%MAXSIZE=sq->front) sq->front=(sq->front+1)%MAXSIZE; sq->table[i].ipaddr=ipad; shostname=gethostbyaddr((char*)&ipad,sizeof(ipad),AF_INET); if(shostname!=NULL) strcpy(sq->table[i].hostname,shostname->h_name); else strcpy(sq->table[i].hostname,""); sq->rear=(sq->rear+1)%MAXSIZE; } } void print_hostname(u_char* ipadd) { unsigned long int ipad; char hostn[MAXSTRINTSIZE]; ipad=*((unsigned long int *)ipadd); iptohost(ipad,hostn) if(strlen(hostn)>0) printf("%s",hostn); else print_ipadd(ipadd); } void packet_proce(u_char* packets,const struct pcap_pkthdr * header,const u_char *pp) { struct ether_header * eth; struct ether_arp * arth; struct ip * iph; struct tcphdr * tcph; struct udphdr * udph; u_short srcport,dstport; char protocol[MAXSTRINGSIZE]; char srcp[MAXSTRINGSIZE],dstp[MAXSTRINGSIZE]; unsigned int ptype; u_char * data; u_char tcpudpdata[MAXSTRINGSIZE]; int i; eth=(struct ether_header *)pp; ptype=ntohs(((struct ether_header *)pp)->ether_type); if((ptype==ETHERTYPE_ARP)||(ptype==ETHERTYPE_RARP)) { arph=(struct ether_arp *)(pp+sizeof(struct ether_header)); if(ptype==ETHERTYPE_ARP) printf("arp "); else printf("rarp "); print_hwadd((u_char *)&(arph->arp_sha)); printf("("); print_hostname((u_char *)&(arph->arp_spa)); printf(")->"); print_hwadd((u_char *)&(arph->arp_tha)); printf("("); print_hostname((u_char *)&(arph->arp_tpa)); printf(")\tpacketlen:%d",header->len); } else if(ptype==ETHERTYPE_IP) { iph=(struct ip *)(pp+sizeof(struct ether_header)); if(iph->ip_p==1) { strcpy(protocol,"icmp"); srcport=dstport=0; } else if(iph->ip_p==6) { strcpy(protocol,"tcp"); tcph=(struct tcphdr *)(pp+sizeof(struct ether_header)+4*iph->ip_hl); srcport=ntohs(tcph->source); dstport=ntohs(tcph->dest); data=(u_char *)(pp+sizeof(struct ether_header)+4*iph->ip_hl+4*tcph->doff); for(i=0;i<MAXSTRINGSIZE-1;++i) { if(i>=header->len-sizeof(struct ether_header)-4*iph->ip_hl-4*tcph->doff); break; else tcpudpdata[i]=data[i]; } } else if(iph->ip_p=17) { strcpy(protocol,"udp"); udph=(struct udphdr *)(pp+sizeof(struct ether_header)+4*iph->ip_hl); srcport=ntohs(udph->source); dstport=ntohs(udph->dest); data=(u_char *)(pp+sizeof(struct ether_header)+4*iph->ip_hl+8); for(i=0;i<MAXSTRINGSIZE-1;++i) { if(i>=header->len-sizeof(struct ether_header)-4*iph->ip_hl-8); break; else tcpudpdata[i]=data[i]; } } tcpudpdata[i]='\0'; getportname(srcport,srcp,protocol); getportname(dstport,dstp,protocol); printf("ip "); print_hwadd(eth->ether_shost); printf("("); print_hostname((u_char *)&(iph->ip_src)); printf(")[%s:%s]->",protocol,srcp); print_hwadd(eth->ether_dhost); printf("("); print_hostname((u_char *)&(iph->ip_dst)); printf(")[%s:%s]",protocol,dstp); printf("\tttl:%d packetlen:%d,iph->ttl,header->len); printf("\n"); printf("%s",tcpudpdata); printf("==endpacket=="); } printf("\n"); } int main(int argc,char ** argv) { char ebuf[pcap_ERRBUF_SIZE]; pcap * pd; if(argc<=1) { printf("usage:%s<network interface>\n",argv[0]); exit(0); } if((pd=pcap_open_live(argv[1],DEFAULT_SNAPLEN,1,1000,ebuf))=NULL) { (void)fprintf(stderr,"%s",ebuf); exit(1); } if(pcap_loop(pd,-1,packet_proce,NULL)<0) { (void)fprintf(stderr,"pcap_loop:%s\n",pcap_geterr(pd)); exit(1); } pcap_colse(pd); exit(0); } 编出错如下!请帮助! Error: Unable to open include file 'SYS/SOCKET.H' #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netinet/in_systm.h> #include<netinet/ip.h> #include<netinet/if_ether.h> #include<pcap.h> #include<netdb.h> #define MAXSTRINGSIZE 256 #define MAXSIZE 1024 ────────────────────────────────── Message ─────────────────────────────────── Compiling C:\TURBOC2\TEST\US.C: •Error C:\TURBOC2\TEST\US.C 3: Unable to open include file 'SYS/SOCKET.H' Error C:\TURBOC2\TEST\US.C 4: Unable to open include file 'NETINET/IN.H' Error C:\TURBOC2\TEST\US.C 5: Unable to open include file 'NETINET/IN_SYSTM.H Error C:\TURBOC2\TEST\US.C 6: Unable to open include file 'NETINET/IP.H' Error C:\TURBOC2\TEST\US.C 7: Unable to open include file 'NETINET/IF_ETHER.H Error C:\TURBOC2\TEST\US.C 8: Unable to open include file 'PCAP.H' Error C:\TURBOC2\TEST\US.C 9: Unable to open include file 'NETDB.H' |
地主 发表时间: 10/24 22:48 |
![]() | 回复: ceo_8008 [ceo_8008] ![]() |
登录 |
你TC里的库函数丢失・・・ 应该是吧~~~~ |
B1层 发表时间: 10/25 00:12 |
![]() | 回复: hcz [hcz] ![]() |
登录 |
你必须把*.h指定他的绝对路径 比如你的*.h 是装在c:\tc\include\ 要调用的时候必须#include <c:\tc\include\?.h> |
B2层 发表时间: 10/26 12:43 |
![]() | 回复: Garu [syshunter] ![]() |
登录 |
晕~`,这要能用TC编译我就把电脑吃了,呵呵。放linux下用GCC编译,我保证你通过。 |
B3层 发表时间: 10/26 14:22 |
![]() | 回复: ceo_8008 [ceo_8008] ![]() |
登录 |
Garu版主 不怕你见笑,我刚学C 而且在此之前没有接触过其他的任何语言 我觉得C语言程序用Tucbo C应该就可以编译的 这个程序为什么不行呢? |
B4层 发表时间: 10/26 15:03 |
![]() | 回复: Garu [syshunter] ![]() |
登录 |
不同编译环境有不同的库,这个程序用到的都是*nix系统上的库,而对应windows有自己的一套库,当然系统调用也就不同。WINDOWS下有自己的API,include的东西是由开发工具自带,比如tc\include,而*nix都在/usr/include。 |
B5层 发表时间: 10/26 15:58 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号