| Class | IP |
| In: |
lib/ip/cpal.rb
lib/ip/base.rb lib/ip/socket.rb |
| Parent: | Object |
| PROTO_TO_CLASS | = | {} |
| new | -> | orig_new |
| ctx | [RW] | Routing Context indicates the scope of this address (e.g. virtual router) |
| pfxlen | [R] | Length of prefix (network portion) of address |
Create an instance from an alternative array format:
[context, protocol, address, prefix_length]
# File lib/ip/cpal.rb, line 4 4: def self.from_cpal(cpal) 5: new([cpal[1], cpal[2], cpal[3], cpal[0]]) 6: 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 19
19: def new(src)
20: case src
21: when String
22: parse(src) || (raise ArgumentError, "invalid address")
23: when Array
24: (PROTO_TO_CLASS[src[0]] || (raise ArgumentError, "invalid protocol")).new(*src[1..-1])
25: when IP
26: src.dup
27: else
28: raise ArgumentError, "invalid address"
29: end
30: 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 49
49: def initialize(addr, pfxlen=nil, ctx=nil)
50: @addr = addr.is_a?(String) ? addr.to_i(16) : addr.to_i
51: raise ArgumentError, "Invalid address value" if @addr < 0 || @addr > self.class::MASK
52: self.pfxlen = pfxlen
53: self.ctx = ctx
54: end
# File lib/ip/base.rb, line 195
195: def &(other)
196: self.class.new(@addr & other.to_int, @pfxlen, @ctx)
197: end
# File lib/ip/base.rb, line 187
187: def +(other)
188: self.class.new(@addr + other.to_int, @pfxlen, @ctx)
189: end
# File lib/ip/base.rb, line 191
191: def -(other)
192: self.class.new(@addr - other.to_int, @pfxlen, @ctx)
193: end
# File lib/ip/base.rb, line 203
203: def ^(other)
204: self.class.new(@addr ^ other.to_int, @pfxlen, @ctx)
205: end
Return the address family, Socket::AF_INET or Socket::AF_INET6
# File lib/ip/socket.rb, line 5 5: def af 6: self.class::AF 7: 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 131
131: def broadcast(offset=0)
132: self.class.new((@addr | mask) + offset, @pfxlen, @ctx)
133: 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 149
149: def mask!
150: @addr &= ~mask
151: self
152: 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 123
123: def network(offset=0)
124: self.class.new((@addr & ~mask) + offset, @pfxlen, @ctx)
125: end
Change the prefix length. If nil, the maximum is used (32 or 128)
# File lib/ip/base.rb, line 102
102: def pfxlen=(pfxlen)
103: @mask = nil
104: if pfxlen
105: pfxlen = pfxlen.to_i
106: raise ArgumentError, "Invalid prefix length" if pfxlen < 0 || pfxlen > self.class::ADDR_BITS
107: @pfxlen = pfxlen
108: else
109: @pfxlen = self.class::ADDR_BITS
110: end
111: end
Return the protocol in string form, "v4" or "v6"
# File lib/ip/base.rb, line 57
57: def proto
58: self.class::PROTO
59: 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 87
87: def to_a
88: @ctx ? [self.class::PROTO, @addr, @pfxlen, @ctx] :
89: [self.class::PROTO, @addr, @pfxlen]
90: 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 96
96: def to_ah
97: @ctx ? [self.class::PROTO, to_hex, @pfxlen, @ctx] :
98: [self.class::PROTO, to_hex, @pfxlen]
99: 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 11
11: def to_cpal
12: [@ctx, self.class::PROTO, @addr, @pfxlen]
13: end
As cpal but with a hex string for the address part
# File lib/ip/cpal.rb, line 16
16: def to_cphl
17: [@ctx, self.class::PROTO, to_hex, @pfxlen]
18: end
Return the address as a hexadecimal string (8 or 32 digits)
# File lib/ip/base.rb, line 78
78: def to_hex
79: @addr.to_s(16).rjust(self.class::ADDR_BITS>>2,"0")
80: end
# File lib/ip/base.rb, line 175
175: def to_range
176: a1 = @addr & ~mask
177: a2 = a1 | mask
178: (a1..a2)
179: end
Return the string representation of the address, x.x.x.x[/pfxlen][@ctx]
# File lib/ip/base.rb, line 62
62: def to_s
63: ctx ? "#{to_addrlen}@#{ctx}" : to_addrlen
64: end
Convert to a packed sockaddr structure
# File lib/ip/socket.rb, line 10
10: def to_sockaddr(port=0)
11: Socket.pack_sockaddr_in(port, to_addr)
12: end