RingDotNetfor Ring

IP networks

Which addresses belong to a network? Is this client allowed in? The IPNetwork type answers these questions with the CIDR notation that firewalls, cloud consoles and configuration files use. This lesson parses networks, counts their addresses, tests addresses and builds a small allow list.

What you will learn#

The tutorial Network tools used IPAddress and IPEndPoint. This lesson goes one level up: a range of addresses. A web program (the tutorial A web service with a database) uses it to allow or refuse a client by its address.

The program#

samples/UsingDotNet/tutorials/ch07_ip_networks.ring
# IP networks - CIDR notation, the addresses of a subnet, "is this address in that network?" and a small allow list
load "dotnet.ring"

using {
	"System"
	"System.Net"
}

func main

	# A network is written in CIDR form: an address and the number of bits that are the same for the whole network.
	# 192.168.1.0/24 means: the first 24 bits (192.168.1) are fixed, the last 8 bits (0 to 255) are free.
	oNetworkClass = new IPNetwork()
	aParsed = oNetworkClass.TryParse("192.168.1.0/24")
	oNetwork = aParsed[2]
	? "1. network: " + oNetwork.ToString()
	? "   base address: " + oNetwork.BaseAddress.ToString() + ", prefix length: " + oNetwork.PrefixLength

	# 2. Contains answers whether an address belongs to the network
	oIP = new IPAddress()
	? nl + "2. 192.168.1.77 is inside:  " + oNetwork.Contains(oIP.Parse("192.168.1.77"))
	? "   192.168.2.5 is inside:   " + oNetwork.Contains(oIP.Parse("192.168.2.5"))
	? "   192.168.1.255 is inside: " + oNetwork.Contains(oIP.Parse("192.168.1.255")) + "   (the broadcast address is part of the range)"

	# 3. The number of addresses is 2 to the power of (32 - prefix). Smaller networks have a longer prefix.
	? nl + "3. addresses in a network:"
	for cText in ["10.0.0.0/8","172.16.0.0/12","192.168.0.0/16","192.168.1.0/24","192.168.1.0/28","192.168.1.5/32"]
		aOne = oNetworkClass.TryParse(cText)
		? "   " + padRight(cText,17) + addressCount(aOne[2].PrefixLength)
	next

	# 4. A wrong network is refused: the address must be the BASE of the network (the free bits are zero)
	? nl + "4. 192.168.1.77/24 is a network: " + oNetworkClass.TryParse("192.168.1.77/24")[1] + "   (the last bits are not zero)"
	? "   192.168.1.0/33 is a network: " + oNetworkClass.TryParse("192.168.1.0/33")[1] + "   (IPv4 has 32 bits)"
	? "   192.168.1.0/24 is a network: " + oNetworkClass.TryParse("192.168.1.0/24")[1]

	# 5. An allow list: the private ranges of RFC 1918, and a check that says whether an address is private
	aPrivate = ["10.0.0.0/8","172.16.0.0/12","192.168.0.0/16"]
	? nl + "5. which of these addresses are private:"
	for cAddress in ["10.4.4.4","172.20.1.1","172.32.0.1","192.168.200.9","8.8.8.8","127.0.0.1"]
		? "   " + padRight(cAddress,15) + isPrivate(cAddress,aPrivate)
	next

	# 6. IPv6 works the same way
	oV6 = oNetworkClass.TryParse("2001:db8::/32")[2]
	? nl + "6. 2001:db8:1::5 is in 2001:db8::/32: " + oV6.Contains(oIP.Parse("2001:db8:1::5")) + ", 2001:db9::1: " + oV6.Contains(oIP.Parse("2001:db9::1"))

func addressCount nPrefix

	return floor(pow(2,32 - nPrefix))

func isPrivate cAddress,aRanges

	oNet = new IPNetwork()
	oAddress = new IPAddress().Parse(cAddress)
	for cRange in aRanges
		if oNet.TryParse(cRange)[2].Contains(oAddress)
			return "private (" + cRange + ")"
		ok
	next
	return "not in the private ranges"

func padRight cText,nWidth

	return cText + copy(" ",nWidth - len(cText))
Output
1. network: 192.168.1.0/24
   base address: 192.168.1.0, prefix length: 24

2. 192.168.1.77 is inside:  1
   192.168.2.5 is inside:   0
   192.168.1.255 is inside: 1   (the broadcast address is part of the range)

3. addresses in a network:
   10.0.0.0/8       16777216
   172.16.0.0/12    1048576
   192.168.0.0/16   65536
   192.168.1.0/24   256
   192.168.1.0/28   16
   192.168.1.5/32   1

4. 192.168.1.77/24 is a network: 0   (the last bits are not zero)
   192.168.1.0/33 is a network: 0   (IPv4 has 32 bits)
   192.168.1.0/24 is a network: 1

5. which of these addresses are private:
   10.4.4.4       private (10.0.0.0/8)
   172.20.1.1     private (172.16.0.0/12)
   172.32.0.1     not in the private ranges
   192.168.200.9  private (192.168.0.0/16)
   8.8.8.8        not in the private ranges
   127.0.0.1      not in the private ranges

6. 2001:db8:1::5 is in 2001:db8::/32: 1, 2001:db9::1: 0

How it works#

CIDR notation#

An IPv4 address has 32 bits. A network is written as an address and a prefix length: the number of bits at the start that are the same for every address of the network. In 192.168.1.0/24 the first 24 bits (192.168.1) are fixed and the last 8 bits are free, so the network holds the addresses 192.168.1.0 to 192.168.1.255. IPNetwork.TryParse(text) answers [ success, network ] (the tutorial Network tools); the network has BaseAddress, PrefixLength and ToString().

Contains#

oNetwork.Contains(address) tells whether an IPAddress is inside the range. The first and the last address of the range (192.168.1.0 and 192.168.1.255) count as inside; in a real network they are the network and the broadcast address and are not given to computers, but Contains only looks at the bits.

How many addresses#

A network with prefix length p has 2 to the power of (32 − p) addresses. /8 has 16 777 216, /16 has 65 536, /24 has 256, /28 has 16 and /32 has just one address (a single computer). A longer prefix means a smaller network. For IPv6 the number is 2 to the power of (128 − p), which is too big for a Ring number, so a program counts bits instead.

A wrong network#

The address in the text must be the base of the network: the free bits must be zero. 192.168.1.77/24 is refused because 77 has bits in the last 8 positions; write 192.168.1.0/24. A prefix longer than 32 (/33) is refused for IPv4. TryParse is a yes or no, so the check is safe for text that a user typed.

An allow list#

Step 5 keeps the three private ranges of RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) in a Ring list and checks an address against each. 172.20.1.1 is private and 172.32.0.1 is not, because the second range ends at 172.31.255.255 — the kind of boundary that is easy to get wrong with text comparison and impossible to get wrong with Contains. 127.0.0.1 is loopback, not private, so it is not in the list.

Warning

The address a program sees may not be the address of the real client. Behind a proxy or a load balancer every request seems to come from the proxy; the real address is in a header such as X-Forwarded-For, and that header can be written by the client. Trust it only when your own proxy sets it.

IPv6#

The same type works for IPv6: 2001:db8::/32 holds every address that starts with 2001:db8, so 2001:db8:1::5 is inside and 2001:db9::1 is not.

What you learned#

Exercises#

  1. Write networkOf(cAddress, nPrefix) that returns the network that contains an address.
  2. Write an allow list that reads its networks from a settings file (the tutorial Settings).
  3. Use the list in the tutorial A tiny web framework so that only addresses of 127.0.0.0/8 can see the page.
  4. Print the first and last address of 192.168.1.64/26.

Next#

Sockets

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.