[fpc-pascal] Test for valid ip address
Christo
christo.crause at gmail.com
Thu Jul 28 10:38:59 CEST 2016
On Thu, 2016-07-28 at 10:14 +0200, Koenraad Lelong wrote:
> 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 ?
The in_addr IP address type is a packed record of byte, so if an IP
address part larger than 255 is encountered in a string it will be
truncated when copied to the byte record using StrToHostAddr. This
probably means you have to use some other means of detecting an invalid
address, or add a check for (tmpAddress <> IPAddressStr) to cater for
your situation.
More information about the fpc-pascal
mailing list