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 cidr_to_range function #168

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bastelfreak
Copy link
Member

Pull Request (PR) description

This Pull Request (PR) fixes the following issues

@bastelfreak bastelfreak added the enhancement New feature or request label Oct 4, 2020
@bastelfreak bastelfreak self-assigned this Oct 4, 2020
@bastelfreak bastelfreak force-pushed the cidr_to_range branch 2 times, most recently from 873aaa1 to ce21f86 Compare October 4, 2020 14:18
Copy link
Contributor

@igalic igalic left a comment

Choose a reason for hiding this comment

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

🙋🏻‍♀️

REFERENCE.md Show resolved Hide resolved
Comment on lines +16 to +19
ips = IPAddr.new(ip).to_range.map(&:to_s)
ips.shift
ips.pop
ips
Copy link
Member

Choose a reason for hiding this comment

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

This is only really true for IPv4, not IPv6.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if I can follow. I tested this locally and also wrote a test that passes for IPv6. This works for IPv4 and IPv6?

Copy link
Member

Choose a reason for hiding this comment

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

Popping the last address isn't really needed AFAIK because IPv6 doesn't have a broadcast address. I think the same is true for shifting due to a network address.

Copy link
Member

@kenyon kenyon Oct 4, 2020

Choose a reason for hiding this comment

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

The first address in an IPv6 subnet is reserved as the subnet router anycast address: https://tools.ietf.org/html/rfc4291#section-2.6.1

But the last address in a subnet is usable.

end

def cidr_to_range(ip)
ips = IPAddr.new(ip).to_range.map(&:to_s)
Copy link
Member

Choose a reason for hiding this comment

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

Also, this can be slow and memory consuming for huge IPv6 networks like a /64 (or larger). Why would you want this?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, depending on the subnet size it might take some time.


I know at least one person that uses https://github.com/Yelp/puppet-netstdlib and I require those functions as well. Yelp abandoned the module and I had the impression it's a good idea to add them to extlib. I thought for a hand full of short functions it doesnt make much sense to maintain a whole module.

Copy link
Contributor

Choose a reason for hiding this comment

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

what is this used for? eespecially in the case of IPv6?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, not sure why you would want to enumerate every address of a subnet (you're definitely not doing this for most IPv6 networks), but I think it would be more useful to have a function that gets one of the addresses in a subnet as I mentioned here: #165 (comment)

You could have a loop around such a function if you really wanted to do something with every address.

Copy link
Contributor

Choose a reason for hiding this comment

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

this would make sense if we returned lazy structures

@SimonHoenscheid
Copy link
Member

Maybe these functions can be imported https://forge.puppet.com/yelp/netstdlib

@bastelfreak
Copy link
Member Author

@SimonHoenscheid that's what I'm doing :)

end

context 'when called with an IPv6 CIDR' do
it { is_expected.to run.with_params('fe80::5054:ff:fe47:4a37/126').and_return(['fe80::5054:ff:fe47:4a35', 'fe80::5054:ff:fe47:4a36']) }
Copy link
Member

Choose a reason for hiding this comment

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

I always like to use example IP ranges. In https://community.theforeman.org/t/using-ips-and-domains-in-tests-documentation/16600 I keep a list of references.

end

context 'when called with an IPv6 CIDR' do
it { is_expected.to run.with_params('fe80::5054:ff:fe47:4a37/126').and_return(['fe80::5054:ff:fe47:4a35', 'fe80::5054:ff:fe47:4a36']) }
Copy link
Member

Choose a reason for hiding this comment

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

Indeed this is not correct for IPv6, the last address fe80::5054:ff:fe47:4a37 is usable.

###### calling the function

```puppet
extlib::cidr_to_range('127.0.0.1/8')
Copy link
Member

Choose a reason for hiding this comment

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

I sincerely hope no one calls this with a /8...

Copy link
Contributor

Choose a reason for hiding this comment

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

not sure that's much worse than the standard /64 IPv6 address range…

Copy link
Member

@ekohl ekohl Oct 7, 2020

Choose a reason for hiding this comment

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

Well, IPv4 /8 is 2**24 while IPv6 /64 is 2**64. Every IPv4 address is 32 bits. 32 bits * 2**24 = 67 MB (assuming optimal storage, which it isn't). For IPv6 it's 128 bits * 2**64 = 256 EiB. If my math is correct, you really don't want to do this for IPv6.

@vox-pupuli-tasks
Copy link

Dear @bastelfreak, thanks for the PR!

This is Vox Pupuli Tasks, your friendly Vox Pupuli GitHub Bot. I noticed that your pull request contains merge conflict. Can you please rebase?

You can find my sourcecode at voxpupuli/vox-pupuli-tasks

@ghoneycutt ghoneycutt marked this pull request as draft December 5, 2024 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request merge-conflicts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants