// 子网计算 - 绿色工具箱 // C# 5.0 语法,零依赖。输入 IP/CIDR 或 IP/掩码,输出网段信息 using System; using System.Drawing; using System.Net; using System.Text; using System.Windows.Forms; namespace GreenToolbox { public class SubnetToolForm : Form { private static readonly Color COL_GREEN = Color.FromArgb(0, 145, 88); private static readonly Color COL_BG = Color.White; private static readonly Color COL_PANEL = Color.FromArgb(245, 249, 246); private static readonly Color COL_GRAY = Color.FromArgb(120, 130, 124); private TextBox txtIp; private TextBox txtMask; private TextBox txtOutput; private Button btnCalc; private Button btnCopy; private Label lblStatus; public SubnetToolForm() { InitUi(); } private void InitUi() { this.Text = "子网计算 - 绿色工具箱"; this.Font = new Font("Microsoft YaHei UI", 9.5f); this.BackColor = COL_BG; this.ForeColor = Color.FromArgb(34, 40, 36); this.ClientSize = new Size(700, 470); this.MinimumSize = new Size(620, 430); int x = 22, y = 18; Label l0 = new Label(); l0.Text = "输入 IP 和掩码位数(如 192.168.1.66 / 24),或直接填子网掩码"; l0.AutoSize = true; l0.ForeColor = COL_GRAY; l0.Location = new Point(x, y); this.Controls.Add(l0); y += 34; Label li = new Label(); li.Text = "IP 地址:"; li.AutoSize = true; li.Location = new Point(x, y + 5); this.Controls.Add(li); txtIp = new TextBox(); txtIp.Location = new Point(x + 78, y); txtIp.Size = new Size(240, 27); txtIp.Font = new Font("Consolas", 11f); this.Controls.Add(txtIp); Label lm = new Label(); lm.Text = "掩码/位数:"; lm.AutoSize = true; lm.Location = new Point(x + 336, y + 5); this.Controls.Add(lm); txtMask = new TextBox(); txtMask.Location = new Point(x + 418, y); txtMask.Size = new Size(120, 27); txtMask.Font = new Font("Consolas", 11f); txtMask.Text = "24"; this.Controls.Add(txtMask); btnCalc = new Button(); btnCalc.Text = "计算"; btnCalc.Location = new Point(x + 552, y - 1); btnCalc.Size = new Size(100, 29); btnCalc.BackColor = COL_GREEN; btnCalc.ForeColor = Color.White; btnCalc.FlatStyle = FlatStyle.Flat; btnCalc.FlatAppearance.BorderSize = 0; btnCalc.Click += new EventHandler(CalcClick); this.Controls.Add(btnCalc); y += 44; txtOutput = new TextBox(); txtOutput.Location = new Point(x, y); txtOutput.Size = new Size(654, 300); txtOutput.Multiline = true; txtOutput.ReadOnly = true; txtOutput.ScrollBars = ScrollBars.Vertical; txtOutput.BackColor = COL_PANEL; txtOutput.Font = new Font("Consolas", 10.5f); this.Controls.Add(txtOutput); y += 312; btnCopy = new Button(); btnCopy.Text = "复制结果"; btnCopy.Location = new Point(x, y); btnCopy.Size = new Size(110, 30); btnCopy.FlatStyle = FlatStyle.Flat; btnCopy.FlatAppearance.BorderColor = COL_GREEN; btnCopy.ForeColor = Color.FromArgb(0, 110, 66); btnCopy.Click += new EventHandler(CopyClick); this.Controls.Add(btnCopy); lblStatus = new Label(); lblStatus.Text = ""; lblStatus.Location = new Point(x + 124, y + 7); lblStatus.AutoSize = true; this.Controls.Add(lblStatus); this.AcceptButton = btnCalc; } private void CalcClick(object sender, EventArgs e) { lblStatus.Text = ""; uint ip; if (!ParseIp(txtIp.Text.Trim(), out ip)) { lblStatus.Text = "IP 格式不对,例:192.168.1.66"; return; } uint mask; string maskInput = txtMask.Text.Trim(); if (maskInput.Contains(".")) { if (!ParseIp(maskInput, out mask)) { lblStatus.Text = "掩码格式不对"; return; } } else { int bits; if (!int.TryParse(maskInput, out bits) || bits < 0 || bits > 32) { lblStatus.Text = "位数范围 0-32"; return; } mask = bits == 0 ? 0u : 0xFFFFFFFFu << (32 - bits); } string[] f = ComputeFields(ip, mask); StringBuilder sb = new StringBuilder(); sb.AppendLine("网络地址 " + f[0]); sb.AppendLine("子网掩码 " + f[1] + " (/" + CountBits(mask) + ")"); sb.AppendLine("通配符掩码 " + ToIp(~mask)); sb.AppendLine("广播地址 " + f[2]); sb.AppendLine("可用主机范围 " + f[3] + " ~ " + f[4]); sb.AppendLine("可用主机数 " + f[5]); sb.AppendLine("地址总数 " + f[6]); sb.AppendLine(""); sb.AppendLine("IP 类别 " + IpClass(ip)); sb.AppendLine("是否私网 " + (IsPrivate(ip) ? "是(RFC1918 内网地址)" : "否(公网地址)")); sb.AppendLine("二进制 IP " + Convert.ToString(ip, 2).PadLeft(32, '0').Insert(8, ".").Insert(17, ".").Insert(26, ".")); sb.AppendLine("二进制掩码 " + Convert.ToString(mask, 2).PadLeft(32, '0').Insert(8, ".").Insert(17, ".").Insert(26, ".")); txtOutput.Text = sb.ToString(); lblStatus.Text = "计算完成。"; } // 核心计算(供 UI 与 --selftest 共用):网络地址/掩码/广播/首可用/末可用/可用数/总数 public static string[] ComputeFields(uint ip, uint mask) { uint network = ip & mask; uint broadcast = network | (~mask); // 注意:~mask 是"最大主机位值",地址总数 = ~mask + 1(/24 → 255+1=256) uint hosts = ~mask; long usable = (hosts >= 2) ? (long)hosts - 1 : 0; string first = hosts >= 2 ? ToIp(network + 1) : "无"; string last = hosts >= 2 ? ToIp(broadcast - 1) : "无"; return new string[] { ToIp(network), ToIp(mask), ToIp(broadcast), first, last, usable.ToString("N0"), ((long)hosts + 1).ToString("N0") }; } // 命令行自测:0=全过 1=有失败,结果写 selftest-subnet.txt public static int RunSelfTest() { string[,] tests = new string[,] { // 输入IP 位数 期望网络 期望广播 期望可用数 { "192.168.1.66", "24", "192.168.1.0", "192.168.1.255", "254" }, { "10.0.0.1", "8", "10.0.0.0", "10.255.255.255", "16,777,214" }, { "192.168.1.66", "26", "192.168.1.64", "192.168.1.127", "62" }, { "172.16.5.9", "20", "172.16.0.0", "172.16.15.255", "4,094" }, { "8.8.8.8", "32", "8.8.8.8", "8.8.8.8", "0" } }; int pass = 0, total = tests.GetLength(0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < total; i++) { uint ip, mask; if (!ParseIpStatic(tests[i, 0], out ip)) { sb.AppendLine("解析失败 " + tests[i, 0]); continue; } int bits = int.Parse(tests[i, 1]); mask = bits == 0 ? 0u : 0xFFFFFFFFu << (32 - bits); string[] f = ComputeFields(ip, mask); bool ok = f[0] == tests[i, 2] && f[2] == tests[i, 3] && f[5] == tests[i, 4]; if (ok) pass++; else sb.AppendLine(tests[i, 0] + "/" + bits + " 期望网" + tests[i, 2] + "/广" + tests[i, 3] + "/用" + tests[i, 4] + " 实际网" + f[0] + "/广" + f[2] + "/用" + f[5]); } System.IO.File.WriteAllText("selftest-subnet.txt", "pass=" + pass + "/" + total + "\r\n" + sb.ToString(), Encoding.UTF8); return pass == total ? 0 : 1; } private static bool ParseIpStatic(string s, out uint ip) { IPAddress a; if (IPAddress.TryParse(s, out a) && a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { byte[] b = a.GetAddressBytes(); ip = ((uint)b[0] << 24) | ((uint)b[1] << 16) | ((uint)b[2] << 8) | b[3]; return true; } ip = 0; return false; } private void CopyClick(object sender, EventArgs e) { if (txtOutput.Text.Length > 0) { Clipboard.SetText(txtOutput.Text); lblStatus.Text = "已复制。"; } } private bool ParseIp(string s, out uint ip) { ip = 0; IPAddress a; if (IPAddress.TryParse(s, out a) && a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { byte[] b = a.GetAddressBytes(); ip = ((uint)b[0] << 24) | ((uint)b[1] << 16) | ((uint)b[2] << 8) | b[3]; return true; } return false; } private static string ToIp(uint v) { return ((v >> 24) & 255) + "." + ((v >> 16) & 255) + "." + ((v >> 8) & 255) + "." + (v & 255); } private int CountBits(uint v) { int c = 0; while (v != 0) { c += (int)(v & 1); v >>= 1; } return c; } private string IpClass(uint ip) { uint f = (ip >> 24) & 255; if (f < 128) return "A 类"; if (f < 192) return "B 类"; if (f < 224) return "C 类"; if (f < 240) return "D 类(组播)"; return "E 类(保留)"; } private bool IsPrivate(uint ip) { uint f = (ip >> 24) & 255; uint s = (ip >> 16) & 255; if (f == 10) return true; if (f == 172 && s >= 16 && s <= 31) return true; if (f == 192 && s == 168) return true; if (f == 127) return true; return false; } } static class Program { [STAThread] static void Main(string[] args) { if (args != null && args.Length > 0 && args[0] == "--selftest") { Environment.Exit(SubnetToolForm.RunSelfTest()); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new SubnetToolForm()); } } }