const_sun 2015-05-02 10:59 采纳率: 0%
浏览 2598
已结题

设置网卡为混杂模式时报10022错误码

这是我的代码:
#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;
}

  • 写回答

2条回答 默认 最新

  • const_sun 2015-05-02 11:01
    关注

    看之类的错误是说bind参数有误,我试下各种修改都不行啊

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?