ip 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ip.rb +64 -0
  2. metadata +2 -2
data/lib/ip.rb CHANGED
@@ -25,6 +25,30 @@
25
25
  # perhaps a distant, future release. Any patches that can correct this
26
26
  # issue are most welcome.
27
27
  #
28
+ # ================================================================
29
+ #
30
+ # The compilation of software known as ip.rb is distributed under the
31
+ # following terms:
32
+ # Copyright (C) 2005-2006 Erik Hollensbe. All rights reserved.
33
+ #
34
+ # Redistribution and use in source form, with or without
35
+ # modification, are permitted provided that the following conditions
36
+ # are met:
37
+ # 1. Redistributions of source code must retain the above copyright
38
+ # notice, this list of conditions and the following disclaimer.
39
+ #
40
+ # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
41
+ # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
+ # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
44
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
+ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
+ # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
+ # SUCH DAMAGE.
51
+ #
28
52
 
29
53
  class IP
30
54
 
@@ -158,7 +182,47 @@ class IP
158
182
  lower = rawip & rawnm
159
183
  return IP::Range[IP::Address.unpack(lower), IP::Address.unpack(upper)]
160
184
  end
185
+
186
+ #
187
+ # This returns the first ip address of the cidr as an IP::Address object.
188
+ #
189
+ def first_ip
190
+ rawip = IP::Address.pack(@ip)
191
+ rawnm = 0xFFFFFFFF << (32 - @mask)
192
+ lower = rawip & rawnm
193
+ return IP::Address.unpack(lower)
194
+ end
195
+
196
+ #
197
+ # This returns the last ip address of the cidr as an IP::Address object.
198
+ #
199
+ def last_ip
200
+ rawip = IP::Address.pack(@ip)
201
+ rawnm = 0xFFFFFFFF << (32 - @mask)
202
+ upper = rawip | ~rawnm
203
+ return IP::Address.unpack(upper)
204
+ end
205
+
206
+ #
207
+ # This will take another IP::CIDR object as an argument and check to see
208
+ # if it overlaps with this cidr object. Returns true/false on overlap.
209
+ #
210
+ # This also throws a TypeError if passed invalid data.
211
+ #
212
+ def overlaps?(other_cidr)
213
+ fail TypeError.new("Expected object of type IP::Address") unless(other_cidr.kind_of?(IP::CIDR))
214
+
215
+ myfirst = IP::Address.pack(self.first_ip)
216
+ mylast = IP::Address.pack(self.last_ip)
161
217
 
218
+ otherfirst = IP::Address.pack(other_cidr.first_ip)
219
+ otherlast = IP::Address.pack(other_cidr.last_ip)
220
+
221
+ return ((myfirst >= otherfirst && myfirst <= otherlast) ||
222
+ (mylast <= otherlast && mylast >= otherfirst) ||
223
+ (otherfirst >= myfirst && otherfirst <= mylast)) ? true : false;
224
+ end
225
+
162
226
  end
163
227
 
164
228
  #
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: ip
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2005-11-13 00:00:00 -08:00
6
+ version: 0.0.3
7
+ date: 2006-01-03 00:00:00 -08:00
8
8
  summary: "Ruby classes to work with IP address, ranges, and netmasks"
9
9
  require_paths:
10
10
  - lib