这是我的代码:
#include
#include
#include
#include
#include
#include
#include
#define MAX_HOSTENT_LEN 256
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define MAX_ADDR_LEN 16
typedef struct tcpheader
{
unsigned short int sport; //source address
unsigned short int dport; //destination address
unsigned int th_seq; //sequence number
unsigned int th_ack; //acknowledge number
unsigned char th_x2:4; //header length
unsigned char th_off:4; //reserved
unsigned char th_flag; //flags: URG ACK PSH RST SYN FIN
unsigned short int th_win; //window size
unsigned short int th_sum; //check sum
unsigned short int th_urp; //urgent pointer
}TCP_HDR;
struct ipheader
{
unsigned char h_lenver; //version & header length
unsigned char ip_tos; //tos
unsigned short int ip_len; //total length
unsigned short int ip_id; //id
unsigned short int ip_off; //offset
unsigned char ip_ttl; //time to live
unsigned char ip_p; //protocal
unsigned short int ip_sum; //check sum
unsigned int ip_src; //source address
unsigned int ip_dst; //destination address
}IP_HDR;
typedef struct udphdr
{
unsigned short sport; //source port
unsigned short dport; //destination port
unsigned short len; //UDP length
unsigned short cksum; //check sum(include data)
} UDP_HDR;
typedef struct icmphdr
{
unsigned short sport;
unsigned short dport;
BYTE i_type;
BYTE i_code;
USHORT i_cksum;
USHORT i_id;
USHORT i_seq;
ULONG timestamp;
}ICMP_HDR;
int nBindPort(SOCKET &sock);
int nSetPromisc(SOCKET &sock);
#pragma comment(lib,"WS2_32.lib")
int main()
{
SOCKET sock;
int nRet;
if((nRet = nBindPort(sock)) !=0)
{
printf("bind port error[%d]\n", WSAGetLastError());
exit(1);
}
if((nRet=nSetPromisc(sock)) != 0)
{
printf("set promisc error[%d]\n", WSAGetLastError());
exit(1);
}
while(1)
{
break;
}
return 0;
}
int nBindPort(SOCKET &sock)
{
WSADATA wsd;
SOCKADDR_IN sa;
int nRet;
WSAStartup(MAKEWORD(2,1),&wsd);
if((sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP)) == SOCKET_ERROR)
{
printf("socket error\n");
return -1;
}
#if 1
BOOL flag =TRUE ;
//设置亲自处理IP报文头
nRet = setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag));
if(nRet)
{
printf("setsockopt error\n");
return -1;
}
char LocalName[MAX_HOSTENT_LEN];
struct hostent FAR * pHostent;
pHostent = (struct hostent * )malloc(sizeof(struct hostent));
gethostname((char*)LocalName, sizeof(LocalName)-1);
pHostent = gethostbyname((char*)LocalName);
#endif
memset(&sa, 0x00, sizeof(sa));
sa.sin_family=AF_INET;
sa.sin_port=htons(0);
//sa.sin_addr.s_addr = inet_addr("192.168.1.108");
//sa.sin_addr.S_un.S_addr = inet_addr("192.168.1.108");
sa.sin_addr = *(in_addr *)pHostent->h_addr_list[0];
if(bind(sock, (SOCKADDR *)&sa, sizeof(sa)) != 0)
{
printf("bind error\n");
return -2;
}
return 0;
}
int nSetPromisc(SOCKET &sock)
{
int nRet;
BOOL flag =TRUE ;
DWORD dwOutBuffer[10];
DWORD dwInBufferLen = 1;//RCVALL_ON
DWORD dwBytesReturned = 0;
unsigned int optval;
//设置亲自处理IP报文头
nRet = setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag));
if(nRet)
{
printf("setsockopt error\n");
return -1;
}
#if 1
//网卡设置混杂模式函数
nRet = WSAIoctl(sock, SIO_RCVALL, &dwInBufferLen, (DWORD)sizeof(dwInBufferLen),
dwOutBuffer, (DWORD)sizeof(dwOutBuffer), &dwBytesReturned, NULL, NULL);
if(nRet)
{
printf("WSAIoctl error\n");
return -1;
}
#else
DWORD dwValue = 1;
nRet = ioctlsocket(sock, SIO_RCVALL, &dwValue);
if(nRet)
{
printf("ioctlsocket error\n");
return -1;
}
#endif
return 0;
}