summaryrefslogtreecommitdiff
path: root/lib/ipaddress/prefix.rb
blob: 61e684bba30f3e4441ff630880e3c0ec1ed61deb (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
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

    def initialize(num)
      @prefix = num.to_i
    end

    def to_s
      "#@prefix"
    end
    alias_method :inspect, :to_s

    def to_i
      @prefix
    end

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

   end # class Prefix


  class Prefix32 < Prefix

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

    #
    # Transforms the prefix into a string of bits
    # representing the netmask
    #
    #   prefix = IPAddress::Prefix32.new 24
    # 
    #   prefix.bits 
    #     #=> "11111111111111111111111100000000"
    #
    def bits
      "1" * @prefix + "0" * (32 - @prefix)
    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
      [bits].pack("B*").unpack("N").first
    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 IPAddress::Prefix.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 (1..128).include? num.to_i
        raise ArgumentError, "Prefix must be in range 1..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
      eval "0b#{bits}.to_i"
    end

  end # class Prefix123 < Prefix

end # module IPAddress