netaddr 1.5.3 → 2.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +201 -0
- data/README.md +28 -8
- data/lib/eui48.rb +65 -0
- data/lib/eui64.rb +77 -0
- data/lib/ipv4.rb +87 -0
- data/lib/ipv4net.rb +262 -0
- data/lib/ipv6.rb +146 -0
- data/lib/ipv6net.rb +267 -0
- data/lib/mask128.rb +70 -0
- data/lib/mask32.rb +105 -0
- data/lib/netaddr.rb +122 -20
- data/lib/util.rb +373 -0
- data/test/eui48_test.rb +30 -0
- data/test/eui64_test.rb +32 -0
- data/test/examples.rb +155 -0
- data/test/ipv4_test.rb +60 -0
- data/test/ipv4net_test.rb +206 -0
- data/test/ipv6_test.rb +135 -0
- data/test/ipv6net_test.rb +199 -0
- data/test/mask128_test.rb +45 -0
- data/test/mask32_test.rb +56 -0
- data/test/netaddr_test.rb +149 -0
- data/test/run_all.rb +10 -0
- metadata +25 -20
- data/Errors +0 -7
- data/changelog +0 -52
- data/lib/cidr.rb +0 -2014
- data/lib/cidr_shortcuts.rb +0 -401
- data/lib/eui.rb +0 -402
- data/lib/ip_math.rb +0 -227
- data/lib/methods.rb +0 -1013
- data/lib/tree.rb +0 -816
- data/lib/validation_shortcuts.rb +0 -201
- data/license +0 -13
- data/test/cidr_test.rb +0 -545
- data/test/eui_test.rb +0 -101
- data/test/methods_test.rb +0 -331
- data/test/tree_test.rb +0 -347
data/lib/validation_shortcuts.rb
DELETED
@@ -1,201 +0,0 @@
|
|
1
|
-
module NetAddr
|
2
|
-
private
|
3
|
-
|
4
|
-
# validate options hash
|
5
|
-
#
|
6
|
-
def validate_args(to_validate,known_args)
|
7
|
-
to_validate.each do |x|
|
8
|
-
raise ArgumentError, "Unrecognized argument #{x}. Valid arguments are " +
|
9
|
-
"#{known_args.join(',')}" if (!known_args.include?(x))
|
10
|
-
end
|
11
|
-
end
|
12
|
-
module_function :validate_args
|
13
|
-
|
14
|
-
def validate_ip_int(ip,version)
|
15
|
-
version = 4 if (!version && ip < 2**32)
|
16
|
-
if (version == 4)
|
17
|
-
raise ValidationError, "#{ip} is invalid for IPv4 (Integer is out of bounds)." if ( (ip < 0) || (ip > 2**32-1) )
|
18
|
-
else
|
19
|
-
raise ValidationError, "#{ip} is invalid for both IPv4 and IPv6 (Integer is out of bounds)." if ( (ip < 0) || (ip > 2**128-1) )
|
20
|
-
version = 6
|
21
|
-
end
|
22
|
-
return(version)
|
23
|
-
end
|
24
|
-
module_function :validate_ip_int
|
25
|
-
|
26
|
-
def validate_ip_str(ip,version)
|
27
|
-
# check validity of charaters
|
28
|
-
if (ip =~ /[^0-9a-fA-F\.:]/)
|
29
|
-
raise ValidationError, "#{ip} is invalid (contains invalid characters)."
|
30
|
-
end
|
31
|
-
|
32
|
-
if (version == 4)
|
33
|
-
octets = ip.split('.')
|
34
|
-
raise ValidationError, "#{ip} is invalid (IPv4 requires (4) octets)." if (octets.length != 4)
|
35
|
-
|
36
|
-
# are octets in range 0..255?
|
37
|
-
octets.each do |octet|
|
38
|
-
raise ValidationError, "#{ip} is invalid (IPv4 dotted-decimal format " +
|
39
|
-
"should not contain non-numeric characters)." if (octet =~ /[\D]/ || octet == '')
|
40
|
-
octet = octet.to_i()
|
41
|
-
if ( (octet < 0) || (octet >= 256) )
|
42
|
-
raise ValidationError, "#{ip} is invalid (IPv4 octets should be between 0 and 255)."
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
else
|
47
|
-
# make sure we only have at most (2) colons in a row, and then only
|
48
|
-
# (1) instance of that
|
49
|
-
if ( (ip =~ /:{3,}/) || (ip.split("::").length > 2) )
|
50
|
-
raise ValidationError, "#{ip} is invalid (IPv6 field separators (:) are bad)."
|
51
|
-
end
|
52
|
-
|
53
|
-
# set flags
|
54
|
-
shorthand = false
|
55
|
-
if (ip =~ /\./)
|
56
|
-
dotted_dec = true
|
57
|
-
else
|
58
|
-
dotted_dec = false
|
59
|
-
end
|
60
|
-
|
61
|
-
# split up by ':'
|
62
|
-
fields = []
|
63
|
-
if (ip =~ /::/)
|
64
|
-
shorthand = true
|
65
|
-
ip.split('::').each do |x|
|
66
|
-
fields.concat( x.split(':') )
|
67
|
-
end
|
68
|
-
else
|
69
|
-
fields.concat( ip.split(':') )
|
70
|
-
end
|
71
|
-
|
72
|
-
# make sure we have the correct number of fields
|
73
|
-
if (shorthand)
|
74
|
-
if ( (dotted_dec && fields.length > 6) || (!dotted_dec && fields.length > 7) )
|
75
|
-
raise ValidationError, "#{ip} is invalid (IPv6 shorthand notation has " +
|
76
|
-
"incorrect number of fields)."
|
77
|
-
end
|
78
|
-
else
|
79
|
-
if ( (dotted_dec && fields.length != 7 ) || (!dotted_dec && fields.length != 8) )
|
80
|
-
raise ValidationError, "#{ip} is invalid (IPv6 address has " +
|
81
|
-
"incorrect number of fields)."
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
# if dotted_dec then validate the last field
|
86
|
-
if (dotted_dec)
|
87
|
-
dotted = fields.pop()
|
88
|
-
octets = dotted.split('.')
|
89
|
-
raise ValidationError, "#{ip} is invalid (Legacy IPv4 portion of IPv6 " +
|
90
|
-
"address should contain (4) octets)." if (octets.length != 4)
|
91
|
-
octets.each do |x|
|
92
|
-
raise ValidationError, "#{ip} is invalid (egacy IPv4 portion of IPv6 " +
|
93
|
-
"address should not contain non-numeric characters)." if (x =~ /[^0-9]/ )
|
94
|
-
x = x.to_i
|
95
|
-
if ( (x < 0) || (x >= 256) )
|
96
|
-
raise ValidationError, "#{ip} is invalid (Octets of a legacy IPv4 portion of IPv6 " +
|
97
|
-
"address should be between 0 and 255)."
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
# validate hex fields
|
103
|
-
fields.each do |x|
|
104
|
-
if (x =~ /[^0-9a-fA-F]/)
|
105
|
-
raise ValidationError, "#{ip} is invalid (IPv6 address contains invalid hex characters)."
|
106
|
-
else
|
107
|
-
x = x.to_i(16)
|
108
|
-
if ( (x < 0) || (x >= 2**16) )
|
109
|
-
raise ValidationError, "#{ip} is invalid (Fields of an IPv6 address " +
|
110
|
-
"should be between 0x0 and 0xFFFF)."
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
116
|
-
return(true)
|
117
|
-
end
|
118
|
-
module_function :validate_ip_str
|
119
|
-
|
120
|
-
def validate_netmask_int(netmask,version,is_int=false)
|
121
|
-
address_len = 32
|
122
|
-
address_len = 128 if (version == 6)
|
123
|
-
|
124
|
-
if (!is_int)
|
125
|
-
if (netmask > address_len || netmask < 0 )
|
126
|
-
raise ValidationError, "Netmask, #{netmask}, is out of bounds for IPv#{version}."
|
127
|
-
end
|
128
|
-
else
|
129
|
-
if (netmask >= 2**address_len || netmask < 0 )
|
130
|
-
raise ValidationError, "netmask (#{netmask}) is out of bounds for IPv#{version}."
|
131
|
-
end
|
132
|
-
end
|
133
|
-
return(true)
|
134
|
-
end
|
135
|
-
module_function :validate_netmask_int
|
136
|
-
|
137
|
-
def validate_netmask_str(netmask,version)
|
138
|
-
address_len = 32
|
139
|
-
address_len = 128 if (version == 6)
|
140
|
-
|
141
|
-
if(netmask =~ /\./) # extended netmask
|
142
|
-
all_f = 2**32-1
|
143
|
-
netmask_int = 0
|
144
|
-
|
145
|
-
# validate & pack extended mask
|
146
|
-
begin
|
147
|
-
netmask_int = NetAddr.ip_to_i(netmask, :Version => 4)
|
148
|
-
rescue Exception => error
|
149
|
-
raise ValidationError, "#{netmask} is improperly formed: #{error}"
|
150
|
-
end
|
151
|
-
|
152
|
-
# cycle through the bits of hostmask and compare
|
153
|
-
# with netmask_int. when we hit the firt '1' within
|
154
|
-
# netmask_int (our netmask boundary), xor hostmask and
|
155
|
-
# netmask_int. the result should be all 1's. this whole
|
156
|
-
# process is in place to make sure that we dont have
|
157
|
-
# and crazy masks such as 255.254.255.0
|
158
|
-
hostmask = 1
|
159
|
-
32.times do
|
160
|
-
check = netmask_int & hostmask
|
161
|
-
if ( check != 0)
|
162
|
-
hostmask = hostmask >> 1
|
163
|
-
unless ( (netmask_int ^ hostmask) == all_f)
|
164
|
-
raise ValidationError, "#{netmask} contains '1' bits within the host portion of the netmask."
|
165
|
-
end
|
166
|
-
break
|
167
|
-
else
|
168
|
-
hostmask = hostmask << 1
|
169
|
-
hostmask = hostmask | 1
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
else # cidr format
|
174
|
-
# remove '/' if present
|
175
|
-
if (netmask =~ /^\// )
|
176
|
-
netmask[0] = " "
|
177
|
-
netmask.lstrip!
|
178
|
-
end
|
179
|
-
|
180
|
-
# check if we have any non numeric characters
|
181
|
-
if (netmask =~ /\D/)
|
182
|
-
raise ValidationError, "#{netmask} contains invalid characters."
|
183
|
-
end
|
184
|
-
|
185
|
-
netmask = netmask.to_i
|
186
|
-
if (netmask > address_len || netmask < 0 )
|
187
|
-
raise ValidationError, "Netmask, #{netmask}, is out of bounds for IPv#{version}."
|
188
|
-
end
|
189
|
-
|
190
|
-
end
|
191
|
-
return(true)
|
192
|
-
end
|
193
|
-
module_function :validate_netmask_str
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
end # module NetAddr
|
198
|
-
|
199
|
-
__END__
|
200
|
-
|
201
|
-
|
data/license
DELETED
@@ -1,13 +0,0 @@
|
|
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.
|