[fpc-pascal] Test for valid ip address

Koenraad Lelong fpascal1 at de-brouwerij.be
Thu Jul 28 10:14:10 CEST 2016


Hi,

I need a way to test if an string containing an ipv4-address is really 
an ipv4-address.
I tried
tmpAddress:=HostAddrToStr(StrToHostAddr(IPAddressStr));
writeln(tmpAddress);
  if (tmpAddress='0.0.0.0') then
   begin
    writeln('Error in IP-address');
    IPAddressStr:=tmpAddress;
   end;
When I enter 192.168.185.297 (i.e. not a valid ipv4 address) in 
IPAddressStr I get
192.168.185.41
not the expected error-message.

According to the rtl-manual :

function StrToHostAddr(IP: AnsiString) : in_addr
Description: StrToHostAddr converts the string representation in IP to a 
host address and returns the host
address.
Errors: On error, the host address is filled with zeroes.

I would think that converting those zeroes to a host-address would yield 
0.0.0.0.

Am I missing something ?
Is there a better way, without using some other network-library ?

Koenraad.



More information about the fpc-pascal mailing list