3

I want to list all the MAC addresses that are connected to my router i know it's possible because i have seen it done.

I think all applications use WinPcap for this purpose is there a way i can interface it with my delphi application?

pguetschow
  • 5,176
  • 6
  • 32
  • 45
opc0de
  • 11,557
  • 14
  • 94
  • 187
  • ARP is probably the way to go, see this : [delphi-get-mac-of-router](http://stackoverflow.com/questions/4550672/delphi-get-mac-of-router). – LU RD Dec 14 '11 at 21:09
  • WinPCap is for logging/sniffing and packet capture on your network, not enumerating the IPs and Macs only. – Warren P Dec 14 '11 at 21:36
  • Your probably right it was just an assumption – opc0de Dec 14 '11 at 21:38

2 Answers2

9

There are a couple of ways you can do this. The first is to connect to the router via SNMP and read the atTable (1.3.6.1.2.1.3.1). This will give you a list of IP addresses matched to MAC addresses. You can use the SNMP functionality in Synapse to read the table. To connect to a router that's running SNMPv1 or SNMPv2c you will need the correct read community string. For SNMPv3 you will need the correct authentication details.

Another method is to use ARP. To send an ARP request, you can use the iphlpapi dll. Here's some code that should get you started.

unit MyARP

interface

uses
  Windows, Classes, SysUtils, WinSock;

function SendARP(DestIp: DWORD; srcIP: DWORD; pMacAddr: pointer; PhyAddrLen: Pointer): DWORD;stdcall; external 'iphlpapi.dll';
function MySendARP(const IPAddress: String): String;

implementation

function MySendARP(const IPAddress: String): String;
var
  DestIP: ULONG;
  MacAddr: Array [0..5] of Byte;
  MacAddrLen: ULONG;
  SendArpResult: Cardinal;
begin
  DestIP := inet_addr(PAnsiChar(AnsiString(IPAddress)));
  MacAddrLen := Length(MacAddr);
  SendArpResult := SendARP(DestIP, 0, @MacAddr, @MacAddrLen);

  if SendArpResult = NO_ERROR then
    Result := Format('%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X',
                     [MacAddr[0], MacAddr[1], MacAddr[2],
                      MacAddr[3], MacAddr[4], MacAddr[5]])
  else
    Result := '';
end;

end.

This method will only work on your local subnet.

norgepaul
  • 6,013
  • 4
  • 43
  • 76
  • If i am wirless connected to the network can i use an ARP Request ? – opc0de Dec 14 '11 at 21:35
  • Yes, as long as the device with the MAC address you want to discover is located on the same subnet. e.g. 10.0.0.0/255.255.255.0 contains the IP addresses from 10.0.0.1 to 10.0.0.254 (10.0.0.0 is not often used and 10.0.0.255 is the broadcast address) – norgepaul Dec 14 '11 at 21:52
  • thanks useful function to get gateway mac address using ip – jmp Feb 22 '16 at 21:41
0

You have to query the rouer itself, typically with either SNMP or uPNP, assuming the router supports such a query to begin with.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770