Autor Beitrag
elimhren
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21


D6 Prof
BeitragVerfasst: So 21.08.05 13:24 
Hallo!
Gibt es in Delphi eine Funktion die mir meine IP auf Validität überprüft oder muss ich sie mir selber schreiben? :)

Im zweiteren Falle würde ich den String bei den Punkten in 4 Teile zerlegen und jeden Teil auf einen Integerwert im Bereich von 0 - 255 überprüfen - sollte passen oder?

lg,
johannes
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: So 21.08.05 13:28 
user profile iconelimhren hat folgendes geschrieben:
Gibt es in Delphi eine Funktion die mir meine IP auf Validität überprüft oder muss ich sie mir selber schreiben? :)

Ich kenn' keine. ;)

user profile iconelimhren hat folgendes geschrieben:
Im zweiteren Falle würde ich den String bei den Punkten in 4 Teile zerlegen und jeden Teil auf einen Integerwert im Bereich von 0 - 255 überprüfen - sollte passen oder?

Kommt drauf an, welche IP-Version du meinst. ;)

Ich nehm' mal an diese Forum da: xxx.xxx.xxx.xxx. Dann geht das so, wie du's grad gesagt hast. ;)
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 21.08.05 13:39 
Moin!

Du kannst folgendes versuchen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
uses
  ..., Winsock;

