subnet_calc 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bfe95c8c2371bb03c0efbd706989a2f222e9b5c0
4
- data.tar.gz: 3ca24defd9741771d4d4f5cae620803e73fcecfe
3
+ metadata.gz: c6269772bdd1895f45f3d245d545d2f1f38c0c0b
4
+ data.tar.gz: 4b7c062156c81833fb15752a8f0546d6f7490632
5
5
  SHA512:
6
- metadata.gz: ef8dbcec7e56ad18c6cf949aa0d2c77465f772b3d1e3072bc86b6f46cb0fa25bfe40a9f0102e29ea795a1462a57ab7b1a283257a2c8407a406b5d56dcae163ea
7
- data.tar.gz: 57062c36c999592ee8bc9fe2b7e58303ebafaa3db4d77146791351e97c5138c87abca5bbe0916ed5470e8868a8d8273127dd5477f023ce27445b42b91e88a2c9
6
+ metadata.gz: e89aa0d80d8cd8e5777375d601bbf6950ed8ffdf6bd8ed9965b2a2fcd9319136fa203cb0d84ecc6fb19f344a0fcc2f63c224da6309252d698f300ace9b1fb607
7
+ data.tar.gz: 52a283f8937e9ff97c31429e1881e218afe95cf71b79a3db338b00d51d94ffd434c670c258ccb3146d53a7c5efa534a368eba1bfc06de7959d3f122a4e33900f
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/subnet_calc.rb CHANGED
@@ -22,7 +22,7 @@ class SubnetCalc
22
22
 
23
23
  using Ordinals
24
24
 
25
- attr_reader :to_h
25
+ attr_reader :to_h, :octets
26
26
 
27
27
  def initialize(inputs={})
28
28
 
@@ -64,11 +64,13 @@ class SubnetCalc
64
64
 
65
65
  end
66
66
 
67
+ @octets = octets
68
+
67
69
 
68
70
  # ------------------------------------
69
71
 
70
72
 
71
- octet_n, col = if hosts then
73
+ octet_n, col, class_type = if hosts then
72
74
 
73
75
  hosts_per_subnet = octets.flat_map.with_index do |octet,i|
74
76
  octet.bits.map.with_index {|y,j| [i, j, y.hosts_per_subnet] }
@@ -77,7 +79,18 @@ class SubnetCalc
77
79
  # find the smallest decimal value to match the
78
80
  # required number of hosts on a network
79
81
 
80
- hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1}
82
+ *r, _ = hosts_per_subnet.reverse.detect {|x| x.last > hosts + 1}
83
+
84
+ network = case hosts
85
+ when 1..254
86
+ 'c'
87
+ when 255..65534
88
+ 'b'
89
+ else
90
+ 'a'
91
+ end
92
+
93
+ (r + [network])
81
94
 
82
95
  elsif prefix
83
96
 
@@ -85,12 +98,20 @@ class SubnetCalc
85
98
  octet.map.with_index {|x,j| [i, j, x]}
86
99
  end
87
100
 
88
- prefixes.detect {|x| x.last == prefix}
89
-
101
+ *r, _ = prefixes.detect {|x| x.last == prefix}
102
+
103
+ network = case prefix
104
+ when 24..32
105
+ 'c'
106
+ when 16..23
107
+ 'b'
108
+ else
109
+ 'a'
110
+ end
111
+
112
+ (r + [network])
90
113
  end
91
114
 
92
- # determine what class of network we are using
93
- class_type = ((hosts and hosts <= 254) or (prefix and prefix >= 24)) ? 'c' : 'b'
94
115
 
95
116
  # Identify the network bits and the host bits
96
117
 
@@ -111,7 +132,6 @@ class SubnetCalc
111
132
  end
112
133
 
113
134
 
114
-
115
135
  bit = octets[octet_n].bits[col]
116
136
 
117
137
  magic_number, hosts_per_subnet, prefix = bit.decimal,
@@ -119,8 +139,7 @@ class SubnetCalc
119
139
 
120
140
  no_of_subnets = (2 ** 8.0) / bit.hosts_per_subnet
121
141
 
122
- segments = (no_of_subnets >= 1 ? no_of_subnets :
123
- 256 / (256 * no_of_subnets)).to_i
142
+ segments = (no_of_subnets >= 1 ? no_of_subnets : (1 / no_of_subnets)).to_i
124
143
  n = col
125
144
 
126
145
  # add the new mask to the octet
@@ -133,6 +152,8 @@ class SubnetCalc
133
152
  class_subnets(segments, hosts_per_subnet)
134
153
  when 'b'
135
154
  class_b_subnets(256 / segments, bit.decimal)
155
+ when 'a'
156
+ class_a_subnets(256 ** 2 / segments, bit.decimal)
136
157
  end
137
158
 
138
159
  ip = (default_inputs[:ip] + ' ') .split('.')[0..octet_n-1]
@@ -145,7 +166,8 @@ class SubnetCalc
145
166
  magic_number: magic_number,
146
167
  hosts: hosts_per_subnet - 2,
147
168
  subnet_mask: subnet_mask,
148
- subnet_bitmask: subnet_mask.split('.').map {|x| x.to_i.to_s(2)},
169
+ subnet_bitmask: subnet_mask.split('.').map \
170
+ {|x| ('0' * 7 + x.to_i.to_s(2))[-8..-1]},
149
171
  prefix: prefix,
150
172
  subnets: subnets,
151
173
  range: "%s-%s" % [first_ip, last_ip],
@@ -160,7 +182,6 @@ class SubnetCalc
160
182
 
161
183
  def to_s()
162
184
 
163
-
164
185
  tfo = TableFormatter.new
165
186
 
166
187
  tfo.source = @h[:subnets].map.with_index do |x,i|
@@ -244,9 +265,7 @@ EOF
244
265
  broadcast = i + block_size - 1
245
266
  first = i + 1
246
267
  last = broadcast - 1
247
-
248
-
249
-
268
+
250
269
  h = if block_given? then
251
270
  yield(i,first,last,broadcast)
252
271
  else
@@ -274,7 +293,21 @@ EOF
274
293
  end
275
294
 
276
295
  end
277
-
296
+
297
+ def class_a_subnets(n, block_size)
298
+
299
+ class_subnets(n, block_size) do |network, first, last, broadcast|
300
+
301
+ {
302
+ network: [network, 0, 0].join('.'),
303
+ first: [network, 1, 1].join('.'),
304
+ last: [broadcast, 255, 254].join('.'),
305
+ broadcast: [broadcast, 255, 255].join('.')
306
+ }
307
+
308
+ end
309
+
310
+ end
278
311
 
279
312
  def indent(s, i=2)
280
313
  s.lines.map{|x| ' ' * i + x}.join
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subnet_calc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  oAH9dWawpFIItSVexeVF4ycrINDboVQH/q93Udr9ZLbwux6Dp2bpsAxyXAdIw+Um
32
32
  ZWJvjR10ewlMyQ==
33
33
  -----END CERTIFICATE-----
34
- date: 2016-11-06 00:00:00.000000000 Z
34
+ date: 2016-11-07 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: kvx
metadata.gz.sig CHANGED
Binary file