Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15315644
  • 博文数量: 112
  • 博客积分: 11195
  • 博客等级: 上将
  • 技术积分: 1989
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-20 11:04
文章分类

全部博文(112)

文章存档

2013年(2)

2012年(27)

2011年(6)

2010年(11)

2009年(6)

2007年(7)

2006年(23)

2005年(30)

分类: WINDOWS

2006-05-09 18:13:13

#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;
}
阅读(35250) | 评论(0) | 转发(0) |
0

上一篇:linux2.6.9源码目录结构

下一篇:涅盘

给主人留下些什么吧!~~