Ver Mensaje Individual
  #3 (permalink)  
Antiguo 28/12/2007, 05:07
Avatar de dogduck
dogduck
 
Fecha de Ingreso: enero-2006
Ubicación: ¿Atlantida, Hesperides, Islas afortunadas?
Mensajes: 2.231
Antigüedad: 18 años, 10 meses
Puntos: 19
Re: Obtener IP mediante MAC en LAN

Mirate: http://www.codeguru.com/Cpp/I-N/netw...icle.php/c5451

Cita:
How do I get a MAC in C or C++, Win32 environment?

There are numerous ways to do this. As I was on my investigation, I realized that there was no clear and simple way to do this. A lot of people said to use NETSTAT and to parse the result. Some people said to create a UUID and pull the MAC from there. A couple of people said to use NetBIOS, and finally one said to query the NDIS miniport driver itself.

Although the last solution sounded the coolest, apparently doing that from user mode wasn't the easiest thing to do. I'll give you a set of solutions from worst to best and then a quick discussion of the Miniport method.

Method One: UuidCreate

One quick way to find out your MAC address, which is very hacky and I wouldn't recommend would be to create a sequential Uuid. Apparently Microsoft uses your MAC address to help it create a universally unique identifier.

All you have to do is check out bytes 2 through 8 and you are done. The code is below and the downloadable EXE and sample code is listed at the end of the article.

// Fetches the MAC address and prints it
static void GetMACaddress(void)
{
unsigned char MACData[6];

UUID uuid;
UuidCreateSequential( &uuid ); // Ask OS to create UUID

for (int i=2; i<8; i++) // Bytes 2 through 7 inclusive
// are MAC address
MACData[i - 2] = uuid.Data4[i];

PrintMACaddress(MACData); // Print MAC address
}

This code will only work in Windows 2000/XP since Microsoft replaced UuidCreate in Windows 2000/XP with one that doesn't use the PC's MAC address. UuidCreate with your MAC address can easily be considered a security risk since you are distributing your ethernet card's address.

Microsoft created UuidCreateSequential in Win2K and XP to do what the old UuidCreate did on Windows 95/98/Me. The current UuidCreate in Windows 2000/XP is not composed of a number which includes the MAC address of your primary NIC, hence they moved over that functionality to UuidCreateSequential. On the older OSes you may still use the UuidCreate function to obtain its MAC address.

This example only supports one NIC card on your PC.

Method Two: Use NetBIOS

This solution is a lot more complicated then the final solution. It supports multiple NIC cards, but requires NetBIOS to be installed on the computer. It also requires you to have the cable connected to a valid NetBIOS network. It works great under all OSes including 95/98/Me/NT/2000/XP.

// Fetches the MAC address and prints it
static void GetMACaddress(void)
{
unsigned char MACData[8]; // Allocate data structure
// for MAC (6 bytes needed)

WKSTA_TRANSPORT_INFO_0 *pwkti; // Allocate data structure
// for NetBIOS
DWORD dwEntriesRead;
DWORD dwTotalEntries;
BYTE *pbBuffer;

// Get MAC address via NetBIOS's enumerate function
NET_API_STATUS dwStatus = NetWkstaTransportEnum(
NULL, // [in] server name
0, // [in] data structure to return
&pbBuffer, // [out] pointer to buffer
MAX_PREFERRED_LENGTH, // [in] maximum length
&dwEntriesRead, // [out] counter of elements
// actually enumerated
&dwTotalEntries, // [out] total number of elements
// that could be enumerated
NULL); // [in/out] resume handle
assert(dwStatus == NERR_Success);

pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer; // type cast the buffer

for(DWORD i=1; i< dwEntriesRead; i++) // first address is
// 00000000, skip it
{ // enumerate MACs & print
swscanf((wchar_t *)pwkti[i].wkti0_transport_address,
L"%2hx%2hx%2hx%2hx%2hx%2hx",
&MACData[0],
&MACData[1],
&MACData[2],
&MACData[3],
&MACData[4],
&MACData[5]);
PrintMACaddress(MACData);
}

// Release pbBuffer allocated by above function
dwStatus = NetApiBufferFree(pbBuffer);
assert(dwStatus == NERR_Success);
}

As you can tell converting the wide string which is returned from NetWkstaTransportEnum to an BYTE based array is a mess in itself. The great thing about this is that it goes through all the NICs located on your PC. You are also able to easily query other people's PCs by passing in a NetBIOS computer name as the first parameter to this function.

Method Three: Use GetAdaptersInfo

The cleanest way I could find to get all the MAC addresses located on a PC was to use the GetAdaptersInfo method. It includes almost as much information as IPCONFIG /ALL including your DHCP server, Gateway, IP address list, subnet mask and WINS servers. It also enumerates all the NICs on your PC and is supported in 95/98/Me/NT/2000/XP. Finally it also works if your NICs are not connected to valid networks (eg. wires are not even hooked up), but the NICs do have to be "enabled" in Windows.

// Fetches the MAC address and prints it
static void GetMACaddress(void)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information
// for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save memory size of buffer

DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to receive data
&dwBufLen); // [in] size of receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value is
// valid, no buffer overflow

PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; // Contains pointer to
// current adapter info
do {
PrintMACaddress(pAdapterInfo->Address); // Print MAC address
pAdapterInfo = pAdapterInfo->Next; // Progress through
// linked list
}
while(pAdapterInfo); // Terminate if last adapter
}

I probably should mention that statically allocating an array for up to 16 NICs is not the best way to do this. It is a quick and dirty solution that should show you essentially how to get and enumerate all the MAC addresses on your PC.

Method Four: The Miniport Driver

I never implemented this method since it required some very low level coding. It also probably wouldn't be a good method since you are directly talking to the underlying NDIS miniport driver. The basic concept of this is to hit the miniport driver with an OIS query of OID_802_3_CURRENT_ADDRESS. This should return a buffer with the current MAC address.

The way problem with this solution is that there is no easy way to do this from user mode, which is exactly the mode we are coding all our apps in, this is as opposed to kernal mode which drivers reside in.

Conclusion

Although there are no functions named GetMACaddress in the Win32 API, ATL, MFC, or C#, it is fairly easy to find and associate your MAC address with its related IP addresses by calling GetAdaptersInfo(). GetAdaptersInfo is located in the Platform SDK.

I hope you had an interesting time reading this article.

Reference: Get IP address in C#
Reference: Get IP address in C++

About the Author

Khalid Shaikh is a software engineer currently contracting for HP in Palo Alto. He has worked at such companies such as Microsoft, Nvidia, and several silicon valley startups. Khalid has been featured in PC Gamer for innovative driver development techniques and is currently co-founder of HTTP-Tunnel Corp.

If you have any questions, don't hestitate to e-mail me.