Windows下开启混杂模式
void OpenPromiscuous()
{
WSADATA wsadata = { 0,0, };
DWORD wsaioctl_promiscuous_mode = RCVALL_ON;
DWORD wsaioctl_promiscuous_mode_out[128] = { 0 };
DWORD cbBytesOut = sizeof(wsaioctl_promiscuous_mode_out);
DWORD cbBytesReturned = cbBytesOut;
struct sockaddr_in local_addr;
WSAStartup(MAKEWORD(2, 2), &wsadata);
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_port = htons(3717);
SOCKET s = socket(PF_INET, SOCK_RAW, IPPROTO_IP);
if (bind(s, (struct sockaddr*)&local_addr, sizeof(local_addr)) == SOCKET_ERROR)
{
printf("bind failed(errno=%d)\n", WSAGetLastError());
}
// 设置混杂模式 抓取所有经过网卡的数据包
if (ioctlsocket(s, SIO_RCVALL, &wsaioctl_promiscuous_mode) == SOCKET_ERROR)
{
printf("ioctlsocket failed(errno=%d)\n", WSAGetLastError());
}
closesocket(s);
s = WSASocketW(AF_INET, SOCK_RAW, 0, NULL, 0, WSA_FLAG_OVERLAPPED);
if (bind(s, (struct sockaddr*)&local_addr, sizeof(local_addr)) == SOCKET_ERROR)
{
printf("bind failed(errno=%d)\n", WSAGetLastError());
}
// 设置混杂模式 抓取所有经过网卡的数据包
if (WSAIoctl(s, SIO_RCVALL, &wsaioctl_promiscuous_mode, sizeof(wsaioctl_promiscuous_mode), NULL, 0, &cbBytesReturned, NULL, NULL) == SOCKET_ERROR)
{
printf("WSAIoctl failed(errno=%d)\n", WSAGetLastError());
}
closesocket(s);
WSACleanup();
}