if (inet_addr(PChar(Edit1.Text)) <> -1then
  ShowMessage('OK')
else
  ShowMessage('Fehler!');


cu
Narses
DaRkFiRe
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 526

WinXP Home & Professional
C, C++, Delphi
BeitragVerfasst: So 21.08.05 14:05 
Lieber selber schreiben, dann brauchst Du nicht Winsock einzubinden.

_________________
Lang ist der Weg durch Lehren - kurz und wirksam durch Beispiele! Seneca
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: So 21.08.05 14:07 
Moin!

Die Wahrscheinlichkeit, dass du Winsock sowieso schon drin hast, wenn du mit IPs arbeitest, ist mal ziemlich groß. Und dann brauch ich nicht das Rad nochmal erfinden, zusätzlichen Code erzeugen... :wink:

cu
Narses
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 21.08.05 14:40 
user profile iconDaRkFiRe hat folgendes geschrieben:
Lieber selber schreiben, dann brauchst Du nicht Winsock einzubinden.

:roll: Diese Unit macht das Programm nur unwesentlich größer.
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: So 21.08.05 15:09 
Das einzige, was man vom Schreiben der Funktion (da es sie ja jetzt doch gibt) hätte, wär' der Lerneffekt. ;)
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: So 21.08.05 22:26 
bei den schweizern gibbet da was !

_________________
In the beginning was the word.
And the word was content-type: text/plain.
elimhren Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21


D6 Prof
BeitragVerfasst: Mo 22.08.05 12:33 
fyi - habs nun selber getippselt:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
function validIP(Host: String): Boolean;
var arrIP   : Array of String[3];
    bResult : Boolean;
        i   : Integer;

begin
  bResult := True;
  i       := 0;
  while Pos('.',Host) > 0 do
  begin
    SetLength(arrIP, High(arrIP) + 2);
    arrIP[i] := Copy(Host,0,Pos('.',Host)-1);
    Host    := Copy(Host,Pos('.',Host)+1, Length(Host));
    if Pos('.',Host) = 0 then
    begin
      SetLength(arrIP, High(arrIP) + 2);
      arrIP[i+1] := Host;
      Break;
    end;
    inc(i);
  end;
  if High(arrIP) <> 3 then bResult := False;
  for i:=low(arrIP) to high(arrIP) do if not ((StrToInt(arrIP[i]) >= 0and (StrToInt(arrIP[i]) <= 255)) then bResult := False;
  Result := bResult;
end;
matze.de
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 576

Win95, Win98 SE, WinXp Prof SP2
D7P, D8P, FPC2.0
BeitragVerfasst: Mo 22.08.05 12:50 
Hier noch eine Funktion:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
Function IsWrongIP(Ip: String): Boolean;
Const
    Z = ['0'..'9''.'];
Var
    I, J, P: Integer;
    W: String;
Begin
    Result := False;
    If (Length(Ip) > 15Or (Ip[1] = '.'Then
        Exit;
    I := 1;
    J := 0;
    P := 0;
    W := '';
    Repeat
        If (Ip[I] In Z) And (J < 4Then
        Begin
            If Ip[I] = '.' Then
            Begin
                Inc(P);
                J := 0;
                Try
                    StrToInt(Ip[I + 1]);
                Except
                    Exit;
                End;
                W := '';
            End
            Else
            Begin
                W := W + Ip[I];
                If (StrToInt(W) > 255Or (Length(W) > 3Then
                    Exit;
                Inc(J);
            End;
        End
        Else
            Exit;
        Inc(I);
    Until I > Length(Ip);
    If P < 3 Then
        Exit;
    Result := True;
End;


mfg matze

_________________
si tacuisses, philosophus mansisses.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 22.08.05 13:04 
Moin!

Hey, das wird ein Wettbewerb :arrow: nach Optimierung damit... :wink:

Meine Version, nicht unbedingt effizient, aber elegant:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
function CheckIP4(IP: String): Boolean;
  var
    SL: TStringList;
    i: Integer;
begin
  Result := FALSE;
  try
    SL := TStringList.Create;
    SL.Delimiter := '.';
    SL.DelimitedText := IP;
    if (SL.Count = 4then begin
      Result := TRUE;
      for i := 0 to 3 do
        Result := Result and (StrToIntDef(SL.Strings[i],256in [0..255]);
    end;
  finally
    SL.Free;
  end;
end;


cu
Narses
WeBsPaCe
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 2322
Erhaltene Danke: 1

FireFox 3, Internet Explorer 6 SP1
D1, D3Prof, D6Pers, D7Pers+Indy, VisualStudio Express
BeitragVerfasst: Mo 22.08.05 13:09 
Ich hab auch was... :D

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
function IsIpAdr(Adr: String): Boolean;
var
i: Integer; // Schleifenzahl
j: Integer; // Anzahl der Punkte
s: Array [1..4of String// Einzelne Oktale (wenn die so heißen :mrgreen:)
begin
Result := False;
j := 0;
for i := 1 to Length(Adr) do
  if Adr[i] = '.' then
    j := j + 1
  else
    try
      s[j+1] := s[j+1] + Adr[i];
    except
      Exit;
    end;
for i := 1 to 4 do
  begin
  if s[i] = '' then
    Exit;
  try
    if (StrToInt(s[i]) < 0or (StrToInt(s[i]) > 255then
      Exit;
  except
    Exit;
  end;
  end;
Result := True;
end;


Verkürzt so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
function IsIpAdr(Adr: String): Boolean;
var
i, j: Integer;
s: Array [1..4of String;
begin
Result := False;
j := 0;
for i := 1 to Length(Adr) do if Adr[i] = '.' then j := j + 1 else try s[j+1] := s[j+1] + Adr[i];
except Exit;
end;
for i := 1 to 4 do begin if s[i] = '' then Exit;
try if (StrToInt(s[i]) < 0or (StrToInt(s[i]) > 255then Exit;
except Exit;
end;
end;
Result := True;
end;


:rofl:
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Di 30.08.05 09:04 
die gehen aber alle nur mit IPv4 dadressen und nicht mit IPv6 !

_________________
In the beginning was the word.
And the word was content-type: text/plain.
tomp
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Di 30.08.05 09:20 
Schließe ich mich an. IP v6 verursacht sogar massive probleme bei der art von validierung. auch sollte man die ausschluesse nach rfc, die nicht geroutet werden, beachten.......


gruss, thomas
matze.de
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 576

Win95, Win98 SE, WinXp Prof SP2
D7P, D8P, FPC2.0
BeitragVerfasst: Di 30.08.05 09:52 
IPv4 kommt nunmal ja auch öfter vor und er meinte diese version sicher auch. ipv6 ist ja ganz anders aufgebaut...

mfg matze

_________________
si tacuisses, philosophus mansisses.