blob: fe696a4ba74a411d2547f7dc780782b9e4742ec7 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH FCALL 3
2.SH NAME
3Fcall, convS2M, convD2M, convM2S, convM2D, fcallfmt, dirfmt, dirmodefmt, read9pmsg, statcheck, sizeS2M, sizeD2M \- interface to Plan 9 File protocol
4.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.br
9.br
10.B #include <fcall.h>
11.PP
12.B
13uint convS2M(Fcall *f, uchar *ap, uint nap)
14.PP
15.B
16uint convD2M(Dir *d, uchar *ap, uint nap)
17.PP
18.B
19uint convM2S(uchar *ap, uint nap, Fcall *f)
20.PP
21.B
22uint convM2D(uchar *ap, uint nap, Dir *d, char *strs)
23.PP
24.B
25int dirfmt(Fmt*)
26.PP
27.B
28int fcallfmt(Fmt*)
29.PP
30.B
31int dirmodefmt(Fmt*)
32.PP
33.B
34int read9pmsg(int fd, uchar *buf, uint nbuf)
35.PP
36.B
37int statcheck(uchar *buf, uint nbuf)
38.PP
39.B
40uint sizeS2M(Fcall *f)
41.PP
42.B
43uint sizeD2M(Dir *d)
44.SH DESCRIPTION
45These
46routines convert messages in the machine-independent format of
47the Plan 9 file protocol,
489P, to and from a more convenient form,
49an
50.B Fcall
51structure:
52.PP
53.EX
54.if n .ta 4n +6n +5n +6n +18n +4n
55.if t .ta \w'xxxx'u +\w'short 'u +\w'xxxx'u +\w'ushort 'u +\w'ticket[TICKETLEN]; 'u +\w'/* 'u
56#define MAXWELEM 16
57
58typedef
59struct Fcall
60{
61 uchar type;
62 u32int fid;
63 ushort tag;
64 union {
65 struct {
66 u32int msize; /* Tversion, Rversion */
67 char *version; /* Tversion, Rversion */
68 };
69 struct {
70 ushort oldtag; /* Tflush */
71 };
72 struct {
73 char *ename; /* Rerror */
74 };
75 struct {
76 Qid qid; /* Rattach, Ropen, Rcreate */
77 u32int iounit; /* Ropen, Rcreate */
78 };
79 struct {
80 Qid aqid; /* Rauth */
81 };
82 struct {
83 u32int afid; /* Tauth, Tattach */
84 char *uname; /* Tauth, Tattach */
85 char *aname; /* Tauth, Tattach */
86 };
87 struct {
88 u32int perm; /* Tcreate */
89 char *name; /* Tcreate */
90 uchar mode; /* Tcreate, Topen */
91 };
92 struct {
93 u32int newfid; /* Twalk */
94 ushort nwname; /* Twalk */
95 char *wname[MAXWELEM]; /* Twalk */
96 };
97 struct {
98 ushort nwqid; /* Rwalk */
99 Qid wqid[MAXWELEM]; /* Rwalk */
100 };
101 struct {
102 vlong offset; /* Tread, Twrite */
103 u32int count; /* Tread, Twrite, Rread */
104 char *data; /* Twrite, Rread */
105 };
106 struct {
107 ushort nstat; /* Twstat, Rstat */
108 uchar *stat; /* Twstat, Rstat */
109 };
110 };
111} Fcall;
112.EE
113.EX
114
115/* these are implemented as macros */
116
117uchar GBIT8(uchar*)
118ushort GBIT16(uchar*)
119ulong GBIT32(uchar*)
120vlong GBIT64(uchar*)
121
122void PBIT8(uchar*, uchar)
123void PBIT16(uchar*, ushort)
124void PBIT32(uchar*, ulong)
125void PBIT64(uchar*, vlong)
126
127#define BIT8SZ 1
128#define BIT16SZ 2
129#define BIT32SZ 4
130#define BIT64SZ 8
131.EE
132.PP
133This structure is defined in
134.BR <fcall.h> .
135See section 5
136for a full description of 9P messages and their encoding.
137For all message types, the
138.B type
139field of an
140.B Fcall
141holds one of
142.BR Tversion ,
143.BR Rversion ,
144.BR Tattach ,
145.BR Rattach ,
146etc. (defined in an enumerated type in
147.BR <fcall.h> ).
148.B Fid
149is used by most messages, and
150.B tag
151is used by all messages.
152The other fields are used selectively by the message types
153given in comments.
154.PP
155.I ConvM2S
156takes a 9P message at
157.I ap
158of length
159.IR nap ,
160and uses it to fill in
161.B Fcall
162structure
163.IR f .
164If the passed message
165including any data for
166.B Twrite
167and
168.B Rread
169messages
170is formatted properly,
171the return value is the number of bytes the message occupied in the buffer
172.IR ap ,
173which will always be less than or equal to
174.IR nap ;
175otherwise it is 0.
176For
177.B Twrite
178and
179.B Tread
180messages,
181.B data
182is set to a pointer into the argument message,
183not a copy.
184.PP
185.I ConvS2M
186does the reverse conversion, turning
187.I f
188into a message starting at
189.IR ap .
190The length of the resulting message is returned.
191For
192.B Twrite
193and
194.B Rread
195messages,
196.B count
197bytes starting at
198.B data
199are copied into the message.
200.PP
201The constant
202.B IOHDRSZ
203is a suitable amount of buffer to reserve for storing
204the 9P header;
205the data portion of a
206.B Twrite
207or
208.B Rread
209will be no more than the buffer size negotiated in the
210.BR Tversion/Rversion
211exchange, minus
212.BR IOHDRSZ .
213.PP
214The routine
215.I sizeS2M
216returns the number of bytes required to store the machine-independent representation of the
217.B Fcall
218structure
219.IR f ,
220including its initial 32-bit size field.
221In other words, it reports the number of bytes produced
222by a successful call to
223.IR convS2M .
224.PP
225Another structure is
226.BR Dir ,
227used by the routines described in
rscbf8a59f2004-04-11 03:42:27 +0000228.IR stat (3).
rsccfa37a72004-04-10 18:53:55 +0000229.I ConvM2D
230converts the machine-independent form starting at
231.I ap
232into
233.IR d
234and returns the length of the machine-independent encoding.
235The strings in the returned
236.B Dir
237structure are stored at successive locations starting at
238.BR strs .
239Usually
240.B strs
241will point to storage immediately after the
242.B Dir
243itself.
244It can also be a
245.B nil
246pointer, in which case the string pointers in the returned
247.B Dir
248are all
249.BR nil ;
250however, the return value still includes their length.
251.PP
252.I ConvD2M
253does the reverse translation,
254also returning the length of the encoding.
255If the buffer is too short, the return value will be
256.B BIT16SZ
257and the correct size will be returned in the first
258.B BIT16SZ
259bytes.
260(If the buffer is less that
261.BR BIT16SZ ,
262the return value is zero; therefore a correct test for
263complete packing of the message is that the return value is
264greater than
265.BR BIT16SZ ).
266The macro
267.B GBIT16
268can be used to extract the correct value.
269The related macros with different sizes retrieve the corresponding-sized quantities.
270.B PBIT16
271and its brethren place values in messages.
272With the exception of handling short buffers in
273.IR convD2M ,
274these macros are not usually needed except by internal routines.
275.PP
276Analogous to
277.IR sizeS2M ,
278.I sizeD2M
279returns the number of bytes required to store the machine-independent representation of the
280.B Dir
281structure
282.IR d ,
283including its initial 16-bit size field.
284.PP
285The routine
286.B statcheck
287checks whether the
288.I nbuf
289bytes of
290.I buf
291contain a validly formatted machine-independent
292.B Dir
293entry suitable as an argument, for example, for the
294.B wstat
295(see
rscbf8a59f2004-04-11 03:42:27 +0000296.IR stat (3))
rsccfa37a72004-04-10 18:53:55 +0000297system call.
298It checks that the sizes of all the elements of the the entry sum to exactly
299.IR nbuf ,
300which is a simple but effective test of validity.
301.I Nbuf
302and
303.I buf
304should include the second two-byte (16-bit) length field that precedes the entry when
305formatted in a 9P message (see
rsc058b0112005-01-03 06:40:20 +0000306.IR stat (9p));
rsccfa37a72004-04-10 18:53:55 +0000307in other words,
308.I nbuf
309is 2 plus the sum of the sizes of the entry itself.
310.I Statcheck
311also verifies that the length field has the correct value (that is,
312.IB nbuf -2\f1).
313It returns
314.B 0
315for a valid entry and
316.B -1
317for an incorrectly formatted entry.
318.PP
319.IR Dirfmt ,
320.IR fcallfmt ,
321and
322.I dirmodefmt
323are formatting routines, suitable for
rscbf8a59f2004-04-11 03:42:27 +0000324.IR fmtinstall (3).
rsccfa37a72004-04-10 18:53:55 +0000325They convert
326.BR Dir* ,
327.BR Fcall* ,
328and
329.BR long
330values into string representations of the directory buffer,
331.B Fcall
332buffer,
333or file mode value.
334.I Fcallfmt
335assumes that
336.I dirfmt
337has been installed with format letter
338.L D
339and
340.I dirmodefmt
341with format letter
342.LR M .
343.PP
344.I Read9pmsg
345calls
rscbf8a59f2004-04-11 03:42:27 +0000346.IR read (3)
rsccfa37a72004-04-10 18:53:55 +0000347multiple times, if necessary, to read an entire 9P message into
348.BR buf .
349The return value is 0 for end of file, or -1 for error; it does not return
350partial messages.
351.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000352.B \*9/src/lib9
rsccfa37a72004-04-10 18:53:55 +0000353.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +0000354.IR intro (3),
355.IR 9p (3),
356.IR stat (3),
rsc058b0112005-01-03 06:40:20 +0000357.IR intro (9p)