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

function to generate a range of IPv4 or IPv6 IP addresses in an array #120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ ip.cidrSubnet('192.168.1.134/26')
// range checking
ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true


// ipv4 long conversion
ip.toLong('127.0.0.1'); // 2130706433
ip.fromLong(2130706433); // '127.0.0.1'

// generate range
ip.range('192.168.0.1', '192.168.0.4'); // ['192.168.0.1', '192.168.0.2', '192.168.0.3', '192.168.0.4']
ip.range('::1', '::4'); // ['::1', '::2', '::3', '::4']
```

### License
Expand Down
39 changes: 39 additions & 0 deletions lib/ip.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,42 @@ ip.fromLong = function (ipl) {
ipl >> 8 & 255}.${
ipl & 255}`);
};



ip.range = function(start, end) {
let range = [];
if (this.isV4Format(start) && this.isV4Format(end)) {
let a = ip.toLong(start);
let b = ip.toLong(end);
for (let i = a; i <= b; i++) {
range.push(ip.fromLong(i));
}
}
else {
let a = ip.toBuffer(start);
let b = ip.toBuffer(end);
// check same format
if (a.length === b.length) {
// while a <= b
while (Buffer.compare(a, b) <= 0) {
range.push(ip.toString(a));

// increment with carry
let increment = true;
let p = a.length - 1;
while (p >= 0 && increment) {
if (a[p] === 255) {
a[p] = 0;
increment = true;
} else {
a[p]++;
increment = false;
}
p--;
}
}
}
}
return range;
};
23 changes: 23 additions & 0 deletions test/api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,27 @@ describe('IP library for node.js', () => {
assert.equal(ip.fromLong(4294967295), '255.255.255.255');
});
});

describe('range() method', function() {
it('should respond with array of ipv4 addresses', () => {
let range_array = ip.range('192.168.0.0', '192.168.1.255');
assert.equal(range_array.length, 512);
assert.equal(range_array[0], '192.168.0.0');
assert.equal(range_array[1], '192.168.0.1');
assert.equal(range_array[510], '192.168.1.254');
assert.equal(range_array[511], '192.168.1.255');
});
});

describe('range() method', function() {
it('should respond with array of ipv6 addresses', () => {
let range_array = ip.range('2000::fffe', '2000::1:2');
assert.equal(range_array.length, 5);
assert.equal(range_array[0], '2000::fffe');
assert.equal(range_array[1], '2000::ffff');
assert.equal(range_array[2], '2000::1:0');
assert.equal(range_array[3], '2000::1:1');
assert.equal(range_array[4], '2000::1:2');
});
});
});