Skip to content

Instantly share code, notes, and snippets.

@majastrz
Last active June 27, 2023 22:05
Show Gist options
  • Save majastrz/bdd776addfa72c0719334996c0aa78f5 to your computer and use it in GitHub Desktop.
Save majastrz/bdd776addfa72c0719334996c0aa78f5 to your computer and use it in GitHub Desktop.
CIDR release notes

CIDR Functions

parseCidr(network: string)

Parses an IP address range in CIDR notation and returns an object containing various properties of the address range.

Parameter Required Type Description
network Yes string String containing an IP address range to convert in CIDR format

cidrSubnet(network: string, cidr: int, subnetIndex: int)

Splits the specified IP address range in CIDR notation into subnets with a new CIDR value and returns the IP address range of the subnet with the specified index.

Parameter Required Type Description
network Yes string String containing an IP address range to convert in CIDR format
cidr Yes int An integer representing the CIDR to be used to subnet
subnetIndex Yes int Index of the desired subnet IP address range to return

cidrHost(network: string, hostIndex: int)

Calculates the IP address of the host with the specified index on the specified IP address range in CIDR notation. Only considers usable IP addresses.

Parameter Required Type Description
network Yes string String containing an ip network to convert (must be correct networking format)
hostIndex Yes int The index of the host IP address to return.

Sample

/* 
  ----- IPv4 -----
*/

// parse IPv4 CIDR string
output v4info object = parseCidr('10.144.0.0/20')
/* 
Returns:
{
  network: '10.144.0.0'
  netmask: '255.255.240.0'
  broadcast: '10.144.15.255'
  firstUsable: '10.144.0.1'
  lastUsable: '10.144.15.254'
  cidr: 20
}
*/

// calculate the first five /24 subnet ranges from the specified /20
output v4subnets array = [for i in range(0, 5): cidrSubnet('10.144.0.0/20', 24, i)]
/*
Returns:
[
  '10.144.0.0/24'
  '10.144.1.0/24'
  '10.144.2.0/24'
  '10.144.3.0/24'
  '10.144.4.0/24'
]
*/

// calculate the first five usable host IP addresses from the specified /24
output v4hosts array = [for i in range(0, 5): cidrHost('10.144.3.0/24', i)]
/*
Returns:
[
  '10.144.3.1'
  '10.144.3.2'
  '10.144.3.3'
  '10.144.3.4'
  '10.144.3.5'
]
*/

/* 
  ----- IPv6 -----
*/

// parse IPv6 CIDR string
output v6info object = parseCidr('fdad:3236:5555::/48')
/*
Returns:
{
  network: 'fdad:3236:5555::'
  netmask: 'ffff:ffff:ffff::'
  firstUsable: 'fdad:3236:5555::'
  lastUsable: 'fdad:3236:5555:ffff:ffff:ffff:ffff:ffff'
  cidr: 48
}
*/

// calculate the first five /52 subnet ranges from the specified /48
output v6subnets array = [for i in range(0, 5): cidrSubnet('fdad:3236:5555::/48', 52, i)]
/*
Returns:
[
  'fdad:3236:5555::/52'
  'fdad:3236:5555:1000::/52'
  'fdad:3236:5555:2000::/52'
  'fdad:3236:5555:3000::/52'
  'fdad:3236:5555:4000::/52'
]
*/

// calculate the first five usable host IP addresses from the specified /52
output v6hosts array = [for i in range(0, 5): cidrHost('fdad:3236:5555:3000::/52', i)]
/*
Returns:
[
  'fdad:3236:5555:3000::1'
  'fdad:3236:5555:3000::2'
  'fdad:3236:5555:3000::3'
  'fdad:3236:5555:3000::4'
  'fdad:3236:5555:3000::5'
]
*/
@Lddeiva
Copy link

Lddeiva commented Jun 2, 2023

Hi @majastrz , I need a clarification on this as the first 3 host addresses are reserved by Azure.

// Reserved by Azure
  '10.144.3.1'
  '10.144.3.2'
  '10.144.3.3'

Does this mean that the consumers of this function should take this into consideration and plan the usable IP addresses properly?

It will be cool if the function can return the response as below.

// calculate the first five usable host IP addresses from the specified /24
output v4hosts array = [for i in range(0, 5): cidrHost('10.144.3.0/24', i)]

/*
Returns:
[
  '10.144.3.4'
  '10.144.3.5'
  '10.144.3.6'
  '10.144.3.7'
  '10.144.3.8'
]
*/

@majastrz
Copy link
Author

majastrz commented Jun 7, 2023

Yes, the first 3 host addresses are reserved in Azure subnets. See Azure/bicep#10822 for more details.

For variable size subnets, can you open a new issue to track the feature request?

@tstooke
Copy link

tstooke commented Jun 14, 2023

I would offer that forcing the function to skip the first 3 would limit it's application to non-Azure CIDR values, such as when calculating first/last usable for a CIDR from an external network to configure firewall rules. Those external networks don't necessarily skip the first 3 like Azure does.

If we need to skip the first few for an Azure CIDR, we should be able to just start the range at something higher than 0, correct?

@majastrz
Copy link
Author

I would offer that forcing the function to skip the first 3 would limit it's application to non-Azure CIDR values, such as when calculating first/last usable for a CIDR from an external network to configure firewall rules. Those external networks don't necessarily skip the first 3 like Azure does.

If we need to skip the first few for an Azure CIDR, we should be able to just start the range at something higher than 0, correct?

Yes and we have no intention of changing the current behavior of the cidrHost() function. However, we will likely add a new optional parameter to the function to let users customize the behavior.

@patild05
Copy link

I am trying to create subnet address spaces using cidrSubnet but my CIDR blocks are not same

param vnetAddressSpace string = '10.238.0.64/27'

output address1 string = cidrSubnet(vnetAddressSpace,28,0)
output address2 string = cidrSubnet(vnetAddressSpace,29,1)
output address3 string = cidrSubnet(vnetAddressSpace,29,2)

If I see the output, the values are
address1= 10.238.0.64/28
address2= 10.238.0.72/29
address3= 10.238.0.80/29

As you can see the address2 is wrong. Is this a bug or a limitation?

@majastrz
Copy link
Author

@patild05 Can you open an issue for this? It'll be easier to have a conversation about this particular issue and easier to track on our side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment