Object
Copyright (C) 2009-2010 Brian Candler <www.deploy2.net/> Licensed under the same terms as ruby. See LICENCE.txt and COPYING.txt
Copyright (C) 2009-2010 Brian Candler <www.deploy2.net/> Licensed under the same terms as ruby. See LICENCE.txt and COPYING.txt
Create an instance from an alternative array format:
[context, protocol, address, prefix_length]
# File lib/ip/cpal.rb, line 7 def self.from_cpal(cpal) new([cpal[1], cpal[2], cpal[3], cpal[0]]) end
Examples:
IP.new("1.2.3.4")
IP.new("1.2.3.4/28")
IP.new("1.2.3.4/28@routing_context")
Array form (inverse of to_a and to_ah):
IP.new(["v4", 0x01020304]) IP.new(["v4", 0x01020304, 28]) IP.new(["v4", 0x01020304, 28, "routing_context"]) IP.new(["v4", "01020304", 28, "routing_context"])
Note that this returns an instance of IP::V4 or IP::V6. IP is the base class of both of those, but cannot be instantiated itself.
# File lib/ip/base.rb, line 22 def new(src) case src when String parse(src) || (raise ArgumentError, "invalid address") when Array (PROTO_TO_CLASS[src[0]] || (raise ArgumentError, "invalid protocol")).new(*src[1..-1]) when IP src.dup else raise ArgumentError, "invalid address" end end
Examples:
IP::V4.new(0x01020304)
IP::V4.new("01020304")
IP::V4.new(0x01020304, 28)
IP::V4.new(0x01020304, 28, "routing_context")
# File lib/ip/base.rb, line 52 def initialize(addr, pfxlen=nil, ctx=nil) @addr = addr.is_a?(String) ? addr.to_i(16) : addr.to_i raise ArgumentError, "Invalid address value" if @addr < 0 || @addr > self.class::MASK self.pfxlen = pfxlen self.ctx = ctx end
Parse a string as an IP address - return a V4/V6 object or nil
# File lib/ip/base.rb, line 36 def parse(str) V4.parse(str) || V6.parse(str) end
# File lib/ip/base.rb, line 205 def &(other) self.class.new(@addr & other.to_int, @pfxlen, @ctx) end
# File lib/ip/base.rb, line 197 def +(other) self.class.new(@addr + other.to_int, @pfxlen, @ctx) end
# File lib/ip/base.rb, line 201 def -(other) self.class.new(@addr - other.to_int, @pfxlen, @ctx) end
# File lib/ip/base.rb, line 259 def <=>(other) to_a <=> other.to_a end
# File lib/ip/base.rb, line 213 def ^(other) self.class.new(@addr ^ other.to_int, @pfxlen, @ctx) end
Return the address family, Socket::AF_INET or Socket::AF_INET6
# File lib/ip/socket.rb, line 8 def af self.class::AF end
Return a new IP object at the top of the subnet, with an optional offset applied.
IP.new("1.2.3.4/24").broadcast => #<IP::V4 1.2.3.255/24>
IP.new("1.2.3.4/24").broadcast(-1) => #<IP::V4 1.2.3.254/24>
# File lib/ip/base.rb, line 134 def broadcast(offset=0) self.class.new((@addr | mask) + offset, @pfxlen, @ctx) end
# File lib/ip/base.rb, line 255 def eql?(other) to_a.eql?(other.to_a) end
# File lib/ip/base.rb, line 230 def inspect res = "#<#{self.class} #{to_s}>" end
Return the mask for this pfxlen as an integer. For example, a V4 /24 address has a mask of 255 (0x000000ff)
# File lib/ip/base.rb, line 118 def mask @mask ||= (1 << (self.class::ADDR_BITS - @pfxlen)) - 1 end
Masks the address such that it is the base of the subnet
IP.new("1.2.3.4/24").mask! => #<IP::V4 1.2.3.0/24>
# File lib/ip/base.rb, line 152 def mask! @addr &= ~mask self end
Return a new IP object representing the netmask
IP.new("1.2.3.4/24").netmask => #<IP::V4 255.255.255.0>
# File lib/ip/base.rb, line 140 def netmask self.class.new(self.class::MASK & ~mask) end
Return a new IP object at the base of the subnet, with an optional offset applied.
IP.new("1.2.3.4/24").network => #<IP::V4 1.2.3.0/24>
IP.new("1.2.3.4/24").network(7) => #<IP::V4 1.2.3.7/24>
# File lib/ip/base.rb, line 126 def network(offset=0) self.class.new((@addr & ~mask) + offset, @pfxlen, @ctx) end
Returns offset from base of subnet to this address
IP.new("1.2.3.4/24").offset => 4
# File lib/ip/base.rb, line 166 def offset @addr - (@addr & ~mask) end
Returns true if this is not the base address of the subnet implied from the prefix length (e.g. 1.2.3.4/24 is offset, because the base is 1.2.3.0/24)
# File lib/ip/base.rb, line 160 def offset? @addr != (@addr & ~mask) end
Change the prefix length. If nil, the maximum is used (32 or 128)
# File lib/ip/base.rb, line 105 def pfxlen=(pfxlen) @mask = nil if pfxlen pfxlen = pfxlen.to_i raise ArgumentError, "Invalid prefix length" if pfxlen < 0 || pfxlen > self.class::ADDR_BITS @pfxlen = pfxlen else @pfxlen = self.class::ADDR_BITS end end
Return the protocol in string form, “v4” or “v6”
# File lib/ip/base.rb, line 60 def proto self.class::PROTO end
If the address is not on the base, turn it into a single IP.
IP.new("1.2.3.4/24").reset_pfxlen! => <IP::V4 1.2.3.4>
IP.new("1.2.3.0/24").reset_pfxlen! => <IP::V4 1.2.3.0/24>
# File lib/ip/base.rb, line 173 def reset_pfxlen! self.pfxlen = nil if offset? self end
The number of IP addresses in subnet
IP.new("1.2.3.4/24").size => 256
# File lib/ip/base.rb, line 193 def size mask + 1 end
# File lib/ip/base.rb, line 221 def succ self.class.new(@addr + size, @pfxlen, @ctx) end
Return an array representation of the address, with 3 or 4 elements depending on whether there is a routing context set.
["v4", 16909060, 28] ["v4", 16909060, 28, "context"]
(Removing the last element makes them Comparable, as nil.<=> doesn’t exist)
# File lib/ip/base.rb, line 90 def to_a @ctx ? [self.class::PROTO, @addr, @pfxlen, @ctx] : [self.class::PROTO, @addr, @pfxlen] end
Return an array representation of the address, with 3 or 4 elements depending on whether there is a routing context set, using hexadecimal.
["v4", "01020304", 28] ["v4", "01020304", 28, "context"]
# File lib/ip/base.rb, line 99 def to_ah @ctx ? [self.class::PROTO, to_hex, @pfxlen, @ctx] : [self.class::PROTO, to_hex, @pfxlen] end
Return an alternative 4-element array format with the routing context as the first element. Useful for grouping by context.
cpal = [context, proto, address, prefix_length]
# File lib/ip/cpal.rb, line 14 def to_cpal [@ctx, self.class::PROTO, @addr, @pfxlen] end
As cpal but with a hex string for the address part
# File lib/ip/cpal.rb, line 19 def to_cphl [@ctx, self.class::PROTO, to_hex, @pfxlen] end
Return the address as a hexadecimal string (8 or 32 digits)
# File lib/ip/base.rb, line 81 def to_hex @addr.to_s(16).rjust(self.class::ADDR_BITS>>2,"0") end
Return the address as an Integer
# File lib/ip/base.rb, line 76 def to_i @addr end
# File lib/ip/base.rb, line 178 def to_irange a1 = @addr & ~mask a2 = a1 | mask (a1..a2) end
QUERY: IPAddr (1.9) turns 1.2.3.0/24 into 1.2.3.0/24..1.2.3.255/24 Here I turn it into 1.2.3.0..1.2.3.255. Which is better?
# File lib/ip/base.rb, line 186 def to_range self.class.new(@addr & ~mask, self.class::ADDR_BITS, @ctx) .. self.class.new(@addr | mask, self.class::ADDR_BITS, @ctx) end
Return the string representation of the address, x.x.x.x[@ctx]
# File lib/ip/base.rb, line 65 def to_s ctx ? "#{to_addrlen}@#{ctx}" : to_addrlen end
Convert to a packed sockaddr structure
# File lib/ip/socket.rb, line 13 def to_sockaddr(port=0) Socket.pack_sockaddr_in(port, to_addr) end
Return a new IP object representing the wildmask (inverse netmask)
IP.new("1.2.3.4/24").netmask => #<IP::V4 0.0.0.255>
# File lib/ip/base.rb, line 146 def wildmask self.class.new(mask) end
Generated with the Darkfish Rdoc Generator 2.