|
![]() | 作者: hack520 [hack520]
![]() |
登录 |
各位好! 小弟想学编程,可是不知众那里入手,我在网上看到一些如下程序, 请问在win2000下要用什么软件才能把他编成一个*.exe文件呢? 我现在用turboc2编过,可是出现很多错, 请帮助,非常感谢! //example.c //使用方法:example〈网络接口名〉 > 〈输出文件名〉 //例如:example etho > temp.txe //结束方法:ctrl+c //程序开始,读入头文件 #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> //pcap程序库 #include<netdb.h> //DNS检索使用 #define MAXSTRINGSIZE 256 //字符串长度 #define MAXSIZE 1024 //主机高速缓存中的最大记录条数 #fefine DEFAULT_SNAPLEN 68 /数据包数据的长度 typedef struct { unsigned long int ipaddr; //IP地址 char hostname[MAXSTRINGSIZE]; //主机名 }dnstable; //高速缓存数据结构 typedef struct { dnstable table[MAXSIZE]; int front; int rear; }sequeue; sequeue *sq; //定义缓存队列 sq->rear=sq->front=0; //初始化队列 //输出MAC地址函数 void print_hwadd(u_char * hwadd) { for(int i=0,i<5;++i) printf("%2x:",hwadd[i]); printf("%2x",hwadd[i]); } //输出IP地址的函数 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); } //将IP转化为DNS名 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) { //检查IP是否第一次出现 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; //ARP报头 struct ip * iph; //IP报头 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) //IP数据报 { iph=(struct ip *)(pp+sizeof(struct ether_header)); if(iph->ip_p==1) //ICMP报文 { strcpy(protocol,"icmp"); srcport=dstport=0; } else if(iph->ip_p==6) //TCP报文 { 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]; } } //TCP数据处理完毕 else if(iph->ip_p=17) //UDP报文 { 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"); } //Main函数取数据包并初始化程序环境 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); } //设置PCAP程序库 if((pd=pcap_open_live(argv[1],DEFAULT_SNAPLEN,1,1000,ebuf))=NULL) { (void)fprintf(stderr,"%s",ebuf); exit(1); } //循环取数据包 //改变参数-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); } //程序结束 |
地主 发表时间: 10/24 22:25 |
![]() | 回复: ceo_8008 [ceo_8008] ![]() |
登录 |
整个程序我没去看 但首先在Turbo C里不允许用//来注释的 那是在C++里的 你改一下再运行一下,对了你说要产生exe文件,你运行后到TC目录下可 以找到,它是自动产生的,具体目录在那里你可以在TC设置里查看 最后说明一下,我觉得你更多的是关注这个程序运行的结果是吧? 其中的过程对你来说不是很重要 所以想学编程,还是从简单点下手吧~~~~ ![]() |
B1层 发表时间: 10/24 22:34 |
![]() | 回复: hack520 [hack520] ![]() |
登录 |
寒枫: 非常感谢!你能给我帮助和建义,这也许是我的性格吧,也是我一直没学好编程的原因之一吧,但我想从别人的源代码中找到动力吧! 也希望还能得到你的提示 谢谢! |
B2层 发表时间: 10/24 22:42 |
![]() | 回复: ceo_8008 [ceo_8008] ![]() |
登录 |
:) 其实呢,我也是刚入门的~~~~~ 对了你的那个产生exe文件是在TC下的EXAMPLE目录下(TC默认) 你可以打开TC,OPTIONS\Directories\OutDirectories所显示的目录就是 你程序所产生的exe文件所在文件夹,你可以修改它,另外建立一个文件 夹,免得以后运行的程序多了都乱了,当然,在你修改后,记得在 directories下面有个save options,你要保存你所做的修改・・・ |
B3层 发表时间: 10/25 00:05 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号