An IP address is the number that names one network interface and tells routers where to deliver a packet. An IPv4 address has 32 bits and is written as four numbers, such as 172.20.1.57; an IPv6 address has 128 bits. Every address has a network part, which routers use to pick a path, and a host part, which picks one interface on that network.
In this article we plan the network of a coaching campus in Hyderabad. It has 300 lab PCs, Wi-Fi, CCTV cameras and a small server room, all behind one router and a single block of public addresses from the internet provider. We turn addresses into numbers, carve the campus into subnets, watch a router choose between overlapping routes, fill a NAT table until it runs out of ports and finish with IPv6. Six runnable programs in Java (with Python and C++ versions) and one Java-only program give real numbers for each step.
Key takeaways
An IPv4 address is just a 32-bit number:
172.20.1.57is 2,886,992,185. The private ranges hold 17,891,328 of the 4,294,967,296 possible addresses, only 0.42% of the space.A /26 holds 64 addresses but only 62 are usable. Three hundred lab PCs need a /23 and waste 210 addresses. A plan for six networks used 756 of the 1,024 addresses in a /22.
Two campuses that reuse the same plan overlap, and the router cannot tell which side owns an address. Three of the five ranges we tested clashed with the campus block.
A router picks the longest matching prefix, not the first one. A packet for
172.20.2.150matched three routes and the /26 won. Summarizing nine branch routes left five, because adjacent blocks only merge when they are aligned.With NAT, ten public ports served ten different connections and the next two were dropped. A real public address has about 64,512 ports per destination, so six addresses give 387,072 flows.
One IPv6
/48holds 65,536 subnets of/64, and a single/64is 4,294,967,296 times the whole IPv4 space.Java has its own traps: IP bytes are signed (203 prints as -53),
isSiteLocalAddress()is false for the carrier-grade range, and an IPv4-mapped IPv6 literal becomes anInet4Address.
1. What Is an IP Address?
A network trip needs two answers: who is the destination, and which way do I send the packet next. The IP address gives both. It names an interface, the network card of a server, the Wi-Fi adapter of a phone, a virtual card in a VM or a container. A machine with two cards has two addresses, and a container may get a new address every time it starts.
That is why an IP address is not an identity. It says where an interface is attached to the network right now, not who is using it. Many students behind one router share one public address, and one laptop moving from Wi-Fi to a mobile hotspot changes its address in a second.
This lesson follows OSI Model, where IP is layer 3. Later lessons build on it: TCP vs UDP adds ports on top of addresses, DNS turns names into addresses and Proxy vs Reverse Proxy explains why your server often sees the wrong client address.
2. The Scenario We Will Follow
The campus has one router. The internet provider gave it a small block, 203.0.113.64/29: eight addresses, of which the provider's gateway uses .65 and our router's outside port uses .66. (These are reserved documentation addresses, so no real network is touched.) Inside, the campus uses the private block 172.20.0.0/22, which has 1,024 addresses, split into five networks.
The campus: one public block outside, one private block split into networks inside.
The whole design question is how to share 1,024 private addresses between networks of very different sizes, how the router knows where each packet goes, and how 300 private machines reach the internet through a handful of public addresses.
3. An IPv4 Address Is a 32-Bit Number
The dotted form is only a reading aid. The machine sees four 8-bit groups, called octets, in one 32-bit number. To get the number from the dots, take each octet in turn and compute value * 256 + octet.
The same address as dots, bits and a single number.
The first program does the conversion in both directions, prints the bits, and classifies fifteen addresses by the standard special ranges. Run it and look at the Kind column.
Output
Address Value Kind
172.20.1.57 2886992185 private (RFC 1918)
172.31.255.255 2887778303 private (RFC 1918)
172.32.0.1 2887778305 public
192.168.0.1 3232235521 private (RFC 1918)
192.169.0.1 3232301057 public
10.250.3.4 184156932 private (RFC 1918)
100.72.9.4 1682442500 shared, carrier-grade NAT
169.254.7.9 2851997449 link-local
127.0.0.53 2130706485 loopback
203.0.113.66 3405803842 documentation
1.1.1.1 16843009 public
224.0.0.251 3758096635 multicast
240.0.0.1 4026531841 reserved
255.255.255.255 4294967295 limited broadcast
0.0.0.0 0 this network
Bits of three addresses
172.20.1.57 10101100.00010100.00000001.00111001
172.32.0.1 10101100.00100000.00000000.00000001
203.0.113.66 11001011.00000000.01110001.01000010
Private space: 17891328 of 4294967296 addresses = 0.42%
172.20.1.57 = 172*2^24 + 20*2^16 + 1*2^8 + 57 = 2886992185
172.16.0.0/12 ends at 172.31.255.255, and 172.32.0.0 is public
Three results are worth noticing. 172.32.0.1 looks private but is public, because the private block 172.16.0.0/12 ends at 172.31.255.255. 192.169.0.1 is public for the same reason. And 100.72.9.4 belongs to a different range, used by internet providers for carrier-grade NAT, which is not private in the RFC 1918 sense but is not public either.
4. Special Ranges You Should Recognise
Not every address can be used for a normal host. Some ranges are reserved for a fixed purpose, and seeing one in a log tells you a lot.
| Range | Meaning | Routed on the internet? |
|---|---|---|
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 | Private networks (RFC 1918) | No |
100.64.0.0/10 | Shared space for carrier-grade NAT (RFC 6598) | No |
127.0.0.0/8 | Loopback, this machine only | No |
169.254.0.0/16 | Link-local, self-assigned when DHCP fails (RFC 3927) | No |
192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24 | Documentation examples (RFC 5737) | No |
224.0.0.0/4 | Multicast | Only with multicast routing |
240.0.0.0/4 | Reserved for future use | No |
255.255.255.255 | Limited broadcast on the local link | No |
A laptop that shows 169.254.x.x did not get an answer from a DHCP server. Loopback 127.0.0.1 never leaves the machine, so a service bound only to it cannot be reached from another host, a common surprise for Java developers who start a server on localhost and then try to open it from a phone.
5. Network Part and Host Part: Masks and CIDR
An address has two parts. The leading bits are the network part, shared by every interface on that network. The remaining bits are the host part, which numbers the interfaces inside it. The subnet mask says where the split is: ones for the network bits, zeros for the host bits.
Early IP fixed the split by the first bits of the address (class A, B and C), which wasted huge blocks. Since 1993 the split can sit anywhere. CIDR notation writes the number of network bits after a slash, so 172.20.1.57/26 means 26 network bits and 6 host bits, the same as the mask 255.255.255.192.
The prefix splits the address; the network part decides direct delivery or the router.
This split is what a host does before every send. It applies its own mask to the destination. If the result equals its own network, it sends straight to the neighbour. If not, it hands the packet to the default gateway, which is the router.
6. Reading a Prefix: Network, Broadcast and Usable Hosts
For any prefix you can work out four numbers. The network address has all host bits zero. The broadcast address has all host bits one. The usable hosts are everything in between, so a block of 2^(32 - prefix) addresses has two fewer usable ones. Two prefixes break this rule: a /31 is used on point-to-point links where both addresses are hosts (RFC 3021), and a /32 names exactly one host.
The second program applies those rules to one campus host under nine different prefix lengths, finds the smallest prefix for several host counts and shows how the mask changes the answer to "are these two on the same network".
Output
Host 172.20.1.57 under different prefix lengths
Len Mask Size Usable Network Broadcast First host Last host
/22 255.255.252.0 1024 1022 172.20.0.0 172.20.3.255 172.20.0.1 172.20.3.254
/23 255.255.254.0 512 510 172.20.0.0 172.20.1.255 172.20.0.1 172.20.1.254
/25 255.255.255.128 128 126 172.20.1.0 172.20.1.127 172.20.1.1 172.20.1.126
/26 255.255.255.192 64 62 172.20.1.0 172.20.1.63 172.20.1.1 172.20.1.62
/27 255.255.255.224 32 30 172.20.1.32 172.20.1.63 172.20.1.33 172.20.1.62
/28 255.255.255.240 16 14 172.20.1.48 172.20.1.63 172.20.1.49 172.20.1.62
/30 255.255.255.252 4 2 172.20.1.56 172.20.1.59 172.20.1.57 172.20.1.58
/31 255.255.255.254 2 2 172.20.1.56 172.20.1.57 172.20.1.56 172.20.1.57
/32 255.255.255.255 1 1 172.20.1.57 172.20.1.57 172.20.1.57 172.20.1.57
Smallest prefix that fits N hosts
2 hosts -> /30 (2 usable, 0 wasted)
10 hosts -> /28 (14 usable, 4 wasted)
20 hosts -> /27 (30 usable, 10 wasted)
60 hosts -> /26 (62 usable, 2 wasted)
120 hosts -> /25 (126 usable, 6 wasted)
300 hosts -> /23 (510 usable, 210 wasted)
1000 hosts -> /22 (1022 usable, 22 wasted)
172.20.1.57 and 172.20.1.190 share a network under:
/22 -> same network, direct delivery
/24 -> same network, direct delivery
/25 -> different networks, via router
/26 -> different networks, via router
Read the two lower sections carefully. Three hundred hosts do not fit in a /24 (254 usable), so they take a /23 and waste 210 addresses. And 172.20.1.57 and 172.20.1.190 are neighbours under a /24 but strangers under a /25, because the host part of the second address crosses the halfway mark of 128.
7. Planning Subnets with VLSM
If every network got the same size, the labs would need a /23 and the router link would waste hundreds of addresses. VLSM, variable-length subnet masks, gives each network the smallest block that fits. The rule that makes it work is simple: sort by size, place the largest first and start every block on a multiple of its own size, so blocks never straddle a boundary.
One /22 pool carved into six networks, with the remainder kept free.
Output
Plan for 172.20.0.0/22 (1024 addresses)
Name Need Len Network First host Last host Wasted
Labs 300 /23 172.20.0.0 172.20.0.1 172.20.1.254 210
Wi-Fi 120 /25 172.20.2.0 172.20.2.1 172.20.2.126 6
CCTV 60 /26 172.20.2.128 172.20.2.129 172.20.2.190 2
Servers 20 /27 172.20.2.192 172.20.2.193 172.20.2.222 10
Management 10 /28 172.20.2.224 172.20.2.225 172.20.2.238 4
Router link 2 /30 172.20.2.240 172.20.2.241 172.20.2.242 0
Allocated 756 of 1024 addresses, 268 left
Next free address: 172.20.2.244
Peering check against the campus 172.20.0.0/22
Second campus, same plan 172.20.0.0/22 OVERLAP, cannot peer
Second campus, shifted 172.20.2.0/23 OVERLAP, cannot peer
Second campus, new block 172.21.0.0/22 no overlap
Cloud VPC 10.30.0.0/16 no overlap
Home VPN pool 172.20.3.128/25 OVERLAP, cannot peer
The plan uses 756 of the 1,024 addresses and leaves 268 free from 172.20.2.244. The largest waste is the labs network, which holds 510 usable addresses for 300 PCs; that is deliberate headroom for growth, and cheaper than renumbering a whole lab later.
8. Overlapping Ranges: Why VPN and Peering Fail
The second half of the third program is the failure that costs real companies days. When the campus opens a second branch, someone copies the working plan. Now both sites use 172.20.0.0/22.
Two sites with the same range cannot be connected, because no router can tell which side owns an address.
The program tested five candidate ranges against the campus block. Three clash, including a home VPN pool tucked inside the campus range, and only the new 172.21.0.0/22 and the cloud 10.30.0.0/16 are safe. The same check applies when you peer two cloud networks or connect a data centre to a cloud: the ranges must not overlap, and most cloud providers refuse the peering when they do.
The fix is organisational, not technical. Keep one address register for the whole company, give each site and each cloud account a block from it and leave gaps for growth.
9. How a Router Picks a Route: Longest Prefix Match
A router holds a table of prefixes, each with a next hop. A destination can match several of them at once, because a default route matches everything and a campus route matches a whole block. The rule is the longest prefix match: among all matching routes, the one with the most network bits wins, because it is the most specific.
Three routes match, and the most specific one forwards the packet.
Output
Longest prefix match on the campus router
Destination Matches Chosen Next hop Note
172.20.0.77 3 /23 via 172.20.0.2 labs switch
172.20.1.200 3 /23 via 172.20.0.2 labs switch
172.20.2.77 3 /25 via 172.20.2.2 Wi-Fi controller
172.20.2.150 3 /26 via 172.20.2.130 CCTV gateway
172.20.2.230 2 /22 connected whole campus
172.20.3.10 2 /22 connected whole campus
10.50.7.9 3 /24 via 10.50.7.1 branch router
10.50.8.9 2 /16 via vpn0 branch VPN
1.1.1.1 1 /0 via 203.0.113.65 default to the ISP
Branch routes before summarizing: 9
After summarizing: 5
10.60.4.0/22
10.60.9.0/24
10.60.10.0/23
10.60.13.0/24
10.60.14.0/24
Look at the Matches column. 172.20.2.150 matches the default route, the /22 for the whole campus and the /26 for the CCTV network, and the /26 wins. 172.20.2.230 has no specific route (the management network is directly connected), so it falls back to the /22. The default route 0.0.0.0/0 only wins for 1.1.1.1, where it is the single match.
10. Route Summarization
Routers keep tables in fast memory, and every route also has to be advertised to neighbours. Route summarization replaces many contiguous routes with one shorter prefix. The second half of the fourth program summarizes nine branch routes.
Four /24 routes become one /22; two adjacent routes stay apart because they are not aligned.
Two prefixes can merge only when they have the same length, they sit next to each other and the lower one starts on a multiple of the combined size. The program merged 10.60.4.0 to 10.60.7.0 into one /22 and 10.60.10.0 with 10.60.11.0 into a /23. But 10.60.13.0/24 and 10.60.14.0/24 are adjacent and still cannot merge: a /23 starting at 13 would not be a valid block. Nine routes became five. Planning blocks on the right boundaries at the start is what makes summarization possible later.
11. Public, Private and NAT
Private addresses are not routed on the internet, so a lab PC at 172.20.1.57 cannot receive a reply from a public server on its own. NAT, network address translation (RFC 3022), fixes this at the router. The most common form also rewrites the port, so many private hosts share one public address. Each connection gets its own public port, and the router remembers which private address and port it belongs to.
The router rewrites the source on the way out and uses the port to find the owner on the way back.
The remote server sees only 203.0.113.66:40000. It cannot reach the lab PC directly, and an unsolicited packet from the internet finds no table entry and is dropped. That is why NAT also behaves like a basic firewall, although it was designed to save addresses and not for security.
12. A NAT Table in Action, and Its Limits
The fifth program simulates the campus router with a deliberately tiny pool of ten public ports, so we can see the table fill up. Thirteen connection attempts come from the lab and Wi-Fi networks. Two lab PCs even use the same source port, 51234, which is fine because the table key includes the private address.
Output
Outbound connections through 203.0.113.66, ports 40000-40009
#1 172.20.1.57:51234 -> 198.51.100.40:443 203.0.113.66:40000 (new)
#2 172.20.1.58:51234 -> 198.51.100.40:443 203.0.113.66:40001 (new)
#3 172.20.1.57:51234 -> 198.51.100.40:443 203.0.113.66:40000 (reused)
#4 172.20.2.14:40112 -> 198.51.100.77:80 203.0.113.66:40002 (new)
#5 172.20.2.20:50000 -> 198.51.100.40:443 203.0.113.66:40003 (new)
#6 172.20.2.21:50001 -> 198.51.100.40:443 203.0.113.66:40004 (new)
#7 172.20.2.22:50002 -> 198.51.100.40:443 203.0.113.66:40005 (new)
#8 172.20.2.23:50003 -> 198.51.100.40:443 203.0.113.66:40006 (new)
#9 172.20.2.24:50004 -> 198.51.100.40:443 203.0.113.66:40007 (new)
#10 172.20.2.25:50005 -> 198.51.100.40:443 203.0.113.66:40008 (new)
#11 172.20.2.26:50006 -> 198.51.100.40:443 203.0.113.66:40009 (new)
#12 172.20.2.27:50007 -> 198.51.100.40:443 DROPPED, no free port
#13 172.20.2.28:50008 -> 198.51.100.40:443 DROPPED, no free port
Table size: 10 mappings, 0 ports free
Inbound packets
203.0.113.66:40001 -> forwarded to 172.20.1.58:51234
203.0.113.66:40003 -> forwarded to 172.20.2.20:50000
203.0.113.66:40022 -> dropped, no mapping
Real capacity: 64512 ports per public IP per destination
A /29 gives 6 usable addresses: 387072 flows to one destination
Connection 3 repeats connection 1 and reuses its public port, connections 12 and 13 are dropped because no port is left, and a reply to port 40022 is dropped because nothing owns it. The same limit exists in the real world, only larger. A public address has about 64,512 ports (1,024 to 65,535) per destination address and port, and the six usable addresses of a /29 allow 387,072 flows to one server. On Linux the default ephemeral range is narrower, commonly 32768 to 60999, so check the real value with the operating system, not the textbook number.
Port exhaustion is a real outage for a busy service that opens many short outgoing connections to one API. The fix is to reuse connections (HTTP keep-alive, a connection pool in your Java client), add public addresses to the NAT pool, or give workloads a route that avoids NAT.
13. Static, Dynamic and Reserved Addresses
An address reaches an interface in one of two ways. A static address is typed in by an administrator. A dynamic address is leased by a DHCP server, which hands out an address, mask, default gateway and DNS server for a fixed time (RFC 2131). The exchange has four steps, usually remembered as DORA.
Discover, offer, request, acknowledge, then renew at half the lease.
DHCP messages run over UDP, with the server on port 67 and the client on port 68. A laptop with no address cannot unicast, so its first message is a broadcast. If no server answers, it gives up and uses a self-assigned 169.254.x.x address, which is why that range in a log or a screenshot is a clue for troubleshooting.
For servers, the usual choices are a static address or a DHCP reservation, which always gives the same address to the same hardware address. Printers, cameras and databases should not change address by accident. Laptops, phones and lab PCs can stay dynamic.
14. IPv6: Addresses and Subnets
IPv4 has about 4.3 billion addresses, which is why NAT exists. IPv6 uses 128 bits, written as eight groups of four hex digits (RFC 4291). To keep them readable, leading zeros in a group can go, and one run of all-zero groups can be written as ::. RFC 5952 adds a canonical form: lower case, and the longest run of zeros is the one compressed, the first one if there is a tie.
An IPv6 address split into the provider prefix, the campus subnet ID and the interface ID.
Output
Expand and compress (RFC 5952 rules)
2001:0db8:0000:0000:0000:ff00:0042:8329 2001:0db8:0000:0000:0000:ff00:0042:8329 2001:db8::ff00:42:8329
2001:db8:0:0:1:0:0:1 2001:0db8:0000:0000:0001:0000:0000:0001 2001:db8::1:0:0:1
fe80:0:0:0:0:0:0:1 fe80:0000:0000:0000:0000:0000:0000:0001 fe80::1
::1 0000:0000:0000:0000:0000:0000:0000:0001 ::1
:: 0000:0000:0000:0000:0000:0000:0000:0000 ::
2001:db8:acad:5::25 2001:0db8:acad:0005:0000:0000:0000:0025 2001:db8:acad:5::25
::ffff:cb00:7109 0000:0000:0000:0000:0000:ffff:cb00:7109 ::ffff:cb00:7109
One /64 per VLAN from the ISP block 2001:db8:acad::/48
VLAN 0 -> 2001:db8:acad::/64
VLAN 1 -> 2001:db8:acad:1::/64
VLAN 2 -> 2001:db8:acad:2::/64
VLAN 3 -> 2001:db8:acad:3::/64
VLAN 4 -> 2001:db8:acad:4::/64
VLAN 5 -> 2001:db8:acad:5::/64
Host 2001:db8:acad:5::25 lives in 2001:db8:acad:5::/64
Subnet counts
/48 holds 65536 subnets of size /64
/56 holds 256 subnets of size /64
/60 holds 16 subnets of size /64
One /64 has 2^64 addresses, which is 4294967296 times the whole IPv4 space (2^32)
The second row is the tie rule at work: 2001:db8:0:0:1:0:0:1 has two runs of two zero groups and only the first is compressed. The last row is an IPv4-mapped address for 203.0.113.9. The campus gets one /48 from its provider and uses one /64 per VLAN, which leaves room for 65,536 networks. Hosts are not counted any more: one /64 has 2^64 addresses, 4,294,967,296 times all of IPv4. With addresses this plentiful, each device can have a public address and the whole NAT table disappears; the job of filtering moves to a firewall.
| Property | IPv4 | IPv6 |
|---|---|---|
| Address size | 32 bits | 128 bits |
| Written as | Four decimal octets | Eight hex groups, :: allowed |
| Typical subnet | Sized to the hosts, such as /26 | /64 for every network |
| Address sharing | NAT is everywhere | Not needed |
| Documentation range | 203.0.113.0/24 and two more | 2001:db8::/32 |
15. Dual Stack and Moving Between Versions
Most networks run both versions at once, called dual stack. A server listens on both an IPv4 and an IPv6 address, and a client picks whichever works. The old and the new world also meet inside one socket: when a dual-stack socket accepts an IPv4 client, the operating system shows the client as an IPv4-mapped IPv6 address, ::ffff:a.b.c.d. Your code has to handle that form in logs and in allow lists.
16. IP Addresses in Java
In Java the class for an address is java.net.InetAddress, with two subclasses, Inet4Address and Inet6Address. The last program runs on Java 21 and uses only address literals, so it makes no DNS lookup and no network call. It prints the properties Java reports for ten addresses, and then shows the byte trap.
Output
Literal Ver Site Loop Link Multi getHostAddress()
172.20.1.57 v4 true false false false 172.20.1.57
100.72.9.4 v4 false false false false 100.72.9.4
127.0.0.1 v4 false true false false 127.0.0.1
169.254.7.9 v4 false false true false 169.254.7.9
224.0.0.251 v4 false false false true 224.0.0.251
203.0.113.66 v4 false false false false 203.0.113.66
::1 v6 false true false false 0:0:0:0:0:0:0:1
fe80::1 v6 false false true false fe80:0:0:0:0:0:0:1
2001:db8::1 v6 false false false false 2001:db8:0:0:0:0:0:1
::ffff:203.0.113.9 v4 false false false false 203.0.113.9
203.0.113.66 as signed bytes: -53 0 113 66
203.0.113.66 as unsigned bytes: 203 0 113 66
Built from bytes: 172.20.1.57, equals parsed literal: true
Bytes are signed.
getAddress()returns abyte[], and Java bytes run from -128 to 127, so 203 prints as -53. Always mask withb & 0xFFbefore showing or comparing an octet.Site-local means three private ranges.
isSiteLocalAddress()is true for172.20.1.57but false for100.72.9.4, the carrier-grade range, and for IPv6 it checks the deprecatedfec0::/10block, not the unique local rangefc00::/7.Mapped addresses collapse.
::ffff:203.0.113.9came back as anInet4Address, so code that checksinstanceof Inet6Addresswill treat a mapped client as IPv4.IPv6 text is not compressed.
getHostAddress()returned2001:db8:0:0:0:0:0:1. Do not compare addresses as strings; compare theInetAddressobjects or parse them first.
Parsing a hostname with InetAddress.getByName("example.com") does a DNS lookup and can block, so never call it on a request thread without a timeout. Parsing a literal does not.
17. IP Addresses in System Design
These ideas show up in every cloud design. A virtual network gets a CIDR block, public and private subnets divide it, and routing decides what can talk to what.
A three-layer layout: only the load balancer is public, and NAT serves outbound traffic.
Keep databases private. Only the load balancer lives in the public subnet. Application servers and the database get private addresses and no route from the internet.
Plan the CIDR first. Choose a block that does not overlap with offices, other clouds or partners, and leave room for new subnets. Changing a network's range later usually means rebuilding it.
The client address may be a proxy. Behind a load balancer,
HttpServletRequest.getRemoteAddr()returns the balancer, not the user. The real address arrives in a header such asX-Forwarded-For, and you should trust it only when the request came from your own proxy.Do not use an IP as identity. One mobile provider can put thousands of users behind one public address, and one user changes address when moving between networks. Rate limits by address alone punish many innocent users.
Log addresses carefully. Keep both the proxy address and the forwarded client address, with the time, because NAT and DHCP reassign addresses.
18. Troubleshooting IP Problems
Work from the host outwards, one hop at a time, and the first failing step names the problem. Start with the address the machine actually has: ipconfig on Windows, ip addr on Linux. Then test the gateway, the next router and the destination.
Each step rules out one part of the path; the first red box names the fault.
19. Common Mistakes
Assuming all of
172.x.x.xis private. Only172.16.0.0/12is.Treating the network and broadcast addresses as usable hosts.
Giving two sites the same private range and discovering it when the VPN is already built.
Sizing a subnet for today's host count with no growth room.
Comparing IPv6 addresses as strings, and printing Java address bytes without masking.
Trusting
X-Forwarded-Forfrom any client, which lets an attacker choose the address your code sees.
20. Best Practices
Keep one address register for every site, cloud account and VPN, and allocate blocks on power-of-two boundaries.
Use VLSM: largest network first, each block aligned to its own size, free space kept at the end.
Keep servers on static addresses or DHCP reservations, and everything else on DHCP.
Keep data stores in private subnets and send outbound traffic through NAT.
Reuse outgoing connections so NAT ports last longer.
Plan for dual stack, and store and compare addresses as parsed values.
21. Practice Problems
In the second program, add a /29 for the same host
172.20.1.57. What are the network and broadcast addresses, and how many hosts are usable? Hint: a /29 holds 8 addresses and 57 is in the block starting at 56.Add a seventh network, "Library" with 40 hosts, to the VLSM plan. Which prefix does it get, and where does it start? Hint: it sorts between CCTV and Servers, which shifts the later blocks.
Add the route
172.20.2.192/27to the router table of the fourth program. Which route does172.20.2.200use now? Hint: it now matches a /27, which is longer than the /22.In the NAT program, change the port range to 40000-40019. How many of the thirteen connections succeed, and how many table entries exist at the end? Hint: reused connections do not add entries.
22. Interview Questions
How many usable hosts does a /26 have? 62: the block holds 64 addresses and the network and broadcast addresses are reserved.
What is the difference between a public and a private address? Private addresses (RFC 1918) are not routed on the internet and can be reused by any organisation. Public addresses are globally unique.
How does a router choose between two matching routes? It uses the longest prefix match, the most specific route, and falls back to the default route only when nothing else matches.
What does NAT do, and what is its main limit? It rewrites the source address and port so many private hosts share a public address. It is limited by the number of ports per public address and destination, and it breaks inbound connections that have no table entry.
Why do VPN and VPC peering fail with overlapping CIDR blocks? A router cannot tell which side owns an address, so traffic stays local. You have to renumber one side.
What does a 169.254.x.x address mean? The host could not reach a DHCP server and gave itself a link-local address.
How do you find the client's IP behind a load balancer in a Java web app? Read the forwarded header set by your own proxy, for example
X-Forwarded-For, and ignore it when the request did not come from that proxy.getRemoteAddr()shows the balancer.How is IPv6 different for subnetting? Every network is a /64, a site gets a /48 or /56, and address sharing through NAT is not needed.
Summary
An IP address names an interface and tells routers where it is attached; the prefix separates the network part from the host part.
Every prefix gives a network address, a broadcast address and a fixed number of usable hosts.
VLSM sizes each subnet to its hosts, and overlapping ranges break every VPN and peering.
Routers choose the longest matching prefix and summarize aligned blocks into fewer routes.
NAT shares one public address through ports; DHCP leases addresses; IPv6 removes the shortage.
In Java, mask address bytes, mind the site-local check, and trust forwarded addresses only from your own proxy.
Conclusion
IP addressing looks like a table of numbers, but every cloud design and every outage starts here: which network is this machine in, which route will a packet take, and who else shares this address. Keep the plan on paper, check the ranges before you connect two networks and read addresses carefully in logs. Continue with TCP vs UDP to see how ports and reliability sit on top of addresses, or return to the HLD course home to pick another topic.
Previous topic: OSI Model. Next: TCP vs UDP.
References and further reading
RFC 791: Internet Protocol and RFC 4632: Classless Inter-domain Routing (CIDR).
RFC 1918: Address Allocation for Private Internets, RFC 6598: IANA-Reserved IPv4 Prefix for Shared Address Space and RFC 3927: Link-Local Addresses.
RFC 5737: IPv4 Documentation Address Blocks and RFC 3021: Using 31-Bit Prefixes on Point-to-Point Links.
RFC 3022: Traditional IP Network Address Translator and RFC 2131: Dynamic Host Configuration Protocol.
RFC 4291: IPv6 Addressing Architecture, RFC 5952: Recommendation for IPv6 Address Text Representation and RFC 3849: IPv6 Address Prefix Reserved for Documentation.
AlgoMaster: IP Addresses (a general overview with different examples).