Ver Mensaje Individual
  #4  
Antiguo 26-04-2013
paquechu paquechu is offline
Miembro
 
Registrado: oct 2008
Posts: 51
Reputación: 16
paquechu Va por buen camino
Bueno, pues voy por aqui, pero no me funciona...

Al llamar a la funcion

Código Delphi [-]
ip_pertenece_a_red('132.18.3.100', '255.255.0.0/16')

Siempre devuelve falso

Por otro lado, la verdad es que no se si el codigo PHP funciona correctamente......

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;

Última edición por paquechu fecha: 26-04-2013 a las 23:44:38. Razón: No se si el codigo PHP funciona bien
Responder Con Cita