fcall 6.9 KB

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