Calculate the number of ports and validate ranges within TCP/UDP port allocations.
The 16-bit TCP/UDP port number space (0-65535) is divided by IANA into three categories: well-known ports (0-1023), reserved for system/standard services like HTTP (80) and SSH (22) and traditionally requiring elevated privileges to bind; registered ports (1024-49151), which applications and services can register with IANA for specific use (e.g. many database and application server defaults); and dynamic/private ports (49152-65535), used for ephemeral, temporary connections such as a client's outbound source port for a new connection. A port range can span one or more of these categories depending on where its start and end fall.
Total ports in range
total_ports = end_port − start_port + 1
On Unix-like systems, binding to ports below 1024 has historically required root/administrator privileges, a security convention meant to ensure that only trusted, system-level processes can claim these reserved 'standard service' ports (e.g. so a regular user process can't impersonate a web server on port 80).
Operating systems automatically assign a port from the dynamic/private range (49152-65535, sometimes called ephemeral ports) as the temporary source port for outbound client connections — for example, when your browser connects to a web server, it uses a random port from this range as its own source port for that connection.
Yes — while IANA maintains an official registry mapping many registered ports to specific well-known applications (e.g. 3306 for MySQL, 5432 for PostgreSQL) as a convention to avoid conflicts, nothing technically prevents any application from using an unregistered port in that range; it's a convention, not an enforced restriction.
Some legacy applications or custom services intentionally use wide port ranges that straddle category boundaries (e.g. an FTP passive-mode range spanning into both registered and dynamic ports), which is why understanding what categories a given range covers matters for both security review and troubleshooting connectivity issues.