Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IPAddr#+/- #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add IPAddr#+/- #71

wants to merge 2 commits into from

Conversation

taketo1113
Copy link
Contributor

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 taketo1113 requested a review from knu as a code owner August 7, 2024 10:36
@olleolleolle
Copy link
Contributor

olleolleolle commented Aug 7, 2024

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.

@taketo1113
Copy link
Contributor Author

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

Copy link
Member

@knu knu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this effectively introduces the concept of delta of IP addresses, does ipaddr1 - ipaddr2 make sense if they share the address family?

@taketo1113
Copy link
Contributor Author

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

@knu
Copy link
Member

knu commented Aug 9, 2024

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.

@taketo1113
Copy link
Contributor Author

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants