-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
os: add CIDR support #14307
Merged
refack
merged 1 commit into
nodejs:master
from
zeusdeux:feature/cidr-support-issue-14006
Aug 14, 2017
Merged
os: add CIDR support #14307
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
function getCIDRSuffix(mask, protocol = 'ipv4') { | ||
const isV6 = protocol === 'ipv6'; | ||
const bitsString = mask | ||
.split(isV6 ? ':' : '.') | ||
.filter((v) => !!v) | ||
.map((v) => pad(parseInt(v, isV6 ? 16 : 10).toString(2), isV6)) | ||
.join(''); | ||
|
||
if (isValidMask(bitsString)) { | ||
return countOnes(bitsString); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
function pad(binaryString, isV6) { | ||
const groupLength = isV6 ? 16 : 8; | ||
const binLen = binaryString.length; | ||
|
||
return binLen < groupLength ? | ||
`${'0'.repeat(groupLength - binLen)}${binaryString}` : binaryString; | ||
} | ||
|
||
function isValidMask(bitsString) { | ||
const firstIndexOfZero = bitsString.indexOf(0); | ||
const lastIndexOfOne = bitsString.lastIndexOf(1); | ||
|
||
return firstIndexOfZero < 0 || firstIndexOfZero > lastIndexOfOne; | ||
} | ||
|
||
function countOnes(bitsString) { | ||
return bitsString | ||
.split('') | ||
.reduce((acc, bit) => acc += parseInt(bit, 10), 0); | ||
} | ||
|
||
module.exports = { | ||
getCIDRSuffix | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const getCIDRSuffix = require('internal/os').getCIDRSuffix; | ||
|
||
const specs = [ | ||
// valid | ||
['128.0.0.0', 'ipv4', 1], | ||
['255.0.0.0', 'ipv4', 8], | ||
['255.255.255.128', 'ipv4', 25], | ||
['255.255.255.255', 'ipv4', 32], | ||
['ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'ipv6', 128], | ||
['ffff:ffff:ffff:ffff::', 'ipv6', 64], | ||
['ffff:ffff:ffff:ff80::', 'ipv6', 57], | ||
// invalid | ||
['255.0.0.1', 'ipv4', null], | ||
['255.255.9.0', 'ipv4', null], | ||
['255.255.1.0', 'ipv4', null], | ||
['ffff:ffff:43::', 'ipv6', null], | ||
['ffff:ffff:ffff:1::', 'ipv6', null] | ||
]; | ||
|
||
specs.forEach(([mask, protocol, expectedSuffix]) => { | ||
const actualSuffix = getCIDRSuffix(mask, protocol); | ||
|
||
assert.strictEqual( | ||
actualSuffix, expectedSuffix, | ||
`Mask: ${mask}, expected: ${expectedSuffix}, actual: ${actualSuffix}` | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add some tests for non-byte-aligned subnet masks, e.g.
255.255.224.0
->19
andffff:ffff:ffff:ff80::
->57
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hard as I rely on the output of
networkInterfaces
method and that gets data from theos
binding. So, I don't really know where I can inject some mock data with a custom non-alignednetmask
and have the js landnetworkInterfaces
method consume that. Any ideas?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, you could potential define the parsing method in a internal
os
module and test that separately.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@silverwind
So I moved the code out but requiringFound this registryinternal/os
fails with a module not found. Do I need to register it somewhere?node.gyp
.Secondly, where do tests for internal modules go? I see the
test
folder inlib/internal
only has tests forunicode
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zeusdeux In
test
, just like everything else. You would need to specify a// Flags: --expose-internals
to make the testing harness expose internal modules for testing though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the CIDR logic to
internal/os
and added tests for it @silverwind @TimothyGu.