Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2023-01

Function to find if IP address is local

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ondrej Pokorny

Hello!

Is there a routine in RTL to find out if a given IPv4 address is from the private address range (10.* etc)?

Thanks
Ondrej

Reply by email

Michael Van Canneyt
to Ondrej Pokorny
Ondrej Pokorny wrote
Hello! 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.

Michael.

Reply by email

Ondrej Pokorny
to Michael Van Canneyt
Michael Van Canneyt wrote 5 lines
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.

Thank you!
Ondrej

Reply by email

Michael Van Canneyt
to Ondrej Pokorny
Ondrej Pokorny wrote 26 lines
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.

Reply by email