or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

address-types.mdformat-conversion.mdindex.mdipv4.mdipv6.mdspecial-analysis.mdsubnet-operations.md
tile.json

subnet-operations.mddocs/

0

# Subnet Operations

1

2

Advanced subnet analysis including network/broadcast address calculation, host range determination, and subnet containment testing for both IPv4 and IPv6 addresses.

3

4

## Capabilities

5

6

### Subnet Range Calculations

7

8

Calculate network boundaries and host ranges within subnets.

9

10

```typescript { .api }

11

/**

12

* Get the first address in the subnet (Network Address)

13

* Available on both Address4 and Address6 instances

14

* @returns Address instance representing the network address

15

*/

16

startAddress(): Address4 | Address6;

17

18

/**

19

* Get the first host address in the subnet (after Network Address)

20

* Available on both Address4 and Address6 instances

21

* @returns Address instance representing the first usable host address

22

*/

23

startAddressExclusive(): Address4 | Address6;

24

25

/**

26

* Get the last address in the subnet (Broadcast Address for IPv4)

27

* Available on both Address4 and Address6 instances

28

* @returns Address instance representing the last address in range

29

*/

30

endAddress(): Address4 | Address6;

31

32

/**

33

* Get the last host address in the subnet (before Broadcast for IPv4)

34

* Available on both Address4 and Address6 instances

35

* @returns Address instance representing the last usable host address

36

*/

37

endAddressExclusive(): Address4 | Address6;

38

```

39

40

**IPv4 Examples:**

41

42

```typescript

43

import { Address4 } from "ip-address";

44

45

const network = new Address4("192.168.1.0/24");

46

47

console.log(network.startAddress().correctForm()); // "192.168.1.0" (Network)

48

console.log(network.startAddressExclusive().correctForm()); // "192.168.1.1" (First host)

49

console.log(network.endAddress().correctForm()); // "192.168.1.255" (Broadcast)

50

console.log(network.endAddressExclusive().correctForm()); // "192.168.1.254" (Last host)

51

52

// Smaller subnet

53

const smallNet = new Address4("10.0.0.0/30");

54

console.log(smallNet.startAddress().correctForm()); // "10.0.0.0"

55

console.log(smallNet.startAddressExclusive().correctForm()); // "10.0.0.1"

56

console.log(smallNet.endAddress().correctForm()); // "10.0.0.3"

57

console.log(smallNet.endAddressExclusive().correctForm()); // "10.0.0.2"

58

```

59

60

**IPv6 Examples:**

61

62

```typescript

63

import { Address6 } from "ip-address";

64

65

const network = new Address6("2001:db8::/64");

66

67

console.log(network.startAddress().correctForm()); // "2001:db8::" (Network)

68

console.log(network.startAddressExclusive().correctForm()); // "2001:db8::1" (First host)

69

console.log(network.endAddress().correctForm()); // "2001:db8::ffff:ffff:ffff:ffff" (Last)

70

console.log(network.endAddressExclusive().correctForm()); // "2001:db8::ffff:ffff:ffff:fffe" (Last host)

71

72

// Smaller IPv6 subnet

73

const smallNet = new Address6("2001:db8:1234::/126");

74

console.log(smallNet.startAddress().correctForm()); // "2001:db8:1234::"

75

console.log(smallNet.endAddress().correctForm()); // "2001:db8:1234::3"

76

```

77

78

### Subnet Containment Testing

79

80

Determine if addresses belong to specific subnets.

81

82

```typescript { .api }

83

/**

84

* Check if another address is contained within this address's subnet

85

* Available on both Address4 and Address6 instances

86

* @param address - Address to test for containment

87

* @returns true if the address is within this subnet

88

*/

89

isInSubnet(address: Address4 | Address6): boolean;

90

```

91

92

**IPv4 Examples:**

93

94

```typescript

95

const network = new Address4("192.168.1.0/24");

96

const host1 = new Address4("192.168.1.100");

97

const host2 = new Address4("192.168.2.100");

98

99

console.log(network.isInSubnet(host1)); // true (within 192.168.1.0/24)

100

console.log(network.isInSubnet(host2)); // false (different network)

101

102

// Test with different subnet sizes

103

const largeNet = new Address4("10.0.0.0/8");

104

const mediumNet = new Address4("10.1.0.0/16");

105

const host = new Address4("10.1.2.3");

106

107

console.log(largeNet.isInSubnet(host)); // true

108

console.log(largeNet.isInSubnet(mediumNet)); // true (subnet within subnet)

109

console.log(mediumNet.isInSubnet(largeNet)); // false (larger subnet not in smaller)

110

```

111

112

**IPv6 Examples:**

113

114

```typescript

115

const network = new Address6("2001:db8::/32");

116

const host1 = new Address6("2001:db8:1234::1");

117

const host2 = new Address6("2001:db9::1");

118

119

console.log(network.isInSubnet(host1)); // true (within 2001:db8::/32)

120

console.log(network.isInSubnet(host2)); // false (different prefix)

121

122

// IPv6 with different prefix lengths

123

const site = new Address6("2001:db8:1000::/48");

124

const subnet = new Address6("2001:db8:1000:1::/64");

125

const host = new Address6("2001:db8:1000:1::100");

126

127

console.log(site.isInSubnet(subnet)); // true

128

console.log(site.isInSubnet(host)); // true

129

console.log(subnet.isInSubnet(host)); // true

130

```

131

132

### Subnet Masking Operations

133

134

Work with subnet masks and extract specific bit ranges.

135

136

