netaddr 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of netaddr might be problematic. Click here for more details.
- data/changelog +14 -6
- data/lib/cidr.rb +497 -225
- data/lib/cidr_shortcuts.rb +21 -21
- data/lib/eui.rb +162 -133
- data/lib/ip_math.rb +45 -27
- data/lib/methods.rb +187 -178
- data/lib/{net_addr.rb → netaddr.rb} +3 -1
- data/lib/tree.rb +87 -52
- data/lib/validation_shortcuts.rb +10 -10
- data/tests/cidr_test.rb +253 -167
- data/tests/eui_test.rb +39 -25
- data/tests/methods_test.rb +100 -118
- data/tests/tree_test.rb +99 -56
- metadata +12 -12
data/lib/ip_math.rb
CHANGED
@@ -4,6 +4,24 @@ private
|
|
4
4
|
|
5
5
|
# IP MATH METHODS
|
6
6
|
|
7
|
+
#==============================================================================#
|
8
|
+
# binary_mirror()
|
9
|
+
#==============================================================================#
|
10
|
+
|
11
|
+
# given an integer and number of bits to consider, return its binary mirror
|
12
|
+
#
|
13
|
+
def binary_mirror(num, bit_count)
|
14
|
+
mirror = 0
|
15
|
+
bit_count.times do # make mirror image of num by capturning lsb and left-shifting it onto mirror
|
16
|
+
mirror = mirror << 1
|
17
|
+
lsb = num & 1
|
18
|
+
num = num >> 1
|
19
|
+
mirror = mirror | lsb
|
20
|
+
end
|
21
|
+
return(mirror)
|
22
|
+
end
|
23
|
+
module_function :binary_mirror
|
24
|
+
|
7
25
|
#==============================================================================#
|
8
26
|
# bits_to_mask()
|
9
27
|
#==============================================================================#
|
@@ -71,15 +89,15 @@ module_function :ip_count_to_size
|
|
71
89
|
|
72
90
|
# unpack an int into an ip address string
|
73
91
|
#
|
74
|
-
def ip_int_to_str(
|
92
|
+
def ip_int_to_str(ip_int, version, ipv4_mapped=nil)
|
75
93
|
ip = nil
|
76
|
-
version = 4 if (!version &&
|
94
|
+
version = 4 if (!version && ip_int < 2**32)
|
77
95
|
if (version == 4)
|
78
96
|
octets = []
|
79
97
|
4.times do
|
80
|
-
octet =
|
98
|
+
octet = ip_int & 0xFF
|
81
99
|
octets.unshift(octet.to_s)
|
82
|
-
|
100
|
+
ip_int = ip_int >> 8
|
83
101
|
end
|
84
102
|
ip = octets.join('.')
|
85
103
|
else
|
@@ -88,16 +106,16 @@ def ip_int_to_str(packed_ip, version, ipv4_mapped=nil)
|
|
88
106
|
loop_count = 8
|
89
107
|
else
|
90
108
|
loop_count = 6
|
91
|
-
|
92
|
-
ipv4_addr = ip_int_to_str(
|
109
|
+
ipv4_int = ip_int & 0xffffffff
|
110
|
+
ipv4_addr = ip_int_to_str(ipv4_int, 4)
|
93
111
|
fields.unshift(ipv4_addr)
|
94
|
-
|
112
|
+
ip_int = ip_int >> 32
|
95
113
|
end
|
96
114
|
|
97
115
|
loop_count.times do
|
98
|
-
octet =
|
116
|
+
octet = ip_int & 0xFFFF
|
99
117
|
octet = octet.to_s(16)
|
100
|
-
|
118
|
+
ip_int = ip_int >> 16
|
101
119
|
|
102
120
|
# if octet < 4 characters, then pad with 0's
|
103
121
|
(4 - octet.length).times do
|
@@ -118,13 +136,13 @@ module_function :ip_int_to_str
|
|
118
136
|
# convert an ip string into an int
|
119
137
|
#
|
120
138
|
def ip_str_to_int(ip,version)
|
121
|
-
|
139
|
+
ip_int = 0
|
122
140
|
if ( version == 4)
|
123
141
|
octets = ip.split('.')
|
124
142
|
(0..3).each do |x|
|
125
143
|
octet = octets.pop.to_i
|
126
144
|
octet = octet << 8*x
|
127
|
-
|
145
|
+
ip_int = ip_int | octet
|
128
146
|
end
|
129
147
|
|
130
148
|
else
|
@@ -157,12 +175,12 @@ def ip_str_to_int(ip,version)
|
|
157
175
|
|
158
176
|
if (dotted_dec)
|
159
177
|
ipv4_addr = fields.pop
|
160
|
-
|
178
|
+
ipv4_int = NetAddr.ip_to_i(ipv4_addr, :Version => 4)
|
161
179
|
octets = []
|
162
180
|
2.times do
|
163
|
-
octet =
|
181
|
+
octet = ipv4_int & 0xFFFF
|
164
182
|
octets.unshift(octet.to_s(16))
|
165
|
-
|
183
|
+
ipv4_int = ipv4_int >> 16
|
166
184
|
end
|
167
185
|
fields.concat(octets)
|
168
186
|
end
|
@@ -171,11 +189,11 @@ def ip_str_to_int(ip,version)
|
|
171
189
|
(0..7).each do |x|
|
172
190
|
field = fields.pop.to_i(16)
|
173
191
|
field = field << 16*x
|
174
|
-
|
192
|
+
ip_int = ip_int | field
|
175
193
|
end
|
176
194
|
|
177
195
|
end
|
178
|
-
return(
|
196
|
+
return(ip_int)
|
179
197
|
end
|
180
198
|
module_function :ip_str_to_int
|
181
199
|
|
@@ -185,23 +203,23 @@ module_function :ip_str_to_int
|
|
185
203
|
|
186
204
|
# convert integer into a cidr formatted netmask (bits)
|
187
205
|
#
|
188
|
-
def mask_to_bits(
|
189
|
-
return(
|
206
|
+
def mask_to_bits(netmask_int)
|
207
|
+
return(netmask_int) if (netmask_int == 0)
|
190
208
|
|
191
209
|
mask = nil
|
192
|
-
if (
|
210
|
+
if (netmask_int < 2**32)
|
193
211
|
mask = 32
|
194
|
-
validate_netmask_int(
|
212
|
+
validate_netmask_int(netmask_int, 4, true)
|
195
213
|
else
|
196
214
|
mask = 128
|
197
|
-
validate_netmask_int(
|
215
|
+
validate_netmask_int(netmask_int, 6, true)
|
198
216
|
end
|
199
217
|
|
200
218
|
mask.times do
|
201
|
-
if ( (
|
219
|
+
if ( (netmask_int & 1) == 1)
|
202
220
|
break
|
203
221
|
end
|
204
|
-
|
222
|
+
netmask_int = netmask_int >> 1
|
205
223
|
mask = mask - 1
|
206
224
|
end
|
207
225
|
return(mask)
|
@@ -215,11 +233,11 @@ module_function :mask_to_bits
|
|
215
233
|
# convert string into integer mask
|
216
234
|
#
|
217
235
|
def netmask_str_to_int(netmask,version)
|
218
|
-
|
236
|
+
netmask_int = nil
|
219
237
|
all_f = 2**32-1
|
220
238
|
all_f = 2**128-1 if (version == 6)
|
221
239
|
if(netmask =~ /\./)
|
222
|
-
|
240
|
+
netmask_int = NetAddr.ip_to_i(netmask)
|
223
241
|
else
|
224
242
|
# remove '/' if present
|
225
243
|
if (netmask =~ /^\// )
|
@@ -227,9 +245,9 @@ def netmask_str_to_int(netmask,version)
|
|
227
245
|
netmask.lstrip!
|
228
246
|
end
|
229
247
|
netmask = netmask.to_i
|
230
|
-
|
248
|
+
netmask_int = all_f ^ (all_f >> netmask)
|
231
249
|
end
|
232
|
-
return(
|
250
|
+
return(netmask_int)
|
233
251
|
end
|
234
252
|
module_function :netmask_str_to_int
|
235
253
|
|
data/lib/methods.rb
CHANGED
@@ -1,11 +1,146 @@
|
|
1
1
|
=begin rdoc
|
2
|
-
Copyleft (c) 2006 Dustin Spinhirne
|
2
|
+
Copyleft (c) 2006 Dustin Spinhirne
|
3
3
|
|
4
4
|
Licensed under the same terms as Ruby, No Warranty is provided.
|
5
5
|
=end
|
6
6
|
|
7
7
|
module NetAddr
|
8
8
|
|
9
|
+
#==============================================================================#
|
10
|
+
# i_to_bits()
|
11
|
+
#==============================================================================#
|
12
|
+
|
13
|
+
#===Synopsis
|
14
|
+
#Convert an Integer representing a binary netmask into an Integer representing
|
15
|
+
#the number of bits in that netmask.
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
# NetAddr.i_to_bits(0xfffffffe) => 31
|
19
|
+
# NetAddr.i_to_bits(0xffffffffffffffff0000000000000000) => 64
|
20
|
+
#
|
21
|
+
#===Arguments:
|
22
|
+
#* netmask_int = Integer representing a binary netmask
|
23
|
+
#
|
24
|
+
#===Returns:
|
25
|
+
#* Integer
|
26
|
+
#
|
27
|
+
def i_to_bits(netmask_int)
|
28
|
+
|
29
|
+
# validate netmask_int
|
30
|
+
raise ArgumentError, "Integer expected for argument 'netmask_int', " +
|
31
|
+
"but #{netmask_int.class} provided." if (!netmask_int.kind_of?(Integer))
|
32
|
+
|
33
|
+
|
34
|
+
return( mask_to_bits(netmask_int) )
|
35
|
+
end
|
36
|
+
module_function :i_to_bits
|
37
|
+
|
38
|
+
#==============================================================================#
|
39
|
+
# i_to_ip()
|
40
|
+
#==============================================================================#
|
41
|
+
|
42
|
+
#===Synopsis
|
43
|
+
#Convert an Integer into an IP address. This method will attempt to auto-detect the IP version
|
44
|
+
#if not provided, however, a slight speed increase is realized if version is provided.
|
45
|
+
#
|
46
|
+
# Example:
|
47
|
+
# NetAddr.i_to_ip(3232235906) => "192.168.1.130"
|
48
|
+
# NetAddr.i_to_ip(0xffff0000000000000000000000000001, :Version => 6) => "ffff:0000:0000:0000:0000:0000:0000:0001"
|
49
|
+
#
|
50
|
+
#===Arguments:
|
51
|
+
#* ip_int = IP address as an Integer
|
52
|
+
#* options = Hash with the following keys:
|
53
|
+
# :Version -- IP version - Integer (optional)
|
54
|
+
# :IPv4Mapped -- if true, unpack IPv6 as an IPv4 mapped address (optional)
|
55
|
+
#
|
56
|
+
#===Returns:
|
57
|
+
#* String
|
58
|
+
#
|
59
|
+
def i_to_ip(ip_int, options=nil)
|
60
|
+
known_args = [:Version, :IPv4Mapped]
|
61
|
+
ipv4_mapped = false
|
62
|
+
version = nil
|
63
|
+
|
64
|
+
# validate options
|
65
|
+
if (options)
|
66
|
+
raise ArgumentError, "Hash expected for argument 'options' but #{options.class} provided." if (!options.kind_of?(Hash))
|
67
|
+
NetAddr.validate_args(options.keys,known_args)
|
68
|
+
|
69
|
+
if (options.has_key?(:Version))
|
70
|
+
version = options[:Version]
|
71
|
+
if (version != 4 && version != 6)
|
72
|
+
raise VersionError, ":Version should be 4 or 6, but was '#{version}'."
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
if (options.has_key?(:IPv4Mapped) && options[:IPv4Mapped] == true)
|
77
|
+
ipv4_mapped = true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# validate & unpack
|
82
|
+
raise ArgumentError, "Integer expected for argument 'ip_int', " +
|
83
|
+
"but #{ip_int.class} provided." if (!ip_int.kind_of?(Integer))
|
84
|
+
version = validate_ip_int(ip_int, version)
|
85
|
+
ip = ip_int_to_str(ip_int, version, ipv4_mapped)
|
86
|
+
|
87
|
+
return(ip)
|
88
|
+
end
|
89
|
+
module_function :i_to_ip
|
90
|
+
|
91
|
+
#==============================================================================#
|
92
|
+
# ip_to_i()
|
93
|
+
#==============================================================================#
|
94
|
+
|
95
|
+
#===Synopsis
|
96
|
+
#Convert IP addresses into an Integer. This method will attempt to auto-detect the IP version
|
97
|
+
#if not provided, however a slight speed increase is realized if version is provided.
|
98
|
+
#
|
99
|
+
# Example:
|
100
|
+
# NetAddr.ip_to_i('192.168.1.1') => 3232235777
|
101
|
+
# NetAddr.ip_to_i('ffff::1', :Version => 6) => 340277174624079928635746076935438991361
|
102
|
+
# NetAddr.ip_to_i('::192.168.1.1') => 3232235777
|
103
|
+
#
|
104
|
+
#===Arguments:
|
105
|
+
#* ip = IP address as a String
|
106
|
+
#* options = Hash with the following keys:
|
107
|
+
# :Version -- IP version - Integer
|
108
|
+
#
|
109
|
+
#===Returns:
|
110
|
+
#* Integer
|
111
|
+
#
|
112
|
+
def ip_to_i(ip, options=nil)
|
113
|
+
known_args = [:Version]
|
114
|
+
to_validate = {}
|
115
|
+
version = nil
|
116
|
+
|
117
|
+
# validate options
|
118
|
+
if (options)
|
119
|
+
raise ArgumentError, "Hash expected for argument 'options' but #{options.class} provided." if (!options.kind_of?(Hash))
|
120
|
+
validate_args(options.keys,known_args)
|
121
|
+
|
122
|
+
if (options.has_key?(:Version))
|
123
|
+
version = options[:Version]
|
124
|
+
to_validate[:Version] = version
|
125
|
+
if (version != 4 && version != 6)
|
126
|
+
raise VersionError, ":Version should be 4 or 6, but was '#{version}'."
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
if ( ip.kind_of?(String) )
|
132
|
+
version = detect_ip_version(ip) if (!version)
|
133
|
+
validate_ip_str(ip,version)
|
134
|
+
ip_int = ip_str_to_int(ip,version)
|
135
|
+
|
136
|
+
else
|
137
|
+
raise ArgumentError, "String expected for argument 'ip' but #{ip.class} provided."
|
138
|
+
end
|
139
|
+
|
140
|
+
return(ip_int)
|
141
|
+
end
|
142
|
+
module_function :ip_to_i
|
143
|
+
|
9
144
|
#==============================================================================#
|
10
145
|
# merge()
|
11
146
|
#==============================================================================#
|
@@ -127,8 +262,8 @@ module_function :merge
|
|
127
262
|
#netmask (bits by default) required for that subnet. IP version is assumed to be 4 unless specified otherwise.
|
128
263
|
#
|
129
264
|
# Example:
|
130
|
-
# NetAddr.
|
131
|
-
# NetAddr.
|
265
|
+
# NetAddr.minimum_size(14) => 28
|
266
|
+
# NetAddr.minimum_size(65536, :Version => 6) => 112
|
132
267
|
#
|
133
268
|
#===Arguments:
|
134
269
|
#* ipcount = IP count as an Integer
|
@@ -167,74 +302,21 @@ end
|
|
167
302
|
module_function :minimum_size
|
168
303
|
|
169
304
|
#==============================================================================#
|
170
|
-
#
|
171
|
-
#==============================================================================#
|
172
|
-
|
173
|
-
#===Synopsis
|
174
|
-
#Convert IP addresses into an Integer. This method will attempt to auto-detect the IP version
|
175
|
-
#if not provided, however a slight speed increase is realized if version is provided.
|
176
|
-
#
|
177
|
-
# Example:
|
178
|
-
# pack_ip_addr('192.168.1.1')
|
179
|
-
# pack_ip_addr(ffff::1', :Version => 6)
|
180
|
-
# pack_ip_addr(::192.168.1.1')
|
181
|
-
#
|
182
|
-
#===Arguments:
|
183
|
-
#* ip = IP address as a String
|
184
|
-
#* options = Hash with the following keys:
|
185
|
-
# :Version -- IP version - Integer
|
186
|
-
#
|
187
|
-
#===Returns:
|
188
|
-
#* Integer
|
189
|
-
#
|
190
|
-
def pack_ip_addr(ip, options=nil)
|
191
|
-
known_args = [:Version]
|
192
|
-
to_validate = {}
|
193
|
-
version = nil
|
194
|
-
|
195
|
-
# validate options
|
196
|
-
if (options)
|
197
|
-
raise ArgumentError, "Hash expected for argument 'options' but #{options.class} provided." if (!options.kind_of?(Hash))
|
198
|
-
validate_args(options.keys,known_args)
|
199
|
-
|
200
|
-
if (options.has_key?(:Version))
|
201
|
-
version = options[:Version]
|
202
|
-
to_validate[:Version] = version
|
203
|
-
if (version != 4 && version != 6)
|
204
|
-
raise VersionError, ":Version should be 4 or 6, but was '#{version}'."
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
if ( ip.kind_of?(String) )
|
210
|
-
version = detect_ip_version(ip) if (!version)
|
211
|
-
validate_ip_str(ip,version)
|
212
|
-
packed_ip = ip_str_to_int(ip,version)
|
213
|
-
|
214
|
-
else
|
215
|
-
raise ArgumentError, "String expected for argument 'ip' but #{ip.class} provided."
|
216
|
-
end
|
217
|
-
|
218
|
-
return(packed_ip)
|
219
|
-
end
|
220
|
-
module_function :pack_ip_addr
|
221
|
-
|
222
|
-
#==============================================================================#
|
223
|
-
# pack_ip_netmask()
|
305
|
+
# netmask_to_i()
|
224
306
|
#==============================================================================#
|
225
307
|
|
226
308
|
#===Synopsis
|
227
309
|
#Convert IP netmask into an Integer. Netmask may be in either CIDR (/yy) or
|
228
310
|
#extended (y.y.y.y) format. CIDR formatted netmasks may either
|
229
|
-
#be a String or an Integer. IP version defaults to 4. It may be necessary
|
311
|
+
#be a String or an Integer. IP version defaults to 4. It may be necessary
|
230
312
|
#to specify the version if an IPv6 netmask of /32 or smaller is provided.
|
231
313
|
#
|
232
314
|
# Example:
|
233
|
-
# NetAddr.
|
234
|
-
# NetAddr.
|
235
|
-
# NetAddr.
|
236
|
-
# NetAddr.
|
237
|
-
# NetAddr.
|
315
|
+
# NetAddr.netmask_to_i('255.255.255.0') => 4294967040
|
316
|
+
# NetAddr.netmask_to_i('24') => 4294967040
|
317
|
+
# NetAddr.netmask_to_i(24) => 4294967040
|
318
|
+
# NetAddr.netmask_to_i('/24') => 4294967040
|
319
|
+
# NetAddr.netmask_to_i('32', :Version => 6) => 340282366841710300949110269838224261120
|
238
320
|
#
|
239
321
|
#===Arguments
|
240
322
|
#* netmask = Netmask as a String or Integer
|
@@ -244,10 +326,10 @@ module_function :pack_ip_addr
|
|
244
326
|
#===Returns:
|
245
327
|
#* Integer
|
246
328
|
#
|
247
|
-
def
|
329
|
+
def netmask_to_i(netmask, options=nil)
|
248
330
|
known_args = [:Version]
|
249
331
|
version = 4
|
250
|
-
|
332
|
+
netmask_int = nil
|
251
333
|
|
252
334
|
# validate options
|
253
335
|
if (options)
|
@@ -264,20 +346,20 @@ def pack_ip_netmask(netmask, options=nil)
|
|
264
346
|
|
265
347
|
if (netmask.kind_of?(String))
|
266
348
|
validate_netmask_str(netmask, version)
|
267
|
-
|
349
|
+
netmask_int = netmask_str_to_int(netmask,version)
|
268
350
|
|
269
351
|
elsif (netmask.kind_of?(Integer))
|
270
352
|
validate_netmask_int(netmask, version, true)
|
271
|
-
|
353
|
+
netmask_int = bits_to_mask(netmask,version)
|
272
354
|
|
273
355
|
else
|
274
356
|
raise ArgumentError, "String or Integer expected for argument 'netmask', " +
|
275
|
-
"but #{netmask.class} provided." if (!netmask.kind_of?(Integer) && !netmask.kind_of?(String))
|
357
|
+
"but #{netmask.class} provided." if (!netmask.kind_of?(Integer) && !netmask.kind_of?(String))
|
276
358
|
end
|
277
359
|
|
278
|
-
return(
|
360
|
+
return(netmask_int)
|
279
361
|
end
|
280
|
-
module_function :
|
362
|
+
module_function :netmask_to_i
|
281
363
|
|
282
364
|
#==============================================================================#
|
283
365
|
# range()
|
@@ -311,6 +393,15 @@ module_function :pack_ip_netmask
|
|
311
393
|
#===Returns:
|
312
394
|
#* Array of Strings or NetAddr::CIDR objects, or an Integer
|
313
395
|
#
|
396
|
+
#===Note:
|
397
|
+
#If you do not need all of the fancy options in this method, then please consider
|
398
|
+
#using the standard Ruby Range class as shown below.
|
399
|
+
#
|
400
|
+
# Example:
|
401
|
+
# start = NetAddr::CIDR.create('192.168.1.0')
|
402
|
+
# fin = NetAddr::CIDR.create('192.168.2.3')
|
403
|
+
# (start..fin).each {|addr| puts addr.desc}
|
404
|
+
#
|
314
405
|
def range(lower, upper, options=nil)
|
315
406
|
known_args = [:Bitstep, :Inclusive, :Limit, :Objectify, :Short, :Size]
|
316
407
|
list = []
|
@@ -374,7 +465,7 @@ def range(lower, upper, options=nil)
|
|
374
465
|
# check version, store & sort
|
375
466
|
if (lower.version == upper.version)
|
376
467
|
version = lower.version
|
377
|
-
boundaries = [lower.
|
468
|
+
boundaries = [lower.to_i(:ip), upper.to_i(:ip)]
|
378
469
|
boundaries.sort
|
379
470
|
else
|
380
471
|
raise VersionError, "Provided NetAddr::CIDR objects are of different IP versions."
|
@@ -422,7 +513,7 @@ module_function :range
|
|
422
513
|
#The address should not contain a netmask.
|
423
514
|
#
|
424
515
|
# Example:
|
425
|
-
# NetAddr.shorten('fec0:0000:0000:0000:0000:0000:0000:0001')
|
516
|
+
# NetAddr.shorten('fec0:0000:0000:0000:0000:0000:0000:0001') => "fec0::1"
|
426
517
|
#
|
427
518
|
#===Arguments:
|
428
519
|
#* addr = String
|
@@ -510,10 +601,9 @@ module_function :shorten
|
|
510
601
|
#==============================================================================#
|
511
602
|
|
512
603
|
#===Synopsis
|
513
|
-
#Given a list of CIDR addresses or NetAddr::CIDR objects,
|
604
|
+
#Given a list of CIDR addresses or NetAddr::CIDR objects,
|
514
605
|
#sort them from lowest to highest by Network/Netmask. This method will use the
|
515
|
-
#base address
|
516
|
-
#objects, or the base address of any CIDR addresses passed
|
606
|
+
#base address of any NetAddr::CIDR objects or CIDR addresses provided.
|
517
607
|
#
|
518
608
|
# Example:
|
519
609
|
# cidr1 = NetAddr::CIDR.create('192.168.1.32/27')
|
@@ -568,87 +658,6 @@ def sort(list)
|
|
568
658
|
end
|
569
659
|
module_function :sort
|
570
660
|
|
571
|
-
#==============================================================================#
|
572
|
-
# unpack_ip_addr()
|
573
|
-
#==============================================================================#
|
574
|
-
|
575
|
-
#===Synopsis
|
576
|
-
#Unack a packed IP address back into a printable string. This method will attempt to auto-detect the IP version
|
577
|
-
#if not provided, however a slight speed increase is realized if version is provided.
|
578
|
-
#
|
579
|
-
# Example:
|
580
|
-
# NetAddr.unpack_ip_addr(3232235906)
|
581
|
-
# NetAddr.unpack_ip_addr(packed, :Version => 6)
|
582
|
-
#
|
583
|
-
#===Arguments:
|
584
|
-
#* packed_ip = Packed IP address as an Integer
|
585
|
-
#* options = Hash with the following keys:
|
586
|
-
# :Version -- IP version - Integer (optional)
|
587
|
-
# :IPv4Mapped -- if true, unpack IPv6 as an IPv4 mapped address (optional)
|
588
|
-
#
|
589
|
-
#===Returns:
|
590
|
-
#* String
|
591
|
-
#
|
592
|
-
def unpack_ip_addr(packed_ip, options=nil)
|
593
|
-
known_args = [:Version, :IPv4Mapped]
|
594
|
-
ipv4_mapped = false
|
595
|
-
version = nil
|
596
|
-
|
597
|
-
# validate options
|
598
|
-
if (options)
|
599
|
-
raise ArgumentError, "Hash expected for argument 'options' but #{options.class} provided." if (!options.kind_of?(Hash))
|
600
|
-
NetAddr.validate_args(options.keys,known_args)
|
601
|
-
|
602
|
-
if (options.has_key?(:Version))
|
603
|
-
version = options[:Version]
|
604
|
-
if (version != 4 && version != 6)
|
605
|
-
raise VersionError, ":Version should be 4 or 6, but was '#{version}'."
|
606
|
-
end
|
607
|
-
end
|
608
|
-
|
609
|
-
if (options.has_key?(:IPv4Mapped) && options[:IPv4Mapped] == true)
|
610
|
-
ipv4_mapped = true
|
611
|
-
end
|
612
|
-
end
|
613
|
-
|
614
|
-
# validate & unpack
|
615
|
-
raise ArgumentError, "Integer expected for argument 'packed_ip', " +
|
616
|
-
"but #{packed_ip.class} provided." if (!packed_ip.kind_of?(Integer))
|
617
|
-
version = validate_ip_int(packed_ip, version)
|
618
|
-
ip = ip_int_to_str(packed_ip, version, ipv4_mapped)
|
619
|
-
|
620
|
-
return(ip)
|
621
|
-
end
|
622
|
-
module_function :unpack_ip_addr
|
623
|
-
|
624
|
-
#==============================================================================#
|
625
|
-
# unpack_ip_netmask()
|
626
|
-
#==============================================================================#
|
627
|
-
|
628
|
-
#===Synopsis
|
629
|
-
#Unpack a packed IP netmask into an Integer representing the number of
|
630
|
-
#bits in the CIDR mask.
|
631
|
-
#
|
632
|
-
# Example:
|
633
|
-
# NetAddr.unpack_ip_netmask(0xfffffffe)
|
634
|
-
#
|
635
|
-
#===Arguments:
|
636
|
-
#* packed_netmask = Packed netmask as an Integer
|
637
|
-
#
|
638
|
-
#===Returns:
|
639
|
-
#* Integer
|
640
|
-
#
|
641
|
-
def unpack_ip_netmask(packed_netmask)
|
642
|
-
|
643
|
-
# validate packed_netmask
|
644
|
-
raise ArgumentError, "Integer expected for argument 'packed_netmask', " +
|
645
|
-
"but #{packed_netmask.class} provided." if (!packed_netmask.kind_of?(Integer))
|
646
|
-
|
647
|
-
|
648
|
-
return( mask_to_bits(packed_netmask) )
|
649
|
-
end
|
650
|
-
module_function :unpack_ip_netmask
|
651
|
-
|
652
661
|
#==============================================================================#
|
653
662
|
# unshorten()
|
654
663
|
#==============================================================================#
|
@@ -658,7 +667,7 @@ module_function :unpack_ip_netmask
|
|
658
667
|
#notation. The address should not contain a netmask.
|
659
668
|
#
|
660
669
|
# Example:
|
661
|
-
# NetAddr.unshorten('fec0::1')
|
670
|
+
# NetAddr.unshorten('fec0::1') => "fec0:0000:0000:0000:0000:0000:0000:0001"
|
662
671
|
#
|
663
672
|
#===Arguments:
|
664
673
|
#* ip = CIDR address as a String
|
@@ -676,11 +685,11 @@ def unshorten(ip)
|
|
676
685
|
validate_ip_str(ip, 6)
|
677
686
|
ipv4_mapped = true if (ip =~ /\./)
|
678
687
|
|
679
|
-
|
688
|
+
ip_int = ip_to_i(ip, :Version => 6)
|
680
689
|
if (!ipv4_mapped)
|
681
|
-
long = ip_int_to_str(
|
690
|
+
long = ip_int_to_str(ip_int, 6)
|
682
691
|
else
|
683
|
-
long = ip_int_to_str(
|
692
|
+
long = ip_int_to_str(ip_int, 6, true)
|
684
693
|
end
|
685
694
|
|
686
695
|
return(long)
|
@@ -695,7 +704,7 @@ module_function :unshorten
|
|
695
704
|
#Validate an EUI-48 or EUI-64 address. Raises NetAddr::ValidationError on validation failure.
|
696
705
|
#
|
697
706
|
# Example:
|
698
|
-
# NetAddr.validate_eui('01-00-5e-12-34-56')
|
707
|
+
# NetAddr.validate_eui('01-00-5e-12-34-56') => true
|
699
708
|
#
|
700
709
|
# - Arguments
|
701
710
|
#* eui = EUI address as a String
|
@@ -751,12 +760,12 @@ module_function :validate_eui
|
|
751
760
|
#Raises NetAddr::ValidationError on validation failure.
|
752
761
|
#
|
753
762
|
# Example:
|
754
|
-
# NetAddr.validate_ip_addr('192.168.1.1')
|
755
|
-
# NetAddr.validate_ip_addr('ffff::1', :Version => 6)
|
756
|
-
# NetAddr.validate_ip_addr('::192.168.1.1')
|
757
|
-
# NetAddr.validate_ip_addr(0xFFFFFF)
|
758
|
-
# NetAddr.validate_ip_addr(2**128-1)
|
759
|
-
# NetAddr.validate_ip_addr(2**32-1, :Version => 4)
|
763
|
+
# NetAddr.validate_ip_addr('192.168.1.1') => true
|
764
|
+
# NetAddr.validate_ip_addr('ffff::1', :Version => 6) => true
|
765
|
+
# NetAddr.validate_ip_addr('::192.168.1.1') => true
|
766
|
+
# NetAddr.validate_ip_addr(0xFFFFFF) => true
|
767
|
+
# NetAddr.validate_ip_addr(2**128-1) => true
|
768
|
+
# NetAddr.validate_ip_addr(2**32-1, :Version => 4) => true
|
760
769
|
#
|
761
770
|
#===Arguments
|
762
771
|
#* ip = IP address as a String or Integer
|
@@ -804,26 +813,26 @@ module_function :validate_ip_addr
|
|
804
813
|
#==============================================================================#
|
805
814
|
|
806
815
|
#===Synopsis
|
807
|
-
#Validate IP Netmask.Version defaults to 4 if not specified.
|
816
|
+
#Validate IP Netmask. Version defaults to 4 if not specified.
|
808
817
|
#Raises NetAddr::ValidationError on validation failure.
|
809
818
|
#
|
810
819
|
# Examples:
|
811
|
-
# NetAddr.validate_ip_netmask('/32')
|
812
|
-
# NetAddr.validate_ip_netmask(32)
|
813
|
-
# NetAddr.validate_ip_netmask(0xffffffff, :
|
820
|
+
# NetAddr.validate_ip_netmask('/32') => true
|
821
|
+
# NetAddr.validate_ip_netmask(32) => true
|
822
|
+
# NetAddr.validate_ip_netmask(0xffffffff, :Integer => true) => true
|
814
823
|
#
|
815
824
|
#===Arguments:
|
816
825
|
#* netmask = Netmask as a String or Integer
|
817
826
|
#* options = Hash with the following keys:
|
818
|
-
# :
|
827
|
+
# :Integer -- if true, the provided Netmask is an Integer mask
|
819
828
|
# :Version -- IP version - Integer (optional)
|
820
829
|
#
|
821
830
|
#===Returns:
|
822
831
|
#* True
|
823
832
|
#
|
824
833
|
def validate_ip_netmask(netmask, options=nil)
|
825
|
-
known_args = [:
|
826
|
-
|
834
|
+
known_args = [:Integer, :Version]
|
835
|
+
is_integer = false
|
827
836
|
version = 4
|
828
837
|
|
829
838
|
# validate options
|
@@ -831,8 +840,8 @@ def validate_ip_netmask(netmask, options=nil)
|
|
831
840
|
raise ArgumentError, "Hash expected for argument 'options' but #{options.class} provided." if (!options.kind_of?(Hash))
|
832
841
|
NetAddr.validate_args(options.keys,known_args)
|
833
842
|
|
834
|
-
if (options.has_key?(:
|
835
|
-
|
843
|
+
if (options.has_key?(:Integer) && options[:Integer] == true)
|
844
|
+
is_integer = true
|
836
845
|
end
|
837
846
|
|
838
847
|
if (options.has_key?(:Version))
|
@@ -847,7 +856,7 @@ def validate_ip_netmask(netmask, options=nil)
|
|
847
856
|
if (netmask.kind_of?(String))
|
848
857
|
validate_netmask_str(netmask,version)
|
849
858
|
elsif (netmask.kind_of?(Integer) )
|
850
|
-
validate_netmask_int(netmask,version,
|
859
|
+
validate_netmask_int(netmask,version,is_integer)
|
851
860
|
else
|
852
861
|
raise ArgumentError, "Integer or String expected for argument 'netmask' but " +
|
853
862
|
"#{netmask.class} provided." if (!netmask.kind_of?(String) && !netmask.kind_of?(Integer))
|
@@ -877,7 +886,7 @@ module_function :validate_ip_netmask
|
|
877
886
|
#* ip = Wildcard IP address as a String
|
878
887
|
#
|
879
888
|
#===Returns:
|
880
|
-
#* CIDR object
|
889
|
+
#* CIDR object
|
881
890
|
#
|
882
891
|
def wildcard(ip)
|
883
892
|
version = 4
|