我正在尝试修改当前的套接字程序,以捕获混杂的数据包。我目前所拥有的能够正常捕获数据包。关于如何在Linux上执行此操作,我已经看到了其他答案,但我需要找到一种在Windows上完成此操作的方法。
这是我的代码:
int main(int argc, char const *argv[])
{
SOCKET s; //The bound socket
struct sockaddr_in server;
int recv_len; //Size of received data
char udpbuf[BUFLEN]; //A buffer for the incoming data.
float data; //The data in the packet
//Create a socket
if ((s = socket(AF_INET, SOCK_RAW, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ADDR);
server.sin_port = htons(PORT);
//Bind socket to address
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
puts("Bind done\n");
while (true)
{
//Block statment. Code will wait until it detect packets.
if ((recv_len = recvfrom(s, udpbuf, BUFLEN, 0, 0, 0)) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
return 0;
}
答案 0 :(得分:2)
要在Windows上将套接字置于混杂模式,您需要调用WSAIoCtl()
来向套接字发出SIO_RCVALL
control code。
int main(int argc, char const *argv[])
{
WSADATA wsa;
SOCKET s; //The bound socket
struct sockaddr_in server;
int recv_len; //Size of received data
char udpbuf[BUFLEN]; //A buffer for the incoming data.
//Initialize Winsock
int err = WSAStartup(MAKEWORD(2,0), &wsa);
if (err != 0)
{
printf("Could not initialize Winsock : %d", err);
exit(EXIT_FAILURE);
}
//Create a socket
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_IP)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
WSACleanup();
exit(EXIT_FAILURE);
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(ADDR);
server.sin_port = htons(PORT);
//Bind socket to address
if (bind(s, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
closesocket(s);
WSACleanup();
exit(EXIT_FAILURE);
}
puts("Bind done\n");
// enable promiscuous mode
DWORD dwValue = RCVALL_ON;
DWORD dwBytesReturned = 0;
if (WSAIoctl(s, SIO_RCVALL, &dwValue, sizeof(dwValue), NULL, 0, &dwBytesReturned, NULL, NULL) == SOCKET_ERROR)
{
printf("Ioctl failed with error code : %d", WSAGetLastError());
closesocket(s);
WSACleanup();
exit(EXIT_FAILURE);
}
puts("Ioctl done\n");
while (true)
{
//Block statment. Code will wait until it detect packets.
if ((recv_len = recvfrom(s, udpbuf, BUFLEN, 0, 0, 0)) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d", WSAGetLastError());
closesocket(s);
WSACleanup();
exit(EXIT_FAILURE);
}
}
closesocket(s);
WSACleanup();
return 0;
}
或者,Microsoft还有一个单独的Network Monitor API,可让您完全不使用Winsock来监视和捕获网络流量。
答案 1 :(得分:0)
我建议您使用winpcap库。请参阅www.winpcap.org。 他们提供了有关如何执行此操作的综合文档和示例: https://www.winpcap.org/docs/docs_412/html/group__wpcap__tut3.html
例如Wireshark在Windows上使用winpcap。