Skip to content

Commit d564b1d

Browse files
toof-jpwhitequark
authored andcommitted
Add IPv4 CIDR four-part decimal validator
1 parent 08c2cd4 commit d564b1d

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ The `ipaddr.IPv4` and `ipaddr.IPv6` objects have some methods defined, too. All
127127
`ipaddr.IPvX.isValid(string)` can be used to check if the string is a valid address for particular protocol, and `ipaddr.IPvX.parse(string)` is the error-throwing parser.
128128

129129
`ipaddr.IPvX.isValid(string)` uses the same format for parsing as the POSIX `inet_ntoa` function, which accepts unusual formats like `0xc0.168.1.1` or `0x10000000`. The function `ipaddr.IPv4.isValidFourPartDecimal(string)` validates the IPv4 address and also ensures that it is written in four-part decimal format.
130+
`ipaddr.IPv4.isValidCIDRFourPartDecimal(string)` validates an IPv4 address in CIDR notation and also ensures that its address portion is written in four-part decimal format.
130131

131132
[IPv6 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js#L530
132133
[IPv4 ranges]: https://github.com/whitequark/ipaddr.js/blob/master/lib/ipaddr.js#L182

lib/ipaddr.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,17 @@
365365
}
366366
};
367367

368+
// Checks if a given string is a full four-part IPv4 Address with CIDR prefix.
369+
ipaddr.IPv4.isValidCIDRFourPartDecimal = function (string) {
370+
const match = string.match(/^(.+)\/(\d+)$/);
371+
372+
if (!ipaddr.IPv4.isValidCIDR(string) || !match) {
373+
return false;
374+
}
375+
376+
return ipaddr.IPv4.isValidFourPartDecimal(match[1]);
377+
};
378+
368379
// A utility function to return network address given the IPv4 interface and prefix length in CIDR notation
369380
ipaddr.IPv4.networkAddressFromCIDR = function (string) {
370381
let cidr, i, ipInterfaceOctets, octets, subnetMaskOctets;

lib/ipaddr.js.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ declare module "ipaddr.js" {
3030
static isValid(addr: string): boolean;
3131
static isValidCIDR(addr: string): boolean;
3232
static isValidFourPartDecimal(addr: string): boolean;
33+
static isValidCIDRFourPartDecimal(addr: string): boolean;
3334
static networkAddressFromCIDR(addr: string): IPv4;
3435
static parse(addr: string): IPv4;
3536
static parseCIDR(addr: string): [IPv4, number];

test/ipaddr.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ describe('ipaddr', () => {
160160
assert.equal(ipaddr.IPv4.isValidFourPartDecimal('0xc0.168.1.1'), false);
161161
})
162162

163+
it('checks the conventional IPv4 CIDR format', () => {
164+
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('192.168.1.1/24'), true);
165+
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('0.0.0.0/0'), true);
166+
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('0xc0.168.1.1/24'), false);
167+
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('192.168.1.1/33'), false);
168+
assert.equal(ipaddr.IPv4.isValidCIDRFourPartDecimal('192.168.1/24'), false);
169+
})
170+
163171
it('refuses to construct IPv4 address with trailing and leading zeros', () => {
164172
assert.equal(ipaddr.IPv4.isValidFourPartDecimal('000000192.168.100.2'), false);
165173
assert.equal(ipaddr.IPv4.isValidFourPartDecimal('192.0000168.100.2'), false);

0 commit comments

Comments
 (0)