Scoop API

scoop:scoop.platform:0.1.0

src/net/address.scoop

Selected production source · src/net/address.scoop

1package scoop.platform.net
3import scoop.platform.fs.withNativeString
4import scoop.platform.fs.copyNativeString
5import scoop.platform.system.OsString
6import scoop.text.*
8/**
9 * The supported IP address families.
10 */
11public enum AddressFamily {
12 /**
13 * The four-byte IPv4 address family.
14 */
15 Ipv4,
16 /**
17 * The sixteen-byte IPv6 address family.
18 */
19 Ipv6
22/**
23 * An immutable numeric IP address with family and optional IPv6 scope identifier. Parsing does not
24 * perform DNS resolution.
25 */
26public class IpAddress private constructor(internal val native: NativeSocketAddress): Equals<
27 IpAddress
28>, Hash, ToString {
29 /**
30 * The address's IPv4 or IPv6 family.
31 */
32 public val family: AddressFamily get() =
33 if (native.family == 4)
34 AddressFamily.Ipv4
35 else
36 AddressFamily.Ipv6
37 /**
38 * The IPv6 scope identifier, or zero when no scope is selected.
39 */
40 public val scopeId: UInt get() = native.scopeId
42 /**
43 * Copies the four or sixteen address bytes in network order.
44 */
45 public fun toByteArray(): Array<Byte> =
46 Array<Byte>(
47 if (native.family == 4)
48 4L
49 else
50 16L
51 ) {
52 index ->
53 val word =
54 when (index / 4L) {
55 case 0L -> native.a
56 case 1L -> native.b
57 case 2L -> native.c
58 case _ -> native.d
59 }
60 ((word shr ((3L - index % 4L) * 8L)) and 255u).toByte()
61 }
63 /**
64 * Compares the complete normalized address identity, including its relevant family, scope,
65 * prefix, or port.
66 */
67 public override operator fun equals(other: IpAddress): Boolean =
68 native.family ==
69 other.native.family &&
70 native.scopeId ==
71 other.native.scopeId &&
72 native.a ==
73 other.native.a &&
74 native.b ==
75 other.native.b &&
76 native.c ==
77 other.native.c &&
78 native.d ==
79 other.native.d
81 /**
82 * Hashes the same address identity used by equality.
83 */
84 public override fun hash(): Long =
85 (
86 ((native.a.hash() * 31L + native.b.hash()) * 31L + native.c.hash()) *
87 31L +
88 native.d.hash()
89 ) *
90 31L +
91 native.scopeId.hash() +
92 native.family.toLong()
94 /**
95 * Returns numeric address notation without performing name resolution.
96 */
97 public override fun toString(): String {
98 val bytes = MutableArray<Byte>(46L) { 0.toByte() }
99 @Unsafe
100 {
101 val text =
102 bytes.withDataPointer {
103 pointer,
104 _ -> @Unsafe
105 {
106 checkSocketError("format IP address", nativeFormat(native, pointer))
107 return@withDataPointer copyNativeString(pointer).decodeUtf8()
108 }
109 }
110 return if (scopeId == 0u)
111 text
112 else
113 text + "%" + scopeId.toString()
114 }
115 }
117 /**
118 * Numeric address parsing, construction, and wildcard/loopback constants.
119 */
120 public companion object {
121 /**
122 * The IPv4 unspecified address for wildcard binding.
123 */
124 public val IPV4_ANY: IpAddress = IpAddress(NativeSocketAddress(4, 0, 0u, 0u, 0u, 0u, 0u))
125 /**
126 * The IPv4 loopback address.
127 */
128 public val IPV4_LOOPBACK: IpAddress =
129 IpAddress(NativeSocketAddress(4, 0, 0u, 0x7f000001u, 0u, 0u, 0u))
130 /**
131 * The IPv6 unspecified address for wildcard binding.
132 */
133 public val IPV6_ANY: IpAddress = IpAddress(NativeSocketAddress(6, 0, 0u, 0u, 0u, 0u, 0u))
134 /**
135 * The IPv6 loopback address.
136 */
137 public val IPV6_LOOPBACK: IpAddress =
138 IpAddress(NativeSocketAddress(6, 0, 0u, 0u, 0u, 0u, 1u))
140 /**
141 * Creates an address from exactly four or sixteen network-order bytes and a compatible
142 * scope identifier.
143 */
144 public fun fromByteArray(bytes: List<Byte>, scopeId: UInt = 0u): IpAddress {
145 if (bytes.size != 4L && bytes.size != 16L)
146 throw IllegalArgumentException(Some("IP address requires 4 or 16 bytes"))
147 if (bytes.size == 4L && scopeId != 0u)
148 throw IllegalArgumentException(Some("IPv4 does not have a scope"))
149 val snapshot = bytes.toArray()
150 return IpAddress(
151 NativeSocketAddress(
152 if (snapshot.size == 4L)
153 4
154 else
155 6,
156 0,
157 scopeId,
158 word(snapshot, 0L),
159 word(snapshot, 4L),
160 word(snapshot, 8L),
161 word(snapshot, 12L)
162 )
163 )
164 }
166 /**
167 * Parses a numeric IP literal without resolving a DNS name.
168 */
169 public fun parse(text: String): IpAddress {
170 val separator = text.indexOf('%')
171 val literal =
172 if (separator < 0L)
173 text
174 else
175 text.slice(0L, separator)
176 val scope =
177 if (separator < 0L)
178 0.toUInt()
179 else
180 decimal(text.slice(separator + 1L, text.charLength))
181 @Unsafe
182 {
183 val parsed =
184 OsString.fromString(literal).withNativeString {
185 pointer -> @Unsafe
186 {
187 return@withNativeString nativeParse(pointer)
188 }
189 }
190 if (parsed.family == 0 || (separator >= 0L && parsed.family != 6)) {
191 throw IllegalArgumentException(Some("invalid IP address"))
192 }
193 return IpAddress(
194 NativeSocketAddress(
195 parsed.family,
196 0,
197 scope,
198 parsed.a,
199 parsed.b,
200 parsed.c,
201 parsed.d
202 )
203 )
204 }
205 }
207 internal fun fromNative(value: NativeSocketAddress): IpAddress =
208 IpAddress(
209 NativeSocketAddress(
210 value.family,
211 0,
212 value.scopeId,
213 value.a,
214 value.b,
215 value.c,
216 value.d
217 )
218 )
219 }
222private fun word(bytes: Array<Byte>, offset: Long): UInt {
223 if (offset >= bytes.size)
224 return 0u
225 var value: UInt = 0u
226 for (index in offset ..< offset + 4L)
227 value = (value shl 8L) or bytes[index].toUByte().toUInt()
228 return value
231internal fun decimal(text: String): UInt {
232 if (text.charLength == 0L)
233 throw IllegalArgumentException(Some("empty address number"))
234 for (byte in text.toByteArray()) {
235 if (byte < 48 || byte > 57)
236 throw IllegalArgumentException(Some("invalid address number"))
237 }
238 return text.toUIntOrNone() ?: throw IllegalArgumentException(Some("address number overflow"))
241@Extern(name = "scoop_platform_ip_parse")
242private fun nativeParse(text: Ptr<Byte>): NativeSocketAddress
244@Extern(name = "scoop_platform_ip_format")
245private fun nativeFormat(address: NativeSocketAddress, text: Ptr<Byte>): Int