Class: Ip2locationIpTools

Inherits:
Object
  • Object
show all
Defined in:
lib/ip2location_ruby.rb

Instance Method Summary collapse

Instance Method Details

#cidr_to_ipv4(cidr) ⇒ Object



984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/ip2location_ruby.rb', line 984

def cidr_to_ipv4(cidr)
  if cidr.include? "/"
    cidr = IPAddr.new(cidr)
    arr_tmp = cidr.to_range.to_s.split('..')
    ip_arr = {}
    ip_arr['ip_start'] = arr_tmp[0]
    ip_arr['ip_end'] = arr_tmp[1]
    return ip_arr
  else
    return
  end
end

#cidr_to_ipv6(cidr) ⇒ Object



1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
# File 'lib/ip2location_ruby.rb', line 1055

def cidr_to_ipv6(cidr)
  if cidr.include? "/"
    ip_start = IPAddr.new(cidr).to_i.to_s(16).scan(/.{4}/)
    ip_start = ip_start[0] + ':' + ip_start[1] + ':' + ip_start[2] + ':' + ip_start[3] + ':' + ip_start[4] + ':' + ip_start[5] + ':' + ip_start[6] + ':' + ip_start[7]

    cidr_new = IPAddr.new(cidr)
    arr_tmp = cidr_new.to_range.to_s.split('..')
    ip_end = IPAddr.new(arr_tmp[1]).to_i.to_s(16).scan(/.{4}/)
    ip_end = ip_end[0] + ':' + ip_end[1] + ':' + ip_end[2] + ':' + ip_end[3] + ':' + ip_end[4] + ':' + ip_end[5] + ':' + ip_end[6] + ':' + ip_end[7]

    ip_arr = {}
    ip_arr['ip_start'] = ip_start
    ip_arr['ip_end'] = ip_end
    return ip_arr
  else
    return
  end
end

#compress_ipv6(ip) ⇒ Object



1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/ip2location_ruby.rb', line 1074

def compress_ipv6(ip)
  if !(IPAddr.new(ip) rescue nil).nil?
    ip = IPAddr.new(ip)
    if ip.ipv6?
      return ip
    else
      return
    end
  else
    return
  end
end

#decimal_to_ipv4(number) ⇒ Object



916
917
918
919
920
921
922
# File 'lib/ip2location_ruby.rb', line 916

def decimal_to_ipv4(number)
  if number.is_a? Numeric
    return IPAddr.new(number, Socket::AF_INET).to_s
  else
    return
  end
end

#decimal_to_ipv6(number) ⇒ Object



937
938
939
940
941
942
943
# File 'lib/ip2location_ruby.rb', line 937

def decimal_to_ipv6(number)
  if number.is_a? Numeric
    return IPAddr.new(number, Socket::AF_INET6).to_s
  else
    return
  end
end

#expand_ipv6(ip) ⇒ Object



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'lib/ip2location_ruby.rb', line 1087

def expand_ipv6(ip)
  if !(IPAddr.new(ip) rescue nil).nil?
    ip = IPAddr.new(ip)
    if ip.ipv6?
      res = ip.to_i.to_s(16).scan(/.{4}/)
      return res[0] + ':' + res[1] + ':' + res[2] + ':' + res[3] + ':' + res[4] + ':' + res[5] + ':' + res[6] + ':' + res[7]
    else
      return
    end
  else
    return
  end
end

#ipv4_to_cidr(ipfrom, ipto) ⇒ Object



945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
# File 'lib/ip2location_ruby.rb', line 945

def ipv4_to_cidr(ipfrom, ipto)
  if (!(IPAddr.new(ipfrom) rescue nil).nil?) and (!(IPAddr.new(ipto) rescue nil).nil?)
    ipfrom = IPAddr.new(ipfrom)
    ipto = IPAddr.new(ipto)
    if ipfrom.ipv4? and ipto.ipv4?
      ipfrom = ipfrom.to_i
      ipto = ipto.to_i
      result = []
      while ipto >= ipfrom do
        maxSize = 32
        while maxSize > 0 do
          mask = (2**32 - 2**(32-(maxSize - 1)))
          maskBase = ipfrom & mask
          if maskBase != ipfrom
            break
          end
            maxSize-=1
        end
        x = Math.log(ipto - ipfrom + 1)/Math.log(2)
        maxDiff = (32 - x.floor).floor

        if maxSize < maxDiff
          maxSize = maxDiff
        end

        ip = IPAddr.new(ipfrom, Socket::AF_INET).to_s
        cidr = [ip, maxSize].join('/')
        result.push cidr
        ipfrom += 2**(32-maxSize)
      end
      return result
    else
      return
    end
  else
    return
  end
