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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
.TH 9P-NINEP 2
.SH NAME
Ninep: Rmsg, Tmsg, dir2text, istmsg, packdir, packdirsize, readmsg, qid2text, unpackdir \- interface to 9P file protocol
.SH SYNOPSIS
.EX
include "9p.m";
ninep := load Ninep Ninep->PATH;
Tmsg: adt {
tag: int;
pick {
Readerror =>
error: string; # tag is unused in this case
Version =>
msize: int;
version: string;
Auth =>
afid: int;
uname, aname: string;
Attach =>
fid, afid: int;
uname, aname: string;
Flush =>
oldtag: int;
Walk =>
fid, newfid: int;
names: array of string;
Open =>
fid, mode: int;
Create =>
fid: int;
name: string;
perm, mode: int;
Read =>
fid: int;
offset: big;
count: int;
Write =>
fid: int;
offset: big;
data: array of byte;
Clunk or
Stat or
Remove =>
fid: int;
Wstat =>
fid: int;
stat: Sys->Dir;
}
read: fn(fd: ref Sys->FD, msize: int): ref Tmsg;
unpack: fn(a: array of byte): (int, ref Tmsg);
pack: fn(nil: self ref Tmsg): array of byte;
packedsize: fn(nil: self ref Tmsg): int;
text: fn(nil: self ref Tmsg): string;
mtype: fn(nil: self ref Tmsg): int;
};
Rmsg: adt {
tag: int;
pick {
Readerror =>
error: string; # tag is unused in this case
Version =>
msize: int;
version: string;
Auth =>
aqid: Sys->Qid;
Attach =>
qid: Sys->Qid;
Flush =>
Error =>
ename: string;
Clunk or
Remove or
Wstat =>
Walk =>
qids: array of Sys->Qid;
Create or
Open =>
qid: Sys->Qid;
iounit: int;
Read =>
data: array of byte;
Write =>
count: int;
Stat =>
stat: Sys->Dir;
}
read: fn(fd: ref Sys->FD, msize: int): ref Rmsg;
unpack: fn(a: array of byte): (int, ref Rmsg);
pack: fn(nil: self ref Rmsg): array of byte;
packedsize: fn(nil: self ref Rmsg): int;
text: fn(nil: self ref Rmsg): string;
mtype: fn(nil: self ref Rmsg): int;
};
init: fn();
readmsg: fn(fd: ref Sys->FD, msize: int): (array of byte, string);
istmsg: fn(f: array of byte): int;
compatible: fn(t: ref Tmsg.Version, msize: int, version: string): (int, string);
packdirsize: fn(d: Sys->Dir): int;
packdir: fn(d: Sys->Dir): array of byte;
unpackdir: fn(f: array of byte): (int, Sys->Dir);
dir2text: fn(d: Sys->Dir): string;
qid2text: fn(q: Sys->Qid): string;
VERSION: con "9P2000";
MAXWELEM: con 16;
NOTAG: con 16rFFFF;
NOFID: con ~0;
IOHDRSZ: con \fIimplementation-defined\f5;
DEFMSIZE: con \fIimplementation-defined\f5;
.EE
.SH DESCRIPTION
.B Ninep
provides a Limbo interface to send and receive messages of the 9P file service protocol,
described by Section 5 of this manual, a thorough reading of which
is advised before using this module.
.B Init
must be called before using any other function in the module.
.PP
A 9P client transmits requests to a server as `T-messages'
and receives replies in matching `R-messages'.
A T-message is here represented by values of the type
.BR Tmsg ,
and an R-message by values of type
.BR Rmsg .
Every message has a
.B tag
value, and the alternatives of the pick adt represent the possible operation types of a T-message,
generally with parameter names and types corresponding to those described in section 5.
The exceptions are:
.B Tmsg.Write
and
.B Rmsg.Read
contain an array of byte,
.BR data ,
to hold the data for the corresponding message, and the `count' parameter of the message is simply the length of that array;
and there is an alternative labelled
.B Readerror
that does not appear in the protocol but is used to represent input errors as described below.
Also note that values that are `unsigned' integers in the protocol are typically given signed integer
types in the Limbo representation (in particular, fids, qid paths, counts and offsets), and applications
should take appropriate care when manipulating them.
.PP
The following functions are provided by
.BR Tmsg:
.TP
.BI read( fd\fP,\fP\ msize )
Read file descriptor
.I fd
to obtain exactly one T-message and return (a reference to) the corresponding
.BR Tmsg .
A nil value is returned on end of file.
Otherwise, if the read fails or the data read does not form a valid T-message,
the value returned will be a
.B Tmsg.Readerror
value in which the
.B error
member describes the error.
.I Msize
gives the maximum number of bytes in any acceptable T-message,
and should be the value negotiated in the exchange of
.B version
messages;
any incoming message larger than that will result in a diagnostic as a
.B Tmsg.Readerror
value.
An
.I msize
of 0 means `no limit negotiated' and should (only) be used until a message size
has been negotiated by exchange of
.IR version (5)
messages.
.TP
.IB t .pack()
Return an array of bytes containing the value of
.I t
in the machine-independent format described in Section 5.
It can return nil only if the message
.I t
is itself nil or has an invalid type.
.TP
.BI unpack( a )
The array
.I a
is assumed to contain zero or more T-messages.
.B Unpack
attempts to unpack the first message, and returns a tuple of the form
.RI ( n , v ).
If successful,
.I n
is the number of bytes at the start of
.I a
used by the message, and
.I v
is the corresponding
.B Tmsg
value.
If
.I a
contains the prefix of a valid message but more data is required to complete it,
.I n
is zero (and
.I v
is nil); the caller will typically read more data, append it to
.IR a ,
and try again.
If the message is invalid,
.I n
is -1
and
.I v
is nil.
.TP
.IB t .packedsize()
Return the number of bytes required for the value of
.I t
when packed in its machine-independent format.
(Zero is returned if
.I t
is invalid.)
.TP
.IB t .text()
Return a printable string showing the contents of
.IR t ,
for tracing or debugging.
.TP
.IB t .mtype()
Return the 9P message type of the message.
.PP
An R-message is represented by
.BR Rmsg .
Its member functions behave exactly as those for
.BR Tmsg ,
except that they operate on R-messages not T-messages.
.PP
When a client reads a directory, the data returned in the reply must be formatted
as described in
.IR read (5):
an array of directory entries, one per file, with each entry formatted in
a machine-independent format.
An appropriate array value can be produced by
.B packdir
from a
.B Sys->Dir
structure, as used by
.IR sys-stat (2).
The space that packed representation will take can be calculated beforehand by
.BR packdirsize .
The server will usually fill the buffer for the reply to the read
with as many entries as will fit,
checking the space remaining against the result of
.B packdirsize
and if the value will fit, storing the result of
.BR packdir .
Given an array
.I a
containing at most one packed directory value (as produced by
.BR packdir ),
.B unpackdir
returns a tuple
.RI ( n,\ d )
where
.I n
is \-1 if
.I a
is illegally formatted;
.I n
is zero if
.I a
does not contain a complete directory entry value;
and otherwise
.I n
is the number of bytes of
.I a
used to produce the unpacked
.B Dir
value
.I d .
.PP
The functions
.B dir2text
and
.B qid2text
produce printable strings showing the contents of the corresponding data structures,
for use when tracing or debugging.
.PP
Applications that acts as file servers will read T-messages and
reply with R-messages.
They can use
.B Tmsg.read
to read each T-message, build an
.B Rmsg
reply value
.IR r ,
and use
.IB r .pack
to produce an array of bytes to be written in reply by
.B Sys->write
(see
.IR sys-read (2)).
.PP
A few specialised programs might need the lower-level function
.B readmsg
that underlies
.B Tmsg.read
and
.BR Rmsg.read .
It reads a single message, which can be either a T-message or R-message,
and returns it as an array of bytes,
which can then be unpacked using
.B Tmsg.unpack
or
.BR Rmsg.unpack .
.I Msize
is the negotiated message size, or 0 meaning `no limit'.
The predicate
.B istmsg
returns true if the contents of array
.I f
looks like a packed representation of a T-message,
judging only by its
.I type
byte.
.PP
When generating the
.B version
message (see
.IR version (5)),
the constant
.B NOTAG
can be used in
.B Tmsg.tag
and
.B Rmsg.tag
to represent `no tag value'.
The constant
.B VERSION
names the current version of the protocol, and can be
used as the value of
.BR Tmsg.version .
.PP
.B Compatible
can be used by a server to
compare its
.I msize
and
.I version
(which is typically
.BR VERSION )
to those in the
.B Tmsg.Version
message received from a client, to decide its reply,
following the rules in
.IR version (5).
It returns a tuple
.RI ( m ", " v )
with values for use in the
.B Rmsg.Version
reply.
.I M
is the lesser of
.I msize
and
.IB t .msize ,
and
.I v
is the negotiated protocol version, or the value \f5"unknown"\f1
if no version could be agreed.
The constant
.B DEFMSIZE
is a reasonable value for
.I msize
on current systems.
The resulting value
.I m
can subsequently be given to the various read functions as the limit
.IR msize .
The constant
.B IOHDRSZ
gives the amount to allow for protocol overhead, when limiting data size for
.B Tmsg.Write
and
.BR Rmsg.Read .
.PP
The constant
.B NOFID
can be used as the value of
.B afid
of the
.B attach
message when authentication is not required (see
.IR attach (5)).
.PP
The constant
.B MAXWELEM
gives the protocol-defined limit on the length of the arrays
.B Tmsg.names
and
.BR Rmsg.qids .
For specialised applications, the module defines constants
.BR Tversion ,
.BR Rversion ,
etc. for the message types of the protocol, and the
other constants mentioned in Section 5.
.SH SOURCE
.B /appl/lib/ninep.b
.SH SEE ALSO
.IR styxservers (2),
.IR intro (5)
|