netaddr 1.5.1 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of netaddr might be problematic. Click here for more details.

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