Skip to content

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

Open
lgfausak opened this issue May 13, 2024 · 4 comments
Open

ip arithmetic #3142

lgfausak opened this issue May 13, 2024 · 4 comments
Labels
FeatureRequest New feature or request Triage Requires triage/attention

Comments

@lgfausak
Copy link

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

@lgfausak lgfausak added FeatureRequest New feature or request Triage Requires triage/attention labels May 13, 2024
@NoamTD
Copy link
Contributor

NoamTD commented May 14, 2024

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:

sourceIp: [192, 168, 0, 0]
withOffset: "192.168.0.1"

@slewiskelly
Copy link
Contributor

it's a bit convoluted, but would something like this work for you?

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.

@myitcv
Copy link
Member

myitcv commented Mar 8, 2025

but I'm not sure if something like this would be added to the standard library

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?

@takonomura
Copy link

@myitcv ​Thank you for considering support for IP address arithmetic in the standard library.

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?

In the Go standard library, there is no built-in functionality to perform arithmetic operations on IP addresses.
In cases where arithmetic operations are needed, users typically convert IP addresses into big.Int to perform arithmetic operations12.
In contrast, Python's ipaddress module supports addition and subtraction through operator overloading, offering a more natural and intuitive interface3.

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

  1. https://github.com/kubernetes/utils/blob/24370beab75816ab1d2b33fba9e288ee667530dd/net/net.go#L55-L68

  2. https://github.com/containernetworking/plugins/blob/v1.6.2/pkg/ip/cidr.go#L60-L76

  3. https://docs.python.org/3/library/ipaddress.html#arithmetic-operators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest New feature or request Triage Requires triage/attention
Projects
None yet
Development

No branches or pull requests

5 participants