summaryrefslogtreecommitdiff
path: root/lib/ipaddress/prefix.rb
blob: 136f2d3457ca0cd77f369109ea76cd14a97c759d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
module IPAddress
  
  #
  # =NAME
  #   
  # IPAddress::Prefix
  #
  # =SYNOPSIS
  #  
  # Parent class for Prefix32 and Prefix128
  #
  # =DESCRIPTION
  #
  # IPAddress::Prefix is the parent class for IPAddress::Prefix32 
  # and IPAddress::Prefix128, defining some modules in common for
  # both the subclasses.
  #
  # IPAddress::Prefix shouldn't be accesses directly, unless
  # for particular needs.
  #
  class Prefix 

    include Comparable

    attr_reader :prefix

    #
    # Creates a new general prefix
    #
    def initialize(num)
      @prefix = num.to_i
    end

    #
    # Returns a string with the prefix 
    #
    def to_s
      "#@prefix"
    end
    alias_method :inspect, :to_s

    # 
    # Returns the prefix
    #
    def to_i
      @prefix
    end

    # 
    # Compare the prefix
    #
    def <=>(oth)
      @prefix <=> oth.to_i
    end

    #
    # Sums two prefixes or a prefix to a 
    # number, returns a Integer
    #
    def +(oth)
      if oth.is_a? Integer
        self.prefix + oth
      else
        self.prefix + oth.prefix
      end
    end

    #
    # Returns the difference between two
    # prefixes, or a prefix and a number,
    # as a Integer
    #
    def -(oth)
      if oth.is_a? Integer
        self.prefix - oth
      else
        (self.prefix - oth.prefix).abs
      end
    end
    
   end # class Prefix


  class Prefix32 < Prefix

    IN4MASK = 0xffffffff
    
    #
    # Creates a new prefix object for 32 bits IPv4 addresses
    #
    #   prefix = IPAddress::Prefix32.new 24
    #     #=> 24
    #
    def initialize(num)
      unless (0..32).include? num
        raise ArgumentError, "Prefix must be in range 0..32, got: #{num}"
      end
      super(num)
    end

    #
    # Returns the length of the host portion
    # of a netmask. 
    #
    #   prefix = Prefix32.new 24
    #
    #   prefix.host_prefix
    #     #=> 8
    #
    def host_prefix
      32 - @prefix
    end
    
    #
    # Transforms the prefix into a string of bits
    # representing the netmask
    #
    #   prefix = IPAddress::Prefix32.new 24
    # 
    #   prefix.bits 
    #     #=> "11111111111111111111111100000000"
    #
    def bits
      "%.32b" % to_u32
    end

    #
    # Gives the prefix in IPv4 dotted decimal format, 
    # i.e. the canonical netmask we're all used to
    #
    #   prefix = IPAddress::Prefix32.new 24
    #
    #   prefix.to_ip
    #     #=> "255.255.255.0"
    #
    def to_ip
      [bits].pack("B*").unpack("CCCC").join(".")
    end

    #
    # An array of octets of the IPv4 dotted decimal 
    # format 
    #
    #   prefix = IPAddress::Prefix32.new 24
    #
    #   prefix.octets
    #     #=> [255, 255, 255, 0]
    #
    def octets
      to_ip.split(".").map{|i| i.to_i}
    end

    #
    # Unsigned 32 bits decimal number representing
    # the prefix
    #
    #   prefix = IPAddress::Prefix32.new 24
    #
    #   prefix.to_u32
    #     #=> 4294967040
    #
    def to_u32
      (IN4MASK >> host_prefix) << host_prefix
    end
    
    #
    # Shortcut for the octecs in the dotted decimal 
    # representation
    #
    #   prefix = IPAddress::Prefix32.new 24
    #
    #   prefix[2]
    #     #=> 255
    #
    def [](index)
      octets[index]
    end

    #
    # The hostmask is the contrary of the subnet mask,
    # as it shows the bits that can change within the
    # hosts
    #
    #   prefix = IPAddress::Prefix32.new 24
    #
    #   prefix.hostmask
    #     #=> "0.0.0.255"
    #
    def hostmask
      [~to_u32].pack("N").unpack("CCCC").join(".")
    end
    
    #
    # Creates a new prefix by parsing a netmask in 
    # dotted decimal form
    #
    #   prefix = IPAddress::Prefix32::parse_netmask "255.255.255.0"
    #     #=> 24
    #
    def self.parse_netmask(netmask)
      octets = netmask.split(".").map{|i| i.to_i}
      num = octets.pack("C"*octets.size).unpack("B*").first.count "1"
      return self.new(num)
    end
    
  end # class Prefix32 < Prefix

  class Prefix128 < Prefix

    #
    # Creates a new prefix object for 128 bits IPv6 addresses
    #
    #   prefix = IPAddress::Prefix128.new 64
    #     #=> 64
    #
    def initialize(num=128)
      unless (0..128).include? num.to_i
        raise ArgumentError, "Prefix must be in range 0..128, got: #{num}"
      end
      super(num.to_i)
    end

    #
    # Transforms the prefix into a string of bits
    # representing the netmask
    #
    #   prefix = IPAddress::Prefix128.new 64
    #
    #   prefix.bits
    #     #=> "1111111111111111111111111111111111111111111111111111111111111111"
    #         "0000000000000000000000000000000000000000000000000000000000000000"
    #
    def bits
      "1" * @prefix + "0" * (128 - @prefix)
    end

    #
    # Unsigned 128 bits decimal number representing
    # the prefix
    #
    #   prefix = IPAddress::Prefix128.new 64
    #
    #   prefix.to_u128
    #     #=> 340282366920938463444927863358058659840
    #
    def to_u128
      bits.to_i(2)
    end

    #
    # Returns the length of the host portion
    # of a netmask. 
    #
    #   prefix = Prefix128.new 96
    #
    #   prefix.host_prefix
    #     #=> 32
    #
    def host_prefix
      128 - @prefix
    end

  end # class Prefix123 < Prefix

end # module IPAddress