Class: SubnetCalc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs = {hosts: 254}) ⇒ SubnetCalc

Returns a new instance of SubnetCalc.



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
# File 'lib/subnet_calc.rb', line 27

def initialize(inputs={hosts: 254})

  @inputs = inputs
  default_inputs = {hosts: 254, ip: '192.168.0.0'}.merge(inputs)

  @prefixes = 1.upto(32).each_slice(8).to_a
  
=begin
  #=> [
       [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]
      ]
=end

  

  octets = @prefixes.map.with_index do |octet, j|

    a = octet.map.with_index do |prefix, i|
      
      decimal = 2 ** (octet.length - 1 - i )

      h = {
            prefix: prefix,
            decimal: decimal,
            hosts_per_subnet: (2 ** 8) ** (@prefixes.length - 1 - j) * decimal
          }

      OpenStruct.new h
      
    end

    OpenStruct.new mask: nil, bits: a

  end
  
  hosts_per_subnet = octets.map.with_index do |x,i| 
    x.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] }
  end.flatten(1)
  

  # determine what class of network we are using

  class_type = 'c'

  # Identify the network bits and the host bits

  classes = ('a'..'d')

  # identify the initial network bits for a class *a,b or c*

  class_n = (classes.to_a.index(class_type) + 1)
  network_bits = class_n.times.map {|x| Array.new(8, 1)}
  host_bits = (classes.to_a.length - class_n).times.map {|x| Array.new(8, 0)}
  address_bits = network_bits + host_bits

  # add the mask to each octet

  octets.each.with_index do |octet, i|
    octet.mask = address_bits[i].join.to_i(2)
  end

  # ------------------------------------

  # Using the input(s) supplied:

  hosts = default_inputs[:hosts]



  # find the smallest decimal value to match the 
  # required number of hosts on a network    
  
  octet_n, col = hosts_per_subnet.reverse.detect {|x| x.last >  hosts}    

  bit = octets[octet_n].bits[col]

  magic_number, prefix = bit.hosts_per_subnet, bit.prefix
  no_of_subnets = (2 ** 8) / bit.decimal

  
  n =  col

  # add the new mask to the octet

  octets[octet_n].mask = octets[octet_n].bits.map(&:decimal)[0..n].inject(:+)
  subnet_mask = octets.map(&:mask).join('.')

  subnets = no_of_subnets.times.inject([]) do |r,n|
    
    i = r.last ? r.last.network + magic_number : 0

    broadcast = i + magic_number - 1
    first = i + 1
    last = broadcast - 1
    r << OpenStruct.new({network: i, first: first, last: last, 
                         broadcast: broadcast})
  end
  
  ip = (default_inputs[:ip] + ' ') .split('.')
  ip[class_n] = subnets.first.first
  first_ip = ip.join('.')
  ip[class_n] = subnets.first.last
  last_ip = ip.join('.')

  result = {
    class_type: class_type.upcase,
    magic_number: magic_number,
    hosts: magic_number - 2,
    subnet_mask: subnet_mask,
    subnet_bitmask: subnet_mask.split('.').map {|x| x.to_i.to_s(2)},
    prefix: prefix,
    subnets: subnets,
    range: "%s-%s" % [first_ip, last_ip],
    subnet_bits: n+1,
    max_subnets: no_of_subnets
  }

  @octet_n = octet_n
  @h = result
end

Instance Attribute Details

#to_hObject (readonly)

Returns the value of attribute to_h.



25
26
27
# File 'lib/subnet_calc.rb', line 25

def to_h
  @to_h
end

Instance Method Details

#to_sObject



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
# File 'lib/subnet_calc.rb', line 149

def to_s()


  tfo = TableFormatter.new

  tfo.source = @h[:subnets].map{|x| x.to_h.values.map(&:to_s) }
  tfo.labels = %w(Network 1st last broadcast)
  subnets_table = tfo.display(markdown: true).to_s

  # octet broken down

  tfo2 = TableFormatter.new

  prefixes = @prefixes[@octet_n].map {|x| '/' + x.to_s}
  tfo2.source = [@h[:subnet_bitmask][@octet_n].chars, prefixes]
  tfo2.labels = 8.times.map {|n| (2 ** n).to_s }.reverse
  octet_table = tfo2.display(markdown: true).to_s

"\n\nSubnet calculator\n=================\n\nInputs: \n\n\#{Kvx.new(@inputs).to_s.lines.map {|x| ' ' * 2 + x}.join}\n\nSummary\n-------\n\n* Network class: \#{@h[:class_type]}\n* magic number: \#{@h[:magic_number]}\n* hosts per subnet (magic number - 2 ): \#{@h[:hosts]}\n* subnet mask: \#{@h[:subnet_mask]}\n* subnet bitmask: \#{@h[:subnet_bitmask].join('.')}\n* prefix bit-length: \#{@h[:prefix]}\n* range: \#{@h[:range]}\n\n* subnet_bits: \#{@h[:subnet_bits]}\n* maximum subnets: \#{@h[:max_subnets]}\n\n\n\nBreakdown\n---------\n\n\#{(@octet_n + 1).ordinal} octet:\n\n\#{indent octet_table}\n\n### Subnets\n\n\n\#{indent subnets_table}\n\n-----------------------------------------------\n\n"

end