I'm trying to use the dhspsapi function DHCPEnumSubnets, to list all the available subnets on an office DHCP server. For security sake, all IP addresses have been modified from the actual ones used in our corporate environment, to addresses in the 192.168.0.1/24 network. Since I'm writing in Borland Delphi 7, the library I used for this task was Delphi JEDI, specifically the JwaDhcpsApi unit. In JWADhcpApi.pas, DHCPEnumSubnets is declared like this:
type
DHCP_RESUME_HANDLE = DWORD;
_DHCP_IP_ARRAY = record
NumElements: DWORD;
Elements: LPDHCP_IP_ADDRESS; //array
end;
{$EXTERNALSYM _DHCP_IP_ARRAY}
DHCP_IP_ARRAY = _DHCP_IP_ARRAY;
{$EXTERNALSYM DHCP_IP_ARRAY}
LPDHCP_IP_ARRAY = ^DHCP_IP_ARRAY;
{$EXTERNALSYM LPDHCP_IP_ARRAY}
LPPDHCP_IP_ARRAY = ^LPDHCP_IP_ARRAY;
...
function DhcpEnumSubnets(ServerIpAddress: PWideChar; var ResumeHandle: DHCP_RESUME_HANDLE; PreferredMaximum: DWORD; EnumInfo: LPDHCP_IP_ARRAY; out ElementsRead, ElementsTotal: DWORD): DWORD; stdcall;
So EnumInfo holds a pointer to a DHCP_IP_ARRAY structure, which is a record containing a field holding the total number of subnets and another field containing a pointer to an array of DWORD. According to MSDN, the subnet IDs are DWORD values holding the bits of the subnet IP address in binary form.
This is the Delphi code I use for reading subnets from the DHCP server. There is only one subnet on that particular server, so no need to loop DHCPEnumSubnets and wait for ERROR_NO_MORE_ITEMS.
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, JwaDhcpsApi, StdCtrls, Math;
type
TfmMain = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
function IpAddressToId(const Address: string): Cardinal;
function IdToIpAddress(const Id: Cardinal):string;
procedure Trace(const S: string);
public
procedure PrintMemory(const P: Pointer; const Text: string; const Bytes: integer);
function FindIp(const Value: string; const Address: Pointer; Length: integer): Cardinal;
end;
var
fmMain: TfmMain;
implementation
{$R *.dfm}
procedure TfmMain.Button1Click(Sender: TObject);
var
ServerIpAddress: WideString;
PServerIPAddress: PWidechar;
ResumeHandle: DHCP_RESUME_HANDLE;
PreferredMaximum: Cardinal;
ElementsRead: Cardinal;
ElementsTotal: Cardinal;
Error: Cardinal;
i: Cardinal;
ElementPointer: LPDHCP_IP_ADDRESS;
subnet: DWORD;
EnumInfo: LPDHCP_IP_ARRAY;
NumElements: DWORD;
ArrayAddress: Cardinal;
begin
ServerIPAddress:='192.168.0.238';
PServerIPAddress:=PWideChar(ServerIPAddress);
PreferredMaximum:=65536;
GetMem(EnumInfo,SizeOf(Pointer));
GetMem(EnumInfo^.Elements,10000);
ElementsRead:=0;
ElementsTotal:=0;
ResumeHandle:=0;
try
try
Error:=DhcpEnumSubnets(PServerIPAddress,ResumeHandle,PreferredMaximum,EnumInfo,ElementsRead,ElementsTotal);
if Error <> ERROR_SUCCESS then
begin
Trace('Error reading subnets from DHCP server');
Exit;
end;
//NumElements:=EnumInfo^.NumElements; DOES NOT WORK !!!
NumElements:=PDWORD(EnumInfo^.NumElements)^;
Trace('NumElements = ' + IntToStr(NumElements));
Trace('DhcpEnumSubnets returned ' + IntToStr(Error));
Trace('ElementsRead = ' + IntToStr(ElementsRead));
ArrayAddress:=Cardinal(EnumInfo^.Elements);
for i:=0 to NumElements - 1 do begin
New(ElementPointer);
ElementPointer:=LPDHCP_IP_ADDRESS(ArrayAddress + i*SizeOf(Pointer));
subnet:=ElementPointer^;
Trace('subnet['+IntToStr(i)+'] = ' + IntToStr(subnet) + ' ; IP = ' + IdToIpAddress(subnet));
end;
finally
DHCPRpcFreeMemory(EnumInfo);
end;
end;
function TfmMain.FindIp(const Value: string;
const Address: Pointer; Length: integer): Cardinal;
var
i: Cardinal;
subnet: Cardinal;
begin
Result:=0;
if (Value = '') or (Address = nil) or (Length <=0) then Exit;
subnet:=IpAddressToId(Value);
for i:=0 to Length - 1 do
begin
Trace(IntToStr(i) + ' : ' + IdToIpAddress(PDWORD(Cardinal(Address) + i*SizeOf(Pointer))^));
if PDWORD(Cardinal(Address) + i)^ = subnet then
begin
Result:=Cardinal(Address) + i;
Break;
end;
end;
end;
function TfmMain.IdToIpAddress(const Id: Cardinal): string;
var
b1,b2,b3,b4: Cardinal;
begin
Result:='';
if Id > Power(2,32) then Exit;
b1:=Id and 255;
b2:=(Id and 65280) shr 8;
b3:=(Id and 16711680) shr 16;
b4:=(Id and 4278190080) shr 24;
Result:=IntToStr(b4) + '.' + IntToStr(b3) + '.' + IntToStr(b2) + '.' + IntToStr(b1);
end;
function TfmMain.IpAddressToId(const Address: string): Cardinal;
var
S: string;
b1,b2,b3,b4: integer;
p: integer;
begin
Result:=0;
if Address = '' then Exit;
S:=Address;
try
p:=Pos('.',S);
b1:=StrToInt(Copy(S,1,P-1));
Delete(S,1,P);
p:=Pos('.',S);
b2:=StrToInt(Copy(S,1,P-1));
Delete(S,1,P);
p:=Pos('.',S);
b3:=StrToInt(Copy(S,1,P-1));
Delete(S,1,P);
b4:=StrToInt(S);
Result:= 16777216*b1 + 65536*b2 + 256*b3 + b4;
except
end;
end;
procedure TfmMain.PrintMemory(const P: Pointer; const Text: string; const Bytes: integer);
var
i: integer;
address: integer;
L: integer;
S: string;
begin
if P = nil then Exit;
L:=Length(Text);
for i:=0 to Bytes - 1 do
begin
address:=Integer(P) + i;
S:=PWideChar(address);
if Text = '' then Trace(IntToStr(i) + ' : ' + S)
else if Copy(S,1,L) = Text then Trace(IntToStr(i) + ' : ' + S);
end;
end;
procedure TfmMain.Trace(const S: string);
begin
Memo1.Lines.Add(S);
end;
end.
The code compiles and runs, but it does not return the correct values from DHCP.
- According to MSDN, EnumSubnets.NumElements should contain the number of subnets enumerated by DHCPEnumSubnets. In fact it contains some huge number, while the actual number of subnets is 1. That number was suspiciously resembling a memory address, so I decided to read it as a pointer to DWORD. And that actually gave the correct value: 1.
- DHCP subnet is 192.168.0.192. The subnet Id read from EnumInfo.Elements was 4535816 which translates to an IP "address" of 0.69.54.8
I wrote the function SearchIP, and tried reading the memory allocated to EnumInfo directly and search for a DWORD value equivalent to 192.168.0.192, but no luck.
The PC I'm running the program on is Windows 10. I also tried running it on the server itself, with the same result.
Please give me some advice on how to approach this problem. Could this be an issue with JEDI API in a 64-bit environment?
UPDATE: I have imported the functions directly from dhcpsapi.dll, with the same result. So this has nothing to do with Delphi JEDI, it's either an issue with the Delphi compiler, or with the dll itself.