Scoop API

scoop:scoop.platform:0.1.0

src/net/network.scoop

Selected production source · src/net/network.scoop

1package scoop.platform.net
3import scoop.text.*
5/**
6 * An IP prefix whose stored network address has host bits cleared. Membership requires a
7 * compatible address family and matching prefix.
8 */
9public class IpNetwork public constructor(
10 address: IpAddress,
11 /**
12 * The number of leading address bits belonging to the network prefix.
13 */
14 public val prefixLength: Int
15): Equals<IpNetwork>, Hash, ToString {
16 /**
17 * The normalized network address with host bits cleared.
18 */
19 public val address: IpAddress = networkAddress(address, prefixLength)
20 /**
21 * The network's address family.
22 */
23 public val family: AddressFamily get() = address.family
25 /**
26 * Tests whether a compatible candidate shares this network prefix.
27 */
28 public fun contains(candidate: IpAddress): Boolean {
29 if (candidate.family != family)
30 return false
31 val expected = address.toByteArray()
32 val actual = candidate.toByteArray()
33 for (index in 0L ..< expected.size) {
34 val mask = networkMask(prefixLength, index)
35 if ((actual[index].toInt() and mask) != (expected[index].toInt() and 255))
36 return false
37 }
38 return true
39 }
41 /**
42 * Compares the complete normalized address identity, including its relevant family, scope,
43 * prefix, or port.
44 */
45 public override operator fun equals(other: IpNetwork): Boolean =
46 prefixLength == other.prefixLength && address == other.address
47 /**
48 * Hashes the same address identity used by equality.
49 */
50 public override fun hash(): Long = address.hash() * 31L + prefixLength.hash()
51 /**
52 * Returns numeric address notation without performing name resolution.
53 */
54 public override fun toString(): String = address.toString() + "/" + prefixLength.toString()
56 /**
57 * Parses numeric CIDR network notation.
58 */
59 public companion object {
60 /**
61 * Parses an IP address and prefix length, validating the family-specific range.
62 */
63 public fun parse(text: String): IpNetwork {
64 val separator = text.indexOf('/')
65 if (separator <= 0L)
66 throw IllegalArgumentException(
67 Some("CIDR requires an IP address and prefix length")
68 )
69 val prefix = decimal(text.slice(separator + 1L, text.charLength))
70 if (prefix > 128u)
71 throw IllegalArgumentException(Some("CIDR prefix length is out of range"))
72 return IpNetwork(IpAddress.parse(text.slice(0L, separator)), prefix.toInt())
73 }
74 }
77private fun networkAddress(address: IpAddress, prefixLength: Int): IpAddress {
78 val bits =
79 if (address.family == AddressFamily.Ipv4)
80 32
81 else
82 128
83 if (prefixLength < 0 || prefixLength > bits || address.scopeId != 0u)
84 throw IllegalArgumentException(
85 Some("CIDR requires an unscoped address and valid prefix length")
86 )
87 val bytes = address.toByteArray()
88 return IpAddress.fromByteArray(
89 Array<Byte>(bytes.size) {
90 index -> (bytes[index].toInt() and networkMask(prefixLength, index)).toByte()
91 }
92 )
95private fun networkMask(prefixLength: Int, index: Long): Int {
96 val remaining = prefixLength.toLong() - index * 8L
97 return if (remaining <= 0L)
98 0
99 else
100 if (remaining >= 8L)
101 255
102 else
103 (255 shl (8L - remaining)) and 255