ipadmin 0.1.3 → 0.1.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/ip_admin.rb +201 -7
  2. data/tests/cidr_test.rb +4 -0
  3. metadata +2 -2
@@ -198,8 +198,7 @@ module_function :compare
198
198
  #
199
199
  def compare_cidr(cidr1,cidr2)
200
200
 
201
- puts "compare_cidr() DEPRICATED, DO NOT USE - this will be removed in future releases."
202
-
201
+
203
202
  # we only accept CIDR objects
204
203
  unless ( (cidr1.kind_of? IPAdmin::CIDR)&&(cidr2.kind_of? IPAdmin::CIDR) )
205
204
  raise ArgumentError, "Arguments shouldbe of type IPAdmin::CIDR."
@@ -264,7 +263,6 @@ module_function :compare_cidr
264
263
  #
265
264
  def merge_cidr(cidr_list)
266
265
 
267
- puts "merge_cidr() DEPRICATED, DO NOT USE - this will be removed in future releases."
268
266
  # make sure we have an array with at least 2 objects
269
267
  unless ( (cidr_list.kind_of? Array) && (cidr_list.length > 1) )
270
268
  raise ArgumentError, "Array of at least two IPAdmin::CIDR objects required."
@@ -1326,6 +1324,7 @@ class CIDR
1326
1324
 
1327
1325
  if (network =~/\./) # assume IPv4
1328
1326
  @version = 4
1327
+ @all_f = 2**32 - 1
1329
1328
 
1330
1329
  if (network =~ /\//)
1331
1330
  ip,netmask = network.split(/\//)
@@ -1358,6 +1357,7 @@ class CIDR
1358
1357
 
1359
1358
  elsif (network =~/:/) # assume IPv6
1360
1359
  @version = 6
1360
+ @all_f = 2**128 - 1
1361
1361
 
1362
1362
  if (network =~ /\//)
1363
1363
  ip,netmask = network.split(/\//)
@@ -1538,6 +1538,39 @@ class CIDR
1538
1538
  #=====================================#
1539
1539
 
1540
1540
 
1541
+ #==============================================================================#
1542
+ # desc_short()
1543
+ #==============================================================================#
1544
+
1545
+ # Displays the IPv6 network/netmask of this object in IPv6 short-hand
1546
+ # notation. This method does not work for IPv4 objects.
1547
+ #
1548
+ # - Arguments:
1549
+ # * none
1550
+ #
1551
+ # - Returns:
1552
+ # * Description in network/netmask format.
1553
+ #
1554
+ # Example:
1555
+ # desc = cidr.desc_short()
1556
+ #
1557
+ def desc_short()
1558
+ if (@version == 4)
1559
+ raise "desc_short() is not supported for IPv4 objects."
1560
+ end
1561
+
1562
+ ip = IPAdmin.unpack_ipv6_addr(@network)
1563
+ ip = IPAdmin.shorten(ip)
1564
+ mask = IPAdmin.unpack_ipv6_netmask(@netmask)
1565
+
1566
+ return("#{ip}/#{mask}")
1567
+
1568
+ end
1569
+
1570
+ #======================================#
1571
+ #
1572
+ #======================================#
1573
+
1541
1574
 
1542
1575
  #============================================================================#
1543
1576
  # enumerate()
@@ -1688,6 +1721,57 @@ class CIDR
1688
1721
  #=====================================#
1689
1722
 
1690
1723
 
1724
+ #==============================================================================#
1725
+ # last()
1726
+ #==============================================================================#
1727
+
1728
+ # Provide last IP address in this CIDR object. The broadcast() method is
1729
+ # aliased to this method, and thus works for IPv6 despite the fact that the
1730
+ # IPv6 protocol does not support IP broadcasting.
1731
+ #
1732
+ # - Arguments:
1733
+ # * Optional Hash with the following fields:
1734
+ # - :Objectify -- return IPAdmin::CIDR object (optional)
1735
+ #
1736
+ # - Returns:
1737
+ # * IP address or IPAdmin::CIDR object.
1738
+ #
1739
+ # Example:
1740
+ # puts cidr.last()
1741
+ #
1742
+ def last(options=nil)
1743
+ objectify = nil
1744
+
1745
+ if (options)
1746
+ unless(options.kind_of?(Hash))
1747
+ raise Argumenterror, "Expected Hash, but " +
1748
+ "#{options.class} provided."
1749
+ end
1750
+ objectify = options[:Objectify] if (options.has_key?(:Objectify) &&
1751
+ options[:Objectify] != nil)
1752
+ end
1753
+
1754
+ packed_ip = @network | @hostmask
1755
+ if (@version == 4)
1756
+ ip = IPAdmin.unpack_ipv4_addr(packed_ip)
1757
+ else
1758
+ ip = IPAdmin.unpack_ipv6_addr(packed_ip)
1759
+ end
1760
+
1761
+ if (objectify)
1762
+ ip = IPAdmin::CIDR.new(:CIDR => ip)
1763
+ end
1764
+
1765
+ return(ip)
1766
+ end
1767
+
1768
+ alias :broadcast :last
1769
+
1770
+ #======================================#
1771
+ #
1772
+ #======================================#
1773
+
1774
+
1691
1775
 
1692
1776
  #============================================================================#
1693
1777
  # netmask()
@@ -1776,12 +1860,124 @@ class CIDR
1776
1860
 
1777
1861
  return(formatted)
1778
1862
  end
1863
+
1864
+ alias :base :network
1865
+ alias :first :network
1779
1866
 
1780
1867
  #=====================================#
1781
1868
  #
1782
1869
  #=====================================#
1783
1870
 
1784
1871
 
1872
+ #==============================================================================#
1873
+ # next_ip()
1874
+ #==============================================================================#
1875
+
1876
+ # Provide the next IP following the last available IP within this
1877
+ # CIDR object.
1878
+ #
1879
+ # - Arguments:
1880
+ # * Optional Hash with the following fields:
1881
+ # - :Bitstep -- step in X sized steps (optional)
1882
+ # - :Objectify -- return IPAdmin::CIDR object (optional)
1883
+ #
1884
+ # - Returns:
1885
+ # * IP address or IPAdmin::CIDR object.
1886
+ #
1887
+ # Example:
1888
+ # puts cidr.next_ip()
1889
+ #
1890
+ def next_ip(options=nil)
1891
+ bitstep = 1
1892
+ objectify = nil
1893
+
1894
+ if (options)
1895
+ unless(options.kind_of?(Hash))
1896
+ raise Argumenterror, "Expected Hash, but " +
1897
+ "#{options.class} provided."
1898
+ end
1899
+ bitstep = options[:Bitstep] if (options.has_key?(:Bitstep))
1900
+ objectify = options[:Objectify] if (options.has_key?(:Objectify) &&
1901
+ options[:Objectify] != nil)
1902
+ end
1903
+
1904
+ next_ip = @network + @hostmask + bitstep
1905
+
1906
+ unless (next_ip <= @all_f)
1907
+ raise "Returned IP is out of bounds for IPv#{@version}."
1908
+ end
1909
+
1910
+ next_ip = IPAdmin.unpack_ipv4_addr(next_ip) if (@version == 4)
1911
+ next_ip = IPAdmin.unpack_ipv6_addr(next_ip) if (@version == 6)
1912
+ if (objectify)
1913
+ next_ip = IPAdmin::CIDR.new(:CIDR => next_ip)
1914
+ end
1915
+
1916
+ return(next_ip)
1917
+ end
1918
+
1919
+ #======================================#
1920
+ #
1921
+ #======================================#
1922
+
1923
+
1924
+ #==============================================================================#
1925
+ # next_subnet()
1926
+ #==============================================================================#
1927
+
1928
+ # Provide the next subnet following this CIDR object. The next subnet will
1929
+ # be of the same size as the current CIDR object.
1930
+ #
1931
+ # - Arguments:
1932
+ # * Optional Hash with the following fields:
1933
+ # - :Bitstep -- step in X sized steps. (optional)
1934
+ # - :Objectify -- return IPAdmin::CIDR object (optional)
1935
+ #
1936
+ # - Returns:
1937
+ # * CIDR address or IPAdmin::CIDR object.
1938
+ #
1939
+ # Example:
1940
+ # puts cidr.next_subnet()
1941
+ #
1942
+ def next_subnet(options=nil)
1943
+ bitstep = 1
1944
+ objectify = nil
1945
+
1946
+ if (options)
1947
+ unless(options.kind_of?(Hash))
1948
+ raise Argumenterror, "Expected Hash, but " +
1949
+ "#{options.class} provided."
1950
+ end
1951
+ bitstep = options[:Bitstep] if (options.has_key?(:Bitstep))
1952
+ objectify = options[:Objectify] if (options.has_key?(:Objectify) &&
1953
+ options[:Objectify] != nil)
1954
+ end
1955
+
1956
+ maxbits = 32 if (@version == 4)
1957
+ maxbits = 128 if (@version ==6)
1958
+ bitstep = bitstep * (2**(maxbits - self.bits) )
1959
+ next_sub = @network + bitstep
1960
+
1961
+ unless (next_sub <= @all_f)
1962
+ raise "Returned subnet is out of bounds for IPv#{@version}."
1963
+ end
1964
+
1965
+ next_sub = IPAdmin.unpack_ipv4_addr(next_sub) if (@version == 4)
1966
+ next_sub = IPAdmin.unpack_ipv6_addr(next_sub) if (@version == 6)
1967
+ next_sub = next_sub << "/" << self.bits.to_s
1968
+
1969
+ if (objectify)
1970
+ next_sub = IPAdmin::CIDR.new(:CIDR => next_sub)
1971
+ end
1972
+
1973
+ return(next_sub)
1974
+ end
1975
+
1976
+ #======================================#
1977
+ #
1978
+ #======================================#
1979
+
1980
+
1785
1981
 
1786
1982
  #============================================================================#
1787
1983
  # nth()
@@ -1940,7 +2136,6 @@ class CIDR
1940
2136
  #=====================================#
1941
2137
 
1942
2138
 
1943
-
1944
2139
  #============================================================================#
1945
2140
  # size()
1946
2141
  #============================================================================#
@@ -2131,7 +2326,6 @@ class CIDRTable
2131
2326
  #
2132
2327
  def initialize(version)
2133
2328
 
2134
- puts "DEPRICATED, DO NOT USE - this will be removed in future releases."
2135
2329
  unless ( version.kind_of? Fixnum )
2136
2330
  raise ArgumentError, "Expected Fixnum, but #{version.class} provided."
2137
2331
  end
@@ -2472,8 +2666,8 @@ end
2472
2666
  #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
2473
2667
 
2474
2668
  =begin rdoc
2475
- A class & series of methods for creating and manipulating IP
2476
- addresses. Both IPv4 and IPv6 are supported.
2669
+ DEPRICATED, DO NOT USE - this will be removed in future releases, and all
2670
+ functionality moved to IPAdmin::CIDR
2477
2671
  =end
2478
2672
 
2479
2673
  class IPAddr
@@ -29,9 +29,13 @@ class TestCIDR < Test::Unit::TestCase
29
29
  assert_equal(1,cidr4.contains(cidr4_2) )
30
30
  assert_equal(1,cidr6.contains(cidr6_2) )
31
31
 
32
+ assert_equal('192.168.1.255',cidr4.last() )
33
+
32
34
  assert_equal('192.168.1.0/24',cidr4.desc() )
33
35
  assert_equal('fec0:0000:0000:0000:0000:0000:0000:0000/64',cidr6.desc() )
34
36
 
37
+ assert_equal('fec0::/64',cidr6.desc_short() )
38
+
35
39
  assert_equal('0.0.0.255',cidr4.hostmask_ext() )
36
40
 
37
41
  assert_equal(24,cidr4.bits() )
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: ipadmin
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.3
7
- date: 2006-02-10 00:00:00 -06:00
6
+ version: 0.1.3.1
7
+ date: 2006-02-15 00:00:00 -06:00
8
8
  summary: A package for manipulating IPv4/IPv6 address space.
9
9
  require_paths:
10
10
  - lib