mirror of
https://github.com/lissy93/networking-toolbox.git
synced 2026-12-06 19:22:40 +03:00
Fixes incorrect usable hosts for ipv4 subnet (#29)
This commit is contained in:
1 parent
b7c2652096
commit
92f46e0b37
4 files changed
+123
-16
No files matched your search
@@ -16,6 +16,7 @@
|
||||
function generateNetworkBlocks() {
|
||||
const totalHosts = subnetInfo.hostCount;
|
||||
const _usableHosts = subnetInfo.usableHosts;
|
||||
const cidr = subnetInfo.cidr;
|
||||
|
||||
// For visualization, we'll show up to 256 blocks max
|
||||
const maxBlocks = 256;
|
||||
@@ -24,20 +25,40 @@
|
||||
|
||||
const blocks = [];
|
||||
|
||||
for (let i = 0; i < blocksToShow; i++) {
|
||||
const isNetwork = i === 0;
|
||||
const isBroadcast = i === blocksToShow - 1 && totalHosts > 2;
|
||||
const _isUsable = !isNetwork && !isBroadcast;
|
||||
// RFC 3021: /31 networks have no network/broadcast addresses
|
||||
const is31Subnet = cidr === 31;
|
||||
const is32Subnet = cidr === 32;
|
||||
|
||||
blocks.push({
|
||||
id: i,
|
||||
type: isNetwork ? 'network' : isBroadcast ? 'broadcast' : 'usable',
|
||||
represents: blockSize,
|
||||
tooltip: isNetwork
|
||||
for (let i = 0; i < blocksToShow; i++) {
|
||||
let type: 'network' | 'broadcast' | 'usable';
|
||||
let tooltip: string;
|
||||
|
||||
if (is31Subnet) {
|
||||
// /31: Both IPs are usable hosts (point-to-point)
|
||||
type = 'usable';
|
||||
tooltip = i === 0 ? 'Usable Host 1 (P2P)' : 'Usable Host 2 (P2P)';
|
||||
} else if (is32Subnet) {
|
||||
// /32: Single host
|
||||
type = 'usable';
|
||||
tooltip = 'Single Host';
|
||||
} else {
|
||||
// Normal subnet: first is network, last is broadcast, rest are usable
|
||||
const isNetwork = i === 0;
|
||||
const isBroadcast = i === blocksToShow - 1 && totalHosts > 2;
|
||||
|
||||
type = isNetwork ? 'network' : isBroadcast ? 'broadcast' : 'usable';
|
||||
tooltip = isNetwork
|
||||
? 'Network Address'
|
||||
: isBroadcast
|
||||
? 'Broadcast Address'
|
||||
: `Usable Host${blockSize > 1 ? 's' : ''}`,
|
||||
: `Usable Host${blockSize > 1 ? 's' : ''}`;
|
||||
}
|
||||
|
||||
blocks.push({
|
||||
id: i,
|
||||
type,
|
||||
represents: blockSize,
|
||||
tooltip,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -73,10 +73,37 @@ export function calculateSubnet(ip: string, cidr: number): SubnetInfo {
|
||||
|
||||
const hostBits = 32 - cidr;
|
||||
const hostCount = Math.pow(2, hostBits);
|
||||
const usableHosts = hostCount > 2 ? hostCount - 2 : 0;
|
||||
|
||||
const firstHost = numberToIP(ipToNumber(network.octets.join('.')) + 1);
|
||||
const lastHost = numberToIP(ipToNumber(broadcast.octets.join('.')) - 1);
|
||||
// Calculate usable hosts based on CIDR
|
||||
// RFC 3021: /31 has 2 usable hosts (point-to-point, no network/broadcast)
|
||||
// /32 has 1 usable host (single host route)
|
||||
// All others: total - 2 (excluding network and broadcast)
|
||||
let usableHosts: number;
|
||||
if (cidr === 32) {
|
||||
usableHosts = 1;
|
||||
} else if (cidr === 31) {
|
||||
usableHosts = 2; // Both IPs are usable for point-to-point links
|
||||
} else {
|
||||
usableHosts = hostCount - 2;
|
||||
}
|
||||
|
||||
// Calculate first and last host based on CIDR
|
||||
let firstHost: IPAddress;
|
||||
let lastHost: IPAddress;
|
||||
|
||||
if (cidr === 32) {
|
||||
// Host route - first and last are the same
|
||||
firstHost = network;
|
||||
lastHost = network;
|
||||
} else if (cidr === 31) {
|
||||
// Point-to-point - use network and broadcast as hosts (RFC 3021)
|
||||
firstHost = network;
|
||||
lastHost = broadcast;
|
||||
} else {
|
||||
// Normal subnet - exclude network and broadcast
|
||||
firstHost = numberToIP(ipToNumber(network.octets.join('.')) + 1);
|
||||
lastHost = numberToIP(ipToNumber(broadcast.octets.join('.')) - 1);
|
||||
}
|
||||
|
||||
return {
|
||||
network,
|
||||
|
||||
@@ -51,10 +51,10 @@ describe('IPv4 Subnet Calculator Route Functionality', () => {
|
||||
expect(result.wildcardMask.octets).toEqual([0, 0, 0, 0]);
|
||||
expect(result.cidr).toBe(32);
|
||||
expect(result.hostCount).toBe(1);
|
||||
expect(result.usableHosts).toBe(0);
|
||||
expect(result.usableHosts).toBe(1); // Single host route
|
||||
});
|
||||
|
||||
it('handles /31 point-to-point link correctly', () => {
|
||||
it('handles /31 point-to-point link correctly (RFC 3021)', () => {
|
||||
const result = calculateSubnet('192.168.1.0', 31);
|
||||
|
||||
expect(result.network.octets).toEqual([192, 168, 1, 0]);
|
||||
@@ -63,7 +63,7 @@ describe('IPv4 Subnet Calculator Route Functionality', () => {
|
||||
expect(result.wildcardMask.octets).toEqual([0, 0, 0, 1]);
|
||||
expect(result.cidr).toBe(31);
|
||||
expect(result.hostCount).toBe(2);
|
||||
expect(result.usableHosts).toBe(0); // Traditional calculation: 2 hosts = 0 usable
|
||||
expect(result.usableHosts).toBe(2); // RFC 3021: both IPs usable for point-to-point
|
||||
});
|
||||
|
||||
it('handles /28 network correctly', () => {
|
||||
|
||||
@@ -186,4 +186,63 @@ describe('IP calculations core logic', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('/31 point-to-point subnet (RFC 3021)', () => {
|
||||
it('reports 2 usable hosts for /31 subnet', () => {
|
||||
const subnet = calculateSubnetInfo('192.168.1.0', 31);
|
||||
expect(subnet.usableHosts).toBe(2);
|
||||
expect(subnet.hostCount).toBe(2);
|
||||
});
|
||||
|
||||
it('uses network address as first usable host', () => {
|
||||
const subnet = calculateSubnetInfo('10.0.0.0', 31);
|
||||
expect(subnet.firstHost.octets).toEqual([10, 0, 0, 0]);
|
||||
});
|
||||
|
||||
it('uses broadcast address as second usable host', () => {
|
||||
const subnet = calculateSubnetInfo('10.0.0.0', 31);
|
||||
expect(subnet.lastHost.octets).toEqual([10, 0, 0, 1]);
|
||||
});
|
||||
|
||||
it('calculates correct network and broadcast for /31', () => {
|
||||
const subnet = calculateSubnetInfo('172.16.0.10', 31);
|
||||
// Network should be even address
|
||||
expect(subnet.network.octets).toEqual([172, 16, 0, 10]);
|
||||
// Broadcast should be odd address (network + 1)
|
||||
expect(subnet.broadcast.octets).toEqual([172, 16, 0, 11]);
|
||||
});
|
||||
|
||||
it('handles multiple /31 subnets in same range', () => {
|
||||
const subnet1 = calculateSubnetInfo('192.168.1.0', 31);
|
||||
const subnet2 = calculateSubnetInfo('192.168.1.2', 31);
|
||||
const subnet3 = calculateSubnetInfo('192.168.1.4', 31);
|
||||
|
||||
// First /31: .0 and .1
|
||||
expect(subnet1.firstHost.octets[3]).toBe(0);
|
||||
expect(subnet1.lastHost.octets[3]).toBe(1);
|
||||
|
||||
// Second /31: .2 and .3
|
||||
expect(subnet2.firstHost.octets[3]).toBe(2);
|
||||
expect(subnet2.lastHost.octets[3]).toBe(3);
|
||||
|
||||
// Third /31: .4 and .5
|
||||
expect(subnet3.firstHost.octets[3]).toBe(4);
|
||||
expect(subnet3.lastHost.octets[3]).toBe(5);
|
||||
});
|
||||
|
||||
it('reports 100% address utilization for /31', () => {
|
||||
const subnet = calculateSubnetInfo('192.168.1.0', 31);
|
||||
const utilization = (subnet.usableHosts / subnet.hostCount) * 100;
|
||||
expect(utilization).toBe(100);
|
||||
});
|
||||
|
||||
it('correctly identifies both IPs as usable in /31', () => {
|
||||
const subnet = calculateSubnetInfo('203.0.113.0', 31);
|
||||
|
||||
// Both the network and broadcast addresses are usable
|
||||
expect(subnet.firstHost.octets).toEqual(subnet.network.octets);
|
||||
expect(subnet.lastHost.octets).toEqual(subnet.broadcast.octets);
|
||||
expect(subnet.usableHosts).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user