SendARP结合线程池扫描局域网计算机

来源:互联网 发布:公司开淘宝店要交税吗 编辑:程序博客网 时间:2024/06/09 14:46

由于SendARP本身是不能异步调用的,因此这里采用结合线程池的方法来加快扫描速度。



// Scaner.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdlib.h>#include <WinSock2.h>#include <IPHlpApi.h>#pragma comment(lib, "iphlpapi.lib")#pragma comment(lib, "ws2_32.lib")#define IT_TIME 3DWORD SrcAddr = 0;DWORD __stdcall AsySendArp(__in LPVOID lpParameter){unsigned char mac[6];in_addr addr;addr.S_un.S_addr = (ULONG)lpParameter;ULONG Len = 6;DWORD RetD = SendARP(addr.S_un.S_addr, SrcAddr, mac, &Len);//addr.S_un.S_addr = ntohl(addr.S_un.S_addr);if(RetD == NO_ERROR){printf("%s mac is ", inet_ntoa(addr));for(int i = 0; i < 5; i++)printf("%02x:", mac[i]);printf("%02x\n", mac[5]);}else{printf("%s has no mac\n", inet_ntoa(addr));}return 0;}int _tmain(int argc, _TCHAR* argv[]){PMIB_IPADDRTABLE IpTable;int it = IT_TIME;DWORD i;int chose;ULONG dwSize = 0x1000;DWORD RetD;while(it-- > 0){IpTable = (PMIB_IPADDRTABLE)malloc(dwSize);if(!IpTable){printf("内存不足\n");goto End;}//注意得到的是网络字节序,即ip的msb在底地址RetD = GetIpAddrTable(IpTable, &dwSize, FALSE);if(RetD == NO_ERROR)break;else if(RetD == ERROR_INSUFFICIENT_BUFFER){free(IpTable);}else{free(IpTable);printf("发生未知错误\n");goto End;}}for(i = 0; i < IpTable->dwNumEntries; i++){printf("    %d: %s\n", i, inet_ntoa(*(in_addr*)&(IpTable->table[i].dwAddr)));}printf("选择网络(0):");chose = -1;scanf("%d", &chose);if(chose == -1 || chose >= IpTable->dwNumEntries)chose = 0;DWORD LowAddr;DWORD HighAddr;LowAddr = ntohl((IpTable->table[chose].dwAddr & IpTable->table[chose].dwMask)) + 1;HighAddr = ntohl(IpTable->table[chose].dwAddr | ~IpTable->table[chose].dwMask) - 1;unsigned char mac[6];int size = 0;in_addr in;SrcAddr = IpTable->table[chose].dwAddr;for(i = LowAddr; i <= HighAddr; i++){QueueUserWorkItem(AsySendArp, (void*)htonl(i), WT_EXECUTELONGFUNCTION);}Sleep(-1);End:return 0;}


原创粉丝点击