Skip to content

Commit 1ff098d

Browse files
BornToBeRootclaude
andcommitted
Feature: Run command for Subnet Calculator and Lookup
- Subnet Calculator (subnetcalculator, subnet, ipcalc): "subnet 10.0.0.0/24" calculates the subnet, a new subnet mask or CIDR as second argument ("subnet 10.0.0.0/16 /24") opens Subnetting, a second subnet ("subnet 10.0.0.0/24 10.0.1.0/24") opens Wide Subnet. - Lookup: "lookup oui <query>" and "lookup port <query>". The type is the first word after the command (RunCommandDefinition.TypeBeforeTarget), the rest of the input is the query, so vendors can contain spaces. Without a query, the lookup is only shown. Suggestions offer the lookups after "lookup " and preselect the matching one while typing. - Commands can opt out of profiles (Subnet Calculator, Lookup), the syntax hint then has no "@<Profile>". - Subnet Calculator and Lookup host views handle requests and select the tab (SubnetCalculatorRequest, LookupRequest). - Fix: Subnetting got stuck if the new subnet mask was smaller than the one of the subnet or too large for the address family (e.g. /40 for IPv4). An error is shown instead; the run command validates the same range (Subnetmask.ConvertSubnetmaskOrCidrToCidr). - Parser tests, docs and changelog. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent efdf21b commit 1ff098d

29 files changed

Lines changed: 422 additions & 39 deletions

‎Source/NETworkManager.Localization/Resources/Strings.Designer.cs‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Source/NETworkManager.Localization/Resources/Strings.resx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<root>
33
<!--
44
Microsoft ResX Schema
@@ -4887,4 +4887,7 @@ Cloudflare may log your IP address and network information. See Cloudflare's pri
48874887
<data name="Recent" xml:space="preserve">
48884888
<value>Recent</value>
48894889
</data>
4890+
<data name="EnterSubnetmaskOrCIDRBetweenXAndY" xml:space="preserve">
4891+
<value>Enter a subnet mask or CIDR between /{0} and /{1}!</value>
4892+
</data>
48904893
</root>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace NETworkManager.Models.Lookup;
2+
3+
/// <summary>
4+
/// Lookups of the Lookup application.
5+
/// </summary>
6+
public enum LookupType
7+
{
8+
OUI,
9+
Port
10+
}

‎Source/NETworkManager.Models/Network/Subnetmask.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Net;
3+
using NETworkManager.Utilities;
34

45
namespace NETworkManager.Models.Network;
56

@@ -37,4 +38,19 @@ public static int ConvertSubnetmaskToCidr(IPAddress subnetmask)
3738
{
3839
return string.Join("", IPv4Address.ToBinaryString(subnetmask.ToString()).Replace(".", "").TrimEnd('0')).Length;
3940
}
41+
42+
/// <summary>
43+
/// Converts a subnet mask (e.g. 255.255.255.0) or a CIDR (e.g. /24 or 24) to the CIDR. The value must be valid
44+
/// (see IPv4IPv6SubnetmaskOrCIDRValidator).
45+
/// </summary>
46+
/// <param name="subnetmaskOrCidr">Subnet mask or CIDR.</param>
47+
/// <returns>CIDR.</returns>
48+
public static int ConvertSubnetmaskOrCidrToCidr(string subnetmaskOrCidr)
49+
{
50+
subnetmaskOrCidr = subnetmaskOrCidr.Trim();
51+
52+
return RegexHelper.SubnetmaskRegex().IsMatch(subnetmaskOrCidr)
53+
? ConvertSubnetmaskToCidr(IPAddress.Parse(subnetmaskOrCidr))
54+
: int.Parse(subnetmaskOrCidr.TrimStart('/'));
55+
}
4056
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
namespace NETworkManager.Models.RunCommand;
22

33
/// <summary>
4-
/// Second argument of a run command after the target.
4+
/// Second argument of a run command after the target. <see cref="Subnet" /> is a new subnet mask or a second
5+
/// subnet (Subnet Calculator).
56
/// </summary>
67
public enum RunCommandArgument
78
{
89
None,
910
Ports,
1011
DNSServer,
11-
Broadcast
12+
Broadcast,
13+
Subnet
1214
}

‎Source/NETworkManager.Models/RunCommand/RunCommandDefinition.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public sealed class RunCommandDefinition
5050
/// </summary>
5151
public IReadOnlyList<string> Types { get; init; } = [];
5252

53+
/// <summary>
54+
/// The type keyword is required and is the first word after the command (e.g. "lookup port 22"). The rest of the
55+
/// input is the target, so it can contain spaces (e.g. "lookup oui Cisco Systems").
56+
/// </summary>
57+
public bool TypeBeforeTarget { get; init; }
58+
5359
/// <summary>
5460
/// Syntax of the arguments for the hint (e.g. "&lt;hosts&gt; [server] [type]"), empty if there are none.
5561
/// </summary>

‎Source/NETworkManager.Models/RunCommand/RunCommandParser.cs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public static partial class RunCommandParser
3232
/// - A value with spaces is written in quotes (e.g. a DNS server "Google Public DNS").
3333
/// - Further words are a type keyword of the command or its second argument (e.g. ports, DNS server), in any
3434
/// order.
35+
/// - Commands with <see cref="RunCommandDefinition.TypeBeforeTarget" /> have the type keyword before the target
36+
/// and the rest of the input as target ("lookup oui Cisco Systems").
3537
/// </summary>
3638
/// <param name="input">Input of the dialog.</param>
3739
/// <param name="commands">Available commands.</param>
@@ -78,7 +80,7 @@ public static RunCommandParseResult Parse(string input, IEnumerable<RunCommandDe
7880
{
7981
Command = command,
8082
CommandText = commandText,
81-
Position = RunCommandPosition.Target,
83+
Position = command.TypeBeforeTarget ? RunCommandPosition.Type : RunCommandPosition.Target,
8284
InputBeforeCurrentWord = commandPrefix
8385
};
8486

@@ -145,6 +147,28 @@ public static RunCommandParseResult Parse(string input, IEnumerable<RunCommandDe
145147
ErrorValue = command.Command
146148
};
147149

150+
// Type keyword before the target (e.g. "lookup port 22"), the rest of the input is the target.
151+
if (command.TypeBeforeTarget)
152+
{
153+
var typeKeyword = words[1].ToLowerInvariant();
154+
var typeBeforeTarget = command.Types.Contains(typeKeyword) ? typeKeyword : null;
155+
var target = words.Length > 2 ? string.Join(" ", words[2..]) : null;
156+
var isTypingType = words.Length == 2 && !endsWithWhitespace;
157+
158+
return new RunCommandParseResult
159+
{
160+
Command = command,
161+
CommandText = commandText,
162+
Target = target,
163+
Type = typeBeforeTarget,
164+
Position = isTypingType ? RunCommandPosition.Type : RunCommandPosition.Target,
165+
CurrentWord = isTypingType ? words[1] : target ?? string.Empty,
166+
InputBeforeCurrentWord = isTypingType ? commandPrefix : $"{commandPrefix}{tokens[1]} ",
167+
Error = typeBeforeTarget == null ? RunCommandParseError.UnknownArgument : RunCommandParseError.None,
168+
ErrorValue = typeBeforeTarget == null ? words[1] : null
169+
};
170+
}
171+
148172
// Further words: type keyword or second argument, in any order. A type keyword is never the second argument.
149173
string type = null;
150174
string argument = null;

‎Source/NETworkManager.Models/RunCommand/RunCommandPosition.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public enum RunCommandPosition
77
{
88
Command,
9+
Type,
910
Target,
1011
Argument
1112
}

‎Source/NETworkManager.Tests/RunCommand/RunCommandParserTests.cs‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ public class RunCommandParserTests
3232
new()
3333
{
3434
Type = RunCommandType.Application, Application = ApplicationName.SubnetCalculator,
35-
Command = "subnetcalculator"
35+
Command = "subnetcalculator", Aliases = ["subnet"], SupportsTarget = true,
36+
Argument = RunCommandArgument.Subnet
3637
},
38+
new()
39+
{
40+
Type = RunCommandType.Application, Application = ApplicationName.Lookup, Command = "lookup",
41+
SupportsTarget = true, Types = ["oui", "port"], TypeBeforeTarget = true
42+
},
43+
new() { Type = RunCommandType.Application, Application = ApplicationName.Firewall, Command = "firewall" },
3744
new() { Type = RunCommandType.Setting, Command = "settings" }
3845
];
3946

@@ -240,7 +247,7 @@ public void UnknownArgument(string input, string unknown)
240247
}
241248

242249
[Theory]
243-
[InlineData("subnetcalculator 10.0.0.0/24")]
250+
[InlineData("firewall 10.0.0.1")]
244251
[InlineData("settings general")]
245252
public void NoArguments(string input)
246253
{
@@ -314,4 +321,69 @@ public void PositionForSuggestions(string input, RunCommandPosition position, st
314321
Assert.Equal(currentWord, result.CurrentWord);
315322
Assert.Equal(before, result.InputBeforeCurrentWord);
316323
}
324+
325+
[Theory]
326+
[InlineData("subnet 10.0.0.0/24", "10.0.0.0/24", null)]
327+
[InlineData("subnet 10.0.0.0/16 /24", "10.0.0.0/16", "/24")]
328+
[InlineData("subnet 10.0.0.0/24 10.0.1.0/24", "10.0.0.0/24", "10.0.1.0/24")]
329+
public void SubnetWithNewSubnetmaskOrSubnet2(string input, string target, string argument)
330+
{
331+
var result = Parse(input);
332+
333+
Assert.Equal(RunCommandParseError.None, result.Error);
334+
Assert.Equal(target, result.Target);
335+
Assert.Equal(argument, result.Argument);
336+
}
337+
338+
[Theory]
339+
[InlineData("lookup port 22", "port", "22")]
340+
[InlineData("lookup PORT 22; 80; ssh", "port", "22;80;ssh")]
341+
[InlineData("lookup oui 00:11:22", "oui", "00:11:22")]
342+
[InlineData("lookup oui Cisco Systems", "oui", "Cisco Systems")]
343+
[InlineData("lookup oui \"Cisco Systems\"", "oui", "Cisco Systems")]
344+
[InlineData("lookup port", "port", null)]
345+
public void TypeBeforeTarget(string input, string type, string target)
346+
{
347+
var result = Parse(input);
348+
349+
Assert.Equal(RunCommandParseError.None, result.Error);
350+
Assert.Equal(type, result.Type);
351+
Assert.Equal(target, result.Target);
352+
}
353+
354+
[Theory]
355+
[InlineData("lookup po", "po")]
356+
[InlineData("lookup foo 22", "foo")]
357+
public void TypeBeforeTargetUnknownType(string input, string unknown)
358+
{
359+
var result = Parse(input);
360+
361+
Assert.Null(result.Type);
362+
Assert.Equal(RunCommandParseError.UnknownArgument, result.Error);
363+
Assert.Equal(unknown, result.ErrorValue);
364+
}
365+
366+
[Fact]
367+
public void TypeBeforeTargetNoProfiles()
368+
{
369+
var result = Parse("lookup @server");
370+
371+
Assert.Equal(RunCommandParseError.NoProfiles, result.Error);
372+
}
373+
374+
[Theory]
375+
[InlineData("lookup ", RunCommandPosition.Type, "", "lookup ")]
376+
[InlineData("lookup po", RunCommandPosition.Type, "po", "lookup ")]
377+
[InlineData("lookup port ", RunCommandPosition.Target, "", "lookup port ")]
378+
[InlineData("lookup port 2", RunCommandPosition.Target, "2", "lookup port ")]
379+
[InlineData("lookup oui Cisco Sys", RunCommandPosition.Target, "Cisco Sys", "lookup oui ")]
380+
public void TypeBeforeTargetPositionForSuggestions(string input, RunCommandPosition position, string currentWord,
381+
string before)
382+
{
383+
var result = Parse(input);
384+
385+
Assert.Equal(position, result.Position);
386+
Assert.Equal(currentWord, result.CurrentWord);
387+
Assert.Equal(before, result.InputBeforeCurrentWord);
388+
}
317389
}

‎Source/NETworkManager/ApplicationRequest.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DnsClient;
22
using NETworkManager.Models;
3+
using NETworkManager.Models.Lookup;
34
using NETworkManager.Models.RDAP;
45
using NETworkManager.Profiles;
56

@@ -72,3 +73,31 @@ public sealed record RDAPRequest() : ApplicationRequest(ApplicationName.RDAP)
7273
/// </summary>
7374
public RDAPQueryType? QueryType { get; init; }
7475
}
76+
77+
/// <summary>
78+
/// Request to open the Subnet Calculator. <see cref="ApplicationRequest.Target" /> is the subnet, which is calculated
79+
/// (or subnetted / combined with <see cref="NewSubnetmask" /> / <see cref="Subnet2" />).
80+
/// </summary>
81+
public sealed record SubnetCalculatorRequest() : ApplicationRequest(ApplicationName.SubnetCalculator)
82+
{
83+
/// <summary>
84+
/// New subnet mask or CIDR (e.g. "/26") to split the subnet into (Subnetting).
85+
/// </summary>
86+
public string NewSubnetmask { get; init; }
87+
88+
/// <summary>
89+
/// Second subnet to calculate the wide subnet with (Wide Subnet).
90+
/// </summary>
91+
public string Subnet2 { get; init; }
92+
}
93+
94+
/// <summary>
95+
/// Request to open the Lookup. Without a <see cref="ApplicationRequest.Target" />, only the lookup is shown.
96+
/// </summary>
97+
public sealed record LookupRequest() : ApplicationRequest(ApplicationName.Lookup)
98+
{
99+
/// <summary>
100+
/// Lookup to show (and run with the target).
101+
/// </summary>
102+
public LookupType Type { get; init; }
103+
}

0 commit comments

Comments
 (0)