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

ip arithmetic #3142

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

ip arithmetic #3142

lgfausak opened this issue May 13, 2024 · 2 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.

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

3 participants