netaddr 1.4.0 → 1.5.3
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.
- checksums.yaml +7 -0
- data/README.md +9 -0
- data/changelog +26 -9
- data/lib/cidr.rb +67 -231
- data/lib/cidr_shortcuts.rb +25 -35
- data/lib/eui.rb +78 -95
- data/lib/ip_math.rb +0 -32
- data/lib/methods.rb +96 -62
- data/lib/netaddr.rb +6 -12
- data/lib/tree.rb +2 -107
- data/lib/validation_shortcuts.rb +0 -18
- data/license +13 -0
- data/{tests → test}/cidr_test.rb +2 -1
- data/{tests → test}/eui_test.rb +21 -4
- data/{tests → test}/methods_test.rb +20 -1
- data/{tests → test}/tree_test.rb +1 -1
- metadata +36 -42
- data/README +0 -17
data/lib/methods.rb
CHANGED
@@ -1,15 +1,5 @@
|
|
1
|
-
=begin rdoc
|
2
|
-
Copyleft (c) 2006 Dustin Spinhirne
|
3
|
-
|
4
|
-
Licensed under the same terms as Ruby, No Warranty is provided.
|
5
|
-
=end
|
6
|
-
|
7
1
|
module NetAddr
|
8
2
|
|
9
|
-
#==============================================================================#
|
10
|
-
# i_to_bits()
|
11
|
-
#==============================================================================#
|
12
|
-
|
13
3
|
#===Synopsis
|
14
4
|
#Convert an Integer representing a binary netmask into an Integer representing
|
15
5
|
#the number of bits in that netmask.
|
@@ -35,10 +25,6 @@ def i_to_bits(netmask_int)
|
|
35
25
|
end
|
36
26
|
module_function :i_to_bits
|
37
27
|
|
38
|
-
#==============================================================================#
|
39
|
-
# i_to_ip()
|
40
|
-
#==============================================================================#
|
41
|
-
|
42
28
|
#===Synopsis
|
43
29
|
#Convert an Integer into an IP address. This method will attempt to auto-detect the IP version
|
44
30
|
#if not provided, however, a slight speed increase is realized if version is provided.
|
@@ -88,10 +74,6 @@ def i_to_ip(ip_int, options=nil)
|
|
88
74
|
end
|
89
75
|
module_function :i_to_ip
|
90
76
|
|
91
|
-
#==============================================================================#
|
92
|
-
# ip_to_i()
|
93
|
-
#==============================================================================#
|
94
|
-
|
95
77
|
#===Synopsis
|
96
78
|
#Convert IP addresses into an Integer. This method will attempt to auto-detect the IP version
|
97
79
|
#if not provided, however a slight speed increase is realized if version is provided.
|
@@ -141,10 +123,6 @@ def ip_to_i(ip, options=nil)
|
|
141
123
|
end
|
142
124
|
module_function :ip_to_i
|
143
125
|
|
144
|
-
#==============================================================================#
|
145
|
-
# merge()
|
146
|
-
#==============================================================================#
|
147
|
-
|
148
126
|
#===Synopsis
|
149
127
|
#Given a list of CIDR addresses or NetAddr::CIDR objects,
|
150
128
|
#merge (summarize) them in the most efficient way possible. Summarization
|
@@ -253,10 +231,6 @@ def merge(list,options=nil)
|
|
253
231
|
end
|
254
232
|
module_function :merge
|
255
233
|
|
256
|
-
#==============================================================================#
|
257
|
-
# minimum_size()
|
258
|
-
#==============================================================================#
|
259
|
-
|
260
234
|
#===Synopsis
|
261
235
|
#Given the number of IP addresses required in a subnet, return the minimum
|
262
236
|
#netmask (bits by default) required for that subnet. IP version is assumed to be 4 unless specified otherwise.
|
@@ -301,10 +275,6 @@ def minimum_size(ipcount, options=nil)
|
|
301
275
|
end
|
302
276
|
module_function :minimum_size
|
303
277
|
|
304
|
-
#==============================================================================#
|
305
|
-
# netmask_to_i()
|
306
|
-
#==============================================================================#
|
307
|
-
|
308
278
|
#===Synopsis
|
309
279
|
#Convert IP netmask into an Integer. Netmask may be in either CIDR (/yy) or
|
310
280
|
#extended (y.y.y.y) format. CIDR formatted netmasks may either
|
@@ -361,10 +331,6 @@ def netmask_to_i(netmask, options=nil)
|
|
361
331
|
end
|
362
332
|
module_function :netmask_to_i
|
363
333
|
|
364
|
-
#==============================================================================#
|
365
|
-
# range()
|
366
|
-
#==============================================================================#
|
367
|
-
|
368
334
|
#===Synopsis
|
369
335
|
#Given two CIDR addresses or NetAddr::CIDR objects of the same version,
|
370
336
|
#return all IP addresses between them. NetAddr.range will use the original IP
|
@@ -492,7 +458,7 @@ def range(lower, upper, options=nil)
|
|
492
458
|
|
493
459
|
my_ip = my_ip + bitstep
|
494
460
|
if (limit)
|
495
|
-
limit = limit -1
|
461
|
+
limit = limit - 1
|
496
462
|
break if (limit == 0)
|
497
463
|
end
|
498
464
|
end
|
@@ -504,10 +470,6 @@ def range(lower, upper, options=nil)
|
|
504
470
|
end
|
505
471
|
module_function :range
|
506
472
|
|
507
|
-
#==============================================================================#
|
508
|
-
# shorten()
|
509
|
-
#==============================================================================#
|
510
|
-
|
511
473
|
#===Synopsis
|
512
474
|
#Take a standard IPv6 address and format it in short-hand notation.
|
513
475
|
#The address should not contain a netmask.
|
@@ -596,10 +558,6 @@ def shorten(addr)
|
|
596
558
|
end
|
597
559
|
module_function :shorten
|
598
560
|
|
599
|
-
#==============================================================================#
|
600
|
-
# sort()
|
601
|
-
#==============================================================================#
|
602
|
-
|
603
561
|
#===Synopsis
|
604
562
|
#Sort a list of CIDR addresses or NetAddr::CIDR objects,
|
605
563
|
#
|
@@ -680,9 +638,101 @@ def sort(list, options=nil)
|
|
680
638
|
end
|
681
639
|
module_function :sort
|
682
640
|
|
683
|
-
|
684
|
-
#
|
685
|
-
|
641
|
+
#===Synopsis
|
642
|
+
#Given a list of CIDR addresses or NetAddr::CIDR objects,
|
643
|
+
#return only the top-level supernet CIDR addresses.
|
644
|
+
#
|
645
|
+
#
|
646
|
+
#If the :Objectify option is enabled, then returned CIDR objects will
|
647
|
+
#store the more specific CIDRs (i.e. subnets of those CIDRs) within the tag value :Subnets
|
648
|
+
#For example, cidr_x.tag[:Subnets] would be an Array of CIDR subnets of cidr_x.
|
649
|
+
#
|
650
|
+
# Example:
|
651
|
+
# NetAddr.supernets(['192.168.0.0', '192.168.0.1', '192.168.0.0/31'])
|
652
|
+
#
|
653
|
+
#===Arguments:
|
654
|
+
#* list = Array of CIDR addresses as Strings, or an Array of NetAddr::CIDR objects
|
655
|
+
#* options = Hash with the following keys:
|
656
|
+
# :Objectify -- if true, return NetAddr::CIDR objects
|
657
|
+
# :Short -- if true, return IPv6 addresses in short-hand notation
|
658
|
+
#
|
659
|
+
#===Returns:
|
660
|
+
#* Array of CIDR addresses or NetAddr::CIDR objects
|
661
|
+
#
|
662
|
+
def supernets(list,options=nil)
|
663
|
+
known_args = [:Objectify, :Short]
|
664
|
+
short = false
|
665
|
+
objectify = false
|
666
|
+
verbose = false
|
667
|
+
|
668
|
+
# validate list
|
669
|
+
raise ArgumentError, "Array expected for argument 'list' but #{list.class} provided." if (!list.kind_of?(Array) )
|
670
|
+
|
671
|
+
# validate options
|
672
|
+
if (options)
|
673
|
+
raise ArgumentError, "Hash expected for argument 'options' but #{options.class} provided." if (!options.kind_of?(Hash) )
|
674
|
+
NetAddr.validate_args(options.keys,known_args)
|
675
|
+
|
676
|
+
if (options.has_key?(:Objectify) && options[:Objectify] == true)
|
677
|
+
objectify = true
|
678
|
+
end
|
679
|
+
|
680
|
+
if (options.has_key?(:Short) && options[:Short] == true)
|
681
|
+
short = true
|
682
|
+
end
|
683
|
+
end
|
684
|
+
|
685
|
+
# make sure all are valid types of the same IP version
|
686
|
+
v4_list = []
|
687
|
+
v6_list = []
|
688
|
+
list.each do |obj|
|
689
|
+
if (!obj.kind_of?(NetAddr::CIDR))
|
690
|
+
begin
|
691
|
+
obj = NetAddr::CIDR.create(obj)
|
692
|
+
rescue Exception => error
|
693
|
+
raise ArgumentError, "One of the provided CIDR addresses raised the following " +
|
694
|
+
"errors: #{error}"
|
695
|
+
end
|
696
|
+
end
|
697
|
+
|
698
|
+
if (obj.version == 4)
|
699
|
+
v4_list.push(obj)
|
700
|
+
else
|
701
|
+
v6_list.push(obj)
|
702
|
+
end
|
703
|
+
end
|
704
|
+
|
705
|
+
# do summary calcs
|
706
|
+
v4_summary = []
|
707
|
+
v6_summary = []
|
708
|
+
if (v4_list.length != 0)
|
709
|
+
v4_summary = NetAddr.cidr_supernets(v4_list)
|
710
|
+
end
|
711
|
+
|
712
|
+
if (v6_list.length != 0)
|
713
|
+
v6_summary = NetAddr.cidr_supernets(v6_list)
|
714
|
+
end
|
715
|
+
|
716
|
+
# decide what to return
|
717
|
+
summarized_list = []
|
718
|
+
if (!objectify)
|
719
|
+
summarized_list = []
|
720
|
+
if (v4_summary.length != 0)
|
721
|
+
v4_summary.each {|x| summarized_list.push(x.desc())}
|
722
|
+
end
|
723
|
+
|
724
|
+
if (v6_summary.length != 0)
|
725
|
+
v6_summary.each {|x| summarized_list.push(x.desc(:Short => short))}
|
726
|
+
end
|
727
|
+
|
728
|
+
else
|
729
|
+
summarized_list.concat(v4_summary) if (v4_summary.length != 0)
|
730
|
+
summarized_list.concat(v6_summary) if (v6_summary.length != 0)
|
731
|
+
end
|
732
|
+
|
733
|
+
return(summarized_list)
|
734
|
+
end
|
735
|
+
module_function :supernets
|
686
736
|
|
687
737
|
#===Synopsis
|
688
738
|
#Take an IPv6 address in short-hand format, and expand it into standard
|
@@ -718,10 +768,6 @@ def unshorten(ip)
|
|
718
768
|
end
|
719
769
|
module_function :unshorten
|
720
770
|
|
721
|
-
#==============================================================================#
|
722
|
-
# validate_eui()
|
723
|
-
#==============================================================================#
|
724
|
-
|
725
771
|
#===Synopsis
|
726
772
|
#Validate an EUI-48 or EUI-64 address. Raises NetAddr::ValidationError on validation failure.
|
727
773
|
#
|
@@ -771,10 +817,6 @@ def validate_eui(eui)
|
|
771
817
|
end
|
772
818
|
module_function :validate_eui
|
773
819
|
|
774
|
-
#==============================================================================#
|
775
|
-
# validate_ip_addr()
|
776
|
-
#==============================================================================#
|
777
|
-
|
778
820
|
#===Synopsis
|
779
821
|
#Validate an IP address. The address should not contain a netmask.
|
780
822
|
#This method will attempt to auto-detect the IP version
|
@@ -830,10 +872,6 @@ def validate_ip_addr(ip, options=nil)
|
|
830
872
|
end
|
831
873
|
module_function :validate_ip_addr
|
832
874
|
|
833
|
-
#==============================================================================#
|
834
|
-
# validate_ip_netmask()
|
835
|
-
#==============================================================================#
|
836
|
-
|
837
875
|
#===Synopsis
|
838
876
|
#Validate IP Netmask. Version defaults to 4 if not specified.
|
839
877
|
#Raises NetAddr::ValidationError on validation failure.
|
@@ -888,10 +926,6 @@ def validate_ip_netmask(netmask, options=nil)
|
|
888
926
|
end
|
889
927
|
module_function :validate_ip_netmask
|
890
928
|
|
891
|
-
#==============================================================================#
|
892
|
-
# wildcard()
|
893
|
-
#==============================================================================#
|
894
|
-
|
895
929
|
#===Synopsis
|
896
930
|
#Convert a wildcard IP into a valid CIDR address. Wildcards must always be at
|
897
931
|
#the end of the address. Any data located after the first wildcard will be lost.
|
data/lib/netaddr.rb
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
=begin rdoc
|
2
|
-
Copyleft (c) 2006 Dustin Spinhirne
|
3
|
-
|
4
|
-
Licensed under the same terms as Ruby, No Warranty is provided.
|
5
|
-
=end
|
6
|
-
|
7
1
|
require 'time'
|
8
2
|
require 'digest/sha1'
|
9
3
|
require File.join(File.dirname(__FILE__), 'validation_shortcuts.rb')
|
@@ -16,14 +10,14 @@ require File.join(File.dirname(__FILE__), 'eui.rb')
|
|
16
10
|
|
17
11
|
module NetAddr
|
18
12
|
|
19
|
-
class BoundaryError < StandardError #:nodoc:
|
20
|
-
end
|
13
|
+
class BoundaryError < StandardError #:nodoc:
|
14
|
+
end
|
21
15
|
|
22
|
-
class ValidationError < StandardError #:nodoc:
|
23
|
-
end
|
16
|
+
class ValidationError < StandardError #:nodoc:
|
17
|
+
end
|
24
18
|
|
25
|
-
class VersionError < StandardError #:nodoc:
|
26
|
-
end
|
19
|
+
class VersionError < StandardError #:nodoc:
|
20
|
+
end
|
27
21
|
|
28
22
|
end # module NetAddr
|
29
23
|
|
data/lib/tree.rb
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
=begin rdoc
|
2
|
-
Copyleft (c) 2006 Dustin Spinhirne
|
3
|
-
|
4
|
-
Licensed under the same terms as Ruby, No Warranty is provided.
|
5
|
-
=end
|
6
|
-
|
7
1
|
module NetAddr
|
8
2
|
|
9
3
|
#=Tree
|
@@ -29,10 +23,6 @@ module NetAddr
|
|
29
23
|
#
|
30
24
|
class Tree
|
31
25
|
|
32
|
-
#==============================================================================#
|
33
|
-
# initialize()
|
34
|
-
#==============================================================================#
|
35
|
-
|
36
26
|
#===Synopsis
|
37
27
|
#Create a new Tree object.
|
38
28
|
#
|
@@ -48,10 +38,6 @@ class Tree
|
|
48
38
|
@v6_root = NetAddr::CIDRv6.new(0,0,{:Subnets => []})
|
49
39
|
end
|
50
40
|
|
51
|
-
#==============================================================================#
|
52
|
-
# add!()
|
53
|
-
#==============================================================================#
|
54
|
-
|
55
41
|
#===Synopsis
|
56
42
|
# Add a CIDR address or NetAddr::CIDR object to the tree.
|
57
43
|
# Example:
|
@@ -84,10 +70,6 @@ class Tree
|
|
84
70
|
return(nil)
|
85
71
|
end
|
86
72
|
|
87
|
-
#==============================================================================#
|
88
|
-
# ancestors()
|
89
|
-
#==============================================================================#
|
90
|
-
|
91
73
|
#===Synopsis
|
92
74
|
# Returns all the ancestors of the provided CIDR addresses.
|
93
75
|
#
|
@@ -121,10 +103,6 @@ class Tree
|
|
121
103
|
return(list)
|
122
104
|
end
|
123
105
|
|
124
|
-
#==============================================================================#
|
125
|
-
# children()
|
126
|
-
#==============================================================================#
|
127
|
-
|
128
106
|
#===Synopsis
|
129
107
|
# Returns all the immediate children of the provided CIDR addresses.
|
130
108
|
#
|
@@ -159,10 +137,6 @@ class Tree
|
|
159
137
|
return(list)
|
160
138
|
end
|
161
139
|
|
162
|
-
#==============================================================================#
|
163
|
-
# descendants
|
164
|
-
#==============================================================================#
|
165
|
-
|
166
140
|
#===Synopsis
|
167
141
|
# Return all descendants of the provided CIDR address.
|
168
142
|
#
|
@@ -199,10 +173,6 @@ class Tree
|
|
199
173
|
return(list)
|
200
174
|
end
|
201
175
|
|
202
|
-
#==============================================================================#
|
203
|
-
# delete!()
|
204
|
-
#==============================================================================#
|
205
|
-
|
206
176
|
#===Synopsis
|
207
177
|
# Remove the provided CIDR address from the tree.
|
208
178
|
#
|
@@ -243,10 +213,6 @@ class Tree
|
|
243
213
|
return(removed)
|
244
214
|
end
|
245
215
|
|
246
|
-
#==============================================================================#
|
247
|
-
# dump
|
248
|
-
#==============================================================================#
|
249
|
-
|
250
216
|
#===Synopsis
|
251
217
|
# Dump the contents of this tree.
|
252
218
|
#
|
@@ -268,10 +234,6 @@ class Tree
|
|
268
234
|
return(list)
|
269
235
|
end
|
270
236
|
|
271
|
-
#==============================================================================#
|
272
|
-
# exists?()
|
273
|
-
#==============================================================================#
|
274
|
-
|
275
237
|
#===Synopsis
|
276
238
|
# Has a CIDR address already been added to the tree?
|
277
239
|
#
|
@@ -301,10 +263,6 @@ class Tree
|
|
301
263
|
return(found)
|
302
264
|
end
|
303
265
|
|
304
|
-
#==============================================================================#
|
305
|
-
# fill_in!()
|
306
|
-
#==============================================================================#
|
307
|
-
|
308
266
|
#===Synopsis
|
309
267
|
# Fill in the missing subnets of a particular CIDR.
|
310
268
|
#
|
@@ -341,10 +299,6 @@ class Tree
|
|
341
299
|
return(filled)
|
342
300
|
end
|
343
301
|
|
344
|
-
#==============================================================================#
|
345
|
-
# find()
|
346
|
-
#==============================================================================#
|
347
|
-
|
348
302
|
#===Synopsis
|
349
303
|
# Find and return a CIDR from within the tree.
|
350
304
|
#
|
@@ -375,10 +329,6 @@ class Tree
|
|
375
329
|
return(me)
|
376
330
|
end
|
377
331
|
|
378
|
-
#==============================================================================#
|
379
|
-
# find_space()
|
380
|
-
#==============================================================================#
|
381
|
-
|
382
332
|
#===Synopsis
|
383
333
|
# Find subnets that are of at least size X. Only subnets that are not themselves
|
384
334
|
# subnetted will be returned. :Subnet takes precedence over :IPCount
|
@@ -446,10 +396,6 @@ class Tree
|
|
446
396
|
return(new_list)
|
447
397
|
end
|
448
398
|
|
449
|
-
#==============================================================================#
|
450
|
-
# longest_match()
|
451
|
-
#==============================================================================#
|
452
|
-
|
453
399
|
#===Synopsis
|
454
400
|
#Find the longest matching branch of our tree to which a
|
455
401
|
#CIDR address belongs. Useful for performing 'routing table' style lookups.
|
@@ -479,10 +425,6 @@ class Tree
|
|
479
425
|
return( NetAddr.cidr_build(found.version, found.to_i(:network), found.to_i(:netmask)) )
|
480
426
|
end
|
481
427
|
|
482
|
-
#==============================================================================#
|
483
|
-
# prune!()
|
484
|
-
#==============================================================================#
|
485
|
-
|
486
428
|
#===Synopsis
|
487
429
|
# Remove all subnets of the provided CIDR address.
|
488
430
|
#
|
@@ -518,10 +460,6 @@ class Tree
|
|
518
460
|
return(pruned)
|
519
461
|
end
|
520
462
|
|
521
|
-
#==============================================================================#
|
522
|
-
# remove!()
|
523
|
-
#==============================================================================#
|
524
|
-
|
525
463
|
#===Synopsis
|
526
464
|
# Remove the provided CIDR address, and all of its subnets from the tree.
|
527
465
|
#
|
@@ -559,10 +497,6 @@ class Tree
|
|
559
497
|
return(removed)
|
560
498
|
end
|
561
499
|
|
562
|
-
#==============================================================================#
|
563
|
-
# resize!()
|
564
|
-
#==============================================================================#
|
565
|
-
|
566
500
|
#===Synopsis
|
567
501
|
# Resize the provided CIDR address.
|
568
502
|
#
|
@@ -601,10 +535,6 @@ class Tree
|
|
601
535
|
return(resized)
|
602
536
|
end
|
603
537
|
|
604
|
-
#==============================================================================#
|
605
|
-
# root()
|
606
|
-
#==============================================================================#
|
607
|
-
|
608
538
|
#===Synopsis
|
609
539
|
# Returns the root of the provided CIDR address.
|
610
540
|
#
|
@@ -640,10 +570,6 @@ class Tree
|
|
640
570
|
return( NetAddr.cidr_build(parent.version, parent.to_i(:network), parent.to_i(:netmask)) )
|
641
571
|
end
|
642
572
|
|
643
|
-
#==============================================================================#
|
644
|
-
# show()
|
645
|
-
#==============================================================================#
|
646
|
-
|
647
573
|
#===Synopsis
|
648
574
|
# Print the tree as a formatted string.
|
649
575
|
#
|
@@ -692,10 +618,6 @@ class Tree
|
|
692
618
|
return(printed)
|
693
619
|
end
|
694
620
|
|
695
|
-
#==============================================================================#
|
696
|
-
# siblings()
|
697
|
-
#==============================================================================#
|
698
|
-
|
699
621
|
#===Synopsis
|
700
622
|
# Return list of the sibling CIDRs of the provided CIDR address.
|
701
623
|
#
|
@@ -729,10 +651,6 @@ class Tree
|
|
729
651
|
return(list)
|
730
652
|
end
|
731
653
|
|
732
|
-
#==============================================================================#
|
733
|
-
# summarize_subnets!()
|
734
|
-
#==============================================================================#
|
735
|
-
|
736
654
|
#===Synopsis
|
737
655
|
# Summarize all subnets of the provided CIDR address. The subnets will be
|
738
656
|
# placed under the new summary address within the tree.
|
@@ -771,10 +689,6 @@ class Tree
|
|
771
689
|
end
|
772
690
|
alias :merge_subnets! :summarize_subnets!
|
773
691
|
|
774
|
-
#==============================================================================#
|
775
|
-
# supernets()
|
776
|
-
#==============================================================================#
|
777
|
-
|
778
692
|
#===Synopsis
|
779
693
|
# Return list of the top-level supernets of this tree.
|
780
694
|
#
|
@@ -795,12 +709,9 @@ class Tree
|
|
795
709
|
end
|
796
710
|
|
797
711
|
|
798
|
-
# PRIVATE INSTANCE METHODS
|
799
|
-
private
|
800
712
|
|
801
|
-
|
802
|
-
|
803
|
-
#==============================================================================#
|
713
|
+
|
714
|
+
private
|
804
715
|
|
805
716
|
# Add NetStruct object to an array of NetStruct's
|
806
717
|
#
|
@@ -831,10 +742,6 @@ private
|
|
831
742
|
return(nil)
|
832
743
|
end
|
833
744
|
|
834
|
-
#==============================================================================#
|
835
|
-
# add_to_tree()
|
836
|
-
#==============================================================================#
|
837
|
-
|
838
745
|
# Add CIDR to a Tree
|
839
746
|
#
|
840
747
|
def add_to_tree(cidr,root=nil)
|
@@ -844,10 +751,6 @@ private
|
|
844
751
|
return(nil)
|
845
752
|
end
|
846
753
|
|
847
|
-
#==============================================================================#
|
848
|
-
# dump_children()
|
849
|
-
#==============================================================================#
|
850
|
-
|
851
754
|
# Dump contents of an Array of NetStruct objects
|
852
755
|
#
|
853
756
|
def dump_children(parent,depth=0)
|
@@ -864,10 +767,6 @@ private
|
|
864
767
|
return(list)
|
865
768
|
end
|
866
769
|
|
867
|
-
#==============================================================================#
|
868
|
-
# find_me()
|
869
|
-
#==============================================================================#
|
870
|
-
|
871
770
|
# Find the NetStruct to which a cidr belongs.
|
872
771
|
#
|
873
772
|
def find_me(cidr)
|
@@ -887,10 +786,6 @@ private
|
|
887
786
|
return(me)
|
888
787
|
end
|
889
788
|
|
890
|
-
#==============================================================================#
|
891
|
-
# find_parent()
|
892
|
-
#==============================================================================#
|
893
|
-
|
894
789
|
# Find the parent NetStruct to which a child NetStruct belongs.
|
895
790
|
#
|
896
791
|
def find_parent(cidr,parent=nil)
|
data/lib/validation_shortcuts.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
module NetAddr
|
2
2
|
private
|
3
3
|
|
4
|
-
#==============================================================================#
|
5
|
-
# validate_args()
|
6
|
-
#==============================================================================#
|
7
|
-
|
8
4
|
# validate options hash
|
9
5
|
#
|
10
6
|
def validate_args(to_validate,known_args)
|
@@ -15,10 +11,6 @@ def validate_args(to_validate,known_args)
|
|
15
11
|
end
|
16
12
|
module_function :validate_args
|
17
13
|
|
18
|
-
#==============================================================================#
|
19
|
-
# validate_ip_int()
|
20
|
-
#==============================================================================#
|
21
|
-
|
22
14
|
def validate_ip_int(ip,version)
|
23
15
|
version = 4 if (!version && ip < 2**32)
|
24
16
|
if (version == 4)
|
@@ -31,10 +23,6 @@ def validate_ip_int(ip,version)
|
|
31
23
|
end
|
32
24
|
module_function :validate_ip_int
|
33
25
|
|
34
|
-
#==============================================================================#
|
35
|
-
# validate_ip_str()
|
36
|
-
#==============================================================================#
|
37
|
-
|
38
26
|
def validate_ip_str(ip,version)
|
39
27
|
# check validity of charaters
|
40
28
|
if (ip =~ /[^0-9a-fA-F\.:]/)
|
@@ -129,9 +117,6 @@ def validate_ip_str(ip,version)
|
|
129
117
|
end
|
130
118
|
module_function :validate_ip_str
|
131
119
|
|
132
|
-
#==============================================================================#
|
133
|
-
# validate_netmask_int()
|
134
|
-
#==============================================================================#
|
135
120
|
def validate_netmask_int(netmask,version,is_int=false)
|
136
121
|
address_len = 32
|
137
122
|
address_len = 128 if (version == 6)
|
@@ -149,9 +134,6 @@ def validate_netmask_int(netmask,version,is_int=false)
|
|
149
134
|
end
|
150
135
|
module_function :validate_netmask_int
|
151
136
|
|
152
|
-
#==============================================================================#
|
153
|
-
# validate_netmask_str()
|
154
|
-
#==============================================================================#
|
155
137
|
def validate_netmask_str(netmask,version)
|
156
138
|
address_len = 32
|
157
139
|
address_len = 128 if (version == 6)
|
data/license
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright Dustin L. Spinhirne
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/{tests → test}/cidr_test.rb
RENAMED
data/{tests → test}/eui_test.rb
RENAMED
@@ -1,6 +1,6 @@
|
|
1
1
|
#!/usr/bin/ruby
|
2
2
|
|
3
|
-
|
3
|
+
require_relative "../lib/netaddr.rb"
|
4
4
|
require 'test/unit'
|
5
5
|
|
6
6
|
|
@@ -65,13 +65,13 @@ class TestEUI < Test::Unit::TestCase
|
|
65
65
|
|
66
66
|
def test_link_local
|
67
67
|
mac = NetAddr::EUI.create('aa-bb-cc-dd-ee-ff')
|
68
|
-
assert_equal('fe80:0000:0000:0000:
|
68
|
+
assert_equal('fe80:0000:0000:0000:a8bb:ccff:fedd:eeff', mac.link_local )
|
69
69
|
|
70
70
|
mac = NetAddr::EUI.create('1234.5678.9abc')
|
71
|
-
assert_equal('fe80:0000:0000:0000:
|
71
|
+
assert_equal('fe80:0000:0000:0000:1034:56ff:fe78:9abc', mac.link_local )
|
72
72
|
|
73
73
|
mac = NetAddr::EUI.create('1234.5678.9abc.def0')
|
74
|
-
assert_equal('fe80:0000:0000:0000:
|
74
|
+
assert_equal('fe80:0000:0000:0000:1034:5678:9abc:def0', mac.link_local(:Objectify => true).ip )
|
75
75
|
assert_raise(ArgumentError) {mac.link_local(:test => true)}
|
76
76
|
end
|
77
77
|
|
@@ -79,6 +79,23 @@ class TestEUI < Test::Unit::TestCase
|
|
79
79
|
mac = NetAddr::EUI.create('aa-bb-cc-dd-ee-ff')
|
80
80
|
assert_equal('aa-bb-cc-ff-fe-dd-ee-ff', mac.to_eui64.address )
|
81
81
|
|
82
|
+
# check that to_eui64 has no side effects
|
83
|
+
b = mac.to_eui64
|
84
|
+
c = mac.to_eui64
|
85
|
+
assert_equal(b.to_s, c.to_s)
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_to_ipv6
|
89
|
+
mac = NetAddr::EUI.create('aa-bb-cc-dd-ee-ff')
|
90
|
+
assert_equal('fe80:0000:0000:0000:a8bb:ccff:fedd:eeff', mac.to_ipv6('fe80::/64') )
|
91
|
+
|
92
|
+
mac = NetAddr::EUI.create('1234.5678.9abc')
|
93
|
+
assert_equal('fe80:0000:0000:0000:1034:56ff:fe78:9abc', mac.to_ipv6('fe80::/64') )
|
94
|
+
|
95
|
+
mac = NetAddr::EUI.create('1234.5678.9abc.def0')
|
96
|
+
assert_equal('fe80:0000:0000:0000:1034:5678:9abc:def0', mac.to_ipv6('fe80::/64', :Objectify => true).ip )
|
97
|
+
assert_raise(ArgumentError) {mac.link_local(:test => true)}
|
98
|
+
assert_raise(NetAddr::ValidationError) {mac.to_ipv6('fe80::/65')}
|
82
99
|
end
|
83
100
|
|
84
101
|
end
|