end

#ipv4_to_decimal(ip) ⇒ Object



903
904
905
906
907
908
909
910
911
912
913
914
# File 'lib/ip2location_ruby.rb', line 903

def ipv4_to_decimal(ip)
  if !(IPAddr.new(ip) rescue nil).nil?
    ip = IPAddr.new(ip)
    if ip.ipv4?
      return ip.to_i
    else
      return
    end
  else
    return
  end
end

#ipv6_to_cidr(ipfrom_ori, ipto) ⇒ Object



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
# File 'lib/ip2location_ruby.rb', line 997

def ipv6_to_cidr(ipfrom_ori, ipto)
  if (!(IPAddr.new(ipfrom_ori) rescue nil).nil?) and (!(IPAddr.new(ipto) rescue nil).nil?)
    ipfrom = IPAddr.new(ipfrom_ori)
    ipto = IPAddr.new(ipto)
    if ipfrom.ipv6? and ipto.ipv6?
      ipfrom = '00' + ipfrom.to_i.to_s(2)
      ipto = '00' + ipto.to_i.to_s(2)
      result = []
      if ipfrom == ipto
        cidr = ipfrom_ori + '/128'
        result.push cidr
        return result
      end
      if ipfrom > ipto
        ipfrom, ipto = ipto, ipfrom
      end
      networks = {}
      network_size = 0
      while ipto > ipfrom do
        if ipfrom[-1, 1] == '1'
          networks[ipfrom[network_size, (128 - network_size)] + ('0' * network_size)] = 128 - network_size
          n = ipfrom.rindex('0')
          ipfrom = ((n == 0) ? '' : ipfrom[0, n]) + '1' + ('0' * (128 - n - 1))
        end

        if ipto[-1, 1] == '0'
          networks[ipto[network_size, (128 - network_size)] + ('0' * network_size)] = 128 - network_size
          n = ipto.rindex('1')
          ipto = ((n == 0) ? '' : ipto[0, n]) + '0' + ('1' * (128 - n - 1))
        end

        if ipfrom > ipto
          next
        end

        shift = 128 - [ipfrom.rindex('0'), ipto.rindex('1')].max
        ipfrom = ('0' * shift) + ipfrom[0, (128 - shift)]
        ipto = ('0' * shift) + ipto[0, (128 - shift)]
        network_size = network_size + shift

        if ipfrom == ipto
          networks[ipfrom[network_size, (128 - network_size)] + ('0' * network_size)] = 128 - network_size
          next
        end
      end

      networks.each do |ip, netmask|
        result.push IPAddr.new(ip.to_i(2), Socket::AF_INET6).to_s + '/' + netmask.to_s
      end
      return result
    else
      return
    end
  else
    return
  end
end

#ipv6_to_decimal(ip) ⇒ Object



924
925
926
927
928
929
930
931
932
933
934
935
# File 'lib/ip2location_ruby.rb', line 924

def ipv6_to_decimal(ip)
  if !(IPAddr.new(ip) rescue nil).nil?
    ip = IPAddr.new(ip)
    if ip.ipv6?
      return ip.to_i
    else
      return
    end
  else
    return
  end
end

#is_ipv4(ip) ⇒ Object



879
880
881
882
883
884
885
886
887
888
889
# File 'lib/ip2location_ruby.rb', line 879

def is_ipv4(ip)
  if !(IPAddr.new(ip) rescue nil).nil?
    if IPAddr.new(ip).ipv4?
      return true
    else
      return false
    end
  else
    return false
  end
end

#is_ipv6(ip) ⇒ Object



891
892
893
894
895
896
897
898
899
900
901
# File 'lib/ip2location_ruby.rb', line 891

def is_ipv6(ip)
  if !(IPAddr.new(ip) rescue nil).nil?
    if IPAddr.new(ip).ipv6?
      return true
    else
      return false
    end
  else
    return false
  end
end