Class: IPAddress::IpBits

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, vt_as_compressed_string, vt_as_uncompressed_string, bits, part_bits, dns_bits, rev_domain, part_mod, host_ofs) ⇒ IpBits

Returns a new instance of IpBits.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ipaddress/ip_bits.rb', line 16

def initialize(version,
               vt_as_compressed_string, vt_as_uncompressed_string,
               bits, part_bits, dns_bits, rev_domain, part_mod, host_ofs)
  @version = version
  @vt_as_compressed_string = vt_as_compressed_string
  @vt_as_uncompressed_string = vt_as_uncompressed_string
  @bits = bits
  @part_bits = part_bits
  @dns_bits = dns_bits
  @rev_domain = rev_domain
  @part_mod = part_mod
  @host_ofs = host_ofs
end

Instance Attribute Details

#bitsObject (readonly)

Returns the value of attribute bits.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def bits
  @bits
end

#dns_bitsObject (readonly)

Returns the value of attribute dns_bits.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def dns_bits
  @dns_bits
end

#host_ofsObject (readonly)

Returns the value of attribute host_ofs.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def host_ofs
  @host_ofs
end

#part_bitsObject (readonly)

Returns the value of attribute part_bits.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def part_bits
  @part_bits
end

#part_modObject (readonly)

Returns the value of attribute part_mod.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def part_mod
  @part_mod
end

#rev_domainObject (readonly)

Returns the value of attribute rev_domain.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def rev_domain
  @rev_domain
end

#versionObject (readonly)

Returns the value of attribute version.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def version
  @version
end

#vt_as_compressed_stringObject (readonly)

Returns the value of attribute vt_as_compressed_string.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def vt_as_compressed_string
  @vt_as_compressed_string
end

#vt_as_uncompressed_stringObject (readonly)

Returns the value of attribute vt_as_uncompressed_string.



12
13
14
# File 'lib/ipaddress/ip_bits.rb', line 12

def vt_as_uncompressed_string
  @vt_as_uncompressed_string
end

Class Method Details

.ipv4_as_compressed(ip_bits, host_address) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ipaddress/ip_bits.rb', line 93

def self.ipv4_as_compressed(ip_bits, host_address)
  ret = ""
  sep = ""
  ip_bits.parts(host_address).each do |part|
    ret += sep
    ret += "#{part}"
    sep = "."
  end

  return ret
end

.ipv6_as_compressed(ip_bits, host_address) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ipaddress/ip_bits.rb', line 105

def self.ipv6_as_compressed(ip_bits, host_address)
  ret = ""
  colon = ""
  done = false
  rles = Rle.code(ip_bits.parts(host_address))
  rles.each_with_index do |rle, idx|
    _ = 0
    while _ < rle.cnt
      if (done || !(rle.part == 0 && rle.max) || (rles.length == idx+1 && rle.cnt == 1 && rle.part == 0 && rle.max))
        #puts "-------#{rle.part.class.name}"
        ret += "#{colon}#{rle.part.to_s(16)}"
        colon = ":"
      elsif (rle.part == 0 && rle.max)
        ret += "::"
        colon = ""
        done = true
        break
      end

      _ = _ + 1
    end
  end

  return ret
end

.ipv6_as_uncompressed(ip_bits, host_address) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ipaddress/ip_bits.rb', line 131

def self.ipv6_as_uncompressed(ip_bits, host_address)
  ret = ""
  sep = ""
  ip_bits.parts(host_address).each do |part|
    ret += sep
    ret += (0x10000 + part).to_s(16)[1..-1]
    sep = ":"
  end

  return ret
end

.v4Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ipaddress/ip_bits.rb', line 65

def self.v4()
  @@V4 ||= IpBits.new(
    IpVersion::V4,
    -> (a,b) { IpBits.ipv4_as_compressed(a,b) },
    -> (a,b) { IpBits.ipv4_as_compressed(a,b) },
    32,
    8,
    8,
    "in-addr.arpa",
    1 << 8,
    Crunchy.one()
  )
end

.v6Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ipaddress/ip_bits.rb', line 79

def self.v6()
  @@V6 ||= IpBits.new(
    IpVersion::V6,
    -> (a,b) { IpBits.ipv6_as_compressed(a,b) },
    -> (a,b) { IpBits.ipv6_as_uncompressed(a,b) },
    128,
    16,
    4,
    "ip6.arpa",
    1 << 16,
    Crunchy.zero()
  )
end

Instance Method Details

#as_compressed_string(bu) ⇒ Object



48
49
50
# File 'lib/ipaddress/ip_bits.rb', line 48

def as_compressed_string(bu)
  return self.vt_as_compressed_string.call(self, bu)
end

#as_uncompressed_string(bu) ⇒ Object



52
53
54
# File 'lib/ipaddress/ip_bits.rb', line 52

def as_uncompressed_string(bu)
  return self.vt_as_uncompressed_string.call(self, bu)
end

#cloneObject



30
31
32
# File 'lib/ipaddress/ip_bits.rb', line 30

def clone
  return self
end

#dns_part_format(i) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/ipaddress/ip_bits.rb', line 56

def dns_part_format(i)
  case (self.version)
  when IpVersion::V4
    return "#{i}"
  when IpVersion::V6
    return "#{i.to_s(16)}"
  end
end

#parts(bu) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ipaddress/ip_bits.rb', line 34

def parts(bu)
  vec = []
  my = bu.clone
  part_mod = Crunchy.one().shl(self.part_bits)
  i = 0
  while i < (self.bits / self.part_bits)
    vec.push(my.mod(part_mod).num)
    my = my.shr(self.part_bits)
    i = i + 1
  end

  return vec.reverse()
end