```typescript { .api }

137

/**

138

* Get the first n bits of the address as a binary string

139

* Defaults to the subnet mask length if no parameter provided

140

* Available on both Address4 and Address6 instances

141

* @param mask - Number of bits to include in mask (optional)

142

* @returns Binary string representing the masked portion

143

*/

144

mask(mask?: number): string;

145

146

/**

147

* Get bits in specified range as binary string

148

* Available on both Address4 and Address6 instances

149

* @param start - Starting bit position (0-based)

150

* @param end - Ending bit position (exclusive)

151

* @returns Binary string of the specified bit range

152

*/

153

getBitsBase2(start: number, end: number): string;

154

155

/**

156

* Get bits beyond the subnet mask (IPv6 only)

157

* @returns Binary string of host portion beyond subnet mask

158

*/

159

getBitsPastSubnet(): string;

160

```

161

162

**IPv4 Examples:**

163

164

```typescript

165

const addr = new Address4("192.168.1.100/24");

166

167

console.log(addr.mask()); // First 24 bits: "110000001010100000000001"

168

console.log(addr.mask(16)); // First 16 bits: "1100000010101000"

169

console.log(addr.getBitsBase2(0, 8)); // First octet: "11000000"

170

console.log(addr.getBitsBase2(24, 32)); // Last octet: "01100100"

171

172

// Network portion vs host portion

173

const network = new Address4("10.1.2.0/24");

174

console.log(network.mask(24)); // Network portion (24 bits)

175

console.log(network.getBitsBase2(24, 32)); // Host portion (8 bits)

176

```

177

178

**IPv6 Examples:**

179

180

```typescript

181

const addr = new Address6("2001:db8:1234::abcd/64");

182

183

console.log(addr.mask()); // First 64 bits (network portion)

184

console.log(addr.mask(48)); // First 48 bits (site prefix)

185

console.log(addr.getBitsBase2(0, 16)); // First group

186

console.log(addr.getBitsBase2(64, 128)); // Host portion (interface ID)

187

188

// IPv6 with getBitsPastSubnet for host portion

189

console.log(addr.getBitsPastSubnet()); // Host bits beyond subnet mask

190

```

191

192

### IPv6 Subnet Calculations

193

194

IPv6-specific subnet analysis and calculations.

195

196

```typescript { .api }

197

/**

198

* Calculate the number of possible subnets of a given size

199

* Available on Address6 instances only

200

* @param subnetSize - Target subnet size (default: 128)

201

* @returns String representation of the number of possible subnets

202

*/

203

possibleSubnets(subnetSize?: number): string;

204

```

205

206

**IPv6 Examples:**

207

208

```typescript

209

const prefix = new Address6("2001:db8::/32");

210

211

// How many /64 subnets can fit in a /32?

212

console.log(prefix.possibleSubnets(64)); // "4,294,967,296" (2^32)

213

214

// How many /48 subnets can fit in a /32?

215

console.log(prefix.possibleSubnets(48)); // "65,536" (2^16)

216

217

// How many /128 addresses can fit in a /64?

218

const subnet = new Address6("2001:db8:1::/64");

219

console.log(subnet.possibleSubnets(128)); // "18,446,744,073,709,551,616" (2^64)

220

221

// Edge case: subnet size larger than available space

222

console.log(subnet.possibleSubnets(48)); // "0" (can't fit /48 in /64)

223

```

224

225

### Practical Subnet Examples

226

227

Common subnet operations and use cases.

228

229

**IPv4 Subnet Planning:**

230

231

```typescript

232

// CIDR block analysis

233

const block = new Address4("203.0.113.0/24");

234

235

console.log(`Network: ${block.startAddress().correctForm()}`);

236

console.log(`First Host: ${block.startAddressExclusive().correctForm()}`);

237

console.log(`Last Host: ${block.endAddressExclusive().correctForm()}`);

238

console.log(`Broadcast: ${block.endAddress().correctForm()}`);

239

240

// Check if specific IPs are in range

241

const ips = ["203.0.113.1", "203.0.113.100", "203.0.113.255", "203.0.114.1"];

242

ips.forEach(ip => {

243

const addr = new Address4(ip);

244

console.log(`${ip} in subnet: ${block.isInSubnet(addr)}`);

245

});

246

```

247

248

**IPv6 Prefix Delegation:**

249

250

```typescript

251

// ISP allocation to customer

252

const ispBlock = new Address6("2001:db8::/32");

253

const customerPrefix = new Address6("2001:db8:1234::/48");

254

const customerSubnet = new Address6("2001:db8:1234:1::/64");

255

const host = new Address6("2001:db8:1234:1::100");

256

257

console.log(`Customer prefix in ISP block: ${ispBlock.isInSubnet(customerPrefix)}`);

258

console.log(`Subnet in customer prefix: ${customerPrefix.isInSubnet(customerSubnet)}`);

259

console.log(`Host in subnet: ${customerSubnet.isInSubnet(host)}`);

260

261

// Calculate how many /64s the customer can create

262

console.log(`Available /64 subnets: ${customerPrefix.possibleSubnets(64)}`);

263

```

264

265

**Mixed IPv4/IPv6 Operations:**

266

267

```typescript

268

// IPv6-mapped IPv4 addresses in subnets

269

const mappedAddr = Address6.fromAddress4("192.168.1.100/24");

270

console.log(mappedAddr.correctForm()); // "::ffff:c0a8:164"

271

272

// Extract IPv4 from mapped address and test subnet

273

const ipv4Net = new Address4("192.168.1.0/24");

274

const extractedV4 = mappedAddr.to4();

275

console.log(`IPv4 ${extractedV4.correctForm()} in subnet: ${ipv4Net.isInSubnet(extractedV4)}`);

276

```