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)

---------------------------------------------------------------

Method 4: Use the Network Neighbor method, but only the hosts in this Working Group can be enumerated

Source code:

DWORD dwscope = resource_context;
Netresource * netresource = NULL;
Handle henum;
Wnetopenenum (dwscope, null, null,
Null, & henum );
If (henum)
{
Dword count = 0 xffffffff;
DWORD buffersize = 2048;
Lpvoid buffer = new char [2048];
Wnetenumresource (henum, & count,
Buffer, & buffersize );
Netresource = (netresource *) buffer;

Char szhostname [200];
Unsigned int I;

For (I = 0; I <buffersize/sizeof (netresource); I ++, netresource ++)
{
If (netresource-> dwusage = resourceusage_container & netresource-> dwtype = resourcetype_any)
{
If (netresource-> lpremotename)
{
Cstring strfullname = netresource-> lpremotename;
If (0 = strfullname. Left (2). Compare ("\\\\"))
Strfullname = strfullname. Right (strfullname. getlength ()-2 );
Gethostname (szhostname, strlen (szhostname ));
Host = gethostbyname (strfullname );
If (host = NULL) continue;
Strtemp. Format ("% s", strfullname );
M_list.insertitem (I, strtemp, 0); // obtain the address and add it to the list.
}
}
}
Delete buffer;
Wnetcloseenum (henum );

---------------------------------------------------------------

The fourth method is to use the netenum API as the network neighbor. There is an example on codeproject.
Www.codeproject.com/treectrl/pathpicker.asp

--------------------------------------------------------------


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;
}

If (+ + TEM) % 18 = 0)
{
BADD = true;
Strhost. trimright (char) null );
If (strhost = "")
{
Strmac. Delete (17, Strmac. getlength ()-17 );
M_listview.setitem (index, 4, lvif_text, Strmac, 0, 0, 0 );
Break;
}


If (num = 0 & strhost! = "")
{
M_listview.setitem (index, 2, lvif_text, strhost, 0, 0, 0 );
Host = strhost;
Num ++;
}
Else
{
If (host! = Strhost & num = 1 & strhost! = "")
{
M_listview.setitem (index, 1, lvif_text, strhost, 0, 0, 0 );
Group = strhost;
Num ++;
}
Else
{
If (strhost! = Host & strhost! = Group & num = 2 & strhost! = "")
{
User = strhost;
If (user! = "_ Msbrowse __")
{
M_listview.setitem (index, 3, lvif_text, user, 0, 0, 0 );
Num ++;
}
}
}

}

Strmac = strhex;
Strhost = "";
Strhex = "";

}

}
// Trigger the event, resulting in the continued execution of the thread function
Setevent (wait_handle );


}

---------------------------------------------------------------

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 );

}
Else
{
Trace ("sendarp error % d", RET );
}
Return 0;
}

/* Enumerate all hosts in the LAN and insert the IP/MAC pair to The ListBox */
Void ctestarpdlg: onbutton1 ()
{
// Todo: add your control notification handler code here
Handle hthread [1, 254];
Cstring ipsuffix = "192.168.10 .";
Cstring Strip [254];
Initializecriticalsection (& CS );
For (INT I = 0; I <254; I ++)
{
Strip [I]. Format ("% d", I + 1 );
Strip [I] = ipsuffix + Strip [I];
Hthread [I] = createthread (null, 0, arpthread, strip [I]. getbuffer (0), 0, null );

}
/* Haha, because only 64 kernel objects can be waited at a time, only a few times */
/* You can also use a loop */
Waitformultipleobjects (64, hthread, true, infinite );
Waitformultipleobjects (64, & hthread [64], true, infinite );
Waitformultipleobjects (64, & hthread [128], true, infinite );
Waitformultipleobjects (62, & hthread [192], true, infinite );
Deletecriticalsection (& CS );
Cstring temp;
For (I = 0; I {
Pbyte PMAC = (pbyte) host [I]. szmac;
Temp. format ("% s (% 02x-% 02x-% 02x-% 02x-% 02x-% 02x)", host [I]. szip, PMAC [0], PMAC [1], PMAC [2], PMAC [3], PMAC [4], PMAC [5]);
M_list.addstring (temp );
}


}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.