I am writing a local network scanner in Windows to find online hosts with IP assistants, which is equivalent to nmap -PR , but without WinPcap. I know SendARP will block and send an ARP request 3 times if the remote host does not respond, so I use std::aync to create one thread for each of them, but the problem is that I want to send an ARP request every 20 ms. so that there are not too many ARP packets in a very short time.
#include <iostream> #include <future> #include <vector> #include <winsock2.h> #include <iphlpapi.h> #pragma comment(lib, "iphlpapi.lib") #pragma comment(lib, "ws2_32.lib") using namespace std; int main(int argc, char **argv) { ULONG MacAddr[2]; /* for 6-byte hardware addresses */ ULONG PhysAddrLen = 6; /* default to length of six bytes */ memset(&MacAddr, 0xff, sizeof (MacAddr)); PhysAddrLen = 6; IPAddr SrcIp = 0; IPAddr DestIp = 0; char buf[64] = {0}; size_t start = time(NULL); std::vector<std::future<DWORD> > vResults; for (auto i = 1; i< 255; i++) { sprintf(buf, "192.168.1.%d", i); DestIp = inet_addr(buf); vResults.push_back(std::async(std::launch::async, std::ref(SendARP), DestIp, SrcIp, MacAddr, &PhysAddrLen)); Sleep(20); } for (auto it= vResults.begin(); it != vResults.end(); ++it) { if (it->get() == NO_ERROR) { std::cout<<"host up\n"; } } std::cout<<"time elapsed "<<(time(NULL) - start)<<std::endl; return 0; }
First, I can do this by calling Sleep(20) after the thread starts, but one day SendARP in these threads will resend the ARP requests, if there are no responses from the remote host, this is out of my control and I see a lot of requests in a very short time (< 10ms) in Wireshark, so my question is:
- Any way to make
SendARP asynchronous? - If not, can I control
SendARP send SendARP in streams?
c ++ multithreading asynchronous winapi arp
jfly
source share