ipaddr_range_set 0.9.0 → 0.9.1
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.
- data/README.md +31 -23
- data/lib/ipaddr_range_set.rb +2 -1
- data/lib/ipaddr_range_set/version.rb +1 -1
- data/test/test_ip_addr_range_set.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -2,32 +2,35 @@ ipaddr_range_set
|
|
2
2
|
================
|
3
3
|
|
4
4
|
convenience class to create a set of possibly discontiguous IP address range
|
5
|
-
segments, and check if an IP address is in the set.
|
5
|
+
segments, and check if an IP address is in the set. ruby 1.9.3+ only.
|
6
6
|
|
7
|
-
Ruby stdlib IPAddr
|
8
|
-
|
9
|
-
|
7
|
+
Ruby stdlib [IPAddr](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/ipaddr/rdoc/IPAddr.html)
|
8
|
+
does the heavy-lifting, this is relatively simple code wrapping it
|
9
|
+
in a convenience class. But this can simplify your own code when used.
|
10
|
+
Basing logic on IP address range checking can often be the sign of a bad design,
|
11
|
+
but many of us have to do it anyway.
|
10
12
|
|
11
|
-
|
13
|
+
## Usage
|
12
14
|
|
13
15
|
require 'ipaddr_range_set'
|
14
16
|
|
15
|
-
# Zero or more segment arguments,
|
17
|
+
# Zero or more segment arguments, which can be input in a variety of
|
16
18
|
# of formats.
|
17
19
|
range = IPAddrRangeSet.new(
|
18
|
-
'220.1.10.3',
|
19
|
-
'2001:db8::10', #
|
20
|
-
'8.0.0.0/24', # IPv4 as CIDR, IPv6 CIDR too
|
20
|
+
'220.1.10.3', # a single IPv4 as a string
|
21
|
+
'2001:db8::10', # a single IPv6 as a string
|
22
|
+
'8.0.0.0/24', # IPv4 as CIDR, works for IPv6 CIDR too
|
21
23
|
'8.*.*.*', # informal splat notation, only for IPv4
|
22
|
-
'8.8.0.0'..'8.8.2.255', # arbitrary range
|
24
|
+
'8.8.0.0'..'8.8.2.255', # arbitrary range, works for IPv6 too.
|
23
25
|
IPAddr.new(whatever), # arbitrary existing IPAddr object
|
24
26
|
(ip_addr..ip_addr) # range of arbitrary IPAddr objects.
|
25
27
|
)
|
26
28
|
|
27
|
-
When ruby Range's are used,
|
29
|
+
When ruby Range's are used, IPAddrRangeSet makes sure to use `Range#cover?`
|
28
30
|
internally, not `Range#include?` (the latter being disastrous for anything that
|
29
|
-
doesn't have `#to_int`). Triple dot `...` exclusive ranges are
|
30
|
-
|
31
|
+
doesn't have `#to_int`). Triple dot `...` exclusive endpoint ranges are
|
32
|
+
supported, which can be convenient if you don't like writing lots of `255`s
|
33
|
+
in your range end points.
|
31
34
|
|
32
35
|
range.include? '220.1.10.5'
|
33
36
|
range.include? IPAddr.new('220.1.10.5')
|
@@ -38,7 +41,7 @@ IPAddrRangeSets are immutable, but you can create new ones combining existing
|
|
38
41
|
ranges:
|
39
42
|
|
40
43
|
new_range = IPAddrRangeSet('8.10.5.1') + IPAddrRangeSet('8.11.6.1')
|
41
|
-
new_range = IPAddrRangeSet('8.10.5.1').add('8.0.0.0/24')
|
44
|
+
new_range = IPAddrRangeSet('8.10.5.1').add('8.0.0.0/24', 10.0.0.1..10.1.4.255 )
|
42
45
|
|
43
46
|
The internal implementation just steps through all range segments and checks
|
44
47
|
the argument for inclusion, there's no special optimization to detect overlapping
|
@@ -50,17 +53,17 @@ As above range 'union' is supported, but range intersection is not. It's
|
|
50
53
|
a bit tricky to implement well, and I don't have a use case for it.
|
51
54
|
|
52
55
|
Built-in constants are available for local (private, not publically routable)
|
53
|
-
and loopback ranges in both IPv4 IPv6. IPv4Local
|
54
|
-
IPv6Loopback
|
56
|
+
and loopback ranges in both IPv4 and IPv6. `IPAddrRangeSet::IPv4Local`, `IPv4Loopback`, `IPv6Local`,
|
57
|
+
`IPv6Loopback`. The constant `LocalAddresses` is the union of all v4 and v6 local
|
55
58
|
and loopback addresses.
|
56
59
|
|
57
|
-
IPAddrRangeSet::
|
58
|
-
IPAddrRangeSet::
|
59
|
-
IPAddrRangeSet::
|
60
|
-
IPAddrRangeSet::
|
61
|
-
IPAddrRangeSet::
|
60
|
+
IPAddrRangeSet::LocalAddresses.include? "127.0.0.1" # true
|
61
|
+
IPAddrRangeSet::LocalAddresses.include? "10.0.0.1" # true
|
62
|
+
IPAddrRangeSet::LocalAddresses.include? "192.168.0.1" # true
|
63
|
+
IPAddrRangeSet::LocalAddresses.include? "::1" # true, ipv6 loopback
|
64
|
+
IPAddrRangeSet::LocalAddresses.include? "fc00::1" # an ipv6 local
|
62
65
|
|
63
|
-
|
66
|
+
## Note on ipv6
|
64
67
|
|
65
68
|
It supports ipv6 just because it was so easy to do so with the underlying
|
66
69
|
IPAddr implementation. But I don't have much experience or use for IPv6, there
|
@@ -69,4 +72,9 @@ could be oddities hiding in there.
|
|
69
72
|
You can create an IPAddrRangeSet that includes both IPv4 and IPv6 segments, no
|
70
73
|
problem. But an individual `include?` argument will only match a segment of
|
71
74
|
it's own type, no automatic conversion of IPv4-compatible IPv6 addresses
|
72
|
-
is done (should it be? I have no idea, don't really understand ipv6 use cases).
|
75
|
+
is done (should it be? I have no idea, don't really understand ipv6 use cases).
|
76
|
+
|
77
|
+
## thanks
|
78
|
+
|
79
|
+
to whoever wrote IPAddr in ruby stdlib, nice to have it, this is just a
|
80
|
+
convenience wrapper over it, really.
|
data/lib/ipaddr_range_set.rb
CHANGED
@@ -110,10 +110,11 @@ end
|
|
110
110
|
class IPAddrRangeSet
|
111
111
|
# Constant ranges for local/non-routable/private addresses
|
112
112
|
IPv4Local = IPAddrRangeSet.new("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16")
|
113
|
+
# who knew it's not just 127.0.0.1? Check the spec, it's this:
|
113
114
|
IPv4Loopback = IPAddrRangeSet.new("127.0.0.0/8")
|
114
115
|
|
115
116
|
IPv6Local = IPAddrRangeSet.new("fc00::/7")
|
116
117
|
IPv6Loopback = IPAddrRangeSet.new("::1")
|
117
118
|
|
118
|
-
LocalAddresses = IPv4Local +
|
119
|
+
LocalAddresses = IPv4Local + IPv4Loopback + IPv6Local + IPv6Loopback
|
119
120
|
end
|
@@ -121,7 +121,7 @@ class TestIpAddrRange < Test::Unit::TestCase
|
|
121
121
|
end
|
122
122
|
|
123
123
|
def test_local_constants
|
124
|
-
%w{10.3.3.1 172.16.4.1 192.168.2.1 fc00::1}.each do |ip|
|
124
|
+
%w{10.3.3.1 172.16.4.1 192.168.2.1 fc00::1 127.0.0.1 ::1}.each do |ip|
|
125
125
|
assert IPAddrRangeSet::LocalAddresses.include?(ip), "IPAddrRangeSet::LocalAddresses should include #{ip}"
|
126
126
|
end
|
127
127
|
|