1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
import except.MsizeValueTooBigException
import except.RErrorException
import except.UnaccessibleFileException
import except.UnknownVersionException
import net.InMessage
import net.OutMessage
import net.TransportLayer
import java.io.IOException
import java.math.BigInteger
/**
* This class represents a 9P connection. It provides a practical implementation with networking to the 9P methods
* described in [ProtocolTranslator]. Details about methods related to 9P can be found in [ProtocolTranslator]. Remember
* to disconnect using [disconnect] after use.
*
* Details about network-related topics can be found in [net.TransportLayer] and the implementation of choice.
*
* Details about 9P messages and methods can be found in [ProtocolTranslator].
*
* @param transLay The networking API backend of choice.
* @throws except.UnresolvableHostException if the host resolution made by [transLay] failed.
*/
class Connection(transLay: TransportLayer) : ProtocolTranslator {
/**
* Networking API.
*/
private val tl: TransportLayer = transLay
/**
* Tag generator.
*/
private val tagGen = TagGenerator(TagGenerator.TagGenerationMethod.INCREMENTAL, 1u)
/**
* Maximum size for messages negotiated between the client and the server.
*/
private var maxSize: UInt = 0u
// 9P constants.
val DEFAULT_VERSION = "9P2000"
val NOTAG = 0.toUShort().inv()
val NOFID = 0.toUInt().inv()
/**
* Disconnect from the remote host,
*
* @throws IOException if an I/O error occurred while closing the socket.
*/
fun disconnect() {
this.tl.close()
}
/**
* Handy function to create an [net.InMessage] instance and check for errors. After successfully using this
* function, it is guaranteed that both no error occurred while reading the incoming message and the message is not
* of type R-error.
*
* It uses [tl] and [maxSize] for instancing the [net.InMessage] class.
*
* @return A pair of: (1) a nullable string (which can be: `null` if no error occurred, empty if an error occurred
* with no message, or non-empty with the error message) and (2) the optional [net.InMessage] instance (null if an
* error occurred).
* @throws except.InvalidMessageException if the received message is invalid.
* @throws except.RErrorException if the received message is an R-error message.
*/
private fun checkedInMessage(reqTag: UShort): InMessage {
val imsg = InMessage(this.tl, this.maxSize, reqTag)
if (imsg.type == NinePMessageType.RERROR) {
imsg.applyField(InMessage.Field("ename", InMessage.Field.Type.STRING, 0u))
throw RErrorException(imsg.fieldsStr["ename"])
}
return imsg
}
override fun version(msize: UInt, version: String) {
val omsg = OutMessage(NinePMessageType.TVERSION, this.NOTAG, listOf("msize", "version"),
mapOf(
"msize" to Pair(BigInteger(msize.toString()), 4u)
),
mapOf(
"version" to version
),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
imsg.applySchema(listOf(
InMessage.Field("msize", InMessage.Field.Type.INTEGER, 4u),
InMessage.Field("version", InMessage.Field.Type.STRING, 0u)
))
val remoteMaxSize = imsg.fieldsInt["msize"]!!.toInt().toUInt()
if (remoteMaxSize > this.maxSize) {
throw MsizeValueTooBigException(msize, remoteMaxSize)
}
this.maxSize = remoteMaxSize
if (!imsg.fieldsStr["version"]!!.startsWith("9P2000")) {
throw UnknownVersionException(imsg.fieldsStr["version"]!!)
}
}
override fun auth(afid: UInt, uname: String, aname: String) {
}
override fun flush(oldtag: UShort) {
val omsg = OutMessage(NinePMessageType.TFLUSH, this.tagGen.generate(), listOf("oldtag"),
mapOf(
"oldtag" to Pair(BigInteger(oldtag.toString()), 2u)
),
emptyMap(),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
}
override fun attach(fid: UInt, afid: UInt, uname: String, aname: String): QID {
val omsg = OutMessage(NinePMessageType.TATTACH, this.tagGen.generate(), listOf("fid", "afid", "uname", "aname"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u),
"afid" to Pair(BigInteger(afid.toString()), 4u)
),
mapOf(
"uname" to uname,
"aname" to aname
),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
imsg.applyField(InMessage.Field("qid", InMessage.Field.Type.RAW, 13u))
val qid = QID(imsg.fieldsRaw["qid"]!!.toList())
return qid
}
override fun walk(fid: UInt, newfid: UInt, wname: List<String>): List<QID> {
val nwname = wname.size
val omsg = OutMessage(NinePMessageType.TWALK, this.tagGen.generate(), listOf("fid", "newfid", "nwname"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u),
"newfid" to Pair(BigInteger(newfid.toString()), 4u),
"nwname" to Pair(BigInteger(nwname.toString()), 2u)
),
emptyMap(),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
for (wn in wname) { // write wname elements
this.tl.transmit(OutMessage.convStringToBytes(wn))
}
val imsg = checkedInMessage(omsg.tag)
imsg.applyField(InMessage.Field("nwqid", InMessage.Field.Type.INTEGER, 2u))
val nwqid = imsg.fieldsInt["nwqid"]!!.toInt()
if (nwqid < nwname) {
throw UnaccessibleFileException(wname.slice(0..nwqid))
}
imsg.applyField(InMessage.Field("qids", InMessage.Field.Type.RAW, (nwqid * 13).toUInt()))
val rawQids = imsg.fieldsRaw["qids"]!!
val qids: MutableList<QID> = mutableListOf()
for (i in 0..<nwqid/13) {
val start = i * 13
val end = ((i+1) * 13) - 1
val rawQid = rawQids.slice(start..end)
qids += QID(rawQid)
}
return qids.toList()
}
override fun open(fid: UInt, mode: FileMode): Pair<QID, UInt> {
val omsg = OutMessage(NinePMessageType.TOPEN, this.tagGen.generate(), listOf("fid", "mode"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u),
"mode" to Pair(BigInteger(mode.toModeByte().toString()), 1u)
),
emptyMap(),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
imsg.applySchema(listOf(
InMessage.Field("qid", InMessage.Field.Type.RAW, 13u),
InMessage.Field("iounit", InMessage.Field.Type.INTEGER, 4u)
))
val qid = QID(imsg.fieldsRaw["qid"]!!.toList())
val iounit = imsg.fieldsInt["iounit"]!!.toInt().toUInt()
return Pair(qid, iounit)
}
override fun create(fid: UInt, name: String, perm: FilePermissions, mode: FileMode): Pair<QID, UInt> {
val omsg = OutMessage(NinePMessageType.TCREATE, this.tagGen.generate(), listOf("fid", "name", "perm", "mode"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u),
"perm" to Pair(BigInteger(perm.toPermissionInt().toString()), 4u),
"mode" to Pair(BigInteger(mode.toModeByte().toString()), 1u)
),
mapOf(
"name" to name
),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
imsg.applySchema(listOf(
InMessage.Field("qid", InMessage.Field.Type.RAW, 13u),
InMessage.Field("iounit", InMessage.Field.Type.INTEGER, 4u)
))
val qid = QID(imsg.fieldsRaw["qid"]!!.toList())
val iounit = imsg.fieldsInt["iounit"]!!.toInt().toUInt()
return Pair(qid, iounit)
}
override fun read(fid: UInt, offset: ULong, count: UInt): Array<UByte> {
val omsg = OutMessage(NinePMessageType.TREAD, this.tagGen.generate(), listOf("fid", "offset", "count"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u),
"offset" to Pair(BigInteger(offset.toString()), 8u),
"count" to Pair(BigInteger(count.toString()), 4u)
),
emptyMap(),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
imsg.applyField(InMessage.Field("count", InMessage.Field.Type.INTEGER, 4u))
val count = imsg.fieldsInt["count"]!!.toInt().toUInt()
imsg.applyField(InMessage.Field("data", InMessage.Field.Type.RAW, count))
return imsg.fieldsRaw["data"]!!
}
override fun write(fid: UInt, offset: ULong, count: UInt, data: Iterable<UByte>): UInt {
val data = data.take(count.toInt()).toTypedArray()
val omsg = OutMessage(NinePMessageType.TWRITE, this.tagGen.generate(), listOf("offset", "count", "data"),
mapOf(
"offset" to Pair(BigInteger(offset.toString()), 8u),
"count" to Pair(BigInteger(count.toString()), 4u)
),
emptyMap(),
mapOf(
"data" to data.toList()
),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
imsg.applyField(InMessage.Field("count", InMessage.Field.Type.INTEGER, 4u))
val count = imsg.fieldsInt["count"]!!
return count.toInt().toUInt()
}
override fun clunk(fid: UInt) {
val omsg = OutMessage(NinePMessageType.TCLUNK, this.tagGen.generate(), listOf("fid"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u)
),
emptyMap(),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
}
override fun remove(fid: UInt) {
val omsg = OutMessage(NinePMessageType.TREMOVE, this.tagGen.generate(), listOf("fid"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u)
),
emptyMap(),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
}
override fun stat(fid: UInt): Stat {
val omsg = OutMessage(NinePMessageType.TSTAT, this.tagGen.generate(), listOf("fid"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u)
),
emptyMap(),
emptyMap(),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
imsg.applyField(InMessage.Field("nstat", InMessage.Field.Type.INTEGER, 2u))
val nstat = imsg.fieldsInt["nstat"]!!.toInt().toUInt()
imsg.applyField(InMessage.Field("stat", InMessage.Field.Type.RAW, nstat))
val rawStat = imsg.fieldsRaw["stat"]!!.toList()
return Stat(fid, rawStat)
}
override fun wstat(fid: UInt, stat: Stat) {
val omsg = OutMessage(NinePMessageType.TWSTAT, this.tagGen.generate(), listOf("fid", "stat"),
mapOf(
"fid" to Pair(BigInteger(fid.toString()), 4u)
),
emptyMap(),
mapOf(
"stat" to stat.toRaw()
),
this.maxSize
)
omsg.write(this.tl)
val imsg = checkedInMessage(omsg.tag)
}
}
|