silverwind / cidr-tools

Tools to work with IPv4 and IPv6 CIDR
94 stars 10 forks source link

Can this be used to convert an IP range into a CIDR block? #18

Closed jhuckaby closed 10 months ago

jhuckaby commented 10 months ago

Please forgive the question. I have read the docs, but I cannot see if this is possible with cidr-tools. What I want is to start with something like as the input:

[ '217.146.182.0', '217.146.182.255' ]

And end up with this as the output:

'217.146.182/24'

Does cidr-tools have a function to perform this conversion, or am I barking up the wrong tree?

(If barking up the wrong tree, do you know of a module that can perform this kind of transformation?)

Thank you so much for your work.

silverwind commented 10 months ago

Currently there is no suitable exported function for this, but it may be possible to add the inverse of parse, e.g. stringify, where you would modify at least start and end:

[Object: null prototype] {
  single: true,
  cidr: '1.1.1.0/32',
  version: 4,
  prefix: '32',
  start: 16843008n,
  end: 16843008n
}

E.g. stringify({start: parse("1.0.0.0").start, end: parse("1.0.0.255").start}), but likely it should have a more easy to use methods to convert from and to ranges. Not sure if this really fits the spirit of this module.

jhuckaby commented 10 months ago

I totally understand. Thank you for the reply! I agree it doesn't really fit into your module's API. Closing this issue now. Have a great rest of your weekend!