您的位置:首页 > 理论基础 > 计算机网络

本人大四,马上要交毕业设计了,程序遇到麻烦。。急求助~!

2007-06-09 06:44 816 查看
 源代码只有下面这些,差某些头文件,请高手帮忙

 /************************Tcp_sniff_2.c********************/
 1.#include "stdio.h"
  #include <sys/socket.h>
  #include <netinet/in.h>
  #include <arpa/iner.h>
 2..#include "headers.h"
  #define INTERFACE "eth0"
  /*Prototype area*/
 3.int main() {  
 4.   int sock, bytes_recieved, fromlen;  
 5.   char buffer[65535];
 6.   struct sockaddr_in from;  /*定义socket结构*/
 7.   struct ip *ip;            /*定义IP和TCP结构*/
 8.   struct tcp *tcp;  
 9.   int Open_Raw_Socket(void); 
 10.  int Set_Promisc(char *interface, intsock); /*把网卡置于混杂模式*/
 11.  sock = Open_Raw_Socket();
 12.  Set_Promisc(INTERFACE, sock);
 /*捕获数据包*/
 13.  while(1)
 14.  {
 15.    fromlen = sizeof from;
 /*接受数据并把接受到的数据放入buffer中*/
 16.    bytes_recieved = recvfrom(sock, buffer, sizeofbuffer, 0, (struct sockaddr *)&from, &fromlen);
 17.    printf("/nBytes received :::%5d/n",bytes_recieved); /*显示出接受的数据字节数*/
 18.    printf("Source address :::%s/n",inet_ntoa(from.sin_addr)); /*显示出源地址*/
 /*分析数据包*/
 19.    ip = (struct ip *)buffer;
 /*判断在网络层中是否使用的TCP协议*/
 20.    if(ip->ip_protocol == 6) {
 21.       printf("IP header length :::%d/n",ip->ip_length); /*显示IP头的长度*/
 22.       printf("Protocol :::%d/n",ip->ip_protocol); /*显示协议类型,6是TCP,17是UDP*/
 23.       tcp = (struct tcp *)(buffer +(4*ip->ip_length));
 24.       printf("Source port :::%d/n",ntohs(tcp->tcp_source_port)); /*显示源端口*/
 25.       printf("Dest port :::%d/n",ntohs(tcp->tcp_dest_port)); /*显示目标端口*/
 26.     }     
 27.   }   
 28.}
 29.int Open_Raw_Socket() {    
 30.  int sock;
 31.  if((sock = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) < 0){
 /*建立socket连接,第一个参数是地址簇类型,用INTERNET类型*/
 /*第二个参数是socket类型,这里用了SOCK_RAW,它可以绕过传输层*/
 /*直接访问IP层的包,为了调用SOCK_RAW,需要有root权限*/
 /*第三个参数是协议,选IPPROTO_TCP指定了接受TCP层的内容*/
 /*Then the socket was not created properly and must die*/
 32.  perror("The raw socket was not created");
 33.  exit(0);       
 34.  }  
 35.  return(sock);   
 36. }
 37.int Set_Promisc(char *interface, int sock ) {  
 38.  struct ifreq ifr;      
 39.  strncpy(ifr.ifr_name, interface,strnlen(interface)+1);
 40. 
89a4
if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) {  
 /*Could not retrieve flags for the interface*/
 41.  perror("Could not retrive flags for the interface");
 42.  exit(0);    
 43.  } 
 44.  printf("The interface is ::: %s/n", interface);  
 45.  perror("Retrieved flags from interface successfully");
 46.  ifr.ifr_flags |= IFF_PROMISC;  
 47.  if (ioctl (sock, SIOCSIFFLAGS, &ifr) == -1 ) {  
 /*Could not set the flags on the interface */  
 48.  perror("Could not set the PROMISC flag:");
 49.  exit(0);    
 50.   }
 51.   printf("Setting interface ::: %s ::: to promisc",interface);
 52.   return(0);      
 53. }
 /***********************EOF**********************************/
 
 /*************************headers.h**************************/
 /*structure of an ip header*/ 
   struct ip {   
   unsigned int ip_length:4;   /*定义IP头的长度*/ 
   unsigned int ip_version:4;  /*IP版本,IPV4*/
   unsigned char ip_tos;    /*服务类型*/
   unsigned short ip_total_length;   /*IP数据包的总长度*/
   unsigned short ip_id;     /*鉴定域*/
    unsigned short ip_flags;   /*IP标志*/
   unsigned char ip_ttl;      /*IP包的存活期*/
   unsigned char ip_protocol;  /*IP上层的协议*/
    unsigned short ip_cksum;  /*IP头的校验和*/
   unsigned int ip_source;   /*源IP地址*/   
   unsigned int ip_dest;     /*目的IP地址*/
 };
  /* Structure of a TCP header */
  struct tcp {
  unsigned short tcp_source_port; /*定义TCP端口号*/
  unsigned short tcp_dest_port; /*TCP目的端口*/
  unsigned int tcp_seqno;  /*TCP序列号*/
   unsigned int tcp_ackno; /*发送者期望的下一个序列号*/
  unsigned int tcp_res1:4, /*TCP标志*/
   tcp_hlen:4,
  tcp_fin:1,
   tcp_syn:1,
   tcp_rst:1,
   tcp_psh:1,
   tcp_ack:1,
   tcp_urg:1,
  tcp_res2:2;
  unsigned short tcp_winsize; /*能接受的最大字节数*/
  unsigned short tcp_cksum; /*TCP检验和*/
  unsigned short tcp_urgent; /*紧急事件标志*/
  };
  /*********************EOF***********************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息