Class IP
In: lib/ip/cpal.rb
lib/ip/base.rb
lib/ip/socket.rb
Parent: Object

Methods

&   +   -   <=>   ^   af   broadcast   eql?   from_cpal   hash   inspect   ipv4_compat?   ipv4_mapped?   mask   mask!   native   netmask   network   new   new   offset   offset?   parse   pfxlen=   proto   reset_pfxlen!   size   succ!   to_a   to_addrlen   to_ah   to_cpal   to_cphl   to_hex   to_i   to_range   to_s   to_sockaddr   wildmask   |   ~  

Included Modules

Comparable

Classes and Modules

Class IP::V4
Class IP::V6

Constants

PROTO_TO_CLASS = {}

External Aliases

new -> orig_new

Attributes

ctx  [RW]  Routing Context indicates the scope of this address (e.g. virtual router)
pfxlen  [R]  Length of prefix (network portion) of address

Public Class methods

Create an instance from an alternative array format:

  [context, protocol, address, prefix_length]

[Source]

   # 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.

[Source]

    # 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")

[Source]

    # 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

Parse a string as an IP address - return a V4/V6 object or nil

[Source]

    # File lib/ip/base.rb, line 33
33:     def parse(str)
34:       V4.parse(str) || V6.parse(str)
35:     end

Public Instance methods

[Source]

     # File lib/ip/base.rb, line 195
195:   def &(other)
196:     self.class.new(@addr & other.to_int, @pfxlen, @ctx)
197:   end

[Source]

     # File lib/ip/base.rb, line 187
187:   def +(other)
188:     self.class.new(@addr + other.to_int, @pfxlen, @ctx)
189:   end

[Source]

     # File lib/ip/base.rb, line 191
191:   def -(other)
192:     self.class.new(@addr - other.to_int, @pfxlen, @ctx)
193:   end

[Source]

     # File lib/ip/base.rb, line 240
240:   def <=>(other)
241:     to_a <=> other.to_a
242:   end

[Source]

     # 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

[Source]

   # 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>

[Source]

     # File lib/ip/base.rb, line 131
131:   def broadcast(offset=0)
132:     self.class.new((@addr | mask) + offset, @pfxlen, @ctx)
133:   end

[Source]

     # File lib/ip/base.rb, line 236
236:   def eql?(other)
237:     to_a.eql?(other.to_a)
238:   end

[Source]

     # File lib/ip/base.rb, line 232
232:   def hash
233:     to_a.hash
234:   end

[Source]

     # File lib/ip/base.rb, line 216
216:   def inspect
217:     res = "#<#{self.class} #{to_s}>"
218:   end

[Source]

     # File lib/ip/base.rb, line 224
224:   def ipv4_compat?
225:     false
226:   end

[Source]

     # File lib/ip/base.rb, line 220
220:   def ipv4_mapped?
221:     false
222:   end

Return the mask for this pfxlen as an integer. For example, a V4 /24 address has a mask of 255 (0x000000ff)

[Source]

     # File lib/ip/base.rb, line 115
115:   def mask
116:     @mask ||= (1 << (self.class::ADDR_BITS - @pfxlen)) - 1
117:   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>

[Source]

     # File lib/ip/base.rb, line 149
149:   def mask!
150:     @addr &= ~mask
151:     self
152:   end

[Source]

     # File lib/ip/base.rb, line 228
228:   def native
229:     self
230:   end

Return a new IP object representing the netmask

   IP.new("1.2.3.4/24").netmask  =>  #<IP::V4 255.255.255.0>

[Source]

     # File lib/ip/base.rb, line 137
137:   def netmask
138:     self.class.new(self.class::MASK & ~mask)
139:   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>

[Source]

     # File lib/ip/base.rb, line 123
123:   def network(offset=0)
124:     self.class.new((@addr & ~mask) + offset, @pfxlen, @ctx)
125:   end

Returns offset from base of subnet to this address

   IP.new("1.2.3.4/24").offset   => 4

[Source]

     # File lib/ip/base.rb, line 163
163:   def offset
164:     @addr - (@addr & ~mask)
165:   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)

[Source]

     # File lib/ip/base.rb, line 157
157:   def offset?
158:     @addr != (@addr & ~mask)
159:   end

Change the prefix length. If nil, the maximum is used (32 or 128)

[Source]

     # 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"

[Source]

    # File lib/ip/base.rb, line 57
57:   def proto
58:     self.class::PROTO
59:   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>

[Source]

     # File lib/ip/base.rb, line 170
170:   def reset_pfxlen!
171:     self.pfxlen = nil if offset?
172:     self
173:   end

The number of IP addresses in subnet

   IP.new("1.2.3.4/24").size   => 256

[Source]

     # File lib/ip/base.rb, line 183
183:   def size
184:     mask + 1
185:   end

[Source]

     # File lib/ip/base.rb, line 211
211:   def succ!
212:     @addr += 1
213:     self
214:   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)

[Source]

    # 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 the string representation of the IP address and prefix, or just the IP address if it‘s a single address

[Source]

    # File lib/ip/base.rb, line 68
68:   def to_addrlen
69:     pfxlen == self.class::ADDR_BITS ? to_addr : "#{to_addr}/#{pfxlen}"
70:   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"]

[Source]

    # 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]

[Source]

    # 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

[Source]

    # 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)

[Source]

    # 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

Return the address as an Integer

[Source]

    # File lib/ip/base.rb, line 73
73:   def to_i
74:     @addr
75:   end

[Source]

     # 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]

[Source]

    # 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

[Source]

    # File lib/ip/socket.rb, line 10
10:   def to_sockaddr(port=0)
11:     Socket.pack_sockaddr_in(port, to_addr)
12:   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>

[Source]

     # File lib/ip/base.rb, line 143
143:   def wildmask
144:     self.class.new(mask)
145:   end

[Source]

     # File lib/ip/base.rb, line 199
199:   def |(other)
200:     self.class.new(@addr | other.to_int, @pfxlen, @ctx)
201:   end

[Source]

     # File lib/ip/base.rb, line 207
207:   def ~
208:     self.class.new(~@addr & self.class::MASK, @pfxlen, @ctx)
209:   end

[Validate]