Skip to content

dmki/subnetcalc

Repository files navigation

SubnetCalc - IPv4 Subnet Calculator

A Windows Forms application for calculating IPv4 subnet information based on IP address and CIDR mask bits.

Features

1. IPv4 Address Input

  • Four separate text boxes for entering each octet of an IPv4 address
  • Input validation: only numeric values 0-255 are accepted
  • Default IP: 123.123.234.132

2. Quick IP Entry Buttons

  • Clipboard Paste Button: Pastes IP address from clipboard (supports format xxx.xxx.xxx.xxx)
  • Public IP Button: Retrieves your public IP address from web services (api.ipify.org, icanhazip.com, or ifconfig.me)
  • Both buttons are 28x28 pixels with emoji icons

3. Mask Bits Slider

  • TrackBar control ranging from 0 to 32 bits
  • Default: 28 bits (matching example specification)
  • Real-time updates as slider moves
  • Current value displayed next to slider

4. Subnet Calculation Display

The application calculates and displays:

  • Subnet Bits: Number of bits borrowed from the host portion for subnetting (based on classful addressing)
  • Max Subnets: Maximum number of possible subnets (2^subnet_bits - 2)
  • Host Bits: Number of bits available for host addresses (32 - mask bits)
  • Max Hosts: Maximum usable host addresses per subnet (2^host_bits - 2)
  • Current Mask: Subnet mask in dotted decimal notation

5. IP Range Display

Shows detailed network information:

  • Subnet (network address)
  • Broadcast address
  • First usable host address
  • Last usable host address
  • Total usable hosts

Special cases handled:

  • /31 networks: Point-to-point links (RFC 3021) with 2 usable addresses
  • /32 networks: Single host addresses

6. Tooltips

Every control has descriptive tooltips explaining its purpose and usage.

Technical Implementation

Class-Based Subnet Calculation

The application uses classful IP addressing for subnet calculations:

  • Class A (0-127.x.x.x): Default /8 mask
  • Class B (128-191.x.x.x): Default /16 mask
  • Class C (192-223.x.x.x): Default /24 mask
  • Class D (224-239.x.x.x): Multicast (no subnetting)
  • Class E (240-255.x.x.x): Reserved (no subnetting)

Subnet bits are calculated as: Current Mask Bits - Class Default Mask Bits

Example Calculation

For IP 123.123.234.132 with /28 mask:

  • Class: A (default /8)
  • Subnet bits: 28 - 8 = 20
  • Max subnets: 2^20 - 2 = 1,048,574
  • Host bits: 32 - 28 = 4
  • Max hosts: 2^4 - 2 = 14
  • Mask: 255.255.255.240
  • Subnet: 123.123.234.128
  • Broadcast: 123.123.234.143

This matches the specification exactly.

Real-Time Updates

All calculations update automatically when:

  • Any IP octet value changes
  • The mask slider moves

Clipboard Integration

The paste button parses IP addresses in standard dotted decimal notation (xxx.xxx.xxx.xxx) from clipboard text.

Public IP Retrieval

The public IP button attempts to retrieve your external IP address using multiple fallback services:

  1. https://api.ipify.org
  2. https://icanhazip.com
  3. https://ifconfig.me/ip

If all services fail or timeout (5 second limit), it returns "127.0.0.1" as a fallback.

Building and Running

Requirements

  • .NET 9.0 SDK or later
  • Windows operating system

Build Commands

cd SubnetCalc
dotnet build
dotnet run

Output

The compiled executable is located at: bin/Debug/net9.0-windows/SubnetCalc.exe

Project Structure

SubnetCalc/
├── Form1.cs                 - Main form logic and event handlers
├── Form1.Designer.cs        - UI layout and control definitions
├── Program.cs               - Application entry point
├── SubnetCalc.csproj        - Project configuration
└── README.md                - This file

Code Architecture

Form1.cs Components

Event Handlers

  • Form1_Load: Initializes form and performs first calculation
  • IpOctet_TextChanged: Triggers recalculation when IP changes
  • IpOctet_KeyPress: Validates numeric input only
  • TrackBarMask_ValueChanged: Updates calculations when mask changes
  • BtnPasteClipboard_Click: Handles clipboard paste functionality
  • BtnGetPublicIp_Click: Retrieves public IP address asynchronously

Calculation Methods

  • GetClassMaskBits: Determines IP class and returns default mask bits
  • UpdateSubnetInfo: Main calculation engine - updates all display fields
  • GetCurrentIpAddress: Parses IP octets into 32-bit unsigned integer
  • IpToString: Converts 32-bit integer to dotted decimal notation
  • TryParseIpAddress: Validates and parses IP address strings

Network Methods

  • GetPublicIpAddressAsync: Asynchronous public IP retrieval with fallback

Notes

Emoji Icons

The buttons use Unicode emoji characters:

  • 📋 (Clipboard) for paste button
  • 🌐 (Globe) for public IP button

These should display correctly on Windows 10+ systems with proper font support.

Network Addressing Standards

The application follows historical subnet calculation methods where:

  • Subnet zero and all-ones subnets were reserved (hence the -2 in calculations)
  • Modern networks typically allow these subnets, but classic calculations exclude them

Validation

  • IP octets are validated to ensure values 0-255
  • Only numeric input is allowed in IP fields
  • Empty or invalid fields prevent calculations until corrected

About

Subnet calculator for Windows

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages