How to quickly enumerate the IP addresses of all active hosts in the LAN)
Source: Internet
Author: User
I want to quickly search all hosts. I have tried the following methods:
1. Ping, of course, but if you want Program And I don't think the speed is too fast.
2. You can use Winpcap to send an ARP request packet, but the acquisition time is uncertain and the acquisition speed is not too fast.
3. Using the sendarp function, it is too slow to scan 255 hosts and the program cannot respond. So I tried? The sendarp function is called by 55 threads respectively. The result shows that most sendarp calls fail with the error value 31. I don't know why
4. some people on the internet use the method of Enumerating network resources, obtaining host information, obtaining IP addresses by host names, and obtaining MAC addresses by IP addresses, I don't think the speed is too fast (I have not tried it)
In the past two days, we see a UDP-based CIDR Block scanner. Code The following figure shows the Working Group, machine name, user name, and MAC address.
Byte BS [50] = {0 x, 0x10, 0x0 0 x, 0x43, 0x4b, 0 x x 1 };
Handle wait_handle;
M_udpsocket.sendto (void *) BS, 50, destport, m_strip, 0); // send a datagram to the specified IP Address
Waitforsingleobject (
Wait_handle, // wait for the event handle
Nwait // timeout
);
Resetevent (wait_handle); // reset the event back to the non-trigger status
Void cnbtstatdlg: onreceive ()
{
Byte Buf [500];
Cstring STR, strip, strhost, strhex, Strmac, host, group, user;
Uint dport;
M_udpsocket.receivefrom (BUF, 500, strip, dport, 0); // receives data
// If the received IP address is null or the IP address is the same as the original IP address
If (strip = (char) null | strip = stroldip) return;
Stroldip = strip;
Int Index = m_listview.insertitem (0, strip); // insert IP into listview
Strhost = ""; // machine name
Strhex = ""; // MAC address
User = "? ";//
Host = "\\";
Int TEM = 0, num = 0;
Bool BADD = true;
// Obtain the corresponding information according to the datagram rule
For (I = 57; I <500; I ++) // 57-72
{
If (BUF [I] = 0xcc) break;
If (BUF [I] = 0x20) BADD = false;
If (BADD)
{
Str. Format ("% C", Buf [I]);
If (BUF [I]> = '') strhost + = STR;
Str. Format ("% 02x.", Buf [I]);
Strhex + = STR;
}
The method of using multiple threads to call sendarp is good, and the efficiency is very high (11 machines in my local area network are online in a few seconds). You don't need to run sleep () every time you open a thread, which is very slow, you can pre-define a cstring szip [255]; array as a parameter of the 255 threads. The following is a rough idea of the program:
// Global variable
Typedef struct ipmac
{
Char szip [16];
Char szmac [6];
} Ipmac;
Ipmac host [255];
Int K = 0;
Critical_section Cs;
/* Thread function, used to query the MAC address corresponding to the IP address */
DWORD winapi arpthread (lpvoid lparam)
{
Char * szip = (char *) lparam;
Ulong PMAC [2];
Ulong pulen = 6;
Int ret;
Trace (szip );
If (ret = sendarp (inet_addr (szip), 0, PMAC, & pulen) = 0)
{
Entercriticalsection (& CS); // multithreading synchronization, huh: 0
Strcpy (host [K]. szip, szip );
Pbyte = (pbyte) PMAC;
For (INT I = 0; I <5; I ++)
{
Host [K]. szmac [I] = pbyte [I];
Trace ("% 02x-", pbyte [I]);
}
Trace ("% 02x", pbyte [5]);
Host [K]. szmac [5] = pbyte [5];
K ++;
Leavecriticalsection (& CS );
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.