Sign in Join

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

Mailing lists / fpc-pascal / 2016-07

Test for valid ip address

7 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

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

Koenraad Lelong

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.

Reply by email

Jonas Maebe
to Koenraad Lelong
Koenraad Lelong wrote 14 lines
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.

You can add a multiple of 256 to any octet of an IPv4 address in most programs. Try it in your browser, it will probably also work.

Jonas

Reply by email

Rainer Stratmann
to Koenraad Lelong: I need a way to test if an string containing an ipv4-address is really an ipv4-address. I…

The IP-Adress is a dword, but you can change, that it fits for you.

function str_getval( str : shortstring ) : longint;
var cod : longint;
begin
 val( str , result , cod );
end;
function ip_from_string( ipstr : shortstring ; out error : boolean ) : dword;
var
 c : char;
 ipn , pointanz : longint;
 x : longint;
 pstr : shortstring;
 ip : dword;
 procedure add_ip;
 begin
  ipn := str_getval( pstr );
  if ( ipn > 255 ) or ( pstr = '' ) then error := true;
  ip := ip shl 8;
  ip := ip or ipn;
  pstr := '';
 end;
begin
 ip := 0;
 pstr := '';
 error := false;
 pointanz := 0;
 for x := 1 to length( ipstr ) do begin
  c := ipstr[ x ];
  case c of
   '0'..'9' : pstr := pstr + c;
   '.' : begin
    inc( pointanz );
    add_ip;
   end;
   else error := true;
  end;
 end;
 add_ip;
 if pointanz  3 then error := true;
 if error then result := 0
          else result := ip;
end;
Koenraad Lelong wrote 35 lines
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. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Mattias Gaertner
to Jonas Maebe: You can add a multiple of 256 to any octet of an IPv4 address in most programs. Try it in…
Show quoted text 18 lines
On 28/07/16 10:14, Koenraad Lelong wrote: > 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. You can add a multiple of 256 to any octet of an IPv4 address in most programs. Try it in your browser, it will probably also work.

What browser supports that?

Mattias

Reply by email

Christo
to Koenraad Lelong: I need a way to test if an string containing an ipv4-address is really an ipv4-address. I…
Koenraad Lelong wrote 18 lines
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.

Reply by email

Rainer Stratmann
to Jonas Maebe: You can add a multiple of 256 to any octet of an IPv4 address in most programs. Try it in…
Jonas Maebe wrote 23 lines
On 28/07/16 10:14, Koenraad Lelong wrote: > 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. You can add a multiple of 256 to any octet of an IPv4 address in most programs. Try it in your browser, it will probably also work.

In Firefox 45.2.0 on Linux it does not work.
It is confusing anyway.

Show quoted text 5 lines
Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Koenraad Lelong
to Christo: The in_addr IP address type is a packed record of byte, so if an IP address part larger…

Op 28-07-16 om 10:38 schreef Christo:

Show quoted text 6 lines
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.

Thanks,

I used the check :
(tmpAddress IPAddressStr)
in stead of
(tmpAddress='0.0.0.0')

Koenraad

Reply by email