Ver Mensaje Individual
  #3  
Antiguo 27-04-2013
paquechu paquechu is offline
Miembro
 
Registrado: oct 2008
Posts: 51
Reputación: 16
paquechu Va por buen camino
Hola Casimiro.
Pues aqui lo pongo, es una adaptación que he hecho de una funcion en PHP que se supone que debe funcionar pero que me da la impresión que no.
Un saludo.

Código Delphi [-]
function ip_pertenece_a_red(str_ip, str_rango:string):bool;
var
  str_red, str_mascara: String;
  mascara, inf, sup, red, ip: LongWord;
  p: integer;
begin
  // Extraer la red y mascara
  p:=pos('/',str_rango);
  if p=0 then
  begin
    str_red:=str_rango;
    str_mascara:='';
  end else if p>=1 then
  begin
    str_red:=copy(str_rango,1,p-1);
    str_mascara:=copy(str_rango,p+1);
  end;

  if str_mascara='' then
     mascara:=$FFFFFFFF
  else if pos('.',str_mascara)=0 then
     mascara := $FFFFFFFF shl (32 - StrToInt(str_mascara))
  else
     mascara := ip2long(str_mascara);

  ip := ip2long(str_ip);
  red := ip2long(str_red);
  inf := red and mascara;
  sup := red or ( (not mascara) and $FFFFFFFF);

  Result := (ip>=inf) and (ip<=sup);
end;


function ip2Long(IP : string) : LongWord;
var
  IPaddr   : array[1..4] of integer;
  temp     : string;
  res      : Longword;
  i        : integer;

begin
  temp := ip;
  temp := temp + '.';
  for i := 1 to 4 do
  begin
    ipaddr[i] := strtoint(copy(temp,1,pos('.',temp) - 1));
    delete(temp,1,pos('.',temp));
  end;
  // Check the IP
  for i := 1 to 4 do
  begin
    try
       inttostr(ipaddr[i]);
    except
       result := 0; //'Invalid IP address.';
       exit;
    end;
  end;

  res := (ipaddr[1] * $FFFFFF) + ipaddr[1] +
           (ipaddr[2] * $FFFF)   + ipaddr[2] +
           (ipaddr[3] * $FF)     + ipaddr[3] +
           (ipaddr[4]);
           //result := IntToStr(res);
           Result:=res;
end;
Responder Con Cita