d4t_formulas/assets/formulas/it-networking.d4rt
2026-03-08 21:20:31 +01:00

39 lines
1.4 KiB
Text

[
{
"name": "Network Address from Host IP",
"description": r"""Calculates the network address from a host IP/MASK notation.
Given a host IP address and subnet mask in CIDR notation (e.g., 192.168.1.100/24),
this formula extracts the network address (e.g., 192.168.1.0/24).
Where:
- Host IP/MASK: IP address with CIDR subnet mask (e.g., 192.168.1.100/24)
- Network: The resulting network address in IP/MASK format (e.g., 192.168.1.0/24)
The network address is calculated by applying the subnet mask to zero out the host bits.""",
"input": [
{"name": "hostIP", "unit": "string"}
],
"output": {"name": "network", "unit": "string"},
"d4rtCode": r"""
var parts = hostIP.split('/');
var ip = parts[0];
var mask = int.parse(parts[1]);
var octets = ip.split('.').map((e) => int.parse(e)).toList();
var hostBits = 32 - mask;
var shiftAmount = hostBits;
var networkValue = 0;
for (var i = 0; i < 4; i++) {
networkValue = (networkValue << 8) | octets[i];
}
networkValue = (networkValue >> shiftAmount) << shiftAmount;
var networkOctets = [];
for (var i = 0; i < 4; i++) {
networkOctets.insert(0, networkValue & 0xFF);
networkValue = networkValue >> 8;
}
network = networkOctets.join('.') + '/' + mask.toString();
""",
"tags": ["networking", "ip", "subnetting", "cidr", "network"]
}
]