summaryrefslogtreecommitdiff
path: root/src/main/kotlin/Utils.kt
diff options
context:
space:
mode:
authorEdoardo La Greca2025-06-18 19:14:50 +0200
committerEdoardo La Greca2025-06-18 19:14:50 +0200
commit5a2c35806d78d12d60d49511af2da6a21a91964b (patch)
tree2f1d2d9cd11976e6b488a022ec1d0c5f322a48dd /src/main/kotlin/Utils.kt
parent13421c537773cae59a4ca1fcc798ed3820634139 (diff)
add all source code
Diffstat (limited to 'src/main/kotlin/Utils.kt')
-rw-r--r--src/main/kotlin/Utils.kt47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main/kotlin/Utils.kt b/src/main/kotlin/Utils.kt
new file mode 100644
index 0000000..2f5dbd4
--- /dev/null
+++ b/src/main/kotlin/Utils.kt
@@ -0,0 +1,47 @@
+import java.net.InetAddress
+
+const val DEFAULT_9P_PORT: UShort = 564u
+const val DEFAULT_9P_TLS_PORT: UShort = 17020u
+
+/**
+ * The [nineAddressToValues] function translates an address from one of the dial(2) formats into the respective data.
+ * For the sake of my sanity, the following list highlights differences from the typical dial(2) formats:
+ * 1. The "network" part is ignored.
+ * 2. If nothing is specified in the place of "service", the default unencrypted 9P port is used: 564.
+ * 3. "Service" can only contain port numbers.
+ * All the rest is the same.
+ *
+ * @return A Pair (A, P) containing the host address (A) and the port (P).
+ *
+ * @throws IllegalArgumentException If the address is improperly formatted or has invalid values.
+ * @throws NumberFormatException If the port's value can't be parsed into an [UShort].
+ */
+fun nineAddressToValues(nineAddress: String): Pair<String, UShort> {
+ val parts = nineAddress.split('!', limit = 3)
+ val address: String
+ val port: UShort
+ when (parts.size) {
+ 1 -> {
+ address = parts[0]
+ port = DEFAULT_9P_PORT
+ }
+ 2 -> {
+ address = parts[1]
+ port = DEFAULT_9P_PORT
+ }
+ 3 -> {
+ address = parts[1]
+ port = parts[2].toUShort()
+ }
+ else -> throw IllegalArgumentException()
+ }
+
+ return Pair(address, port)
+}
+
+/**
+ * Handy function that initializes the 9P connection.
+ */
+fun initNineP(npconn: NinePConnection) {
+ // TODO: implement
+} \ No newline at end of file