diff --git a/src/tools/ipv4-subnets-list/index.ts b/src/tools/ipv4-subnets-list/index.ts new file mode 100644 index 00000000..b11b3686 --- /dev/null +++ b/src/tools/ipv4-subnets-list/index.ts @@ -0,0 +1,13 @@ +import { RouterOutlined } from '@vicons/material'; +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'IPv4 Subnets Lister', + path: '/ipv4-subnets-list', + description: 'List all possible subnets of a given CIDR', + keywords: ['ipv4', 'subnet', 'bitmask'], + component: () => import('./ipv4-subnets-list.vue'), + icon: RouterOutlined, + createdAt: new Date('2025-12-06'), + category: 'Network', +}); diff --git a/src/tools/ipv4-subnets-list/ipv4-subnets-list.vue b/src/tools/ipv4-subnets-list/ipv4-subnets-list.vue new file mode 100644 index 00000000..0e60083c --- /dev/null +++ b/src/tools/ipv4-subnets-list/ipv4-subnets-list.vue @@ -0,0 +1,80 @@ + + + diff --git a/src/utils/ip.test.ts b/src/utils/ip.test.ts index 86d7f69a..e78c67d9 100644 --- a/src/utils/ip.test.ts +++ b/src/utils/ip.test.ts @@ -93,7 +93,9 @@ describe('ipv4/6 util', () => { '248.0.0.0/6', // NOSONAR '252.0.0.0/6', // NOSONAR ]); // NOSONAR - expect(getSubnets('1.1.1.1/8')).to.eql([]); // NOSONAR + expect(getSubnets('1.1.1.1/8')).to.eql([ + '1.0.0.0/8', // NOSONAR + ]); // NOSONAR expect(getSubnets('1.1.1.1/11')).to.eql([ // NOSONAR '1.0.0.0/11', // NOSONAR '1.32.0.0/11', // NOSONAR @@ -104,7 +106,9 @@ describe('ipv4/6 util', () => { '1.192.0.0/11', // NOSONAR '1.224.0.0/11', // NOSONAR ]); // NOSONAR - expect(getSubnets('172.16.2.2/16')).to.eql([]); // NOSONAR + expect(getSubnets('172.16.2.2/16')).to.eql([ + '172.16.0.0/16', // NOSONAR + ]); // NOSONAR expect(getSubnets('172.16.2.2/26')).to.eql([ // NOSONAR '172.16.2.0/26', // NOSONAR '172.16.2.64/26', // NOSONAR @@ -112,7 +116,9 @@ describe('ipv4/6 util', () => { '172.16.2.192/26', // NOSONAR ]); // NOSONAR expect(getSubnets('172.16.2.2/31').length).to.eql(128); // NOSONAR - expect(getSubnets('255.255.255.0/32')).to.eql([]); // NOSONAR + expect(getSubnets('255.255.255.0/32')).to.eql([ + '255.255.255.0/32', // NOSONAR + ]); // NOSONAR expect(getSubnets('2001:db8:0:85a3::ac1f:8001/62')).to.eql([]); // NOSONAR expect(getSubnets('2001:db8:0:85a3:0:0:ac1f:8001/62')).to.eql([]); // NOSONAR expect(getSubnets('2001:db8:0:85a3::ac1f:8001/112')).to.eql([]); // NOSONAR diff --git a/src/utils/ip.ts b/src/utils/ip.ts index b41516ce..f0be59d5 100644 --- a/src/utils/ip.ts +++ b/src/utils/ip.ts @@ -31,7 +31,16 @@ export function parseAsCIDR(form: string) { return (ipMatch.convertToMasks() || [])[0]?.convertToSubnet()?.toString() || ''; } -export function getSubnets(cidr: string) { +export interface SubnetInfo { + netAddress: string + firstIP: string + lastIP: string + broadcastIP: string + prefix: number + hostsCount: number +} + +export function getSubnetsInfos(cidr: string): SubnetInfo[] { const [address, prefix] = cidr.split('/'); if (isIPv4(address)) { const prefix4Int = Number(prefix || '32'); @@ -43,15 +52,18 @@ export function getSubnets(cidr: string) { if (prefix4Int < 8) { startNetwork = 0; } - if (prefix4Int % 8 === 0) { - return []; - } startNetwork = bigInt & getMask(prefix4Int); const increment = BigInt(2) ** BigInt(32 - prefix4Int); - const netCount = getNetworksCount(cidr); + const netCount = getNetworksCount(cidr) || 1; for (let netIndex = 0; netIndex < netCount; netIndex += 1) { - const netAddr = Address4.fromBigInt(startNetwork).correctForm(); - subnets.push(`${netAddr}/${prefix4Int}`); + subnets.push({ + netAddress: Address4.fromBigInt(startNetwork).correctForm(), + firstIP: Address4.fromBigInt(startNetwork + 1n).correctForm(), + lastIP: Address4.fromBigInt(startNetwork + increment - 2n).correctForm(), + broadcastIP: Address4.fromBigInt(startNetwork + increment - 1n).correctForm(), + prefix: prefix4Int, + hostsCount: Number(increment - 2n), + }); startNetwork += increment; } return subnets; @@ -60,6 +72,10 @@ export function getSubnets(cidr: string) { return []; } +export function getSubnets(cidr: string) { + return getSubnetsInfos(cidr).map(({ netAddress, prefix }) => `${netAddress}/${prefix}`); +} + export function getNetworksCount(cidr: string) { const [address, prefix] = cidr.split('/'); if (isIPv4(address)) {