uint convS2M(Fcall *f, uchar *ap, uint nap)
uint convD2M(Dir *d, uchar *ap, uint nap)
uint convM2S(uchar *ap, uint nap, Fcall *f)
uint convM2D(uchar *ap, uint nap, Dir *d, char *strs)
int read9pmsg(int fd, uchar *buf, uint nbuf)
int statcheck(uchar *buf, uint nbuf)
uint sizeS2M(Fcall *f)
uint sizeD2M(Dir *d)
routines convert messages in the machine-independent format of
the Plan 9 file protocol,
9P, to and from a more convenient form,
#define MAXWELEM 16
u32intmsize; /* Tversion, Rversion */
char *version; /* Tversion, Rversion */
ushortoldtag; /* Tflush */
char *ename; /* Rerror */
Qid qid; /* Rattach, Ropen, Rcreate */
u32intiounit; /* Ropen, Rcreate */
Qid aqid; /* Rauth */
u32intafid; /* Tauth, Tattach */
char *uname; /* Tauth, Tattach */
char *aname; /* Tauth, Tattach */
u32intperm; /* Tcreate */
char *name; /* Tcreate */
uchar mode; /* Tcreate, Topen */
u32intnewfid; /* Twalk */
ushortnwname; /* Twalk */
char *wname[MAXWELEM]; /* Twalk */
ushortnwqid; /* Rwalk */
Qid wqid[MAXWELEM]; /* Rwalk */
vlong offset; /* Tread, Twrite */
u32intcount; /* Tread, Twrite, Rread */
char *data; /* Twrite, Rread */
ushortnstat; /* Twstat, Rstat */
uchar *stat; /* Twstat, Rstat */
/* these are implemented as macros */
void PBIT8(uchar*, uchar)
void PBIT16(uchar*, ushort)
void PBIT32(uchar*, ulong)
void PBIT64(uchar*, vlong)
#define BIT8SZ 1
#define BIT16SZ 2
#define BIT32SZ 4
#define BIT64SZ 8
This structure is defined in
<fcall.h>. See section 5
for a full description of 9P messages and their encoding.
For all message types, the
type field of an
Fcall holds one of
Rattach, etc. (defined in an enumerated type in
Fid is used by most messages, and
tag is used by all messages.
The other fields are used selectively by the message types
given in comments.
ConvM2S takes a 9P message at
ap of length
nap, and uses it to fill in
f. If the passed message
including any data for
is formatted properly,
the return value is the number of bytes the message occupied in the buffer
ap, which will always be less than or equal to
nap; otherwise it is 0.
data is set to a pointer into the argument message,
not a copy.
ConvS2M does the reverse conversion, turning
f into a message starting at
ap. The length of the resulting message is returned.
count bytes starting at
data are copied into the message.
IOHDRSZ is a suitable amount of buffer to reserve for storing
the 9P header;
the data portion of a
Rread will be no more than the buffer size negotiated in the
Tversion/Rversion exchange, minus
sizeS2M returns the number of bytes required to store the machine-independent representation of the
f, including its initial 32-bit size field.
In other words, it reports the number of bytes produced
by a successful call to
Another structure is
Dir, used by the routines described in
ConvM2D converts the machine-independent form starting at
d and returns the length of the machine-independent encoding.
The strings in the returned
Dir structure are stored at successive locations starting at
strs will point to storage immediately after the
It can also be a
nil pointer, in which case the string pointers in the returned
Dir are all
nil; however, the return value still includes their length.
ConvD2M does the reverse translation,
also returning the length of the encoding.
If the buffer is too short, the return value will be
BIT16SZ and the correct size will be returned in the first
(If the buffer is less that
BIT16SZ, the return value is zero; therefore a correct test for
complete packing of the message is that the return value is
BIT16SZ). The macro
GBIT16 can be used to extract the correct value.
The related macros with different sizes retrieve the corresponding-sized quantities.
PBIT16 and its brethren place values in messages.
With the exception of handling short buffers in
convD2M, these macros are not usually needed except by internal routines.
sizeD2M returns the number of bytes required to store the machine-independent representation of the
d, including its initial 16-bit size field.
statcheck checks whether the
nbuf bytes of
buf contain a validly formatted machine-independent
Dir entry suitable as an argument, for example, for the
It checks that the sizes of all the elements of the the entry sum to exactly
nbuf, which is a simple but effective test of validity.
buf should include the second two-byte (16-bit) length field that precedes the entry when
formatted in a 9P message (see
in other words,
nbuf is 2 plus the sum of the sizes of the entry itself.
Statcheck also verifies that the length field has the correct value (that is,
nbuf-2). It returns
0 for a valid entry and
-1 for an incorrectly formatted entry.
dirmodefmt are formatting routines, suitable for
long values into string representations of the directory buffer,
or file mode value.
Fcallfmt assumes that
dirfmt has been installed with format letter
dirmodefmt with format letter
multiple times, if necessary, to read an entire 9P message into
buf. The return value is 0 for end of file, or -1 for error; it does not return