#include
#include
#include
#include
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
int printaddress(char * ippkg);
int ioct(SOCKET sniffer);
int main()
{
WSADATA wsaData;
BOOL val;
SOCKADDR_IN saddr;
int err;
SOCKET sniffer;
int caddsize;
HANDLE mt;
DWORD tid;
char buf[2048];
int num;
err = WSAStartup( MAKEWORD( 2, 2 ), &wsaData );
if ( err != 0 ) {
printf("error!WSAStartup failed!\n");
return -1;
}
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr("10.170.15.212");
saddr.sin_port = htons(555);
if((sniffer=socket(AF_INET,SOCK_RAW,IPPROTO_IP))==SOCKET_ERROR)
{
printf("socket failed!\n");
return -1;
}
if(bind(sniffer,(SOCKADDR *)&saddr,sizeof(saddr))==SOCKET_ERROR)
{
printf("bind failed!\n");
return -1;
}
ioct(sniffer);
while(1)
{
num = recv(sniffer,buf,2048,0);
if(num>0)
{
printaddress(buf);
}
}
closesocket(sniffer);
WSACleanup();
return 0;
}
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 );
}
int printaddress(char * ippkg)
{
struct ip
{
unsigned int ip1:8;
unsigned int ip2:8;
unsigned int ip3:8;
unsigned int ip4:8;
};
struct head_len
{
unsigned int len:4;
unsigned int version:4;
};
struct port
{
unsigned int sport:16;
unsigned int dport:16;
};
struct ip *source;
struct ip *dest;
struct ip *tmp;
struct port * port1;
tmp=(struct ip*)ippkg+2;
if(tmp->ip2==6)
{
source=(struct ip*)ippkg+3;
dest=(struct ip*)ippkg+4;
port1=(struct port*)ippkg+((struct head_len*)ippkg)->len;
printf("source:%d.%d.%d.%d:%d\n",source->ip1,source->ip2,source->ip3,source->ip4,ntohs(port1->sport));
printf("dest:%d.%d.%d.%d:%d\n",dest->ip1,dest->ip2,dest->ip3,dest->ip4,ntohs(port1->dport));
}
return 0;
}
阅读(35799) | 评论(0) | 转发(0) |