ipaddr icon indicating copy to clipboard operation
ipaddr copied to clipboard

Add IPAddr#+/-

Open taketo1113 opened this issue 1 year ago • 5 comments

Add +/- methods to get an ipaddr instance greater/less than the original address by offset. the current IPAddr#succ only return next address, this IPAddr#+ return any address with offset.

addr = IPAddr.new("192.168.1.10")

addr + 10
=> #<IPAddr: IPv4:192.168.1.20/255.255.255.255>


(addr + 10).to_s
=> "192.168.1.20"

taketo1113 avatar Aug 07 '24 10:08 taketo1113

I logged the build failure as #73

EDIT: I merged fixes to the CI, so @taketo1113, if you rebase this on top of latest, it ought to run tests right.

olleolleolle avatar Aug 07 '24 11:08 olleolleolle

@olleolleolle Thanks for fixing ci build error. It passed CI :-)

taketo1113 avatar Aug 07 '24 11:08 taketo1113

@knu No. I think it is not an introduction to the concept of delta.

The use case I was thinking of is a human friendly calculation of addresses. When a network operator reserved a few addresses (upper/downer) such as VRRP (virtual/primary/secondary), a network user can't use reserved addresses. It needs to calculate available addresses, to indicate available addresses for network user.

addr = IPAddr.new("192.168.1.1")
addr + 10
=> #<IPAddr: IPv4:192.168.1.11/255.255.255.255>


addr = IPAddr.new("192.168.1.255")
addr - 10
=> #<IPAddr: IPv4:192.168.1.245/255.255.255.255>

taketo1113 avatar Aug 08 '24 00:08 taketo1113

One more question: should IPAddr.new("192.168.0.255/24" ) + 1 be considered a valid operation? The resulted address will break out of the network.

knu avatar Aug 09 '24 11:08 knu

I think it should be valid operation, because it just calculates the IP Address (which is not address block or network mask) like the bitwise operator of >> or <<. If needs to detect breaking out of the network, I think it could use #include? or #to_range.

addr = IPAddr.new("192.168.0.0/24") + 255
=> #<IPAddr: IPv4:192.168.0.255/255.255.255.0>

# `"192.168.0.255/24" + 1` just return next ip address
addr + 1
=> #<IPAddr: IPv4:192.168.1.0/255.255.255.0>

taketo1113 avatar Aug 09 '24 13:08 taketo1113

(I spoke directly with @knu and got some feedback. Here are the answers.)

Q1: What is the behavior when incrementing (+1) past the end of an address block? (ex. 192.168.0.255/24 + 1)

A1: It returns the address in the next address block. The behavior is the same as IPAddr#succ.

 ipaddr
=> #<IPAddr: IPv4:192.168.0.255/255.255.255.0>
ipaddr.cidr
=> "192.168.0.255/24"

ipaddr + 1
=> #<IPAddr: IPv4:192.168.1.0/255.255.255.0>

ipaddr.succ
=> #<IPAddr: IPv4:192.168.1.0/255.255.255.0>

Q2: What is the behavior when the result of +1 is an invalid address? (ex. 255.255.255.255 + 1)

A2: It raises an InvalidAddressError exception. This behavior is the same as IPAddr#succ.

# IPv4
IPAddr.new("255.255.255.255") + 1
/path/ipaddr/lib/ipaddr.rb:553:in 'IPAddr#set': invalid address: 4294967296 (IPAddr::InvalidAddressError)

        raise InvalidAddressError, "invalid address: #{addr}"
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# IPv6
IPAddr.new("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") + 1
/path/ipaddr/lib/ipaddr.rb:557:in 'IPAddr#set': invalid address: 340282366920938463463374607431768211456 (IPAddr::InvalidAddressError)

        raise InvalidAddressError, "invalid address: #{addr}"
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Q3: What is the behavior when the result of -1 is an invalid address, similar to Q2? (ex. 0.0.0.0 - 1)

A3: It raises an InvalidAddressError exception. This behavior is the same as with IPAddr#+.

# IPv4
IPAddr.new("0.0.0.0") - 1
/path/ipaddr/lib/ipaddr.rb:553:in 'IPAddr#set': invalid address: -1 (IPAddr::InvalidAddressError)

        raise InvalidAddressError, "invalid address: #{addr}"
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

# IPv6
IPAddr.new("::") - 1
/path/ipaddr/lib/ipaddr.rb:557:in 'IPAddr#set': invalid address: -1 (IPAddr::InvalidAddressError)

        raise InvalidAddressError, "invalid address: #{addr}"
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

taketo1113 avatar Apr 21 '25 14:04 taketo1113

Thanks!

knu avatar Apr 26 '25 11:04 knu