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



918
919
920
921
922
923
924
925
926
927
928
929
# File 'lib/ip2location_ruby.rb', line 918

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



989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
# File 'lib/ip2location_ruby.rb', line 989

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



1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/ip2location_ruby.rb', line 1008

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



850
851
852
853
854
855
856
# File 'lib/ip2location_ruby.rb', line 850

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



871
872
873
874
875
876
877
# File 'lib/ip2location_ruby.rb', line 871

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



1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'lib/ip2location_ruby.rb', line 1021

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



879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'lib/ip2location_ruby.rb', line 879

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



837
838
839
840
841
842
843
844
845
846
847
848
# File 'lib/ip2location_ruby.rb', line 837

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



931
932
933
934
935
936
937
938
939
940
941
942
943
944
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
983
984
985
986
987
# File 'lib/ip2location_ruby.rb', line 931

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



858
859
860
861
862
863
864
865
866
867
868
869
# File 'lib/ip2location_ruby.rb', line 858

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



813
814
815
816
817
818
819
820
821
822
823
# File 'lib/ip2location_ruby.rb', line 813

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



825
826
827
828
829
830
831
832
833
834
835
# File 'lib/ip2location_ruby.rb', line 825

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