};
};
class protocol_tcp :public protocol
{
public:
virtual bool judge(const char * pkg) const
{
if( char_to_ip(pkg)->protocol==6)
return true;
else
return false;
}
virtual processor* create_processor() const
{
return new(processor_buffer) processor_tcp;
}
};
/*********************************udp*************************************************/
class processor_udp:public processor
{
public:
virtual void print(const char * pkg) const
{
cout<<"-----------------------------------------------------------------"< cout<<"this is a udp packet"< print_ip(pkg);
cout<<"source port:"<source)< cout<<"dest port:"<dest)< cout<<"length:"<len<
};
};
class protocol_udp :public protocol
{
public:
virtual bool judge(const char * pkg) const
{
if( char_to_ip(pkg)->protocol==17)
return true;
else
return false;
}
virtual processor* create_processor() const
{
return new(processor_buffer) processor_udp;
}
};
/*********************************udp*************************************************/
class processor_icmp:public processor
{
public:
virtual void print(const char * pkg) const
{
cout<<"-----------------------------------------------------------------"< cout<<"this is a icmp packet"< print_ip(pkg);
};
};
class protocol_icmp :public protocol
{
public:
virtual bool judge(const char * pkg) const
{
if( char_to_ip(pkg)->protocol==1)
return true;
else
return false;
}
virtual processor* create_processor() const
{
return new(processor_buffer) processor_icmp;
}
};
class manager
{
vector container_;
public:
~manager()
{
}
template
void install_protocol()
{
container_.push_back( new _Proctocol());
}
int create_process(const char * pkg) const
{
for(vector::const_iterator it = container_.begin(); it != container_.end(); it++)
{
if((*it)->judge(pkg) == true)
{
processor * xx=(*it)->create_processor();
return 1;
}
}
return 0;
}
};
int main(int argc,char *argv[])
{
manager mgr;
#ifdef WIN
WSADATA wsaData;
SOCKADDR_IN saddr;
SOCKET sniffer;
int len;
struct sockaddr_in addr;
#else
int sniffer;
#endif
int err;
char *buf1=(char *)malloc(1518);
char *buf=buf1+14;
int num;
if(argc!=2)
{
cout<<"Input error,such as:\n"< return -1;
}
#ifdef WIN
err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
if ( err != 0 ) {
cout<<"error!WSAStartup failed!"< return -1;
}
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr(argv[1]);
saddr.sin_port = htons(555);
if((sniffer=socket(AF_INET,SOCK_RAW,IPPROTO_IP))==SOCKET_ERROR)
{
cout<<"socket failed!"< return -1;
}
if(bind(sniffer,(SOCKADDR *)&saddr,sizeof(saddr))==SOCKET_ERROR)
{
cout<<"bind failed!"< return -1;
}
ioct(sniffer);
len = sizeof(addr);
#else
if((sniffer=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_ALL)))==-1)
{
die("socket", 1);
}
do_promisc("eth0", sniffer);
#endif
mgr.install_protocol();
mgr.install_protocol();
mgr.install_protocol();
while(1)
{
#ifndef WIN
num = recv(sniffer,buf1,1518,0);
#else
num = recvfrom(sniffer,buf,1500, 0, (struct sockaddr *)&addr,&len);
#endif
if(num>0)
{
if(mgr.create_process(buf)==1)
{
( reinterpret_cast(processor_buffer))->print(buf);
}
}
}
#ifdef WIN
closesocket(sniffer);
WSACleanup();
#endif
return 0;
}
#ifdef WIN
int ioct(SOCKET sniffer)
{
DWORD dwBufferLen[10] ;
DWORD dwBufferInLen = 1 ;
DWORD dwBytesReturned = 0 ;
WSAIoctl(sniffer, SIO_RCVALL,&dwBufferInLen, sizeof(dwBufferInLen),&dwBufferLen, sizeof(dwBufferLen),&dwBytesReturned , NULL , NULL );
}
#else
int do_promisc(char *nif, int sock )
{
struct ifreq ifr;
strncpy(ifr.ifr_name, nif,strlen(nif)+1);
if((ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)) //..flag
{
die("ioctl", 2);
}
ifr.ifr_flags |= IFF_PROMISC; //..flag..
if(ioctl(sock, SIOCSIFFLAGS, &ifr) == -1 ) //....
{
die("ioctl", 3);
}
}
void die(char *why, int n)
{
perror(why);
exit(n);
}
#endif
int print_ip(const char * ip_hdr)
{
cout<<"version:"<version<cout<<"TTL:"<ttl<cout<<"tot_len:"<tot_len<cout<<"ID:"<id<cout<<"source ip:"<saddr_u.ip_str_saddr.ip1<<"."<saddr_u.ip_str_saddr.ip2<<"."<saddr_u.ip_str_saddr.ip3<<"."<saddr_u.ip_str_saddr.ip4<cout<<"dest ip:"<daddr_u.ip_str_daddr.ip1<<"."<daddr_u.ip_str_daddr.ip2<<"."<daddr_u.ip_str_daddr.ip3<<"."<daddr_u.ip_str_daddr.ip4<
return 0;
}
inline struct iphdr * char_to_ip(const char * pkg)
{
return reinterpret_cast(const_cast(pkg));
}
inline struct udphdr * char_to_udp(const char * pkg)
{
return reinterpret_cast(const_cast(pkg));
}
inline struct tcphdr * char_to_tcp(const char * pkg)
{
return reinterpret_cast(const_cast(pkg));
}