关于数据包嗅探器的!!!急用 !!
以下是一个数据包嗅探器的源代码,它主要完成的任务是截获网络数据包并解析数据包文头中的各字段的意义。(注:是 唐正军 编著的《网络入侵检测系统的设计与实现》一书中的)
*******小弟无论如何也调试不出来,请各位高手帮忙了!!急用啊 ********
只要能成功实现功能就行
先谢过了
谢能解答的和未能解答问题的高手!!!!!!!!
**********************TCP.C*****************
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "headers.h"
int main()
{
int sock,bytes_received,fromlen;
char buffer[65535];
struct sockaddr_in from;
struct ip *ip;
struct tcp *tcp;
sock=socket(AF_INET,SOCK_RAW,IPPROTO_TCP);
while(1)
{
fromlen=sizeof(from);
bytes_received=recvfrom(sock,buffer,sizeof(buffer),0,(struct sockaddr*)&from,&fromlen);
printf("\nBytes received:::%5d\n",bytes_received);
printf("Source address:::%s\n",inet_ntoa(from.sin_addr));
ip=(struct ip *)buffer;
printf("IP header length:::%d\n",ip->ip_length);
printf("Protocol:::%d\n",ip->ip_protocol);
tcp=(struct tcp *)(buffer+4*ip->ip_length);
printf("Source port:::%d\n",ntohs(tcp->tcp_source_port));
printf("Dest port:::%d\n",ntohs(tcp->tcp_dest_port));
}
}
*************************headers.h************************
struct ip{
unsigned int ip_versin:4;
unsigned int ip_length:4;
unsigned char ip_tos;
unsigned short ip_tatal_length;
unsigned short ip_id;
unsigned short ip_flags;
unsigned char ip_ttl;
unsigned char ip_protocol;
unsigned short ip_cksum;
unsigned int ip_source;
unsigned int ip_dest;
};
struct tcp{
unsigned short tcp_source_port;
unsigned short tcp_dest_port;
unsigned int tcp_sepno;
unsigned int tcp_ackno;
unsigned int tcp_res1:4,
tcp_hlen:4,
tcp_fin:1,
tcp_syn:1,
tcp_rst:1,
tcp_ack:1,
tcp_urg:1,
cp_res2:2;
unsigned short tcp_winsize;
unsigned short tcp_cksum;
unsigned short tcp_urgent;
};

