-
Notifications
You must be signed in to change notification settings - Fork 312
ip arithmetic #3142
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
Comments
it's a bit convoluted, but would something like this work for you? import "net"
#IncIP: {
In!: net.IPv4
Offset!: int
_incremented: In[:3] + [In[len(In)-1]+Offset]
Out: net.IPString(_incremented)
}
sourceIp: net.ToIP4("192.168.0.0")
withOffset: (#IncIP & {Offset: 1, In: sourceIp}).Out output:
|
This will fail if exceeding an octet (255). I'm not certain that this is totally correct, but: import (
"math"
"net"
"strconv"
)
ip: (#IPAdd & {ip: "192.168.0.0", i: 256}).out
#IPAdd: {
ip: string
i: uint
out: "\(math.Floor(_out/16777216)).\(math.Floor(_out/65536) mod 256).\(math.Floor(_out/256) mod 256).\(_out mod 256)"
_ip: net.ToIP4(ip)
_out: (_ip[0]*16777216 + _ip[1]*65536 + _ip[2]*256 + _ip[3]) + i
} This is easier to implement in Go, but I'm not sure if something like this would be added to the standard library, though. |
This seems like a reasonable thing to support. Not least because of the problems that arise when trying to "do this by hand" as demonstrated in #3787. But generally because it seems useful enough to have cropped up here more than once. Question: is it generally the case that a plain integer value is added? Asked another way, what should the API of such a function look like? Any good examples from the Go standard library or elsewhere? |
@myitcv Thank you for considering support for IP address arithmetic in the standard library.
In the Go standard library, there is no built-in functionality to perform arithmetic operations on IP addresses. I have implemented a proof-of-concept API and created PR #3815. I look forward to feedback regarding naming conventions, interface design, and any other aspects that could improve the design. Footnotes |
i create a lot of json to do configuration. typically my configurations contain ip addresses. i am new to cue, i've spent the day doing experiments and reading through all of the docs / tutorials / etc. If I wanted to use cue to create an ip address based upon a cidr + offset is that possible? something like "192.168.0.0/24" + 3 == 192.168.0.3
The text was updated successfully, but these errors were encountered: