-
Notifications
You must be signed in to change notification settings - Fork 806
Expand file tree
/
Copy pathSubnetmask.cs
More file actions
40 lines (31 loc) · 993 Bytes
/
Subnetmask.cs
File metadata and controls
40 lines (31 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Collections.Generic;
using System.Net;
namespace NETworkManager.Models.Network;
public static class Subnetmask
{
public static List<SubnetmaskInfo> List => GenerateList();
private static List<SubnetmaskInfo> GenerateList()
{
var list = new List<SubnetmaskInfo>();
for (var i = 2; i < 31; i++)
list.Add(GetFromCidr(i));
return list;
}
public static SubnetmaskInfo GetFromCidr(int cidr)
{
return new SubnetmaskInfo
{
CIDR = cidr,
Subnetmask = ConvertCidrToSubnetmask(cidr)
};
}
public static string ConvertCidrToSubnetmask(int cidr)
{
var bits = new string('1', cidr);
return IPv4Address.ToHumanString(bits.PadRight(32, '0'));
}
public static int ConvertSubnetmaskToCidr(IPAddress subnetmask)
{
return string.Join("", IPv4Address.ToBinaryString(subnetmask.ToString()).Replace(".", "").TrimEnd('0')).Length;
}
}