[fpc-devel] Function to find if IP address is local

Michael Van Canneyt michael at freepascal.org
Mon Jan 9 09:38:27 CET 2023



On Mon, 9 Jan 2023, Ondrej Pokorny wrote:

> On 09.01.2023 09:08, Michael Van Canneyt wrote:
>> On Mon, 9 Jan 2023, Ondrej Pokorny via fpc-devel wrote:
>>> Is there a routine in RTL to find out if a given IPv4 address is from the 
>>> private address range (10.* etc)?
>> 
>> No, not to my knowledge. Feel free to add it somewhere in fcl-net.
>
> Here it is:
>
> function NetAddrIsPrivate(const IP: in_addr): Boolean;
> begin
>   Result :=
>     // 10.0.0.0 – 10.255.255.255
>         (IP.s_bytes[1]=10)
>     // 172.16.0.0 – 172.31.255.255
>     or ((IP.s_bytes[1]=172) and (IP.s_bytes[2]>=16) and (IP.s_bytes[2]<=31))
>     // 192.168.0.0 – 192.168.255.255
>     or ((IP.s_bytes[1]=192) and (IP.s_bytes[2]=168));
> end;
>
> I didn't find a good unit in fcl-net to put it in.
>
> IMO it would be better suited in sockets.pp in rtl-extra where in_addr is 
> declared.
>
> Feel free to add it there or in fcl-net, if you know a good place.

I had a look, seems you are right. I would have thought netdb, but decided
it doesn't quite fit.

I added it to the sockets unit.

Thanks !

Michael.


More information about the fpc-devel mailing list