Better subnet
This commit is contained in:
parent
547a651106
commit
0e46950338
2 changed files with 16 additions and 4 deletions
|
|
@ -16,14 +16,27 @@ The network address is calculated by applying the subnet mask to zero out the ho
|
||||||
],
|
],
|
||||||
"output": {"name": "network", "unit": "string"},
|
"output": {"name": "network", "unit": "string"},
|
||||||
"d4rtCode": r"""
|
"d4rtCode": r"""
|
||||||
|
hostIP = hostIP.toString();
|
||||||
var parts = hostIP.split('/');
|
var parts = hostIP.split('/');
|
||||||
|
if( parts.length != 2 ) {
|
||||||
|
signal('Invalid input format. Expected format: IP/MASK');
|
||||||
|
}
|
||||||
var ip = parts[0];
|
var ip = parts[0];
|
||||||
var mask = int.parse(parts[1]);
|
var mask = int.tryParse(parts[1]);
|
||||||
var octets = ip.split('.').map((e) => int.parse(e)).toList();
|
if( mask == null || mask < 0 || mask > 32 ) {
|
||||||
|
signal('Invalid subnet mask. Must be an integer between 0 and 32.');
|
||||||
|
}
|
||||||
|
var octets = ip.split('.').map((e) => int.tryParse(e)).toList();
|
||||||
|
if( octets.length != 4 ) {
|
||||||
|
signal('Invalid IP address format. Expected 4 octets separated by dots.');
|
||||||
|
}
|
||||||
var hostBits = 32 - mask;
|
var hostBits = 32 - mask;
|
||||||
var shiftAmount = hostBits;
|
var shiftAmount = hostBits;
|
||||||
var networkValue = 0;
|
var networkValue = 0;
|
||||||
for (var i = 0; i < 4; i++) {
|
for (var i = 0; i < 4; i++) {
|
||||||
|
if (octets[i] == null || octets[i] < 0 || octets[i] > 255) {
|
||||||
|
signal('Invalid IP address. Each octet must be between 0 and 255.');
|
||||||
|
}
|
||||||
networkValue = (networkValue << 8) | octets[i];
|
networkValue = (networkValue << 8) | octets[i];
|
||||||
}
|
}
|
||||||
networkValue = (networkValue >> shiftAmount) << shiftAmount;
|
networkValue = (networkValue >> shiftAmount) << shiftAmount;
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,6 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
||||||
errorHandler.notify(e, stack);
|
errorHandler.notify(e, stack);
|
||||||
setState(() {
|
setState(() {
|
||||||
_errorMessage = e.toString();
|
_errorMessage = e.toString();
|
||||||
_isErrorExpanded = true; // Auto-expand on error
|
|
||||||
_result = null;
|
_result = null;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -254,7 +253,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
||||||
color: Theme.of(context).colorScheme.errorContainer,
|
color: Theme.of(context).colorScheme.errorContainer,
|
||||||
child: ExpansionTile(
|
child: ExpansionTile(
|
||||||
title: Text(
|
title: Text(
|
||||||
'⚠️ There were an error. Show details...',
|
'⚠️ There was an error. Show details...',
|
||||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
color: Theme.of(context).colorScheme.onErrorContainer,
|
color: Theme.of(context).colorScheme.onErrorContainer,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue